Search in sources :

Example 1 with JsonException

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

the class Test_JsonText method getAllValueTypesNegative.

@Test
public void getAllValueTypesNegative() {
    JsonText json1 = new JsonText(body1);
    try {
        assertEquals(false, json1.getBoolean(""));
        fail();
    } catch (JsonException e) {
        checkError(e, ".*is not a valid path");
    }
    assertEquals(false, json1.getBoolean("name"));
    try {
        assertEquals(100, json1.getInt(null));
        fail();
    } catch (JsonException e) {
        checkError(e, ".*is not a valid path");
    }
    try {
        assertEquals(100, json1.getInt("name"));
        fail();
    } catch (JsonException e) {
        checkError(e, ".*does not point to a Integer value.*");
    }
    try {
        assertEquals(100.2, json1.getFloat(null), 0.1);
        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 2 with JsonException

use of com.axway.ats.action.exceptions.JsonException 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 3 with JsonException

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

the class JsonText method parseInternalJson.

/**
     * Inspects the JSON content and returns all matched entities into the 
     * provided input list
     * 
     * @param jsonResults the list with matched results
     * @param pathTokens the path to match
     */
private void parseInternalJson(List<JsonText> jsonResults, List<String> pathTokens) {
    List<String> _paths = new ArrayList<>(pathTokens);
    for (String path : pathTokens) {
        Matcher m = NAME_AND_INDEX_PATTERN.matcher(path);
        if (m.find()) {
            // path is pointing to array
            if (m.groupCount() < 2) {
                throw new JsonException("'" + _paths + "' does not specify an array in the valid way 'key_name[index_number]'");
            }
            // name in the path token
            String name = m.group(1);
            // index in the path token, for example "name[3]"
            int index = getIndex(path, m);
            _paths.remove(0);
            if (index == -1) {
                // we have an array but no index is specified -> "[]"
                Object internalObject = jsonObject.get(name);
                if (internalObject instanceof JSONArray) {
                    JSONArray internalJsonArray = (JSONArray) internalObject;
                    for (int i = 0; i < internalJsonArray.size(); i++) {
                        JsonText ooo = new JsonText(internalJsonArray.get(i));
                        if (_paths.size() == 0) {
                            // this is the path end, the last path token ends with "[]"
                            jsonResults.add(ooo);
                        } else {
                            // go deeper
                            ooo.parseInternalJson(jsonResults, _paths);
                        }
                    }
                    // we have cycled deeply into an array, do not go to the next path token
                    return;
                } else {
                    throw new RuntimeException("Not implemented");
                }
            } else if (!StringUtils.isNullOrEmpty(name)) {
                // pointing to JSON object
                if (index >= 0) {
                    _paths.add(0, "[" + String.valueOf(index) + "]");
                }
                new JsonText(jsonObject.get(name)).parseInternalJson(jsonResults, _paths);
                return;
            } else {
                // directly pointing to JSON array, for example "[3]"
                if (_paths.size() == 0) {
                    // this is the path end
                    if (index >= jsonArray.size()) {
                        throw new JsonException("Cannot remove item at positin " + (index + 1) + " as there are only " + jsonArray.size() + " items present");
                    } else {
                        jsonResults.add(new JsonText(jsonArray.get(index)));
                        return;
                    }
                } else {
                    // go deeper
                    new JsonText(jsonArray.get(index)).parseInternalJson(jsonResults, _paths);
                    return;
                }
            }
        } else {
            // path is pointing to object
            if (!jsonObject.containsKey(path)) {
                throw new JsonException("'" + _paths + "' is not a valid path");
            }
            if (jsonObject.get(path) == null) {
                // the value is null
                jsonResults.add(null);
                return;
            }
            JsonText jsonText = new JsonText(jsonObject.get(path));
            _paths.remove(0);
            if (_paths.size() > 0) {
                jsonText.parseInternalJson(jsonResults, _paths);
            } else {
                jsonResults.add(jsonText);
            }
            return;
        }
    }
}
Also used : JsonException(com.axway.ats.action.exceptions.JsonException) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) JSONArray(org.json.simple.JSONArray) JSONObject(org.json.simple.JSONObject)

Example 4 with JsonException

use of com.axway.ats.action.exceptions.JsonException 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 5 with JsonException

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

Aggregations

JsonException (com.axway.ats.action.exceptions.JsonException)9 Matcher (java.util.regex.Matcher)5 BaseTest (com.axway.ats.action.BaseTest)4 JsonText (com.axway.ats.action.json.JsonText)4 ArrayList (java.util.ArrayList)4 Test (org.junit.Test)4 PublicAtsApi (com.axway.ats.common.PublicAtsApi)2 List (java.util.List)2 JSONObject (org.json.simple.JSONObject)2 JSONArray (org.json.simple.JSONArray)1