+ The Validate JSON processor is based on the json-schema library. + The corresponding java documentation can be found + here. +
+ + + diff --git a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestValidateJson.java b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestValidateJson.java new file mode 100644 index 0000000000..b1e2bd353c --- /dev/null +++ b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestValidateJson.java @@ -0,0 +1,105 @@ +/* + * 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 java.io.IOException; +import java.nio.file.Paths; + +import org.apache.commons.io.IOUtils; + +import org.apache.nifi.util.TestRunner; +import org.apache.nifi.util.TestRunners; + +import org.junit.Test; +import org.xml.sax.SAXException; + +public class TestValidateJson { + + @Test + public void testValidJsonArraySchemaFile() throws IOException, SAXException { + final TestRunner runner = TestRunners.newTestRunner(new ValidateJson()); + runner.setProperty(ValidateJson.SCHEMA_FILE, "src/test/resources/TestJson/json-sample-schema.json"); + + runner.enqueue(Paths.get("src/test/resources/TestJson/json-sample.json")); + runner.run(); + + runner.assertAllFlowFilesTransferred(ValidateJson.REL_VALID, 1); + } + + @Test + public void testValidJsonObjectSchemaFile() throws IOException, SAXException { + final TestRunner runner = TestRunners.newTestRunner(new ValidateJson()); + runner.setProperty(ValidateJson.SCHEMA_FILE, "src/test/resources/TestJson/json-object-sample-schema.json"); + + runner.enqueue(Paths.get("src/test/resources/TestJson/json-object-sample.json")); + runner.run(); + + runner.assertAllFlowFilesTransferred(ValidateJson.REL_VALID, 1); + } + + @Test + public void testValidJsonArraySchemaBody() throws IOException, SAXException { + final TestRunner runner = TestRunners.newTestRunner(new ValidateJson()); + + String schemaBody = IOUtils.toString(getClass().getClassLoader().getResourceAsStream("TestJson/json-sample-schema.json"), "UTF-8"); + + runner.setProperty(ValidateJson.SCHEMA_BODY, schemaBody); + + runner.enqueue(Paths.get("src/test/resources/TestJson/json-sample.json")); + runner.run(); + + runner.assertAllFlowFilesTransferred(ValidateJson.REL_VALID, 1); + } + + @Test + public void testValidJsonObjectSchemaBody() throws IOException, SAXException { + final TestRunner runner = TestRunners.newTestRunner(new ValidateJson()); + String schemaBody = IOUtils.toString(getClass().getClassLoader().getResourceAsStream("TestJson/json-object-sample-schema.json"), "UTF-8"); + runner.setProperty(ValidateJson.SCHEMA_BODY, schemaBody); + + runner.enqueue(Paths.get("src/test/resources/TestJson/json-object-sample.json")); + runner.run(); + + runner.assertAllFlowFilesTransferred(ValidateJson.REL_VALID, 1); + } + + @Test + public void testInvalidJsonArraySchemaBody() throws IOException, SAXException { + final TestRunner runner = TestRunners.newTestRunner(new ValidateJson()); + + String schemaBody = "{\"type\": \"object\",\"required\": [\"missingField\"]}"; //invalid schema for JSONArray + + runner.setProperty(ValidateJson.SCHEMA_BODY, schemaBody); + + runner.enqueue(Paths.get("src/test/resources/TestJson/json-sample.json")); + runner.run(); + + runner.assertAllFlowFilesTransferred(ValidateJson.REL_INVALID, 1); + } + + @Test + public void testInvalidJsonObjectSchemaBody() throws IOException, SAXException { + final TestRunner runner = TestRunners.newTestRunner(new ValidateJson()); + String schemaBody = "{\"type\": \"object\",\"required\": [\"missingField\"]}"; //schema requires missingField + runner.setProperty(ValidateJson.SCHEMA_BODY, schemaBody); + + runner.enqueue(Paths.get("src/test/resources/TestJson/json-object-sample.json")); //json without missingField + runner.run(); + + runner.assertAllFlowFilesTransferred(ValidateJson.REL_INVALID, 1); + } +} \ No newline at end of file diff --git a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/resources/TestJson/json-object-sample-schema.json b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/resources/TestJson/json-object-sample-schema.json new file mode 100644 index 0000000000..7b0e293a43 --- /dev/null +++ b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/resources/TestJson/json-object-sample-schema.json @@ -0,0 +1,41 @@ +{ + "type": "object", + "required": ["_id", "index", "guid", "isActive", "balance", "picture", "age", "eyeColor", "name", "company", "email", "phone" ,"address", "about", "registered", "latitude", "longitude", "tags", "range", "friends", "greeting", "favoriteFruit"], + "properties": { + "_id": {"type": "string"}, + "index": {"type": "integer"}, + "guid": {"type": "string"}, + "isActive": { "type": "boolean"}, + "balance": {"type": "string"}, + "picture": {"type": "string"}, + "age": {"type": "integer"}, + "eyeColor": {"type": "string"}, + "name": { + "type": "object", + "properties": { + "first": {"type":"string"}, + "last": {"type":"string"} + } + }, + "company": {"type": "string"}, + "email": {"type": "string"}, + "phone": {"type": "string"}, + "address": {"type": "string"}, + "about": {"type": "string"}, + "registered": {"type": "string"}, + "latitude": {"type": "number"}, + "longitude": {"type": "number"}, + "tags": {"type": "array"}, + "range": {"type": "array"}, + "friends": { + "type": "array", + "required": ["id", "me"], + "properties": { + "id": {"type":"integer"}, + "name": {"type":"string"} + } + }, + "greeting": {"type": "string"}, + "favoriteFruit": {"type": "string"} + } +} \ No newline at end of file diff --git a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/resources/TestJson/json-object-sample.json b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/resources/TestJson/json-object-sample.json new file mode 100644 index 0000000000..e7aa0ce189 --- /dev/null +++ b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/resources/TestJson/json-object-sample.json @@ -0,0 +1,59 @@ +{ + "_id": "54df94072d5dbf7dc6340cc5", + "index": 0, + "guid": "b9f636cb-b939-42a9-b067-70d286116271", + "isActive": true, + "balance": "$3,200.07", + "picture": "http://placehold.it/32x32", + "age": 20, + "eyeColor": "brown", + "name": { + "first": "Shaffer", + "last": "Pearson" + }, + "company": "DATAGEN", + "email": "shaffer.pearson@datagen.co.uk", + "phone": "+1 (972) 588-2272", + "address": "662 Rewe Street, Starks, California, 9066", + "about": "Aliquip exercitation ad duis irure consectetur magna aliquip amet. Exercitation labore ex laboris non dolor eu. In magna amet non nulla sit laboris do aliqua aliquip. Est elit ipsum ad ea in Lorem mollit Lorem laborum. Ad labore minim aliqua dolore reprehenderit commodo nulla fugiat eiusmod nostrud cillum est. Deserunt minim in non aliqua non.\r\n", + "registered": "Wednesday, January 7, 2015 5:51 PM", + "latitude": -50.359159, + "longitude": -94.01781, + "tags": [ + "ea", + "enim", + "commodo", + "magna", + "sunt", + "dolore", + "aute" + ], + "range": [ + 0, + 1, + 2, + 3, + 4, + 5, + 6, + 7, + 8, + 9 + ], + "friends": [ + { + "id": 0, + "name": "Holloway Kim" + }, + { + "id": 1, + "name": "Clark Medina" + }, + { + "id": 2, + "name": "Rosemarie Salazar" + } + ], + "greeting": "Hello, Shaffer! You have 9 unread messages.", + "favoriteFruit": "apple" +} \ No newline at end of file diff --git a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/resources/TestJson/json-sample-schema.json b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/resources/TestJson/json-sample-schema.json new file mode 100644 index 0000000000..dd45c731a2 --- /dev/null +++ b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/resources/TestJson/json-sample-schema.json @@ -0,0 +1,42 @@ +{ + "type": "array", + "minItems": 1, + "items": { + "required": ["_id", "index", "guid", "isActive", "balance", "picture", "age", "eyeColor", "name", "company", "email", "phone" ,"address", "about", "registered", "latitude", "longitude", "tags", "range", "friends", "greeting", "favoriteFruit"], + "_id": {"type": "string"}, + "index": {"type": "integer"}, + "guid": {"type": "string"}, + "isActive": { "type": "boolean"}, + "balance": {"type": "string"}, + "picture": {"type": "string"}, + "age": {"type": "integer"}, + "eyeColor": {"type": "string"}, + "name": { + "type": "object", + "properties": { + "first": {"type":"string"}, + "last": {"type":"string"} + } + }, + "company": {"type": "string"}, + "email": {"type": "string"}, + "phone": {"type": "string"}, + "address": {"type": "string"}, + "about": {"type": "string"}, + "registered": {"type": "string"}, + "latitude": {"type": "number"}, + "longitude": {"type": "number"}, + "tags": {"type": "array"}, + "range": {"type": "array"}, + "friends": { + "type": "array", + "required": ["id", "me"], + "properties": { + "id": {"type":"integer"}, + "name": {"type":"string"} + } + }, + "greeting": {"type": "string"}, + "favoriteFruit": {"type": "string"} + } +} \ No newline at end of file