Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Commit

Permalink
Adding tests for Preview Transport Action
Browse files Browse the repository at this point in the history
  • Loading branch information
saratvemulapalli committed Dec 21, 2020
1 parent d2dfb61 commit 84e0b85
Show file tree
Hide file tree
Showing 2 changed files with 176 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import org.elasticsearch.action.support.HandledTransportAction;
import org.elasticsearch.client.Client;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.CheckedConsumer;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.util.concurrent.ThreadContext;
Expand Down Expand Up @@ -109,22 +108,24 @@ private ActionListener<List<AnomalyResult>> getPreviewDetectorActionListener(
ActionListener<PreviewAnomalyDetectorResponse> listener,
AnomalyDetector detector
) {
return ActionListener.wrap(new CheckedConsumer<List<AnomalyResult>, Exception>() {
return new ActionListener<List<AnomalyResult>>() {
@Override
public void accept(List<AnomalyResult> anomalyResult) throws Exception {
PreviewAnomalyDetectorResponse response = new PreviewAnomalyDetectorResponse(anomalyResult, detector);
public void onResponse(List<AnomalyResult> anomalyResults) {
PreviewAnomalyDetectorResponse response = new PreviewAnomalyDetectorResponse(anomalyResults, detector);
listener.onResponse(response);
}
}, exception -> {
logger.error("Unexpected error running anomaly detector " + detector.getDetectorId(), exception);
listener
.onFailure(
new ElasticsearchException(
"Unexpected error running anomaly detector " + detector.getDetectorId(),
RestStatus.INTERNAL_SERVER_ERROR
)
);
});

@Override
public void onFailure(Exception e) {
listener
.onFailure(
new ElasticsearchException(
"Unexpected error running anomaly detector " + detector.getDetectorId(),
RestStatus.INTERNAL_SERVER_ERROR
)
);
}
};
}

private void previewAnomalyDetector(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,197 @@

package com.amazon.opendistroforelasticsearch.ad.transport;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.io.IOException;
import java.time.Instant;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.concurrent.TimeUnit;

import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.support.ActionFilters;
import org.elasticsearch.client.Client;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.io.stream.BytesStreamOutput;
import org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput;
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
import org.elasticsearch.common.settings.ClusterSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.tasks.Task;
import org.elasticsearch.test.ESSingleNodeTestCase;
import org.elasticsearch.transport.TransportService;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import com.amazon.opendistroforelasticsearch.ad.AnomalyDetectorRunner;
import com.amazon.opendistroforelasticsearch.ad.TestHelpers;
import com.amazon.opendistroforelasticsearch.ad.feature.FeatureManager;
import com.amazon.opendistroforelasticsearch.ad.ml.ModelManager;
import com.amazon.opendistroforelasticsearch.ad.model.AnomalyDetector;
import com.amazon.opendistroforelasticsearch.ad.model.AnomalyResult;
import com.amazon.opendistroforelasticsearch.ad.settings.AnomalyDetectorSettings;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;

public class PreviewAnomalyDetectorActionTests extends ESSingleNodeTestCase {
private ActionListener<PreviewAnomalyDetectorResponse> response;
private PreviewAnomalyDetectorTransportAction action;
private AnomalyDetectorRunner runner;
private ClusterService clusterService;
private Task task;

@Override
@Before
public void setUp() throws Exception {
super.setUp();
task = mock(Task.class);
clusterService = mock(ClusterService.class);
ClusterSettings clusterSettings = new ClusterSettings(
Settings.EMPTY,
Collections.unmodifiableSet(new HashSet<>(Arrays.asList(AnomalyDetectorSettings.MAX_ANOMALY_FEATURES)))
);
when(clusterService.getClusterSettings()).thenReturn(clusterSettings);
runner = new AnomalyDetectorRunner(
mock(ModelManager.class),
mock(FeatureManager.class),
AnomalyDetectorSettings.MAX_PREVIEW_RESULTS
);
action = new PreviewAnomalyDetectorTransportAction(
Settings.EMPTY,
mock(TransportService.class),
clusterService,
mock(ActionFilters.class),
client(),
runner,
xContentRegistry()
);
}

@Override
protected NamedWriteableRegistry writableRegistry() {
return getInstanceFromNode(NamedWriteableRegistry.class);
}

@Test
public void testPreviewTransportActionWithNoFeature() throws IOException {
// Detector with no feature, Preview should fail
AnomalyDetector detector = TestHelpers.randomAnomalyDetector(Collections.emptyList());
PreviewAnomalyDetectorRequest request = new PreviewAnomalyDetectorRequest(
detector,
detector.getDetectorId(),
Instant.now(),
Instant.now()
);
ActionListener<PreviewAnomalyDetectorResponse> previewResponse = new ActionListener<PreviewAnomalyDetectorResponse>() {
@Override
public void onResponse(PreviewAnomalyDetectorResponse response) {
Assert.assertTrue(false);
}

@Override
public void onFailure(Exception e) {
Assert.assertTrue(e.getMessage().contains("Can't preview detector without feature"));
}
};
action.doExecute(task, request, previewResponse);
}

@Test
public void testPreviewTransportActionWithNoDetector() throws IOException {
// When detectorId is null, preview should fail
PreviewAnomalyDetectorRequest request = new PreviewAnomalyDetectorRequest(null, "", Instant.now(), Instant.now());
ActionListener<PreviewAnomalyDetectorResponse> previewResponse = new ActionListener<PreviewAnomalyDetectorResponse>() {
@Override
public void onResponse(PreviewAnomalyDetectorResponse response) {
Assert.assertTrue(false);
}

@Override
public void onFailure(Exception e) {
Assert.assertTrue(e.getMessage().contains("Wrong input, no detector id"));
}
};
action.doExecute(task, request, previewResponse);
}

@Test
public void testPreviewTransportActionWithDetectorID() throws IOException {
// When AD index does not exist, cannot query the detector
PreviewAnomalyDetectorRequest request = new PreviewAnomalyDetectorRequest(null, "1234", Instant.now(), Instant.now());
ActionListener<PreviewAnomalyDetectorResponse> previewResponse = new ActionListener<PreviewAnomalyDetectorResponse>() {
@Override
public void onResponse(PreviewAnomalyDetectorResponse response) {
Assert.assertTrue(false);
}

@Override
public void onFailure(Exception e) {
Assert.assertTrue(e.getMessage().contains("Could not execute get query to find detector"));
}
};
action.doExecute(task, request, previewResponse);
}

@Test
public void testPreviewTransportActionWithIndex() throws IOException {
// When AD index exists, and detector does not exist
PreviewAnomalyDetectorRequest request = new PreviewAnomalyDetectorRequest(null, "1234", Instant.now(), Instant.now());
Settings indexSettings = Settings.builder().put("index.number_of_shards", 5).put("index.number_of_replicas", 1).build();
CreateIndexRequest indexRequest = new CreateIndexRequest(AnomalyDetector.ANOMALY_DETECTORS_INDEX, indexSettings);
client().admin().indices().create(indexRequest).actionGet();
ActionListener<PreviewAnomalyDetectorResponse> previewResponse = new ActionListener<PreviewAnomalyDetectorResponse>() {
@Override
public void onResponse(PreviewAnomalyDetectorResponse response) {
Assert.assertTrue(false);
}

@Override
public void onFailure(Exception e) {
Assert.assertTrue(e.getMessage().contains("Can't find anomaly detector with id:1234"));
}
};
action.doExecute(task, request, previewResponse);
}

@Test
public void testPreviewTransportActionNoContext() throws IOException {
Client client = mock(Client.class);
PreviewAnomalyDetectorTransportAction previewAction = new PreviewAnomalyDetectorTransportAction(
Settings.EMPTY,
mock(TransportService.class),
clusterService,
mock(ActionFilters.class),
client,
runner,
xContentRegistry()
);
AnomalyDetector detector = TestHelpers.randomAnomalyDetector(ImmutableMap.of("testKey", "testValue"), Instant.now());
PreviewAnomalyDetectorRequest request = new PreviewAnomalyDetectorRequest(
detector,
detector.getDetectorId(),
Instant.now(),
Instant.now()
);
ActionListener<PreviewAnomalyDetectorResponse> previewResponse = new ActionListener<PreviewAnomalyDetectorResponse>() {
@Override
public void onResponse(PreviewAnomalyDetectorResponse response) {
Assert.assertTrue(false);
}

@Override
public void onFailure(Exception e) {
Assert.assertTrue(e.getClass() == NullPointerException.class);
}
};
previewAction.doExecute(task, request, previewResponse);
}

@Test
public void testPreviewRequest() throws Exception {
BytesStreamOutput out = new BytesStreamOutput();
Expand Down

0 comments on commit 84e0b85

Please sign in to comment.