Search in sources :

Example 1 with TestCaseData

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

use of org.apache.synapse.unittest.testcase.data.holders.TestCaseData in project wso2-synapse by wso2.

the class RequestHandler method preProcessingData.

/**
 * Processed received message data and stores those data in relevant data holders.
 * Uses configModifier if there are some mock services to start
 *
 * @param receivedMessage received synapseTestcase data message as String
 * @return SynapaseTestCase object contains artifact, test cases and mock services data
 */
private SynapseTestCase preProcessingData(String receivedMessage) {
    try {
        // Read relevant data from the received message and add to the relevant data holders
        SynapseTestcaseDataReader synapseTestcaseDataReader = new SynapseTestcaseDataReader(receivedMessage);
        ArtifactData readArtifactData = synapseTestcaseDataReader.readAndStoreArtifactData();
        TestCaseData readTestCaseData = synapseTestcaseDataReader.readAndStoreTestCaseData();
        MockServiceData readMockServiceData = synapseTestcaseDataReader.readAndStoreMockServiceData();
        // configure connector resources if exists
        if (!readArtifactData.getConnectorResources().isEmpty()) {
            ConnectorDeployer.deployConnectorResources(readArtifactData.getConnectorResources());
        }
        // store registry resources into mock registry in synapse configuration
        if (!readArtifactData.getRegistryResources().isEmpty()) {
            addRegistryResourcesToMockRegistry(readArtifactData.getRegistryResources());
        }
        // configure the artifact if there are mock-services to append
        String exceptionWhileMocking = null;
        if (readMockServiceData.getMockServicesCount() > 0) {
            exceptionWhileMocking = ConfigModifier.mockServiceLoader(readMockServiceData);
        }
        // check is there any error occurred while mocking endpoints if yes stop the testing and return the exception
        if (exceptionWhileMocking != null) {
            exception = exceptionWhileMocking;
            return null;
        }
        // wrap the artifact data, testcase data and mock service data as one
        SynapseTestCase synapseTestCases = new SynapseTestCase();
        synapseTestCases.setArtifacts(readArtifactData);
        synapseTestCases.setTestCases(readTestCaseData);
        return synapseTestCases;
    } catch (Exception e) {
        log.error("Error while reading data from received message", e);
        exception = CommonUtils.stackTraceToString(e);
        return null;
    }
}
Also used : TestCaseData(org.apache.synapse.unittest.testcase.data.holders.TestCaseData) MockServiceData(org.apache.synapse.unittest.testcase.data.holders.MockServiceData) SynapseTestCase(org.apache.synapse.unittest.testcase.data.classes.SynapseTestCase) ArtifactData(org.apache.synapse.unittest.testcase.data.holders.ArtifactData) IOException(java.io.IOException)

Aggregations

TestCaseData (org.apache.synapse.unittest.testcase.data.holders.TestCaseData)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 QName (javax.xml.namespace.QName)1 OMElement (org.apache.axiom.om.OMElement)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 TestCase (org.apache.synapse.unittest.testcase.data.classes.TestCase)1 ArtifactData (org.apache.synapse.unittest.testcase.data.holders.ArtifactData)1 MockServiceData (org.apache.synapse.unittest.testcase.data.holders.MockServiceData)1