SolrSearchDao implements SearchDao { @Override public GroupResponse group(GroupRequest groupRequest) throws InvalidSearchException { try { validateGroupRequest(groupRequest); String groupNames = groupRequest.getGroups().stream().map(Group::getField).collect( Collectors.joining(",")); SolrQuery query = new SolrQuery() .setStart(0) .setRows(0) .setQuery(groupRequest.getQuery()) .setShowDebugInfo(LOG.isDebugEnabled()); query.set("collection", getCollections(groupRequest.getIndices())); Optional<String> scoreField = groupRequest.getScoreField(); if (scoreField.isPresent()) { query.set("stats", true); query.set("stats.field", String.format("{!tag=piv1 sum=true}%s", scoreField.get())); } query.set("facet", true); query.set("facet.pivot", String.format("{!stats=piv1}%s", groupNames)); QueryResponse response = client.query(query); logQueryDebugDetail(query, response); return buildGroupResponse(groupRequest, response); } catch (IOException | SolrServerException e) { String msg = e.getMessage(); LOG.error(msg, e); throw new InvalidSearchException(msg, e); } } SolrSearchDao(SolrClient client, AccessConfig accessConfig); @Override SearchResponse search(SearchRequest searchRequest); SearchResponse search(SearchRequest searchRequest, String fieldList); @Override GroupResponse group(GroupRequest groupRequest);  }
@Test public void searchShouldThrowInvalidSearchExceptionOnNullGroup() { GroupRequest groupRequest = mock(GroupRequest.class); solrSearchDao = spy(new SolrSearchDao(client, accessConfig)); when(groupRequest.getQuery()).thenReturn("query"); when(groupRequest.getGroups()).thenReturn(null); when(groupRequest.getScoreField()).thenReturn(Optional.of("scoreField")); when(groupRequest.getIndices()).thenReturn(Arrays.asList("bro", "snort")); InvalidSearchException e = assertThrows(InvalidSearchException.class, () -> solrSearchDao.group(groupRequest)); assertEquals("At least 1 group must be provided.", e.getMessage()); verifyNoMoreInteractions(client); }
@Test public void searchShouldThrowInvalidSearchExceptionOnEmptyGroup() { GroupRequest groupRequest = mock(GroupRequest.class); solrSearchDao = spy(new SolrSearchDao(client, accessConfig)); when(groupRequest.getQuery()).thenReturn("query"); when(groupRequest.getGroups()).thenReturn(Collections.EMPTY_LIST); when(groupRequest.getScoreField()).thenReturn(Optional.of("scoreField")); when(groupRequest.getIndices()).thenReturn(Arrays.asList("bro", "snort")); InvalidSearchException e = assertThrows(InvalidSearchException.class, () -> solrSearchDao.group(groupRequest)); assertEquals("At least 1 group must be provided.", e.getMessage()); verifyNoMoreInteractions(client); }