Adding an implementation of validation for JsonPath, providing a sample JSON file, and creating an associated test class.

This commit is contained in:
Aldrin Piri 2015-02-14 14:13:49 -05:00
parent e75213eead
commit da6b55f34c
3 changed files with 476 additions and 3 deletions

View File

@ -16,6 +16,9 @@
*/ */
package org.apache.nifi.processors.standard; package org.apache.nifi.processors.standard;
import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.InvalidPathException;
import com.jayway.jsonpath.JsonPath;
import org.apache.nifi.annotation.behavior.EventDriven; import org.apache.nifi.annotation.behavior.EventDriven;
import org.apache.nifi.annotation.behavior.SideEffectFree; import org.apache.nifi.annotation.behavior.SideEffectFree;
import org.apache.nifi.annotation.behavior.SupportsBatching; import org.apache.nifi.annotation.behavior.SupportsBatching;
@ -28,7 +31,11 @@ import org.apache.nifi.components.Validator;
import org.apache.nifi.flowfile.FlowFile; import org.apache.nifi.flowfile.FlowFile;
import org.apache.nifi.processor.*; import org.apache.nifi.processor.*;
import org.apache.nifi.processor.exception.ProcessException; import org.apache.nifi.processor.exception.ProcessException;
import org.apache.nifi.processor.io.InputStreamCallback;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.*; import java.util.*;
@EventDriven @EventDriven
@ -121,14 +128,25 @@ public class EvaluateJsonPath extends AbstractProcessor {
if (flowFile == null) { if (flowFile == null) {
return; return;
} }
processSession.read(flowFile, new InputStreamCallback() {
@Override
public void process(InputStream in) throws IOException {
// Parse the document once to support multiple path evaluations if specified
Object document = Configuration.defaultConfiguration().jsonProvider().parse(in, StandardCharsets.UTF_8.displayName());
}
});
} }
private static class JsonPathValidator implements Validator { private static class JsonPathValidator implements Validator {
@Override @Override
public ValidationResult validate(String subject, String input, ValidationContext context) { public ValidationResult validate(String subject, String input, ValidationContext context) {
return null; String error = null;
try {
JsonPath compile = JsonPath.compile(input);
} catch (InvalidPathException ipe) {
error = ipe.toString();
}
return new ValidationResult.Builder().valid(error == null).explanation(error).build();
} }
} }
} }

View File

@ -0,0 +1,40 @@
/*
* 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 org.apache.nifi.util.TestRunner;
import org.apache.nifi.util.TestRunners;
import org.junit.Assert;
import org.junit.Test;
import java.nio.file.Path;
import java.nio.file.Paths;
public class TestEvaluateJsonPath {
private static final Path JSON_SNIPPET = Paths.get("src/test/resources/TestJson/json-sample.json");
@Test(expected = AssertionError.class)
public void testInvalidJsonPath() {
final TestRunner testRunner = TestRunners.newTestRunner(new EvaluateJsonPath());
testRunner.setProperty(EvaluateJsonPath.DESTINATION, EvaluateJsonPath.DESTINATION_ATTRIBUTE);
testRunner.setProperty("invalid.jsonPath", "$..");
Assert.fail("An improper JsonPath expression was not detected as being invalid.");
}
}

View File

@ -0,0 +1,415 @@
[
{
"_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"
},
{
"_id": "54df94073ab1785758096418",
"index": 1,
"guid": "fda79e72-6489-41f5-bbd5-a5e7d2996dda",
"isActive": false,
"balance": "$1,416.15",
"picture": "http://placehold.it/32x32",
"age": 38,
"eyeColor": "blue",
"name": {
"first": "Frazier",
"last": "Ramsey"
},
"company": "STREZZO",
"email": "frazier.ramsey@strezzo.biz",
"phone": "+1 (909) 448-2724",
"address": "624 Cedar Street, Iola, North Carolina, 2827",
"about": "Sit sunt eiusmod irure ipsum Lorem irure aliquip cupidatat in proident dolore sunt adipisicing. Aute ipsum reprehenderit aute aliquip ad id pariatur dolor dolore et exercitation. Pariatur est adipisicing eu aliqua ea sint qui. Fugiat officia voluptate anim dolore cupidatat amet. Amet cillum dolor magna elit fugiat.\r\n",
"registered": "Sunday, January 5, 2014 1:18 PM",
"latitude": -14.729254,
"longitude": 126.396861,
"tags": [
"non",
"laboris",
"nulla",
"commodo",
"nostrud",
"qui",
"ea"
],
"range": [
0,
1,
2,
3,
4,
5,
6,
7,
8,
9
],
"friends": [
{
"id": 0,
"name": "Valenzuela Stone"
},
{
"id": 1,
"name": "King Munoz"
},
{
"id": 2,
"name": "Kari Woodard"
}
],
"greeting": "Hello, Frazier! You have 7 unread messages.",
"favoriteFruit": "strawberry"
},
{
"_id": "54df9407369a4d3f1b4aed39",
"index": 2,
"guid": "b6a68edb-4ddd-487b-b104-f02bec805e4c",
"isActive": true,
"balance": "$2,487.31",
"picture": "http://placehold.it/32x32",
"age": 27,
"eyeColor": "green",
"name": {
"first": "Cindy",
"last": "Shepherd"
},
"company": "EMTRAK",
"email": "cindy.shepherd@emtrak.org",
"phone": "+1 (867) 466-3223",
"address": "659 Colin Place, Vaughn, Washington, 1106",
"about": "Nulla sunt aliquip eiusmod occaecat duis officia eiusmod aliqua cillum ut. Irure eu est nulla dolor laborum eiusmod Lorem dolore culpa aliquip veniam duis. Sint cupidatat laboris commodo sunt consequat ullamco culpa ad labore. Velit do voluptate quis occaecat ex ipsum cupidatat occaecat dolor officia laborum labore.\r\n",
"registered": "Thursday, June 26, 2014 9:56 PM",
"latitude": 85.829527,
"longitude": -79.452723,
"tags": [
"cillum",
"do",
"veniam",
"dolore",
"voluptate",
"et",
"adipisicing"
],
"range": [
0,
1,
2,
3,
4,
5,
6,
7,
8,
9
],
"friends": [
{
"id": 0,
"name": "Decker Carver"
},
{
"id": 1,
"name": "Donaldson Burgess"
},
{
"id": 2,
"name": "Santana Heath"
}
],
"greeting": "Hello, Cindy! You have 8 unread messages.",
"favoriteFruit": "strawberry"
},
{
"_id": "54df94076f342042d027ca67",
"index": 3,
"guid": "ac591519-1642-4092-9646-17b4b7a9e38b",
"isActive": false,
"balance": "$3,480.12",
"picture": "http://placehold.it/32x32",
"age": 37,
"eyeColor": "green",
"name": {
"first": "Colon",
"last": "Gamble"
},
"company": "RONELON",
"email": "colon.gamble@ronelon.net",
"phone": "+1 (988) 431-2933",
"address": "472 Ryerson Street, Gwynn, Wyoming, 4200",
"about": "Ad duis nostrud laboris id aute reprehenderit veniam aute aute laborum exercitation laborum. In minim quis in sunt minim labore deserunt id dolor ea sit. Ipsum tempor Lorem aliqua ad sit quis duis exercitation quis. Dolore voluptate aute ut est non quis do aute exercitation consectetur reprehenderit proident quis.\r\n",
"registered": "Tuesday, July 29, 2014 1:38 PM",
"latitude": -9.922105,
"longitude": -170.581901,
"tags": [
"fugiat",
"incididunt",
"proident",
"laboris",
"id",
"ullamco",
"non"
],
"range": [
0,
1,
2,
3,
4,
5,
6,
7,
8,
9
],
"friends": [
{
"id": 0,
"name": "Shawn Collins"
},
{
"id": 1,
"name": "Holland West"
},
{
"id": 2,
"name": "Daniel Fischer"
}
],
"greeting": "Hello, Colon! You have 7 unread messages.",
"favoriteFruit": "strawberry"
},
{
"_id": "54df94075774d288fc86a912",
"index": 4,
"guid": "daec0340-7900-4a65-92fc-22e727577660",
"isActive": true,
"balance": "$3,042.74",
"picture": "http://placehold.it/32x32",
"age": 36,
"eyeColor": "brown",
"name": {
"first": "Carter",
"last": "Russo"
},
"company": "NORALEX",
"email": "carter.russo@noralex.biz",
"phone": "+1 (819) 543-3605",
"address": "147 Everit Street, Saticoy, Missouri, 5963",
"about": "Ea irure non pariatur ipsum. Magna eu enim anim Lorem quis sint cillum. Voluptate proident commodo dolor aute consectetur reprehenderit dolor nostrud ipsum cillum magna dolor. Reprehenderit sit consequat pariatur enim do occaecat exercitation reprehenderit.\r\n",
"registered": "Saturday, January 25, 2014 10:12 PM",
"latitude": -65.101248,
"longitude": 19.867506,
"tags": [
"dolore",
"et",
"ex",
"eu",
"nostrud",
"ex",
"ad"
],
"range": [
0,
1,
2,
3,
4,
5,
6,
7,
8,
9
],
"friends": [
{
"id": 0,
"name": "Felicia Hull"
},
{
"id": 1,
"name": "Jerri Mays"
},
{
"id": 2,
"name": "Jo Justice"
}
],
"greeting": "Hello, Carter! You have 7 unread messages.",
"favoriteFruit": "apple"
},
{
"_id": "54df940741be468e58e87dd3",
"index": 5,
"guid": "16a037a3-fe30-4c51-8d09-f24ad54f4719",
"isActive": true,
"balance": "$1,979.92",
"picture": "http://placehold.it/32x32",
"age": 20,
"eyeColor": "blue",
"name": {
"first": "Claudia",
"last": "Houston"
},
"company": "FISHLAND",
"email": "claudia.houston@fishland.com",
"phone": "+1 (860) 498-3802",
"address": "821 Remsen Avenue, Ada, Vermont, 3101",
"about": "Lorem eu deserunt et non id consectetur laborum voluptate id magna labore. Dolore enim voluptate mollit culpa cupidatat officia do aute voluptate Lorem commodo. Nisi nostrud amet in labore ullamco nisi magna adipisicing voluptate aliquip qui consequat enim. Pariatur adipisicing nostrud ut deserunt ad excepteur. Lorem do voluptate adipisicing et laborum commodo nulla excepteur laborum quis tempor proident velit.\r\n",
"registered": "Thursday, August 7, 2014 7:48 AM",
"latitude": 34.6075,
"longitude": -2.643176,
"tags": [
"enim",
"eu",
"sint",
"qui",
"elit",
"laboris",
"commodo"
],
"range": [
0,
1,
2,
3,
4,
5,
6,
7,
8,
9
],
"friends": [
{
"id": 0,
"name": "Boyd Morrison"
},
{
"id": 1,
"name": "Wendi Sandoval"
},
{
"id": 2,
"name": "Mindy Bush"
}
],
"greeting": "Hello, Claudia! You have 8 unread messages.",
"favoriteFruit": "apple"
},
{
"_id": "54df9407fbfc2103751de2e7",
"index": 6,
"guid": "60241980-5362-41dd-b6e5-e55f174904cf",
"isActive": true,
"balance": "$3,106.83",
"picture": "http://placehold.it/32x32",
"age": 40,
"eyeColor": "green",
"name": {
"first": "Beulah",
"last": "Myers"
},
"company": "UNI",
"email": "beulah.myers@uni.tv",
"phone": "+1 (969) 407-3571",
"address": "661 Matthews Court, Osage, Delaware, 1167",
"about": "Officia ipsum reprehenderit in nostrud Lorem labore consectetur nulla quis officia ullamco. Eiusmod ipsum deserunt consectetur cillum et duis do esse veniam occaecat Lorem dolor consequat. Lorem esse cupidatat aute et ut.\r\n",
"registered": "Sunday, January 25, 2015 8:22 PM",
"latitude": 72.620891,
"longitude": 155.859974,
"tags": [
"minim",
"fugiat",
"irure",
"culpa",
"exercitation",
"labore",
"commodo"
],
"range": [
0,
1,
2,
3,
4,
5,
6,
7,
8,
9
],
"friends": [
{
"id": 0,
"name": "Corina Francis"
},
{
"id": 1,
"name": "Vera Carson"
},
{
"id": 2,
"name": "Blevins Camacho"
}
],
"greeting": "Hello, Beulah! You have 8 unread messages.",
"favoriteFruit": "apple"
}
]