Search in sources :

Example 1 with TestMessageContextBuilder

use of org.apache.synapse.TestMessageContextBuilder 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 2 with TestMessageContextBuilder

use of org.apache.synapse.TestMessageContextBuilder 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 3 with TestMessageContextBuilder

use of org.apache.synapse.TestMessageContextBuilder 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 4 with TestMessageContextBuilder

use of org.apache.synapse.TestMessageContextBuilder 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 5 with TestMessageContextBuilder

use of org.apache.synapse.TestMessageContextBuilder 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

TestMessageContextBuilder (org.apache.synapse.TestMessageContextBuilder)29 MessageContext (org.apache.synapse.MessageContext)26 SynapseXPath (org.apache.synapse.util.xpath.SynapseXPath)4 Map (java.util.Map)3 Entry (org.apache.synapse.config.Entry)3 QName (javax.xml.namespace.QName)2 Value (org.apache.synapse.mediators.Value)2 SynapseException (org.apache.synapse.SynapseException)1 Axis2MessageContext (org.apache.synapse.core.axis2.Axis2MessageContext)1