Search in sources :

Example 1 with RegistryResource

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

the class UnitTestMockRegistry method lookup.

@Override
public OMNode lookup(String key) {
    if (key == null) {
        handleException("Resource cannot be found.");
    }
    if (key.isEmpty()) {
        handleException("Resource cannot be empty");
    }
    String resourcePath = getAbsolutePathToRegistry(key);
    if (resourcePath != null && testMockRegistry.containsKey(resourcePath)) {
        RegistryResource resource = testMockRegistry.get(resourcePath);
        String sourceOfResource = resource.getArtifact();
        OMNode omNode = null;
        try {
            ByteArrayInputStream inputStream = new ByteArrayInputStream(sourceOfResource.getBytes());
            try {
                XMLStreamReader parser = XMLInputFactory.newInstance().createXMLStreamReader(inputStream);
                StAXOMBuilder builder = new StAXOMBuilder(parser);
                omNode = builder.getDocumentElement();
            } catch (OMException ignored) {
                omNode = readNonXML(resource);
            } catch (XMLStreamException ignored) {
                omNode = readNonXML(resource);
            } catch (Exception e) {
                log.error("Error while reading the resource '" + key + "'", e);
            } finally {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    log.error("Error while closing the input stream", e);
                }
            }
        } catch (Exception e) {
            log.error("Creating OMNode from registry resource artifact failed", e);
        }
        return omNode;
    } else {
        return null;
    }
}
Also used : OMNode(org.apache.axiom.om.OMNode) XMLStreamReader(javax.xml.stream.XMLStreamReader) XMLStreamException(javax.xml.stream.XMLStreamException) ByteArrayInputStream(java.io.ByteArrayInputStream) StAXOMBuilder(org.apache.axiom.om.impl.builder.StAXOMBuilder) IOException(java.io.IOException) OMException(org.apache.axiom.om.OMException) RegistryResource(org.apache.synapse.unittest.testcase.data.classes.RegistryResource) XMLStreamException(javax.xml.stream.XMLStreamException) SynapseException(org.apache.synapse.SynapseException) IOException(java.io.IOException) OMException(org.apache.axiom.om.OMException)

Example 2 with RegistryResource

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

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

the class RequestHandler method addRegistryResourcesToMockRegistry.

/**
 * Add user sent registry resources into the mock registry.
 *
 * @param resourceMap map which has registry resources
 */
private void addRegistryResourcesToMockRegistry(Map<String, RegistryResource> resourceMap) {
    SynapseConfiguration synapseConfiguration = UnitTestingExecutor.getExecuteInstance().getSynapseConfiguration();
    UnitTestMockRegistry mockRegistry = (UnitTestMockRegistry) synapseConfiguration.getRegistry();
    for (Map.Entry<String, RegistryResource> resource : resourceMap.entrySet()) {
        mockRegistry.addResource(resource.getKey(), resource.getValue());
    }
}
Also used : SynapseConfiguration(org.apache.synapse.config.SynapseConfiguration) AbstractMap(java.util.AbstractMap) Map(java.util.Map) RegistryResource(org.apache.synapse.unittest.testcase.data.classes.RegistryResource)

Example 4 with RegistryResource

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

the class UnitTestMockRegistry method getResourceProperties.

@Override
public Properties getResourceProperties(String entryKey) {
    Properties propertySet = new Properties();
    String filePathAsKey = entryKey.substring(0, entryKey.length() - 1) + ".properties";
    String resourcePath = getAbsolutePathToRegistry(filePathAsKey);
    boolean isFoundPropertyFile = false;
    // check registry has .properties file for the properties with key
    if (resourcePath != null && testMockRegistry.containsKey(resourcePath)) {
        isFoundPropertyFile = true;
    } else {
        // check registry has file for the properties with key
        filePathAsKey = entryKey.substring(0, entryKey.length() - 1);
        resourcePath = getAbsolutePathToRegistry(filePathAsKey);
        if (resourcePath != null && testMockRegistry.containsKey(resourcePath)) {
            isFoundPropertyFile = true;
        }
    }
    // check properties file found in the registry if not return null
    if (!isFoundPropertyFile) {
        return null;
    }
    RegistryResource resource = testMockRegistry.get(resourcePath);
    String sourceOfResource = resource.getArtifact().trim();
    try {
        InputStream input = new ByteArrayInputStream(sourceOfResource.getBytes(StandardCharsets.UTF_8));
        propertySet.load(input);
    } catch (IOException e) {
        log.error("Error in loading properties from registry resource", e);
    }
    return propertySet;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) Properties(java.util.Properties) RegistryResource(org.apache.synapse.unittest.testcase.data.classes.RegistryResource)

Example 5 with RegistryResource

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

the class UnitTestMockRegistry method getRegistryEntry.

@Override
public RegistryEntry getRegistryEntry(String key) {
    // get information from the actual resource
    TestMediationRegistryEntryImpl entryEmbedded = new TestMediationRegistryEntryImpl();
    String resourcePath = getAbsolutePathToRegistry(key);
    RegistryResource resource = testMockRegistry.get(resourcePath);
    if (resourcePath != null && testMockRegistry.containsKey(resourcePath)) {
        String resourceName = resource.getRegistryPath() + resource.getRegistryResourceName();
        Date date = new Date();
        long timestamp = date.getTime();
        entryEmbedded.setKey(key);
        entryEmbedded.setName(resourceName);
        entryEmbedded.setType(FILE);
        entryEmbedded.setDescription("Resource at : " + resourceName);
        entryEmbedded.setLastModified(timestamp);
        entryEmbedded.setVersion(timestamp);
        entryEmbedded.setCachableDuration(15000);
        return entryEmbedded;
    }
    return null;
}
Also used : RegistryResource(org.apache.synapse.unittest.testcase.data.classes.RegistryResource) Date(java.util.Date)

Aggregations

RegistryResource (org.apache.synapse.unittest.testcase.data.classes.RegistryResource)5 IOException (java.io.IOException)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)1 AbstractMap (java.util.AbstractMap)1 Date (java.util.Date)1 Iterator (java.util.Iterator)1 Map (java.util.Map)1 Properties (java.util.Properties)1 QName (javax.xml.namespace.QName)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 XMLStreamReader (javax.xml.stream.XMLStreamReader)1 OMElement (org.apache.axiom.om.OMElement)1 OMException (org.apache.axiom.om.OMException)1 OMNode (org.apache.axiom.om.OMNode)1 StAXOMBuilder (org.apache.axiom.om.impl.builder.StAXOMBuilder)1 SynapseException (org.apache.synapse.SynapseException)1 SynapseConfiguration (org.apache.synapse.config.SynapseConfiguration)1 Artifact (org.apache.synapse.unittest.testcase.data.classes.Artifact)1 ArtifactData (org.apache.synapse.unittest.testcase.data.holders.ArtifactData)1