2020package org .elasticsearch .ingest .common ;
2121
2222import org .elasticsearch .ElasticsearchParseException ;
23+ import org .elasticsearch .ingest .TestTemplateService ;
2324import org .elasticsearch .test .ESTestCase ;
2425import org .joda .time .DateTimeZone ;
26+ import org .junit .Before ;
2527
2628import java .util .Arrays ;
2729import java .util .Collections ;
3436
3537public class DateProcessorFactoryTests extends ESTestCase {
3638
39+ private DateProcessor .Factory factory ;
40+
41+ @ Before
42+ public void init () {
43+ factory = new DateProcessor .Factory (TestTemplateService .instance ());
44+ }
45+
3746 public void testBuildDefaults () throws Exception {
38- DateProcessor .Factory factory = new DateProcessor .Factory ();
3947 Map <String , Object > config = new HashMap <>();
4048 String sourceField = randomAlphaOfLengthBetween (1 , 10 );
4149 config .put ("field" , sourceField );
@@ -46,12 +54,11 @@ public void testBuildDefaults() throws Exception {
4654 assertThat (processor .getField (), equalTo (sourceField ));
4755 assertThat (processor .getTargetField (), equalTo (DateProcessor .DEFAULT_TARGET_FIELD ));
4856 assertThat (processor .getFormats (), equalTo (Collections .singletonList ("dd/MM/yyyyy" )));
49- assertThat (processor .getLocale (), equalTo ( Locale . ROOT ));
50- assertThat (processor .getTimezone (), equalTo ( DateTimeZone . UTC ));
57+ assertNull (processor .getLocale ());
58+ assertNull (processor .getTimezone ());
5159 }
5260
5361 public void testMatchFieldIsMandatory () throws Exception {
54- DateProcessor .Factory factory = new DateProcessor .Factory ();
5562 Map <String , Object > config = new HashMap <>();
5663 String targetField = randomAlphaOfLengthBetween (1 , 10 );
5764 config .put ("target_field" , targetField );
@@ -66,7 +73,6 @@ public void testMatchFieldIsMandatory() throws Exception {
6673 }
6774
6875 public void testMatchFormatsIsMandatory () throws Exception {
69- DateProcessor .Factory factory = new DateProcessor .Factory ();
7076 Map <String , Object > config = new HashMap <>();
7177 String sourceField = randomAlphaOfLengthBetween (1 , 10 );
7278 String targetField = randomAlphaOfLengthBetween (1 , 10 );
@@ -82,7 +88,6 @@ public void testMatchFormatsIsMandatory() throws Exception {
8288 }
8389
8490 public void testParseLocale () throws Exception {
85- DateProcessor .Factory factory = new DateProcessor .Factory ();
8691 Map <String , Object > config = new HashMap <>();
8792 String sourceField = randomAlphaOfLengthBetween (1 , 10 );
8893 config .put ("field" , sourceField );
@@ -91,39 +96,10 @@ public void testParseLocale() throws Exception {
9196 config .put ("locale" , locale .toLanguageTag ());
9297
9398 DateProcessor processor = factory .create (null , null , config );
94- assertThat (processor .getLocale ().toLanguageTag (), equalTo (locale .toLanguageTag ()));
95- }
96-
97- public void testParseInvalidLocale () throws Exception {
98- String [] locales = new String [] { "invalid_locale" , "english" , "xy" , "xy-US" };
99- for (String locale : locales ) {
100- DateProcessor .Factory factory = new DateProcessor .Factory ();
101- Map <String , Object > config = new HashMap <>();
102- String sourceField = randomAlphaOfLengthBetween (1 , 10 );
103- config .put ("field" , sourceField );
104- config .put ("formats" , Collections .singletonList ("dd/MM/yyyyy" ));
105- config .put ("locale" , locale );
106- IllegalArgumentException e = expectThrows (IllegalArgumentException .class ,
107- () -> factory .create (null , null , config ));
108- assertThat (e .getMessage (), equalTo ("Unknown language: " + locale .split ("[_-]" )[0 ]));
109- }
110-
111- locales = new String [] { "en-XY" , "en-Canada" };
112- for (String locale : locales ) {
113- DateProcessor .Factory factory = new DateProcessor .Factory ();
114- Map <String , Object > config = new HashMap <>();
115- String sourceField = randomAlphaOfLengthBetween (1 , 10 );
116- config .put ("field" , sourceField );
117- config .put ("formats" , Collections .singletonList ("dd/MM/yyyyy" ));
118- config .put ("locale" , locale );
119- IllegalArgumentException e = expectThrows (IllegalArgumentException .class ,
120- () -> factory .create (null , null , config ));
121- assertThat (e .getMessage (), equalTo ("Unknown country: " + locale .split ("[_-]" )[1 ]));
122- }
99+ assertThat (processor .getLocale ().newInstance (Collections .emptyMap ()).execute (), equalTo (locale .toLanguageTag ()));
123100 }
124101
125102 public void testParseTimezone () throws Exception {
126- DateProcessor .Factory factory = new DateProcessor .Factory ();
127103 Map <String , Object > config = new HashMap <>();
128104 String sourceField = randomAlphaOfLengthBetween (1 , 10 );
129105 config .put ("field" , sourceField );
@@ -132,26 +108,10 @@ public void testParseTimezone() throws Exception {
132108 DateTimeZone timezone = randomDateTimeZone ();
133109 config .put ("timezone" , timezone .getID ());
134110 DateProcessor processor = factory .create (null , null , config );
135- assertThat (processor .getTimezone (), equalTo (timezone ));
136- }
137-
138- public void testParseInvalidTimezone () throws Exception {
139- DateProcessor .Factory factory = new DateProcessor .Factory ();
140- Map <String , Object > config = new HashMap <>();
141- String sourceField = randomAlphaOfLengthBetween (1 , 10 );
142- config .put ("field" , sourceField );
143- config .put ("match_formats" , Collections .singletonList ("dd/MM/yyyyy" ));
144- config .put ("timezone" , "invalid_timezone" );
145- try {
146- factory .create (null , null , config );
147- fail ("invalid timezone should fail" );
148- } catch (IllegalArgumentException e ) {
149- assertThat (e .getMessage (), equalTo ("The datetime zone id 'invalid_timezone' is not recognised" ));
150- }
111+ assertThat (processor .getTimezone ().newInstance (Collections .emptyMap ()).execute (), equalTo (timezone .getID ()));
151112 }
152113
153114 public void testParseMatchFormats () throws Exception {
154- DateProcessor .Factory factory = new DateProcessor .Factory ();
155115 Map <String , Object > config = new HashMap <>();
156116 String sourceField = randomAlphaOfLengthBetween (1 , 10 );
157117 config .put ("field" , sourceField );
@@ -162,7 +122,6 @@ public void testParseMatchFormats() throws Exception {
162122 }
163123
164124 public void testParseMatchFormatsFailure () throws Exception {
165- DateProcessor .Factory factory = new DateProcessor .Factory ();
166125 Map <String , Object > config = new HashMap <>();
167126 String sourceField = randomAlphaOfLengthBetween (1 , 10 );
168127 config .put ("field" , sourceField );
@@ -177,7 +136,6 @@ public void testParseMatchFormatsFailure() throws Exception {
177136 }
178137
179138 public void testParseTargetField () throws Exception {
180- DateProcessor .Factory factory = new DateProcessor .Factory ();
181139 Map <String , Object > config = new HashMap <>();
182140 String sourceField = randomAlphaOfLengthBetween (1 , 10 );
183141 String targetField = randomAlphaOfLengthBetween (1 , 10 );
0 commit comments