SOLR-11985: added validation and test

This commit is contained in:
Noble Paul 2018-06-29 14:53:14 +10:00
parent c303c5f126
commit 62b9cbc6f9
3 changed files with 18 additions and 2 deletions

View File

@ -105,7 +105,7 @@ The type of the replica to which the policy rule should apply. If omitted, the r
This is a required attribute. The number of replicas that must exist to satisfy the rule. This must be one of This is a required attribute. The number of replicas that must exist to satisfy the rule. This must be one of
* a positive integer . e.g : "`3`" * a positive integer . e.g : "`3`"
* an integer with a decimal value . e.g: "`1.66`" . This means both 1 and 2 are acceptable values but the system would prefer `2` * a number with a decimal value . e.g: "`1.66`" . This means both 1 and 2 are acceptable values but the system would prefer `2`
* a number range. Such as `"3-5"` . This means `3,4,5` are acceptable values * a number range. Such as `"3-5"` . This means `3,4,5` are acceptable values
* a percentage value . e.g: `33%` . This is computed to a decimal value at runtime * a percentage value . e.g: `33%` . This is computed to a decimal value at runtime

View File

@ -345,7 +345,17 @@ public class Clause implements MapWriter, Comparable<Clause> {
@Override @Override
public String match(String val) { public String match(String val) {
if (val != null && !val.isEmpty() && val.charAt(val.length() - 1) == '%') { if (val != null && !val.isEmpty() && val.charAt(val.length() - 1) == '%') {
return val.substring(0, val.length() - 1); String newVal = val.substring(0, val.length() - 1);
double d;
try {
d = Double.parseDouble(newVal);
} catch (NumberFormatException e) {
throw new IllegalArgumentException("Invalid percentage value : " + val);
}
if (d < 0 || d > 100) {
throw new IllegalArgumentException("Percentage value must lie between [1 -100] : provided value : " + val);
}
return newVal;
} else { } else {
return null; return null;
} }

View File

@ -255,6 +255,12 @@ public class TestPolicy extends SolrTestCaseJ4 {
assertEquals(new Double(-1.0), clause.replica.delta(2)); assertEquals(new Double(-1.0), clause.replica.delta(2));
assertEquals(new Double(0.0), clause.replica.delta(4)); assertEquals(new Double(0.0), clause.replica.delta(4));
expectThrows(IllegalArgumentException.class,
() -> Clause.create((Map<String, Object>) Utils.fromJSONString("{replica: '-33%', node:'#ANY'}")));
expectThrows(IllegalArgumentException.class,
() -> Clause.create((Map<String, Object>) Utils.fromJSONString("{replica: 'x%', node:'#ANY'}")));
expectThrows(IllegalArgumentException.class,
() -> Clause.create((Map<String, Object>) Utils.fromJSONString("{replica: '20%-33%', node:'#ANY'}")));
} }