20
20
package org .elasticsearch .ingest .common ;
21
21
22
22
import org .elasticsearch .ElasticsearchException ;
23
+ import org .elasticsearch .common .settings .Settings ;
23
24
import org .elasticsearch .common .xcontent .XContentParseException ;
25
+ import org .elasticsearch .script .IngestScript ;
26
+ import org .elasticsearch .script .MockScriptEngine ;
24
27
import org .elasticsearch .script .Script ;
25
28
import org .elasticsearch .script .ScriptException ;
29
+ import org .elasticsearch .script .ScriptModule ;
26
30
import org .elasticsearch .script .ScriptService ;
31
+ import org .elasticsearch .script .ScriptType ;
27
32
import org .elasticsearch .test .ESTestCase ;
28
33
import org .junit .Before ;
29
34
@@ -51,6 +56,10 @@ public void init() {
51
56
}
52
57
53
58
public void testFactoryValidationWithDefaultLang () throws Exception {
59
+ ScriptService mockedScriptService = mock (ScriptService .class );
60
+ when (mockedScriptService .compile (any (), any ())).thenReturn (mock (IngestScript .Factory .class ));
61
+ factory = new ScriptProcessor .Factory (mockedScriptService );
62
+
54
63
Map <String , Object > configMap = new HashMap <>();
55
64
String randomType = randomFrom ("id" , "source" );
56
65
configMap .put (randomType , "foo" );
@@ -61,6 +70,10 @@ public void testFactoryValidationWithDefaultLang() throws Exception {
61
70
}
62
71
63
72
public void testFactoryValidationWithParams () throws Exception {
73
+ ScriptService mockedScriptService = mock (ScriptService .class );
74
+ when (mockedScriptService .compile (any (), any ())).thenReturn (mock (IngestScript .Factory .class ));
75
+ factory = new ScriptProcessor .Factory (mockedScriptService );
76
+
64
77
Map <String , Object > configMap = new HashMap <>();
65
78
String randomType = randomFrom ("id" , "source" );
66
79
Map <String , Object > randomParams = Collections .singletonMap (randomAlphaOfLength (10 ), randomAlphaOfLength (10 ));
@@ -94,6 +107,10 @@ public void testFactoryValidationAtLeastOneScriptingType() throws Exception {
94
107
}
95
108
96
109
public void testInlineBackcompat () throws Exception {
110
+ ScriptService mockedScriptService = mock (ScriptService .class );
111
+ when (mockedScriptService .compile (any (), any ())).thenReturn (mock (IngestScript .Factory .class ));
112
+ factory = new ScriptProcessor .Factory (mockedScriptService );
113
+
97
114
Map <String , Object > configMap = new HashMap <>();
98
115
configMap .put ("inline" , "code" );
99
116
@@ -117,4 +134,44 @@ public void testFactoryInvalidateWithInvalidCompiledScript() throws Exception {
117
134
118
135
assertThat (exception .getMessage (), is ("compile-time exception" ));
119
136
}
137
+
138
+ public void testInlineIsCompiled () throws Exception {
139
+ String scriptName = "foo" ;
140
+ ScriptService scriptService = new ScriptService (Settings .builder ().build (),
141
+ Collections .singletonMap (
142
+ Script .DEFAULT_SCRIPT_LANG , new MockScriptEngine (
143
+ Script .DEFAULT_SCRIPT_LANG ,
144
+ Collections .singletonMap (scriptName , ctx -> {
145
+ ctx .put ("foo" , "bar" );
146
+ return null ;
147
+ }),
148
+ Collections .emptyMap ()
149
+ )
150
+ ), new HashMap <>(ScriptModule .CORE_CONTEXTS ));
151
+ factory = new ScriptProcessor .Factory (scriptService );
152
+
153
+ Map <String , Object > configMap = new HashMap <>();
154
+ configMap .put ("source" , scriptName );
155
+ ScriptProcessor processor = factory .create (null , randomAlphaOfLength (10 ), configMap );
156
+ assertThat (processor .getScript ().getLang (), equalTo (Script .DEFAULT_SCRIPT_LANG ));
157
+ assertThat (processor .getScript ().getType (), equalTo (ScriptType .INLINE ));
158
+ assertThat (processor .getScript ().getParams (), equalTo (Collections .emptyMap ()));
159
+ assertNotNull (processor .getPrecompiledIngestScript ());
160
+ Map <String , Object > ctx = new HashMap <>();
161
+ processor .getPrecompiledIngestScript ().execute (ctx );
162
+ assertThat (ctx .get ("foo" ), equalTo ("bar" ));
163
+ }
164
+
165
+ public void testStoredIsNotCompiled () throws Exception {
166
+ ScriptService mockedScriptService = mock (ScriptService .class );
167
+ when (mockedScriptService .compile (any (), any ())).thenReturn (mock (IngestScript .Factory .class ));
168
+ factory = new ScriptProcessor .Factory (mockedScriptService );
169
+ Map <String , Object > configMap = new HashMap <>();
170
+ configMap .put ("id" , "script_name" );
171
+ ScriptProcessor processor = factory .create (null , randomAlphaOfLength (10 ), configMap );
172
+ assertNull (processor .getScript ().getLang ());
173
+ assertThat (processor .getScript ().getType (), equalTo (ScriptType .STORED ));
174
+ assertThat (processor .getScript ().getParams (), equalTo (Collections .emptyMap ()));
175
+ assertNull (processor .getPrecompiledIngestScript ());
176
+ }
120
177
}
0 commit comments