Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/develop' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
Sohandey committed Nov 22, 2023
2 parents f32b1f3 + 2f0968b commit 1f82975
Show file tree
Hide file tree
Showing 58 changed files with 1,509 additions and 1,460 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,20 @@
* @author Vignesh
*
*/
public class OutputValidationUtil extends AuthTestsUtil{
public class OutputValidationUtil extends AuthTestsUtil {

private static final Logger OUTPUTVALIDATION_LOGGER = Logger.getLogger(OutputValidationUtil.class);

public static void setLogLevel() {
if (ConfigManager.IsDebugEnabled())
OUTPUTVALIDATION_LOGGER.setLevel(Level.ALL);
else
OUTPUTVALIDATION_LOGGER.setLevel(Level.ERROR);
}

/**
* The method will perform output validation by comparing expected and actual value
* The method will perform output validation by comparing expected and actual
* value
*
* @param actualOutputFile
* @param expOutputFile
Expand All @@ -71,22 +72,22 @@ public static void setLogLevel() {
// return Collections.emptyMap();
// }
// }

/**
* The method will compare expected and actual value
*
* @param actual
* @param exp
* @param actVsExp
* @return map
* @throws JsonMappingException
* @throws JsonParseException
* @throws JsonMappingException
* @throws JsonParseException
*/
public static Map<String, List<OutputValidationDto>> compareActuExpValue(Map<String, String> actual,
Map<String, String> exp, String actVsExp) {
if(actual == null || exp == null)
if (actual == null || exp == null)
throw new SkipException("Marking testcase as Skipped, as no Output comparison done");

Map<String, List<OutputValidationDto>> objMap = new HashMap<>();
List<OutputValidationDto> objList = new ArrayList<OutputValidationDto>();
boolean comparisonDone = false;
Expand Down Expand Up @@ -145,7 +146,8 @@ public static Map<String, List<OutputValidationDto>> compareActuExpValue(Map<Str
objOpDto.setStatus(GlobalConstants.FAIL_STRING);
comparisonDone = true;
}
} else if (expEntry.getValue().contains(GlobalConstants.TOKENID_STRING) && expEntry.getValue().contains(".")) {
} else if (expEntry.getValue().contains(GlobalConstants.TOKENID_STRING)
&& expEntry.getValue().contains(".")) {
String key = expEntry.getValue().replace(GlobalConstants.TOKENID_STRING, "");
String[] keys = key.split(Pattern.quote("."));
String tokenid = RunConfigUtil.getTokenId(keys[0], keys[1]);
Expand Down Expand Up @@ -220,7 +222,8 @@ public static Map<String, List<OutputValidationDto>> compareActuExpValue(Map<Str
JsonPrecondtion.getJsonInOrder(EncryptDecrptUtil.getDecyptFromStr(content)));
if (compareTwoKycMap(expMap, actualMap)) {
Reporter.log("Kyc verification passed");
OUTPUTVALIDATION_LOGGER.info("Kyc Verification Passed \\n Expected Kyc: " + expMap + "\\n Actual Kyc: " + actualMap);
OUTPUTVALIDATION_LOGGER.info("Kyc Verification Passed \\n Expected Kyc: " + expMap
+ "\\n Actual Kyc: " + actualMap);
objOpDto.setFieldName(expEntry.getKey());
objOpDto.setFieldHierarchy(expEntry.getKey());
objOpDto.setActualValue(actualMap.toString());
Expand All @@ -229,7 +232,8 @@ public static Map<String, List<OutputValidationDto>> compareActuExpValue(Map<Str
comparisonDone = true;
} else {
Reporter.log("Kyc verification failed");
OUTPUTVALIDATION_LOGGER.error("Kyc Verification failed \\n Expected Kyc: " + expMap + "\\n Actual Kyc: " + actualMap);
OUTPUTVALIDATION_LOGGER.error("Kyc Verification failed \\n Expected Kyc: " + expMap
+ "\\n Actual Kyc: " + actualMap);
objOpDto.setFieldName(expEntry.getKey());
objOpDto.setFieldHierarchy(expEntry.getKey());
objOpDto.setActualValue(actualMap.toString());
Expand All @@ -255,11 +259,11 @@ public static Map<String, List<OutputValidationDto>> compareActuExpValue(Map<Str
} catch (JSONException | IOException e) {
OUTPUTVALIDATION_LOGGER.error("Kyc Verification failed " + e.getMessage());
}
if(comparisonDone == false)
if (comparisonDone == false)
throw new SkipException("Marking testcase as Skipped, as no Output comparison done");
return objMap;
}

/**
* The method will validate timestamp
*
Expand Down Expand Up @@ -306,11 +310,11 @@ public static boolean validateTimestamp(String timestamp) {
return false;
}
}

/**
* The methold will validate timestamp with Z format
*
* @param timestamp
* @param timestamp
* @return true or false
*/
public static boolean validateTimestampZ(String timestamp) {
Expand Down Expand Up @@ -351,7 +355,7 @@ public static boolean validateTimestampZ(String timestamp) {
return false;
}
}

/**
* The method will validate regular expression
*
Expand All @@ -365,7 +369,7 @@ public static boolean validateRegularExpression(String actValue, String regex) {
else
return false;
}

/**
* The method will publish report
*
Expand All @@ -385,12 +389,12 @@ public static boolean publishOutputResult(Map<String, List<OutputValidationDto>>
OUTPUTVALIDATION_LOGGER.info(GlobalConstants.EXPECTED_VALUE_STRING + dto.getExpValue());
OUTPUTVALIDATION_LOGGER.info(GlobalConstants.ACTUAL_VALUE_STRING + dto.getActualValue());
OUTPUTVALIDATION_LOGGER.info(GlobalConstants.STATUS_STRING + dto.getStatus());
}else if (dto.getStatus().equals("WARNING")) {
} else if (dto.getStatus().equals("WARNING")) {
OUTPUTVALIDATION_LOGGER.info(GlobalConstants.JSONFIELD_PATH_STRING + dto.getFieldName());
OUTPUTVALIDATION_LOGGER.info(GlobalConstants.EXPECTED_VALUE_STRING + dto.getExpValue());
OUTPUTVALIDATION_LOGGER.info(GlobalConstants.ACTUAL_VALUE_STRING + dto.getActualValue());
OUTPUTVALIDATION_LOGGER.info(GlobalConstants.STATUS_STRING + dto.getStatus());
}else if (dto.getStatus().equals(GlobalConstants.FAIL_STRING)) {
} else if (dto.getStatus().equals(GlobalConstants.FAIL_STRING)) {
OUTPUTVALIDATION_LOGGER.error(GlobalConstants.JSONFIELD_PATH_STRING + dto.getFieldName());
OUTPUTVALIDATION_LOGGER.error(GlobalConstants.EXPECTED_VALUE_STRING + dto.getExpValue());
OUTPUTVALIDATION_LOGGER.error(GlobalConstants.ACTUAL_VALUE_STRING + dto.getActualValue());
Expand All @@ -403,7 +407,7 @@ public static boolean publishOutputResult(Map<String, List<OutputValidationDto>>
"*******************************************************************************************************");
return outputStatus;
}

/**
* The method will perform token id operation for uin
*
Expand All @@ -426,107 +430,112 @@ public static void performTokenIdOper(String uin, String tspId, String tokenId)
generateMappingDic(file.getAbsolutePath(), map);
}
}

public static boolean compareTwoKycMap(Map<String, Object> expMap, Map<String, Object> actualMap) {
for (Entry<String, Object> entry : expMap.entrySet()) {
if (actualMap.containsKey(entry.getKey())) {
if(entry.getValue()==null || actualMap.get(entry.getKey())==null)

if (entry.getValue() == null || actualMap.get(entry.getKey()) == null)
continue;
try {
if (actualMap.get(entry.getKey()).toString().contains(",")
&& entry.getValue().toString().contains(",")) {
String[] value = entry.getValue().toString().split(Pattern.quote("}, {"));
for (int i = 0; i < value.length; i++) {
String normalise = value[i].replace("{", "").replace("[", "").replace("}", "")
.replace("]", "");
if (!actualMap.get(entry.getKey()).toString().contains(normalise)) {
return false;
if (actualMap.get(entry.getKey()).toString().contains(",")
&& entry.getValue().toString().contains(",")) {
String[] value = entry.getValue().toString().split(Pattern.quote("}, {"));
for (int i = 0; i < value.length; i++) {
String normalise = value[i].replace("{", "").replace("[", "").replace("}", "").replace("]",
"");
if (!actualMap.get(entry.getKey()).toString().contains(normalise)) {
return false;
}
}
} else if (!actualMap.get(entry.getKey()).equals(entry.getValue())) {
return false;
}
}
else if (!actualMap.get(entry.getKey()).equals(entry.getValue())) {
return false;
}
}catch(Exception e)
{
} catch (Exception e) {
OUTPUTVALIDATION_LOGGER.error(e.getMessage());
}
}
}
return true;
}

public static Map<String, List<OutputValidationDto>> doJsonOutputValidation(String actualOutputJson,
String expOutputJson, boolean checkErrorsOnlyInResponse, String allowedErrorCode) throws AdminTestException {
return doJsonOutputValidation(actualOutputJson,
expOutputJson, checkErrorsOnlyInResponse, GlobalConstants.EXPECTED_VS_ACTUAL, doesResponseHasErrors(actualOutputJson), allowedErrorCode);
String expOutputJson, boolean checkErrorsOnlyInResponse, String allowedErrorCode, int responseStatusCode)
throws AdminTestException {
return doJsonOutputValidation(actualOutputJson, expOutputJson, checkErrorsOnlyInResponse,
GlobalConstants.EXPECTED_VS_ACTUAL, doesResponseHasErrors(actualOutputJson), allowedErrorCode,
responseStatusCode);
}

public static Map<String, List<OutputValidationDto>> doJsonOutputValidation(String actualOutputJson,
String expOutputJson, boolean checkErrorsOnlyInResponse) throws AdminTestException {
return doJsonOutputValidation(actualOutputJson,
expOutputJson, checkErrorsOnlyInResponse, GlobalConstants.EXPECTED_VS_ACTUAL, doesResponseHasErrors(actualOutputJson), null);
String expOutputJson, boolean checkErrorsOnlyInResponse, int responseStatusCode) throws AdminTestException {
return doJsonOutputValidation(actualOutputJson, expOutputJson, checkErrorsOnlyInResponse,
GlobalConstants.EXPECTED_VS_ACTUAL, doesResponseHasErrors(actualOutputJson), null, responseStatusCode);
}



public static Map<String, List<OutputValidationDto>> doJsonOutputValidation(String actualOutputJson,
String expOutputJson, boolean checkErrorsOnlyInResponse, String context, boolean responseHasErrors, String allowedErrorCode) throws AdminTestException {
String expOutputJson, boolean checkErrorsOnlyInResponse, String context, boolean responseHasErrors,
String allowedErrorCode, int responseStatusCode) throws AdminTestException {
if (doesResponseHasErrorCode(actualOutputJson, 500))
throw new AdminTestException("Internal Server Error. Hence marking the test case as failed");
else if (doesResponseHasErrorCode(actualOutputJson, 404))
throw new AdminTestException("Page not found. Hence marking the test case as failed");
throw new SkipException("API end point is not valid. Hence marking the test case as skipped");
else if (!(responseStatusCode >= 200 && responseStatusCode < 300))
throw new SkipException("API endpoint is not valid. Response code: " + responseStatusCode + " Hence marking the test case as skipped");
else if (doesResponseHasErrorCode(actualOutputJson, allowedErrorCode))
return Collections.emptyMap();
JsonPrecondtion jsonPrecondtion = new JsonPrecondtion();
Map<String, String> actual = jsonPrecondtion.retrieveMappingAndItsValueToPerformJsonOutputValidation(actualOutputJson);
Map<String, String> exp = jsonPrecondtion.retrieveMappingAndItsValueToPerformJsonOutputValidation(expOutputJson);

return doJsonOutputValidation(actual, exp, checkErrorsOnlyInResponse, context, responseHasErrors, allowedErrorCode);
Map<String, String> actual = jsonPrecondtion
.retrieveMappingAndItsValueToPerformJsonOutputValidation(actualOutputJson);
Map<String, String> exp = jsonPrecondtion
.retrieveMappingAndItsValueToPerformJsonOutputValidation(expOutputJson);

return doJsonOutputValidation(actual, exp, checkErrorsOnlyInResponse, context, responseHasErrors,
allowedErrorCode, responseStatusCode);
}

public static Map<String, List<OutputValidationDto>> doJsonOutputValidation(Map<String, String> actualOutput,
Map<String, String> expOutput, boolean checkErrorsOnlyInResponse, String context, boolean responseHasErrors, String allowedErrorCode ) throws AdminTestException {
Map<String, String> expOutput, boolean checkErrorsOnlyInResponse, String context, boolean responseHasErrors,
String allowedErrorCode, int responseStatusCode) throws AdminTestException {
try {
return compareActuExpValue(actualOutput, expOutput, context);
}catch (SkipException e) {
if (responseHasErrors)
} catch (SkipException e) {
if (responseHasErrors)
throw new AdminTestException("Response has errors");
else if(!checkErrorsOnlyInResponse)
else if (!checkErrorsOnlyInResponse)
throw new SkipException(e.getMessage());
else
else
return Collections.emptyMap(); // No output validation required. Hence marking the test case as passed
}
}

public static boolean doesResponseHasErrors(String responseString) {
JSONObject responseJson = new JSONObject(responseString);
boolean breturn = false;
JSONArray errors = null;
String error = null;
if (responseJson.has("errors")) {
errors = responseJson.optJSONArray("errors");
}
else if (responseJson.has("error")) {
} else if (responseJson.has("error")) {
error = responseJson.getString("error");
}
if (errors != null)
breturn = (errors.length() > 0);
else if (error != null)
breturn = true;

return breturn;
}

public static boolean doesResponseHasErrorCode(String responseString, int errorCode) {
JSONObject responseJson = new JSONObject(responseString);
if (responseJson.has("status")) {
return responseJson.getInt("status") == errorCode;
}

return false;
}

public static boolean doesResponseHasErrorCode(String responseString, String allowedErrorCode) {
boolean responseHasAllowedErrorCode = false;
if (allowedErrorCode != null) {
Expand All @@ -540,5 +549,5 @@ public static boolean doesResponseHasErrorCode(String responseString, String all
}
return responseHasAllowedErrorCode;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,11 @@ public class GlobalConstants {
public static final String APIKEY = "apiKey";
public static final String PARTNER_APIKEY = "partnerApiKey";
public static final String PARTNERID = "partnerId";
public static final String REPORT_RESPONSE_PREFIX = "<b><u>Response: </u></b>(End Point URL: ";
public static final String REPORT_RESPONSE_PREFIX = "<b><u>Response: </u></b>";
public static final String REPORT_RESPONSE_SUFFIX = "</pre>";
public static final String REPORT_RESPONSE_BODY = ") <pre>";
public static final String REPORT_REQUEST_PREFIX = "<pre>";
public static final String REPORT_RESPONSE_BODY = "<pre>";
public static final String REPORT_REQUEST_PREFIX = "<b><u>Request: </u></b>(End Point URL: ";
public static final String REPORT_REQUEST_BODY = ") <pre>";
public static final String REPORT_REQUEST_SUFFIX = "</pre>";
public static final String EXCEPTION_STRING_1 = " Exception: ";
public static final String EXCEPTION_STRING_2 = "Exception ";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,30 @@ public class GlobalMethods {

private static final Logger logger = Logger.getLogger(GlobalMethods.class);
public static void ReportRequestAndResponse(String reqHeader,String resHeader,String url, String requestBody, String response) {
reportRequest(reqHeader,requestBody);
reportRequest(reqHeader,requestBody, url);
reportResponse(resHeader,url, response);
}


public static void reportRequest(String requestHeader, String request) {
reportRequest(requestHeader, request, "");
}

public static void reportRequest(String requestHeader, String request, String url) {

String formattedHeader = ReportUtil.getTextAreaForHeaders(requestHeader);

if (request != null && !request.equals("{}"))
Reporter.log(GlobalConstants.REPORT_REQUEST_PREFIX + formattedHeader + ReportUtil.getTextAreaJsonMsgHtml(request)
Reporter.log(GlobalConstants.REPORT_REQUEST_PREFIX + url + GlobalConstants.REPORT_REQUEST_BODY + formattedHeader + ReportUtil.getTextAreaJsonMsgHtml(request)
+ GlobalConstants.REPORT_REQUEST_SUFFIX);
else
Reporter.log(
GlobalConstants.REPORT_REQUEST_PREFIX + formattedHeader + "No request body" + GlobalConstants.REPORT_REQUEST_SUFFIX);
GlobalConstants.REPORT_REQUEST_PREFIX + url + GlobalConstants.REPORT_REQUEST_BODY + formattedHeader + "No request body" + GlobalConstants.REPORT_REQUEST_SUFFIX);
}

public static void reportResponse(String responseHeader, String url, Response response) {
String formattedHeader = ReportUtil.getTextAreaForHeaders(responseHeader);

Reporter.log(GlobalConstants.REPORT_RESPONSE_PREFIX + url + GlobalConstants.REPORT_RESPONSE_BODY + formattedHeader
Reporter.log(GlobalConstants.REPORT_RESPONSE_PREFIX + GlobalConstants.REPORT_RESPONSE_BODY + formattedHeader
+ ReportUtil.getTextAreaJsonMsgHtml(response.asString()) + GlobalConstants.REPORT_RESPONSE_SUFFIX);
}

Expand All @@ -49,10 +52,10 @@ public static void reportResponse(String responseHeader, String url, String resp
String formattedHeader = ReportUtil.getTextAreaForHeaders(responseHeader);

if (formatResponse)
Reporter.log(GlobalConstants.REPORT_RESPONSE_PREFIX + url + GlobalConstants.REPORT_RESPONSE_BODY + formattedHeader
Reporter.log(GlobalConstants.REPORT_RESPONSE_PREFIX + GlobalConstants.REPORT_RESPONSE_BODY + formattedHeader
+ ReportUtil.getTextAreaJsonMsgHtml(response) + GlobalConstants.REPORT_RESPONSE_SUFFIX);
else
Reporter.log(GlobalConstants.REPORT_RESPONSE_PREFIX + url + GlobalConstants.REPORT_RESPONSE_BODY + responseHeader + response
Reporter.log(GlobalConstants.REPORT_RESPONSE_PREFIX + GlobalConstants.REPORT_RESPONSE_BODY + responseHeader + response
+ GlobalConstants.REPORT_RESPONSE_SUFFIX);
}

Expand Down
Loading

0 comments on commit 1f82975

Please sign in to comment.