Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Property to Object #1159

Open
wants to merge 1 commit into
base: WS_2_0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions content/io/cloudslang/base/json/add_property_to_object.sl
Original file line number Diff line number Diff line change
@@ -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
132 changes: 132 additions & 0 deletions test/io/cloudslang/base/json/add_property_to_object.inputs.yaml
Original file line number Diff line number Diff line change
@@ -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