|
43 | 43 | import org.elasticsearch.cluster.service.ClusterService; |
44 | 44 | import org.elasticsearch.common.bytes.BytesArray; |
45 | 45 | import org.elasticsearch.common.logging.Loggers; |
| 46 | +import org.elasticsearch.common.settings.Settings; |
46 | 47 | import org.elasticsearch.common.util.concurrent.EsExecutors; |
47 | 48 | import org.elasticsearch.common.xcontent.XContentType; |
48 | 49 | import org.elasticsearch.index.VersionType; |
49 | 50 | import org.elasticsearch.plugins.IngestPlugin; |
| 51 | +import org.elasticsearch.script.MockScriptEngine; |
| 52 | +import org.elasticsearch.script.Script; |
| 53 | +import org.elasticsearch.script.ScriptModule; |
| 54 | +import org.elasticsearch.script.ScriptService; |
| 55 | +import org.elasticsearch.script.ScriptType; |
50 | 56 | import org.elasticsearch.test.ESTestCase; |
51 | 57 | import org.elasticsearch.test.MockLogAppender; |
52 | 58 | import org.elasticsearch.threadpool.ThreadPool; |
|
64 | 70 | import java.util.concurrent.ExecutorService; |
65 | 71 | import java.util.function.BiConsumer; |
66 | 72 | import java.util.function.Consumer; |
| 73 | +import java.util.function.LongSupplier; |
67 | 74 |
|
68 | 75 | import static java.util.Collections.emptyMap; |
69 | 76 | import static java.util.Collections.emptySet; |
@@ -263,6 +270,89 @@ public void testValidateNoIngestInfo() throws Exception { |
263 | 270 | ingestService.validatePipeline(Collections.singletonMap(discoveryNode, ingestInfo), putRequest); |
264 | 271 | } |
265 | 272 |
|
| 273 | + public void testHasProcessor() throws Exception { |
| 274 | + IngestService ingestService = createWithProcessors(); |
| 275 | + String id = "_id"; |
| 276 | + Pipeline pipeline = ingestService.getPipeline(id); |
| 277 | + assertThat(pipeline, nullValue()); |
| 278 | + ClusterState clusterState = ClusterState.builder(new ClusterName("_name")).build(); // Start empty |
| 279 | + |
| 280 | + PutPipelineRequest putRequest = new PutPipelineRequest("_id", new BytesArray( |
| 281 | + "{\"processors\": [{\"set\" : {\"field\": \"_field\", \"value\": \"_value\", \"tag\": \"tag1\"}}," + |
| 282 | + "{\"remove\" : {\"field\": \"_field\", \"tag\": \"tag2\"}}]}"), |
| 283 | + XContentType.JSON); |
| 284 | + ClusterState previousClusterState = clusterState; |
| 285 | + clusterState = IngestService.innerPut(putRequest, clusterState); |
| 286 | + ingestService.applyClusterState(new ClusterChangedEvent("", clusterState, previousClusterState)); |
| 287 | + pipeline = ingestService.getPipeline(id); |
| 288 | + assertThat(pipeline, notNullValue()); |
| 289 | + |
| 290 | + assertTrue(ingestService.hasProcessor(id, Processor.class)); |
| 291 | + assertTrue(ingestService.hasProcessor(id, WrappingProcessorImpl.class)); |
| 292 | + assertTrue(ingestService.hasProcessor(id, WrappingProcessor.class)); |
| 293 | + assertTrue(ingestService.hasProcessor(id, FakeProcessor.class)); |
| 294 | + |
| 295 | + assertFalse(ingestService.hasProcessor(id, ConditionalProcessor.class)); |
| 296 | + } |
| 297 | + |
| 298 | + public void testHasProcessorComplexConditional() throws Exception { |
| 299 | + LongSupplier relativeTimeProvider = mock(LongSupplier.class); |
| 300 | + String scriptName = "conditionalScript"; |
| 301 | + ScriptService scriptService = new ScriptService(Settings.builder().build(), |
| 302 | + Collections.singletonMap( |
| 303 | + Script.DEFAULT_SCRIPT_LANG, |
| 304 | + new MockScriptEngine( |
| 305 | + Script.DEFAULT_SCRIPT_LANG, |
| 306 | + Collections.singletonMap( |
| 307 | + scriptName, ctx -> { |
| 308 | + ctx.get("_type"); |
| 309 | + return true; |
| 310 | + } |
| 311 | + ), |
| 312 | + Collections.emptyMap() |
| 313 | + ) |
| 314 | + ), |
| 315 | + new HashMap<>(ScriptModule.CORE_CONTEXTS) |
| 316 | + ); |
| 317 | + |
| 318 | + Map<String, Processor.Factory> processors = new HashMap<>(); |
| 319 | + processors.put("complexSet", (factories, tag, config) -> { |
| 320 | + String field = (String) config.remove("field"); |
| 321 | + String value = (String) config.remove("value"); |
| 322 | + |
| 323 | + return new ConditionalProcessor(randomAlphaOfLength(10), |
| 324 | + new Script( |
| 325 | + ScriptType.INLINE, Script.DEFAULT_SCRIPT_LANG, |
| 326 | + scriptName, Collections.emptyMap()), scriptService, |
| 327 | + new ConditionalProcessor(randomAlphaOfLength(10) + "-nested", |
| 328 | + new Script( |
| 329 | + ScriptType.INLINE, Script.DEFAULT_SCRIPT_LANG, |
| 330 | + scriptName, Collections.emptyMap()), scriptService, |
| 331 | + new FakeProcessor("complexSet", tag, (ingestDocument) -> ingestDocument.setFieldValue(field, value)))); |
| 332 | + }); |
| 333 | + |
| 334 | + IngestService ingestService = createWithProcessors(processors); |
| 335 | + String id = "_id"; |
| 336 | + Pipeline pipeline = ingestService.getPipeline(id); |
| 337 | + assertThat(pipeline, nullValue()); |
| 338 | + ClusterState clusterState = ClusterState.builder(new ClusterName("_name")).build(); // Start empty |
| 339 | + |
| 340 | + PutPipelineRequest putRequest = new PutPipelineRequest(id, |
| 341 | + new BytesArray("{\"processors\": [{\"complexSet\" : {\"field\": \"_field\", \"value\": \"_value\"}}]}"), XContentType.JSON); |
| 342 | + ClusterState previousClusterState = clusterState; |
| 343 | + clusterState = IngestService.innerPut(putRequest, clusterState); |
| 344 | + ingestService.applyClusterState(new ClusterChangedEvent("", clusterState, previousClusterState)); |
| 345 | + pipeline = ingestService.getPipeline(id); |
| 346 | + assertThat(pipeline, notNullValue()); |
| 347 | + |
| 348 | + assertTrue(ingestService.hasProcessor(id, Processor.class)); |
| 349 | + assertTrue(ingestService.hasProcessor(id, WrappingProcessor.class)); |
| 350 | + assertTrue(ingestService.hasProcessor(id, FakeProcessor.class)); |
| 351 | + assertTrue(ingestService.hasProcessor(id, ConditionalProcessor.class)); |
| 352 | + |
| 353 | + assertFalse(ingestService.hasProcessor(id, WrappingProcessorImpl.class)); |
| 354 | + } |
| 355 | + |
266 | 356 | public void testCrud() throws Exception { |
267 | 357 | IngestService ingestService = createWithProcessors(); |
268 | 358 | String id = "_id"; |
@@ -946,7 +1036,7 @@ public void testStatName(){ |
946 | 1036 | assertThat(IngestService.getProcessorName(processor), equalTo(name + ":" + tag)); |
947 | 1037 |
|
948 | 1038 | ConditionalProcessor conditionalProcessor = mock(ConditionalProcessor.class); |
949 | | - when(conditionalProcessor.getProcessor()).thenReturn(processor); |
| 1039 | + when(conditionalProcessor.getInnerProcessor()).thenReturn(processor); |
950 | 1040 | assertThat(IngestService.getProcessorName(conditionalProcessor), equalTo(name + ":" + tag)); |
951 | 1041 |
|
952 | 1042 | PipelineProcessor pipelineProcessor = mock(PipelineProcessor.class); |
@@ -1012,42 +1102,11 @@ private static IngestService createWithProcessors() { |
1012 | 1102 | processors.put("set", (factories, tag, config) -> { |
1013 | 1103 | String field = (String) config.remove("field"); |
1014 | 1104 | String value = (String) config.remove("value"); |
1015 | | - return new Processor() { |
1016 | | - @Override |
1017 | | - public IngestDocument execute(IngestDocument ingestDocument) { |
1018 | | - ingestDocument.setFieldValue(field, value); |
1019 | | - return ingestDocument; |
1020 | | - } |
1021 | | - |
1022 | | - @Override |
1023 | | - public String getType() { |
1024 | | - return "set"; |
1025 | | - } |
1026 | | - |
1027 | | - @Override |
1028 | | - public String getTag() { |
1029 | | - return tag; |
1030 | | - } |
1031 | | - }; |
| 1105 | + return new FakeProcessor("set", tag, (ingestDocument) ->ingestDocument.setFieldValue(field, value)); |
1032 | 1106 | }); |
1033 | 1107 | processors.put("remove", (factories, tag, config) -> { |
1034 | 1108 | String field = (String) config.remove("field"); |
1035 | | - return new Processor() { |
1036 | | - @Override |
1037 | | - public IngestDocument execute(IngestDocument ingestDocument) { |
1038 | | - ingestDocument.removeField(field); |
1039 | | - return ingestDocument; |
1040 | | - } |
1041 | | - |
1042 | | - @Override |
1043 | | - public String getType() { |
1044 | | - return "remove"; |
1045 | | - } |
1046 | | - |
1047 | | - @Override |
1048 | | - public String getTag() { |
1049 | | - return tag; |
1050 | | - } |
| 1109 | + return new WrappingProcessorImpl("remove", tag, (ingestDocument -> ingestDocument.removeField(field))) { |
1051 | 1110 | }; |
1052 | 1111 | }); |
1053 | 1112 | return createWithProcessors(processors); |
|
0 commit comments