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);
}
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());
}
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());
}
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));
}
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));
}
Aggregations