### Question:
HttpAsyncSearchResponseContextHandler implements SearchResponseContextHandler<AsyncContext> { @Override public void sendResults(String resultsAsString, String mediaType, SearchResultsEvent results, SearchResponseContext<AsyncContext> sctx) { AsyncContext ctx = sctx.getSearchResponseContext(); HttpServletResponse r = null; try { ServletRequest request = ctx.getRequest(); if(request==null) { return; } r = (HttpServletResponse)ctx.getResponse(); if(r!=null) { int statusCode = configuration.getResponseCode(results.getOutcomeType()); r.setStatus(statusCode); r.setContentType(mediaType); r.getWriter().write(resultsAsString); } } catch (IOException e) { if(r!=null) { r.setStatus(500); } } catch (IllegalStateException e) { log.warn("Async Context not available",e); } } HttpAsyncSearchResponseContextHandler(Configuration configuration); @Override void sendResults(String resultsAsString, String mediaType, SearchResultsEvent results, SearchResponseContext<AsyncContext> sctx);  }### Answer:
@Test public void testResultsSent() { Configuration configuration = mock(Configuration.class); when(configuration.getResponseCode(any(SearchResultsOutcome.class))).thenReturn(200); HttpServletRequest request = mock(HttpServletRequest.class); HttpServletResponse response = mock(HttpServletResponse.class); final ByteArrayOutputStream stream = new ByteArrayOutputStream(); ServletOutputStream out = new ServletOutputStream() { public OutputStream getOutputStream() { return stream; } @Override public void write(int b) throws IOException { stream.write(b); } }; StringWriter stringWriter = new StringWriter(); PrintWriter writer = new PrintWriter(stringWriter); try { when(response.getOutputStream()).thenReturn(out); } catch (IOException e) { e.printStackTrace(); } try { when(response.getWriter()).thenReturn(writer); } catch (IOException e) { e.printStackTrace(); } AsyncContext userResponse = getAsyncContext(request,response); SearchResponseContext responseHolder = new AsyncServletSearchResponseContext(userResponse,System.nanoTime()); SearchResponseContextHandler handler = getHandler(configuration); handler.sendResults("results","appplication/json",createSearchResultEvent(),responseHolder); String s = new String(stream.toByteArray()); if(s.length()==0) { s = stringWriter.toString(); } assertEquals("results",s); responseHolder.close(); verify(userResponse,times(1)).complete(); }
@Test public void testResultsNotSentWhenRequestOrResponseNotAvailable() { Configuration configuration = mock(Configuration.class); when(configuration.getResponseCode(any(SearchResultsOutcome.class))).thenReturn(200); AsyncContext userResponse = getAsyncContext(null,null); SearchResponseContext responseHolder = new AsyncServletSearchResponseContext(userResponse,System.nanoTime()); SearchResponseContextHandler handler = getHandler(configuration); handler.sendResults("results","appplication/json",null,responseHolder); verify(configuration,times(0)).getResponseCode(any(SearchResultsOutcome.class)); responseHolder.close(); verify(userResponse,times(1)).complete(); }
@Test public void testIOExceptionIsHandled() { Configuration configuration = mock(Configuration.class); when(configuration.getResponseCode(any(SearchResultsOutcome.class))).thenReturn(200); HttpServletRequest request = mock(HttpServletRequest.class); HttpServletResponse response = mock(HttpServletResponse.class); try { doThrow(new IOException()).when(response).getWriter(); } catch (IOException e) { e.printStackTrace(); } AsyncContext userResponse = getAsyncContext(request,response); SearchResponseContext responseHolder = new AsyncServletSearchResponseContext(userResponse,System.nanoTime()); SearchResponseContextHandler handler = getHandler(configuration); handler.sendResults("results","appplication/json",mock(SearchResultsEvent.class),responseHolder); verify(response,times(1)).setStatus(500); responseHolder.close(); verify(userResponse, times(1)).complete(); }