Search in sources :

Example 1 with AssertionVerificationResult

use of io.irontest.models.assertion.AssertionVerificationResult in project irontest by zheng-wang.

the class ContainsAssertionVerifier method _verify.

/**
 * @param assertion
 * @param input the String that the assertion is verified against
 * @return
 */
@Override
public AssertionVerificationResult _verify(Assertion assertion, Object input) throws Exception {
    AssertionVerificationResult result = new AssertionVerificationResult();
    ContainsAssertionProperties otherProperties = (ContainsAssertionProperties) assertion.getOtherProperties();
    // validate other properties
    if ("".equals(StringUtils.trimToEmpty(otherProperties.getContains()))) {
        throw new IllegalArgumentException("Contains not specified");
    }
    String inputStr = (String) input;
    result.setResult(inputStr.contains(otherProperties.getContains()) ? TestResult.PASSED : TestResult.FAILED);
    return result;
}
Also used : AssertionVerificationResult(io.irontest.models.assertion.AssertionVerificationResult) ContainsAssertionProperties(io.irontest.models.assertion.ContainsAssertionProperties)

Example 2 with AssertionVerificationResult

use of io.irontest.models.assertion.AssertionVerificationResult 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) {
    Assertion assertion = assertionVerificationRequest.getAssertion();
    // gather referenceable string properties
    List<UserDefinedProperty> testcaseUDPs = udpDAO.findTestcaseUDPsByTeststepId(assertion.getTeststepId());
    Map<String, String> referenceableStringProperties = IronTestUtils.udpListToMap(testcaseUDPs);
    DataTable dataTable = utilsDAO.getTestcaseDataTable(teststepDAO.findTestcaseIdById(assertion.getTeststepId()), true);
    if (dataTable.getRows().size() == 1) {
        referenceableStringProperties.putAll(dataTable.getStringPropertiesInRow(0));
    }
    AssertionVerifier assertionVerifier = AssertionVerifierFactory.getInstance().create(assertion.getType(), referenceableStringProperties);
    AssertionVerificationResult result;
    try {
        result = assertionVerifier.verify(assertion, assertionVerificationRequest.getInput());
    } 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) UserDefinedProperty(io.irontest.models.UserDefinedProperty) AssertionVerificationResult(io.irontest.models.assertion.AssertionVerificationResult) Assertion(io.irontest.models.assertion.Assertion) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) PermitAll(javax.annotation.security.PermitAll)

Example 3 with AssertionVerificationResult

use of io.irontest.models.assertion.AssertionVerificationResult 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 4 with AssertionVerificationResult

use of io.irontest.models.assertion.AssertionVerificationResult in project irontest by zheng-wang.

the class IntegerEqualAssertionVerifier method _verify.

/**
 * @param assertion
 * @param input the Integer that the assertion is verified against
 * @return
 */
@Override
public AssertionVerificationResult _verify(Assertion assertion, Object input) throws Exception {
    AssertionVerificationResult result = new AssertionVerificationResult();
    IntegerEqualAssertionProperties properties = (IntegerEqualAssertionProperties) assertion.getOtherProperties();
    result.setResult(new Integer(properties.getNumber()).equals(input) ? TestResult.PASSED : TestResult.FAILED);
    return result;
}
Also used : AssertionVerificationResult(io.irontest.models.assertion.AssertionVerificationResult) IntegerEqualAssertionProperties(io.irontest.models.assertion.IntegerEqualAssertionProperties)

Aggregations

AssertionVerificationResult (io.irontest.models.assertion.AssertionVerificationResult)4 AssertionVerifier (io.irontest.core.assertion.AssertionVerifier)2 Assertion (io.irontest.models.assertion.Assertion)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 DataTable (io.irontest.models.DataTable)1 UserDefinedProperty (io.irontest.models.UserDefinedProperty)1 AssertionVerification (io.irontest.models.assertion.AssertionVerification)1 ContainsAssertionProperties (io.irontest.models.assertion.ContainsAssertionProperties)1 IntegerEqualAssertionProperties (io.irontest.models.assertion.IntegerEqualAssertionProperties)1 TeststepRun (io.irontest.models.testrun.TeststepRun)1 Date (java.util.Date)1 PermitAll (javax.annotation.security.PermitAll)1 POST (javax.ws.rs.POST)1 Path (javax.ws.rs.Path)1