Search in sources :

Example 16 with JsonPath

use of com.jayway.jsonpath.JsonPath in project batfish by batfish.

the class Environment method computePath.

public ArrayNode computePath(String path) {
    ArrayNode pathResult = _pathCache.get(path);
    if (pathResult == null) {
        JsonPath jsonPath = JsonPath.compile(path);
        try {
            pathResult = jsonPath.read(_jsonObject, _configuration);
        } catch (PathNotFoundException e) {
            pathResult = JsonNodeFactory.instance.arrayNode();
        } catch (Exception e) {
            throw new BatfishException("Error reading JSON path: " + path, e);
        }
        _pathCache.put(path, pathResult);
    }
    return pathResult;
}
Also used : BatfishException(org.batfish.common.BatfishException) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) PathNotFoundException(com.jayway.jsonpath.PathNotFoundException) JsonPath(com.jayway.jsonpath.JsonPath) PathNotFoundException(com.jayway.jsonpath.PathNotFoundException) BatfishException(org.batfish.common.BatfishException)

Example 17 with JsonPath

use of com.jayway.jsonpath.JsonPath in project batfish by batfish.

the class JsonPathTest method testTwoNtpServersSanctionedViolation.

@Test
public void testTwoNtpServersSanctionedViolation() {
    String path = "$.nodes[*].ntpServers[?(@ nin [\"9.9.9.9\", \"5.6.7.8\"])]";
    JsonPath jsonPath = JsonPath.compile(path);
    ArrayNode prefixes = null;
    try {
        prefixes = jsonPath.read(_twoNtpServersNodesAnswerJsonObject, _prefixConfiguration);
    } catch (PathNotFoundException e) {
        prefixes = JsonNodeFactory.instance.arrayNode();
    }
    assertThat(prefixes, not(equalTo(nullValue())));
    assertThat(prefixes.size(), equalTo(1));
}
Also used : ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) PathNotFoundException(com.jayway.jsonpath.PathNotFoundException) JsonPath(com.jayway.jsonpath.JsonPath) Test(org.junit.Test)

Example 18 with JsonPath

use of com.jayway.jsonpath.JsonPath in project batfish by batfish.

the class JsonPathTest method testOneNtpServerSanctionedViolation.

@Test
public void testOneNtpServerSanctionedViolation() {
    String path = "$.nodes[*].ntpServers[?(@ nin [\"9.9.9.9\"])]";
    JsonPath jsonPath = JsonPath.compile(path);
    ArrayNode prefixes = null;
    try {
        prefixes = jsonPath.read(_oneNtpServerNodesAnswerJsonObject, _prefixConfiguration);
    } catch (PathNotFoundException e) {
        prefixes = JsonNodeFactory.instance.arrayNode();
    }
    assertThat(prefixes, not(equalTo(nullValue())));
    assertThat(prefixes.size(), equalTo(1));
}
Also used : ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) PathNotFoundException(com.jayway.jsonpath.PathNotFoundException) JsonPath(com.jayway.jsonpath.JsonPath) Test(org.junit.Test)

Example 19 with JsonPath

use of com.jayway.jsonpath.JsonPath in project batfish by batfish.

the class JsonPathTest method testPath1.

@Test
public void testPath1() {
    JsonPath jsonPath = JsonPath.compile("$.nodes[*][?(@.interfaces['Management1'].prefix)].vendorFamily.cisco.logging" + "[?(@.sourceInterface!='Management1')]");
    assertThat(jsonPath, not(equalTo(nullValue())));
}
Also used : JsonPath(com.jayway.jsonpath.JsonPath) Test(org.junit.Test)

Example 20 with JsonPath

use of com.jayway.jsonpath.JsonPath in project batfish by batfish.

the class JsonPathTest method testTwoNtpServersSanctioned.

@Test
public void testTwoNtpServersSanctioned() {
    String path = "$.nodes[*].ntpServers[?(@ nin [\"1.2.3.4\", \"5.6.7.8\"])]";
    JsonPath jsonPath = JsonPath.compile(path);
    ArrayNode prefixes = null;
    try {
        prefixes = jsonPath.read(_twoNtpServersNodesAnswerJsonObject, _prefixConfiguration);
    } catch (PathNotFoundException e) {
        prefixes = JsonNodeFactory.instance.arrayNode();
    }
    assertThat(prefixes, not(equalTo(nullValue())));
    assertThat(prefixes.size(), equalTo(0));
}
Also used : ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) PathNotFoundException(com.jayway.jsonpath.PathNotFoundException) JsonPath(com.jayway.jsonpath.JsonPath) Test(org.junit.Test)

Aggregations

JsonPath (com.jayway.jsonpath.JsonPath)61 Test (org.junit.Test)34 PathNotFoundException (com.jayway.jsonpath.PathNotFoundException)13 BaseTest (com.jayway.jsonpath.BaseTest)12 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)9 ArrayList (java.util.ArrayList)9 LinkedHashMap (java.util.LinkedHashMap)8 List (java.util.List)8 Map (java.util.Map)8 DocumentContext (com.jayway.jsonpath.DocumentContext)6 IOException (java.io.IOException)6 ComponentLog (org.apache.nifi.logging.ComponentLog)6 File (java.io.File)5 Arrays (java.util.Arrays)5 HashMap (java.util.HashMap)5 Collectors (java.util.stream.Collectors)5 DataType (org.apache.nifi.serialization.record.DataType)5 RecordField (org.apache.nifi.serialization.record.RecordField)5 Assert.assertEquals (org.junit.Assert.assertEquals)5 Assert.assertTrue (org.junit.Assert.assertTrue)5