use of org.opensearch.action.get.GetRequest in project anomaly-detection by opensearch-project.
the class MultiEntityProfileRunnerTests method setUp.
@SuppressWarnings("unchecked")
@Before
@Override
public void setUp() throws Exception {
super.setUp();
client = mock(Client.class);
nodeFilter = mock(DiscoveryNodeFilterer.class);
requiredSamples = 128;
detectorId = "A69pa3UBHuCbh-emo9oR";
detector = TestHelpers.randomAnomalyDetectorUsingCategoryFields(detectorId, Arrays.asList("a"));
result = new DetectorInternalState.Builder().lastUpdateTime(Instant.now());
job = TestHelpers.randomAnomalyDetectorJob(true);
adTaskManager = mock(ADTaskManager.class);
transportService = mock(TransportService.class);
doAnswer(invocation -> {
Object[] args = invocation.getArguments();
Consumer<Optional<ADTask>> function = (Consumer<Optional<ADTask>>) args[2];
function.accept(Optional.of(TestHelpers.randomAdTask()));
return null;
}).when(adTaskManager).getAndExecuteOnLatestDetectorLevelTask(any(), any(), any(), any(), anyBoolean(), any());
runner = new AnomalyDetectorProfileRunner(client, xContentRegistry(), nodeFilter, requiredSamples, transportService, adTaskManager);
doAnswer(invocation -> {
Object[] args = invocation.getArguments();
GetRequest request = (GetRequest) args[0];
ActionListener<GetResponse> listener = (ActionListener<GetResponse>) args[1];
String indexName = request.index();
if (indexName.equals(ANOMALY_DETECTORS_INDEX)) {
listener.onResponse(TestHelpers.createGetResponse(detector, detector.getDetectorId(), AnomalyDetector.ANOMALY_DETECTORS_INDEX));
} else if (indexName.equals(CommonName.DETECTION_STATE_INDEX)) {
listener.onResponse(TestHelpers.createGetResponse(result.build(), detector.getDetectorId(), CommonName.DETECTION_STATE_INDEX));
} else if (indexName.equals(ANOMALY_DETECTOR_JOB_INDEX)) {
listener.onResponse(TestHelpers.createGetResponse(job, detector.getDetectorId(), AnomalyDetectorJob.ANOMALY_DETECTOR_JOB_INDEX));
}
return null;
}).when(client).get(any(), any());
stateNError = new HashSet<DetectorProfileName>();
stateNError.add(DetectorProfileName.ERROR);
stateNError.add(DetectorProfileName.STATE);
}
use of org.opensearch.action.get.GetRequest in project anomaly-detection by opensearch-project.
the class NodeStateManagerTests method setupCheckpoint.
@SuppressWarnings("unchecked")
private void setupCheckpoint(boolean responseExists) throws IOException {
when(checkpointResponse.isExists()).thenReturn(responseExists);
doAnswer(invocation -> {
Object[] args = invocation.getArguments();
assertTrue(String.format("The size of args is %d. Its content is %s", args.length, Arrays.toString(args)), args.length >= 2);
GetRequest request = null;
ActionListener<GetResponse> listener = null;
if (args[0] instanceof GetRequest) {
request = (GetRequest) args[0];
}
if (args[1] instanceof ActionListener) {
listener = (ActionListener<GetResponse>) args[1];
}
assertTrue(request != null && listener != null);
listener.onResponse(checkpointResponse);
return null;
}).when(client).get(any(), any(ActionListener.class));
}
use of org.opensearch.action.get.GetRequest in project anomaly-detection by opensearch-project.
the class EntityProfileRunnerTests method testNotMultiEntityDetector.
@SuppressWarnings("unchecked")
public void testNotMultiEntityDetector() throws IOException, InterruptedException {
detector = TestHelpers.randomAnomalyDetectorWithInterval(new IntervalTimeConfiguration(detectorIntervalMin, ChronoUnit.MINUTES));
doAnswer(invocation -> {
Object[] args = invocation.getArguments();
GetRequest request = (GetRequest) args[0];
ActionListener<GetResponse> listener = (ActionListener<GetResponse>) args[1];
String indexName = request.index();
if (indexName.equals(ANOMALY_DETECTORS_INDEX)) {
listener.onResponse(TestHelpers.createGetResponse(detector, detector.getDetectorId(), AnomalyDetector.ANOMALY_DETECTORS_INDEX));
}
return null;
}).when(client).get(any(), any());
final CountDownLatch inProgressLatch = new CountDownLatch(1);
runner.profile(detectorId, entity, state, ActionListener.wrap(response -> {
assertTrue("Should not reach here", false);
inProgressLatch.countDown();
}, exception -> {
assertTrue(exception.getMessage().contains(EntityProfileRunner.NOT_HC_DETECTOR_ERR_MSG));
inProgressLatch.countDown();
}));
assertTrue(inProgressLatch.await(100, TimeUnit.SECONDS));
}
use of org.opensearch.action.get.GetRequest in project anomaly-detection by opensearch-project.
the class EntityProfileRunnerTests method testJobIndexNotFound.
@SuppressWarnings("unchecked")
public void testJobIndexNotFound() throws InterruptedException {
setUpExecuteEntityProfileAction(InittedEverResultStatus.INITTED);
final CountDownLatch inProgressLatch = new CountDownLatch(1);
doAnswer(invocation -> {
Object[] args = invocation.getArguments();
GetRequest request = (GetRequest) args[0];
ActionListener<GetResponse> listener = (ActionListener<GetResponse>) args[1];
String indexName = request.index();
if (indexName.equals(ANOMALY_DETECTORS_INDEX)) {
listener.onResponse(TestHelpers.createGetResponse(detector, detector.getDetectorId(), AnomalyDetector.ANOMALY_DETECTORS_INDEX));
} else if (indexName.equals(ANOMALY_DETECTOR_JOB_INDEX)) {
listener.onFailure(new IndexNotFoundException(ANOMALY_DETECTOR_JOB_INDEX));
}
return null;
}).when(client).get(any(), any());
EntityProfile expectedProfile = new EntityProfile.Builder().build();
runner.profile(detectorId, entity, initNInfo, ActionListener.wrap(response -> {
assertEquals(expectedProfile, response);
inProgressLatch.countDown();
}, exception -> {
LOG.error("Unexpected error", exception);
assertTrue("Should not reach here", false);
inProgressLatch.countDown();
}));
assertTrue(inProgressLatch.await(100, TimeUnit.SECONDS));
}
use of org.opensearch.action.get.GetRequest in project anomaly-detection by opensearch-project.
the class AnomalyDetectorProfileRunnerTests method setUpClientGet.
/**
* Convenience methods for single-stream detector profile tests set up
* @param detectorStatus Detector config status
* @param jobStatus Detector job status
* @param rcfPollingStatus RCF polling result status
* @param errorResultStatus Error result status
* @throws IOException when failing the getting request
*/
@SuppressWarnings("unchecked")
private void setUpClientGet(DetectorStatus detectorStatus, JobStatus jobStatus, RCFPollingStatus rcfPollingStatus, ErrorResultStatus errorResultStatus) throws IOException {
detector = TestHelpers.randomAnomalyDetectorWithInterval(new IntervalTimeConfiguration(detectorIntervalMin, ChronoUnit.MINUTES));
doAnswer(invocation -> {
Object[] args = invocation.getArguments();
GetRequest request = (GetRequest) args[0];
ActionListener<GetResponse> listener = (ActionListener<GetResponse>) args[1];
if (request.index().equals(ANOMALY_DETECTORS_INDEX)) {
switch(detectorStatus) {
case EXIST:
listener.onResponse(TestHelpers.createGetResponse(detector, detector.getDetectorId(), AnomalyDetector.ANOMALY_DETECTORS_INDEX));
break;
case INDEX_NOT_EXIST:
listener.onFailure(new IndexNotFoundException(ANOMALY_DETECTORS_INDEX));
break;
case NO_DOC:
when(detectorGetReponse.isExists()).thenReturn(false);
listener.onResponse(detectorGetReponse);
break;
default:
assertTrue("should not reach here", false);
break;
}
} else if (request.index().equals(ANOMALY_DETECTOR_JOB_INDEX)) {
AnomalyDetectorJob job = null;
switch(jobStatus) {
case INDEX_NOT_EXIT:
listener.onFailure(new IndexNotFoundException(ANOMALY_DETECTOR_JOB_INDEX));
break;
case DISABLED:
job = TestHelpers.randomAnomalyDetectorJob(false, jobEnabledTime, null);
listener.onResponse(TestHelpers.createGetResponse(job, detector.getDetectorId(), AnomalyDetectorJob.ANOMALY_DETECTOR_JOB_INDEX));
break;
case ENABLED:
job = TestHelpers.randomAnomalyDetectorJob(true, jobEnabledTime, null);
listener.onResponse(TestHelpers.createGetResponse(job, detector.getDetectorId(), AnomalyDetectorJob.ANOMALY_DETECTOR_JOB_INDEX));
break;
default:
assertTrue("should not reach here", false);
break;
}
} else {
if (errorResultStatus == ErrorResultStatus.INDEX_NOT_EXIT) {
listener.onFailure(new IndexNotFoundException(CommonName.DETECTION_STATE_INDEX));
return null;
}
DetectorInternalState.Builder result = new DetectorInternalState.Builder().lastUpdateTime(Instant.now());
String error = getError(errorResultStatus);
if (error != null) {
result.error(error);
}
listener.onResponse(TestHelpers.createGetResponse(result.build(), detector.getDetectorId(), CommonName.DETECTION_STATE_INDEX));
}
return null;
}).when(client).get(any(), any());
setUpClientExecuteRCFPollingAction(rcfPollingStatus);
}
Aggregations