Search in sources :

Example 1 with Assertion

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

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

the class AssertionMapper method map.

public Assertion map(int index, ResultSet rs, StatementContext ctx) throws SQLException {
    Assertion assertion = null;
    String type = rs.getString("type");
    String tempAssertionJSON = "{\"type\":\"" + type + "\",\"otherProperties\":" + rs.getString("other_properties") + "}";
    try {
        assertion = new ObjectMapper().readValue(tempAssertionJSON, Assertion.class);
    } catch (IOException e) {
        throw new SQLException("Failed to deserialize other_properties JSON.", e);
    }
    assertion.setId(rs.getLong("id"));
    assertion.setTeststepId(rs.getLong("teststep_id"));
    assertion.setName(rs.getString("name"));
    assertion.setType(type);
    return assertion;
}
Also used : SQLException(java.sql.SQLException) Assertion(io.irontest.models.assertion.Assertion) IOException(java.io.IOException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 3 with Assertion

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

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

the class AssertionVerifier method verify.

/**
 * This method modifies the content of assertion object.
 * @param assertion the assertion to be verified (against the input)
 * @param input the object that the assertion is verified against
 * @return
 */
public AssertionVerificationResult verify(Assertion assertion, Object input) throws Exception {
    MapValueLookup stringPropertyReferenceResolver = new MapValueLookup(referenceableStringProperties, true);
    // resolve string property references in assertion.name
    String resolvedAssertionName = new StrSubstitutor(stringPropertyReferenceResolver).replace(assertion.getName());
    assertion.setName(resolvedAssertionName);
    Set<String> undefinedStringProperties = stringPropertyReferenceResolver.getUnfoundKeys();
    // resolve string property references in assertion.otherProperties
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
    String assertionOtherPropertiesJSON = objectMapper.writeValueAsString(assertion.getOtherProperties());
    String resolvedAssertionOtherPropertiesJSON = new StrSubstitutor(stringPropertyReferenceResolver).replace(assertionOtherPropertiesJSON);
    undefinedStringProperties.addAll(stringPropertyReferenceResolver.getUnfoundKeys());
    String tempAssertionJSON = "{\"type\":\"" + assertion.getType() + "\",\"otherProperties\":" + resolvedAssertionOtherPropertiesJSON + "}";
    Assertion tempAssertion = objectMapper.readValue(tempAssertionJSON, Assertion.class);
    assertion.setOtherProperties(tempAssertion.getOtherProperties());
    if (!undefinedStringProperties.isEmpty()) {
        throw new RuntimeException("String properties " + undefinedStringProperties + " not defined.");
    }
    return _verify(assertion, input);
}
Also used : StrSubstitutor(org.apache.commons.text.StrSubstitutor) MapValueLookup(io.irontest.core.MapValueLookup) Assertion(io.irontest.models.assertion.Assertion) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 5 with Assertion

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

the class TestcaseDAO method duplicate.

/**
 * Clone the test case and its contents.
 * @param oldTestcaseId id of the test case to be cloned
 * @param targetFolderId id of the folder in which the new test case will be created
 * @return ID of the new test case
 */
@Transaction
public long duplicate(long oldTestcaseId, long targetFolderId) throws JsonProcessingException {
    Testcase oldTestcase = findById_Complete_NoTransaction(oldTestcaseId);
    // resolve new test case name
    String newTestcaseName = oldTestcase.getName();
    if (oldTestcase.getParentFolderId() == targetFolderId) {
        int copyIndex = 1;
        newTestcaseName = oldTestcase.getName() + " - Copy";
        while (_nameExistsInFolder(newTestcaseName, targetFolderId)) {
            copyIndex++;
            newTestcaseName = oldTestcase.getName() + " - Copy (" + copyIndex + ")";
        }
    }
    // duplicate the test case record
    Testcase newTestcase = new Testcase();
    newTestcase.setName(newTestcaseName);
    newTestcase.setDescription(oldTestcase.getDescription());
    newTestcase.setParentFolderId(targetFolderId);
    newTestcase = insert_NoTransaction(newTestcase);
    // duplicate user defined properties
    udpDAO().duplicateByTestcase(oldTestcaseId, newTestcase.getId());
    // duplicate test steps
    for (Teststep oldTeststep : oldTestcase.getTeststeps()) {
        Teststep newTeststep = new Teststep();
        newTeststep.setName(oldTeststep.getName());
        newTeststep.setTestcaseId(newTestcase.getId());
        newTeststep.setSequence(oldTeststep.getSequence());
        newTeststep.setType(oldTeststep.getType());
        newTeststep.setDescription(oldTeststep.getDescription());
        newTeststep.setAction(oldTeststep.getAction());
        if (oldTeststep.getRequestType() == TeststepRequestType.TEXT) {
            newTeststep.setRequest(oldTeststep.getRequest());
        } else {
            newTeststep.setRequest(teststepDAO().getBinaryRequestById(oldTeststep.getId()));
        }
        newTeststep.setRequestType(oldTeststep.getRequestType());
        newTeststep.setRequestFilename(oldTeststep.getRequestFilename());
        newTeststep.setOtherProperties(oldTeststep.getOtherProperties());
        Endpoint oldEndpoint = oldTeststep.getEndpoint();
        if (oldEndpoint != null) {
            Endpoint newEndpoint = new Endpoint();
            newTeststep.setEndpoint(newEndpoint);
            if (oldEndpoint.isManaged()) {
                newEndpoint.setId(oldEndpoint.getId());
            } else {
                newEndpoint.setName(oldEndpoint.getName());
                newEndpoint.setType(oldEndpoint.getType());
                newEndpoint.setDescription(oldEndpoint.getDescription());
                newEndpoint.setUrl(oldEndpoint.getUrl());
                newEndpoint.setUsername(oldEndpoint.getUsername());
                newEndpoint.setPassword(oldEndpoint.getPassword());
                newEndpoint.setOtherProperties(oldEndpoint.getOtherProperties());
            }
        }
        long newTeststepId = teststepDAO().insert_NoTransaction(newTeststep, null);
        // duplicate assertions
        for (Assertion oldAssertion : oldTeststep.getAssertions()) {
            Assertion newAssertion = new Assertion();
            newAssertion.setTeststepId(newTeststepId);
            newAssertion.setName(oldAssertion.getName());
            newAssertion.setType(oldAssertion.getType());
            newAssertion.setOtherProperties(oldAssertion.getOtherProperties());
            assertionDAO().insert_NoTransaction(newAssertion);
        }
    }
    return newTestcase.getId();
}
Also used : Teststep(io.irontest.models.teststep.Teststep) Testcase(io.irontest.models.Testcase) Endpoint(io.irontest.models.endpoint.Endpoint) Assertion(io.irontest.models.assertion.Assertion) Endpoint(io.irontest.models.endpoint.Endpoint)

Aggregations

Assertion (io.irontest.models.assertion.Assertion)7 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 AssertionVerifier (io.irontest.core.assertion.AssertionVerifier)2 AssertionVerificationResult (io.irontest.models.assertion.AssertionVerificationResult)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 MQTeststepActionDataBackup (io.irontest.core.MQTeststepActionDataBackup)1 MapValueLookup (io.irontest.core.MapValueLookup)1 DataTable (io.irontest.models.DataTable)1 Testcase (io.irontest.models.Testcase)1 UserDefinedProperty (io.irontest.models.UserDefinedProperty)1 AssertionVerification (io.irontest.models.assertion.AssertionVerification)1 IntegerEqualAssertionProperties (io.irontest.models.assertion.IntegerEqualAssertionProperties)1 Endpoint (io.irontest.models.endpoint.Endpoint)1 TeststepRun (io.irontest.models.testrun.TeststepRun)1 Teststep (io.irontest.models.teststep.Teststep)1 IOException (java.io.IOException)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 PermitAll (javax.annotation.security.PermitAll)1