### Question:
RedirectResourceFilter implements ContainerRequestFilter { @Override public void filter(ContainerRequestContext requestContext) throws IOException { final UriInfo uriInfo = requestContext.getUriInfo(); if (uriInfo.getPath().equals("controller")){ UriBuilder builder = UriBuilder.fromUri(uriInfo.getBaseUri()) .path(SiteToSiteResource.class) .replaceQuery(uriInfo.getRequestUri().getRawQuery()); URI redirectTo = builder.build(); requestContext.setRequestUri(uriInfo.getBaseUri(), redirectTo); } }  @Override void filter(ContainerRequestContext requestContext);  }### Answer:
@Test public void testUnmatched() throws Exception { String path = "unmatched"; String baseUri = "http: UriInfo uriInfo = mock(UriInfo.class); when(uriInfo.getPath()).thenReturn(path); when(uriInfo.getBaseUri()).thenReturn(new URI(baseUri)); when(uriInfo.getRequestUri()).thenReturn(new URI(baseUri + path)); ContainerRequestContext request = mock(ContainerRequestContext.class); when(request.getUriInfo()).thenReturn(uriInfo); doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { fail("setUris shouldn't be called"); return null; } }).when(request).setRequestUri(any(URI.class), any(URI.class)); RedirectResourceFilter filter = new RedirectResourceFilter(); filter.filter(request); }
@Test public void testController() throws Exception { String path = "controller"; String baseUri = "http: UriInfo uriInfo = mock(UriInfo.class); when(uriInfo.getPath()).thenReturn(path); when(uriInfo.getBaseUri()).thenReturn(new URI(baseUri)); when(uriInfo.getRequestUri()).thenReturn(new URI(baseUri + path)); ContainerRequestContext request = mock(ContainerRequestContext.class); when(request.getUriInfo()).thenReturn(uriInfo); 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).setRequestUri(any(URI.class), any(URI.class)); RedirectResourceFilter filter = new RedirectResourceFilter(); filter.filter(request); }
@Test public void testControllerWithParams() throws Exception { String path = "controller"; String baseUri = "http: String query = "?a=1&b=23&cde=456"; UriInfo uriInfo = mock(UriInfo.class); when(uriInfo.getPath()).thenReturn(path); when(uriInfo.getBaseUri()).thenReturn(new URI(baseUri)); when(uriInfo.getRequestUri()).thenReturn(new URI(baseUri + path + query)); ContainerRequestContext request = mock(ContainerRequestContext.class); when(request.getUriInfo()).thenReturn(uriInfo); 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).setRequestUri(any(URI.class), any(URI.class)); RedirectResourceFilter filter = new RedirectResourceFilter(); filter.filter(request); }