Best Testsigma code snippet using com.testsigma.automator.webservices.RestApiResponseValidator.validateHeaders
Source:RestApiResponseValidator.java
...42 String responseStr = restApiResponse.getResponseText();43 Map<String, String> responseHeadersMap = restApiResponse.getHeadersMap();44 String responseHeaders = new ObjectMapperService().convertToJson(responseHeadersMap);45 validateStatus(restfulStepEntity, responseStatus);46 validateHeaders(restfulStepEntity.getResponseHeaders(),responseHeaders);47 validateResponseBody(restfulStepEntity.getResponse(), responseStr, restfulStepEntity.getResponseCompareType(),48 AutomatorMessages.MSG_REST_ERROR_CONTENT);49 }50 private void validateStatus(RestfulStepEntity entity, Integer responseStatus) throws AutomatorException{51 Integer expectedStatus = Integer.parseInt(entity.getStatus());52 if (!(expectedStatus == null || expectedStatus.equals(-1)) && !expectedStatus.equals(responseStatus)) {53 throw new AutomatorException(String.format(MSG_STATUS_MISMATCH,responseStatus,expectedStatus));54 }55 }56 private void validateResponseBody(String expectedStr, String actualStr, String compareType, String msg) throws AutomatorException {57 if (StringUtils.isBlank(expectedStr) || StringUtils.isBlank(actualStr)) {58 return;59 }60 if (!isJSONValid(actualStr)) {61 throw new AutomatorException(MSG_INVALID_JSON);62 }63 switch (JSONCompareMode.valueOf(compareType)) {64 case STRICT:65 case LENIENT:66 case NON_EXTENSIBLE:67 case STRICT_ORDER:68 validateJson(expectedStr, actualStr, compareType, msg);69 break;70 case JSON_PATH:71 validateJsonPath(expectedStr, actualStr, msg);72 break;73 case JSON_SCHEMA:74 validateJsonSchema(expectedStr, actualStr, msg);75 break;76 default:77 break;78 }79 }80 private void validateJsonSchema(String expectedSchema, String responseStr, String msg) {81 if (StringUtils.isNotBlank(expectedSchema)) {82 try {83 JSONObject rawSchema = new JSONObject(new JSONTokener(expectedSchema));84 Schema schema = SchemaLoader.load(rawSchema);85 schema.validate(new JSONObject(responseStr));86 } catch (Exception e) {87 log.error("JSON Schema validation failed.", e);88 testCaseStepResult.setResult(ResultConstant.FAILURE);89 testCaseStepResult.setMessage(msg + AutomatorMessages.MSG_SEPARATOR + e.getMessage());90 }91 }92 }93 private void validateJsonPath(String expectedStr, String actualStr, String msg) throws AutomatorException {94 if (StringUtils.isNotBlank(expectedStr)) {95 Map<String, String> expectedMap = new ObjectMapperService().parseJson(expectedStr, new TypeReference<>() {96 });97 for (Map.Entry<String, String> map : expectedMap.entrySet()) {98 String path = map.getKey();99 String value = map.getValue();100 Object pathResult;101 try{102 pathResult = JsonPath.parse(actualStr).read(path);103 } catch (PathNotFoundException e) {104 log.error("JSON Path Error while validating response .", e);105 throw new AutomatorException(String.format(MSG_REST_RESPONSE_JSON_PATH_NOT_EXIST,path));106 }107 String pathResultStr;108 StringBuilder sb = (testCaseStepResult.getMessage() != null) ?109 new StringBuilder(testCaseStepResult.getMessage()).append(AutomatorMessages.MSG_SEPARATOR) : new StringBuilder();110 if (pathResult instanceof String || pathResult instanceof Number) {111 pathResultStr = pathResult.toString();112 if (!value.equals(pathResultStr)) {113 msg = sb.append(msg).append(AutomatorMessages.MSG_SEPARATOR)114 .append(AutomatorMessages.getMessage(AutomatorMessages.MSG_REST_ERROR_PATH, path)).toString();115 testCaseStepResult.setResult(ResultConstant.FAILURE);116 testCaseStepResult.setMessage(msg);117 }118 } else {119 pathResultStr = new ObjectMapperService().convertToJson(pathResult);120 if (!value.equals(pathResultStr)) {121 new ObjectMapperService().parseJson(pathResultStr, Object.class);122 validateJson(pathResultStr, value, JSONCompareMode.STRICT.name(), msg);123 }124 }125 }126 }127 }128 private void validateHeaders(String expectedHeaders,String actualHeaders) throws AutomatorException{129 if (StringUtils.isNotBlank(expectedHeaders)) {130 try{131 JSONAssert.assertEquals(expectedHeaders, actualHeaders, org.skyscreamer.jsonassert.JSONCompareMode.LENIENT);132 }catch(AssertionError assertionError){133 throw new AutomatorException("Response header(s) verification/validation failed. " +134 "Please verify if the response headers contains expected headers."+assertionError.getMessage());135 }136 }137 }138 private void validateJson(String expectedStr, String actualStr, String compareType, String msg) {139 if (StringUtils.isNotBlank(expectedStr)) {140 try {141 JSONAssert.assertEquals(expectedStr, actualStr, org.skyscreamer.jsonassert.JSONCompareMode.valueOf(compareType));142 } catch (AssertionError ex) {...
validateHeaders
Using AI Code Generation
1import com.testsigma.automator.webservices.RestApiResponseValidator;2Map<String, String> expectedHeaders = new HashMap<String, String>();3expectedHeaders.put("Content-Type", "application/json");4expectedHeaders.put("Content-Encoding", "gzip");5expectedHeaders.put("Transfer-Encoding", "chunked");6expectedHeaders.put("Connection", "keep-alive");7expectedHeaders.put("Date", "Fri, 05 Jul 2019 10:12:28 GMT");8expectedHeaders.put("Server", "nginx/1.14.1");9Map<String, String> actualHeaders = new HashMap<String, String>();10actualHeaders.put("Content-Type", "application/json");11actualHeaders.put("Content-Encoding", "gzip");12actualHeaders.put("Transfer-Encoding", "chunked");13actualHeaders.put("Connection", "keep-alive");14actualHeaders.put("Date", "Fri, 05 Jul 2019 10:12:28 GMT");15actualHeaders.put("Server", "nginx/1.14.1");16RestApiResponseValidator.validateHeaders(expectedHeaders, actualHeaders);
validateHeaders
Using AI Code Generation
1import com.testsigma.automator.webservices.RestApiResponseValidator;2RestApiResponseValidator validator = new RestApiResponseValidator();3Map<String, String> expectedHeaders = new HashMap<String, String>();4expectedHeaders.put("Content-Type", "application/json; charset=utf-8");5expectedHeaders.put("Content-Length", "48");6expectedHeaders.put("ETag", "W/\"30-8q3aVJn9z9X7R5ZdY+ZtVw\"");7Map<String, String> actualHeaders = new HashMap<String, String>();8actualHeaders.put("Content-Type", "application/json; charset=utf-8");9actualHeaders.put("Content-Length", "48");10actualHeaders.put("ETag", "W/\"30-8q3aVJn9z9X7R5ZdY+ZtVw\"");11validator.validateHeaders(expectedHeaders, actualHeaders);
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!