use of org.apache.synapse.MessageContext in project wso2-synapse by wso2.
the class URITemplateBasedDispatcherTest method testDefaultDispatch.
public void testDefaultDispatch() throws Exception {
API api = new API("TestAPI", "/test");
Resource resource = new Resource();
resource.setDispatcherHelper(new URITemplateHelper("/"));
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));
}
use of org.apache.synapse.MessageContext in project wso2-synapse by wso2.
the class URITemplateBasedDispatcherTest method testBasicTemplateDispatch2.
public void testBasicTemplateDispatch2() throws Exception {
API api = new API("TestAPI", "/");
Resource resource = new Resource();
resource.setDispatcherHelper(new URITemplateHelper("/dictionary/{char}/{word}"));
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, "/dictionary/c/cat", "GET");
handler.process(synCtx);
assertEquals(PROP_VALUE, synCtx.getProperty(PROP_NAME));
assertEquals("c", synCtx.getProperty(RESTConstants.REST_URI_VARIABLE_PREFIX + "char"));
assertEquals("cat", synCtx.getProperty(RESTConstants.REST_URI_VARIABLE_PREFIX + "word"));
synCtx = getMessageContext(synapseConfig, false, "/dictionary/d/dog/", "GET");
handler.process(synCtx);
assertEquals(PROP_VALUE, synCtx.getProperty(PROP_NAME));
assertEquals("d", synCtx.getProperty(RESTConstants.REST_URI_VARIABLE_PREFIX + "char"));
assertEquals("dog", synCtx.getProperty(RESTConstants.REST_URI_VARIABLE_PREFIX + "word"));
synCtx = getMessageContext(synapseConfig, false, "/test/c/cat", "GET");
handler.process(synCtx);
assertNull(synCtx.getProperty(PROP_NAME));
synCtx = getMessageContext(synapseConfig, false, "/dictionary/c", "GET");
handler.process(synCtx);
assertNull(synCtx.getProperty(PROP_NAME));
/* With ESBJAVA-4260 we now support optional query parameters for URITemplates */
synCtx = getMessageContext(synapseConfig, false, "/dictionary/c/cat?a=5", "GET");
handler.process(synCtx);
assertEquals(PROP_VALUE, synCtx.getProperty(PROP_NAME));
assertEquals("c", synCtx.getProperty(RESTConstants.REST_URI_VARIABLE_PREFIX + "char"));
assertEquals("cat", synCtx.getProperty(RESTConstants.REST_URI_VARIABLE_PREFIX + "word"));
}
use of org.apache.synapse.MessageContext in project wso2-synapse by wso2.
the class SwitchMediatorTest method setUp.
public void setUp() throws Exception {
ibmMediator = new TestMediator();
ibmMediator.setHandler(new TestMediateHandler() {
public void handle(MessageContext synCtx) {
setExecutedCase("IBM");
}
});
msftMediator = new TestMediator();
msftMediator.setHandler(new TestMediateHandler() {
public void handle(MessageContext synCtx) {
setExecutedCase("MSFT");
}
});
defaultMediator = new TestMediator();
defaultMediator.setHandler(new TestMediateHandler() {
public void handle(MessageContext synCtx) {
setExecutedCase("DEFAULT");
}
});
// create a new switch mediator
switchMediator = new SwitchMediator();
// set xpath condition to select symbol
SynapseXPath xpath = new SynapseXPath("//wsx:symbol");
xpath.addNamespace("wsx", "http://www.webserviceX.NET/");
switchMediator.setSource(xpath);
SwitchCase caseOne = new SwitchCase();
caseOne.setRegex(Pattern.compile("IBM"));
AnonymousListMediator mediatorOne = new AnonymousListMediator();
mediatorOne.addAll(Arrays.asList(new Mediator[] { ibmMediator }));
caseOne.setCaseMediator(mediatorOne);
SwitchCase caseTwo = new SwitchCase();
caseTwo.setRegex(Pattern.compile("MSFT"));
AnonymousListMediator mediatorTwo = new AnonymousListMediator();
mediatorTwo.addAll(Arrays.asList(new Mediator[] { msftMediator }));
caseTwo.setCaseMediator(mediatorTwo);
SwitchCase caseDefault = new SwitchCase();
AnonymousListMediator mediatorDefault = new AnonymousListMediator();
mediatorDefault.addAll(Arrays.asList(new Mediator[] { defaultMediator }));
caseDefault.setCaseMediator(mediatorDefault);
// set ibm mediator to be called for IBM, msft for MSFT and default for others..
switchMediator.addCase(caseOne);
switchMediator.addCase(caseTwo);
switchMediator.setDefaultCase(caseDefault);
}
use of org.apache.synapse.MessageContext in project wso2-synapse by wso2.
the class FaultMediatorTest method testSOAP11Fault.
public void testSOAP11Fault() throws Exception {
FaultMediator faultMediator = new FaultMediator();
faultMediator.setSoapVersion(FaultMediator.SOAP11);
faultMediator.setFaultCodeValue(F_CODE);
faultMediator.setFaultReasonValue(F_STRING);
faultMediator.setFaultRole(new URI(F_ACTOR_URI));
faultMediator.setFaultDetail(F_DETAIL);
// invoke transformation, with static enveope
MessageContext synCtx = TestUtils.getAxis2MessageContext("<empty/>", new HashMap<String, Entry>());
faultMediator.mediate(synCtx);
SOAPEnvelope envelope = synCtx.getEnvelope();
SOAPFault fault = envelope.getBody().getFault();
assertTrue(F_CODE.equals(fault.getCode().getTextAsQName()));
assertTrue(F_STRING.equals(fault.getReason().getText()));
assertTrue(F_ACTOR_URI.equals(fault.getRole().getRoleValue()));
assertTrue(F_DETAIL.equals(fault.getDetail().getText()));
assertEquals(SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI, envelope.getNamespace().getNamespaceURI());
}
use of org.apache.synapse.MessageContext in project wso2-synapse by wso2.
the class HeaderMediatorTest method testEmbeddedXml.
public void testEmbeddedXml() throws Exception {
String simpleHeader = "<header name=\"m:simpleHeader\" value=\"Simple Header\" xmlns:m=\"http://org.synapse.example\"/>";
String complexHeader = "<header><m:complexHeader xmlns:m=\"http://org.synapse.example\"><property key=\"k1\" value=\"v1\"/><property key=\"k2\" value=\"v2\"/></m:complexHeader></header>";
String removeHeader = "<header name=\"m:complexHeader\" action=\"remove\" xmlns:m=\"http://org.synapse.example\"/>";
HeaderMediatorFactory fac = new HeaderMediatorFactory();
try {
// Adding headers.
MessageContext synCtx = TestUtils.getTestContext("<empty/>");
HeaderMediator headerMediator = (HeaderMediator) fac.createMediator(AXIOMUtil.stringToOM(simpleHeader), new Properties());
headerMediator.mediate(synCtx);
headerMediator = (HeaderMediator) fac.createMediator(AXIOMUtil.stringToOM(complexHeader), new Properties());
headerMediator.mediate(synCtx);
// Removing headers.
headerMediator = (HeaderMediator) fac.createMediator(AXIOMUtil.stringToOM(removeHeader), new Properties());
headerMediator.mediate(synCtx);
} catch (XMLStreamException e) {
fail("Error while parsing header mediator configuration: inline XML scenario. " + e.getMessage());
}
}
Aggregations