11
11
import org .elasticsearch .common .xcontent .XContentParser ;
12
12
import org .elasticsearch .test .AbstractSerializingTestCase ;
13
13
import org .elasticsearch .xpack .core .rollup .ConfigTestHelpers ;
14
+ import org .elasticsearch .xpack .core .rollup .RollupField ;
14
15
15
16
import java .io .IOException ;
17
+ import java .util .Arrays ;
16
18
import java .util .Collections ;
17
19
import java .util .HashMap ;
18
20
import java .util .Map ;
19
21
20
22
import static java .util .Collections .singletonList ;
21
23
import static org .hamcrest .Matchers .equalTo ;
24
+ import static org .hamcrest .Matchers .isIn ;
22
25
import static org .mockito .Mockito .mock ;
23
26
import static org .mockito .Mockito .when ;
24
27
@@ -45,8 +48,8 @@ public void testValidateNoMapping() {
45
48
46
49
MetricConfig config = new MetricConfig ("my_field" , singletonList ("max" ));
47
50
config .validateMappings (responseMap , e );
48
- assertThat (e .validationErrors ().get (0 ), equalTo ("Could not find a [numeric] field with name [my_field] in any of the " +
49
- " indices matching the index pattern." ));
51
+ assertThat (e .validationErrors ().get (0 ), equalTo ("Could not find a [numeric] or [date] field with name [my_field] in any" +
52
+ " of the indices matching the index pattern." ));
50
53
}
51
54
52
55
public void testValidateNomatchingField () {
@@ -59,8 +62,8 @@ public void testValidateNomatchingField() {
59
62
60
63
MetricConfig config = new MetricConfig ("my_field" , singletonList ("max" ));
61
64
config .validateMappings (responseMap , e );
62
- assertThat (e .validationErrors ().get (0 ), equalTo ("Could not find a [numeric] field with name [my_field] in any of the " +
63
- " indices matching the index pattern." ));
65
+ assertThat (e .validationErrors ().get (0 ), equalTo ("Could not find a [numeric] or [date] field with name [my_field] in any" +
66
+ " of the indices matching the index pattern." ));
64
67
}
65
68
66
69
public void testValidateFieldWrongType () {
@@ -73,8 +76,8 @@ public void testValidateFieldWrongType() {
73
76
74
77
MetricConfig config = new MetricConfig ("my_field" , singletonList ("max" ));
75
78
config .validateMappings (responseMap , e );
76
- assertThat (e . validationErrors (). get ( 0 ), equalTo ( "The field referenced by a metric group must be a [numeric] type, " +
77
- " but found [keyword] for field [my_field]" ));
79
+ assertThat ("The field referenced by a metric group must be a [numeric] or [date] type," +
80
+ " but found [keyword] for field [my_field]", isIn ( e . validationErrors () ));
78
81
}
79
82
80
83
public void testValidateFieldMatchingNotAggregatable () {
@@ -91,6 +94,21 @@ public void testValidateFieldMatchingNotAggregatable() {
91
94
assertThat (e .validationErrors ().get (0 ), equalTo ("The field [my_field] must be aggregatable across all indices, but is not." ));
92
95
}
93
96
97
+ public void testValidateDateFieldUnsupportedMetric () {
98
+ ActionRequestValidationException e = new ActionRequestValidationException ();
99
+ Map <String , Map <String , FieldCapabilities >> responseMap = new HashMap <>();
100
+
101
+ // Have to mock fieldcaps because the ctor's aren't public...
102
+ FieldCapabilities fieldCaps = mock (FieldCapabilities .class );
103
+ when (fieldCaps .isAggregatable ()).thenReturn (true );
104
+ responseMap .put ("my_field" , Collections .singletonMap ("date" , fieldCaps ));
105
+
106
+ MetricConfig config = new MetricConfig ("my_field" , Arrays .asList ("avg" , "max" ));
107
+ config .validateMappings (responseMap , e );
108
+ assertThat (e .validationErrors ().get (0 ), equalTo ("Only the metrics " + RollupField .SUPPORTED_DATE_METRICS .toString () +
109
+ " are supported for [date] types, but unsupported metrics [avg] supplied for field [my_field]" ));
110
+ }
111
+
94
112
public void testValidateMatchingField () {
95
113
ActionRequestValidationException e = new ActionRequestValidationException ();
96
114
Map <String , Map <String , FieldCapabilities >> responseMap = new HashMap <>();
@@ -153,6 +171,13 @@ public void testValidateMatchingField() {
153
171
config = new MetricConfig ("my_field" , singletonList ("max" ));
154
172
config .validateMappings (responseMap , e );
155
173
assertThat (e .validationErrors ().size (), equalTo (0 ));
174
+
175
+ fieldCaps = mock (FieldCapabilities .class );
176
+ when (fieldCaps .isAggregatable ()).thenReturn (true );
177
+ responseMap .put ("my_field" , Collections .singletonMap ("date" , fieldCaps ));
178
+ config = new MetricConfig ("my_field" , singletonList ("max" ));
179
+ config .validateMappings (responseMap , e );
180
+ assertThat (e .validationErrors ().size (), equalTo (0 ));
156
181
}
157
182
158
183
}
0 commit comments