Search in sources :

Example 1 with AssertEqual

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

the class SynapseTestcaseDataReader method readAndStoreTestCaseData.

/**
 * Read test-case data from the descriptor data.
 * Append test-case data into the test data holder object
 *
 * @return testCaseDataHolder object with test case data
 */
TestCaseData readAndStoreTestCaseData() {
    TestCaseData testCaseDataHolder = new TestCaseData();
    // Set test case count as zero
    int testCasesCount = 0;
    // Read test cases from descriptor data
    QName qualifiedTestCases = new QName("", TEST_CASES, "");
    OMElement testCasesNode = importXMLFile.getFirstChildWithName(qualifiedTestCases);
    // Iterate through test-cases in descriptor data
    Iterator<?> testCaseIterator = Collections.emptyIterator();
    if (testCasesNode != null) {
        testCaseIterator = testCasesNode.getChildElements();
    }
    while (testCaseIterator.hasNext()) {
        TestCase testCase = new TestCase();
        OMElement testCaseNode = (OMElement) (testCaseIterator.next());
        String testCaseName = testCaseNode.getAttributeValue(new QName(NAME_ATTRIBUTE));
        testCase.setTestCaseName(testCaseName);
        // Read input child from test-case node
        QName qualifiedInput = new QName("", TEST_CASE_INPUT, "");
        OMElement testCaseInputNode = testCaseNode.getFirstChildWithName(qualifiedInput);
        // Read input node data of payload and properties if not null
        if (testCaseInputNode != null) {
            readTestCaseInputData(testCaseInputNode, testCase);
        }
        // Read assertions of test-case node
        QName qualifiedAssertions = new QName("", TEST_CASE_ASSERTIONS, "");
        OMElement testCaseAssertionNode = testCaseNode.getFirstChildWithName(qualifiedAssertions);
        ArrayList<AssertEqual> assertEquals = new ArrayList<>();
        ArrayList<AssertNotNull> assertNotNulls = new ArrayList<>();
        readTestCaseAssertions(testCaseAssertionNode, assertEquals, assertNotNulls);
        // set assertion values in testCase object
        testCase.setAssertEquals(assertEquals);
        testCase.setAssertNotNull(assertNotNulls);
        // set testCase object in testCase data holder
        testCaseDataHolder.setTestCases(testCase);
        testCasesCount++;
    }
    // Set test case count in test data holder
    testCaseDataHolder.setTestCaseCount(testCasesCount);
    log.info("Test case data from descriptor data read successfully");
    return testCaseDataHolder;
}
Also used : TestCaseData(org.apache.synapse.unittest.testcase.data.holders.TestCaseData) AssertEqual(org.apache.synapse.unittest.testcase.data.classes.AssertEqual) TestCase(org.apache.synapse.unittest.testcase.data.classes.TestCase) AssertNotNull(org.apache.synapse.unittest.testcase.data.classes.AssertNotNull) QName(javax.xml.namespace.QName) ArrayList(java.util.ArrayList) OMElement(org.apache.axiom.om.OMElement)

Example 2 with AssertEqual

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

the class SynapseTestcaseDataReader method readTestCaseAssertions.

/**
 * Read test case assertion data from the descriptor data.
 * Read assertEquals and assertNotNull data if exists
 *
 * @param testCaseAssertionNode node of test case assertions
 * @param assertEquals          array of assertEquals
 * @param assertNotNulls        array of assertNotNulls
 */
private void readTestCaseAssertions(OMElement testCaseAssertionNode, ArrayList<AssertEqual> assertEquals, ArrayList<AssertNotNull> assertNotNulls) {
    // Read assertions - AssertEquals of test-case node
    Iterator<?> assertEqualsIterator = testCaseAssertionNode.getChildrenWithName(new QName(TEST_CASE_ASSERTION_EQUALS));
    while (assertEqualsIterator.hasNext()) {
        AssertEqual assertion = new AssertEqual();
        OMElement assertEqualNode = (OMElement) (assertEqualsIterator.next());
        QName qualifiedAssertActual = new QName("", ASSERTION_ACTUAL, "");
        OMElement assertActualNode = assertEqualNode.getFirstChildWithName(qualifiedAssertActual);
        String actual = assertActualNode.getText();
        assertion.setActual(actual);
        QName qualifiedAssertMessage = new QName("", ASSERTION_MESSAGE, "");
        OMElement assertMessageNode = assertEqualNode.getFirstChildWithName(qualifiedAssertMessage);
        String message = assertMessageNode.getText();
        assertion.setMessage(message);
        QName qualifiedExpectedMessage = new QName("", ASSERTION_EXPECTED, "");
        OMElement assertExpectedNode = assertEqualNode.getFirstChildWithName(qualifiedExpectedMessage);
        String expected = assertExpectedNode.getText();
        assertion.setExpected(expected);
        assertEquals.add(assertion);
    }
    // Read assertions - AssertNotNull of test-case node
    Iterator<?> assertNotNullIterator = testCaseAssertionNode.getChildrenWithName(new QName(TEST_CASE_ASSERTION_NOTNULL));
    while (assertNotNullIterator.hasNext()) {
        AssertNotNull assertion = new AssertNotNull();
        OMElement assertEqualNode = (OMElement) (assertNotNullIterator.next());
        QName qualifiedAssertActual = new QName("", ASSERTION_ACTUAL, "");
        OMElement assertActualNode = assertEqualNode.getFirstChildWithName(qualifiedAssertActual);
        String actual = assertActualNode.getText();
        assertion.setActual(actual);
        QName qualifiedAssertMessage = new QName("", ASSERTION_MESSAGE, "");
        OMElement assertMessageNode = assertEqualNode.getFirstChildWithName(qualifiedAssertMessage);
        String message = assertMessageNode.getText();
        assertion.setMessage(message);
        assertNotNulls.add(assertion);
    }
}
Also used : AssertEqual(org.apache.synapse.unittest.testcase.data.classes.AssertEqual) AssertNotNull(org.apache.synapse.unittest.testcase.data.classes.AssertNotNull) QName(javax.xml.namespace.QName) OMElement(org.apache.axiom.om.OMElement)

Example 3 with AssertEqual

use of org.apache.synapse.unittest.testcase.data.classes.AssertEqual 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 AssertEqual

use of org.apache.synapse.unittest.testcase.data.classes.AssertEqual 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)

Aggregations

AssertEqual (org.apache.synapse.unittest.testcase.data.classes.AssertEqual)4 OMElement (org.apache.axiom.om.OMElement)3 QName (javax.xml.namespace.QName)2 AssertNotNull (org.apache.synapse.unittest.testcase.data.classes.AssertNotNull)2 TestCaseAssertionSummary (org.apache.synapse.unittest.testcase.data.classes.TestCaseAssertionSummary)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 Header (org.apache.http.Header)1 HttpEntity (org.apache.http.HttpEntity)1 HttpResponse (org.apache.http.HttpResponse)1 Axis2MessageContext (org.apache.synapse.core.axis2.Axis2MessageContext)1 TestCase (org.apache.synapse.unittest.testcase.data.classes.TestCase)1 TestCaseData (org.apache.synapse.unittest.testcase.data.holders.TestCaseData)1