Search in sources :

Example 56 with MessageContext

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...");
}
Also used : AddressEndpoint(org.apache.synapse.endpoints.AddressEndpoint) Endpoint(org.apache.synapse.endpoints.Endpoint) AddressEndpoint(org.apache.synapse.endpoints.AddressEndpoint) MessageContext(org.apache.synapse.MessageContext)

Example 57 with MessageContext

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));
}
Also used : MessageContext(org.apache.synapse.MessageContext) SynapseConfiguration(org.apache.synapse.config.SynapseConfiguration)

Example 58 with MessageContext

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));
}
Also used : MessageContext(org.apache.synapse.MessageContext) SynapseConfiguration(org.apache.synapse.config.SynapseConfiguration)

Example 59 with MessageContext

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));
}
Also used : ContextVersionStrategy(org.apache.synapse.rest.version.ContextVersionStrategy) MessageContext(org.apache.synapse.MessageContext) SynapseConfiguration(org.apache.synapse.config.SynapseConfiguration)

Example 60 with MessageContext

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));
}
Also used : 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