Java Code Examples for io.restassured.path.json.JsonPath#get()

The following examples show how to use io.restassured.path.json.JsonPath#get() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MongoDbTest.java    From camel-quarkus with Apache License 2.0 6 votes vote down vote up
@Test
public void testMongoDbComponent() {
    // Write to collection
    RestAssured.given()
            .contentType(ContentType.JSON)
            .body("{message:\"Hello Camel Quarkus Mongo DB\"}")
            .post("/mongodb/collection/camelTest")
            .then()
            .statusCode(201);

    // Retrieve from collection
    JsonPath jsonPath = RestAssured.get("/mongodb/collection/camelTest")
            .then()
            .contentType(ContentType.JSON)
            .statusCode(200)
            .extract()
            .body()
            .jsonPath();

    List<Map<String, String>> documents = jsonPath.get();
    assertEquals(1, documents.size());

    Map<String, String> document = documents.get(0);
    assertEquals("Hello Camel Quarkus Mongo DB", document.get("message"));
}
 
Example 2
Source File: TaskRestServiceInteractionTest.java    From camunda-bpm-platform with Apache License 2.0 6 votes vote down vote up
private void verifyTaskAttachmentValues(Attachment mockTaskAttachment, String responseContent, boolean urlExist) {
  JsonPath path = from(responseContent);
  String returnedId = path.get("id");
  String returnedTaskId = path.get("taskId");
  String returnedName = path.get("name");
  String returnedType = path.get("type");
  String returnedDescription = path.get("description");
  String returnedUrl = path.get("url");

  Attachment mockAttachment = mockTaskAttachments.get(0);

  assertEquals(mockAttachment.getId(), returnedId);
  assertEquals(mockAttachment.getTaskId(), returnedTaskId);
  assertEquals(mockAttachment.getName(), returnedName);
  assertEquals(mockAttachment.getType(), returnedType);
  assertEquals(mockAttachment.getDescription(), returnedDescription);
  if (urlExist) {
    assertEquals(mockAttachment.getUrl(), returnedUrl);
  }
}
 
Example 3
Source File: OpenTracingTest.java    From camel-quarkus with Apache License 2.0 5 votes vote down vote up
@Test
public void testTraceRoute() {
    // Generate messages
    for (int i = 0; i < 5; i++) {
        RestAssured.get("/opentracing/test/trace")
                .then()
                .statusCode(200);

        // No spans should be recorded for this route as they are excluded by camel.opentracing.exclude-patterns in
        // application.properties
        RestAssured.get("/opentracing/test/trace/filtered")
                .then()
                .statusCode(200);
    }

    // Retrieve recorded spans
    JsonPath jsonPath = RestAssured.given()
            .get("/opentracing/spans")
            .then()
            .statusCode(200)
            .extract()
            .body()
            .jsonPath();

    List<Map<String, String>> spans = jsonPath.get();
    assertEquals(5, spans.size());

    for (Map<String, String> span : spans) {
        assertEquals("server", span.get(Tags.SPAN_KIND.getKey()));
        assertEquals("camel-platform-http", span.get(Tags.COMPONENT.getKey()));
        assertEquals("200", span.get(Tags.HTTP_STATUS.getKey()));
        assertEquals("GET", span.get(Tags.HTTP_METHOD.getKey()));
        assertEquals("platform-http:///opentracing/test/trace?httpMethodRestrict=GET", span.get("camel.uri"));
        assertTrue(span.get(Tags.HTTP_URL.getKey()).endsWith("/opentracing/test/trace"));
    }
}
 
Example 4
Source File: TestCaseUtils.java    From heat with Apache License 2.0 5 votes vote down vote up
private void loadGeneralSettings(JsonPath testSuiteJsonPath) {
    Map<String, String> generalSettings = testSuiteJsonPath.get(JSONPATH_GENERAL_SETTINGS);
    if (generalSettings.containsKey(JSON_FIELD_HTTP_METHOD)) {
        try {
            httpMethod = Method.valueOf(generalSettings.get(JSON_FIELD_HTTP_METHOD));
        } catch (IllegalArgumentException oEx) {
            throw new HeatException("HTTP method '" + generalSettings.get(JSON_FIELD_HTTP_METHOD) + "' not supported");
        }
    }
    if (generalSettings.containsKey(SUITE_DESCRIPTION_PATH)) {
        suiteDescription = generalSettings.get(SUITE_DESCRIPTION_PATH);
    }

}
 
Example 5
Source File: TestCaseUtils.java    From heat with Apache License 2.0 5 votes vote down vote up
private void loadBeforeSuiteSection(JsonPath testSuiteJsonPath) {
    beforeSuiteVariables = testSuiteJsonPath.get(JSONPATH_BEFORE_SUITE_SECTION);
    if (beforeSuiteVariables != null && !beforeSuiteVariables.isEmpty()) {
        logUtils.debug("BEFORE SUITE VARIABLES PRESENT");
        placeholderHandler = new PlaceholderHandler();
        for (Map.Entry<String, Object> entry : beforeSuiteVariables.entrySet()) {
            beforeSuiteVariables.put(entry.getKey(), placeholderHandler.placeholderProcessString(entry.getValue().toString()));
            logUtils.debug("BEFORE SUITE VARIABLE: '{}' = '{}'", entry.getKey(), entry.getValue());
        }
    }
}
 
Example 6
Source File: TestCaseUtils.java    From heat with Apache License 2.0 5 votes vote down vote up
private Iterator<Object[]> getTestCaseIterator(JsonPath testSuiteJsonPath) {
    List<Object> testCases = testSuiteJsonPath.get(JSONPATH_TEST_CASES);
    List<Object[]> listOfArray = new ArrayList();
    for (Object testCase : testCases) {
        listOfArray.add(new Object[]{testCase});
    }
    tcArrayIterator = listOfArray.iterator();
    return tcArrayIterator;
}
 
Example 7
Source File: RestAssuredJSONExamplesTest.java    From tracksrestcasestudy with MIT License 5 votes vote down vote up
@Test
public void aJsonPathExampleFromResponse(){

    /*
        Use RestAssured to return response from HTTP to demonstrate same as using JsonPath on String
     */
    Response response = RestAssured.
            when().
            get(jsonendpoint).
            andReturn();

    JsonPath jsonPath = new JsonPath(response.body().asString());

    // multiple matches returned in an ArrayList
    List<HashMap<String,String>> ret = jsonPath.get("projects.project");
    Assert.assertEquals(6, ret.size());

    Assert.assertEquals("A New Projectaniheeiadtatd",
                        ret.get(0).get("name"));

    // can index on multiple matches with array indexing notation [1]
    Assert.assertEquals("the new name aniheeiaosono",jsonPath.get("projects.project[1].name"));
    Assert.assertEquals(3,jsonPath.getInt("projects.project[1].id"));

    // filters and finding stuff
    // find the project with id == '3' and return the name
    Assert.assertEquals("the new name aniheeiaosono",jsonPath.get("projects.project.find {it.id == 3}.name"));

    // use `findAll` to find all the items that match a condition
    ArrayList projectsWithIdLessThanSix = jsonPath.get("projects.project.findAll {it.id <= 6}");
    Assert.assertEquals(4,projectsWithIdLessThanSix.size());




}
 
Example 8
Source File: SearchApiTests.java    From fess with Apache License 2.0 5 votes vote down vote up
private static String createLabel() {
    Map<String, Object> labelBody = new HashMap<>();
    labelBody.put("name", TEST_LABEL);
    labelBody.put("value", TEST_LABEL);
    labelBody.put("included_paths", ".*tools.*");
    Response response = checkMethodBase(labelBody).put("/api/admin/labeltype/setting");
    JsonPath jsonPath = JsonPath.from(response.asString());
    assertTrue(jsonPath.getBoolean("response.created"));
    assertEquals(0, jsonPath.getInt("response.status"));
    return jsonPath.get("response.id");
}
 
Example 9
Source File: SearchApiTests.java    From fess with Apache License 2.0 5 votes vote down vote up
private static String createCrawlLabel() {
    Map<String, Object> labelBody = new HashMap<>();
    labelBody.put("name", CRAWL_LABEL);
    labelBody.put("value", CRAWL_LABEL);
    labelBody.put("included_paths", ".*");
    Response response = checkMethodBase(labelBody).put("/api/admin/labeltype/setting");
    JsonPath jsonPath = JsonPath.from(response.asString());
    assertTrue(jsonPath.getBoolean("response.created"));
    assertEquals(0, jsonPath.getInt("response.status"));
    return jsonPath.get("response.id");
}
 
Example 10
Source File: DeploymentRestServiceInteractionTest.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
private void verifyStandardDeploymentValues(Deployment mockDeployment, JsonPath path) {
  String returnedId = path.get("id");
  String returnedName = path.get("name");
  Date returnedDeploymentTime = DateTimeUtil.parseDate(path.<String>get("deploymentTime"));

  assertEquals(mockDeployment.getId(), returnedId);
  assertEquals(mockDeployment.getName(), returnedName);
  assertEquals(mockDeployment.getDeploymentTime(), returnedDeploymentTime);
}
 
Example 11
Source File: DeploymentRestServiceInteractionTest.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
private void verifyDeploymentResource(Resource mockDeploymentResource, Response response) {
  String content = response.asString();

  JsonPath path = from(content);
  String returnedId = path.get("id");
  String returnedName = path.get("name");
  String returnedDeploymentId = path.get("deploymentId");

  assertEquals(mockDeploymentResource.getId(), returnedId);
  assertEquals(mockDeploymentResource.getName(), returnedName);
  assertEquals(mockDeploymentResource.getDeploymentId(), returnedDeploymentId);
}
 
Example 12
Source File: TaskRestServiceInteractionTest.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
private void verifyTaskCommentValues(Comment mockTaskComment, String responseContent) {
  JsonPath path = from(responseContent);
  String returnedId = path.get("id");
  String returnedUserId = path.get("userId");
  String returnedTaskId = path.get("taskId");
  Date returnedTime = DateTimeUtil.parseDate(path.<String>get("time"));
  String returnedFullMessage = path.get("message");

  assertEquals(mockTaskComment.getId(), returnedId);
  assertEquals(mockTaskComment.getTaskId(), returnedTaskId);
  assertEquals(mockTaskComment.getUserId(), returnedUserId);
  assertEquals(mockTaskComment.getTime(), returnedTime);
  assertEquals(mockTaskComment.getFullMessage(), returnedFullMessage);
}
 
Example 13
Source File: TestCaseUtils.java    From heat with Apache License 2.0 4 votes vote down vote up
private void loadJsonSchemaForOutputValidation(JsonPath testSuiteJsonPath) {
    jsonSchemas = testSuiteJsonPath.get(JSONPATH_JSONSCHEMAS);
}
 
Example 14
Source File: WiremockSupportHandler.java    From heat with Apache License 2.0 4 votes vote down vote up
private  <T> T applyJsonPath(String httpResp, String jsonPath) {
    JsonPath jsonPathResp = new JsonPath(httpResp);
    T resp = jsonPathResp.get(jsonPath);
    return resp;
}
 
Example 15
Source File: RestAssuredJSONExamplesTest.java    From tracksrestcasestudy with MIT License 2 votes vote down vote up
@Test
public void aSetOfJsonPathExamples(){

    /*
        REST Assured documentation https://github.com/rest-assured/rest-assured/wiki/Usage
        
        JSON parsing https://github.com/rest-assured/rest-assured/wiki/Usage#json-example
        JsonPath - https://github.com/rest-assured/rest-assured/wiki/Usage#json-using-jsonpath

        JsonPath blog post with useful examples:
        https://blog.jayway.com/2013/04/12/whats-new-in-rest-assured-1-8/

        Deserialisation
        https://github.com/rest-assured/rest-assured/wiki/Usage#deserialization

     */
    File jsonExample = new File(System.getProperty("user.dir"),
            "src/test/resources/jsonxml/jsonexample.json");

    JsonPath jsonPath = new JsonPath(jsonExample);

    // multiple matches returned in an ArrayList
    ArrayList ret = jsonPath.get("projects.project");
    Assert.assertEquals(6, ret.size());

    // can index on multiple matches with array indexing notation [1]
    Assert.assertEquals("the new name aniheeiaosono",jsonPath.get("projects.project[1].name"));
    Assert.assertEquals(3,jsonPath.getInt("projects.project[1].id"));

    // can count backwards so -1 is last, -2 is second to last
    Assert.assertEquals(10,jsonPath.getInt("projects.project[-1].id"));

    // filters and finding stuff
    // find the project with id == '3' and return the name
    Assert.assertEquals("the new name aniheeiaosono",jsonPath.get("projects.project.find {it.id == 3}.name"));

    // use `findAll` to find all the items that match a condition
    // conditions can use Groovy so convert the id to an integer in the filter if it was a string with  `{it.id.toInteger() <= 6}`

    // use `it` in the filter condition to refer to currently matched item
    ArrayList projectsWithIdLessThanSix = jsonPath.get("projects.project.findAll {it.id <= 6}");
    Assert.assertEquals(4,projectsWithIdLessThanSix.size());



    // get an 'object' as a Map from the list
    Map<String, Object> project1map = jsonPath.getMap("projects.project[1]");
    Assert.assertEquals("active", project1map.get("state"));


    // set root to an element in the Json to make accessing it easier
    JsonPath project2 = new JsonPath(jsonExample).setRoot("projects.project[2]");
    Assert.assertEquals(5 ,project2.getInt("id"));
    Assert.assertEquals("active" ,project2.get("state"));




}