Skip to content

Commit

Permalink
Increase more coverage and reduce jacocoExclusions
Browse files Browse the repository at this point in the history
  • Loading branch information
Zhangxunmt committed May 3, 2022
1 parent b17d483 commit 5f2e310
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
3 changes: 0 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -576,9 +576,6 @@ List<String> 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'
]


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<CreateIndexResponse> listener = invocation.getArgument(0);
Expand Down

0 comments on commit 5f2e310

Please sign in to comment.