Search in sources :

Example 1 with TestCase

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

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

the class TestingAgent method processTestCases.

/**
 * Check artifact type and pass the test case data to the relevant mediation.
 *
 * @param synapseTestCase test cases data received from client
 */
void processTestCases(SynapseTestCase synapseTestCase, TestSuiteSummary testSuiteSummary) {
    int testCaseCount = synapseTestCase.getTestCases().getTestCaseCount();
    log.info(testCaseCount + " Test case(s) ready to execute");
    String currentTestCaseName = null;
    try {
        // execute test cases with synapse configurations and test data
        for (int i = 0; i < testCaseCount; i++) {
            TestCaseSummary testSummary = new TestCaseSummary();
            TestCase currentTestCase = synapseTestCase.getTestCases().getTestCase(i);
            currentTestCaseName = currentTestCase.getTestCaseName();
            testSummary.setTestCaseName(currentTestCaseName);
            testSuiteSummary.setRecentTestCaseName(currentTestCaseName);
            switch(mainTestArtifactType) {
                case TYPE_SEQUENCE:
                    Map.Entry<Boolean, MessageContext> mediateResult = TestCasesMediator.sequenceMediate(currentTestCase, synapseConfiguration, key);
                    testSuiteSummary.setMediationStatus(Constants.PASSED_KEY);
                    Boolean mediationResult = mediateResult.getKey();
                    MessageContext resultedMessageContext = mediateResult.getValue();
                    // check whether mediation is success or not
                    checkAssertionWithSequenceMediation(mediationResult, resultedMessageContext, currentTestCase, testSummary);
                    break;
                case TYPE_PROXY:
                    Map.Entry<String, HttpResponse> invokedProxyResult = TestCasesMediator.proxyServiceExecutor(currentTestCase, proxyTransportMethod, key);
                    testSuiteSummary.setMediationStatus(Constants.PASSED_KEY);
                    checkAssertionWithProxyMediation(invokedProxyResult, currentTestCase, testSummary);
                    break;
                case TYPE_API:
                    String context = artifactNode.getAttributeValue(new QName(API_CONTEXT));
                    String resourceMethod = currentTestCase.getRequestMethod();
                    String protocolType = currentTestCase.getProtocolType();
                    if (protocolType == null || protocolType.isEmpty()) {
                        protocolType = HTTP_KEY;
                    }
                    Map.Entry<String, HttpResponse> invokedApiResult = TestCasesMediator.apiResourceExecutor(currentTestCase, context, resourceMethod, protocolType);
                    testSuiteSummary.setMediationStatus(Constants.PASSED_KEY);
                    checkAssertionWithAPIMediation(invokedApiResult, currentTestCase, testSummary);
                    break;
                default:
                    break;
            }
            testSuiteSummary.addTestCaseSumamry(testSummary);
        }
    } catch (Exception e) {
        log.error("Error occurred while running test cases", e);
        exception = CommonUtils.stackTraceToString(e);
        testSuiteSummary.setRecentTestCaseName(currentTestCaseName);
        testSuiteSummary.setMediationStatus(Constants.FAILED_KEY);
        testSuiteSummary.setMediationException(exception);
    }
}
Also used : SynapseTestCase(org.apache.synapse.unittest.testcase.data.classes.SynapseTestCase) TestCase(org.apache.synapse.unittest.testcase.data.classes.TestCase) QName(javax.xml.namespace.QName) TestCaseSummary(org.apache.synapse.unittest.testcase.data.classes.TestCaseSummary) HttpResponse(org.apache.http.HttpResponse) MessageContext(org.apache.synapse.MessageContext) HashMap(java.util.HashMap) Map(java.util.Map) AbstractMap(java.util.AbstractMap) IOException(java.io.IOException)

Aggregations

QName (javax.xml.namespace.QName)2 TestCase (org.apache.synapse.unittest.testcase.data.classes.TestCase)2 IOException (java.io.IOException)1 AbstractMap (java.util.AbstractMap)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 OMElement (org.apache.axiom.om.OMElement)1 HttpResponse (org.apache.http.HttpResponse)1 MessageContext (org.apache.synapse.MessageContext)1 AssertEqual (org.apache.synapse.unittest.testcase.data.classes.AssertEqual)1 AssertNotNull (org.apache.synapse.unittest.testcase.data.classes.AssertNotNull)1 SynapseTestCase (org.apache.synapse.unittest.testcase.data.classes.SynapseTestCase)1 TestCaseSummary (org.apache.synapse.unittest.testcase.data.classes.TestCaseSummary)1 TestCaseData (org.apache.synapse.unittest.testcase.data.holders.TestCaseData)1