@Test public void testGetAggregation() { // Create a mock aggregation. Terms aggregation = mock(Terms.class); // Create mock aggregations. Aggregations aggregations = mock(Aggregations.class); when(aggregations.get(AGGREGATION_NAME)).thenReturn(aggregation); // Create a mock search response. SearchResponse searchResponse = mock(SearchResponse.class); when(searchResponse.getAggregations()).thenReturn(aggregations); // Call the method under test. Terms result = elasticsearchHelper.getAggregation(searchResponse, AGGREGATION_NAME); // Verify the external calls. verifyNoMoreInteractionsHelper(); // Validate the result. assertEquals(aggregation, result); }
@Test public void testGetNestedAggregation() { // Create a mock sub-aggregation. Terms subAggregation = mock(Terms.class); // Create mock nested aggregations. Aggregations nestedAggregations = mock(Aggregations.class); when(nestedAggregations.get(SUB_AGGREGATION_NAME)).thenReturn(subAggregation); // Create a mock nested aggregation. Nested nestedAggregation = mock(Nested.class); when(nestedAggregation.getAggregations()).thenReturn(nestedAggregations); // Create mock search response aggregations. Aggregations searchResponseAggregations = mock(Aggregations.class); when(searchResponseAggregations.get(NESTED_AGGREGATION_NAME)).thenReturn(nestedAggregation); // Create a mock search response. SearchResponse searchResponse = mock(SearchResponse.class); when(searchResponse.getAggregations()).thenReturn(searchResponseAggregations); // Call the method under test. Terms result = elasticsearchHelper.getNestedAggregation(searchResponse, NESTED_AGGREGATION_NAME, SUB_AGGREGATION_NAME); // Verify the external calls. verifyNoMoreInteractionsHelper(); // Validate the result. assertEquals(subAggregation, result); }
@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(); }
@Test public void testGetNestedAggregationNestedAggregationIsNull() { // Create mock search response aggregations. Aggregations searchResponseAggregations = mock(Aggregations.class); when(searchResponseAggregations.get(NESTED_AGGREGATION_NAME)).thenReturn(null); // Create a mock search response. SearchResponse searchResponse = mock(SearchResponse.class); when(searchResponse.getAggregations()).thenReturn(searchResponseAggregations); // Mock the external calls. when(jsonHelper.objectToJson(searchResponse)).thenReturn(SEARCH_RESPONSE_JSON_STRING); // Try to call the method under test. try { elasticsearchHelper.getNestedAggregation(searchResponse, NESTED_AGGREGATION_NAME, SUB_AGGREGATION_NAME); fail(); } catch (IllegalStateException e) { assertEquals("Invalid search result.", e.getMessage()); } // Verify the external calls. verify(jsonHelper).objectToJson(searchResponse); verifyNoMoreInteractionsHelper(); }
@Test public void testGetAggregationsFromNestedAggregationAggregationsSetIsNull() { // Create a mock search response. SearchResponse searchResponse = mock(SearchResponse.class); // Create a mock nested aggregation. Nested nestedAggregation = mock(Nested.class); when(nestedAggregation.getAggregations()).thenReturn(null); // Mock the external calls. when(jsonHelper.objectToJson(searchResponse)).thenReturn(SEARCH_RESPONSE_JSON_STRING); when(jsonHelper.objectToJson(nestedAggregation)).thenReturn(NESTED_AGGREGATION_JSON_STRING); // Try to call the method under test. try { elasticsearchHelper.getAggregationsFromNestedAggregation(nestedAggregation, searchResponse); } catch (IllegalStateException e) { assertEquals("Invalid search result.", e.getMessage()); } // Verify the external calls. verify(jsonHelper).objectToJson(searchResponse); verify(jsonHelper).objectToJson(nestedAggregation); verifyNoMoreInteractionsHelper(); }
@Test public void testGetAggregationAggregationIsNull() { // Create mock aggregations. Aggregations aggregations = mock(Aggregations.class); when(aggregations.get(AGGREGATION_NAME)).thenReturn(null); // Create a mock search response. SearchResponse searchResponse = mock(SearchResponse.class); when(searchResponse.getAggregations()).thenReturn(aggregations); // Mock the external calls. when(jsonHelper.objectToJson(searchResponse)).thenReturn(SEARCH_RESPONSE_JSON_STRING); // Try to call the method under test. try { elasticsearchHelper.getAggregation(searchResponse, AGGREGATION_NAME); fail(); } catch (IllegalStateException e) { assertEquals("Invalid search result.", e.getMessage()); } // Verify the external calls. verify(jsonHelper).objectToJson(searchResponse); verifyNoMoreInteractionsHelper(); }