diff --git a/src/main/java/com/amazon/opendistroforelasticsearch/ad/feature/SearchFeatureDao.java b/src/main/java/com/amazon/opendistroforelasticsearch/ad/feature/SearchFeatureDao.java index ad3afe85..7b316bd7 100644 --- a/src/main/java/com/amazon/opendistroforelasticsearch/ad/feature/SearchFeatureDao.java +++ b/src/main/java/com/amazon/opendistroforelasticsearch/ad/feature/SearchFeatureDao.java @@ -173,10 +173,7 @@ public void getFeaturesForPeriod(AnomalyDetector detector, long startTime, long } private Optional parseResponse(SearchResponse response, List featureIds) { - return parseAggregations( - Optional.ofNullable(response).filter(resp -> response.getHits().getTotalHits().value > 0L).map(resp -> resp.getAggregations()), - featureIds - ); + return parseAggregations(Optional.ofNullable(response).map(resp -> resp.getAggregations()), featureIds); } private double parseAggregation(Aggregation aggregation) { diff --git a/src/test/java/com/amazon/opendistroforelasticsearch/ad/feature/SearchFeatureDaoTests.java b/src/test/java/com/amazon/opendistroforelasticsearch/ad/feature/SearchFeatureDaoTests.java index 46973d94..610bc7da 100644 --- a/src/test/java/com/amazon/opendistroforelasticsearch/ad/feature/SearchFeatureDaoTests.java +++ b/src/test/java/com/amazon/opendistroforelasticsearch/ad/feature/SearchFeatureDaoTests.java @@ -68,6 +68,7 @@ import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation; import org.elasticsearch.search.aggregations.metrics.InternalTDigestPercentiles; import org.elasticsearch.search.aggregations.metrics.Max; +import org.elasticsearch.search.aggregations.metrics.NumericMetricsAggregation; import org.elasticsearch.search.aggregations.metrics.Percentile; import org.elasticsearch.search.builder.SearchSourceBuilder; import org.junit.Before; @@ -97,7 +98,6 @@ @PowerMockRunnerDelegate(JUnitParamsRunner.class) @PrepareForTest({ ParseUtils.class }) public class SearchFeatureDaoTests { - private SearchFeatureDao searchFeatureDao; @Mock @@ -435,7 +435,7 @@ public void test_getFeaturesForPeriod_returnEmpty_givenNoData() throws Exception } @Test - public void getFeaturesForPeriod_returnEmpty_givenNoHits() throws Exception { + public void getFeaturesForPeriod_returnNonEmpty_givenDefaultValue() throws Exception { long start = 100L; long end = 200L; @@ -443,11 +443,22 @@ public void getFeaturesForPeriod_returnEmpty_givenNoHits() throws Exception { when(ParseUtils.generateInternalFeatureQuery(eq(detector), eq(start), eq(end), eq(xContent))).thenReturn(searchSourceBuilder); when(searchResponse.getHits()).thenReturn(new SearchHits(new SearchHit[0], new TotalHits(0L, TotalHits.Relation.EQUAL_TO), 1f)); + List aggList = new ArrayList<>(1); + + NumericMetricsAggregation.SingleValue agg = mock(NumericMetricsAggregation.SingleValue.class); + when(agg.getName()).thenReturn("deny_max"); + when(agg.value()).thenReturn(0d); + + aggList.add(agg); + + Aggregations aggregations = new Aggregations(aggList); + when(searchResponse.getAggregations()).thenReturn(aggregations); + // test Optional result = searchFeatureDao.getFeaturesForPeriod(detector, start, end); // verify - assertFalse(result.isPresent()); + assertTrue(result.isPresent()); } private Object[] getFeaturesForSampledPeriodsData() {