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

Changed validate_json.sl to work with Java action #1063

Open
wants to merge 2 commits into
base: master
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
105 changes: 63 additions & 42 deletions content/io/cloudslang/base/json/validate_json.sl
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
# (c) Copyright 2017 EntIT Software LLC, a Micro Focus company, L.P.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be 2018 Micro Focus

# 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: Checks if a JSON is valid.
#! @description: Checks if a JSON is valid and, optionally, if it is valid against a schema.
#!
#! @input json_input: JSON to validate
#! @input json_object: JSON to validate.
#! @input json_schema: JSON schema to validate against. Can also be a JSON schema, an URL or a file path.
#! Optional
#! @input proxy_host: The proxy host for getting the Get request.
#! Optional
#! @input proxy_port: The proxy port for getting the Get request.
#! Optional
#! @input proxy_username: The username for connecting via proxy.
#! Optional
#! @input proxy_password: The password for connecting via proxy.
#! Optional
#!
#! @output return_result: Message of validity or exception
#! @output return_code: "0" if is a valid json, "-1" otherwise
#! @output error_message: Error message if there was an error when executing, empty otherwise
#! @output return_result: JSON was valid or not (optionally, against a JSON schema).
#! Same as exception if an exception was thrown during execution.
#! @output return_code: "0" if validation was successful, "-1" otherwise.
#! @output exception: Exception message if an exception was thrown during execution, empty otherwise.
#!
#! @result SUCCESS: JSON is valid (return_code == '0')
#! @result FAILURE: Otherwise
#! @result SUCCESS: JSON is valid (return_code == '0').
#! @result FAILURE: An error has occurred while trying to validate the given JSON object.
#!!#
########################################################################################################################

Expand All @@ -32,31 +30,54 @@ operation:
name: validate_json

inputs:
- json_input
- json_object
- jsonObject:
default: ${get('json_object', '')}
required: false
private: true
- json_schema:
required: false
- jsonSchema:
default: ${get('json_schema', '')}
required: false
private: true
- proxy_host:
required: false
- proxyHost:
default: ${get('proxy_host', '')}
required: false
private: true
- proxy_port:
required: false
- proxyPort:
default: ${get('proxy_port', '')}
required: false
private: true
- proxy_username:
required: false
- proxyUsername:
default: ${get('proxy_username', '')}
required: false
private: true
- proxy_password:
required: false
sensitive: true
- proxyPassword:
default: ${get('proxy_password', '')}
required: false
private: true
sensitive: true

python_action:
script: |
try:
import json,re
for c in json_input:
if c in ['\'', '\"']:
quote = c
break
if quote == '\'':
json_input = str(re.sub(r"(?<!\\)(\')",'"', json_input))
json_input = str(re.sub(r"(\\)",'', json_input))
decoded = json.loads(json_input)
return_result = 'Valid JSON'
return_code = '0'
except Exception as ex:
return_result = ex
return_code = '-1'
java_action:
gav: 'io.cloudslang.content:cs-json:0.0.9-SNAPSHOT'
class_name: 'io.cloudslang.content.json.actions.ValidateJson'
method_name: 'execute'

outputs:
- return_result: ${ str(return_result) }
- return_code
- error_message: ${ str(return_result) if return_code == '-1' else '' }
- return_result: ${get('returnResult', '')}
- return_code: ${get('returnCode', '')}
- exception: ${get('exception', '')}

results:
- SUCCESS: ${ return_code == '0' }
- SUCCESS: ${returnCode=='0'}
- FAILURE
135 changes: 119 additions & 16 deletions test/io/cloudslang/base/json/validate_json.inputs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

testValidateJsonComplexSuccess:
inputs:
- json_input:
- json_object:
value: '{
"server" : {
"name" : "server_3",
Expand Down Expand Up @@ -50,113 +50,122 @@ testValidateJsonComplexSuccess:
}'
description: Tests that validate_json.sl ends with SUCCESS
testFlowPath: io.cloudslang.base.json.validate_json
testSuites: [validate]

outputs:
- return_result: Valid JSON
- return_result: 'Valid JSON'
- return_code: '0'
result: SUCCESS

testValidateJsonSimpleSuccess:
inputs:
- json_input:
- json_object:
value: '[{"firstName":"Darth", "lastName":"Vader"}, {"firstName":"Luke", "lastName":"Skywalker"}]'
description: Tests that validate_json.sl ends with SUCCESS
testFlowPath: io.cloudslang.base.json.validate_json
testSuites: [validate]
outputs:
- return_result: Valid JSON
- return_code: '0'
result: SUCCESS

testValidateJsonExtraDataFailure:
inputs:
- json_input:
- json_object:
value: '{"firstName":"Darth", "lastName":"Vader"}, {"firstName":"Luke", "lastName":"Skywalker"}'
description: Tests that validate_json.sl ends with FAILURE
testFlowPath: io.cloudslang.base.json.validate_json
testSuites: [validate]
outputs:
- return_result: "Extra data: line 1 column 42 - line 1 column 88 (char 41 - 87)"
- return_code: '-1'
result: FAILURE

testValidateJsonPrimitiveDataFailure:
inputs:
- json_input:
- json_object:
value: '{"Luke":Skywalker}'
description: Tests that validate_json.sl ends with FAILURE
testFlowPath: io.cloudslang.base.json.validate_json
testSuites: [validate]
outputs:
- return_result: "No JSON object could be decoded"
- return_code: "-1"
result: FAILURE

testValidateJsonDataTypesSuccess:
inputs:
- json_input:
- json_object:
value: '[{"integer":5, "float1":2.5, "float2":1.54e-1}, {"boolean1":true, "boolean2":false}, {"null":null}]'
description: Tests that validate_json.sl ends with SUCCESS when JSON contains numbers, booleans and null
testFlowPath: io.cloudslang.base.json.validate_json
testSuites: [validate]
outputs:
- return_result: Valid JSON
- return_code: "0"
result: SUCCESS

testValidateEmptyJson:
inputs:
- json_input:
- json_object:
value: '""'
description: Tests that validate_json.sl ends with SUCCESS for the empty JSON
testFlowPath: io.cloudslang.base.json.validate_json
testSuites: [validate]
outputs:
- return_result: Valid JSON
- return_code: "0"
result: SUCCESS

testValidateNestedJSON:
inputs:
- json_input:
- json_object:
value: '{"level1":{"level2":{"level3":{"level4":{"level5":{"level6":{"level7":{"level8":{"level9":{"level10":{"level11":{"level12":{"level13":{"level14":{"level15":{"level16":{"level17":{"level18":{"level19":{"level20":{"level21":{}}}}}}}}}}}}}}}}}}}}}}'
description: Tests that validate_json.sl ends with SUCCESS when there are many levels of nesting
testFlowPath: io.cloudslang.base.json.validate_json
testSuites: [validate]
outputs:
- return_result: Valid JSON
- return_code: "0"
result: SUCCESS

testValidateSpecialCharactersJSON:
inputs:
- json_input:
- json_object:
value: '{"k1": "v1","k1!#%@&*`|>-": "v2!#%@&*`?|>{[-"}'
description: Tests that validate_json.sl ends with SUCCESS when there are many levels of nesting
testFlowPath: io.cloudslang.base.json.validate_json
testSuites: [validate]
outputs:
- return_result: Valid JSON
- return_code: "0"
result: SUCCESS

testValidateJsonDataSuccess:
inputs:
- json_input:
- json_object:
value: '[15, "string", 1.54e-1]'
description: Tests that validate_json.sl ends with SUCCESS when JSON contains standalone numbers or strings
testFlowPath: io.cloudslang.base.json.validate_json
testSuites: [validate]
outputs:
- return_result: Valid JSON
- return_code: "0"
result: SUCCESS

testValidateJsonSingleQuote:
inputs:
- json_input:
- json_object:
value: "[{'firstName':'Darth', 'lastName':'Vader'}, {'firstName':'Luke', 'lastName':'Skywalker'}]"
description: Tests that validate_json.sl ends with SUCCESS for a single quotes JSON
testFlowPath: io.cloudslang.base.json.validate_json
testSuites: [validate]
outputs:
- return_result: "Valid JSON"
- return_code: "0"
result: SUCCESS

testValidateJsonSingleQuoteSuccess:
inputs:
- json_input:
- json_object:
value: "{
'server' : {
'name' : 'server_3',
Expand Down Expand Up @@ -191,14 +200,15 @@ testValidateJsonSingleQuoteSuccess:
}"
description: Tests that validate_json.sl ends with SUCCESS for a complex single quotes JSON
testFlowPath: io.cloudslang.base.json.validate_json
testSuites: [validate]
outputs:
- return_result: "Valid JSON"
- return_code: "0"
result: SUCCESS

testValidateJsonSingleQuoteFailure:
inputs:
- json_input:
- json_object:
value: "{
'server' : {
'name' : 'server_3',
Expand Down Expand Up @@ -233,6 +243,99 @@ testValidateJsonSingleQuoteFailure:
}"
description: Tests that validate_json.sl ends with FAILURE for a invalid complex single quotes JSON
testFlowPath: io.cloudslang.base.json.validate_json
testSuites: [validate]
outputs:
- return_code: "-1"
result: FAILURE

testEmptySchemaSuccess:
inputs:
- json_object:
value: '{"first_name": "George", "last_name": "Daniel"}'
- json_schema:
value: ''
description: Tests that validate_json.sl ends with SUCCESS for an empty JSON schema
testFlowPath: io.cloudslang.base.json.validate_json
testSuites: [validate]
outputs:
- return_code: "0"
result: SUCCESS

testValidateJsonAgainstSchemaSuccess:
inputs:
- json_object:
value: '{
"first_name": "George",
"last_name": "Washington",
"birthday": "22-02-1732",
"address": {
"street_address": "3200 Mount Vernon Memorial Highway",
"city": "Mount Vernon",
"state": "Virginia",
"country": "United States"
}
}'
- json_schema:
value: '{
"type": "object",
"properties": {
"first_name": { "type": "string" },
"last_name": { "type": "string" },
"birthday": { "type": "string", "format": "string" },
"address": {
"type": "object",
"properties": {
"street_address": { "type": "string" },
"city": { "type": "string" },
"state": { "type": "string" },
"country": { "type" : "string" }
}
}
}
}'
description: Tests that validate_json.sl ends with SUCCESS for a JSON string that validates against a JSON schema
testFlowPath: io.cloudslang.base.json.validate_json
testSuites: [validate]
outputs:
- return_code: "0"
result: SUCCESS

testValidateJsonAgainstSchemaFailure:
inputs:
- json_object:
value: '{
"first_name": "George",
"last_name": "Washington",
"birthday": "22-02-1732",
"address": {
"street_address": "3200 Mount Vernon Memorial Highway",
"city": "Mount Vernon",
"state": "Virginia",
"country": "United States"
}
}'
- json_schema:
value: '{
"type": "object",
"properties": {
"first_name": { "type": "number" },
"last_name": { "type": "string" },
"birthday": { "type": "string", "format": "string" },
"address": {
"type": "object",
"properties": {
"street_address": { "type": "string" },
"city": { "type": "string" },
"state": { "type": "string" },
"country": { "type" : "string" }
}
}
}
}'
description: Tests that validate_json.sl ends with FAILURE for a JSON string that does not validate against a JSON schema
testFlowPath: io.cloudslang.base.json.validate_json
testSuites: [validate]
outputs:
- return_code: "-1"
result: FAILURE
result: FAILURE