Search in sources :

Example 71 with MessageContext

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

the class HeaderMediatorTest method testSimpleHeaderXPathSetAndRemove.

public void testSimpleHeaderXPathSetAndRemove() throws Exception {
    HeaderMediator headerMediator = new HeaderMediator();
    headerMediator.setQName(new QName(SynapseConstants.HEADER_TO));
    headerMediator.setExpression(new SynapseXPath("concat('http://','server','/path')"));
    // invoke transformation, with static enveope
    MessageContext synCtx = TestUtils.getTestContext("<empty/>");
    headerMediator.mediate(synCtx);
    assertTrue(TEST_HEADER.equals(synCtx.getTo().getAddress()));
    // set the header mediator as a remove-header
    headerMediator.setAction(HeaderMediator.ACTION_REMOVE);
    headerMediator.mediate(synCtx);
    assertTrue(synCtx.getTo() == null);
}
Also used : SynapseXPath(org.apache.synapse.util.xpath.SynapseXPath) QName(javax.xml.namespace.QName) MessageContext(org.apache.synapse.MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext)

Example 72 with MessageContext

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

the class PayloadFactoryMediatorTest method testWithStaticArguments.

/**
 * Test payloadFactory Mediator with static arguments set
 * @throws Exception in case of argument evaluation issue
 */
public void testWithStaticArguments() throws Exception {
    PayloadFactoryMediator payloadFactoryMediator = new PayloadFactoryMediator();
    payloadFactoryMediator.setFormat(format);
    // prepare arguments
    Argument argument1 = new Argument();
    argument1.setValue("John");
    Argument argument2 = new Argument();
    argument2.setValue("2017.09.26");
    Argument argument3 = new Argument();
    argument3.setValue("1234564632");
    Argument argument4 = new Argument();
    argument4.setValue("Colombo, Sri Lanka");
    // 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 expectedEnv = "<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\">John</xs:name>\n" + " <xs:request_time xmlns:xs=\"http://ws.wso2.org/dataservice\">2017.09.26</xs:request_time>\n" + " <xs:tp_number xmlns:xs=\"http://ws.wso2.org/dataservice\">1234564632</xs:tp_number>\n" + " <xs:address xmlns:xs=\"http://ws.wso2.org/dataservice\">Colombo, Sri Lanka</xs:address>\n" + " </p:addCustomer></soapenv:Body>";
    assertEquals("PayloadFactory mediator has not " + "set expected format", expectedEnv, synCtx.getEnvelope().getBody().toString());
}
Also used : MessageContext(org.apache.synapse.MessageContext)

Example 73 with MessageContext

use of org.apache.synapse.MessageContext 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 74 with MessageContext

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

the class URLMappingBasedDispatcherTest method testPathBasedDispatch.

public void testPathBasedDispatch() throws Exception {
    API api = new API("TestAPI", "/test");
    Resource resource = new Resource();
    resource.setDispatcherHelper(new URLMappingHelper("/foo/bar/*"));
    resource.setInSequence(getTestSequence(PROP_NAME, PROP_VALUE));
    api.addResource(resource);
    SynapseConfiguration synapseConfig = new SynapseConfiguration();
    synapseConfig.addAPI(api.getName(), api);
    RESTRequestHandler handler = new RESTRequestHandler();
    MessageContext synCtx = getMessageContext(synapseConfig, false, "/test/foo/bar", "GET");
    handler.process(synCtx);
    assertEquals(PROP_VALUE, synCtx.getProperty(PROP_NAME));
    synCtx = getMessageContext(synapseConfig, false, "/test/foo/bar/", "GET");
    handler.process(synCtx);
    assertEquals(PROP_VALUE, synCtx.getProperty(PROP_NAME));
    synCtx = getMessageContext(synapseConfig, false, "/test/foo/bar/index.jsp", "GET");
    handler.process(synCtx);
    assertEquals(PROP_VALUE, synCtx.getProperty(PROP_NAME));
    synCtx = getMessageContext(synapseConfig, false, "/test/foo/bar?a=b", "GET");
    handler.process(synCtx);
    assertEquals(PROP_VALUE, synCtx.getProperty(PROP_NAME));
    synCtx = getMessageContext(synapseConfig, false, "/test/foo/bar/baz?a=b", "GET");
    handler.process(synCtx);
    assertEquals(PROP_VALUE, synCtx.getProperty(PROP_NAME));
    synCtx = getMessageContext(synapseConfig, false, "/test/foo/bar/?a=b", "GET");
    handler.process(synCtx);
    assertEquals(PROP_VALUE, synCtx.getProperty(PROP_NAME));
    synCtx = getMessageContext(synapseConfig, false, "/test/foo/bars?a=b", "GET");
    handler.process(synCtx);
    assertNull(synCtx.getProperty(PROP_NAME));
    synCtx = getMessageContext(synapseConfig, false, "/test/foo/", "GET");
    handler.process(synCtx);
    assertNull(synCtx.getProperty(PROP_NAME));
}
Also used : URLMappingHelper(org.apache.synapse.rest.dispatch.URLMappingHelper) MessageContext(org.apache.synapse.MessageContext) SynapseConfiguration(org.apache.synapse.config.SynapseConfiguration)

Example 75 with MessageContext

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

the class URLMappingBasedDispatcherTest method testDefaultResourceDispatch.

public void testDefaultResourceDispatch() throws Exception {
    API api = new API("TestAPI", "/test");
    Resource resource = new Resource();
    resource.setDispatcherHelper(new URLMappingHelper("/"));
    resource.setInSequence(getTestSequence(PROP_NAME, PROP_VALUE));
    api.addResource(resource);
    SynapseConfiguration synapseConfig = new SynapseConfiguration();
    synapseConfig.addAPI(api.getName(), api);
    RESTRequestHandler handler = new RESTRequestHandler();
    MessageContext synCtx = getMessageContext(synapseConfig, false, "/test", "GET");
    handler.process(synCtx);
    assertEquals(PROP_VALUE, synCtx.getProperty(PROP_NAME));
}
Also used : URLMappingHelper(org.apache.synapse.rest.dispatch.URLMappingHelper) MessageContext(org.apache.synapse.MessageContext) SynapseConfiguration(org.apache.synapse.config.SynapseConfiguration)

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