/** * Returns the aggregation that is associated with the specified name. This method also validates that the retrieved aggregation exists. * * @param searchResponse the response of the search request * @param aggregationName the name of the aggregation * * @return the aggregation */ public Terms getAggregation(SearchResponse searchResponse, String aggregationName) { // Retrieve the aggregations from the search response. Aggregations aggregations = getAggregationsFromSearchResponse(searchResponse); // Retrieve the specified aggregation. Terms aggregation = aggregations.get(aggregationName); // Fail if retrieved aggregation is null. if (aggregation == null) { // Log the error along with the search response contents. LOGGER.error("Failed to retrieve \"{}\" aggregation from the search response. searchResponse={}", aggregationName, jsonHelper.objectToJson(searchResponse)); // Throw an exception. throw new IllegalStateException("Invalid search result."); } return aggregation; }
/** * Returns the aggregation that is associated with the specified name. This method also validates that the retrieved aggregation exists. * * @param searchResponse the response of the search request * @param aggregationName the name of the aggregation * * @return the aggregation */ public Terms getAggregation(SearchResponse searchResponse, String aggregationName) { // Retrieve the aggregations from the search response. Aggregations aggregations = getAggregationsFromSearchResponse(searchResponse); // Retrieve the specified aggregation. Terms aggregation = aggregations.get(aggregationName); // Fail if retrieved aggregation is null. if (aggregation == null) { // Log the error along with the search response contents. LOGGER.error("Failed to retrieve \"{}\" aggregation from the search response. searchResponse={}", aggregationName, jsonHelper.objectToJson(searchResponse)); // Throw an exception. throw new IllegalStateException("Invalid search result."); } return aggregation; }
Aggregations searchResponseAggregations = getAggregationsFromSearchResponse(searchResponse);
Aggregations searchResponseAggregations = getAggregationsFromSearchResponse(searchResponse);
@Test public void testGetAggregationsFromSearchResponseAggregationsSetIsNull() { // Create a mock search response. SearchResponse searchResponse = mock(SearchResponse.class); when(searchResponse.getAggregations()).thenReturn(null); // Mock the external calls. when(jsonHelper.objectToJson(searchResponse)).thenReturn(SEARCH_RESPONSE_JSON_STRING); // Try to call the method under test. try { elasticsearchHelper.getAggregationsFromSearchResponse(searchResponse); } catch (IllegalStateException e) { assertEquals("Invalid search result.", e.getMessage()); } // Verify the external calls. verify(jsonHelper).objectToJson(searchResponse); verifyNoMoreInteractionsHelper(); }