Search in sources :

Example 1 with JsonText

use of com.axway.ats.action.json.JsonText in project ats-framework by Axway.

the class RestOperationTests method get_json.

/**
 * A GET which returns some JSON body.
 *
 * We read the response body as JsonText and use its common methods to retrieve the needed data
 */
@Test
public void get_json() {
    createPersonToManipulateInTheTest();
    RestClient client = new RestClient(BASE_URI + "peoplepage/get_jsonPojo");
    client.addResourcePath("Chuck");
    client.setResponseMediaType(RestMediaType.APPLICATION_JSON);
    RestResponse response = client.get();
    // get the body as JsonText
    JsonText jsonResponse = response.getBodyAsJson();
    // now call the different getter methods to retrieve the needed data
    String firstName = jsonResponse.getString("firstName");
    String lastName = jsonResponse.getString("lastName");
    int age = jsonResponse.getInt("age");
    log.info("Got back a JSON body saying that " + firstName + " " + lastName + " is " + age + " old");
    log.info("A JSON body looks like that:\n" + jsonResponse.toString());
    log.info("A well formatter JSON body looks like that:\n" + jsonResponse.toFormattedString());
}
Also used : RestResponse(com.axway.ats.action.rest.RestResponse) JsonText(com.axway.ats.action.json.JsonText) RestClient(com.axway.ats.action.rest.RestClient) Test(org.testng.annotations.Test)

Example 2 with JsonText

use of com.axway.ats.action.json.JsonText in project ats-framework by Axway.

the class HttpOperationTests method get_json.

/**
 * A GET which returns some JSON body.
 *
 * We read the response body as JsonText and use its common methods to retrieve the needed data
 */
@Test
public void get_json() {
    createPersonToManipulateInTheTest();
    HttpClient client = new HttpClient(BASE_URI + "peoplepage/get_jsonPojo");
    client.addResourcePath("Chuck");
    HttpResponse response = client.get();
    // get the body as JsonText
    JsonText jsonResponse = response.getBodyAsJsonText();
    // now call the different getter methods to retrieve the needed data
    String firstName = jsonResponse.getString("firstName");
    String lastName = jsonResponse.getString("lastName");
    int age = jsonResponse.getInt("age");
    log.info("Got back a JSON body saying that " + firstName + " " + lastName + " is " + age + " old");
    log.info("A JSON body looks like that:\n" + jsonResponse.toString());
    log.info("A well formatter JSON body looks like that:\n" + jsonResponse.toFormattedString());
}
Also used : JsonText(com.axway.ats.action.json.JsonText) HttpClient(com.axway.ats.action.http.HttpClient) HttpResponse(com.axway.ats.action.http.HttpResponse) Test(org.testng.annotations.Test)

Example 3 with JsonText

use of com.axway.ats.action.json.JsonText in project ats-framework by Axway.

the class Test_JsonText method addArray_intValues.

@Test
public void addArray_intValues() {
    JsonText json = new JsonText().addArray("name", new int[] { 2, 45, 34, 23, 7, 5, 3 });
    assertEquals("{\"name\":[2,45,34,23,7,5,3]}", json.toString());
}
Also used : JsonText(com.axway.ats.action.json.JsonText) BaseTest(com.axway.ats.action.BaseTest) Test(org.junit.Test)

Example 4 with JsonText

use of com.axway.ats.action.json.JsonText in project ats-framework by Axway.

the class Test_JsonText method addArrayWhenNotAllowed.

@Test
public void addArrayWhenNotAllowed() {
    try {
        new JsonText().add("name", new int[] { 1, 2, 3 });
        fail();
    } catch (JsonException e) {
        checkError(e, "Use the appropriate method to add.*");
    }
}
Also used : JsonException(com.axway.ats.action.exceptions.JsonException) JsonText(com.axway.ats.action.json.JsonText) BaseTest(com.axway.ats.action.BaseTest) Test(org.junit.Test)

Example 5 with JsonText

use of com.axway.ats.action.json.JsonText in project ats-framework by Axway.

the class Test_JsonText method getAllValueTypes.

@Test
public void getAllValueTypes() {
    JsonText json1 = new JsonText(body1);
    assertEquals("Acme 1006", json1.getString("name"));
    assertEquals(false, json1.getBoolean("enabled"));
    assertEquals(100, json1.getInt("integer"));
    assertEquals(100.2, json1.getFloat("float"), 0.1);
    assertEquals(null, json1.get("null_object"));
    JsonText json2 = new JsonText(body2);
    assertEquals(5, json2.getInt("lotto/lottoId"));
    assertEquals(54, json2.getInt("lotto/winners[1]/winnerId"));
    assertEquals(11, json2.getInt("lotto/winners[1]/numbers[3]"));
    assertEquals(45, json2.getInt("lotto/winning-numbers[1]"));
}
Also used : JsonText(com.axway.ats.action.json.JsonText) BaseTest(com.axway.ats.action.BaseTest) Test(org.junit.Test)

Aggregations

JsonText (com.axway.ats.action.json.JsonText)20 BaseTest (com.axway.ats.action.BaseTest)16 Test (org.junit.Test)16 JsonException (com.axway.ats.action.exceptions.JsonException)4 Test (org.testng.annotations.Test)2 RestException (com.axway.ats.action.exceptions.RestException)1 HttpClient (com.axway.ats.action.http.HttpClient)1 HttpResponse (com.axway.ats.action.http.HttpResponse)1 RestClient (com.axway.ats.action.rest.RestClient)1 RestResponse (com.axway.ats.action.rest.RestResponse)1 LinkedList (java.util.LinkedList)1