Search in sources :

Example 1 with SynapseTestCase

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

Example 2 with SynapseTestCase

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

the class RequestHandler method run.

@Override
public void run() {
    try {
        log.info("Start processing test-case handler");
        checkTransportPassThroughPortAvailability();
        String receivedData = readData();
        SynapseConfiguration synapseConfiguration = UnitTestingExecutor.getExecuteInstance().getSynapseConfiguration();
        synapseConfiguration.setProperty(Constants.IS_RUNNING_AS_UNIT_TEST, "true");
        SynapseTestCase synapseTestCases = preProcessingData(receivedData);
        if (synapseTestCases != null) {
            runTestingAgent(synapseTestCases);
            clearRegistryAndConnectorResources(synapseTestCases);
        } else {
            log.error("Reading Synapse testcase data failed");
            testSuiteSummary.setDescription("Failed while reading synapseTestCase data");
            testSuiteSummary.setDeploymentStatus(Constants.SKIPPED_KEY);
            testSuiteSummary.setDeploymentException(exception);
        }
        writeData(testSuiteSummary);
        MockServiceCreator.stopServices();
        log.info("End processing test-case handler");
    } catch (Exception e) {
        log.error("Error while running client request in test agent", e);
    } finally {
        closeSocket();
    }
}
Also used : SynapseTestCase(org.apache.synapse.unittest.testcase.data.classes.SynapseTestCase) SynapseConfiguration(org.apache.synapse.config.SynapseConfiguration) IOException(java.io.IOException)

Example 3 with SynapseTestCase

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

IOException (java.io.IOException)3 SynapseTestCase (org.apache.synapse.unittest.testcase.data.classes.SynapseTestCase)3 AbstractMap (java.util.AbstractMap)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 QName (javax.xml.namespace.QName)1 HttpResponse (org.apache.http.HttpResponse)1 MessageContext (org.apache.synapse.MessageContext)1 SynapseConfiguration (org.apache.synapse.config.SynapseConfiguration)1 TestCase (org.apache.synapse.unittest.testcase.data.classes.TestCase)1 TestCaseSummary (org.apache.synapse.unittest.testcase.data.classes.TestCaseSummary)1 ArtifactData (org.apache.synapse.unittest.testcase.data.holders.ArtifactData)1 MockServiceData (org.apache.synapse.unittest.testcase.data.holders.MockServiceData)1 TestCaseData (org.apache.synapse.unittest.testcase.data.holders.TestCaseData)1