diff --git a/build.gradle b/build.gradle index 43f2a4101..c37888655 100644 --- a/build.gradle +++ b/build.gradle @@ -576,9 +576,6 @@ List jacocoExclusions = [ // https://github.com/opensearch-project/anomaly-detection/issues/241 'org.opensearch.ad.task.ADBatchTaskRunner', 'org.opensearch.ad.task.ADTaskManager', - - //TODO: custom result index caused coverage drop - 'org.opensearch.ad.transport.handler.AnomalyResultBulkIndexHandler' ] diff --git a/src/test/java/org/opensearch/ad/transport/handler/AnomalyResultBulkIndexHandlerTests.java b/src/test/java/org/opensearch/ad/transport/handler/AnomalyResultBulkIndexHandlerTests.java index a6959b260..1ee99af7d 100644 --- a/src/test/java/org/opensearch/ad/transport/handler/AnomalyResultBulkIndexHandlerTests.java +++ b/src/test/java/org/opensearch/ad/transport/handler/AnomalyResultBulkIndexHandlerTests.java @@ -19,6 +19,7 @@ import static org.mockito.Mockito.spy; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; import static org.opensearch.ad.constant.CommonName.ANOMALY_RESULT_INDEX_ALIAS; import java.io.IOException; @@ -94,6 +95,39 @@ public void testNullAnomalyResults() { verify(anomalyDetectionIndices, never()).doesAnomalyDetectorIndexExist(); } + public void testAnomalyResultBulkIndexHandler_IndexNotExist() { + when(anomalyDetectionIndices.doesIndexExist("testIndex")).thenReturn(false); + AnomalyResult anomalyResult = mock(AnomalyResult.class); + when(anomalyResult.getDetectorId()).thenReturn("testId"); + + bulkIndexHandler.bulkIndexAnomalyResult("testIndex", ImmutableList.of(anomalyResult), listener); + verify(listener, times(1)).onFailure(exceptionCaptor.capture()); + assertEquals("Can't find result index testIndex", exceptionCaptor.getValue().getMessage()); + } + + public void testAnomalyResultBulkIndexHandler_InValidResultIndexMapping() { + when(anomalyDetectionIndices.doesIndexExist("testIndex")).thenReturn(true); + when(anomalyDetectionIndices.isValidResultIndexMapping("testIndex")).thenReturn(false); + AnomalyResult anomalyResult = mock(AnomalyResult.class); + when(anomalyResult.getDetectorId()).thenReturn("testId"); + + bulkIndexHandler.bulkIndexAnomalyResult("testIndex", ImmutableList.of(anomalyResult), listener); + verify(listener, times(1)).onFailure(exceptionCaptor.capture()); + assertEquals("wrong index mapping of custom AD result index", exceptionCaptor.getValue().getMessage()); + } + + public void testAnomalyResultBulkIndexHandler_FailBulkIndexAnomaly() throws IOException { + when(anomalyDetectionIndices.doesIndexExist("testIndex")).thenReturn(true); + when(anomalyDetectionIndices.isValidResultIndexMapping("testIndex")).thenReturn(true); + AnomalyResult anomalyResult = mock(AnomalyResult.class); + when(anomalyResult.getDetectorId()).thenReturn("testId"); + when(anomalyResult.toXContent(any(), any())).thenThrow(new RuntimeException()); + + bulkIndexHandler.bulkIndexAnomalyResult("testIndex", ImmutableList.of(anomalyResult), listener); + verify(listener, times(1)).onFailure(exceptionCaptor.capture()); + assertEquals("Failed to prepare request to bulk index anomaly results", exceptionCaptor.getValue().getMessage()); + } + public void testCreateADResultIndexNotAcknowledged() throws IOException { doAnswer(invocation -> { ActionListener listener = invocation.getArgument(0);