use of org.apache.synapse.MessageContext in project wso2-synapse by wso2.
the class DynamicResourceTest method testDynamicEndpointLookup.
public void testDynamicEndpointLookup() throws Exception {
System.out.println("Testing dynamic endpoint lookup...");
// Phase 1
System.out.println("Testing basic registry lookup functionality...");
MessageContext synCtx = TestUtils.createSynapseMessageContext("<empty/>", config);
Endpoint ep1 = synCtx.getEndpoint(KEY_DYNAMIC_ENDPOINT_1);
assertNotNull(ep1);
assertTrue(ep1.isInitialized());
assertEquals(1, registry.getHitCount());
assertEquals("http://test.url", ((AddressEndpoint) ep1).getDefinition().getAddress());
// Phase 2
System.out.println("Testing basic endpoint caching...");
synCtx = TestUtils.createSynapseMessageContext("<empty/>", config);
Endpoint ep2 = synCtx.getEndpoint(KEY_DYNAMIC_ENDPOINT_1);
assertNotNull(ep2);
assertEquals(1, registry.getHitCount());
assertTrue(ep1 == ep2);
// Phase 3
System.out.println("Testing advanced endpoint caching...");
synCtx = TestUtils.createSynapseMessageContext("<empty/>", config);
System.out.println("Waiting for the cache to expire...");
Thread.sleep(8500L);
Endpoint ep3 = synCtx.getEndpoint(KEY_DYNAMIC_ENDPOINT_1);
assertNotNull(ep3);
assertEquals(1, registry.getHitCount());
assertTrue(ep1 == ep3);
// Phase 4
System.out.println("Testing endpoint reloading...");
registry.updateResource(KEY_DYNAMIC_ENDPOINT_1, TestUtils.createOMElement(DYNAMIC_ENDPOINT_2));
System.out.println("Waiting for the cache to expire...");
Thread.sleep(8500L);
synCtx = TestUtils.createSynapseMessageContext("<empty/>", config);
Endpoint ep4 = synCtx.getEndpoint(KEY_DYNAMIC_ENDPOINT_1);
assertNotNull(ep4);
assertTrue(ep4.isInitialized());
assertEquals(2, registry.getHitCount());
assertEquals("http://test2.url", ((AddressEndpoint) ep4).getDefinition().getAddress());
assertTrue(ep1 != ep4);
assertTrue(!ep1.isInitialized());
// Phase 5
System.out.println("Testing for non-existing endpoints...");
synCtx = TestUtils.createSynapseMessageContext("<empty/>", config);
Endpoint ep5 = synCtx.getEndpoint("non-existing-endpoint");
assertNull(ep5);
System.out.println("Dynamic endpoint lookup tests were successful...");
}
use of org.apache.synapse.MessageContext in project wso2-synapse by wso2.
the class APIDispatcherTest method testGeneralAPIDispatch.
public void testGeneralAPIDispatch() throws Exception {
API api = new API(TEST_API, "/");
SynapseConfiguration synapseConfig = new SynapseConfiguration();
synapseConfig.addAPI(TEST_API, api);
RESTRequestHandler handler = new RESTRequestHandler();
MessageContext synCtx = getMessageContext(synapseConfig, false, "/test", "GET");
handler.process(synCtx);
assertEquals(TEST_API, synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
synCtx = getMessageContext(synapseConfig, false, "/", "GET");
handler.process(synCtx);
assertEquals(TEST_API, synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
synCtx = getMessageContext(synapseConfig, false, "/foo/bar?a=5", "GET");
handler.process(synCtx);
assertEquals(TEST_API, synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
}
use of org.apache.synapse.MessageContext in project wso2-synapse by wso2.
the class APIDispatcherTest method testResponseDispatch.
public void testResponseDispatch() throws Exception {
API api = new API(TEST_API, "/test");
SynapseConfiguration synapseConfig = new SynapseConfiguration();
synapseConfig.addAPI(TEST_API, api);
RESTRequestHandler handler = new RESTRequestHandler();
// Messages with '/test' context should ne dispatched
MessageContext synCtx = getMessageContext(synapseConfig, false, "/test", "GET");
synCtx.setResponse(true);
assertFalse(handler.process(synCtx));
synCtx.setProperty(RESTConstants.SYNAPSE_REST_API, TEST_API);
assertTrue(handler.process(synCtx));
}
use of org.apache.synapse.MessageContext in project wso2-synapse by wso2.
the class APIDispatcherTest method testAPIContextVersionBasedDispatchEndingWithVersion.
public void testAPIContextVersionBasedDispatchEndingWithVersion() throws Exception {
API api = new API(TEST_API, "/test/{version}");
api.setVersionStrategy(new ContextVersionStrategy(api, TEST_API_VERSION, null));
SynapseConfiguration synapseConfig = new SynapseConfiguration();
synapseConfig.addAPI(api.getName(), api);
RESTRequestHandler handler = new RESTRequestHandler();
// Messages with '/test' context should NOT be dispatched
MessageContext synCtx = getMessageContext(synapseConfig, false, "/test/", "GET");
handler.process(synCtx);
assertNull(synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
assertNull(synCtx.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION));
synCtx = getMessageContext(synapseConfig, false, "/test/1.0.0", "GET");
handler.process(synCtx);
assertEquals(api.getName(), synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
assertEquals(TEST_API_VERSION, synCtx.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION));
synCtx = getMessageContext(synapseConfig, false, "/test/1.0.0/", "GET");
handler.process(synCtx);
assertEquals(api.getName(), synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
assertEquals(TEST_API_VERSION, synCtx.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION));
synCtx = getMessageContext(synapseConfig, false, "/test/1.0.0/foo/bar?a=5", "GET");
handler.process(synCtx);
assertEquals(api.getName(), synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
assertEquals(TEST_API_VERSION, synCtx.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION));
synCtx = getMessageContext(synapseConfig, false, "/test/1.0.0?a=5", "GET");
handler.process(synCtx);
assertEquals(api.getName(), synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
assertEquals(TEST_API_VERSION, synCtx.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION));
// Message with '/test' context & URL as a Query Parameter should be dispatched
synCtx = getMessageContext(synapseConfig, false, "/test/1.0.0?a=http://localhost.com", "GET");
handler.process(synCtx);
assertEquals(api.getName(), synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
assertEquals(TEST_API_VERSION, synCtx.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION));
// Messages WITHOUT the '/test' context should NOT be dispatched
synCtx = getMessageContext(synapseConfig, false, "/foo/test/bar?a=5", "GET");
handler.process(synCtx);
assertNull(synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
assertNull(synCtx.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION));
// Messages WITHOUT the '/test' context and proper version should NOT be dispatched
synCtx = getMessageContext(synapseConfig, false, "/test/1.0.1/foo/bar?a=5", "GET");
handler.process(synCtx);
assertNull(synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
assertNull(synCtx.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION));
synCtx = getMessageContext(synapseConfig, false, "/test/2.0/foo/bar?a=5", "GET");
handler.process(synCtx);
assertNull(synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
assertNull(synCtx.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION));
synCtx = getMessageContext(synapseConfig, false, "/test/2.0.0.0/foo/bar?a=5", "GET");
handler.process(synCtx);
assertNull(synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
assertNull(synCtx.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION));
}
use of org.apache.synapse.MessageContext in project wso2-synapse by wso2.
the class APIDispatcherTest method testBasicAPIDispatch.
public void testBasicAPIDispatch() throws Exception {
API api = new API(TEST_API, "/test");
SynapseConfiguration synapseConfig = new SynapseConfiguration();
synapseConfig.addAPI(TEST_API, api);
RESTRequestHandler handler = new RESTRequestHandler();
// Messages with '/test' context should be dispatched
MessageContext synCtx = getMessageContext(synapseConfig, false, "/test", "GET");
handler.process(synCtx);
assertEquals(TEST_API, synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
synCtx = getMessageContext(synapseConfig, false, "/test/", "GET");
handler.process(synCtx);
assertEquals(TEST_API, synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
synCtx = getMessageContext(synapseConfig, false, "/test/foo/bar?a=5", "GET");
handler.process(synCtx);
assertEquals(TEST_API, synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
synCtx = getMessageContext(synapseConfig, false, "/test?a=5", "GET");
handler.process(synCtx);
assertEquals(TEST_API, synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
// Messages WITHOUT the '/test' context should NOT be dispatched
synCtx = getMessageContext(synapseConfig, false, "/foo/test/bar?a=5", "GET");
handler.process(synCtx);
assertNull(synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
synCtx = getMessageContext(synapseConfig, false, "/test1/bar?a=5", "GET");
handler.process(synCtx);
assertNull(synCtx.getProperty(RESTConstants.SYNAPSE_REST_API));
}
Aggregations