Search in sources :

Example 1 with ObjectMapperService

use of com.testsigma.automator.service.ObjectMapperService in project testsigma by testsigmahq.

the class RestAPIRunTimeDataProcessor method storeRuntimeVariblesJsonPath.

private void storeRuntimeVariblesJsonPath(String expectedStr, String actualStr) throws AutomatorException {
    if (StringUtils.isNotBlank(expectedStr) && StringUtils.isNotBlank(actualStr)) {
        boolean jsonPathValidationsFailed = false;
        List<String> failedJsonPathsList = new ArrayList<>();
        Map<String, String> pathMap = new ObjectMapperService().parseJson(expectedStr, new TypeReference<>() {
        });
        for (Map.Entry<String, String> map : pathMap.entrySet()) {
            String name = map.getKey();
            String path = map.getValue();
            Object pathResult;
            try {
                if (path.equals(JSON_PATH_ALL)) {
                    pathResult = actualStr;
                } else {
                    pathResult = JsonPath.parse(actualStr).read(path);
                }
                new RuntimeDataProvider().storeRuntimeVariable(name, pathResult.toString());
            } catch (PathNotFoundException e) {
                jsonPathValidationsFailed = true;
                failedJsonPathsList.add(path);
                log.error("JSON Path Error while saving response to runtime variable.", e);
            }
        }
        if (jsonPathValidationsFailed) {
            throw new AutomatorException(String.format(MSG_REST_RESPONSE_JSON_PATH_NOT_EXIST, failedJsonPathsList));
        }
    }
}
Also used : AutomatorException(com.testsigma.automator.exceptions.AutomatorException) ObjectMapperService(com.testsigma.automator.service.ObjectMapperService) ArrayList(java.util.ArrayList) RuntimeDataProvider(com.testsigma.automator.utilities.RuntimeDataProvider) PathNotFoundException(com.jayway.jsonpath.PathNotFoundException) Map(java.util.Map)

Example 2 with ObjectMapperService

use of com.testsigma.automator.service.ObjectMapperService in project testsigma by testsigmahq.

the class RestApiResponseValidator method validateJsonPath.

private void validateJsonPath(String expectedStr, String actualStr, String msg) throws AutomatorException {
    if (StringUtils.isNotBlank(expectedStr)) {
        Map<String, String> expectedMap = new ObjectMapperService().parseJson(expectedStr, new TypeReference<>() {
        });
        for (Map.Entry<String, String> map : expectedMap.entrySet()) {
            String path = map.getKey();
            String value = map.getValue();
            Object pathResult;
            try {
                pathResult = JsonPath.parse(actualStr).read(path);
            } catch (PathNotFoundException e) {
                log.error("JSON Path Error while validating response .", e);
                throw new AutomatorException(String.format(MSG_REST_RESPONSE_JSON_PATH_NOT_EXIST, path));
            }
            String pathResultStr;
            StringBuilder sb = (testCaseStepResult.getMessage() != null) ? new StringBuilder(testCaseStepResult.getMessage()).append(AutomatorMessages.MSG_SEPARATOR) : new StringBuilder();
            if (pathResult instanceof String || pathResult instanceof Number) {
                pathResultStr = pathResult.toString();
                if (!value.equals(pathResultStr)) {
                    msg = sb.append(msg).append(AutomatorMessages.MSG_SEPARATOR).append(AutomatorMessages.getMessage(AutomatorMessages.MSG_REST_ERROR_PATH, path)).toString();
                    testCaseStepResult.setResult(ResultConstant.FAILURE);
                    testCaseStepResult.setMessage(msg);
                }
            } else {
                pathResultStr = new ObjectMapperService().convertToJson(pathResult);
                if (!value.equals(pathResultStr)) {
                    new ObjectMapperService().parseJson(pathResultStr, Object.class);
                    validateJson(pathResultStr, value, JSONCompareMode.STRICT.name(), msg);
                }
            }
        }
    }
}
Also used : AutomatorException(com.testsigma.automator.exceptions.AutomatorException) ObjectMapperService(com.testsigma.automator.service.ObjectMapperService) JSONObject(org.json.JSONObject) PathNotFoundException(com.jayway.jsonpath.PathNotFoundException) Map(java.util.Map)

Example 3 with ObjectMapperService

use of com.testsigma.automator.service.ObjectMapperService in project testsigma by testsigmahq.

the class RestApiResponseValidator method validateResponse.

public void validateResponse() throws AutomatorException {
    List<Integer> compareTypes = restfulStepEntity.getResultCompareTypes();
    Integer responseStatus = restApiResponse.getStatusCode();
    String responseStr = restApiResponse.getResponseText();
    Map<String, String> responseHeadersMap = restApiResponse.getHeadersMap();
    String responseHeaders = new ObjectMapperService().convertToJson(responseHeadersMap);
    validateStatus(restfulStepEntity, responseStatus);
    validateHeaders(restfulStepEntity.getResponseHeaders(), responseHeaders);
    validateResponseBody(restfulStepEntity.getResponse(), responseStr, restfulStepEntity.getResponseCompareType(), AutomatorMessages.MSG_REST_ERROR_CONTENT);
}
Also used : ObjectMapperService(com.testsigma.automator.service.ObjectMapperService)

Example 4 with ObjectMapperService

use of com.testsigma.automator.service.ObjectMapperService in project testsigma by testsigmahq.

the class RestAPIRunTimeDataProcessor method storeResponseData.

public void storeResponseData(HttpResponse<String> response) throws AutomatorException {
    String responseStr = response.getResponseText();
    Map<String, String> responseHeadersMap = response.getHeadersMap();
    String responseHeaders = new ObjectMapperService().convertToJson(responseHeadersMap);
    storeRuntimeVariblesJsonPath(restfulStepEntity.getHeaderRuntimeData(), responseHeaders);
    storeRuntimeVariblesJsonPath(restfulStepEntity.getBodyRuntimeData(), responseStr);
}
Also used : ObjectMapperService(com.testsigma.automator.service.ObjectMapperService)

Example 5 with ObjectMapperService

use of com.testsigma.automator.service.ObjectMapperService in project testsigma by testsigmahq.

the class WebserviceUtil method setAuthorizationHeaders.

private void setAuthorizationHeaders(RestfulStepEntity entity, Map<String, String> headers) {
    log.debug("Set Authorization headers for entity:" + entity);
    log.debug("Set Authorization headers ,headers:" + headers);
    if (entity.getAuthorizationType() != null) {
        headers = (headers != null) ? headers : new HashMap<>();
        Map<String, String> info = new ObjectMapperService().parseJson(entity.getAuthorizationValue(), new TypeReference<>() {
        });
        if (AuthorizationTypes.BASIC == entity.getAuthorizationType()) {
            headers.put(HttpHeaders.AUTHORIZATION, com.testsigma.automator.http.HttpClient.BASIC_AUTHORIZATION + " " + encodedCredentials(info.get("username") + ":" + info.get("password")));
        } else if (entity.getAuthorizationType() == AuthorizationTypes.BEARER) {
            headers.put("Authorization", "Bearer " + info.get("Bearertoken"));
        }
    }
}
Also used : HashMap(java.util.HashMap) ObjectMapperService(com.testsigma.automator.service.ObjectMapperService)

Aggregations

ObjectMapperService (com.testsigma.automator.service.ObjectMapperService)8 Map (java.util.Map)4 PathNotFoundException (com.jayway.jsonpath.PathNotFoundException)3 AutomatorException (com.testsigma.automator.exceptions.AutomatorException)3 HashMap (java.util.HashMap)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 AutomatorConfig (com.testsigma.automator.AutomatorConfig)1 AutomatorMessages (com.testsigma.automator.constants.AutomatorMessages)1 DriverSessionType (com.testsigma.automator.constants.DriverSessionType)1 ErrorCodes (com.testsigma.automator.constants.ErrorCodes)1 DriverManager (com.testsigma.automator.drivers.DriverManager)1 com.testsigma.automator.entity (com.testsigma.automator.entity)1 TestsigmaFileNotFoundException (com.testsigma.automator.exceptions.TestsigmaFileNotFoundException)1 TestsigmaTestdataNotFoundException (com.testsigma.automator.exceptions.TestsigmaTestdataNotFoundException)1 RuntimeDataProvider (com.testsigma.automator.utilities.RuntimeDataProvider)1 ScreenCaptureUtil (com.testsigma.automator.utilities.ScreenCaptureUtil)1 ScreenshotUploadTask (com.testsigma.automator.utilities.ScreenshotUploadTask)1 UploadThreadPool (com.testsigma.automator.utilities.UploadThreadPool)1 File (java.io.File)1 IOException (java.io.IOException)1