Search in sources :

Example 1 with RestException

use of com.axway.ats.action.exceptions.RestException in project ats-framework by Axway.

the class Test_JsonText method scoptToInternalPart_topElementIsJsonObject.

@Test
public void scoptToInternalPart_topElementIsJsonObject() {
    // JSON Object
    JsonText json = new JsonText(body2);
    JsonText internalJson = json.get("lotto");
    assertEquals(json.get("lotto").toString(), internalJson.toString());
    // JSON Object -> JSON Array
    internalJson = json.get("lotto/winners");
    assertEquals("[{\"winnerId\":23,\"numbers\":[2,45,34,23,3,5]},{\"winnerId\":54,\"numbers\":[52,3,12,11,18,22]}]", internalJson.toString());
    // JSON Object -> JSON Array -> JSON Object
    internalJson = json.get("lotto/winners[1]");
    assertEquals("{\"winnerId\":54,\"numbers\":[52,3,12,11,18,22]}", internalJson.toString());
    // JSON Object -> JSON Array -> JSON Object -> JSON Array
    internalJson = json.get("lotto/winners[1]/numbers");
    assertEquals("[52,3,12,11,18,22]", internalJson.toString());
    // Getting a Number as JsonText fails
    try {
        json.get("lotto/winners[1]/numbers[3]");
    } catch (RestException e) {
        assertEquals("java.lang.Integer cannot be cast to org.json.JSONObject", e.getCause().getMessage());
    }
}
Also used : JsonText(com.axway.ats.action.json.JsonText) RestException(com.axway.ats.action.exceptions.RestException) BaseTest(com.axway.ats.action.BaseTest) Test(org.junit.Test)

Aggregations

BaseTest (com.axway.ats.action.BaseTest)1 RestException (com.axway.ats.action.exceptions.RestException)1 JsonText (com.axway.ats.action.json.JsonText)1 Test (org.junit.Test)1