2020
2121import org .elasticsearch .ingest .IngestDocument ;
2222import org .elasticsearch .ingest .TestTemplateService ;
23- import org .elasticsearch .script .Script ;
24- import org .elasticsearch .script .ScriptContext ;
25- import org .elasticsearch .script .ScriptService ;
26- import org .elasticsearch .script .TemplateScript ;
2723import org .elasticsearch .test .ESTestCase ;
2824import org .joda .time .DateTime ;
2925import org .joda .time .DateTimeZone ;
30- import org .mockito .Matchers ;
31- import org .mockito .Mockito ;
26+ import org .joda .time .format .DateTimeFormat ;
3227
3328import java .util .Collections ;
29+ import java .util .List ;
3430import java .util .Locale ;
3531import java .util .function .Function ;
3632
37- import static org .elasticsearch .script .Script .DEFAULT_TEMPLATE_LANG ;
3833import static org .hamcrest .CoreMatchers .equalTo ;
39- import static org .mockito .Matchers .any ;
40- import static org .mockito .Mockito .mock ;
41- import static org .mockito .Mockito .when ;
4234
4335public class DateIndexNameProcessorTests extends ESTestCase {
4436
4537 public void testJodaPattern () throws Exception {
4638 Function <String , DateTime > function = DateFormat .Joda .getFunction ("yyyy-MM-dd'T'HH:mm:ss.SSSZ" , DateTimeZone .UTC , Locale .ROOT );
47- DateIndexNameProcessor processor = new DateIndexNameProcessor (
48- "_tag" , "_field" , Collections .singletonList (function ), DateTimeZone .UTC ,
49- "events-" , "y" , "yyyyMMdd" , TestTemplateService .instance ()
50- );
51-
39+ DateIndexNameProcessor processor = createProcessor ("_field" , Collections .singletonList (function ),
40+ DateTimeZone .UTC , "events-" , "y" , "yyyyMMdd" );
5241 IngestDocument document = new IngestDocument ("_index" , "_type" , "_id" , null , null , null ,
5342 Collections .singletonMap ("_field" , "2016-04-25T12:24:20.101Z" ));
5443 processor .execute (document );
@@ -57,8 +46,8 @@ public void testJodaPattern() throws Exception {
5746
5847 public void testTAI64N ()throws Exception {
5948 Function <String , DateTime > function = DateFormat .Tai64n .getFunction (null , DateTimeZone .UTC , null );
60- DateIndexNameProcessor dateProcessor = new DateIndexNameProcessor ( "_tag" , "_field" , Collections .singletonList (function ),
61- DateTimeZone .UTC , "events-" , "m" , "yyyyMMdd" , TestTemplateService . instance () );
49+ DateIndexNameProcessor dateProcessor = createProcessor ( "_field" , Collections .singletonList (function ),
50+ DateTimeZone .UTC , "events-" , "m" , "yyyyMMdd" );
6251 IngestDocument document = new IngestDocument ("_index" , "_type" , "_id" , null , null , null ,
6352 Collections .singletonMap ("_field" , (randomBoolean () ? "@" : "" ) + "4000000050d506482dbdf024" ));
6453 dateProcessor .execute (document );
@@ -67,8 +56,8 @@ public void testTAI64N()throws Exception {
6756
6857 public void testUnixMs ()throws Exception {
6958 Function <String , DateTime > function = DateFormat .UnixMs .getFunction (null , DateTimeZone .UTC , null );
70- DateIndexNameProcessor dateProcessor = new DateIndexNameProcessor ( "_tag" , "_field" , Collections .singletonList (function ),
71- DateTimeZone .UTC , "events-" , "m" , "yyyyMMdd" , TestTemplateService . instance () );
59+ DateIndexNameProcessor dateProcessor = createProcessor ( "_field" , Collections .singletonList (function ),
60+ DateTimeZone .UTC , "events-" , "m" , "yyyyMMdd" );
7261 IngestDocument document = new IngestDocument ("_index" , "_type" , "_id" , null , null , null ,
7362 Collections .singletonMap ("_field" , "1000500" ));
7463 dateProcessor .execute (document );
@@ -82,29 +71,41 @@ public void testUnixMs()throws Exception {
8271
8372 public void testUnix ()throws Exception {
8473 Function <String , DateTime > function = DateFormat .Unix .getFunction (null , DateTimeZone .UTC , null );
85- DateIndexNameProcessor dateProcessor = new DateIndexNameProcessor ( "_tag" , "_field" , Collections .singletonList (function ),
86- DateTimeZone .UTC , "events-" , "m" , "yyyyMMdd" , TestTemplateService . instance () );
74+ DateIndexNameProcessor dateProcessor = createProcessor ( "_field" , Collections .singletonList (function ),
75+ DateTimeZone .UTC , "events-" , "m" , "yyyyMMdd" );
8776 IngestDocument document = new IngestDocument ("_index" , "_type" , "_id" , null , null , null ,
8877 Collections .singletonMap ("_field" , "1000.5" ));
8978 dateProcessor .execute (document );
9079 assertThat (document .getSourceAndMetadata ().get ("_index" ), equalTo ("<events-{19700101||/m{yyyyMMdd|UTC}}>" ));
9180 }
9281
93- public void testTemplateSupported () throws Exception {
94- ScriptService scriptService = mock (ScriptService .class );
95- TestTemplateService .MockTemplateScript .Factory factory = new TestTemplateService .MockTemplateScript .Factory ("script_result" );
96- when (scriptService .compile (any (Script .class ), Matchers .<ScriptContext <TemplateScript .Factory >>any ())).thenReturn (factory );
97- when (scriptService .isLangSupported (DEFAULT_TEMPLATE_LANG )).thenReturn (true );
82+ public void testTemplatedFields () throws Exception {
83+ String indexNamePrefix = randomAlphaOfLength (10 );
84+ String dateRounding = randomFrom ("y" , "M" , "w" , "d" , "h" , "m" , "s" );
85+ String indexNameFormat = randomFrom ("yyyy-MM-dd'T'HH:mm:ss.SSSZ" , "yyyyMMdd" , "MM/dd/yyyy" );
86+ String date = Integer .toString (randomInt ());
87+ Function <String , DateTime > dateTimeFunction = DateFormat .Unix .getFunction (null , DateTimeZone .UTC , null );
88+
89+ DateIndexNameProcessor dateProcessor = createProcessor ("_field" ,
90+ Collections .singletonList (dateTimeFunction ), DateTimeZone .UTC , indexNamePrefix ,
91+ dateRounding , indexNameFormat );
9892
99- DateIndexNameProcessor dateProcessor = new DateIndexNameProcessor ("_tag" , "_field" ,
100- Collections .singletonList (DateFormat .Unix .getFunction (null , DateTimeZone .UTC , null )),
101- DateTimeZone .UTC , "events-" , "m" , "yyyyMMdd" , scriptService );
10293 IngestDocument document = new IngestDocument ("_index" , "_type" , "_id" , null , null , null ,
103- Collections .singletonMap ("_field" , "1000.5" ));
94+ Collections .singletonMap ("_field" , date ));
10495 dateProcessor .execute (document );
10596
106- // here we only care that the script was compiled and that it returned what we expect.
107- Mockito .verify (scriptService ).compile (any (Script .class ), Matchers .<ScriptContext <TemplateScript .Factory >>any ());
108- assertThat (document .getSourceAndMetadata ().get ("_index" ), equalTo ("script_result" ));
97+ assertThat (document .getSourceAndMetadata ().get ("_index" ),
98+ equalTo ("<" +indexNamePrefix +"{" +DateTimeFormat .forPattern (indexNameFormat )
99+ .print (dateTimeFunction .apply (date ))+"||/" +dateRounding +"{" +indexNameFormat +"|UTC}}>" ));
100+ }
101+
102+ private DateIndexNameProcessor createProcessor (String field , List <Function <String , DateTime >> dateFormats ,
103+ DateTimeZone timezone , String indexNamePrefix , String dateRounding ,
104+ String indexNameFormat ) {
105+ return new DateIndexNameProcessor (randomAlphaOfLength (10 ), field , dateFormats , timezone ,
106+ new TestTemplateService .MockTemplateScript .Factory (indexNamePrefix ),
107+ new TestTemplateService .MockTemplateScript .Factory (dateRounding ),
108+ new TestTemplateService .MockTemplateScript .Factory (indexNameFormat )
109+ );
109110 }
110111}
0 commit comments