@Test public void testExecuteJsonObjectComplex() { String jsonObject = "{\"one\":{\"a\":\"a\",\"B\":\"B\"}, \"two\":\"two\", \"three\":[1,2,3.4]}"; String name = "test"; String value = "{\"a\":\"b\"}"; final Map<String, String> result = addJsonPropertyToObject.execute(jsonObject, name, value, VALIDATE_VALUE_FALSE); assertEquals("{\"one\":{\"a\":\"a\",\"B\":\"B\"},\"two\":\"two\",\"three\":[1,2,3.4],\"test\":{\"a\":\"b\"}}", result.get(RETURN_RESULT)); }
@Action(name = "Add JSON Property to 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.JSON_OBJECT, required = true) String jsonObject, @Param(value = Constants.InputNames.NEW_PROPERTY_NAME, required = true) String newPropertyName, @Param(value = Constants.InputNames.NEW_PROPERTY_VALUE, required = true) String newPropertyValue, @Param(value = Constants.InputNames.VALIDATE_VALUE) String validateValue) { Map<String, String> returnResult = new HashMap<>(); if (jsonObject == null || jsonObject.trim().equals(OtherValues.EMPTY_STRING)) { return populateResult(returnResult, new Exception("Empty jsonObject provided!")); } ObjectMapper mapper = new ObjectMapper().configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true); final boolean validateValueBoolean = JsonUtils.parseBooleanWithDefault(validateValue, true); if (StringUtilities.isBlank(newPropertyValue)) { final String exceptionValue = VALUE_FOR_PROPERTY + newPropertyName + " it is not a valid JSON object!"; return populateResult(returnResult, new Exception(exceptionValue)); } if (newPropertyName == null) { return populateResult(returnResult, new Exception("Null newPropertyName provided!")); } JsonNode jsonRoot; try { jsonRoot = mapper.readTree(jsonObject); } catch (Exception exception) { final String exceptionValue = INVALID_JSON_OBJECT_PROVIDED + exception.getMessage(); return populateResult(returnResult, exceptionValue, exception); } ContainerNode jsonNodes = null; JsonNode jsonNodeValueWrapper; try { jsonNodeValueWrapper = mapper.readTree(newPropertyValue); } catch (IOException exception) { if (!validateValueBoolean) { jsonNodeValueWrapper = mapper.valueToTree(newPropertyValue); } else { final String exceptionValue = VALUE_FOR_PROPERTY + newPropertyName + " it is not a valid JSON object!"; return populateResult(returnResult, exceptionValue, exception); } } if (jsonRoot instanceof ObjectNode) { jsonNodes = ((ObjectNode) jsonRoot).putPOJO(newPropertyName, jsonNodeValueWrapper); } if (jsonRoot instanceof ArrayNode) { jsonNodes = ((ArrayNode) jsonRoot).add(jsonNodeValueWrapper); } if (jsonNodes == null) { return populateResult(returnResult, new Exception("The value cannot be added!")); } return populateResult(returnResult, jsonNodes.toString(), null); }
AddJsonPropertyToObject { @Action(name = "Add JSON Property to 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.JSON_OBJECT, required = true) String jsonObject, @Param(value = Constants.InputNames.NEW_PROPERTY_NAME, required = true) String newPropertyName, @Param(value = Constants.InputNames.NEW_PROPERTY_VALUE, required = true) String newPropertyValue, @Param(value = Constants.InputNames.VALIDATE_VALUE) String validateValue) { Map<String, String> returnResult = new HashMap<>(); if (jsonObject == null || jsonObject.trim().equals(OtherValues.EMPTY_STRING)) { return populateResult(returnResult, new Exception("Empty jsonObject provided!")); } ObjectMapper mapper = new ObjectMapper().configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true); final boolean validateValueBoolean = JsonUtils.parseBooleanWithDefault(validateValue, true); if (StringUtilities.isBlank(newPropertyValue)) { final String exceptionValue = VALUE_FOR_PROPERTY + newPropertyName + " it is not a valid JSON object!"; return populateResult(returnResult, new Exception(exceptionValue)); } if (newPropertyName == null) { return populateResult(returnResult, new Exception("Null newPropertyName provided!")); } JsonNode jsonRoot; try { jsonRoot = mapper.readTree(jsonObject); } catch (Exception exception) { final String exceptionValue = INVALID_JSON_OBJECT_PROVIDED + exception.getMessage(); return populateResult(returnResult, exceptionValue, exception); } ContainerNode jsonNodes = null; JsonNode jsonNodeValueWrapper; try { jsonNodeValueWrapper = mapper.readTree(newPropertyValue); } catch (IOException exception) { if (!validateValueBoolean) { jsonNodeValueWrapper = mapper.valueToTree(newPropertyValue); } else { final String exceptionValue = VALUE_FOR_PROPERTY + newPropertyName + " it is not a valid JSON object!"; return populateResult(returnResult, exceptionValue, exception); } } if (jsonRoot instanceof ObjectNode) { jsonNodes = ((ObjectNode) jsonRoot).putPOJO(newPropertyName, jsonNodeValueWrapper); } if (jsonRoot instanceof ArrayNode) { jsonNodes = ((ArrayNode) jsonRoot).add(jsonNodeValueWrapper); } if (jsonNodes == null) { return populateResult(returnResult, new Exception("The value cannot be added!")); } return populateResult(returnResult, jsonNodes.toString(), null); } }
AddJsonPropertyToObject { @Action(name = "Add JSON Property to 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.JSON_OBJECT, required = true) String jsonObject, @Param(value = Constants.InputNames.NEW_PROPERTY_NAME, required = true) String newPropertyName, @Param(value = Constants.InputNames.NEW_PROPERTY_VALUE, required = true) String newPropertyValue, @Param(value = Constants.InputNames.VALIDATE_VALUE) String validateValue) { Map<String, String> returnResult = new HashMap<>(); if (jsonObject == null || jsonObject.trim().equals(OtherValues.EMPTY_STRING)) { return populateResult(returnResult, new Exception("Empty jsonObject provided!")); } ObjectMapper mapper = new ObjectMapper().configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true); final boolean validateValueBoolean = JsonUtils.parseBooleanWithDefault(validateValue, true); if (StringUtilities.isBlank(newPropertyValue)) { final String exceptionValue = VALUE_FOR_PROPERTY + newPropertyName + " it is not a valid JSON object!"; return populateResult(returnResult, new Exception(exceptionValue)); } if (newPropertyName == null) { return populateResult(returnResult, new Exception("Null newPropertyName provided!")); } JsonNode jsonRoot; try { jsonRoot = mapper.readTree(jsonObject); } catch (Exception exception) { final String exceptionValue = INVALID_JSON_OBJECT_PROVIDED + exception.getMessage(); return populateResult(returnResult, exceptionValue, exception); } ContainerNode jsonNodes = null; JsonNode jsonNodeValueWrapper; try { jsonNodeValueWrapper = mapper.readTree(newPropertyValue); } catch (IOException exception) { if (!validateValueBoolean) { jsonNodeValueWrapper = mapper.valueToTree(newPropertyValue); } else { final String exceptionValue = VALUE_FOR_PROPERTY + newPropertyName + " it is not a valid JSON object!"; return populateResult(returnResult, exceptionValue, exception); } } if (jsonRoot instanceof ObjectNode) { jsonNodes = ((ObjectNode) jsonRoot).putPOJO(newPropertyName, jsonNodeValueWrapper); } if (jsonRoot instanceof ArrayNode) { jsonNodes = ((ArrayNode) jsonRoot).add(jsonNodeValueWrapper); } if (jsonNodes == null) { return populateResult(returnResult, new Exception("The value cannot be added!")); } return populateResult(returnResult, jsonNodes.toString(), null); }  }
AddJsonPropertyToObject { @Action(name = "Add JSON Property to 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.JSON_OBJECT, required = true) String jsonObject, @Param(value = Constants.InputNames.NEW_PROPERTY_NAME, required = true) String newPropertyName, @Param(value = Constants.InputNames.NEW_PROPERTY_VALUE, required = true) String newPropertyValue, @Param(value = Constants.InputNames.VALIDATE_VALUE) String validateValue) { Map<String, String> returnResult = new HashMap<>(); if (jsonObject == null || jsonObject.trim().equals(OtherValues.EMPTY_STRING)) { return populateResult(returnResult, new Exception("Empty jsonObject provided!")); } ObjectMapper mapper = new ObjectMapper().configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true); final boolean validateValueBoolean = JsonUtils.parseBooleanWithDefault(validateValue, true); if (StringUtilities.isBlank(newPropertyValue)) { final String exceptionValue = VALUE_FOR_PROPERTY + newPropertyName + " it is not a valid JSON object!"; return populateResult(returnResult, new Exception(exceptionValue)); } if (newPropertyName == null) { return populateResult(returnResult, new Exception("Null newPropertyName provided!")); } JsonNode jsonRoot; try { jsonRoot = mapper.readTree(jsonObject); } catch (Exception exception) { final String exceptionValue = INVALID_JSON_OBJECT_PROVIDED + exception.getMessage(); return populateResult(returnResult, exceptionValue, exception); } ContainerNode jsonNodes = null; JsonNode jsonNodeValueWrapper; try { jsonNodeValueWrapper = mapper.readTree(newPropertyValue); } catch (IOException exception) { if (!validateValueBoolean) { jsonNodeValueWrapper = mapper.valueToTree(newPropertyValue); } else { final String exceptionValue = VALUE_FOR_PROPERTY + newPropertyName + " it is not a valid JSON object!"; return populateResult(returnResult, exceptionValue, exception); } } if (jsonRoot instanceof ObjectNode) { jsonNodes = ((ObjectNode) jsonRoot).putPOJO(newPropertyName, jsonNodeValueWrapper); } if (jsonRoot instanceof ArrayNode) { jsonNodes = ((ArrayNode) jsonRoot).add(jsonNodeValueWrapper); } if (jsonNodes == null) { return populateResult(returnResult, new Exception("The value cannot be added!")); } return populateResult(returnResult, jsonNodes.toString(), null); }  @Action(name = "Add JSON Property to 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.JSON_OBJECT, required = true) String jsonObject,
            @Param(value = Constants.InputNames.NEW_PROPERTY_NAME, required = true) String newPropertyName,
            @Param(value = Constants.InputNames.NEW_PROPERTY_VALUE, required = true) String newPropertyValue,
            @Param(value = Constants.InputNames.VALIDATE_VALUE) String validateValue); }
AddJsonPropertyToObject { @Action(name = "Add JSON Property to 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.JSON_OBJECT, required = true) String jsonObject, @Param(value = Constants.InputNames.NEW_PROPERTY_NAME, required = true) String newPropertyName, @Param(value = Constants.InputNames.NEW_PROPERTY_VALUE, required = true) String newPropertyValue, @Param(value = Constants.InputNames.VALIDATE_VALUE) String validateValue) { Map<String, String> returnResult = new HashMap<>(); if (jsonObject == null || jsonObject.trim().equals(OtherValues.EMPTY_STRING)) { return populateResult(returnResult, new Exception("Empty jsonObject provided!")); } ObjectMapper mapper = new ObjectMapper().configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true); final boolean validateValueBoolean = JsonUtils.parseBooleanWithDefault(validateValue, true); if (StringUtilities.isBlank(newPropertyValue)) { final String exceptionValue = VALUE_FOR_PROPERTY + newPropertyName + " it is not a valid JSON object!"; return populateResult(returnResult, new Exception(exceptionValue)); } if (newPropertyName == null) { return populateResult(returnResult, new Exception("Null newPropertyName provided!")); } JsonNode jsonRoot; try { jsonRoot = mapper.readTree(jsonObject); } catch (Exception exception) { final String exceptionValue = INVALID_JSON_OBJECT_PROVIDED + exception.getMessage(); return populateResult(returnResult, exceptionValue, exception); } ContainerNode jsonNodes = null; JsonNode jsonNodeValueWrapper; try { jsonNodeValueWrapper = mapper.readTree(newPropertyValue); } catch (IOException exception) { if (!validateValueBoolean) { jsonNodeValueWrapper = mapper.valueToTree(newPropertyValue); } else { final String exceptionValue = VALUE_FOR_PROPERTY + newPropertyName + " it is not a valid JSON object!"; return populateResult(returnResult, exceptionValue, exception); } } if (jsonRoot instanceof ObjectNode) { jsonNodes = ((ObjectNode) jsonRoot).putPOJO(newPropertyName, jsonNodeValueWrapper); } if (jsonRoot instanceof ArrayNode) { jsonNodes = ((ArrayNode) jsonRoot).add(jsonNodeValueWrapper); } if (jsonNodes == null) { return populateResult(returnResult, new Exception("The value cannot be added!")); } return populateResult(returnResult, jsonNodes.toString(), null); }  @Action(name = "Add JSON Property to 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.JSON_OBJECT, required = true) String jsonObject,
            @Param(value = Constants.InputNames.NEW_PROPERTY_NAME, required = true) String newPropertyName,
            @Param(value = Constants.InputNames.NEW_PROPERTY_VALUE, required = true) String newPropertyValue,
            @Param(value = Constants.InputNames.VALIDATE_VALUE) String validateValue);  }