Search in sources :

Example 1 with TestCaseAssertionSummary

use of org.apache.synapse.unittest.testcase.data.classes.TestCaseAssertionSummary in project wso2-synapse by wso2.

the class Assertor method startAssertNotNullsForServices.

/**
 * Method of assertNotNull for Service test cases.
 *
 * @param assertNotNull array of assertNotNull
 * @param response     service's http response
 * @param testCaseSummary testSummary object for this test case
 */
private static void startAssertNotNullsForServices(List<AssertNotNull> assertNotNull, Map.Entry<String, HttpResponse> response, TestCaseSummary testCaseSummary) {
    log.info("Assert Not Null - assert property for services started");
    for (AssertNotNull assertItem : assertNotNull) {
        TestCaseAssertionSummary testAssertion = new TestCaseAssertionSummary();
        Assertor.AssertionPrerequisite.setAssertNotNullPrerequisite(assertItem);
        boolean isAssertNull = true;
        HttpResponse serviceResponse = response.getValue();
        String mediatedResult = Constants.STRING_NULL;
        switch(Assertor.AssertionPrerequisite.getExpressionPrefix()) {
            case INPUT_PROPERTY_BODY:
                isAssertNull = serviceResponse.getEntity() == null;
                break;
            case RESPONSE_PROPERTY_STATUS_CODE:
                isAssertNull = serviceResponse.getStatusLine() == null;
                break;
            case RESPONSE_PROPERTY_HTTP_VERSION:
                if (serviceResponse.getStatusLine() != null) {
                    isAssertNull = serviceResponse.getStatusLine().getProtocolVersion() == null;
                }
                break;
            case INPUT_PROPERTY_TRANSPORT:
                Header[] responseHeaders = serviceResponse.getAllHeaders();
                if (responseHeaders != null) {
                    for (Header header : responseHeaders) {
                        if (header.getName().equals(Assertor.AssertionPrerequisite.getExpressionProperty())) {
                            isAssertNull = header.getValue() == null;
                            break;
                        }
                    }
                }
                break;
            default:
                mediatedResult = "Received assert expression: " + Assertor.AssertionPrerequisite.getAssertExpression() + " is not a valid operation type for services";
                log.error(mediatedResult);
        }
        log.info("Service Assert Actual - " + Assertor.AssertionPrerequisite.getAssertExpression());
        log.info("Service mediated result for assertNotNull is - " + !isAssertNull);
        if (!isAssertNull) {
            log.info("Service assertNotNull for " + Assertor.AssertionPrerequisite.getAssertExpression() + " expression passed successfully");
        } else {
            testAssertion.setAssertionType(Constants.TEST_CASE_ASSERTION_NOTNULL);
            testAssertion.setAssertionExpression(Assertor.AssertionPrerequisite.getAssertExpression());
            testAssertion.setAssertionActualValue(mediatedResult);
            testAssertion.setAssertionDescription("Tested service url - " + response.getKey() + "\nRecieved status code - " + (serviceResponse.getStatusLine() != null ? serviceResponse.getStatusLine().getStatusCode() : "null"));
            testAssertion.setAssertionErrorMessage(Assertor.AssertionPrerequisite.getMessage());
            testCaseSummary.addTestCaseAssertion(testAssertion);
            log.error("Service assertNotNull for " + Assertor.AssertionPrerequisite.getAssertExpression() + " expression failed with a message - " + Assertor.AssertionPrerequisite.getMessage());
        }
    }
}
Also used : Header(org.apache.http.Header) AssertNotNull(org.apache.synapse.unittest.testcase.data.classes.AssertNotNull) TestCaseAssertionSummary(org.apache.synapse.unittest.testcase.data.classes.TestCaseAssertionSummary) HttpResponse(org.apache.http.HttpResponse)

Example 2 with TestCaseAssertionSummary

use of org.apache.synapse.unittest.testcase.data.classes.TestCaseAssertionSummary in project wso2-synapse by wso2.

the class Assertor method startAssertNotNullsForSequence.

/**
 * Method of assertionNotNull for Sequence test cases.
 *
 * @param assertNotNull  array of assertNotNull
 * @param messageContext message context
 * @param testCaseSummary testSummary object for this test case
 */
private static void startAssertNotNullsForSequence(List<AssertNotNull> assertNotNull, MessageContext messageContext, TestCaseSummary testCaseSummary) {
    log.info("Assert Not Null - assert property for sequences started");
    for (AssertNotNull assertItem : assertNotNull) {
        TestCaseAssertionSummary testAssertion = new TestCaseAssertionSummary();
        Assertor.AssertionPrerequisite.setAssertNotNullPrerequisite(assertItem);
        boolean isAssertNull = true;
        String mediatedResult = Constants.STRING_NULL;
        Axis2MessageContext axis2MessageContext = (Axis2MessageContext) messageContext;
        org.apache.axis2.context.MessageContext axis2MessageCtx = axis2MessageContext.getAxis2MessageContext();
        switch(Assertor.AssertionPrerequisite.getExpressionPrefix()) {
            case INPUT_PROPERTY_BODY:
                isAssertNull = messageContext.getEnvelope().getBody().getFirstElement() == null;
                break;
            case INPUT_PROPERTY_CONTEXT:
                isAssertNull = messageContext.getProperty(Assertor.AssertionPrerequisite.getExpressionProperty()) == null;
                break;
            case INPUT_PROPERTY_AXIS2:
                isAssertNull = axis2MessageCtx.getProperty(Assertor.AssertionPrerequisite.getExpressionProperty()) == null;
                break;
            case INPUT_PROPERTY_TRANSPORT:
                Object headers = axis2MessageCtx.getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);
                @SuppressWarnings("unchecked") Map<String, Object> headersMap = (Map) headers;
                isAssertNull = headersMap.get(Assertor.AssertionPrerequisite.getExpressionProperty()) == null;
                break;
            default:
                mediatedResult = "Received assert expression: " + Assertor.AssertionPrerequisite.getAssertExpression() + " is not a valid operation type for sequences";
                log.error(mediatedResult);
        }
        log.info("Sequence Assertion Expression - " + Assertor.AssertionPrerequisite.getAssertExpression());
        log.info("Sequence mediated result for assertNotNull is " + !isAssertNull);
        if (!isAssertNull) {
            log.info("Sequence assertNotNull for " + Assertor.AssertionPrerequisite.getAssertExpression() + " expression passed successfully");
        } else {
            testAssertion.setAssertionType(Constants.TEST_CASE_ASSERTION_NOTNULL);
            testAssertion.setAssertionExpression(Assertor.AssertionPrerequisite.getAssertExpression());
            testAssertion.setAssertionActualValue(mediatedResult);
            testAssertion.setAssertionErrorMessage(Assertor.AssertionPrerequisite.getMessage());
            testCaseSummary.addTestCaseAssertion(testAssertion);
            log.error("Sequence assertNotNull for " + Assertor.AssertionPrerequisite.getAssertExpression() + " expression failed with a message - " + Assertor.AssertionPrerequisite.getMessage());
        }
    }
}
Also used : AssertNotNull(org.apache.synapse.unittest.testcase.data.classes.AssertNotNull) TestCaseAssertionSummary(org.apache.synapse.unittest.testcase.data.classes.TestCaseAssertionSummary) Map(java.util.Map) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext)

Example 3 with TestCaseAssertionSummary

use of org.apache.synapse.unittest.testcase.data.classes.TestCaseAssertionSummary in project wso2-synapse by wso2.

the class Assertor method startAssertEqualsForServices.

/**
 * Method of assertionEquals for Service test cases.
 *
 * @param assertEquals array of assertEquals
 * @param response     service's http response
 * @param testCaseSummary testSummary object for this test case
 * @throws IOException when converting http response into a string
 */
private static void startAssertEqualsForServices(List<AssertEqual> assertEquals, Map.Entry<String, HttpResponse> response, TestCaseSummary testCaseSummary) throws IOException {
    log.info("Assert Equals - assert property for services started");
    for (AssertEqual assertItem : assertEquals) {
        TestCaseAssertionSummary testAssertion = new TestCaseAssertionSummary();
        Assertor.AssertionPrerequisite.setAssertEqualPrerequisite(assertItem);
        String mediatedResult = Constants.STRING_NULL;
        HttpResponse serviceResponse = response.getValue();
        HttpEntity responseEntity = serviceResponse.getEntity();
        boolean isAssert = false;
        switch(Assertor.AssertionPrerequisite.getExpressionPrefix()) {
            case INPUT_PROPERTY_BODY:
                if (responseEntity != null) {
                    mediatedResult = RequestProcessor.trimStrings(EntityUtils.toString(responseEntity, Constants.STRING_UTF8));
                    isAssert = Assertor.AssertionPrerequisite.getExpected().equals(mediatedResult);
                }
                break;
            case RESPONSE_PROPERTY_STATUS_CODE:
                if (serviceResponse.getStatusLine() != null) {
                    mediatedResult = Integer.toString(serviceResponse.getStatusLine().getStatusCode());
                    isAssert = Assertor.AssertionPrerequisite.getExpected().equals(mediatedResult);
                }
                break;
            case RESPONSE_PROPERTY_HTTP_VERSION:
                if (serviceResponse.getStatusLine().getProtocolVersion() != null) {
                    mediatedResult = serviceResponse.getStatusLine().getProtocolVersion().toString();
                    isAssert = Assertor.AssertionPrerequisite.getExpected().equals(mediatedResult);
                }
                break;
            case INPUT_PROPERTY_TRANSPORT:
                Header[] responseHeaders = serviceResponse.getAllHeaders();
                if (responseHeaders == null) {
                    break;
                }
                for (Header header : responseHeaders) {
                    if (header.getName() != null && header.getName().equals(Assertor.AssertionPrerequisite.getExpressionProperty())) {
                        if (header.getValue() != null) {
                            mediatedResult = RequestProcessor.trimStrings(header.getValue());
                            isAssert = Assertor.AssertionPrerequisite.getExpected().equals(mediatedResult);
                        }
                        break;
                    }
                }
                break;
            default:
                mediatedResult = "Received assert expression: " + Assertor.AssertionPrerequisite.getAssertExpression() + " is not a valid operation type for services";
                log.error(mediatedResult);
        }
        log.info("Service Assert Expression - " + Assertor.AssertionPrerequisite.getAssertExpression());
        log.info("Service mediated result for Actual - " + mediatedResult);
        log.info("Service Assert Expected - " + Assertor.AssertionPrerequisite.getExpected());
        if (isAssert) {
            log.info("Service assertEquals for " + Assertor.AssertionPrerequisite.getAssertExpression() + " expression passed successfully");
        } else {
            testAssertion.setAssertionType(Constants.TEST_CASE_ASSERTION_EQUALS);
            testAssertion.setAssertionExpression(Assertor.AssertionPrerequisite.getAssertExpression());
            testAssertion.setAssertionExpectedValue(Assertor.AssertionPrerequisite.getExpected());
            testAssertion.setAssertionActualValue(mediatedResult);
            if (mediatedResult.equals(Constants.STRING_NULL)) {
                testAssertion.setAssertionDescription("Tested service url - " + response.getKey() + "\nReceived status code - " + (serviceResponse.getStatusLine() != null ? serviceResponse.getStatusLine().getStatusCode() : "null"));
            }
            testAssertion.setAssertionErrorMessage(Assertor.AssertionPrerequisite.getMessage());
            testCaseSummary.addTestCaseAssertion(testAssertion);
            log.error("Service assertEquals for " + Assertor.AssertionPrerequisite.getAssertExpression() + " expression failed with a message - " + Assertor.AssertionPrerequisite.getMessage());
        }
    }
}
Also used : AssertEqual(org.apache.synapse.unittest.testcase.data.classes.AssertEqual) HttpEntity(org.apache.http.HttpEntity) Header(org.apache.http.Header) TestCaseAssertionSummary(org.apache.synapse.unittest.testcase.data.classes.TestCaseAssertionSummary) HttpResponse(org.apache.http.HttpResponse)

Example 4 with TestCaseAssertionSummary

use of org.apache.synapse.unittest.testcase.data.classes.TestCaseAssertionSummary in project wso2-synapse by wso2.

the class Assertor method startAssertEqualsForSequence.

/**
 * Method of assertionEquals for Sequence test cases.
 *
 * @param assertEquals   array of assertEquals
 * @param messageContext message context
 * @param testCaseSummary  testSummary object for this test case
 */
private static void startAssertEqualsForSequence(List<AssertEqual> assertEquals, MessageContext messageContext, TestCaseSummary testCaseSummary) {
    log.info("AssertEquals - assert property for sequences started");
    for (AssertEqual assertItem : assertEquals) {
        TestCaseAssertionSummary testAssertion = new TestCaseAssertionSummary();
        Assertor.AssertionPrerequisite.setAssertEqualPrerequisite(assertItem);
        String mediatedResult = Constants.STRING_NULL;
        boolean isAssert = false;
        Axis2MessageContext axis2MessageContext = (Axis2MessageContext) messageContext;
        org.apache.axis2.context.MessageContext axis2MessageCtx = axis2MessageContext.getAxis2MessageContext();
        switch(Assertor.AssertionPrerequisite.getExpressionPrefix()) {
            case INPUT_PROPERTY_BODY:
                try {
                    org.apache.axis2.context.MessageContext axis2MsgContxt = ((Axis2MessageContext) messageContext).getAxis2MessageContext();
                    if (JsonUtil.hasAJsonPayload(axis2MsgContxt)) {
                        if (JsonUtil.getJsonPayload(axis2MsgContxt) != null) {
                            mediatedResult = RequestProcessor.trimStrings(IOUtils.toString(JsonUtil.getJsonPayload(axis2MsgContxt)));
                        } else {
                            mediatedResult = EMPTY_VALUE;
                        }
                    } else {
                        String omElement = messageContext.getEnvelope().getBody().getFirstElement().toString();
                        if (omElement.contains(TEXT_NAMESPACE)) {
                            OMElement omElementOfText = AXIOMUtil.stringToOM(omElement);
                            mediatedResult = RequestProcessor.trimStrings(omElementOfText.getText());
                        } else {
                            mediatedResult = RequestProcessor.trimStrings(omElement);
                        }
                    }
                } catch (XMLStreamException e) {
                    mediatedResult = EMPTY_VALUE;
                    log.error("Exception while reading the text output from the message context", e);
                } catch (IOException e) {
                    mediatedResult = EMPTY_VALUE;
                    log.error("Exception while reading the JSON output from the message context", e);
                }
                isAssert = Assertor.AssertionPrerequisite.getExpected().equals(mediatedResult);
                break;
            case INPUT_PROPERTY_CONTEXT:
                if (messageContext.getProperty(Assertor.AssertionPrerequisite.getExpressionProperty()) != null) {
                    mediatedResult = RequestProcessor.trimStrings(messageContext.getProperty(Assertor.AssertionPrerequisite.getExpressionProperty()).toString());
                    isAssert = Assertor.AssertionPrerequisite.getExpected().equals(mediatedResult);
                }
                break;
            case INPUT_PROPERTY_AXIS2:
                if (axis2MessageCtx.getProperty(Assertor.AssertionPrerequisite.getExpressionProperty()) != null) {
                    mediatedResult = RequestProcessor.trimStrings(axis2MessageCtx.getProperty(Assertor.AssertionPrerequisite.getExpressionProperty()).toString());
                    isAssert = Assertor.AssertionPrerequisite.getExpected().equals(mediatedResult);
                }
                break;
            case INPUT_PROPERTY_TRANSPORT:
                Object headers = axis2MessageCtx.getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);
                @SuppressWarnings("unchecked") Map<String, Object> headersMap = (Map) headers;
                if (headersMap.get(Assertor.AssertionPrerequisite.getExpressionProperty()) != null) {
                    mediatedResult = RequestProcessor.trimStrings(headersMap.get(Assertor.AssertionPrerequisite.getExpressionProperty()).toString());
                    isAssert = Assertor.AssertionPrerequisite.getExpected().equals(mediatedResult);
                }
                break;
            default:
                mediatedResult = "Received assert expression: " + Assertor.AssertionPrerequisite.getAssertExpression() + " is not a valid operation type for sequences";
                log.error(mediatedResult);
        }
        log.info("Sequence Assert Expression - " + Assertor.AssertionPrerequisite.getAssertExpression());
        log.info("Sequence mediated result for Actual - " + mediatedResult);
        log.info("Sequence Assert Expected - " + Assertor.AssertionPrerequisite.getExpected());
        if (isAssert) {
            log.info("Sequence assertEqual for " + Assertor.AssertionPrerequisite.getAssertExpression() + " expression passed successfully");
        } else {
            testAssertion.setAssertionType(Constants.TEST_CASE_ASSERTION_EQUALS);
            testAssertion.setAssertionExpression(Assertor.AssertionPrerequisite.getAssertExpression());
            testAssertion.setAssertionExpectedValue(Assertor.AssertionPrerequisite.getExpected());
            testAssertion.setAssertionActualValue(mediatedResult);
            testAssertion.setAssertionErrorMessage(Assertor.AssertionPrerequisite.getMessage());
            testCaseSummary.addTestCaseAssertion(testAssertion);
            log.error("Sequence assertEqual for " + Assertor.AssertionPrerequisite.getAssertExpression() + " expression failed with a message - " + Assertor.AssertionPrerequisite.getMessage());
        }
    }
}
Also used : TestCaseAssertionSummary(org.apache.synapse.unittest.testcase.data.classes.TestCaseAssertionSummary) OMElement(org.apache.axiom.om.OMElement) IOException(java.io.IOException) AssertEqual(org.apache.synapse.unittest.testcase.data.classes.AssertEqual) XMLStreamException(javax.xml.stream.XMLStreamException) Map(java.util.Map) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext)

Example 5 with TestCaseAssertionSummary

use of org.apache.synapse.unittest.testcase.data.classes.TestCaseAssertionSummary in project wso2-synapse by wso2.

the class RequestHandler method createResponseJSON.

/**
 * Create a json response message including all the details of the test suite.
 *
 * @return json message
 */
private JsonObject createResponseJSON(TestSuiteSummary testSummary) {
    JsonObject jsonResponse = new JsonObject();
    jsonResponse.addProperty(Constants.DEPLOYMENT_STATUS, testSummary.getDeploymentStatus());
    jsonResponse.addProperty(Constants.DEPLOYMENT_EXCEPTION, testSummary.getDeploymentException());
    jsonResponse.addProperty(Constants.DEPLOYMENT_DESCRIPTION, testSummary.getDescription());
    jsonResponse.addProperty(Constants.MEDIATION_STATUS, testSummary.getMediationStatus());
    jsonResponse.addProperty(Constants.CURRENT_TESTCASE, testSummary.getRecentTestCaseName());
    jsonResponse.addProperty(Constants.MEDIATION_EXCEPTION, testSummary.getMediationException());
    JsonArray jsonArray = new JsonArray();
    for (TestCaseSummary summary : testSummary.getTestCaseSumamryList()) {
        JsonObject testObject = new JsonObject();
        testObject.addProperty(Constants.TEST_CASE_NAME, summary.getTestCaseName());
        testObject.addProperty(Constants.MEDIATION_STATUS, summary.getMediationStatus());
        testObject.addProperty(Constants.ASSERTION_STATUS, summary.getAssertionStatus());
        testObject.addProperty(Constants.ASSERTION_EXCEPTION, summary.getTestException());
        JsonArray jsonFailedAssertionArray = new JsonArray();
        for (TestCaseAssertionSummary assertionFailure : summary.getTestCaseAssertionList()) {
            JsonObject failedAssertionObject = new JsonObject();
            failedAssertionObject.addProperty(Constants.ASSERTION_TYPE, assertionFailure.getAssertionType());
            failedAssertionObject.addProperty(Constants.ASSERTION_EXPRESSION, assertionFailure.getAssertionExpression());
            failedAssertionObject.addProperty(Constants.ASSERTION_ACTUAL, assertionFailure.getAssertionActualValue());
            failedAssertionObject.addProperty(Constants.ASSERTION_EXPECTED, assertionFailure.getAssertionExpectedValue());
            failedAssertionObject.addProperty(Constants.ASSERTION_DESCRIPTION, assertionFailure.getAssertionDescription());
            failedAssertionObject.addProperty(Constants.ASSERTION_MESSAGE, assertionFailure.getAssertionErrorMessage());
            jsonFailedAssertionArray.add(failedAssertionObject);
        }
        if (jsonFailedAssertionArray.size() > 0) {
            testObject.add(Constants.FAILURE_ASSERTIONS, jsonFailedAssertionArray);
        }
        jsonArray.add(testObject);
    }
    jsonResponse.add("testCases", jsonArray);
    return jsonResponse;
}
Also used : JsonArray(com.google.gson.JsonArray) TestCaseAssertionSummary(org.apache.synapse.unittest.testcase.data.classes.TestCaseAssertionSummary) TestCaseSummary(org.apache.synapse.unittest.testcase.data.classes.TestCaseSummary) JsonObject(com.google.gson.JsonObject)

Aggregations

TestCaseAssertionSummary (org.apache.synapse.unittest.testcase.data.classes.TestCaseAssertionSummary)5 Map (java.util.Map)2 Header (org.apache.http.Header)2 HttpResponse (org.apache.http.HttpResponse)2 Axis2MessageContext (org.apache.synapse.core.axis2.Axis2MessageContext)2 AssertEqual (org.apache.synapse.unittest.testcase.data.classes.AssertEqual)2 AssertNotNull (org.apache.synapse.unittest.testcase.data.classes.AssertNotNull)2 JsonArray (com.google.gson.JsonArray)1 JsonObject (com.google.gson.JsonObject)1 IOException (java.io.IOException)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 OMElement (org.apache.axiom.om.OMElement)1 HttpEntity (org.apache.http.HttpEntity)1 TestCaseSummary (org.apache.synapse.unittest.testcase.data.classes.TestCaseSummary)1