Package core.apiCore.helpers
Class JsonHelper
java.lang.Object
core.apiCore.helpers.JsonHelper
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionarrayRemoveAllEmptyString(List<String> list) static voidconfigMapJsonKeyValues(io.restassured.response.Response response, KeyValue keyword) map key value to config eg.features.features.id:1:invalid input: '<'$id>static voidconfigMapKeyValues(io.restassured.response.Response response, KeyValue keyword) map key value to config eg.features.features.id:1:invalid input: '<'$id>static booleancontainsEscapeChar(String value) \t Insert a tab in the text at this point.static StringconvertJsonFileToString(Path templatePath) static ObjectconvertToObject(String value, boolean isPathValueString) convert string to object converts string to appropriate object type json supports string, number, boolean, null, object, arraystatic org.json.JSONArraygetJsonArray(String jsonString) return true if json string is a json arraystatic org.json.JSONObjectgetJsonObject(String jsonString) return true if json string is a json ostatic ObjectgetJsonPathValue(String json, String path, boolean isAlwaysReturnList) gets json value as list if applicable, or string if single item converts to string separated by "," https://github.com/json-path/JsonPathstatic ObjectgetJsonPathValue(String json, String path, boolean isAlwaysReturnList, boolean checkError) gets json value as list if applicable, or string if single item converts to string separated by "," https://github.com/json-path/JsonPathstatic StringgetJsonValue(io.restassured.response.Response response, String path) gets json value as list if applicable, or string if single item converts to string separated by ","static StringgetJsonValue(String json, String path) gets json value as list if applicable, or string if single item converts to string separated by "," https://github.com/json-path/JsonPathstatic StringgetJsonValue(String json, String path, boolean isAlwaysReturnList) gets json value as list if applicable, or string if single item converts to string separated by "," https://github.com/json-path/JsonPathstatic StringgetJsonValueFromXml(String xml, String path) get json path value from xml stringstatic StringgetRequestBodyFromJsonTemplate(ServiceObject serviceObject) if request body is empty, return json template string if request body contains @ variable tag, replace tag with value format for request body: json path:position:value or json path:vlaue eg.static StringgetResponseValue(io.restassured.response.Response response) static booleanisJsonFile(String filename) return true if file is json filestatic booleanisJsonPathValueExist(String jsonString, String jsonPath) static booleanisJsonPathValueString(String json, String path) static booleanisJSONValid(String value, boolean isFailOnError) validates json stringstatic booleanisValidJsonObject(Object jsonObject) checks if json string is a structured json body with key value pairs, or just array list.static StringremoveResponseIndicator(String expected) remove response indicatorsstatic StringreplaceJsonPathValue(String jsonString, String path, String value) replace json string value based on json path eg.static voidsaveOutboundJsonParameters(io.restassured.response.Response response, String outputParam) replaces output parameter with response values eg.static StringupdateJsonFromRequestBody(ServiceObject service) static StringupdateJsonFromRequestBody(String requestbody, String jsonString) static StringvalidateByJsonBody(String expectedJson, String response) validates expected json string against json body from responsestatic StringvalidateByJsonBody(String expectedJson, String response, boolean hasToBeJsonBody) validates expected json string against json body from responsevalidateByKeywords(String expectedJson, io.restassured.response.Response response) validates json response against keywordsvalidateByKeywords(String expectedJson, String responseString) validates json response against keywordsvalidateJsonKeywords(List<KeyValue> keywords, String responseString) validates the json maps against the keyword requirements examples: "person.roles.name": hasItems("admin"), "person.lastName": equalTo("Administrator"), "person.lastName": isNotEmpty, "person.roles.name": contains("admin"), "person.roles.name": containsInAnyOrder(admin), "person.roles": nodeSizeGreaterThan(0), "person.sites.": nodeSizeExact(0) "person.roles.name": sequence("admin"),static StringvalidateResponseBody(String expected, io.restassured.response.Response response) validates response body this is validating the response body as textstatic StringvalidateResponseBody(String expected, String responseString) validates response body this is validating the response body as textstatic Stringconvert xml string to json string
-
Field Details
-
failOnEscapeChars
-
-
Constructor Details
-
JsonHelper
public JsonHelper()
-
-
Method Details
-
saveOutboundJsonParameters
public static void saveOutboundJsonParameters(io.restassured.response.Response response, String outputParam) replaces output parameter with response values eg. $token with id form values are in form of list separated by ";" if Command is used- Parameters:
response-outputParam-
-
configMapJsonKeyValues
public static void configMapJsonKeyValues(io.restassured.response.Response response, KeyValue keyword) map key value to config eg.features.features.id:1:invalid input: '<'$id>- Parameters:
response-keyValue-
-
configMapKeyValues
map key value to config eg.features.features.id:1:invalid input: '<'$id>- Parameters:
response-keyValue-
-
getJsonValue
gets json value as list if applicable, or string if single item converts to string separated by ","- Parameters:
response-path-- Returns:
-
getJsonValue
gets json value as list if applicable, or string if single item converts to string separated by "," https://github.com/json-path/JsonPath- Parameters:
path- https://github.com/json-path/JsonPath for testing json path values: http://jsonpath.herokuapp.com/- Returns:
- value string list separated by ","
-
getJsonValue
gets json value as list if applicable, or string if single item converts to string separated by "," https://github.com/json-path/JsonPath- Parameters:
path- https://github.com/json-path/JsonPath for testing json path values: http://jsonpath.herokuapp.com/- Returns:
- value string list separated by ","
-
getJsonPathValue
gets json value as list if applicable, or string if single item converts to string separated by "," https://github.com/json-path/JsonPath- Parameters:
path- https://github.com/json-path/JsonPath for testing json path values: http://jsonpath.herokuapp.com/- Returns:
- value string list separated by ","
-
getJsonPathValue
public static Object getJsonPathValue(String json, String path, boolean isAlwaysReturnList, boolean checkError) gets json value as list if applicable, or string if single item converts to string separated by "," https://github.com/json-path/JsonPath- Parameters:
path- https://github.com/json-path/JsonPath for testing json path values: http://jsonpath.herokuapp.com/- Returns:
- value string list separated by ","
-
isJsonPathValueString
-
containsEscapeChar
\t Insert a tab in the text at this point. \b Insert a backspace in the text at this point. \n Insert a newline in the text at this point. \r Insert a carriage return in the text at this point. \f Insert a formfeed in the text at this point. \' Insert a single quote character in the text at this point. \" Insert a double quote character in the text at this point. \\ Insert a backslash character in the text at this point.- Parameters:
value-- Returns:
-
getJsonValueFromXml
get json path value from xml string- Parameters:
xml-path-- Returns:
-
XMLToJson
convert xml string to json string- Parameters:
xml- string- Returns:
- json string
-
getResponseValue
-
validateJsonKeywords
validates the json maps against the keyword requirements examples: "person.roles.name": hasItems("admin"), "person.lastName": equalTo("Administrator"), "person.lastName": isNotEmpty, "person.roles.name": contains("admin"), "person.roles.name": containsInAnyOrder(admin), "person.roles": nodeSizeGreaterThan(0), "person.sites.": nodeSizeExact(0) "person.roles.name": sequence("admin"),- Parameters:
jsonMap-response-
-
isJSONValid
validates json string- Parameters:
value-- Returns:
-
validateByJsonBody
validates expected json string against json body from response- Parameters:
expectedJson-actualJson-- Returns:
-
validateByJsonBody
public static String validateByJsonBody(String expectedJson, String response, boolean hasToBeJsonBody) validates expected json string against json body from response- Parameters:
expectedJson-actualJson-- Returns:
-
validateByKeywords
public static List<String> validateByKeywords(String expectedJson, io.restassured.response.Response response) validates json response against keywords- Parameters:
expectedJson-response-
-
validateByKeywords
validates json response against keywords- Parameters:
expectedJson-response-
-
validateResponseBody
public static String validateResponseBody(String expected, io.restassured.response.Response response) validates response body this is validating the response body as text- Parameters:
expected-response-- Returns:
-
validateResponseBody
validates response body this is validating the response body as text- Parameters:
expected-response-- Returns:
-
removeResponseIndicator
remove response indicators- Parameters:
expected-- Returns:
-
getRequestBodyFromJsonTemplate
if request body is empty, return json template string if request body contains @ variable tag, replace tag with value format for request body: json path:position:value or json path:vlaue eg. "features.feature.name:1:value_invalid input: '<'@_TIME_19>"- Parameters:
serviceObject-- Returns:
-
isJsonFile
return true if file is json file- Parameters:
filename-- Returns:
-
convertJsonFileToString
-
updateJsonFromRequestBody
-
updateJsonFromRequestBody
-
replaceJsonPathValue
replace json string value based on json path eg. path: cars.name:toyota or cars.name:2:toyota- Parameters:
jsonString-path-value-- Returns:
-
isJsonPathValueExist
-
convertToObject
convert string to object converts string to appropriate object type json supports string, number, boolean, null, object, array- Parameters:
value-isPathValueString- the path value to replace. if string, then replacement type will be string- Returns:
-
getJsonArray
return true if json string is a json array- Parameters:
jsonString-- Returns:
-
getJsonObject
return true if json string is a json o- Parameters:
jsonString-- Returns:
-
isValidJsonObject
checks if json string is a structured json body with key value pairs, or just array list. eg: [{"key":"value"}] vs ["value1","value2"] when syntax is {} then this is JsonObject when syntax is [] then this is JsonArray- Parameters:
jsonString-- Returns:
-
arrayRemoveAllEmptyString
-