1111import org .elasticsearch .common .xcontent .XContentParser ;
1212import org .elasticsearch .test .AbstractSerializingTestCase ;
1313import org .elasticsearch .xpack .core .rollup .ConfigTestHelpers ;
14+ import org .elasticsearch .xpack .core .rollup .RollupField ;
1415
1516import java .io .IOException ;
17+ import java .util .Arrays ;
1618import java .util .Collections ;
1719import java .util .HashMap ;
1820import java .util .Map ;
1921
2022import static java .util .Collections .singletonList ;
2123import static org .hamcrest .Matchers .equalTo ;
24+ import static org .hamcrest .Matchers .isIn ;
2225import static org .mockito .Mockito .mock ;
2326import static org .mockito .Mockito .when ;
2427
@@ -45,8 +48,8 @@ public void testValidateNoMapping() {
4548
4649 MetricConfig config = new MetricConfig ("my_field" , singletonList ("max" ));
4750 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." ));
5053 }
5154
5255 public void testValidateNomatchingField () {
@@ -59,8 +62,8 @@ public void testValidateNomatchingField() {
5962
6063 MetricConfig config = new MetricConfig ("my_field" , singletonList ("max" ));
6164 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." ));
6467 }
6568
6669 public void testValidateFieldWrongType () {
@@ -73,8 +76,8 @@ public void testValidateFieldWrongType() {
7376
7477 MetricConfig config = new MetricConfig ("my_field" , singletonList ("max" ));
7578 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 () ));
7881 }
7982
8083 public void testValidateFieldMatchingNotAggregatable () {
@@ -91,6 +94,21 @@ public void testValidateFieldMatchingNotAggregatable() {
9194 assertThat (e .validationErrors ().get (0 ), equalTo ("The field [my_field] must be aggregatable across all indices, but is not." ));
9295 }
9396
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+
94112 public void testValidateMatchingField () {
95113 ActionRequestValidationException e = new ActionRequestValidationException ();
96114 Map <String , Map <String , FieldCapabilities >> responseMap = new HashMap <>();
@@ -153,6 +171,13 @@ public void testValidateMatchingField() {
153171 config = new MetricConfig ("my_field" , singletonList ("max" ));
154172 config .validateMappings (responseMap , e );
155173 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 ));
156181 }
157182
158183}
0 commit comments