Search in sources :

Example 1 with AssertionVerification

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

the class TeststepRunMapper method map.

public TeststepRun map(int index, ResultSet rs, StatementContext ctx) throws SQLException {
    TeststepRun teststepRun = new TeststepRun();
    ObjectMapper objectMapper = new ObjectMapper();
    teststepRun.setStartTime(rs.getTimestamp("starttime"));
    teststepRun.setDuration(rs.getLong("duration"));
    teststepRun.setResult(TestResult.getByText(rs.getString("result")));
    Teststep teststep = null;
    try {
        teststep = objectMapper.readValue(rs.getString("teststep"), Teststep.class);
    } catch (IOException e) {
        throw new SQLException("Failed to deserialize teststep JSON.", e);
    }
    teststepRun.setTeststep(teststep);
    // Use LinkedHashMap here instead of Object (for covering specific response type like DBAPIResponse),
    // because TeststepRun is used for displaying report, so JSON representation of the response is sufficient.
    LinkedHashMap response = null;
    try {
        response = objectMapper.readValue(rs.getString("response"), LinkedHashMap.class);
    } catch (IOException e) {
        throw new SQLException("Failed to deserialize response JSON.", e);
    }
    teststepRun.setResponse(response);
    teststepRun.setInfoMessage(rs.getString("info_message"));
    teststepRun.setErrorMessage(rs.getString("error_message"));
    List<AssertionVerification> assertionVerifications = null;
    try {
        assertionVerifications = new ObjectMapper().readValue(rs.getString("assertion_verifications"), new TypeReference<List<AssertionVerification>>() {
        });
    } catch (IOException e) {
        throw new SQLException("Failed to deserialize stepruns JSON.", e);
    }
    teststepRun.setAssertionVerifications(assertionVerifications);
    return teststepRun;
}
Also used : Teststep(io.irontest.models.teststep.Teststep) TeststepRun(io.irontest.models.testrun.TeststepRun) SQLException(java.sql.SQLException) IOException(java.io.IOException) AssertionVerification(io.irontest.models.assertion.AssertionVerification) TypeReference(com.fasterxml.jackson.core.type.TypeReference) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with AssertionVerification

use of io.irontest.models.assertion.AssertionVerification 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 AssertionVerification

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

the class TeststepRunMapper method map.

public TeststepRun map(ResultSet rs, StatementContext ctx) throws SQLException {
    TeststepRun teststepRun = new TeststepRun();
    ObjectMapper objectMapper = new ObjectMapper();
    teststepRun.setId(rs.getLong("id"));
    teststepRun.setStartTime(rs.getTimestamp("starttime"));
    teststepRun.setDuration(rs.getLong("duration"));
    teststepRun.setResult(TestResult.getByText(rs.getString("result")));
    Teststep teststep;
    try {
        teststep = objectMapper.readValue(rs.getString("teststep"), Teststep.class);
    } catch (IOException e) {
        throw new SQLException("Failed to deserialize teststep JSON.", e);
    }
    teststepRun.setTeststep(teststep);
    APIResponse response;
    try {
        response = objectMapper.readValue(rs.getString("response"), APIResponse.class);
    } catch (IOException e) {
        throw new SQLException("Failed to deserialize response JSON.", e);
    }
    teststepRun.setResponse(response);
    teststepRun.setInfoMessage(rs.getString("info_message"));
    teststepRun.setErrorMessage(rs.getString("error_message"));
    List<AssertionVerification> assertionVerifications;
    try {
        assertionVerifications = new ObjectMapper().readValue(rs.getString("assertion_verifications"), new TypeReference<List<AssertionVerification>>() {
        });
    } catch (IOException e) {
        throw new SQLException("Failed to deserialize stepruns JSON.", e);
    }
    teststepRun.setAssertionVerifications(assertionVerifications);
    return teststepRun;
}
Also used : Teststep(io.irontest.models.teststep.Teststep) TeststepRun(io.irontest.models.testrun.TeststepRun) APIResponse(io.irontest.core.teststep.APIResponse) SQLException(java.sql.SQLException) IOException(java.io.IOException) AssertionVerification(io.irontest.models.assertion.AssertionVerification) TypeReference(com.fasterxml.jackson.core.type.TypeReference) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

AssertionVerification (io.irontest.models.assertion.AssertionVerification)3 TeststepRun (io.irontest.models.testrun.TeststepRun)3 TypeReference (com.fasterxml.jackson.core.type.TypeReference)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 Teststep (io.irontest.models.teststep.Teststep)2 IOException (java.io.IOException)2 SQLException (java.sql.SQLException)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 AssertionVerifier (io.irontest.core.assertion.AssertionVerifier)1 APIResponse (io.irontest.core.teststep.APIResponse)1 Assertion (io.irontest.models.assertion.Assertion)1 AssertionVerificationResult (io.irontest.models.assertion.AssertionVerificationResult)1 Date (java.util.Date)1 LinkedHashMap (java.util.LinkedHashMap)1