emiliosetiadarma commented on code in PR #6321:
URL: https://github.com/apache/nifi/pull/6321#discussion_r954356927


##########
nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestFlattenJson.java:
##########
@@ -0,0 +1,520 @@
+/*
+ * 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.standard;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.apache.nifi.util.MockFlowFile;
+import org.apache.nifi.util.TestRunner;
+import org.apache.nifi.util.TestRunners;
+import org.junit.jupiter.api.Test;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+public class TestFlattenJson {
+    private static final ObjectMapper mapper = new ObjectMapper();
+
+    @Test
+    void testFlatten() throws JsonProcessingException {
+        final TestRunner testRunner = 
TestRunners.newTestRunner(FlattenJson.class);
+        final String json = "{\n" +
+                "    \"test\": {\n" +
+                "        \"msg\": \"Hello, world\"\n" +
+                "    },\n" +
+                "    \"first\": {\n" +
+                "        \"second\": {\n" +
+                "            \"third\": [\n" +
+                "                \"one\",\n" +
+                "                \"two\",\n" +
+                "                \"three\",\n" +
+                "                \"four\",\n" +
+                "                \"five\"\n" +
+                "            ]\n" +
+                "        }\n" +
+                "    }\n" +
+                "}";
+        final Map parsed = (Map) baseTest(testRunner, json, 2);
+        assertEquals(parsed.get("test.msg"), "Hello, world", "test.msg should 
exist, but doesn't");
+        assertEquals(parsed.get("first.second.third"),
+                Arrays.asList("one", "two", "three", "four", "five"),
+                "Three level block doesn't exist.");
+    }
+
+    private Object baseTest(TestRunner testRunner, String json, int keyCount) 
throws JsonProcessingException {
+        return baseTest(testRunner, json, Collections.emptyMap(), keyCount);
+    }
+
+    private Object baseTest(TestRunner testRunner, String json, Map attrs, int 
keyCount) throws JsonProcessingException {
+        testRunner.enqueue(json, attrs);
+        testRunner.run(1, true);
+        testRunner.assertTransferCount(FlattenJson.REL_FAILURE, 0);
+        testRunner.assertTransferCount(FlattenJson.REL_SUCCESS, 1);
+
+        final List<MockFlowFile> flowFiles = 
testRunner.getFlowFilesForRelationship(FlattenJson.REL_SUCCESS);
+        final byte[] content = 
testRunner.getContentAsByteArray(flowFiles.get(0));
+        final String asJson = new String(content);
+        if (asJson.startsWith("[")) {
+            final List parsed;
+            parsed = mapper.readValue(asJson, List.class);
+            assertEquals(keyCount, parsed.size(), "Too many keys");
+            return parsed;
+        } else {
+            final Map parsed;
+            parsed = mapper.readValue(asJson, Map.class);
+            assertEquals(keyCount, parsed.size(), "Too many keys");
+            return parsed;
+        }
+    }

Review Comment:
   Moved it!



-- 
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