Search in sources :

Example 16 with JsonText

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

the class Test_JsonText method replaceValue.

@Test
public void replaceValue() {
    String keyPath = "name";
    JsonText jsonText1 = new JsonText(body1);
    assertEquals("Acme 1006", jsonText1.getString(keyPath));
    assertEquals("this name was changed", jsonText1.replace(keyPath, "this name was changed").getString(keyPath));
    JsonText jsonText2 = new JsonText(body2);
    keyPath = "lotto/lottoId";
    assertEquals(5, jsonText2.getInt(keyPath));
    assertEquals(15, jsonText2.replace(keyPath, 15).getInt(keyPath));
    keyPath = "lotto/winners[1]/winnerId";
    assertEquals(54, jsonText2.getInt(keyPath));
    assertEquals(64, jsonText2.replace(keyPath, 64).getInt(keyPath));
    keyPath = "lotto/winners[1]/numbers[2]";
    assertEquals(12, jsonText2.getInt(keyPath));
    assertEquals(13, jsonText2.replace(keyPath, 13).getInt(keyPath));
}
Also used : JsonText(com.axway.ats.action.json.JsonText) BaseTest(com.axway.ats.action.BaseTest) Test(org.junit.Test)

Example 17 with JsonText

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

the class Test_JsonText method removeValue.

@Test
public void removeValue() {
    String keyPath = "name";
    JsonText jsonText1 = new JsonText(body1);
    assertNotNull(jsonText1.get(keyPath));
    try {
        jsonText1.remove(keyPath).get(keyPath);
        fail();
    } catch (JsonException e) {
        checkError(e, ".*is not a valid path.*");
    }
    JsonText jsonText2 = new JsonText(body2);
    keyPath = "lotto/lottoId";
    assertNotNull(jsonText2.get(keyPath));
    try {
        assertNull(jsonText2.remove(keyPath).get(keyPath));
        fail();
    } catch (JsonException e) {
        checkError(e, ".*is not a valid path.*");
    }
    keyPath = "lotto/winners[1]/winnerId";
    assertNotNull(jsonText2.get(keyPath));
    try {
        assertNull(jsonText2.remove(keyPath).get(keyPath));
        fail();
    } catch (JsonException e) {
        checkError(e, ".*is not a valid path.*");
    }
    keyPath = "lotto/winners[1]/numbers[2]";
    assertEquals(12, jsonText2.getInt(keyPath));
    assertEquals(11, jsonText2.remove(keyPath).getInt(keyPath));
    assertEquals(18, jsonText2.remove(keyPath).getInt(keyPath));
    assertEquals(22, jsonText2.remove(keyPath).getInt(keyPath));
    try {
        assertEquals(18, jsonText2.remove(keyPath).getInt(keyPath));
        fail();
    } catch (JsonException e) {
        checkError(e, ".*Cannot remove item at positin 3 as there are only 2 items present.*");
    }
    try {
        new JsonText(body2).remove("lotto/winning-numbers[100]");
        fail();
    } catch (JsonException e) {
        checkError(e, ".*Cannot remove item at position 101 as there are only 7 items present.*");
    }
    try {
        new JsonText(body2).remove("lotto/non-existing-element");
        fail();
    } catch (JsonException e) {
        checkError(e, ".*Cannot remove JSON item .* as it does not exist.*");
    }
    try {
        new JsonText(body2).remove("lotto/winning-numbers[XYZ]");
        fail();
    } catch (JsonException e) {
        checkError(e, ".*Cannot remove JSON item .* as it does not exist.*");
    }
    new JsonText(body3).remove("[0]/id");
    new JsonText(body3).remove("[0]");
    new JsonText(body2).remove("lotto/winners/numbers");
    new JsonText(body2).remove("lotto/winning-numbers");
    try {
        new JsonText(body3).remove("[0]/non-existing-element");
        fail();
    } catch (JsonException e) {
        checkError(e, ".*Cannot remove JSON item .* as it does not exist.*");
    }
    try {
        new JsonText(body2).remove("[XYZ]");
        fail();
    } catch (JsonException e) {
        checkError(e, ".*is not a valid path.*");
    }
    try {
        new JsonText(body3).remove("[XYZ]");
        fail();
    } catch (JsonException e) {
        checkError(e, ".*is not a valid path.*");
    }
    try {
        new JsonText(body2).remove("[-10]");
        fail();
    } catch (JsonException e) {
        checkError(e, ".*is not a valid path.*");
    }
    try {
        new JsonText(body3).remove("[-10]");
        fail();
    } catch (JsonException e) {
        checkError(e, ".*is not a valid path.*");
    }
    try {
        new JsonText(body2).remove("[10000]");
        fail();
    } catch (JsonException e) {
        checkError(e, ".*is not a valid path.*");
    }
    try {
        new JsonText(body3).remove("[10000]");
        fail();
    } catch (JsonException e) {
        checkError(e, ".*is not a valid path.*");
    }
    try {
        new JsonText(body2).remove("not_existing[10000]");
        fail();
    } catch (JsonException e) {
        checkError(e, ".*is not a valid path.*");
    }
    try {
        new JsonText(body3).remove("not_existing[10000]");
        fail();
    } catch (JsonException e) {
        checkError(e, ".*is not a valid path.*");
    }
    try {
        new JsonText(body2).remove("not_existing[0]");
        fail();
    } catch (JsonException e) {
        checkError(e, ".*is not a valid path.*");
    }
    try {
        new JsonText(body3).remove("not_existing[0]");
        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 18 with JsonText

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

the class Test_JsonText method topLevelArray_getAllValueTypes.

@Test
public void topLevelArray_getAllValueTypes() {
    JsonText jsonText = new JsonText(body3);
    assertEquals("9fc1f799-cb49-440e-ae68-a5dd1d1f8dd1", jsonText.getString("[0]/id"));
    assertEquals(false, jsonText.getBoolean("[2]/enabled"));
    assertEquals(100, jsonText.getInt("[2]/integer"));
    assertEquals(100.2, jsonText.getFloat("[2]/float"), 0.1);
    assertEquals(null, jsonText.get("[2]/null_object"));
}
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)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