Search in sources :

Example 6 with ObjectMapperService

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

the class WebserviceUtil method getEntity.

private HttpEntity getEntity(RestfulStepEntity entity, Map<String, String> envSettings, Map<String, String> headers) throws Exception {
    MultipartEntityBuilder builder = MultipartEntityBuilder.create();
    if (entity.getIsMultipart() != null && entity.getIsMultipart()) {
        removeContentTypeHeader(headers);
        Map<String, Object> payload = new ObjectMapperService().parseJson(entity.getPayload(), new TypeReference<>() {
        });
        for (Map.Entry<String, Object> data : payload.entrySet()) {
            removeContentTypeHeader(headers);
            boolean isFileUrl = data.getValue() != null && (data.getValue().toString().startsWith("http") || data.getValue().toString().startsWith("https"));
            if (isFileUrl) {
                String filePath = downloadFile(data.getValue().toString(), envSettings);
                String[] fileNames = filePath.split(File.separator);
                builder.addBinaryBody(data.getKey(), new File(filePath), ContentType.DEFAULT_BINARY, fileNames[fileNames.length - 1]);
            } else {
                builder.addPart(data.getKey(), new StringBody(new ObjectMapperService().convertToJson(data.getValue()), ContentType.APPLICATION_JSON));
            }
        }
        return builder.build();
    } else if (entity.getPayload() != null) {
        return new StringEntity(entity.getPayload());
    }
    return null;
}
Also used : MultipartEntityBuilder(org.apache.http.entity.mime.MultipartEntityBuilder) ObjectMapperService(com.testsigma.automator.service.ObjectMapperService) StringEntity(org.apache.http.entity.StringEntity) StringBody(org.apache.http.entity.mime.content.StringBody) HashMap(java.util.HashMap) Map(java.util.Map) File(java.io.File)

Example 7 with ObjectMapperService

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

the class WebserviceUtil method execute.

public void execute(TestCaseStepEntity testcaseStep, TestCaseStepResult result, Map<String, String> envSettings, TestCaseResult testCaseResult) throws TestsigmaTestdataNotFoundException {
    log.debug("Executing Rest step:" + testcaseStep);
    RestfulStepEntity entity = new ObjectMapper().convertValue(testcaseStep.getAdditionalData().get(TestCaseStepEntity.REST_DETAILS_KEY), RestfulStepEntity.class);
    result.setResult(ResultConstant.SUCCESS);
    try {
        log.debug("Updating Rest step variables for RestStepEntity:" + entity);
        new RestAPIRunTimeDataProcessor(entity, result).processRestAPIStep();
        initializeHttpClient(entity);
        Map<String, String> headers = fetchHeadersFromRestStep(entity);
        setAuthorizationHeaders(entity, headers);
        setDefaultHeaders(entity, headers);
        entity.setUrl(entity.getUrl().replace("\\", "/"));
        HttpResponse<String> response = executeRestCall(entity.getUrl(), RequestMethod.valueOf(entity.getMethod().toUpperCase()), headers, getEntity(entity, envSettings, headers));
        log.debug("Rest Url - " + entity.getUrl() + " Method " + entity.getMethod().toUpperCase() + " Headers - " + new ObjectMapperService().convertToJson(headers) + " PayLoad - " + entity.getPayload());
        result.getMetadata().setReqEntity(entity);
        log.debug("Method - " + entity.getMethod().toUpperCase() + "response - " + new ObjectMapperService().convertToJson(response));
        ((Map<String, Object>) (testcaseStep.getAdditionalData().get(TestCaseStepEntity.REST_DETAILS_KEY))).put("url", entity.getUrl());
        WebserviceResponse resObj = new WebserviceResponse();
        resObj.setStatus(response.getStatusCode());
        if (entity.getStoreMetadata()) {
            resObj.setContent(response.getResponseText());
            resObj.setHeaders(response.getHeadersMap());
        }
        result.getMetadata().setRestResult(resObj);
        new RestApiResponseValidator(entity, result, response).validateResponse();
        new RestAPIRunTimeDataProcessor(entity, result).storeResponseData(response);
    } catch (Exception e) {
        log.error("Error while executing Rest Step:" + testcaseStep, e);
        String genericExceptionMessage = getExceptionSpecificMessage(e, result);
        result.setResult(ResultConstant.FAILURE);
        result.setMessage(genericExceptionMessage);
    }
    log.debug("Test Step Result :: " + new ObjectMapperService().convertToJson(result));
}
Also used : ObjectMapperService(com.testsigma.automator.service.ObjectMapperService) HashMap(java.util.HashMap) Map(java.util.Map) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ProtocolException(org.apache.http.ProtocolException) ClientProtocolException(org.apache.http.client.ClientProtocolException) TestsigmaTestdataNotFoundException(com.testsigma.automator.exceptions.TestsigmaTestdataNotFoundException) IOException(java.io.IOException) TestsigmaFileNotFoundException(com.testsigma.automator.exceptions.TestsigmaFileNotFoundException) UnknownHostException(java.net.UnknownHostException) CircularRedirectException(org.apache.http.client.CircularRedirectException) PathNotFoundException(com.jayway.jsonpath.PathNotFoundException)

Example 8 with ObjectMapperService

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

the class TestcaseRunner method run.

public TestCaseResult run() throws Exception {
    resetThreadContextData();
    populateThreadContextData();
    log.info("Running testcase - " + testCaseEntity.getTestCaseName());
    if (testDeviceEntity.getCreateSessionAtCaseLevel()) {
        startSession();
    } else {
        restartCurrentSession();
    }
    environmentRunResult.setSessionCreatedOn(new Timestamp(System.currentTimeMillis()));
    ResultConstant result = ResultConstant.SUCCESS;
    List<TestCaseStepEntity> stepList = testCaseEntity.getTestSteps();
    List<TestCaseStepResult> testCaseStepsResult = new ArrayList<>();
    testCaseResult.setTestCaseStepResults(testCaseStepsResult);
    testCaseResult.setIsStepGroup(testCaseEntity.getIsStepGroup());
    testCaseResult.setDataDriven(testCaseEntity.getIsDataDriven());
    testCaseResult.setTestPlanResultId(testDeviceEntity.getExecutionRunId());
    testCaseResult.setTestCaseName(testCaseEntity.getTestCaseName());
    try {
        HashMap<Long, TestCaseStepResult> parentStatus = new HashMap<>();
        int currentIndex = 0;
        testCaseResult.setCurrentIndex(currentIndex);
        int lastStep = stepList.size() - 1;
        ScreenCaptureUtil screenCaptureUtil = new ScreenCaptureUtil(new ArrayList<>());
        for (int i = 0; i < stepList.size(); i++) {
            TestCaseStepEntity testCaseStepEntity = stepList.get(i);
            TestCaseStepResult testCaseStepResult = new TestCaseStepResult();
            testCaseStepResult.setTestCaseStepId(testCaseStepEntity.getId());
            testCaseStepResult.setSkipExe(skipExecution);
            testCaseStepResult.setSkipMessage(resultFailureMessage);
            /*  if (!skipExecution && (workspaceType.equals(WorkspaceType.WebApplication) || workspaceType
          .equals(WorkspaceType.MobileWeb))) {
          url = getUrl();
        }*/
            boolean skipGetUrl = false;
            if (testCaseStepEntity.getAction() != null) {
                skipGetUrl = Arrays.asList(SKIP_GETURL).stream().filter(action -> testCaseStepEntity.getAction().contains(action)).count() > 0;
            }
            TestStepType type = testCaseStepEntity.getType();
            TestcaseStepRunner testcaseStepRunner = new TestcaseStepRunnerFactory().getRunner(this.workspaceType, testDeviceSettings.getOs(), type);
            TestCaseStepResult parentResult = parentStatus.get(testCaseStepEntity.getParentId());
            RunnerUtil util = new RunnerUtil();
            boolean isFailure = util.canSkipNormalStep(parentResult, testCaseStepEntity, testCaseStepResult) || util.nestedConditionalStep(parentResult, testCaseStepEntity, testCaseStepResult) || util.canSkipIfElse(parentResult, testCaseStepEntity, testCaseStepResult) || util.canSkipIfElseIf(parentResult, testCaseStepEntity, testCaseStepResult) || util.canSkipElseIfElseIf(parentResult, testCaseStepEntity, testCaseStepResult) || util.canSkipElseIfElse(parentResult, testCaseStepEntity, testCaseStepResult) || util.canSkipIfCondition(parentResult, testCaseStepEntity, testCaseStepResult) || util.canSkipForLoop(parentResult, testCaseStepEntity, testCaseStepResult);
            log.info(new ObjectMapperService().convertToJson(testCaseStepEntity));
            int stepResultUpdateSize = 10;
            if (!skipExecution && isFailure) {
                parentStatus.put(testCaseStepEntity.getId(), testCaseStepResult);
                testCaseStepResult.setResult(ResultConstant.NOT_EXECUTED);
                int processedSteps = processedStepCount(testCaseStepsResult);
                if (processedSteps % stepResultUpdateSize == 0) {
                    testCaseResult.setTestCaseStepResults(testCaseStepsResult);
                    postTestcaseResult();
                    currentIndex = +stepResultUpdateSize;
                    processedSteps = 0;
                    testCaseStepsResult = new ArrayList<>();
                    testCaseResult.setCurrentIndex(currentIndex);
                }
                if (screenCaptureUtil.screenshots.size() > 0 && (lastStep == i)) {
                    log.debug("Screenshots task for :::" + testCaseStepResult);
                    final String requestId = ThreadContext.get("X-Request-Id");
                    UploadThreadPool.getInstance().upload(new ScreenshotUploadTask(screenCaptureUtil.screenshots, requestId, testDeviceEntity));
                    screenCaptureUtil.screenshots = new ArrayList<>();
                }
                testCaseStepResult = testcaseStepRunner.run(testCaseStepEntity, testCaseStepResult, mapStepResult, testCaseResult, parentStatus, false, false, screenCaptureUtil);
                testCaseStepsResult.add(testCaseStepResult);
                mapStepResult.put(testCaseStepEntity.getId(), testCaseStepResult);
                continue;
            }
            testCaseStepResult = testcaseStepRunner.run(testCaseStepEntity, testCaseStepResult, mapStepResult, testCaseResult, parentStatus, false, false, screenCaptureUtil);
            if (screenCaptureUtil.screenshots.size() == SAVE_BATCH_IMAGES || (lastStep == i)) {
                final String requestId = ThreadContext.get("X-Request-Id");
                UploadThreadPool.getInstance().upload(new ScreenshotUploadTask(screenCaptureUtil.screenshots, requestId, testDeviceEntity));
                screenCaptureUtil.screenshots = new ArrayList<>();
            }
            skipExecution = testCaseStepResult.getSkipExe();
            resultFailureMessage = testCaseStepResult.getSkipMessage();
            testCaseStepsResult.add(testCaseStepResult);
            mapStepResult.put(testCaseStepEntity.getId(), testCaseStepResult);
            TestCaseStepEntity stepGroup = null;
            TestCaseStepResult stepGroupResult = null;
            if (testCaseStepEntity.getStepGroupId() != null) {
                stepGroup = ((testCaseStepEntity.getTestCaseSteps() != null) && (testCaseStepEntity.getTestCaseSteps().size() > 0)) ? testCaseStepEntity.getTestCaseSteps().get(0) : null;
                stepGroupResult = ((testCaseStepResult.getStepResults() != null) && (testCaseStepResult.getStepResults().size() > 0)) ? testCaseStepResult.getStepResults().get(0) : null;
            }
            if (i == 0 && ((workspaceType == WorkspaceType.WebApplication) || (workspaceType == WorkspaceType.MobileWeb)) && (getUrl() != null) && !skipGetUrl && ((testCaseStepEntity.getStepGroupId() != null && stepGroup != null && (stepGroup.getStepGroupId() != null && !stepGroup.getAction().contains(AutomatorMessages.KEYWORD_GO_TO) && stepGroupResult.getResult() == ResultConstant.SUCCESS)) || (testCaseStepEntity.getAction() != null && !testCaseStepEntity.getAction().contains(AutomatorMessages.KEYWORD_GO_TO) && testCaseStepResult.getResult() == ResultConstant.SUCCESS))) {
            } else if (i == (stepList.size() - 1) && !skipGetUrl && testCaseStepResult.getResult() == ResultConstant.SUCCESS && (workspaceType.equals(WorkspaceType.WebApplication) || workspaceType.equals(WorkspaceType.MobileWeb))) {
                ExecutionEnvironmentRunner.addUrl(testPlanId, testCaseEntity.getId(), getUrl());
            }
            // TODO:use check based step type
            if (testCaseStepEntity.getConditionType() == null || testCaseStepEntity.getConditionType() == ConditionType.NOT_USED || ConditionType.LOOP_FOR == testCaseStepEntity.getConditionType()) {
                result = (result.getId() < testCaseStepResult.getResult().getId()) ? testCaseStepResult.getResult() : result;
            }
            int processedSteps = processedStepCount(testCaseStepsResult);
            if (processedSteps % stepResultUpdateSize == 0) {
                testCaseResult.setTestCaseStepResults(testCaseStepsResult);
                postTestcaseResult();
                currentIndex = +stepResultUpdateSize;
                processedSteps = 0;
                testCaseStepsResult = new ArrayList<>();
                testCaseResult.setCurrentIndex(currentIndex);
            }
        }
        testCaseResult.setTestCaseStepResults(testCaseStepsResult);
    } catch (Exception e) {
        log.error(e.getMessage(), e);
    }
    testCaseResult.setResult(ObjectUtils.defaultIfNull(testCaseResult.getResult(), result));
    testCaseResult.setEndTime(new Timestamp(System.currentTimeMillis()));
    if ((testCaseResult.getResult() == ResultConstant.SUCCESS) && (result == ResultConstant.SUCCESS)) {
        testCaseResult.setMessage(AutomatorMessages.MSG_TEST_CASE_SUCCESS);
    } else if (StringUtils.isBlank(testCaseResult.getMessage())) {
        testCaseResult.setMessage(AutomatorMessages.MSG_TEST_CASE_FAILURE);
    }
    // check for browser closed condition
    if (testCaseResult.getResult() != ResultConstant.SUCCESS) {
        if (ErrorCodes.BROWSER_CLOSED.equals(testCaseResult.getErrorCode())) {
            DriverManager.getDriverManager().setRestartDriverSession(Boolean.TRUE);
        }
    }
    if (testDeviceEntity.getCreateSessionAtCaseLevel()) {
        endSession();
    }
    resetThreadContextData();
    return testCaseResult;
}
Also used : DriverSessionType(com.testsigma.automator.constants.DriverSessionType) AutomatorException(com.testsigma.automator.exceptions.AutomatorException) ThreadContext(org.apache.logging.log4j.ThreadContext) java.util(java.util) Timestamp(java.sql.Timestamp) AutomatorMessages(com.testsigma.automator.constants.AutomatorMessages) ScreenshotUploadTask(com.testsigma.automator.utilities.ScreenshotUploadTask) ScreenCaptureUtil(com.testsigma.automator.utilities.ScreenCaptureUtil) StringUtils(org.apache.commons.lang3.StringUtils) ObjectMapperService(com.testsigma.automator.service.ObjectMapperService) UploadThreadPool(com.testsigma.automator.utilities.UploadThreadPool) ObjectUtils(org.apache.commons.lang3.ObjectUtils) com.testsigma.automator.entity(com.testsigma.automator.entity) Log4j2(lombok.extern.log4j.Log4j2) AutomatorConfig(com.testsigma.automator.AutomatorConfig) ErrorCodes(com.testsigma.automator.constants.ErrorCodes) DriverManager(com.testsigma.automator.drivers.DriverManager) ScreenCaptureUtil(com.testsigma.automator.utilities.ScreenCaptureUtil) ObjectMapperService(com.testsigma.automator.service.ObjectMapperService) ScreenshotUploadTask(com.testsigma.automator.utilities.ScreenshotUploadTask) Timestamp(java.sql.Timestamp) AutomatorException(com.testsigma.automator.exceptions.AutomatorException)

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