Search in sources :

Example 26 with MessageContext

use of org.apache.synapse.MessageContext in project wso2-synapse by wso2.

the class XSLTMediatorTest method testTransformXSLTSmallMessages.

public void testTransformXSLTSmallMessages() throws Exception {
    // create a new switch mediator
    XSLTMediator transformMediator = new XSLTMediator();
    // set XSLT transformation URL
    setXsltTransformationURL(transformMediator, "xslt-key");
    for (int i = 0; i < 5; i++) {
        // invoke transformation, with static enveope
        MessageContext synCtx = new TestMessageContextBuilder().addFileEntry("xslt-key", "../../repository/conf/sample/resources/transform/transform_load_2.xml").setBodyFromFile("../../repository/conf/sample/resources/transform/small_message.xml").addTextAroundBody().build();
        // MessageContext synCtx = TestUtils.getTestContextForXSLTMediator(SOURCE, props);
        transformMediator.mediate(synCtx);
    // System.out.println("done : " + i + " :: " + Runtime.getRuntime().freeMemory());
    }
}
Also used : TestMessageContextBuilder(org.apache.synapse.TestMessageContextBuilder) MessageContext(org.apache.synapse.MessageContext)

Example 27 with MessageContext

use of org.apache.synapse.MessageContext in project wso2-synapse by wso2.

the class XSLTMediatorTest method testMultipleKeys.

/**
 * Test with multiple keys including static and dynamic keys
 *
 * @param num number from 0 to 2
 * @throws Exception Exception in case of an error in tests
 */
private void testMultipleKeys(int num) throws Exception {
    String xsltKeyValue = null;
    String path;
    SynapseXPath xpath;
    Value xsltKey;
    XSLTMediator transformMediator = new XSLTMediator();
    // default source, xsltFile, and state of key (dynamic or static)
    String source = "";
    String xsltFile = "";
    boolean isDynamicKey = true;
    // based on source, different XSLTFiles can be used
    if (num == 0) {
        source = SOURCE_STATIC_KEY;
        xsltFile = "static_key.xslt";
        xsltKeyValue = "StaticXsltKey";
        isDynamicKey = false;
    } else if (num == 1) {
        source = SOURCE_DYNAMIC_KEY1;
        xsltFile = "dynamic_key_1.xslt";
        xsltKeyValue = "DynamicXsltKey1";
        isDynamicKey = true;
    } else if (num == 2) {
        source = SOURCE_DYNAMIC_KEY2;
        xsltFile = "dynamic_key_2.xslt";
        xsltKeyValue = "DynamicXsltKey2";
        isDynamicKey = true;
    }
    if (isDynamicKey) {
        path = "//m0:CheckPriceRequest/m0:" + xsltKeyValue;
        xpath = new SynapseXPath(path);
        xpath.addNamespace("m0", "http://services.samples/xsd");
        // Create key from dynamic key (xpath)
        xsltKey = new Value(xpath);
        // set XSLT transformation URL (Xpath)
        transformMediator.setXsltKey(xsltKey);
    } else {
        // static key
        path = xsltKeyValue;
        // set XSLT transformation URL (static)
        setXsltTransformationURL(transformMediator, path);
    }
    // Mediate twice for synCtx
    MessageContext synCtx = new TestMessageContextBuilder().addEntry(xsltKeyValue, getClass().getResource(xsltFile)).setBodyFromString(source).addTextAroundBody().build();
    transformMediator.mediate(synCtx);
    synCtx = new TestMessageContextBuilder().addEntry(xsltKeyValue, getClass().getResource(xsltFile)).setBodyFromString(source).addTextAroundBody().build();
    transformMediator.mediate(synCtx);
}
Also used : SynapseXPath(org.apache.synapse.util.xpath.SynapseXPath) TestMessageContextBuilder(org.apache.synapse.TestMessageContextBuilder) Value(org.apache.synapse.mediators.Value) MessageContext(org.apache.synapse.MessageContext)

Example 28 with MessageContext

use of org.apache.synapse.MessageContext in project wso2-synapse by wso2.

the class XSLTMediatorTest method testTransformXSLTLargeMessagesCSV.

public void testTransformXSLTLargeMessagesCSV() throws Exception {
    // create a new switch mediator
    XSLTMediator transformMediator = new XSLTMediator();
    // set XSLT transformation URL
    setXsltTransformationURL(transformMediator, "xslt-key");
    for (int i = 0; i < 2; i++) {
        // invoke transformation, with static enveope
        MessageContext synCtx = new TestMessageContextBuilder().addFileEntry("xslt-key", "../../repository/conf/sample/resources/transform/transform_load.xml").setBodyFromFile("../../repository/conf/sample/resources/transform/message.xml").addTextAroundBody().build();
        // MessageContext synCtx = TestUtils.getTestContextForXSLTMediator(SOURCE, props);
        transformMediator.mediate(synCtx);
    // synCtx.getEnvelope().serializeAndConsume(new FileOutputStream("/tmp/out.xml"));
    // System.gc();
    // System.out.println("done : " + i + " :: " + Runtime.getRuntime().freeMemory());
    }
}
Also used : TestMessageContextBuilder(org.apache.synapse.TestMessageContextBuilder) MessageContext(org.apache.synapse.MessageContext)

Example 29 with MessageContext

use of org.apache.synapse.MessageContext in project wso2-synapse by wso2.

the class XSLTMediatorTest method testTextEncoding.

public void testTextEncoding() throws Exception {
    XSLTMediator transformMediator = new XSLTMediator();
    setXsltTransformationURL(transformMediator, "xslt-key");
    MessageContext mc = new TestMessageContextBuilder().addFileEntry("xslt-key", "../../repository/conf/sample/resources/transform/encoding_test.xslt").setEnvelopeFromFile("../../repository/conf/sample/resources/transform" + "/encoding_test.xml").build();
    transformMediator.mediate(mc);
    OMElement resultElement = mc.getEnvelope().getBody().getFirstElement();
    assertEquals(BaseConstants.DEFAULT_TEXT_WRAPPER, resultElement.getQName());
    assertEquals("\u00e0 peine arriv\u00e9s nous entr\u00e2mes dans sa chambre", resultElement.getText());
}
Also used : TestMessageContextBuilder(org.apache.synapse.TestMessageContextBuilder) MessageContext(org.apache.synapse.MessageContext)

Example 30 with MessageContext

use of org.apache.synapse.MessageContext in project wso2-synapse by wso2.

the class XSLTMediatorTest method testSynapse242.

public void testSynapse242() throws Exception {
    // create a new switch mediator
    XSLTMediator transformMediator = new XSLTMediator();
    // set XSLT transformation URL
    setXsltTransformationURL(transformMediator, "xslt-key");
    // invoke transformation, with static enveope
    MessageContext synCtx = new TestMessageContextBuilder().addFileEntry("xslt-key", "../../repository/conf/sample/resources/transform/transform_load_2.xml").setBodyFromFile("../../repository/conf/sample/resources/transform/med_message.xml").addTextAroundBody().build();
    transformMediator.mediate(synCtx);
    // validate result
    OMContainer body = synCtx.getEnvelope().getBody();
    assertTrue(body.getFirstOMChild().getNextOMSibling() instanceof OMElement);
    assertTrue(((OMElement) body.getFirstOMChild().getNextOMSibling()).getText().length() > 0);
}
Also used : TestMessageContextBuilder(org.apache.synapse.TestMessageContextBuilder) MessageContext(org.apache.synapse.MessageContext)

Aggregations

MessageContext (org.apache.synapse.MessageContext)220 Axis2MessageContext (org.apache.synapse.core.axis2.Axis2MessageContext)86 SynapseConfiguration (org.apache.synapse.config.SynapseConfiguration)54 SynapseException (org.apache.synapse.SynapseException)29 TestMessageContextBuilder (org.apache.synapse.TestMessageContextBuilder)26 ArrayList (java.util.ArrayList)24 Axis2SynapseEnvironment (org.apache.synapse.core.axis2.Axis2SynapseEnvironment)24 SynapseXPath (org.apache.synapse.util.xpath.SynapseXPath)24 ConfigurationContext (org.apache.axis2.context.ConfigurationContext)18 SynapseEnvironment (org.apache.synapse.core.SynapseEnvironment)18 OMElement (org.apache.axiom.om.OMElement)17 AxisConfiguration (org.apache.axis2.engine.AxisConfiguration)17 HashMap (java.util.HashMap)16 Mediator (org.apache.synapse.Mediator)16 TestMessageContext (org.apache.synapse.TestMessageContext)16 Map (java.util.Map)15 Properties (java.util.Properties)15 Test (org.junit.Test)15 SOAPEnvelope (org.apache.axiom.soap.SOAPEnvelope)14 QName (javax.xml.namespace.QName)13