24
24
import org .elasticsearch .ingest .TestTemplateService ;
25
25
import org .elasticsearch .script .TemplateScript ;
26
26
import org .elasticsearch .test .ESTestCase ;
27
- import org .joda .time .DateTime ;
28
- import org .joda .time .DateTimeZone ;
29
27
28
+ import java .time .ZoneId ;
29
+ import java .time .ZoneOffset ;
30
+ import java .time .ZonedDateTime ;
30
31
import java .util .ArrayList ;
31
32
import java .util .Collections ;
32
33
import java .util .HashMap ;
36
37
37
38
import static org .hamcrest .CoreMatchers .containsString ;
38
39
import static org .hamcrest .CoreMatchers .equalTo ;
39
- import static org .joda .time .DateTimeZone .UTC ;
40
40
41
41
public class DateProcessorTests extends ESTestCase {
42
+
42
43
private TemplateScript .Factory templatize (Locale locale ) {
43
44
return new TestTemplateService .MockTemplateScript .Factory (locale .getLanguage ());
44
45
}
45
46
46
- private TemplateScript .Factory templatize (DateTimeZone timezone ) {
47
- return new TestTemplateService .MockTemplateScript .Factory (timezone .getID ());
47
+ private TemplateScript .Factory templatize (ZoneId timezone ) {
48
+ // prevent writing "UTC" as string, as joda time does not parse it
49
+ String id = timezone .equals (ZoneOffset .UTC ) ? "UTC" : timezone .getId ();
50
+ return new TestTemplateService .MockTemplateScript .Factory (id );
48
51
}
49
52
public void testJodaPattern () {
50
53
DateProcessor dateProcessor = new DateProcessor (randomAlphaOfLength (10 ),
51
- templatize (DateTimeZone . forID ("Europe/Amsterdam" )), templatize (Locale .ENGLISH ),
54
+ templatize (ZoneId . of ("Europe/Amsterdam" )), templatize (Locale .ENGLISH ),
52
55
"date_as_string" , Collections .singletonList ("yyyy dd MM hh:mm:ss" ), "date_as_date" );
53
56
Map <String , Object > document = new HashMap <>();
54
57
document .put ("date_as_string" , "2010 12 06 11:05:15" );
@@ -63,7 +66,7 @@ public void testJodaPatternMultipleFormats() {
63
66
matchFormats .add ("dd/MM/yyyy" );
64
67
matchFormats .add ("dd-MM-yyyy" );
65
68
DateProcessor dateProcessor = new DateProcessor (randomAlphaOfLength (10 ),
66
- templatize (DateTimeZone . forID ("Europe/Amsterdam" )), templatize (Locale .ENGLISH ),
69
+ templatize (ZoneId . of ("Europe/Amsterdam" )), templatize (Locale .ENGLISH ),
67
70
"date_as_string" , matchFormats , "date_as_date" );
68
71
69
72
Map <String , Object > document = new HashMap <>();
@@ -98,7 +101,7 @@ public void testJodaPatternMultipleFormats() {
98
101
public void testInvalidJodaPattern () {
99
102
try {
100
103
DateProcessor processor = new DateProcessor (randomAlphaOfLength (10 ),
101
- templatize (UTC ), templatize (randomLocale (random ())),
104
+ templatize (ZoneOffset . UTC ), templatize (randomLocale (random ())),
102
105
"date_as_string" , Collections .singletonList ("invalid pattern" ), "date_as_date" );
103
106
Map <String , Object > document = new HashMap <>();
104
107
document .put ("date_as_string" , "2010" );
@@ -112,7 +115,7 @@ public void testInvalidJodaPattern() {
112
115
113
116
public void testJodaPatternLocale () {
114
117
DateProcessor dateProcessor = new DateProcessor (randomAlphaOfLength (10 ),
115
- templatize (DateTimeZone . forID ("Europe/Amsterdam" )), templatize (Locale .ITALIAN ),
118
+ templatize (ZoneId . of ("Europe/Amsterdam" )), templatize (Locale .ITALIAN ),
116
119
"date_as_string" , Collections .singletonList ("yyyy dd MMM" ), "date_as_date" );
117
120
Map <String , Object > document = new HashMap <>();
118
121
document .put ("date_as_string" , "2010 12 giugno" );
@@ -123,18 +126,18 @@ public void testJodaPatternLocale() {
123
126
124
127
public void testJodaPatternDefaultYear () {
125
128
DateProcessor dateProcessor = new DateProcessor (randomAlphaOfLength (10 ),
126
- templatize (DateTimeZone . forID ("Europe/Amsterdam" )), templatize (Locale .ENGLISH ),
129
+ templatize (ZoneId . of ("Europe/Amsterdam" )), templatize (Locale .ENGLISH ),
127
130
"date_as_string" , Collections .singletonList ("dd/MM" ), "date_as_date" );
128
131
Map <String , Object > document = new HashMap <>();
129
132
document .put ("date_as_string" , "12/06" );
130
133
IngestDocument ingestDocument = RandomDocumentPicks .randomIngestDocument (random (), document );
131
134
dateProcessor .execute (ingestDocument );
132
135
assertThat (ingestDocument .getFieldValue ("date_as_date" , String .class ),
133
- equalTo (DateTime .now ().getYear () + "-06-12T00:00:00.000+02:00" ));
136
+ equalTo (ZonedDateTime .now ().getYear () + "-06-12T00:00:00.000+02:00" ));
134
137
}
135
138
136
139
public void testTAI64N () {
137
- DateProcessor dateProcessor = new DateProcessor (randomAlphaOfLength (10 ), templatize (DateTimeZone . forOffsetHours (2 )),
140
+ DateProcessor dateProcessor = new DateProcessor (randomAlphaOfLength (10 ), templatize (ZoneOffset . ofHours (2 )),
138
141
templatize (randomLocale (random ())),
139
142
"date_as_string" , Collections .singletonList ("TAI64N" ), "date_as_date" );
140
143
Map <String , Object > document = new HashMap <>();
@@ -146,8 +149,8 @@ public void testTAI64N() {
146
149
}
147
150
148
151
public void testUnixMs () {
149
- DateProcessor dateProcessor = new DateProcessor (randomAlphaOfLength (10 ), templatize (UTC ), templatize ( randomLocale ( random ()) ),
150
- "date_as_string" , Collections .singletonList ("UNIX_MS" ), "date_as_date" );
152
+ DateProcessor dateProcessor = new DateProcessor (randomAlphaOfLength (10 ), templatize (ZoneOffset . UTC ),
153
+ templatize ( randomLocale ( random ())), "date_as_string" , Collections .singletonList ("UNIX_MS" ), "date_as_date" );
151
154
Map <String , Object > document = new HashMap <>();
152
155
document .put ("date_as_string" , "1000500" );
153
156
IngestDocument ingestDocument = RandomDocumentPicks .randomIngestDocument (random (), document );
@@ -162,7 +165,7 @@ public void testUnixMs() {
162
165
}
163
166
164
167
public void testUnix () {
165
- DateProcessor dateProcessor = new DateProcessor (randomAlphaOfLength (10 ), templatize (UTC ),
168
+ DateProcessor dateProcessor = new DateProcessor (randomAlphaOfLength (10 ), templatize (ZoneOffset . UTC ),
166
169
templatize (randomLocale (random ())),
167
170
"date_as_string" , Collections .singletonList ("UNIX" ), "date_as_date" );
168
171
Map <String , Object > document = new HashMap <>();
@@ -186,7 +189,7 @@ public void testInvalidTimezone() {
186
189
187
190
public void testInvalidLocale () {
188
191
DateProcessor processor = new DateProcessor (randomAlphaOfLength (10 ),
189
- templatize (UTC ), new TestTemplateService .MockTemplateScript .Factory ("invalid_locale" ),
192
+ templatize (ZoneOffset . UTC ), new TestTemplateService .MockTemplateScript .Factory ("invalid_locale" ),
190
193
"date_as_string" , Collections .singletonList ("yyyy" ), "date_as_date" );
191
194
Map <String , Object > document = new HashMap <>();
192
195
document .put ("date_as_string" , "2010" );
0 commit comments