Skip to content

Commit 9ffc24f

Browse files
committed
add more IT
Signed-off-by: Jackie Han <hnyng@amazon.com>
1 parent f079baf commit 9ffc24f

File tree

1 file changed

+75
-1
lines changed

1 file changed

+75
-1
lines changed

src/test/java/org/opensearch/ad/rest/AnomalyDetectorRestApiIT.java

+75-1
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ public void testCreateAnomalyDetectorWithDuplicateName() throws Exception {
194194
);
195195
}
196196

197-
public void testCreateAnomalyDetectorWithFlattenedResultIndex() throws Exception {
197+
public void testCreateAnomalyDetector_withFlattenedResultIndex() throws Exception {
198198
AnomalyDetector detector = createIndexAndGetAnomalyDetector(
199199
INDEX_NAME,
200200
ImmutableList.of(TestHelpers.randomFeature("feature_bytes", "agg", true)),
@@ -258,6 +258,80 @@ public void testCreateAnomalyDetectorWithFlattenedResultIndex() throws Exception
258258
assertTrue("Flattened field 'feature_data_feature_bytes' does not exist", properties.containsKey("feature_data_feature_bytes"));
259259
}
260260

261+
public void testUpdateAnomalyDetector_disableFlattenResultIndex_shouldDeletePipeline() throws Exception {
262+
AnomalyDetector detector = createIndexAndGetAnomalyDetector(
263+
INDEX_NAME,
264+
ImmutableList.of(TestHelpers.randomFeature("feature_bytes", "agg", true)),
265+
false,
266+
true
267+
);
268+
269+
// test behavior when AD is enabled
270+
updateClusterSettings(ADEnabledSetting.AD_ENABLED, true);
271+
Response response = TestHelpers
272+
.makeRequest(client(), "POST", TestHelpers.AD_BASE_DETECTORS_URI, ImmutableMap.of(), TestHelpers.toHttpEntity(detector), null);
273+
assertEquals("Create anomaly detector with flattened result index failed", RestStatus.CREATED, TestHelpers.restStatus(response));
274+
Map<String, Object> responseMap = entityAsMap(response);
275+
String id = (String) responseMap.get("_id");
276+
String expectedFlattenedIndex = String
277+
.format(Locale.ROOT, "opensearch-ad-plugin-result-test_flattened_%s", id.toLowerCase(Locale.ROOT));
278+
String expectedPipelineId = String.format(Locale.ROOT, "flatten_result_index_ingest_pipeline%s", id.toLowerCase(Locale.ROOT));
279+
String getIngestPipelineEndpoint = String.format(Locale.ROOT, "_ingest/pipeline/%s", expectedPipelineId);
280+
Response getPipelineResponse = TestHelpers.makeRequest(client(), "GET", getIngestPipelineEndpoint, ImmutableMap.of(), "", null);
281+
assertEquals(
282+
"Expected 200 response but got: " + getPipelineResponse.getStatusLine().getStatusCode(),
283+
200,
284+
getPipelineResponse.getStatusLine().getStatusCode()
285+
);
286+
List<Feature> features = detector.getFeatureAttributes();
287+
AnomalyDetector newDetector = new AnomalyDetector(
288+
id,
289+
detector.getVersion(),
290+
detector.getName(),
291+
detector.getDescription(),
292+
detector.getTimeField(),
293+
detector.getIndices(),
294+
features,
295+
detector.getFilterQuery(),
296+
detector.getInterval(),
297+
detector.getWindowDelay(),
298+
detector.getShingleSize(),
299+
detector.getUiMetadata(),
300+
detector.getSchemaVersion(),
301+
detector.getLastUpdateTime(),
302+
null,
303+
detector.getUser(),
304+
detector.getCustomResultIndexOrAlias(),
305+
TestHelpers.randomImputationOption(features),
306+
randomIntBetween(1, 10000),
307+
randomInt(TimeSeriesSettings.MAX_SHINGLE_SIZE / 2),
308+
randomIntBetween(1, 1000),
309+
null,
310+
null,
311+
null,
312+
null,
313+
false,
314+
detector.getLastBreakingUIChangeTime()
315+
);
316+
Response updateResponse = TestHelpers
317+
.makeRequest(
318+
client(),
319+
"PUT",
320+
TestHelpers.AD_BASE_DETECTORS_URI + "/" + id + "?refresh=true",
321+
ImmutableMap.of(),
322+
TestHelpers.toHttpEntity(newDetector),
323+
null
324+
);
325+
assertEquals("Update anomaly detector failed", RestStatus.OK, TestHelpers.restStatus(updateResponse));
326+
Response getPipelineResponseAfterUpdatingDetector = TestHelpers
327+
.makeRequest(client(), "GET", getIngestPipelineEndpoint, ImmutableMap.of(), "", null);
328+
assertEquals(
329+
"Expected 404 response but got: " + getPipelineResponseAfterUpdatingDetector.getStatusLine().getStatusCode(),
330+
404,
331+
getPipelineResponseAfterUpdatingDetector.getStatusLine().getStatusCode()
332+
);
333+
}
334+
261335
public void testCreateAnomalyDetector() throws Exception {
262336
AnomalyDetector detector = createIndexAndGetAnomalyDetector(INDEX_NAME);
263337
updateClusterSettings(ADEnabledSetting.AD_ENABLED, false);

0 commit comments

Comments
 (0)