Search in sources :

Example 36 with DocumentContext

use of com.jayway.jsonpath.DocumentContext in project JsonPath by json-path.

the class IssuesTest method issue_171.

@Test
public void issue_171() {
    String json = "{\n" + "  \"can delete\": \"this\",\n" + "  \"can't delete\": \"this\"\n" + "}";
    DocumentContext context = using(JACKSON_JSON_NODE_CONFIGURATION).parse(json);
    context.set("$.['can delete']", null);
    context.set("$.['can\\'t delete']", null);
    ObjectNode objectNode = context.read("$");
    assertThat(objectNode.get("can delete").isNull());
    assertThat(objectNode.get("can't delete").isNull());
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) DocumentContext(com.jayway.jsonpath.DocumentContext) BaseTest(com.jayway.jsonpath.BaseTest) Test(org.junit.Test)

Example 37 with DocumentContext

use of com.jayway.jsonpath.DocumentContext in project JsonPath by json-path.

the class JsonContextTest method cached_path_with_predicates.

@Test
public void cached_path_with_predicates() {
    Filter feq = Filter.filter(Criteria.where("category").eq("reference"));
    Filter fne = Filter.filter(Criteria.where("category").ne("reference"));
    DocumentContext JsonDoc = JsonPath.parse(JSON_DOCUMENT);
    List<String> eq = JsonDoc.read("$.store.book[?].category", feq);
    List<String> ne = JsonDoc.read("$.store.book[?].category", fne);
    Assertions.assertThat(eq).contains("reference");
    Assertions.assertThat(ne).doesNotContain("reference");
}
Also used : Filter(com.jayway.jsonpath.Filter) DocumentContext(com.jayway.jsonpath.DocumentContext) BaseTest(com.jayway.jsonpath.BaseTest) Test(org.junit.Test)

Example 38 with DocumentContext

use of com.jayway.jsonpath.DocumentContext in project credhub by cloudfoundry-incubator.

the class UserGenerationTest method whenGivenAUsernameAndPasswordParameters_usesUsernameAndGeneratesPassword.

@Test
public void whenGivenAUsernameAndPasswordParameters_usesUsernameAndGeneratesPassword() throws Exception {
    MockHttpServletRequestBuilder post = post("/api/v1/data").header("Authorization", "Bearer " + AuthConstants.UAA_OAUTH2_PASSWORD_GRANT_TOKEN).accept(APPLICATION_JSON).contentType(APPLICATION_JSON).content("{" + "\"name\": \"" + credentialName1 + "\"," + "\"type\": \"user\"," + "\"value\": {" + "\"username\": \"test-username\"" + "}," + "\"parameters\": {" + "\"length\": 40," + "\"exclude_upper\": true," + "\"exclude_lower\": true," + "\"exclude_number\": false," + "\"include_special\": false" + "}" + "}");
    final MockHttpServletResponse response = this.mockMvc.perform(post).andExpect(status().isOk()).andReturn().getResponse();
    final DocumentContext parsedResponse = JsonPath.parse(response.getContentAsString());
    final String password = parsedResponse.read("$.value.password");
    assertThat(password.length(), equalTo(40));
    assertThat(isDigits(password), equalTo(true));
    final String username = parsedResponse.read("$.value.username");
    assertThat(username, equalTo("test-username"));
}
Also used : MockHttpServletRequestBuilder(org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder) DocumentContext(com.jayway.jsonpath.DocumentContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 39 with DocumentContext

use of com.jayway.jsonpath.DocumentContext in project credhub by cloudfoundry-incubator.

the class UserGenerationTest method returnsAConsistentPasswordHash.

@Test
public void returnsAConsistentPasswordHash() throws Exception {
    MockHttpServletRequestBuilder postRequest = post("/api/v1/data").header("Authorization", "Bearer " + AuthConstants.UAA_OAUTH2_PASSWORD_GRANT_TOKEN).accept(APPLICATION_JSON).contentType(APPLICATION_JSON).content("{" + "\"name\": \"" + credentialName1 + "\"," + "\"type\": \"user\"," + "\"value\": {" + "\"username\": \"test-username\"" + "}" + "}");
    final MockHttpServletResponse postResponse = this.mockMvc.perform(postRequest).andExpect(status().isOk()).andReturn().getResponse();
    final MockHttpServletRequestBuilder getRequest = get("/api/v1/data?name=" + credentialName1).header("Authorization", "Bearer " + AuthConstants.UAA_OAUTH2_PASSWORD_GRANT_TOKEN).accept(APPLICATION_JSON).contentType(APPLICATION_JSON);
    final MockHttpServletResponse getResponse = this.mockMvc.perform(getRequest).andExpect(status().isOk()).andReturn().getResponse();
    final DocumentContext parsedPostResponse = JsonPath.parse(postResponse.getContentAsString());
    final DocumentContext parsedGetResponse = JsonPath.parse(getResponse.getContentAsString());
    final String postHash = parsedPostResponse.read("$.value.password_hash");
    final String getHash = parsedGetResponse.read("$.data[0].value.password_hash");
    assertThat(postHash, equalTo(getHash));
    assertThat(postHash.matches("^\\$6\\$[a-zA-Z0-9/.]+\\$[a-zA-Z0-9/.]+$"), equalTo(true));
}
Also used : MockHttpServletRequestBuilder(org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder) DocumentContext(com.jayway.jsonpath.DocumentContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 40 with DocumentContext

use of com.jayway.jsonpath.DocumentContext in project pom-manipulation-ext by release-engineering.

the class JSONIOTest method modifyFile.

@Test
public void modifyFile() throws ManipulationException, IOException {
    String deletePath = "$..resolved";
    DocumentContext doc = jsonIO.parseJSON(npmFile);
    doc.delete(deletePath);
    File target = tf.newFile();
    jsonIO.writeJSON(target, doc.jsonString());
    assertFalse(FileUtils.contentEquals(npmFile, target));
}
Also used : DocumentContext(com.jayway.jsonpath.DocumentContext) File(java.io.File) Test(org.junit.Test)

Aggregations

DocumentContext (com.jayway.jsonpath.DocumentContext)150 Test (org.junit.Test)106 HashMap (java.util.HashMap)14 BaseTest (com.jayway.jsonpath.BaseTest)12 Map (java.util.Map)12 PathNotFoundException (com.jayway.jsonpath.PathNotFoundException)9 JsonPath (com.jayway.jsonpath.JsonPath)7 File (java.io.File)7 List (java.util.List)7 JsonPathAssert (com.revinate.assertj.json.JsonPathAssert)6 Query (org.graylog.plugins.views.search.Query)6 MessageList (org.graylog.plugins.views.search.searchtypes.MessageList)6 Time (org.graylog.plugins.views.search.searchtypes.pivot.buckets.Time)6 Values (org.graylog.plugins.views.search.searchtypes.pivot.buckets.Values)6 DateTime (org.joda.time.DateTime)6 Configuration (com.jayway.jsonpath.Configuration)5 ArrayList (java.util.ArrayList)5 Test (org.junit.jupiter.api.Test)5 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)5 IOException (java.io.IOException)4