### Question:
CreateQuerySessionIDFilter implements ContainerResponseFilter { @Override public void filter(ContainerRequestContext request, ContainerResponseContext response) throws IOException { ResourceMethodInvoker method = (ResourceMethodInvoker) request.getProperty(ResourceMethodInvoker.class.getName()); GenerateQuerySessionId annotation = FindAnnotation.findAnnotation(method.getMethodAnnotations(), GenerateQuerySessionId.class); String path = annotation.cookieBasePath(); String id = ""; String cookieValue = generateCookieValue(); boolean setCookie = true; switch (response.getStatusInfo().getFamily()) { case SERVER_ERROR: case CLIENT_ERROR: setCookie = false; QUERY_ID.set(null); break; default: if (StringUtils.isEmpty(QUERY_ID.get())) { log.error(method.getResourceClass() + "." + method.getMethod().getName() + " did not set QUERY_ID threadlocal."); } else { id = QUERY_ID.get(); QUERY_ID.set(null); } break; } if (setCookie) { response.getHeaders().add(HttpHeaderNames.SET_COOKIE, new NewCookie(Constants.QUERY_COOKIE_NAME, cookieValue, path + id, null, null, NewCookie.DEFAULT_MAX_AGE, false)); } }  @Override void filter(ContainerRequestContext request, ContainerResponseContext response); static String generateCookieValue(); static final ThreadLocal<String> QUERY_ID; }### Answer:
@Test public void filterClientError() throws Exception { response.setStatusInfo(Response.Status.BAD_REQUEST); EasyMock.expect(method.getMethodAnnotations()).andReturn(new Annotation[] {annotation}); replayAll(); CreateQuerySessionIDFilter.QUERY_ID.set("1234"); filter.filter(request, response); NewCookie responseCookie = (NewCookie) response.getHeaders().getFirst("Set-Cookie"); assertNull("Cookie present when we shouldn't have one.", responseCookie); verifyAll(); }
@Test public void filter() throws Exception { EasyMock.expect(method.getMethodAnnotations()).andReturn(new Annotation[] {annotation}); replayAll(); CreateQuerySessionIDFilter.QUERY_ID.set("1234"); filter.filter(request, response); NewCookie responseCookie = (NewCookie) response.getHeaders().getFirst("Set-Cookie"); assertNotNull("No cookie present when we should have one.", responseCookie); assertEquals("query-session-id", responseCookie.getName()); assertEquals("/test/path/1234", responseCookie.getPath()); verifyAll(); }
@Test public void filterNoQueryId() throws Exception { EasyMock.expect(method.getMethodAnnotations()).andReturn(new Annotation[] {annotation}); EasyMock.expect(method.getResourceClass()).andReturn(null); EasyMock.expect(method.getMethod()).andReturn(getClass().getMethod("filterNoQueryId", null)); replayAll(); CreateQuerySessionIDFilter.QUERY_ID.set(null); filter.filter(request, response); NewCookie responseCookie = (NewCookie) response.getHeaders().getFirst("Set-Cookie"); assertNotNull("No cookie present when we should have one.", responseCookie); assertEquals("query-session-id", responseCookie.getName()); assertEquals("/test/path/", responseCookie.getPath()); verifyAll(); }
@Test public void filterServerError() throws Exception { response.setStatusInfo(Response.Status.INTERNAL_SERVER_ERROR); EasyMock.expect(method.getMethodAnnotations()).andReturn(new Annotation[] {annotation}); replayAll(); CreateQuerySessionIDFilter.QUERY_ID.set("1234"); filter.filter(request, response); NewCookie responseCookie = (NewCookie) response.getHeaders().getFirst("Set-Cookie"); assertNull("Cookie present when we shouldn't have one.", responseCookie); verifyAll(); }