diff --git a/content/io/cloudslang/base/json/add_property_to_object.sl b/content/io/cloudslang/base/json/add_property_to_object.sl new file mode 100644 index 0000000000..dbd475b072 --- /dev/null +++ b/content/io/cloudslang/base/json/add_property_to_object.sl @@ -0,0 +1,71 @@ +# (c) Copyright 2014-2017 EntIT Software LLC, a Micro Focus company, L.P. +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License v2.0 which accompany this distribution. +# +# The Apache License is available at +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +######################################################################################################################## +#!! +#! @description: This operation adds a new property into a JSON object, with a value of type string. +#! In case that a new property with the same name as an existing one is added, the old property's value +#! will be overwritten. +#! +#! @input json_object: String representation of a JSON object. +#! @input new_property_name: The name of the new property to add to the JSON object. +#! Optional +#! @input new_property_value: The value for the new property. This is interpreted as a string, +#! no matter what the contents of the input. +#! Optional +#! +#! @output return_result: That will contain the JSON with the new property/value added. +#! @output return_code: '0' if success, '-1' otherwise. +#! @output exception: The exception message if the operation goes to failure. +#! +#! @result SUCCESS: Operation executed successfully. +#! @result FAILURE: An error occurred while trying to complete the operation. +#!!# +######################################################################################################################## + +namespace: io.cloudslang.base.json + +operation: + name: add_property_to_object + + inputs: + - json_object + - jsonObject: + default: ${json_object} + private: true + - new_property_name: + required: false + - newPropertyName: + default: ${get('new_property_name', '')} + required: false + private: true + - new_property_value: + required: false + - newPropertyValue: + default: ${get('new_property_value', '')} + required: false + private: true + + java_action: + gav: 'io.cloudslang.content:cs-json:0.0.9-SNAPSHOT' + class_name: io.cloudslang.content.json.actions.AddPropertyToObject + method_name: execute + + outputs: + - return_result: ${returnResult} + - return_code: ${returnCode} + - exception: ${get(exception, '')} + + results: + - SUCCESS: ${returnCode == '0'} + - FAILURE diff --git a/test/io/cloudslang/base/json/add_property_to_object.inputs.yaml b/test/io/cloudslang/base/json/add_property_to_object.inputs.yaml new file mode 100644 index 0000000000..89ba192674 --- /dev/null +++ b/test/io/cloudslang/base/json/add_property_to_object.inputs.yaml @@ -0,0 +1,132 @@ +# (c) Copyright 2017 EntIT Software LLC, a Micro Focus company, L.P. +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License v2.0 which accompany this distribution. +# +# The Apache License is available at +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +######################################################################################################################## + +testAddValidStringValue: + inputs: + - json_object: '{}' + - new_property_name: 'propertyName' + - new_property_value: 'propertyValue' + description: Tests that add_property_to_object.sl correctly adds a property with a string value to a JSON + testFlowPath: io.cloudslang.base.json.add_property_to_object + testSuites: [add_property_to_object] + outputs: + - return_result: '{"propertyName":"propertyValue"}' + - return_code: '0' + - exception: '' + result: SUCCESS + +testAddValidNumericValue: + inputs: + - json_object: '{}' + - new_property_name: 'propertyName' + - new_property_value: '1' + description: Tests that add_property_to_object.sl correctly adds a property with a numeric value to a JSON + testFlowPath: io.cloudslang.base.json.add_property_to_object + testSuites: [add_property_to_object] + outputs: + - return_result: '{"propertyName":"1"}' + - return_code: '0' + - exception: '' + result: SUCCESS + +testAddValidArrayValue: + inputs: + - json_object: '{}' + - new_property_name: 'propertyName' + - new_property_value: '["a":1]' + description: Tests that add_property_to_object.sl correctly adds a property with an array value to a JSON + testFlowPath: io.cloudslang.base.json.add_property_to_object + testSuites: [add_property_to_object] + outputs: + - return_result: '{"propertyName":"[\"a\":1]"}' + - return_code: '0' + - exception: '' + result: SUCCESS + +testAddPropertyNameAlreadyExisting: + inputs: + - json_object: '{"property":"value1"}' + - new_property_name: 'property' + - new_property_value: 'value2' + description: Tests that add_property_to_object.sl overwrites value for property with the same name + testFlowPath: io.cloudslang.base.json.add_property_to_object + testSuites: [add_property_to_object] + outputs: + - return_result: '{"property":"value2"}' + - return_code: '0' + - exception: '' + result: SUCCESS + +testAddPropertyWithNoNameProvided: + inputs: + - json_object: '{}' + - new_property_value: 'value' + description: Tests that add_property_to_object.sl adds property with empty name if no name is provided + testFlowPath: io.cloudslang.base.json.add_property_to_object + testSuites: [add_property_to_object] + outputs: + - return_result: '{"":"value"}' + - return_code: '0' + - exception: '' + result: SUCCESS + +testAddPropertyWithNoValueProvided: + inputs: + - json_object: '{}' + - new_property_name: 'property' + description: Tests that add_property_to_object.sl adds property with empty string value if no value is provided + testFlowPath: io.cloudslang.base.json.add_property_to_object + testSuites: [add_property_to_object] + outputs: + - return_result: '{"property":""}' + - return_code: '0' + - exception: '' + result: SUCCESS + +testAddToBlankObject: + inputs: + - json_object: ' \t' + - new_property_name: 'propertyName' + - new_property_value: 'value' + description: Tests that add_property_to_object.sl cannot add a property to a non-JSON (blank string) + testFlowPath: io.cloudslang.base.json.add_property_to_object + testSuites: [add_property_to_object] + outputs: + - return_code: '-1' + result: FAILURE + +testAddToInvalidObject: + inputs: + - json_object: '{invalidPropName:2}' + - new_property_name: 'propertyName' + - new_property_value: 'propertyValue' + description: Tests that add_property_to_object.sl cannot add a property to a non-JSON (invalid json) + testFlowPath: io.cloudslang.base.json.add_property_to_object + testSuites: [add_property_to_object] + outputs: + - return_code: '-1' + result: FAILURE + +testAddToArray: + inputs: + - json_object: '[]' + - new_property_name: 'propertyName' + - new_property_value: 'propertyValue' + description: Tests that add_property_to_object.sl cannot add a property to a JSON array + testFlowPath: io.cloudslang.base.json.add_property_to_object + testSuites: [add_property_to_object] + outputs: + - return_code: '-1' + result: FAILURE \ No newline at end of file