@Test public void testHandlingSearchResultsEventWithNoConverterResponseEvent() throws Exception { SearchResultsConverterFactory resultsConverterFactory = mock(SearchResultsConverterFactory.class); NumberOfSearchResultsConverter searchResultsConverter = new NumberOfSearchResultsConverter("application/json"); when(resultsConverterFactory.getConverter(FrequentlyRelatedSearchResult[].class)).thenReturn(searchResultsConverter); SearchResponseContextHandler contextHandler = mock(SearchResponseContextHandler.class); SearchResponseContextHandler defaultHandler = mock(SearchResponseContextHandler.class); Map<Class,SearchResponseContextHandler> mappings = new HashMap<Class,SearchResponseContextHandler>(4); mappings.put(AsyncContext.class,contextHandler); CountDownLatch latch = new CountDownLatch(1); eventHandler = createResponseEventHandler(configuration,new MapBasedSearchResponseContextHandlerLookup(defaultHandler,mappings),resultsConverterFactory,latch); SearchResponseContext<AsyncContext> context = mock(AsyncServletSearchResponseContext.class); when(context.getContextType()).thenReturn(AsyncContext.class); List<SearchResponseContext> holder = new ArrayList<SearchResponseContext>(1); holder.add(context); SearchResultsEvent searchResultsEvent = createStringResponse(); List<List<SearchResponseContext>> list= new ArrayList<List<SearchResponseContext>>(1); list.add(holder); eventHandler.handleResponseEvents(createSearchResultsEvent(new SearchResultsEvent[] {searchResultsEvent}),list); try { boolean handled = latch.await(2000, TimeUnit.MILLISECONDS); if(handled==false) fail("Failed waiting for latch"); } catch(Exception e) { fail("Failed waiting for latch"); } assertEquals(0, searchResultsConverter.getNoOfExecutions()); verify(contextHandler,times(1)).sendResults(eq(ResponseContextTypeBasedResponseEventHandler.ERROR_RESPONSE),eq(ResponseContextTypeBasedResponseEventHandler.ERROR_MEDIA_TYPE),any(SearchResultsEvent.class),any(SearchResponseContext.class)); verify(defaultHandler,times(0)).sendResults(anyString(),anyString(),any(SearchResultsEvent.class),any(SearchResponseContext.class)); verify(context, times(1)).close(); }
@Override public void handleResponseEvents(SearchResultEventWithSearchRequestKey[] searchResults,List<List<SearchResponseContext>> responseContexts) { for(int i=0;i<searchResults.length;i++) { log.debug("handling search result {}",i); handleResponseEvent(searchResults[i],responseContexts.get(i)); } }
ResponseContextTypeBasedResponseEventHandler implements ResponseEventHandler { @Override public void handleResponseEvents(SearchResultEventWithSearchRequestKey[] searchResults,List<List<SearchResponseContext>> responseContexts) { for(int i=0;i<searchResults.length;i++) { log.debug("handling search result {}",i); handleResponseEvent(searchResults[i],responseContexts.get(i)); } } }
ResponseContextTypeBasedResponseEventHandler implements ResponseEventHandler { @Override public void handleResponseEvents(SearchResultEventWithSearchRequestKey[] searchResults,List<List<SearchResponseContext>> responseContexts) { for(int i=0;i<searchResults.length;i++) { log.debug("handling search result {}",i); handleResponseEvent(searchResults[i],responseContexts.get(i)); } } ResponseContextTypeBasedResponseEventHandler(SearchResponseContextHandlerLookup responseContextHandler,
                                                        SearchResultsConverterFactory factory); }
ResponseContextTypeBasedResponseEventHandler implements ResponseEventHandler { @Override public void handleResponseEvents(SearchResultEventWithSearchRequestKey[] searchResults,List<List<SearchResponseContext>> responseContexts) { for(int i=0;i<searchResults.length;i++) { log.debug("handling search result {}",i); handleResponseEvent(searchResults[i],responseContexts.get(i)); } } ResponseContextTypeBasedResponseEventHandler(SearchResponseContextHandlerLookup responseContextHandler,
                                                        SearchResultsConverterFactory factory); @Override void handleResponseEvents(SearchResultEventWithSearchRequestKey[] searchResults,List<List<SearchResponseContext>> responseContexts); @Override void shutdown(); void handleResponseEvent(SearchResultEventWithSearchRequestKey results,List<SearchResponseContext> awaitingResponses); }
ResponseContextTypeBasedResponseEventHandler implements ResponseEventHandler { @Override public void handleResponseEvents(SearchResultEventWithSearchRequestKey[] searchResults,List<List<SearchResponseContext>> responseContexts) { for(int i=0;i<searchResults.length;i++) { log.debug("handling search result {}",i); handleResponseEvent(searchResults[i],responseContexts.get(i)); } } ResponseContextTypeBasedResponseEventHandler(SearchResponseContextHandlerLookup responseContextHandler,
                                                        SearchResultsConverterFactory factory); @Override void handleResponseEvents(SearchResultEventWithSearchRequestKey[] searchResults,List<List<SearchResponseContext>> responseContexts); @Override void shutdown(); void handleResponseEvent(SearchResultEventWithSearchRequestKey results,List<SearchResponseContext> awaitingResponses); static final String ERROR_RESPONSE; static final String ERROR_MEDIA_TYPE; }