Apache Lucene open-source search software
SOLR-11985: added validation and test
Noble Paul62b9cbc6f956
diff --git a/solr/solr-ref-guide/src/solrcloud-autoscaling-policy-preferences.adoc b/solr/solr-ref-guide/src/solrcloud-autoscaling-policy-preferences.adocindex 305d91a42b7..bdc10d06c5b 100644--- a/solr/solr-ref-guide/src/solrcloud-autoscaling-policy-preferences.adoc+++ b/solr/solr-ref-guide/src/solrcloud-autoscaling-policy-preferences.adoc@@ -105,7 +105,7 @@ The type of the replica to which the policy rule should apply. If omitted, the rThis 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`"-* 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 percentage value . e.g: `33%` . This is computed to a decimal value at runtimediff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/cloud/autoscaling/Clause.java b/solr/solrj/src/java/org/apache/solr/client/solrj/cloud/autoscaling/Clause.javaindex d40f0d8beea..e51158c2c6e 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/cloud/autoscaling/Clause.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/cloud/autoscaling/Clause.java@@ -345,7 +345,17 @@ public class Clause implements MapWriter, Comparable<Clause> {@Overridepublic String match(String val) {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 {return null;}diff --git a/solr/solrj/src/test/org/apache/solr/client/solrj/cloud/autoscaling/TestPolicy.java b/solr/solrj/src/test/org/apache/solr/client/solrj/cloud/autoscaling/TestPolicy.javaindex 3558b1a796e..bee7e5479e4 100644--- a/solr/solrj/src/test/org/apache/solr/client/solrj/cloud/autoscaling/TestPolicy.java+++ b/solr/solrj/src/test/org/apache/solr/client/solrj/cloud/autoscaling/TestPolicy.java@@ -255,6 +255,12 @@ public class TestPolicy extends SolrTestCaseJ4 {assertEquals(new Double(-1.0), clause.replica.delta(2));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'}")));}