File tree Expand file tree Collapse file tree 3 files changed +19
-0
lines changed
main/java/org/elasticsearch/xpack/core/ml/job
test/java/org/elasticsearch/xpack/core/ml/job/config Expand file tree Collapse file tree 3 files changed +19
-0
lines changed Original file line number Diff line number Diff line change @@ -84,6 +84,10 @@ public boolean isEmpty() {
8484 public void validate (Set <String > validKeys ) {
8585 Optional <String > invalidKey = scope .keySet ().stream ().filter (k -> !validKeys .contains (k )).findFirst ();
8686 if (invalidKey .isPresent ()) {
87+ if (validKeys .isEmpty ()) {
88+ throw ExceptionsHelper .badRequestException (Messages .getMessage (Messages .JOB_CONFIG_DETECTION_RULE_SCOPE_NO_AVAILABLE_FIELDS ,
89+ invalidKey .get ()));
90+ }
8791 throw ExceptionsHelper .badRequestException (Messages .getMessage (Messages .JOB_CONFIG_DETECTION_RULE_SCOPE_HAS_INVALID_FIELD ,
8892 invalidKey .get (), validKeys ));
8993 }
Original file line number Diff line number Diff line change @@ -95,6 +95,8 @@ public final class Messages {
9595 "Invalid detector rule: function {0} only supports conditions that apply to time" ;
9696 public static final String JOB_CONFIG_DETECTION_RULE_REQUIRES_SCOPE_OR_CONDITION =
9797 "Invalid detector rule: at least scope or a condition is required" ;
98+ public static final String JOB_CONFIG_DETECTION_RULE_SCOPE_NO_AVAILABLE_FIELDS =
99+ "Invalid detector rule: scope field ''{0}'' is invalid; detector has no available fields for scoping" ;
98100 public static final String JOB_CONFIG_DETECTION_RULE_SCOPE_HAS_INVALID_FIELD =
99101 "Invalid detector rule: scope field ''{0}'' is invalid; select from {1}" ;
100102 public static final String JOB_CONFIG_FIELDNAME_INCOMPATIBLE_FUNCTION = "field_name cannot be used with function ''{0}''" ;
Original file line number Diff line number Diff line change 1010import org .elasticsearch .common .util .set .Sets ;
1111import org .elasticsearch .test .AbstractWireSerializingTestCase ;
1212
13+ import java .util .Collections ;
14+
1315import static org .hamcrest .Matchers .contains ;
1416import static org .hamcrest .Matchers .equalTo ;
1517import static org .hamcrest .Matchers .is ;
@@ -53,6 +55,17 @@ public void testValidate_GivenMultipleValidFields() {
5355 scope .validate (Sets .newHashSet ("foo" , "bar" , "foobar" ));
5456 }
5557
58+ public void testValidate_GivenNoAvailableFieldsForScope () {
59+ RuleScope scope = RuleScope .builder ()
60+ .include ("foo" , "filter1" )
61+ .build ();
62+ assertThat (scope .isEmpty (), is (false ));
63+
64+ ElasticsearchStatusException e = expectThrows (ElasticsearchStatusException .class , () -> scope .validate (Collections .emptySet ()));
65+ assertThat (e .getMessage (), equalTo ("Invalid detector rule: scope field 'foo' is invalid; " +
66+ "detector has no available fields for scoping" ));
67+ }
68+
5669 public void testValidate_GivenMultipleFieldsIncludingInvalid () {
5770 RuleScope scope = RuleScope .builder ()
5871 .include ("foo" , "filter1" )
You can’t perform that action at this time.
0 commit comments