dan-s1 commented on code in PR #7537:
URL: https://github.com/apache/nifi/pull/7537#discussion_r1284667812


##########
nifi-nar-bundles/nifi-elasticsearch-bundle/nifi-elasticsearch-restapi-processors/src/test/java/org/apache/nifi/processors/elasticsearch/PutElasticsearchJsonTest.java:
##########
@@ -0,0 +1,409 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.nifi.processors.elasticsearch;
+
+import org.apache.nifi.elasticsearch.IndexOperationRequest;
+import org.apache.nifi.elasticsearch.IndexOperationResponse;
+import org.apache.nifi.processor.exception.ProcessException;
+import org.apache.nifi.processors.elasticsearch.mock.MockBulkLoadClientService;
+import org.apache.nifi.provenance.ProvenanceEventType;
+import org.apache.nifi.util.MockFlowFile;
+import org.apache.nifi.util.TestRunner;
+import org.apache.nifi.util.TestRunners;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.function.Consumer;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+public class PutElasticsearchJsonTest extends 
AbstractPutElasticsearchTest<PutElasticsearchJson> {
+    private static final String TEST_DIR = 
"src/test/resources/PutElasticsearchJsonTest";
+    private static final String TEST_COMMON_DIR = "src/test/resources/common";
+    private static final Path BATCH_WITH_ERROR = 
Paths.get(TEST_DIR,"batchWithError.json");
+    private MockBulkLoadClientService clientService;
+    private TestRunner runner;
+    private static String flowFileContents;
+    private static String sampleErrorResponse;
+
+    @Override
+    public Class<? extends AbstractPutElasticsearch> getTestProcessor() {
+        return PutElasticsearchJson.class;
+    }
+
+    @BeforeAll
+    public static void setUpBeforeClass() throws Exception {
+        sampleErrorResponse = 
Files.readString(Paths.get(TEST_COMMON_DIR,"sampleErrorResponse.json"));
+        flowFileContents = Files.readString(Paths.get(TEST_DIR, 
"flowFileContents.json"));
+    }
+
+    @BeforeEach
+    public void setup() throws Exception {
+        runner = TestRunners.newTestRunner(getTestProcessor());
+        clientService = new MockBulkLoadClientService();
+        clientService.setResponse(new IndexOperationResponse(1500));
+        runner.addControllerService("clientService", clientService);
+        runner.setProperty(PutElasticsearchJson.ID_ATTRIBUTE, "doc_id");
+        runner.setProperty(PutElasticsearchJson.INDEX_OP, 
IndexOperationRequest.Operation.Index.getValue());
+        runner.setProperty(PutElasticsearchJson.INDEX, "test_index");
+        runner.setProperty(PutElasticsearchJson.TYPE, "test_type");
+        runner.setProperty(PutElasticsearchJson.BATCH_SIZE, "1");
+        runner.setProperty(PutElasticsearchJson.OUTPUT_ERROR_DOCUMENTS, 
"false");
+        runner.setProperty(PutElasticsearchJson.LOG_ERROR_RESPONSES, "false");
+        runner.setProperty(PutElasticsearchJson.CLIENT_SERVICE, 
"clientService");
+        runner.setProperty(PutElasticsearchJson.NOT_FOUND_IS_SUCCESSFUL, 
"true");
+        runner.setProperty(PutElasticsearchJson.OUTPUT_ERROR_RESPONSES, 
"false");
+        runner.enableControllerService(clientService);
+
+        runner.assertValid();
+    }
+
+    public void basicTest(int failure, int retry, int success) {
+        Consumer<List<IndexOperationRequest>> consumer = 
(List<IndexOperationRequest> items) -> {
+            long nullIdCount = items.stream().filter(item -> item.getId() == 
null).count();
+            long indexCount = items.stream().filter(item -> 
"test_index".equals(item.getIndex())).count();
+            long typeCount = items.stream().filter(item -> 
"test_type".equals(item.getType())).count();
+            long opCount = items.stream().filter(item -> 
IndexOperationRequest.Operation.Index.equals(item.getOperation())).count();
+            long emptyScriptCount = items.stream().filter(item -> 
item.getScript().isEmpty()).count();
+            long falseScriptedUpsertCount = items.stream().filter(item -> 
!item.isScriptedUpsert()).count();
+            long emptyDynamicTemplatesCount = items.stream().filter(item -> 
item.getDynamicTemplates().isEmpty()).count();
+            long emptyHeaderFields = items.stream().filter(item -> 
item.getHeaderFields().isEmpty()).count();
+
+            assertEquals(1L, nullIdCount);
+            assertEquals(1L, indexCount);
+            assertEquals(1L, typeCount);
+            assertEquals(1L, opCount);
+            assertEquals(1L, emptyScriptCount);
+            assertEquals(1L, falseScriptedUpsertCount);
+            assertEquals(1L, emptyDynamicTemplatesCount);
+            assertEquals(1L, emptyHeaderFields);
+        };
+
+        basicTest(failure, retry, success, consumer, null);
+    }
+
+    public void basicTest(int failure, int retry, int success, 
Consumer<List<IndexOperationRequest>> consumer, Map<String, String> attr) {
+        clientService.setEvalConsumer(consumer);
+        basicTest(failure, retry, success, attr);
+    }
+
+    public void basicTest(int failure, int retry, int success, Map<String, 
String> attr) {
+        if (attr != null) {
+            runner.enqueue(flowFileContents, attr);
+        } else {
+            runner.enqueue(flowFileContents);
+        }
+
+        runner.run();
+
+        runner.assertTransferCount(PutElasticsearchJson.REL_FAILURE, failure);
+        runner.assertTransferCount(PutElasticsearchJson.REL_RETRY, retry);
+        runner.assertTransferCount(PutElasticsearchJson.REL_SUCCESS, success);
+        runner.assertTransferCount(PutElasticsearchJson.REL_FAILED_DOCUMENTS, 
0);
+        runner.assertTransferCount(PutElasticsearchRecord.REL_ERROR_RESPONSES, 
0);
+
+        assertEquals(success, runner.getProvenanceEvents().stream()
+                .filter(e -> ProvenanceEventType.SEND == e.getEventType() && 
e.getDetails() == null)
+                .count());
+    }
+
+    @Test
+    public void simpleTest() {
+        clientService.setEvalParametersConsumer((Map<String, String> params) 
-> assertTrue(params.isEmpty()));
+
+        basicTest(0, 0, 1);
+    }
+
+    @Test
+    public void simpleTestWithDocIdAndRequestParametersAndBulkHeaders() {
+        runner.setProperty("refresh", "true");
+        runner.setProperty(AbstractPutElasticsearch.BULK_HEADER_PREFIX + 
"routing", "1");
+        runner.setProperty(AbstractPutElasticsearch.BULK_HEADER_PREFIX + 
"version", "${version}");
+        runner.setProperty(AbstractPutElasticsearch.BULK_HEADER_PREFIX + 
"empty", "${empty}");
+        runner.setProperty("slices", "${slices}");
+        runner.setProperty("another", "${blank}");
+        runner.setVariable("slices", "auto");
+        runner.setVariable("blank", " ");
+        runner.setVariable("version", "external");
+        runner.assertValid();
+
+        clientService.setEvalParametersConsumer((Map<String, String> params) 
-> {
+            assertEquals(2, params.size());
+            assertEquals("true", params.get("refresh"));
+            assertEquals("auto", params.get("slices"));
+        });
+
+        clientService.setEvalConsumer((List<IndexOperationRequest> items) -> {
+            long idCount = items.stream().filter(item -> 
"123".equals(item.getId())).count();
+            long indexCount = items.stream().filter(item -> 
"test_index".equals(item.getIndex())).count();
+            long typeCount = items.stream().filter(item -> 
"test_type".equals(item.getType())).count();
+            long opCount = items.stream().filter(item -> 
IndexOperationRequest.Operation.Index.equals(item.getOperation())).count();
+            long headerFieldsCount = items.stream().filter(item -> 
!item.getHeaderFields().isEmpty()).count();
+            assertEquals(1L, idCount);
+            assertEquals(1L, indexCount);
+            assertEquals(1L, typeCount);
+            assertEquals(1L, opCount);
+            assertEquals(1L, headerFieldsCount);
+
+            Map<String, String> headerFields = items.get(0).getHeaderFields();
+            assertEquals(2, headerFields.size());
+            assertEquals("1", headerFields.get("routing"));
+            assertEquals("external", headerFields.get("version"));
+        });
+
+        basicTest(0, 0, 1, Collections.singletonMap("doc_id", "123"));
+    }
+
+    @Test
+    public void simpleTestWithRequestParametersAndBulkHeadersFlowFileEL() {
+        runner.setProperty("refresh", "true");
+        runner.setProperty("slices", "${slices}");
+        runner.setVariable("blank", " ");
+        runner.setProperty(AbstractPutElasticsearch.BULK_HEADER_PREFIX + 
"routing", "1");
+        runner.setProperty(AbstractPutElasticsearch.BULK_HEADER_PREFIX + 
"version", "${version}");
+        runner.setProperty(AbstractPutElasticsearch.BULK_HEADER_PREFIX + 
"empty", "${empty}");
+        runner.assertValid();
+
+        clientService.setEvalParametersConsumer((Map<String, String> params) 
-> {
+            assertEquals(2, params.size());
+            assertEquals("true", params.get("refresh"));
+            assertEquals("auto", params.get("slices"));
+        });
+
+        clientService.setEvalConsumer((List<IndexOperationRequest> items) -> {
+            long nullIdCount = items.stream().filter(item -> item.getId() == 
null).count();
+            long headerFieldsCount = items.stream().filter(item -> 
!item.getHeaderFields().isEmpty()).count();
+            assertEquals(1L, nullIdCount);
+            assertEquals(1L, headerFieldsCount);
+
+            Map<String, String> headerFields = items.get(0).getHeaderFields();
+            assertEquals(2, headerFields.size());
+            assertEquals("1", headerFields.get("routing"));
+            assertEquals("external", headerFields.get("version"));
+        });
+
+        Map<String, String> attributes = new LinkedHashMap<>();
+        attributes.put("slices", "auto");
+        attributes.put("version", "external");
+        attributes.put("blank", " ");
+        attributes.put("doc_id", "");
+        basicTest(0, 0, 1, attributes);
+    }
+
+    @Test
+    public void simpleTestWithScriptAndDynamicTemplates() throws Exception {
+        runner.setProperty(PutElasticsearchJson.SCRIPT, 
Files.readString(Paths.get(TEST_DIR,"script.json")));
+        runner.setProperty(PutElasticsearchJson.DYNAMIC_TEMPLATES, 
Files.readString(Paths.get(TEST_COMMON_DIR,"dynamicTemplates.json")));
+        final Map<String, Object> expectedScript = new LinkedHashMap<>();
+        expectedScript.put("_source", "some script");
+        expectedScript.put("language", "painless");
+        final Map<String, Object> expectedDynamicTemplate = new 
LinkedHashMap<>();
+        expectedDynamicTemplate.put("my_field", "keyword");
+        final Map<String, Object> yourField = new LinkedHashMap<>();
+        yourField.put("type", "text");
+        yourField.put("keyword", Collections.singletonMap("type", "text"));
+        expectedDynamicTemplate.put("your_field", yourField);
+        Consumer<List<IndexOperationRequest>> consumer = 
(List<IndexOperationRequest> items) -> {
+            long scriptCount = items.stream().filter(item -> 
item.getScript().equals(expectedScript)).count();
+            long falseScriptedUpsertCount = items.stream().filter(item -> 
!item.isScriptedUpsert()).count();
+            long dynamicTemplatesCount = items.stream().filter(item -> 
item.getDynamicTemplates().equals(expectedDynamicTemplate)).count();
+            assertEquals(1L, scriptCount);
+            assertEquals(1L, falseScriptedUpsertCount);
+            assertEquals(1L, dynamicTemplatesCount);
+        };
+        basicTest(0, 0, 1, consumer, null);
+
+        runner.clearTransferState();
+        runner.clearProvenanceEvents();
+
+        runner.setProperty(PutElasticsearchJson.INDEX_OP, 
IndexOperationRequest.Operation.Upsert.getValue().toLowerCase());

Review Comment:
   Should line 260-266 be its own unit test? If so what should it be named? 
Perhaps simpleTestWithNonJsonScriptedUpsert.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to