Search in sources :

Example 1 with MockServiceData

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

the class SynapseTestcaseDataReader method readAndStoreMockServiceData.

/**
 * Read mock-service data from the descriptor data.
 * Append mock-service data into the test data holder object
 *
 * @return mockServiceDataHolder object with test case data
 */
MockServiceData readAndStoreMockServiceData() {
    MockServiceData mockServiceDataHolder = new MockServiceData();
    // Set mock service count as zero
    int mockServiceCount = 0;
    // Read mock services from descriptor data
    QName qualifiedMockServices = new QName("", MOCK_SERVICES, "");
    OMElement mockServicesNode = importXMLFile.getFirstChildWithName(qualifiedMockServices);
    // check whether descriptor data has mock services
    if (mockServicesNode != null) {
        // Iterate through mock-service in descriptor data
        Iterator<?> iterator = mockServicesNode.getChildElements();
        while (iterator.hasNext()) {
            OMElement mockServiceNode = (OMElement) (iterator.next());
            MockService service = new MockService();
            // Read service name child attribute from mock service node
            QName qualifiedServiceName = new QName("", SERVICE_NAME, "");
            OMElement serviceNameNode = mockServiceNode.getFirstChildWithName(qualifiedServiceName);
            String serviceName = serviceNameNode.getText();
            service.setServiceName(serviceName);
            // Read service port child attribute from mock service node
            QName qualifiedServicePort = new QName("", SERVICE_PORT, "");
            OMElement servicePortNode = mockServiceNode.getFirstChildWithName(qualifiedServicePort);
            int servicePort = Integer.parseInt(servicePortNode.getText());
            service.setPort(servicePort);
            // Read service path child attribute from mock service node
            QName qualifiedServicePath = new QName("", SERVICE_CONTEXT, "");
            OMElement servicePathNode = mockServiceNode.getFirstChildWithName(qualifiedServicePath);
            String servicePath = servicePathNode.getText();
            service.setContext(servicePath);
            // Read resources of the mock service
            QName qualifiedServiceResources = new QName("", SERVICE_RESOURCES, "");
            OMElement serviceResourcesNode = mockServiceNode.getFirstChildWithName(qualifiedServiceResources);
            // Read resource of the resources
            Iterator<?> resourceIterator = serviceResourcesNode.getChildElements();
            List<ServiceResource> resources = new ArrayList<>();
            while (resourceIterator.hasNext()) {
                OMElement mockServiceResourceNode = (OMElement) (resourceIterator.next());
                ServiceResource resource = new ServiceResource();
                // Read service type child attribute from mock service node
                QName qualifiedServiceMethod = new QName("", SERVICE_RESOURCE_METHOD, "");
                OMElement serviceMethodNode = mockServiceResourceNode.getFirstChildWithName(qualifiedServiceMethod);
                String serviceMethod = serviceMethodNode.getText();
                resource.setMethod(serviceMethod);
                // Read service sub-context child attribute from mock service node
                QName qualifiedServiceSubContext = new QName("", SERVICE_RESOURCE_SUBCONTEXT, "");
                OMElement serviceMethodSubContextNode = mockServiceResourceNode.getFirstChildWithName(qualifiedServiceSubContext);
                String serviceSubContext = serviceMethodSubContextNode.getText();
                resource.setSubContext(serviceSubContext);
                // Read service request data of payload and headers
                readMockServicesRequest(mockServiceResourceNode, resource);
                // Read service response data of payload and headers
                readMockServicesResponse(mockServiceResourceNode, resource);
                resources.add(resource);
            }
            // adding resource to the service
            service.setResources(resources);
            // adding created mock service object to data holder
            mockServiceDataHolder.setServiceNameIndex(service.getServiceName(), mockServiceCount);
            mockServiceDataHolder.addMockServices(service);
            mockServiceCount++;
        }
    }
    // Set mock service count in mock service data holder
    mockServiceDataHolder.setMockServicesCount(mockServiceCount);
    log.info("Mock service data from descriptor data read successfully");
    return mockServiceDataHolder;
}
Also used : MockServiceData(org.apache.synapse.unittest.testcase.data.holders.MockServiceData) QName(javax.xml.namespace.QName) ArrayList(java.util.ArrayList) MockService(org.apache.synapse.unittest.testcase.data.classes.MockService) OMElement(org.apache.axiom.om.OMElement) ServiceResource(org.apache.synapse.unittest.testcase.data.classes.ServiceResource)

Example 2 with MockServiceData

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

MockServiceData (org.apache.synapse.unittest.testcase.data.holders.MockServiceData)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 QName (javax.xml.namespace.QName)1 OMElement (org.apache.axiom.om.OMElement)1 MockService (org.apache.synapse.unittest.testcase.data.classes.MockService)1 ServiceResource (org.apache.synapse.unittest.testcase.data.classes.ServiceResource)1 SynapseTestCase (org.apache.synapse.unittest.testcase.data.classes.SynapseTestCase)1 ArtifactData (org.apache.synapse.unittest.testcase.data.holders.ArtifactData)1 TestCaseData (org.apache.synapse.unittest.testcase.data.holders.TestCaseData)1