Search in sources :

Example 1 with ServiceResource

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

the class ConfigModifier method mockServiceLoader.

/**
 * Method parse the artifact data received and replaces actual endpoint urls with mock urls.
 * Call mock service creator with relevant mock service data.
 * Thread waits until all mock services are starts by checking port availability
 *
 * @param mockServiceData mock service data received from descriptor data
 * @return return a exception of error occurred while creating mock services
 */
static String mockServiceLoader(MockServiceData mockServiceData) {
    ArrayList<Integer> mockServicePorts = new ArrayList<>();
    for (int i = 0; i < mockServiceData.getMockServicesCount(); i++) {
        String endpointName = mockServiceData.getMockServices(i).getServiceName();
        int serviceElementIndex = mockServiceData.getServiceNameIndex(endpointName);
        int port = mockServiceData.getMockServices(serviceElementIndex).getPort();
        String context = mockServiceData.getMockServices(serviceElementIndex).getContext();
        String cloneContext = context;
        if (cloneContext.equals(BACK_SLASH)) {
            cloneContext = EMPTY_VALUE;
        }
        String serviceURL = HTTP + SERVICE_HOST + ":" + port + cloneContext;
        Map<String, String> mockServiceResources = new HashMap<>();
        for (ServiceResource resource : mockServiceData.getMockServices(serviceElementIndex).getResources()) {
            String resourcePath = resource.getSubContext();
            mockServiceResources.put(cloneContext + resourcePath, serviceURL + resourcePath);
        }
        mockServicePorts.add(port);
        unitTestMockEndpointMap.put(endpointName, mockServiceResources);
        log.info("Mock service creator ready to start service for " + endpointName);
        MockServiceCreator.startMockServiceServer(endpointName, SERVICE_HOST, port, context, mockServiceData.getMockServices(serviceElementIndex).getResources());
    }
    // check services are ready to serve by checking the ports
    if (!mockServicePorts.isEmpty()) {
        try {
            checkServiceStatus(mockServicePorts);
        } catch (IOException e) {
            log.error("Error occurred in checking services are ready to serve in given ports", e);
        }
    }
    return null;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ServiceResource(org.apache.synapse.unittest.testcase.data.classes.ServiceResource) IOException(java.io.IOException)

Example 2 with ServiceResource

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

the class MockServiceCreator method startMockServiceServer.

/**
 * Start service for given parameters using emulator.
 *
 * @param mockServiceName endpoint name given in descriptor data
 * @param host            domain of the url
 * @param context         path of the url
 * @param resources       resources of the service
 */
static void startMockServiceServer(String mockServiceName, String host, int port, String context, List<ServiceResource> resources) {
    try {
        HTTPProtocolEmulator httpEmulator = new Emulator().getHttpProtocolEmulator();
        emulatorServiceList.add(httpEmulator);
        HttpConsumerContext emulator = httpEmulator.consumer().host(host).port(port).context(context);
        for (ServiceResource resource : resources) {
            routeThroughResourceMethod(resource, emulator);
        }
        emulator.operations().start();
        log.info("Mock service started for " + mockServiceName + " in - http://" + host + ":" + port + context);
    } catch (Exception e) {
        log.error("Error in initiating mock service named " + mockServiceName, e);
    }
}
Also used : HttpConsumerContext(org.apache.synapse.commons.emulator.http.dsl.HttpConsumerContext) HTTPProtocolEmulator(org.apache.synapse.commons.emulator.http.HTTPProtocolEmulator) Emulator(org.apache.synapse.commons.emulator.core.Emulator) HTTPProtocolEmulator(org.apache.synapse.commons.emulator.http.HTTPProtocolEmulator) ServiceResource(org.apache.synapse.unittest.testcase.data.classes.ServiceResource) MalformedURLException(java.net.MalformedURLException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 3 with ServiceResource

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

Aggregations

ServiceResource (org.apache.synapse.unittest.testcase.data.classes.ServiceResource)3 ArrayList (java.util.ArrayList)2 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 MalformedURLException (java.net.MalformedURLException)1 HashMap (java.util.HashMap)1 QName (javax.xml.namespace.QName)1 OMElement (org.apache.axiom.om.OMElement)1 Emulator (org.apache.synapse.commons.emulator.core.Emulator)1 HTTPProtocolEmulator (org.apache.synapse.commons.emulator.http.HTTPProtocolEmulator)1 HttpConsumerContext (org.apache.synapse.commons.emulator.http.dsl.HttpConsumerContext)1 MockService (org.apache.synapse.unittest.testcase.data.classes.MockService)1 MockServiceData (org.apache.synapse.unittest.testcase.data.holders.MockServiceData)1