diff --git a/src/test/java/org/opensearch/ad/rest/AnomalyDetectorRestApiIT.java b/src/test/java/org/opensearch/ad/rest/AnomalyDetectorRestApiIT.java index 65516a241..2deec995f 100644 --- a/src/test/java/org/opensearch/ad/rest/AnomalyDetectorRestApiIT.java +++ b/src/test/java/org/opensearch/ad/rest/AnomalyDetectorRestApiIT.java @@ -194,7 +194,7 @@ public void testCreateAnomalyDetectorWithDuplicateName() throws Exception { ); } - public void testCreateAnomalyDetectorWithFlattenedResultIndex() throws Exception { + public void testCreateAnomalyDetector_withFlattenedResultIndex() throws Exception { AnomalyDetector detector = createIndexAndGetAnomalyDetector( INDEX_NAME, ImmutableList.of(TestHelpers.randomFeature("feature_bytes", "agg", true)), @@ -258,6 +258,80 @@ public void testCreateAnomalyDetectorWithFlattenedResultIndex() throws Exception assertTrue("Flattened field 'feature_data_feature_bytes' does not exist", properties.containsKey("feature_data_feature_bytes")); } + public void testUpdateAnomalyDetector_disableFlattenResultIndex_shouldDeletePipeline() throws Exception { + AnomalyDetector detector = createIndexAndGetAnomalyDetector( + INDEX_NAME, + ImmutableList.of(TestHelpers.randomFeature("feature_bytes", "agg", true)), + false, + true + ); + + // test behavior when AD is enabled + updateClusterSettings(ADEnabledSetting.AD_ENABLED, true); + Response response = TestHelpers + .makeRequest(client(), "POST", TestHelpers.AD_BASE_DETECTORS_URI, ImmutableMap.of(), TestHelpers.toHttpEntity(detector), null); + assertEquals("Create anomaly detector with flattened result index failed", RestStatus.CREATED, TestHelpers.restStatus(response)); + Map responseMap = entityAsMap(response); + String id = (String) responseMap.get("_id"); + String expectedFlattenedIndex = String + .format(Locale.ROOT, "opensearch-ad-plugin-result-test_flattened_%s", id.toLowerCase(Locale.ROOT)); + String expectedPipelineId = String.format(Locale.ROOT, "flatten_result_index_ingest_pipeline%s", id.toLowerCase(Locale.ROOT)); + String getIngestPipelineEndpoint = String.format(Locale.ROOT, "_ingest/pipeline/%s", expectedPipelineId); + Response getPipelineResponse = TestHelpers.makeRequest(client(), "GET", expectedPipelineId, ImmutableMap.of(), "", null); + assertEquals( + "Expected 200 response but got: " + getPipelineResponse.getStatusLine().getStatusCode(), + 200, + getPipelineResponse.getStatusLine().getStatusCode() + ); + List features = detector.getFeatureAttributes(); + AnomalyDetector newDetector = new AnomalyDetector( + detector.getId(), + detector.getVersion(), + detector.getName(), + detector.getDescription(), + detector.getTimeField(), + detector.getIndices(), + features, + detector.getFilterQuery(), + detector.getInterval(), + detector.getWindowDelay(), + detector.getShingleSize(), + detector.getUiMetadata(), + detector.getSchemaVersion(), + detector.getLastUpdateTime(), + null, + detector.getUser(), + detector.getCustomResultIndexOrAlias(), + TestHelpers.randomImputationOption(features), + randomIntBetween(1, 10000), + randomInt(TimeSeriesSettings.MAX_SHINGLE_SIZE / 2), + randomIntBetween(1, 1000), + null, + null, + null, + null, + false, + detector.getLastBreakingUIChangeTime() + ); + Response updateResponse = TestHelpers + .makeRequest( + client(), + "PUT", + TestHelpers.AD_BASE_DETECTORS_URI + "/" + detector.getId() + "?refresh=true", + ImmutableMap.of(), + TestHelpers.toHttpEntity(newDetector), + null + ); + assertEquals("Update anomaly detector failed", RestStatus.OK, TestHelpers.restStatus(updateResponse)); + Response getPipelineResponseAfterUpdatingDetector = TestHelpers + .makeRequest(client(), "GET", expectedPipelineId, ImmutableMap.of(), "", null); + assertEquals( + "Expected 404 response but got: " + getPipelineResponseAfterUpdatingDetector.getStatusLine().getStatusCode(), + 404, + getPipelineResponseAfterUpdatingDetector.getStatusLine().getStatusCode() + ); + } + public void testCreateAnomalyDetector() throws Exception { AnomalyDetector detector = createIndexAndGetAnomalyDetector(INDEX_NAME); updateClusterSettings(ADEnabledSetting.AD_ENABLED, false);