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;
}
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;
}
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;
}
Aggregations