Package core.apiCore.helpers
Class JsonHelper
- java.lang.Object
-
- core.apiCore.helpers.JsonHelper
-
public class JsonHelper extends Object
-
-
Field Summary
Fields Modifier and Type Field Description static StringfailOnEscapeChars
-
Constructor Summary
Constructors Constructor Description JsonHelper()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static List<String>arrayRemoveAllEmptyString(List<String> list)static voidconfigMapJsonKeyValues(io.restassured.response.Response response, KeyValue keyword)map key value to config eg.features.features.id:1:<$id>static voidconfigMapKeyValues(io.restassured.response.Response response, KeyValue keyword)map key value to config eg.features.features.id:1:<$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 responsestatic List<String>validateByKeywords(String expectedJson, io.restassured.response.Response response)validates json response against keywordsstatic List<String>validateByKeywords(String expectedJson, String responseString)validates json response against keywordsstatic List<String>validateJsonKeywords(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 StringXMLToJson(String xml)convert xml string to json string
-
-
-
Field Detail
-
failOnEscapeChars
public static String failOnEscapeChars
-
-
Method Detail
-
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:<$id>- Parameters:
response-keyValue-
-
configMapKeyValues
public static void configMapKeyValues(io.restassured.response.Response response, KeyValue keyword)map key value to config eg.features.features.id:1:<$id>- Parameters:
response-keyValue-
-
getJsonValue
public static String getJsonValue(io.restassured.response.Response response, String path)
gets json value as list if applicable, or string if single item converts to string separated by ","- Parameters:
response-path-- Returns:
-
getJsonValue
public static String getJsonValue(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/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
public static String getJsonValue(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/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)
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 ","
-
containsEscapeChar
public static boolean containsEscapeChar(String value)
\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
public static String getJsonValueFromXml(String xml, String path)
get json path value from xml string- Parameters:
xml-path-- Returns:
-
XMLToJson
public static String XMLToJson(String xml)
convert xml string to json string- Parameters:
xml- string- Returns:
- json string
-
getResponseValue
public static String getResponseValue(io.restassured.response.Response response)
-
validateJsonKeywords
public static List<String> validateJsonKeywords(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"),- Parameters:
jsonMap-response-
-
isJSONValid
public static boolean isJSONValid(String value, boolean isFailOnError)
validates json string- Parameters:
value-- Returns:
-
validateByJsonBody
public static String validateByJsonBody(String expectedJson, String response)
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
public static List<String> validateByKeywords(String expectedJson, String responseString)
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
public static String validateResponseBody(String expected, String responseString)
validates response body this is validating the response body as text- Parameters:
expected-response-- Returns:
-
removeResponseIndicator
public static String removeResponseIndicator(String expected)
remove response indicators- Parameters:
expected-- Returns:
-
getRequestBodyFromJsonTemplate
public static String getRequestBodyFromJsonTemplate(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. "features.feature.name:1:value_<@_TIME_19>"- Parameters:
serviceObject-- Returns:
-
isJsonFile
public static boolean isJsonFile(String filename)
return true if file is json file- Parameters:
filename-- Returns:
-
updateJsonFromRequestBody
public static String updateJsonFromRequestBody(ServiceObject service)
-
updateJsonFromRequestBody
public static String updateJsonFromRequestBody(String requestbody, String jsonString)
-
replaceJsonPathValue
public static String replaceJsonPathValue(String jsonString, String path, String value)
replace json string value based on json path eg. path: cars.name:toyota or cars.name:2:toyota- Parameters:
jsonString-path-value-- Returns:
-
isJsonPathValueExist
public static boolean isJsonPathValueExist(String jsonString, String jsonPath)
-
convertToObject
public static Object convertToObject(String value, boolean isPathValueString)
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
public static org.json.JSONArray getJsonArray(String jsonString)
return true if json string is a json array- Parameters:
jsonString-- Returns:
-
getJsonObject
public static org.json.JSONObject getJsonObject(String jsonString)
return true if json string is a json o- Parameters:
jsonString-- Returns:
-
isValidJsonObject
public static boolean isValidJsonObject(Object jsonObject)
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:
-
-