Search in sources :

Example 1 with MockServlet

use of org.apache.knox.test.mock.MockServlet in project knox by apache.

the class ServiceRegistryFunctionsTest method setUp.

public void setUp(String username, Map<String, String> initParams) throws Exception {
    ServiceRegistry mockServiceRegistry = EasyMock.createNiceMock(ServiceRegistry.class);
    EasyMock.expect(mockServiceRegistry.lookupServiceURL("test-cluster", "NAMENODE")).andReturn("test-nn-scheme://test-nn-host:411").anyTimes();
    EasyMock.expect(mockServiceRegistry.lookupServiceURL("test-cluster", "JOBTRACKER")).andReturn("test-jt-scheme://test-jt-host:511").anyTimes();
    GatewayServices mockGatewayServices = EasyMock.createNiceMock(GatewayServices.class);
    EasyMock.expect(mockGatewayServices.getService(GatewayServices.SERVICE_REGISTRY_SERVICE)).andReturn(mockServiceRegistry).anyTimes();
    EasyMock.replay(mockServiceRegistry, mockGatewayServices);
    String descriptorUrl = getTestResource("rewrite.xml").toExternalForm();
    Log.setLog(new NoOpLogger());
    server = new ServletTester();
    server.setContextPath("/");
    server.getContext().addEventListener(new UrlRewriteServletContextListener());
    server.getContext().setInitParameter(UrlRewriteServletContextListener.DESCRIPTOR_LOCATION_INIT_PARAM_NAME, descriptorUrl);
    server.getContext().setAttribute(GatewayServices.GATEWAY_CLUSTER_ATTRIBUTE, "test-cluster");
    server.getContext().setAttribute(GatewayServices.GATEWAY_SERVICES_ATTRIBUTE, mockGatewayServices);
    FilterHolder setupFilter = server.addFilter(SetupFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST));
    setupFilter.setFilter(new SetupFilter(username));
    FilterHolder rewriteFilter = server.addFilter(UrlRewriteServletFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST));
    if (initParams != null) {
        for (Map.Entry<String, String> entry : initParams.entrySet()) {
            rewriteFilter.setInitParameter(entry.getKey(), entry.getValue());
        }
    }
    rewriteFilter.setFilter(new UrlRewriteServletFilter());
    interactions = new ArrayQueue<MockInteraction>();
    ServletHolder servlet = server.addServlet(MockServlet.class, "/");
    servlet.setServlet(new MockServlet("mock-servlet", interactions));
    server.start();
    interaction = new MockInteraction();
    request = HttpTester.newRequest();
    response = null;
}
Also used : GatewayServices(org.apache.knox.gateway.services.GatewayServices) MockInteraction(org.apache.knox.test.mock.MockInteraction) ServletTester(org.eclipse.jetty.servlet.ServletTester) FilterHolder(org.eclipse.jetty.servlet.FilterHolder) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) UrlRewriteServletContextListener(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteServletContextListener) MockServlet(org.apache.knox.test.mock.MockServlet) UrlRewriteServletFilter(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteServletFilter) NoOpLogger(org.apache.knox.test.log.NoOpLogger) ServiceRegistry(org.apache.knox.gateway.services.registry.ServiceRegistry) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with MockServlet

use of org.apache.knox.test.mock.MockServlet in project knox by apache.

the class UrlRewriteServletFilterTest method setUp.

public void setUp(Map<String, String> initParams) throws Exception {
    String descriptorUrl = getTestResource("rewrite.xml").toExternalForm();
    server = new ServletTester();
    server.setContextPath("/");
    server.getContext().addEventListener(new UrlRewriteServletContextListener());
    server.getContext().setInitParameter(UrlRewriteServletContextListener.DESCRIPTOR_LOCATION_INIT_PARAM_NAME, descriptorUrl);
    FilterHolder setupFilter = server.addFilter(SetupFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST));
    setupFilter.setFilter(new SetupFilter());
    FilterHolder rewriteFilter = server.addFilter(UrlRewriteServletFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST));
    if (initParams != null) {
        for (Map.Entry<String, String> entry : initParams.entrySet()) {
            rewriteFilter.setInitParameter(entry.getKey(), entry.getValue());
        }
    }
    rewriteFilter.setFilter(new UrlRewriteServletFilter());
    interactions = new ArrayQueue<MockInteraction>();
    ServletHolder servlet = server.addServlet(MockServlet.class, "/");
    servlet.setServlet(new MockServlet("mock-servlet", interactions));
    server.start();
    interaction = new MockInteraction();
    request = HttpTester.newRequest();
    response = null;
}
Also used : MockInteraction(org.apache.knox.test.mock.MockInteraction) ServletTester(org.eclipse.jetty.servlet.ServletTester) FilterHolder(org.eclipse.jetty.servlet.FilterHolder) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) MockServlet(org.apache.knox.test.mock.MockServlet) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with MockServlet

use of org.apache.knox.test.mock.MockServlet in project knox by apache.

the class UsernameFunctionProcessorTest method setUp.

public void setUp(String username, Map<String, String> initParams) throws Exception {
    String descriptorUrl = getTestResource("rewrite.xml").toExternalForm();
    Log.setLog(new NoOpLogger());
    server = new ServletTester();
    server.setContextPath("/");
    server.getContext().addEventListener(new UrlRewriteServletContextListener());
    server.getContext().setInitParameter(UrlRewriteServletContextListener.DESCRIPTOR_LOCATION_INIT_PARAM_NAME, descriptorUrl);
    FilterHolder setupFilter = server.addFilter(SetupFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST));
    setupFilter.setFilter(new SetupFilter(username));
    FilterHolder rewriteFilter = server.addFilter(UrlRewriteServletFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST));
    if (initParams != null) {
        for (Map.Entry<String, String> entry : initParams.entrySet()) {
            rewriteFilter.setInitParameter(entry.getKey(), entry.getValue());
        }
    }
    rewriteFilter.setFilter(new UrlRewriteServletFilter());
    interactions = new ArrayQueue<MockInteraction>();
    ServletHolder servlet = server.addServlet(MockServlet.class, "/");
    servlet.setServlet(new MockServlet("mock-servlet", interactions));
    server.start();
    interaction = new MockInteraction();
    request = HttpTester.newRequest();
    response = null;
}
Also used : MockInteraction(org.apache.knox.test.mock.MockInteraction) ServletTester(org.eclipse.jetty.servlet.ServletTester) FilterHolder(org.eclipse.jetty.servlet.FilterHolder) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) UrlRewriteServletContextListener(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteServletContextListener) MockServlet(org.apache.knox.test.mock.MockServlet) UrlRewriteServletFilter(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteServletFilter) NoOpLogger(org.apache.knox.test.log.NoOpLogger) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with MockServlet

use of org.apache.knox.test.mock.MockServlet in project knox by apache.

the class UrlRewriteServletContextListenerTest method setUp.

@Before
public void setUp() throws Exception {
    String descriptorUrl = getTestResource("rewrite.xml").toExternalForm();
    server = new ServletTester();
    server.setContextPath("/");
    server.getContext().addEventListener(new UrlRewriteServletContextListener());
    server.getContext().setInitParameter(UrlRewriteServletContextListener.DESCRIPTOR_LOCATION_INIT_PARAM_NAME, descriptorUrl);
    FilterHolder filter = server.addFilter(UrlRewriteServletFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST));
    filter.setFilter(new UrlRewriteServletFilter());
    interactions = new ArrayQueue<MockInteraction>();
    ServletHolder servlet = server.addServlet(MockServlet.class, "/");
    servlet.setServlet(new MockServlet("mock-servlet", interactions));
    servlet.setInitParameter(UrlRewriteServletContextListener.DESCRIPTOR_LOCATION_INIT_PARAM_NAME, getTestResource("rewrite.xml").toExternalForm());
    server.start();
    interaction = new MockInteraction();
    request = HttpTester.newRequest();
    response = null;
}
Also used : MockInteraction(org.apache.knox.test.mock.MockInteraction) ServletTester(org.eclipse.jetty.servlet.ServletTester) FilterHolder(org.eclipse.jetty.servlet.FilterHolder) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) MockServlet(org.apache.knox.test.mock.MockServlet) Before(org.junit.Before)

Example 5 with MockServlet

use of org.apache.knox.test.mock.MockServlet in project knox by apache.

the class FrontendFunctionProcessorTest method setUp.

public void setUp(String username, Map<String, String> initParams, Attributes attributes) throws Exception {
    ServiceRegistry mockServiceRegistry = EasyMock.createNiceMock(ServiceRegistry.class);
    EasyMock.expect(mockServiceRegistry.lookupServiceURL("test-cluster", "NAMENODE")).andReturn("test-nn-scheme://test-nn-host:411").anyTimes();
    EasyMock.expect(mockServiceRegistry.lookupServiceURL("test-cluster", "JOBTRACKER")).andReturn("test-jt-scheme://test-jt-host:511").anyTimes();
    GatewayServices mockGatewayServices = EasyMock.createNiceMock(GatewayServices.class);
    EasyMock.expect(mockGatewayServices.getService(GatewayServices.SERVICE_REGISTRY_SERVICE)).andReturn(mockServiceRegistry).anyTimes();
    EasyMock.replay(mockServiceRegistry, mockGatewayServices);
    String descriptorUrl = TestUtils.getResourceUrl(FrontendFunctionProcessorTest.class, "rewrite.xml").toExternalForm();
    Log.setLog(new NoOpLogger());
    server = new ServletTester();
    server.setContextPath("/");
    server.getContext().addEventListener(new UrlRewriteServletContextListener());
    server.getContext().setInitParameter(UrlRewriteServletContextListener.DESCRIPTOR_LOCATION_INIT_PARAM_NAME, descriptorUrl);
    if (attributes != null) {
        server.getContext().setAttributes(attributes);
    }
    server.getContext().setAttribute(GatewayServices.GATEWAY_CLUSTER_ATTRIBUTE, "test-cluster");
    server.getContext().setAttribute(GatewayServices.GATEWAY_SERVICES_ATTRIBUTE, mockGatewayServices);
    FilterHolder setupFilter = server.addFilter(SetupFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST));
    setupFilter.setFilter(new SetupFilter(username));
    FilterHolder rewriteFilter = server.addFilter(UrlRewriteServletFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST));
    if (initParams != null) {
        for (Map.Entry<String, String> entry : initParams.entrySet()) {
            rewriteFilter.setInitParameter(entry.getKey(), entry.getValue());
        }
    }
    rewriteFilter.setFilter(new UrlRewriteServletFilter());
    interactions = new ArrayQueue<MockInteraction>();
    ServletHolder servlet = server.addServlet(MockServlet.class, "/");
    servlet.setServlet(new MockServlet("mock-servlet", interactions));
    server.start();
    interaction = new MockInteraction();
    request = HttpTester.newRequest();
    response = null;
}
Also used : GatewayServices(org.apache.knox.gateway.services.GatewayServices) MockInteraction(org.apache.knox.test.mock.MockInteraction) ServletTester(org.eclipse.jetty.servlet.ServletTester) FilterHolder(org.eclipse.jetty.servlet.FilterHolder) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) UrlRewriteServletContextListener(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteServletContextListener) MockServlet(org.apache.knox.test.mock.MockServlet) UrlRewriteServletFilter(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteServletFilter) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) NoOpLogger(org.apache.knox.test.log.NoOpLogger) ServiceRegistry(org.apache.knox.gateway.services.registry.ServiceRegistry) Map(java.util.Map) AttributesMap(org.eclipse.jetty.util.AttributesMap) HashMap(java.util.HashMap)

Aggregations

MockInteraction (org.apache.knox.test.mock.MockInteraction)5 MockServlet (org.apache.knox.test.mock.MockServlet)5 FilterHolder (org.eclipse.jetty.servlet.FilterHolder)5 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)5 ServletTester (org.eclipse.jetty.servlet.ServletTester)5 HashMap (java.util.HashMap)4 Map (java.util.Map)4 UrlRewriteServletContextListener (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteServletContextListener)3 UrlRewriteServletFilter (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteServletFilter)3 NoOpLogger (org.apache.knox.test.log.NoOpLogger)3 GatewayServices (org.apache.knox.gateway.services.GatewayServices)2 ServiceRegistry (org.apache.knox.gateway.services.registry.ServiceRegistry)2 AttributesMap (org.eclipse.jetty.util.AttributesMap)1 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)1 Before (org.junit.Before)1