### Question:
RedirectResourceFilter implements ContainerRequestFilter { @Override public ContainerRequest filter(ContainerRequest containerRequest) { if(containerRequest.getPath().equals("controller")){ UriBuilder builder = UriBuilder.fromUri(containerRequest.getBaseUri()) .path(SiteToSiteResource.class) .replaceQuery(containerRequest.getRequestUri().getRawQuery()); URI redirectTo = builder.build(); containerRequest.setUris(containerRequest.getBaseUri(), redirectTo); } return containerRequest; }  @Override ContainerRequest filter(ContainerRequest containerRequest);  }### Answer:
@Test public void testUnmatched() throws Exception { ContainerRequest request = mock(ContainerRequest.class); String path = "unmatched"; String baseUri = "http: when(request.getPath()).thenReturn(path); when(request.getBaseUri()).thenReturn(new URI(baseUri)); when(request.getRequestUri()).thenReturn(new URI(baseUri + path)); doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { fail("setUris shouldn't be called"); return null; } }).when(request).setUris(any(URI.class), any(URI.class)); RedirectResourceFilter filter = new RedirectResourceFilter(); filter.filter(request); }
@Test public void testController() throws Exception { ContainerRequest request = mock(ContainerRequest.class); String path = "controller"; String baseUri = "http: when(request.getPath()).thenReturn(path); when(request.getBaseUri()).thenReturn(new URI(baseUri)); when(request.getRequestUri()).thenReturn(new URI(baseUri + path)); doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { assertEquals("base uri should be retained", new URI(baseUri), invocation.getArguments()[0]); assertEquals("request uri should be redirected", new URI(baseUri + "site-to-site"), invocation.getArguments()[1]); return null; } }).when(request).setUris(any(URI.class), any(URI.class)); RedirectResourceFilter filter = new RedirectResourceFilter(); filter.filter(request); }
@Test public void testControllerWithParams() throws Exception { ContainerRequest request = mock(ContainerRequest.class); String path = "controller"; String baseUri = "http: String query = "?a=1&b=23&cde=456"; when(request.getPath()).thenReturn(path); when(request.getBaseUri()).thenReturn(new URI(baseUri)); when(request.getRequestUri()).thenReturn(new URI(baseUri + path + query)); doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { assertEquals("base uri should be retained", new URI(baseUri), invocation.getArguments()[0]); assertEquals("request uri should be redirected with query parameters", new URI(baseUri + "site-to-site" + query), invocation.getArguments()[1]); return null; } }).when(request).setUris(any(URI.class), any(URI.class)); RedirectResourceFilter filter = new RedirectResourceFilter(); filter.filter(request); }