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
3 files changed +19
-0
lines changed Original file line number Diff line number Diff line change @@ -84,6 +84,10 @@ public boolean isEmpty() {
84
84
public void validate (Set <String > validKeys ) {
85
85
Optional <String > invalidKey = scope .keySet ().stream ().filter (k -> !validKeys .contains (k )).findFirst ();
86
86
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
+ }
87
91
throw ExceptionsHelper .badRequestException (Messages .getMessage (Messages .JOB_CONFIG_DETECTION_RULE_SCOPE_HAS_INVALID_FIELD ,
88
92
invalidKey .get (), validKeys ));
89
93
}
Original file line number Diff line number Diff line change @@ -95,6 +95,8 @@ public final class Messages {
95
95
"Invalid detector rule: function {0} only supports conditions that apply to time" ;
96
96
public static final String JOB_CONFIG_DETECTION_RULE_REQUIRES_SCOPE_OR_CONDITION =
97
97
"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" ;
98
100
public static final String JOB_CONFIG_DETECTION_RULE_SCOPE_HAS_INVALID_FIELD =
99
101
"Invalid detector rule: scope field ''{0}'' is invalid; select from {1}" ;
100
102
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 10
10
import org .elasticsearch .common .util .set .Sets ;
11
11
import org .elasticsearch .test .AbstractWireSerializingTestCase ;
12
12
13
+ import java .util .Collections ;
14
+
13
15
import static org .hamcrest .Matchers .contains ;
14
16
import static org .hamcrest .Matchers .equalTo ;
15
17
import static org .hamcrest .Matchers .is ;
@@ -53,6 +55,17 @@ public void testValidate_GivenMultipleValidFields() {
53
55
scope .validate (Sets .newHashSet ("foo" , "bar" , "foobar" ));
54
56
}
55
57
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
+
56
69
public void testValidate_GivenMultipleFieldsIncludingInvalid () {
57
70
RuleScope scope = RuleScope .builder ()
58
71
.include ("foo" , "filter1" )
You can’t perform that action at this time.
0 commit comments