Search in sources :

Example 21 with JsonPath

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

the class JsonPathTest method testTwoNtpServersPresent.

@Test
public void testTwoNtpServersPresent() {
    String path = "$.nodes[*][?(!([\"1.2.3.4\", \"5.6.7.8\"] subsetof @.ntpServers))].ntpServers";
    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)

Example 22 with JsonPath

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

the class JsonPathTest method testPath2.

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

Example 23 with JsonPath

use of com.jayway.jsonpath.JsonPath in project TOSCAna by StuPro-TOSCAna.

the class TransformationControllerTest method getInputsTest.

@Test
public void getInputsTest() throws Exception {
    preInitNonCreationTests();
    MvcResult result = mvc.perform(get(INPUTS_VALID_URL)).andDo(print()).andExpect(status().is(200)).andExpect(content().contentType(DEFAULT_CHARSET_HAL_JSON)).andExpect(jsonPath("$.inputs").isArray()).andExpect(jsonPath("$.inputs").isNotEmpty()).andExpect(jsonPath("$.inputs[0].key").isString()).andExpect(jsonPath("$.inputs[0].type").isString()).andExpect(jsonPath("$.inputs[0].description").isString()).andExpect(jsonPath("$.inputs[0].required").isBoolean()).andExpect(jsonPath("$.links[0].rel").value("self")).andExpect(jsonPath("$.links[0].href").value("http://localhost/api/csars/kubernetes-cluster/transformations/p-a/inputs")).andReturn();
    MockHttpServletResponse response = result.getResponse();
    String responseJson = new String(response.getContentAsByteArray());
    String[] values = JsonPath.parse(responseJson).read("$.inputs[*].value", String[].class);
    long nullCount = Arrays.asList(values).stream().filter(Objects::isNull).count();
    long testCount = Arrays.asList(values).stream().filter(e -> e != null && e.equals(INPUT_TEST_DEFAULT_VALUE)).count();
    assertEquals(8, nullCount);
    assertEquals(1, testCount);
}
Also used : TargetArtifact(org.opentosca.toscana.core.transformation.artifacts.TargetArtifact) Arrays(java.util.Arrays) MockMvcResultMatchers.jsonPath(org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath) Random(java.util.Random) TransformationService(org.opentosca.toscana.core.transformation.TransformationService) JSONObject(org.json.JSONObject) PlatformNotFoundException(org.opentosca.toscana.api.exceptions.PlatformNotFoundException) Map(java.util.Map) OutputProperty(org.opentosca.toscana.core.transformation.properties.OutputProperty) MockMvcRequestBuilders.put(org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put) MockMvcResultHandlers.print(org.springframework.test.web.servlet.result.MockMvcResultHandlers.print) PropertyType(org.opentosca.toscana.core.transformation.properties.PropertyType) MediaType(org.springframework.http.MediaType) PlatformInput(org.opentosca.toscana.core.transformation.properties.PlatformInput) Set(java.util.Set) Csar(org.opentosca.toscana.core.csar.Csar) Objects(java.util.Objects) List(java.util.List) Log(org.opentosca.toscana.core.transformation.logging.Log) LogEntry(org.opentosca.toscana.core.transformation.logging.LogEntry) PlatformService(org.opentosca.toscana.core.transformation.platform.PlatformService) Optional(java.util.Optional) TransformationState(org.opentosca.toscana.core.transformation.TransformationState) TestCsars(org.opentosca.toscana.core.testdata.TestCsars) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Transformation(org.opentosca.toscana.core.transformation.Transformation) HashMap(java.util.HashMap) Mockito.spy(org.mockito.Mockito.spy) MockMvcResultMatchers.content(org.springframework.test.web.servlet.result.MockMvcResultMatchers.content) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) ArrayList(java.util.ArrayList) TransformationImpl(org.opentosca.toscana.core.transformation.TransformationImpl) HashSet(java.util.HashSet) MockMvc(org.springframework.test.web.servlet.MockMvc) MockMvcRequestBuilders.delete(org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete) Lists(com.google.common.collect.Lists) MockMvcResultMatchers.status(org.springframework.test.web.servlet.result.MockMvcResultMatchers.status) MockMvcRequestBuilders.post(org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post) Platform(org.opentosca.toscana.core.transformation.platform.Platform) MvcResult(org.springframework.test.web.servlet.MvcResult) MockMvcBuilders(org.springframework.test.web.servlet.setup.MockMvcBuilders) CsarImpl(org.opentosca.toscana.core.csar.CsarImpl) Before(org.junit.Before) ByteArrayUtils(org.opentosca.toscana.core.testdata.ByteArrayUtils) Assert.assertTrue(org.junit.Assert.assertTrue) FileUtils(org.apache.commons.io.FileUtils) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) JsonPath(com.jayway.jsonpath.JsonPath) File(java.io.File) CsarService(org.opentosca.toscana.core.csar.CsarService) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) BaseSpringTest(org.opentosca.toscana.core.BaseSpringTest) Level(ch.qos.logback.classic.Level) HALRelationUtils(org.opentosca.toscana.api.utils.HALRelationUtils) MockMvcRequestBuilders.get(org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get) Collections(java.util.Collections) JSONArray(org.json.JSONArray) Assert.assertEquals(org.junit.Assert.assertEquals) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) MvcResult(org.springframework.test.web.servlet.MvcResult) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test) BaseSpringTest(org.opentosca.toscana.core.BaseSpringTest)

Example 24 with JsonPath

use of com.jayway.jsonpath.JsonPath in project pentaho-kettle by pentaho.

the class FastJsonReader method evalCombinedResult.

private List<List<?>> evalCombinedResult() throws JsonInputException {
    int lastSize = -1;
    String prevPath = null;
    List<List<?>> results = new ArrayList<>(compiledJsonPaths.length);
    int i = 0;
    for (JsonPath path : compiledJsonPaths) {
        List<Object> result = getReadContext().read(path);
        if (result.size() != lastSize && lastSize > 0 && !result.isEmpty()) {
            throw new JsonInputException(BaseMessages.getString(PKG, "JsonInput.Error.BadStructure", result.size(), inputFields[i].getPath(), prevPath, lastSize));
        }
        if (!isIgnoreMissingPath() && (isAllNull(result) || result.isEmpty())) {
            throw new JsonInputException(BaseMessages.getString(PKG, "JsonReader.Error.CanNotFindPath", inputFields[i].getPath()));
        }
        results.add(result);
        lastSize = result.size();
        prevPath = inputFields[i].getPath();
        i++;
    }
    return results;
}
Also used : ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) JsonPath(com.jayway.jsonpath.JsonPath) JsonInputException(org.pentaho.di.trans.steps.jsoninput.exception.JsonInputException)

Example 25 with JsonPath

use of com.jayway.jsonpath.JsonPath in project bender by Nextdoor.

the class JsonPathProvider method delete.

public static void delete(Object json, String pathStr) {
    JsonPath path = getPath(pathStr);
    path.delete(json, CONFIG);
}
Also used : JsonPath(com.jayway.jsonpath.JsonPath)

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