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 = new HttpTester();
    response = new HttpTester();
  }
 /**
  * Set the servlet that provides content for the GzipFilter in being tested.
  *
  * @param servletClass the servlet that will provide content.
  * @return the FilterHolder for configuring the GzipFilter's initParameters with
  */
 public FilterHolder setContentServlet(Class<? extends Servlet> servletClass) throws IOException {
   servletTester = new ServletTester();
   servletTester.setContextPath("/context");
   servletTester.setResourceBase(testdir.getDir().getCanonicalPath());
   ServletHolder servletHolder = servletTester.addServlet(servletClass, "/");
   servletHolder.setInitParameter("baseDir", testdir.getDir().getAbsolutePath());
   FilterHolder holder = servletTester.addFilter(gzipFilterClass, "/*", 0);
   return holder;
 }