Search in sources :

Example 16 with SynapseXPath

use of org.apache.synapse.util.xpath.SynapseXPath in project wso2-synapse by wso2.

the class PayloadFactoryMediatorTest method testWithExpressionsAsArguments.

/**
 * Test payloadFactory Mediator with dynamic expressions set
 * @throws Exception in case of argument evaluation issue
 */
public void testWithExpressionsAsArguments() throws Exception {
    PayloadFactoryMediator payloadFactoryMediator = new PayloadFactoryMediator();
    payloadFactoryMediator.setFormat(format);
    // prepare arguments
    Argument argument1 = new Argument();
    argument1.setExpression(new SynapseXPath("//name"));
    Argument argument2 = new Argument();
    argument2.setExpression(new SynapseXPath("get-property('SYSTEM_DATE', 'yyyy.MM.dd')"));
    Argument argument3 = new Argument();
    argument3.setExpression(new SynapseXPath("//tpNumber"));
    Argument argument4 = new Argument();
    argument4.setExpression(new SynapseXPath("//address"));
    // add arguments
    payloadFactoryMediator.addPathArgument(argument1);
    payloadFactoryMediator.addPathArgument(argument2);
    payloadFactoryMediator.addPathArgument(argument3);
    payloadFactoryMediator.addPathArgument(argument4);
    // do mediation
    MessageContext synCtx = TestUtils.getAxis2MessageContext(inputPayload, null);
    payloadFactoryMediator.mediate(synCtx);
    String expectedEnvelope = "<soapenv:Body xmlns:soapenv=\"http://schemas.xmlsoap" + ".org/soap/envelope/\"><p:addCustomer xmlns:p=\"http://ws.wso2.org/dataservice\">\n" + " <xs:name xmlns:xs=\"http://ws.wso2.org/dataservice\">Smith</xs:name>\n" + " <xs:request_time xmlns:xs=\"http://ws.wso2.org/dataservice\">" + new SimpleDateFormat("yyyy.MM.dd").format(Calendar.getInstance().getTime()) + "</xs:request_time>\n" + " <xs:tp_number xmlns:xs=\"http://ws.wso2.org/dataservice\">0834558649</xs:tp_number>\n" + " <xs:address xmlns:xs=\"http://ws.wso2.org/dataservice\">No. 456, Gregory Road, Los " + "Angeles</xs:address>\n" + " </p:addCustomer></soapenv:Body>";
    assertEquals("PayloadFactory mediator has not " + "set expected format", expectedEnvelope, synCtx.getEnvelope().getBody().toString());
}
Also used : SynapseXPath(org.apache.synapse.util.xpath.SynapseXPath) MessageContext(org.apache.synapse.MessageContext) SimpleDateFormat(java.text.SimpleDateFormat)

Example 17 with SynapseXPath

use of org.apache.synapse.util.xpath.SynapseXPath in project wso2-synapse by wso2.

the class SynapseXPathTest method testStringXPath2.

public void testStringXPath2() throws Exception {
    SynapseXPath xpath = SynapseXPath.parseXPathString("$body//{http://somens}test/{http://someother}another");
    MessageContext ctx = TestUtils.getTestContext("<m0:test xmlns:m0=\"http://somens\"><m1:another xmlns:m1=\"http://someother\">" + message + "</m1:another></m0:test>");
    assertEquals(message, xpath.stringValueOf(ctx));
}
Also used : SynapseXPath(org.apache.synapse.util.xpath.SynapseXPath) TestMessageContext(org.apache.synapse.TestMessageContext) MessageContext(org.apache.synapse.MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext)

Example 18 with SynapseXPath

use of org.apache.synapse.util.xpath.SynapseXPath in project wso2-synapse by wso2.

the class SynapseXPathTest method testCustomVariables.

public void testCustomVariables() throws Exception {
    SynapseXPath xpath = new SynapseXPath("$myvar");
    SimpleVariableContext variableContext = new SimpleVariableContext();
    variableContext.setVariableValue("myvar", "myvalue");
    xpath.setVariableContext(variableContext);
    assertEquals("myvalue", xpath.evaluate(TestUtils.getTestContext("<test/>")));
}
Also used : SynapseXPath(org.apache.synapse.util.xpath.SynapseXPath) SimpleVariableContext(org.jaxen.SimpleVariableContext)

Example 19 with SynapseXPath

use of org.apache.synapse.util.xpath.SynapseXPath in project wso2-synapse by wso2.

the class SynapseXPathTest method testBodyRelativeXPath.

public void testBodyRelativeXPath() throws Exception {
    SynapseXPath xpath = new SynapseXPath("$body/test");
    MessageContext ctx = TestUtils.getTestContext("<test>" + message + "</test>");
    assertEquals(message, xpath.stringValueOf(ctx));
    Object node = xpath.selectSingleNode(ctx);
    assertTrue(node instanceof OMElement);
    assertEquals(message, ((OMElement) node).getText());
}
Also used : SynapseXPath(org.apache.synapse.util.xpath.SynapseXPath) OMElement(org.apache.axiom.om.OMElement) TestMessageContext(org.apache.synapse.TestMessageContext) MessageContext(org.apache.synapse.MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext)

Example 20 with SynapseXPath

use of org.apache.synapse.util.xpath.SynapseXPath in project wso2-synapse by wso2.

the class SynapseXPathTest method testTransportHeaders.

public void testTransportHeaders() throws Exception {
    Axis2MessageContext synCtx = TestUtils.getAxis2MessageContext("<test/>", null);
    org.apache.axis2.context.MessageContext axis2MessageCtx = synCtx.getAxis2MessageContext();
    Object headers = axis2MessageCtx.getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);
    String name = "MyHeader";
    String resultValue = "TestValue";
    if (headers != null && headers instanceof Map) {
        Map headersMap = (Map) headers;
        headersMap.put(name, resultValue);
    }
    if (headers == null) {
        Map headersMap = new HashMap();
        headersMap.put(name, resultValue);
        axis2MessageCtx.setProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS, headersMap);
    }
    assertEquals(resultValue, new SynapseXPath("$trp:" + name).evaluate(synCtx));
}
Also used : SynapseXPath(org.apache.synapse.util.xpath.SynapseXPath) HashMap(java.util.HashMap) Map(java.util.Map) HashMap(java.util.HashMap) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext)

Aggregations

SynapseXPath (org.apache.synapse.util.xpath.SynapseXPath)68 MessageContext (org.apache.synapse.MessageContext)24 JaxenException (org.jaxen.JaxenException)20 Axis2MessageContext (org.apache.synapse.core.axis2.Axis2MessageContext)15 OMElement (org.apache.axiom.om.OMElement)14 OMAttribute (org.apache.axiom.om.OMAttribute)11 Value (org.apache.synapse.mediators.Value)9 TestMessageContext (org.apache.synapse.TestMessageContext)8 Pattern (java.util.regex.Pattern)6 Iterator (java.util.Iterator)5 QName (javax.xml.namespace.QName)5 EndpointReference (org.apache.axis2.addressing.EndpointReference)4 SynapseException (org.apache.synapse.SynapseException)4 TestMessageContextBuilder (org.apache.synapse.TestMessageContextBuilder)4 SynapseJsonPath (org.apache.synapse.util.xpath.SynapseJsonPath)4 Method (java.lang.reflect.Method)3 Map (java.util.Map)3 MediatorProperty (org.apache.synapse.mediators.MediatorProperty)3 Field (java.lang.reflect.Field)2 List (java.util.List)2