@Test public void testExecuteJsonArrayBadIndex() { String jsonObject = "{ \"location\": [\n" + " {\"city\": \"Roseville\", \"country\": \"United States\"},\n" + " {\"city\": \"Cluj\", \"country\": \"Romania\"},\n" + " {\"city\": \"Yehud\", \"country\": \"Israel\"}] \n" + "}"; String key = "location[987].city"; Map<String, String> result = getValueFromObject.execute(jsonObject, key); assertEquals("The provided 987 index is out of range! Provide a valid index value in the provided JSON!", result.get(RETURN_RESULT)); key = "location[a]"; result = getValueFromObject.execute(jsonObject, key); assertEquals("The location[a] key does not exist in JavaScript object!", result.get(RETURN_RESULT)); key = "location[0].a"; result = getValueFromObject.execute(jsonObject, key); assertEquals("The a key does not exist in JavaScript object!", result.get(RETURN_RESULT)); key = "location[a].b"; result = getValueFromObject.execute(jsonObject, key); assertEquals("The location[a] key does not exist in JavaScript object!", result.get(RETURN_RESULT)); }
@Action(name = "Get Value from Object", outputs = { @Output(OutputNames.RETURN_RESULT), @Output(OutputNames.RETURN_CODE), @Output(OutputNames.EXCEPTION) }, responses = { @Response(text = ResponseNames.SUCCESS, field = OutputNames.RETURN_CODE, value = ReturnCodes.SUCCESS, matchType = MatchType.COMPARE_EQUAL, responseType = ResponseType.RESOLVED), @Response(text = ResponseNames.FAILURE, field = OutputNames.RETURN_CODE, value = ReturnCodes.FAILURE, matchType = MatchType.COMPARE_EQUAL, responseType = ResponseType.ERROR, isOnFail = true) }) public Map<String, String> execute( @Param(value = Constants.InputNames.OBJECT, required = true) String object, @Param(value = Constants.InputNames.KEY, required = true) String key) { Map<String, String> returnResult = new HashMap<>(); if (StringUtilities.isBlank(object)) { return populateResult(returnResult, new Exception("Empty object provided!")); } if (key == null) { return populateResult(returnResult, new Exception("Null key provided!")); } final JsonNode jsonRoot; ObjectMapper objectMapper = new ObjectMapper(); try { jsonRoot = objectMapper.readTree(object); } catch (Exception exception) { final String value = "Invalid object provided! " + exception.getMessage(); return populateResult(returnResult, value, exception); } int startIndex = 0; final JsonNode valueFromObject; try { valueFromObject = getObject(jsonRoot, key.split(ESCAPED_SLASH + "."), startIndex); } catch (Exception exception) { return populateResult(returnResult, exception); } if (valueFromObject.isValueNode()) { return populateResult(returnResult, valueFromObject.asText(), null); } else { return populateResult(returnResult, valueFromObject.toString(), null); } }
GetValueFromObject { @Action(name = "Get Value from Object", outputs = { @Output(OutputNames.RETURN_RESULT), @Output(OutputNames.RETURN_CODE), @Output(OutputNames.EXCEPTION) }, responses = { @Response(text = ResponseNames.SUCCESS, field = OutputNames.RETURN_CODE, value = ReturnCodes.SUCCESS, matchType = MatchType.COMPARE_EQUAL, responseType = ResponseType.RESOLVED), @Response(text = ResponseNames.FAILURE, field = OutputNames.RETURN_CODE, value = ReturnCodes.FAILURE, matchType = MatchType.COMPARE_EQUAL, responseType = ResponseType.ERROR, isOnFail = true) }) public Map<String, String> execute( @Param(value = Constants.InputNames.OBJECT, required = true) String object, @Param(value = Constants.InputNames.KEY, required = true) String key) { Map<String, String> returnResult = new HashMap<>(); if (StringUtilities.isBlank(object)) { return populateResult(returnResult, new Exception("Empty object provided!")); } if (key == null) { return populateResult(returnResult, new Exception("Null key provided!")); } final JsonNode jsonRoot; ObjectMapper objectMapper = new ObjectMapper(); try { jsonRoot = objectMapper.readTree(object); } catch (Exception exception) { final String value = "Invalid object provided! " + exception.getMessage(); return populateResult(returnResult, value, exception); } int startIndex = 0; final JsonNode valueFromObject; try { valueFromObject = getObject(jsonRoot, key.split(ESCAPED_SLASH + "."), startIndex); } catch (Exception exception) { return populateResult(returnResult, exception); } if (valueFromObject.isValueNode()) { return populateResult(returnResult, valueFromObject.asText(), null); } else { return populateResult(returnResult, valueFromObject.toString(), null); } } }
GetValueFromObject { @Action(name = "Get Value from Object", outputs = { @Output(OutputNames.RETURN_RESULT), @Output(OutputNames.RETURN_CODE), @Output(OutputNames.EXCEPTION) }, responses = { @Response(text = ResponseNames.SUCCESS, field = OutputNames.RETURN_CODE, value = ReturnCodes.SUCCESS, matchType = MatchType.COMPARE_EQUAL, responseType = ResponseType.RESOLVED), @Response(text = ResponseNames.FAILURE, field = OutputNames.RETURN_CODE, value = ReturnCodes.FAILURE, matchType = MatchType.COMPARE_EQUAL, responseType = ResponseType.ERROR, isOnFail = true) }) public Map<String, String> execute( @Param(value = Constants.InputNames.OBJECT, required = true) String object, @Param(value = Constants.InputNames.KEY, required = true) String key) { Map<String, String> returnResult = new HashMap<>(); if (StringUtilities.isBlank(object)) { return populateResult(returnResult, new Exception("Empty object provided!")); } if (key == null) { return populateResult(returnResult, new Exception("Null key provided!")); } final JsonNode jsonRoot; ObjectMapper objectMapper = new ObjectMapper(); try { jsonRoot = objectMapper.readTree(object); } catch (Exception exception) { final String value = "Invalid object provided! " + exception.getMessage(); return populateResult(returnResult, value, exception); } int startIndex = 0; final JsonNode valueFromObject; try { valueFromObject = getObject(jsonRoot, key.split(ESCAPED_SLASH + "."), startIndex); } catch (Exception exception) { return populateResult(returnResult, exception); } if (valueFromObject.isValueNode()) { return populateResult(returnResult, valueFromObject.asText(), null); } else { return populateResult(returnResult, valueFromObject.toString(), null); } }  }
GetValueFromObject { @Action(name = "Get Value from Object", outputs = { @Output(OutputNames.RETURN_RESULT), @Output(OutputNames.RETURN_CODE), @Output(OutputNames.EXCEPTION) }, responses = { @Response(text = ResponseNames.SUCCESS, field = OutputNames.RETURN_CODE, value = ReturnCodes.SUCCESS, matchType = MatchType.COMPARE_EQUAL, responseType = ResponseType.RESOLVED), @Response(text = ResponseNames.FAILURE, field = OutputNames.RETURN_CODE, value = ReturnCodes.FAILURE, matchType = MatchType.COMPARE_EQUAL, responseType = ResponseType.ERROR, isOnFail = true) }) public Map<String, String> execute( @Param(value = Constants.InputNames.OBJECT, required = true) String object, @Param(value = Constants.InputNames.KEY, required = true) String key) { Map<String, String> returnResult = new HashMap<>(); if (StringUtilities.isBlank(object)) { return populateResult(returnResult, new Exception("Empty object provided!")); } if (key == null) { return populateResult(returnResult, new Exception("Null key provided!")); } final JsonNode jsonRoot; ObjectMapper objectMapper = new ObjectMapper(); try { jsonRoot = objectMapper.readTree(object); } catch (Exception exception) { final String value = "Invalid object provided! " + exception.getMessage(); return populateResult(returnResult, value, exception); } int startIndex = 0; final JsonNode valueFromObject; try { valueFromObject = getObject(jsonRoot, key.split(ESCAPED_SLASH + "."), startIndex); } catch (Exception exception) { return populateResult(returnResult, exception); } if (valueFromObject.isValueNode()) { return populateResult(returnResult, valueFromObject.asText(), null); } else { return populateResult(returnResult, valueFromObject.toString(), null); } }  @Action(name = "Get Value from Object", outputs = { @Output(OutputNames.RETURN_RESULT), @Output(OutputNames.RETURN_CODE), @Output(OutputNames.EXCEPTION) }, responses = { @Response(text = ResponseNames.SUCCESS, field = OutputNames.RETURN_CODE, value = ReturnCodes.SUCCESS, matchType = MatchType.COMPARE_EQUAL, responseType = ResponseType.RESOLVED), @Response(text = ResponseNames.FAILURE, field = OutputNames.RETURN_CODE, value = ReturnCodes.FAILURE, matchType = MatchType.COMPARE_EQUAL, responseType = ResponseType.ERROR, isOnFail = true) }) Map<String, String> execute(
            @Param(value = Constants.InputNames.OBJECT, required = true) String object,
            @Param(value = Constants.InputNames.KEY, required = true) String key); }
GetValueFromObject { @Action(name = "Get Value from Object", outputs = { @Output(OutputNames.RETURN_RESULT), @Output(OutputNames.RETURN_CODE), @Output(OutputNames.EXCEPTION) }, responses = { @Response(text = ResponseNames.SUCCESS, field = OutputNames.RETURN_CODE, value = ReturnCodes.SUCCESS, matchType = MatchType.COMPARE_EQUAL, responseType = ResponseType.RESOLVED), @Response(text = ResponseNames.FAILURE, field = OutputNames.RETURN_CODE, value = ReturnCodes.FAILURE, matchType = MatchType.COMPARE_EQUAL, responseType = ResponseType.ERROR, isOnFail = true) }) public Map<String, String> execute( @Param(value = Constants.InputNames.OBJECT, required = true) String object, @Param(value = Constants.InputNames.KEY, required = true) String key) { Map<String, String> returnResult = new HashMap<>(); if (StringUtilities.isBlank(object)) { return populateResult(returnResult, new Exception("Empty object provided!")); } if (key == null) { return populateResult(returnResult, new Exception("Null key provided!")); } final JsonNode jsonRoot; ObjectMapper objectMapper = new ObjectMapper(); try { jsonRoot = objectMapper.readTree(object); } catch (Exception exception) { final String value = "Invalid object provided! " + exception.getMessage(); return populateResult(returnResult, value, exception); } int startIndex = 0; final JsonNode valueFromObject; try { valueFromObject = getObject(jsonRoot, key.split(ESCAPED_SLASH + "."), startIndex); } catch (Exception exception) { return populateResult(returnResult, exception); } if (valueFromObject.isValueNode()) { return populateResult(returnResult, valueFromObject.asText(), null); } else { return populateResult(returnResult, valueFromObject.toString(), null); } }  @Action(name = "Get Value from Object", outputs = { @Output(OutputNames.RETURN_RESULT), @Output(OutputNames.RETURN_CODE), @Output(OutputNames.EXCEPTION) }, responses = { @Response(text = ResponseNames.SUCCESS, field = OutputNames.RETURN_CODE, value = ReturnCodes.SUCCESS, matchType = MatchType.COMPARE_EQUAL, responseType = ResponseType.RESOLVED), @Response(text = ResponseNames.FAILURE, field = OutputNames.RETURN_CODE, value = ReturnCodes.FAILURE, matchType = MatchType.COMPARE_EQUAL, responseType = ResponseType.ERROR, isOnFail = true) }) Map<String, String> execute(
            @Param(value = Constants.InputNames.OBJECT, required = true) String object,
            @Param(value = Constants.InputNames.KEY, required = true) String key);  }