Search in sources :

Example 1 with AssertionVerifier

use of io.irontest.core.assertion.AssertionVerifier in project irontest by zheng-wang.

the class AssertionResource method verify.

/**
 * This is a stateless operation, i.e. not persisting anything in database.
 * @param assertionVerificationRequest
 * @return
 */
@POST
@Path("assertions/{assertionId}/verify")
@PermitAll
public AssertionVerificationResult verify(AssertionVerificationRequest assertionVerificationRequest) throws IOException {
    Assertion assertion = assertionVerificationRequest.getAssertion();
    // populate xsd file bytes for JSONValidAgainstJSONSchema or XMLValidAgainstXSD assertion which are not passed from UI to this method
    if (Assertion.TYPE_JSON_VALID_AGAINST_JSON_SCHEMA.equals(assertion.getType()) || Assertion.TYPE_XML_VALID_AGAINST_XSD.equals(assertion.getType())) {
        assertion.setOtherProperties(assertionDAO.findById(assertion.getId()).getOtherProperties());
    }
    // gather referenceable string properties
    long testcaseId = teststepDAO.findTestcaseIdById(assertion.getTeststepId());
    List<UserDefinedProperty> testcaseUDPs = udpDAO.findByTestcaseId(testcaseId);
    Map<String, String> referenceableStringProperties = IronTestUtils.udpListToMap(testcaseUDPs);
    Set<String> udpNames = referenceableStringProperties.keySet();
    DataTable dataTable = dataTableDAO.getTestcaseDataTable(testcaseId, true);
    if (dataTable.getRows().size() > 0) {
        IronTestUtils.checkDuplicatePropertyNameBetweenDataTableAndUPDs(udpNames, dataTable);
        referenceableStringProperties.putAll(dataTable.getStringPropertiesInRow(0));
    }
    AssertionVerifier assertionVerifier = AssertionVerifierFactory.getInstance().create(assertion, referenceableStringProperties);
    Object assertionInput = assertionVerificationRequest.getInput();
    if (Assertion.TYPE_HAS_AN_MQRFH2_FOLDER_EQUAL_TO_XML.equals(assertion.getType())) {
        assertionInput = new ObjectMapper().convertValue(assertionInput, MQRFH2Header.class);
    }
    AssertionVerificationResult result;
    try {
        result = assertionVerifier.verify(assertionInput);
    } catch (Exception e) {
        LOGGER.error("Failed to verify assertion", e);
        result = new AssertionVerificationResult();
        result.setResult(TestResult.FAILED);
        result.setError(e.getMessage());
    }
    return result;
}
Also used : AssertionVerifier(io.irontest.core.assertion.AssertionVerifier) DataTable(io.irontest.models.DataTable) MQRFH2Header(io.irontest.models.teststep.MQRFH2Header) UserDefinedProperty(io.irontest.models.UserDefinedProperty) IOException(java.io.IOException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) PermitAll(javax.annotation.security.PermitAll)

Example 2 with AssertionVerifier

use of io.irontest.core.assertion.AssertionVerifier in project irontest by zheng-wang.

the class TestcaseRunner method runTeststep.

protected TeststepRun runTeststep(Teststep teststep) {
    TeststepRun teststepRun = new TeststepRun();
    teststepRun.setTeststep(teststep);
    // test step run starts
    Date teststepRunStartTime = new Date();
    teststepRun.setStartTime(teststepRunStartTime);
    referenceableStringProperties.put(IMPLICIT_PROPERTY_NAME_TEST_STEP_START_TIME, IMPLICIT_PROPERTY_DATE_TIME_FORMAT.format(teststepRunStartTime));
    LOGGER.info("Start running test step: " + teststep.getName());
    // run test step
    BasicTeststepRun basicTeststepRun;
    // use this flag instead of checking stepRun.getErrorMessage() != null, for code clarity
    boolean exceptionOccurred = false;
    try {
        basicTeststepRun = TeststepRunnerFactory.getInstance().newTeststepRunner(teststep, teststepDAO, utilsDAO, referenceableStringProperties, referenceableEndpointProperties, testcaseRunContext).run();
        LOGGER.info("Finish running test step: " + teststep.getName());
        teststepRun.setResponse(basicTeststepRun.getResponse());
        teststepRun.setInfoMessage(basicTeststepRun.getInfoMessage());
    } catch (Exception e) {
        exceptionOccurred = true;
        String message = e.getMessage();
        // exception message could be null (though rarely)
        teststepRun.setErrorMessage(message == null ? "null" : message);
        LOGGER.error(message, e);
    }
    // verify assertions
    if (exceptionOccurred) {
        teststepRun.setResult(TestResult.FAILED);
    } else {
        teststepRun.setResult(TestResult.PASSED);
        // get input for assertion verifications
        Object apiResponse = teststepRun.getResponse();
        Object assertionVerificationInput;
        if (Teststep.TYPE_SOAP.equals(teststep.getType())) {
            assertionVerificationInput = ((SOAPAPIResponse) apiResponse).getHttpBody();
        } else if (Teststep.TYPE_DB.equals(teststep.getType())) {
            assertionVerificationInput = ((DBAPIResponse) apiResponse).getRowsJSON();
        } else if (Teststep.TYPE_MQ.equals(teststep.getType())) {
            assertionVerificationInput = ((MQAPIResponse) apiResponse).getValue();
        } else {
            assertionVerificationInput = apiResponse;
        }
        if (Teststep.TYPE_DB.equals(teststep.getType()) && assertionVerificationInput == null) {
        // SQL inserts/deletes/updates, no assertion verification needed
        } else {
            // verify assertions against the input
            for (Assertion assertion : teststep.getAssertions()) {
                AssertionVerification verification = new AssertionVerification();
                teststepRun.getAssertionVerifications().add(verification);
                verification.setAssertion(assertion);
                AssertionVerifier verifier = AssertionVerifierFactory.getInstance().create(assertion.getType(), referenceableStringProperties);
                AssertionVerificationResult verificationResult;
                try {
                    verificationResult = verifier.verify(assertion, assertionVerificationInput);
                } catch (Exception e) {
                    LOGGER.error("Failed to verify assertion", e);
                    verificationResult = new AssertionVerificationResult();
                    verificationResult.setResult(TestResult.FAILED);
                    String message = e.getMessage();
                    // exception message could be null (though rarely)
                    verificationResult.setError(message == null ? "null" : message);
                }
                verification.setVerificationResult(verificationResult);
                if (TestResult.FAILED == verificationResult.getResult()) {
                    teststepRun.setResult(TestResult.FAILED);
                }
            }
        }
    }
    // test step run ends
    teststepRun.setDuration(new Date().getTime() - teststepRun.getStartTime().getTime());
    return teststepRun;
}
Also used : AssertionVerifier(io.irontest.core.assertion.AssertionVerifier) TeststepRun(io.irontest.models.testrun.TeststepRun) AssertionVerificationResult(io.irontest.models.assertion.AssertionVerificationResult) Assertion(io.irontest.models.assertion.Assertion) AssertionVerification(io.irontest.models.assertion.AssertionVerification) Date(java.util.Date) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 3 with AssertionVerifier

use of io.irontest.core.assertion.AssertionVerifier in project irontest by zheng-wang.

the class TestcaseRunner method verifyAssertions.

/**
 * Verify assertions against the API response.
 * @param teststepType
 * @param teststepAction
 * @param assertions
 * @param apiResponse
 * @param teststepRun
 */
private void verifyAssertions(String teststepType, String teststepAction, List<Assertion> assertions, Object apiResponse, TeststepRun teststepRun) throws IOException {
    for (Assertion assertion : assertions) {
        Object assertionVerificationInput = resolveAssertionVerificationInputFromAPIResponse(teststepType, teststepAction, assertion.getType(), apiResponse);
        // resolve assertion verification input2 if applicable
        Object assertionVerificationInput2 = null;
        if (Assertion.TYPE_HTTP_STUB_HIT.equals(assertion.getType())) {
            HTTPStubHitAssertionProperties otherProperties = (HTTPStubHitAssertionProperties) assertion.getOtherProperties();
            assertionVerificationInput2 = getTestcaseRunContext().getHttpStubMappingInstanceIds().get(otherProperties.getStubNumber());
        }
        AssertionVerification verification = new AssertionVerification();
        teststepRun.getAssertionVerifications().add(verification);
        verification.setAssertion(assertion);
        AssertionVerifier verifier = AssertionVerifierFactory.getInstance().create(assertion, referenceableStringProperties);
        AssertionVerificationResult verificationResult;
        try {
            verificationResult = verifier.verify(assertionVerificationInput, assertionVerificationInput2);
        } catch (Exception e) {
            LOGGER.error("Failed to verify assertion", e);
            verificationResult = new AssertionVerificationResult();
            verificationResult.setResult(TestResult.FAILED);
            String message = e.getMessage();
            // exception message could be null (though rarely)
            verificationResult.setError(message == null ? "null" : message);
        }
        verification.setVerificationResult(verificationResult);
        if (TestResult.FAILED == verificationResult.getResult()) {
            teststepRun.setResult(TestResult.FAILED);
        }
    }
}
Also used : AssertionVerifier(io.irontest.core.assertion.AssertionVerifier) IOException(java.io.IOException)

Aggregations

AssertionVerifier (io.irontest.core.assertion.AssertionVerifier)3 IOException (java.io.IOException)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 DataTable (io.irontest.models.DataTable)1 UserDefinedProperty (io.irontest.models.UserDefinedProperty)1 Assertion (io.irontest.models.assertion.Assertion)1 AssertionVerification (io.irontest.models.assertion.AssertionVerification)1 AssertionVerificationResult (io.irontest.models.assertion.AssertionVerificationResult)1 TeststepRun (io.irontest.models.testrun.TeststepRun)1 MQRFH2Header (io.irontest.models.teststep.MQRFH2Header)1 Date (java.util.Date)1 PermitAll (javax.annotation.security.PermitAll)1