20
20
21
21
import org .elasticsearch .ingest .IngestDocument ;
22
22
import 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 ;
27
23
import org .elasticsearch .test .ESTestCase ;
28
24
import org .joda .time .DateTime ;
29
25
import org .joda .time .DateTimeZone ;
30
- import org .mockito .Matchers ;
31
- import org .mockito .Mockito ;
26
+ import org .joda .time .format .DateTimeFormat ;
32
27
33
28
import java .util .Collections ;
29
+ import java .util .List ;
34
30
import java .util .Locale ;
35
31
import java .util .function .Function ;
36
32
37
- import static org .elasticsearch .script .Script .DEFAULT_TEMPLATE_LANG ;
38
33
import 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 ;
42
34
43
35
public class DateIndexNameProcessorTests extends ESTestCase {
44
36
45
37
public void testJodaPattern () throws Exception {
46
38
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" );
52
41
IngestDocument document = new IngestDocument ("_index" , "_type" , "_id" , null , null , null ,
53
42
Collections .singletonMap ("_field" , "2016-04-25T12:24:20.101Z" ));
54
43
processor .execute (document );
@@ -57,8 +46,8 @@ public void testJodaPattern() throws Exception {
57
46
58
47
public void testTAI64N ()throws Exception {
59
48
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" );
62
51
IngestDocument document = new IngestDocument ("_index" , "_type" , "_id" , null , null , null ,
63
52
Collections .singletonMap ("_field" , (randomBoolean () ? "@" : "" ) + "4000000050d506482dbdf024" ));
64
53
dateProcessor .execute (document );
@@ -67,8 +56,8 @@ public void testTAI64N()throws Exception {
67
56
68
57
public void testUnixMs ()throws Exception {
69
58
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" );
72
61
IngestDocument document = new IngestDocument ("_index" , "_type" , "_id" , null , null , null ,
73
62
Collections .singletonMap ("_field" , "1000500" ));
74
63
dateProcessor .execute (document );
@@ -82,29 +71,41 @@ public void testUnixMs()throws Exception {
82
71
83
72
public void testUnix ()throws Exception {
84
73
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" );
87
76
IngestDocument document = new IngestDocument ("_index" , "_type" , "_id" , null , null , null ,
88
77
Collections .singletonMap ("_field" , "1000.5" ));
89
78
dateProcessor .execute (document );
90
79
assertThat (document .getSourceAndMetadata ().get ("_index" ), equalTo ("<events-{19700101||/m{yyyyMMdd|UTC}}>" ));
91
80
}
92
81
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 );
98
92
99
- DateIndexNameProcessor dateProcessor = new DateIndexNameProcessor ("_tag" , "_field" ,
100
- Collections .singletonList (DateFormat .Unix .getFunction (null , DateTimeZone .UTC , null )),
101
- DateTimeZone .UTC , "events-" , "m" , "yyyyMMdd" , scriptService );
102
93
IngestDocument document = new IngestDocument ("_index" , "_type" , "_id" , null , null , null ,
103
- Collections .singletonMap ("_field" , "1000.5" ));
94
+ Collections .singletonMap ("_field" , date ));
104
95
dateProcessor .execute (document );
105
96
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
+ );
109
110
}
110
111
}
0 commit comments