use of org.apache.synapse.unittest.testcase.data.classes.MockService 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;
}
Aggregations