5
5
*/
6
6
package org .elasticsearch .xpack .core .ml .job .config ;
7
7
8
+ import com .carrotsearch .randomizedtesting .generators .CodepointSetGenerator ;
9
+ import org .elasticsearch .ElasticsearchStatusException ;
8
10
import org .elasticsearch .common .io .stream .Writeable .Reader ;
9
11
import org .elasticsearch .common .xcontent .XContentParser ;
10
12
import org .elasticsearch .common .xcontent .json .JsonXContent ;
17
19
import static org .hamcrest .Matchers .contains ;
18
20
import static org .hamcrest .Matchers .containsString ;
19
21
import static org .hamcrest .Matchers .equalTo ;
22
+ import static org .hamcrest .Matchers .startsWith ;
20
23
21
24
public class MlFilterTests extends AbstractSerializingTestCase <MlFilter > {
22
25
@@ -30,7 +33,12 @@ protected MlFilter createTestInstance() {
30
33
}
31
34
32
35
public static MlFilter createRandom () {
33
- return createRandom (randomAlphaOfLengthBetween (1 , 20 ));
36
+ return createRandom (randomValidFilterId ());
37
+ }
38
+
39
+ public static String randomValidFilterId () {
40
+ CodepointSetGenerator generator = new CodepointSetGenerator ("abcdefghijklmnopqrstuvwxyz" .toCharArray ());
41
+ return generator .ofCodePointsLength (random (), 10 , 10 );
34
42
}
35
43
36
44
public static MlFilter createRandom (String filterId ) {
@@ -58,13 +66,13 @@ protected MlFilter doParseInstance(XContentParser parser) {
58
66
}
59
67
60
68
public void testNullId () {
61
- NullPointerException ex = expectThrows (NullPointerException .class , () -> MlFilter .builder (null ).build ());
62
- assertEquals (MlFilter . ID . getPreferredName () + " must not be null" , ex .getMessage ());
69
+ Exception ex = expectThrows (IllegalArgumentException .class , () -> MlFilter .builder (null ).build ());
70
+ assertEquals ("[filter_id] must not be null. " , ex .getMessage ());
63
71
}
64
72
65
73
public void testNullItems () {
66
74
NullPointerException ex = expectThrows (NullPointerException .class ,
67
- () -> MlFilter .builder (randomAlphaOfLength ( 20 )).setItems ((SortedSet <String >) null ).build ());
75
+ () -> MlFilter .builder (randomValidFilterId ( )).setItems ((SortedSet <String >) null ).build ());
68
76
assertEquals (MlFilter .ITEMS .getPreferredName () + " must not be null" , ex .getMessage ());
69
77
}
70
78
@@ -89,6 +97,11 @@ public void testLenientParser() throws IOException {
89
97
}
90
98
}
91
99
100
+ public void testInvalidId () {
101
+ ElasticsearchStatusException e = expectThrows (ElasticsearchStatusException .class , () -> MlFilter .builder ("Invalid id" ).build ());
102
+ assertThat (e .getMessage (), startsWith ("Invalid filter_id; 'Invalid id' can contain lowercase" ));
103
+ }
104
+
92
105
public void testItemsAreSorted () {
93
106
MlFilter filter = MlFilter .builder ("foo" ).setItems ("c" , "b" , "a" ).build ();
94
107
assertThat (filter .getItems (), contains ("a" , "b" , "c" ));
0 commit comments