Search in sources :

Example 1 with ArtifactData

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

the class RequestHandler method clearRegistryAndConnectorResources.

/**
 * Clear unit test mock registry resources and remove connector test folder in temp if exists.
 *
 * @param synapseTestCases map which has test suite data
 */
private void clearRegistryAndConnectorResources(SynapseTestCase synapseTestCases) {
    ArtifactData readArtifactData = synapseTestCases.getArtifacts();
    try {
        // remove connector resources include test directory in temp
        if (!readArtifactData.getConnectorResources().isEmpty()) {
            String connectorDestination = System.getProperty(Constants.PRAM_TEMP_DIR) + File.separator + Constants.CONNECTOR_TEST_FOLDER;
            FileUtils.deleteDirectory(new File(connectorDestination));
            log.info("Removed connector resources from the temp directory");
        }
        // clear registry resources map in mock registry
        if (!readArtifactData.getRegistryResources().isEmpty()) {
            SynapseConfiguration synapseConfiguration = UnitTestingExecutor.getExecuteInstance().getSynapseConfiguration();
            UnitTestMockRegistry mockRegistry = (UnitTestMockRegistry) synapseConfiguration.getRegistry();
            mockRegistry.clearResources();
            log.info("Clear registry resources from the UnitTestMockRegistry");
        }
    } catch (IOException e) {
        log.error("Exception while removing mock connector directory in temp", e);
    }
}
Also used : IOException(java.io.IOException) SynapseConfiguration(org.apache.synapse.config.SynapseConfiguration) File(java.io.File) ArtifactData(org.apache.synapse.unittest.testcase.data.holders.ArtifactData)

Example 2 with ArtifactData

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

the class SynapseTestcaseDataReader method readAndStoreArtifactData.

/**
 * Read artifact data from the descriptor data.
 * Append artifact data into the data holder object
 *
 * @return dataHolder object with artifact data
 */
ArtifactData readAndStoreArtifactData() throws XMLStreamException, IOException {
    ArtifactData artifactDataHolder = new ArtifactData();
    Artifact testArtifact = new Artifact();
    // Read artifact from descriptor data
    QName qualifiedArtifacts = new QName("", ARTIFACTS, "");
    OMElement artifactsNode = importXMLFile.getFirstChildWithName(qualifiedArtifacts);
    QName qualifiedTestArtifact = new QName("", TEST_ARTIFACT, "");
    OMElement testArtifactNode = artifactsNode.getFirstChildWithName(qualifiedTestArtifact);
    QName qualifiedArtifact = new QName("", ARTIFACT, "");
    OMElement testArtifactDataNode = testArtifactNode.getFirstChildWithName(qualifiedArtifact);
    // Read test artifact type from synapse test data
    String testArtifactType = testArtifactDataNode.getFirstElement().getLocalName();
    String testArtifactData = testArtifactDataNode.getFirstElement().toString();
    if (testArtifactType.equals(TYPE_SEQUENCE)) {
        testArtifactData = updateCallMediatorBlockingMode(testArtifactData);
    }
    testArtifact.setArtifact(testArtifactData);
    testArtifact.setArtifactType(testArtifactType);
    // Read artifact name from descriptor data
    String testArtifactNameOrKey;
    if (testArtifactType.equals(TYPE_LOCAL_ENTRY)) {
        testArtifactNameOrKey = testArtifactDataNode.getFirstElement().getAttributeValue(new QName(ARTIFACT_KEY_ATTRIBUTE));
    } else {
        testArtifactNameOrKey = testArtifactDataNode.getFirstElement().getAttributeValue(new QName(NAME_ATTRIBUTE));
    }
    testArtifact.setArtifactNameOrKey(testArtifactNameOrKey);
    // Read artifact transport from descriptor data if artifact is a proxy
    if (testArtifact.getArtifactType().equals(TYPE_PROXY)) {
        String transport = testArtifactDataNode.getFirstElement().getAttributeValue(new QName(ARTIFACT_TRANSPORTS_ATTRIBUTE));
        if (transport == null) {
            throw new IOException("Local transport method for proxy currently not supported");
        }
        String[] transportMethods = transport.split(" ");
        if (Arrays.asList(transportMethods).contains(HTTP_KEY)) {
            testArtifact.setTransportMethod(HTTP_KEY);
        } else if (Arrays.asList(transportMethods).contains(HTTPS_KEY)) {
            testArtifact.setTransportMethod(HTTPS_KEY);
        } else {
            throw new IOException("Defined transport method for proxy currently not supported");
        }
    }
    artifactDataHolder.setTestArtifact(testArtifact);
    // Read supportive test cases data
    QName qualifiedSupportiveTestArtifact = new QName("", SUPPORTIVE_ARTIFACTS, "");
    OMElement supportiveArtifactsNode = artifactsNode.getFirstChildWithName(qualifiedSupportiveTestArtifact);
    Iterator artifactIterator = Collections.emptyIterator();
    int supportiveArtifactCount = 0;
    if (supportiveArtifactsNode != null) {
        artifactIterator = supportiveArtifactsNode.getChildElements();
    }
    while (artifactIterator.hasNext()) {
        OMElement artifact = (OMElement) artifactIterator.next();
        Artifact supportiveArtifact = new Artifact();
        // Read supportive artifact from synapse test data
        String supportiveArtifactData = artifact.getFirstElement().toString();
        // Read supportive artifact type from synapse test data
        String supportiveArtifactType = artifact.getFirstElement().getLocalName();
        if (supportiveArtifactType.equals(TYPE_SEQUENCE) || supportiveArtifactType.equals(TYPE_TEMPLATE)) {
            supportiveArtifactData = updateCallMediatorBlockingMode(supportiveArtifactData);
        }
        supportiveArtifact.setArtifact(supportiveArtifactData);
        supportiveArtifact.setArtifactType(supportiveArtifactType);
        // Read artifact name from descriptor data
        String supportiveArtifactNameOrKey;
        if (supportiveArtifactType.equals(TYPE_LOCAL_ENTRY)) {
            supportiveArtifactNameOrKey = artifact.getFirstElement().getAttributeValue(new QName(ARTIFACT_KEY_ATTRIBUTE));
        } else {
            supportiveArtifactNameOrKey = artifact.getFirstElement().getAttributeValue(new QName(NAME_ATTRIBUTE));
        }
        supportiveArtifact.setArtifactNameOrKey(supportiveArtifactNameOrKey);
        artifactDataHolder.addSupportiveArtifact(supportiveArtifact);
        supportiveArtifactCount++;
    }
    // set supportive artifact count
    artifactDataHolder.setSupportiveArtifactCount(supportiveArtifactCount);
    // Read and store registry data
    QName qualifiedRegistryArtifact = new QName("", REGISTRY_RESOURCES, "");
    OMElement registryArtifactsNode = artifactsNode.getFirstChildWithName(qualifiedRegistryArtifact);
    Iterator<?> registryIterator = Collections.emptyIterator();
    if (registryArtifactsNode != null) {
        registryIterator = registryArtifactsNode.getChildElements();
    }
    while (registryIterator.hasNext()) {
        OMElement resource = (OMElement) registryIterator.next();
        // Read registry artifact from synapse test data
        QName qualifiedRegistryArtifactFileName = new QName("", REGISTRY_NAME, "");
        OMElement registryArtifactsNameNode = resource.getFirstChildWithName(qualifiedRegistryArtifactFileName);
        String resourceName = registryArtifactsNameNode.getText();
        QName qualifiedRegistryArtifactFilePath = new QName("", REGISTRY_PATH, "");
        OMElement registryArtifactsPathNode = resource.getFirstChildWithName(qualifiedRegistryArtifactFilePath);
        String resourcePath = registryArtifactsPathNode.getText();
        QName qualifiedRegistryArtifactFile = new QName("", ARTIFACT, "");
        OMElement registryArtifactsFileNode = resource.getFirstChildWithName(qualifiedRegistryArtifactFile);
        String resourceArtifact = registryArtifactsFileNode.getText();
        if (resourceArtifact.isEmpty()) {
            resourceArtifact = registryArtifactsFileNode.getFirstOMChild().toString();
        }
        QName qualifiedRegistryArtifactMediaType = new QName("", REGISTRY_MEDIA_TYPE, "");
        OMElement registryArtifactsMediaTypeNode = resource.getFirstChildWithName(qualifiedRegistryArtifactMediaType);
        String resourceMediaType = registryArtifactsMediaTypeNode.getText();
        RegistryResource registryResource = new RegistryResource();
        registryResource.setRegistryResourceName(resourceName);
        registryResource.setArtifact(resourceArtifact);
        registryResource.setRegistryPath(resourcePath);
        registryResource.setMediaType(resourceMediaType);
        String registryKey = resourcePath + Constants.BACK_SLASH + resourceName;
        artifactDataHolder.addRegistryResource(registryKey, registryResource);
    }
    // Read and store registry data
    QName qualifiedConnectorResources = new QName("", CONNECTOR_RESOURCES, "");
    OMElement connectorResourcesNode = artifactsNode.getFirstChildWithName(qualifiedConnectorResources);
    Iterator<?> connectorIterator = Collections.emptyIterator();
    if (connectorResourcesNode != null) {
        connectorIterator = connectorResourcesNode.getChildElements();
    }
    while (connectorIterator.hasNext()) {
        OMElement resource = (OMElement) connectorIterator.next();
        String connectorResourceFileBase64 = resource.getText();
        artifactDataHolder.addConnectorResource(connectorResourceFileBase64);
    }
    log.info("Artifact data from descriptor data read successfully");
    return artifactDataHolder;
}
Also used : QName(javax.xml.namespace.QName) Iterator(java.util.Iterator) OMElement(org.apache.axiom.om.OMElement) IOException(java.io.IOException) ArtifactData(org.apache.synapse.unittest.testcase.data.holders.ArtifactData) Artifact(org.apache.synapse.unittest.testcase.data.classes.Artifact) RegistryResource(org.apache.synapse.unittest.testcase.data.classes.RegistryResource)

Example 3 with ArtifactData

use of org.apache.synapse.unittest.testcase.data.holders.ArtifactData 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 ArtifactData (org.apache.synapse.unittest.testcase.data.holders.ArtifactData)3 File (java.io.File)1 Iterator (java.util.Iterator)1 QName (javax.xml.namespace.QName)1 OMElement (org.apache.axiom.om.OMElement)1 SynapseConfiguration (org.apache.synapse.config.SynapseConfiguration)1 Artifact (org.apache.synapse.unittest.testcase.data.classes.Artifact)1 RegistryResource (org.apache.synapse.unittest.testcase.data.classes.RegistryResource)1 SynapseTestCase (org.apache.synapse.unittest.testcase.data.classes.SynapseTestCase)1 MockServiceData (org.apache.synapse.unittest.testcase.data.holders.MockServiceData)1 TestCaseData (org.apache.synapse.unittest.testcase.data.holders.TestCaseData)1