19
19
package org .elasticsearch .ingest .common ;
20
20
21
21
import org .elasticsearch .ingest .IngestDocument ;
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 ;
22
27
import org .elasticsearch .test .ESTestCase ;
23
28
import org .joda .time .DateTime ;
24
29
import org .joda .time .DateTimeZone ;
30
+ import org .mockito .Matchers ;
31
+ import org .mockito .Mockito ;
25
32
26
33
import java .util .Collections ;
27
34
import java .util .Locale ;
28
35
import java .util .function .Function ;
29
36
37
+ import static org .elasticsearch .script .Script .DEFAULT_TEMPLATE_LANG ;
30
38
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 ;
31
42
32
43
public class DateIndexNameProcessorTests extends ESTestCase {
33
44
34
45
public void testJodaPattern () throws Exception {
35
46
Function <String , DateTime > function = DateFormat .Joda .getFunction ("yyyy-MM-dd'T'HH:mm:ss.SSSZ" , DateTimeZone .UTC , Locale .ROOT );
36
47
DateIndexNameProcessor processor = new DateIndexNameProcessor (
37
48
"_tag" , "_field" , Collections .singletonList (function ), DateTimeZone .UTC ,
38
- "events-" , "y" , "yyyyMMdd"
49
+ "events-" , "y" , "yyyyMMdd" , TestTemplateService . instance ()
39
50
);
40
51
41
52
IngestDocument document = new IngestDocument ("_index" , "_type" , "_id" , null , null , null ,
@@ -47,7 +58,7 @@ public void testJodaPattern() throws Exception {
47
58
public void testTAI64N ()throws Exception {
48
59
Function <String , DateTime > function = DateFormat .Tai64n .getFunction (null , DateTimeZone .UTC , null );
49
60
DateIndexNameProcessor dateProcessor = new DateIndexNameProcessor ("_tag" , "_field" , Collections .singletonList (function ),
50
- DateTimeZone .UTC , "events-" , "m" , "yyyyMMdd" );
61
+ DateTimeZone .UTC , "events-" , "m" , "yyyyMMdd" , TestTemplateService . instance () );
51
62
IngestDocument document = new IngestDocument ("_index" , "_type" , "_id" , null , null , null ,
52
63
Collections .singletonMap ("_field" , (randomBoolean () ? "@" : "" ) + "4000000050d506482dbdf024" ));
53
64
dateProcessor .execute (document );
@@ -57,7 +68,7 @@ public void testTAI64N()throws Exception {
57
68
public void testUnixMs ()throws Exception {
58
69
Function <String , DateTime > function = DateFormat .UnixMs .getFunction (null , DateTimeZone .UTC , null );
59
70
DateIndexNameProcessor dateProcessor = new DateIndexNameProcessor ("_tag" , "_field" , Collections .singletonList (function ),
60
- DateTimeZone .UTC , "events-" , "m" , "yyyyMMdd" );
71
+ DateTimeZone .UTC , "events-" , "m" , "yyyyMMdd" , TestTemplateService . instance () );
61
72
IngestDocument document = new IngestDocument ("_index" , "_type" , "_id" , null , null , null ,
62
73
Collections .singletonMap ("_field" , "1000500" ));
63
74
dateProcessor .execute (document );
@@ -72,11 +83,28 @@ public void testUnixMs()throws Exception {
72
83
public void testUnix ()throws Exception {
73
84
Function <String , DateTime > function = DateFormat .Unix .getFunction (null , DateTimeZone .UTC , null );
74
85
DateIndexNameProcessor dateProcessor = new DateIndexNameProcessor ("_tag" , "_field" , Collections .singletonList (function ),
75
- DateTimeZone .UTC , "events-" , "m" , "yyyyMMdd" );
86
+ DateTimeZone .UTC , "events-" , "m" , "yyyyMMdd" , TestTemplateService . instance () );
76
87
IngestDocument document = new IngestDocument ("_index" , "_type" , "_id" , null , null , null ,
77
88
Collections .singletonMap ("_field" , "1000.5" ));
78
89
dateProcessor .execute (document );
79
90
assertThat (document .getSourceAndMetadata ().get ("_index" ), equalTo ("<events-{19700101||/m{yyyyMMdd|UTC}}>" ));
80
91
}
81
92
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 );
98
+
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
+ IngestDocument document = new IngestDocument ("_index" , "_type" , "_id" , null , null , null ,
103
+ Collections .singletonMap ("_field" , "1000.5" ));
104
+ dateProcessor .execute (document );
105
+
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" ));
109
+ }
82
110
}
0 commit comments