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