Search in sources :

Example 11 with JsonText

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

the class Test_JsonText method topLevelArray_removeValue.

@Test
public void topLevelArray_removeValue() {
    JsonText jsonText = new JsonText(body3);
    String keyPath = "[1]/type";
    assertNotNull(jsonText.get(keyPath));
    try {
        jsonText.remove(keyPath).getString(keyPath);
        fail();
    } catch (JsonException e) {
        checkError(e, ".*is not a valid path.*");
    }
}
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 12 with JsonText

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

the class Test_JsonText method iterateOverArrayElement.

@Test
public void iterateOverArrayElement() {
    //@formatter:off
    String[] expectedElementNames = new String[] { "id", "summary", "name", "type", "uri", "id", "summary", "name", "type", "uri", "enabled", "null_object", "integer", "string", "float" };
    //@formatter:on
    JsonText jsonText1 = new JsonText(body3);
    assertTrue(!jsonText1.isTopLevelObject());
    assertEquals(3, jsonText1.getNumberOfElements(""));
    List<String> actualElementNames = new LinkedList<String>();
    for (int i = 0; i < jsonText1.getNumberOfElements(""); i++) {
        JsonText jsonText2 = jsonText1.get("[" + i + "]");
        for (String name : jsonText2.getElementNames()) {
            actualElementNames.add(name);
        }
    }
    assertTrue(compareJSon(Arrays.asList(expectedElementNames), actualElementNames));
}
Also used : JsonText(com.axway.ats.action.json.JsonText) LinkedList(java.util.LinkedList) BaseTest(com.axway.ats.action.BaseTest) Test(org.junit.Test)

Example 13 with JsonText

use of com.axway.ats.action.json.JsonText 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)

Example 14 with JsonText

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

the class Test_JsonText method addArray_floatValues.

@Test
public void addArray_floatValues() {
    JsonText json = new JsonText().addArray("name", new float[] { 2.2f, 45.1f, 34.0f, 23.3f, 7f, 5f, 3f });
    assertEquals("{\"name\":[2.2,45.1,34.0,23.3,7.0,5.0,3.0]}", json.toString());
}
Also used : JsonText(com.axway.ats.action.json.JsonText) BaseTest(com.axway.ats.action.BaseTest) Test(org.junit.Test)

Example 15 with JsonText

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

the class Test_JsonText method getFromArray.

//    @Test
public void getFromArray() {
    JsonText[] jsons = new JsonText(body4).getArray("lotto/winners[]/winnerId");
    assertEquals(2, jsons.length);
    assertEquals(23, jsons[0].getInt(""));
    assertEquals(54, jsons[1].getInt(null));
    jsons = new JsonText(body4).getArray("lotto/winners[]");
    assertEquals(2, jsons.length);
    assertTrue(jsons[0].toString().contains("winnerId\":23"));
    assertTrue(jsons[0].toString().contains("city\":\"London"));
    assertTrue(jsons[1].toString().contains("winnerId\":54"));
    assertTrue(jsons[1].toString().contains("city\":\"Plovdiv"));
    jsons = new JsonText(body4).getArray("lotto/winners[]/numbers");
    assertEquals(2, jsons.length);
    assertEquals("[2,45,34,23,3,5]", jsons[0].getString(null));
    assertEquals("[52,3,12,11,18,22]", jsons[1].toString());
    jsons = new JsonText(body4).getArray("lotto/winners[]/numbers[]");
    assertEquals(12, jsons.length);
    assertEquals(2, jsons[0].getInt(null));
    assertEquals(5, jsons[5].getInt(null));
    assertEquals(52, jsons[6].getInt(null));
    assertEquals(22, jsons[11].getInt(null));
    jsons = new JsonText(body4).getArray("lotto/winners[]/numbers[3]");
    assertEquals(2, jsons.length);
    assertEquals(23, jsons[0].getInt(null));
    assertEquals(11, jsons[1].getInt(null));
    jsons = new JsonText(body4).getArray("lotto/winners[]/address");
    assertEquals(2, jsons.length);
    assertEquals("[{\"street\":\"Main\",\"city\":\"Paris\"},{\"street\":\"Glory\",\"city\":\"London\"}]", jsons[0].toString());
    assertEquals("[{\"street\":\"Vasil Levski\",\"city\":\"Sofia\"},{\"street\":\"Hristo Botev\",\"city\":\"Plovdiv\"}]", jsons[1].toString());
    jsons = new JsonText(body4).getArray("lotto/winners[]/address[]");
    assertEquals(4, jsons.length);
    assertEquals("{\"street\":\"Main\",\"city\":\"Paris\"}", jsons[0].toString());
    assertEquals("{\"street\":\"Glory\",\"city\":\"London\"}", jsons[1].toString());
    assertEquals("{\"street\":\"Vasil Levski\",\"city\":\"Sofia\"}", jsons[2].toString());
    assertEquals("{\"street\":\"Hristo Botev\",\"city\":\"Plovdiv\"}", jsons[3].toString());
    jsons = new JsonText(body4).getArray("lotto/winners[]/address[]/street");
    assertEquals(4, jsons.length);
    assertEquals("Main", jsons[0].getString(null));
    assertEquals("Glory", jsons[1].getString(null));
    assertEquals("Vasil Levski", jsons[2].getString(null));
    assertEquals("Hristo Botev", jsons[3].getString(null));
    jsons = new JsonText(body4).getArray("lotto/winners[]/address[1]/street");
    assertEquals(2, jsons.length);
    assertEquals("Glory", jsons[0].getString(""));
    assertEquals("Hristo Botev", jsons[1].getString(null));
    jsons = new JsonText(body4).getArray("lotto/winners[1]/address[1]/street");
    assertEquals(1, jsons.length);
    assertEquals("Hristo Botev", jsons[0].getString(""));
    jsons = new JsonText(body4).getArray("lotto/winners[1]/address[]/street");
    assertEquals(2, jsons.length);
    assertEquals("Vasil Levski", jsons[0].getString(""));
    assertEquals("Hristo Botev", jsons[1].getString(null));
}
Also used : JsonText(com.axway.ats.action.json.JsonText)

Aggregations

JsonText (com.axway.ats.action.json.JsonText)18 BaseTest (com.axway.ats.action.BaseTest)16 Test (org.junit.Test)16 JsonException (com.axway.ats.action.exceptions.JsonException)4 RestException (com.axway.ats.action.exceptions.RestException)1 LinkedList (java.util.LinkedList)1