Skip to content

Commit b876a22

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

File tree

1 file changed

+78
-3
lines changed

1 file changed

+78
-3
lines changed

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

+78-3
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ public void testCreateAnomalyDetector_withFlattenedResultIndex() throws Exceptio
241241
.format(Locale.ROOT, "/opensearch-ad-plugin-result-test_flattened_%s", id.toLowerCase(Locale.ROOT));
242242
// wait for the detector starts writing result
243243
try {
244-
Thread.sleep(60 * 1000);
244+
Thread.sleep(20 * 1000);
245245
} catch (InterruptedException e) {
246246
Thread.currentThread().interrupt();
247247
throw new RuntimeException("Thread was interrupted while waiting", e);
@@ -256,6 +256,81 @@ public void testCreateAnomalyDetector_withFlattenedResultIndex() throws Exceptio
256256
assertEquals("Dynamic field is not set to true", "true", dynamicValue.toString());
257257
Map<String, Object> properties = (Map<String, Object>) mappings.get("properties");
258258
assertTrue("Flattened field 'feature_data_feature_bytes' does not exist", properties.containsKey("feature_data_feature_bytes"));
259+
260+
}
261+
262+
public void testUpdateAnomalyDetector_enableFlattenResultIndex_shouldCreatePipeline() throws Exception {
263+
AnomalyDetector detector = createIndexAndGetAnomalyDetector(
264+
INDEX_NAME,
265+
ImmutableList.of(TestHelpers.randomFeature("feature_bytes", "agg", true)),
266+
false,
267+
false
268+
);
269+
270+
// test behavior when AD is enabled
271+
updateClusterSettings(ADEnabledSetting.AD_ENABLED, true);
272+
// create a detector with flatten result index disabled, shouldn't find related ingest pipeline
273+
Response response = TestHelpers
274+
.makeRequest(client(), "POST", TestHelpers.AD_BASE_DETECTORS_URI, ImmutableMap.of(), TestHelpers.toHttpEntity(detector), null);
275+
assertEquals("Create anomaly detector with flattened result index failed", RestStatus.CREATED, TestHelpers.restStatus(response));
276+
Map<String, Object> responseMap = entityAsMap(response);
277+
String id = (String) responseMap.get("_id");
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+
ResponseException responseException = expectThrows(
281+
ResponseException.class,
282+
() -> TestHelpers.makeRequest(client(), "GET", getIngestPipelineEndpoint, ImmutableMap.of(), "", null)
283+
);
284+
int statusCode = responseException.getResponse().getStatusLine().getStatusCode();
285+
assertEquals("Expected 404 response but got: " + statusCode, 404, statusCode);
286+
287+
// update the detector with flatten result index enabled, should be able to find ingest pipeline
288+
List<Feature> features = detector.getFeatureAttributes();
289+
AnomalyDetector newDetector = new AnomalyDetector(
290+
id,
291+
detector.getVersion(),
292+
detector.getName(),
293+
detector.getDescription(),
294+
detector.getTimeField(),
295+
detector.getIndices(),
296+
features,
297+
detector.getFilterQuery(),
298+
detector.getInterval(),
299+
detector.getWindowDelay(),
300+
detector.getShingleSize(),
301+
detector.getUiMetadata(),
302+
detector.getSchemaVersion(),
303+
detector.getLastUpdateTime(),
304+
null,
305+
detector.getUser(),
306+
detector.getCustomResultIndexOrAlias(),
307+
TestHelpers.randomImputationOption(features),
308+
randomIntBetween(1, 10000),
309+
randomInt(TimeSeriesSettings.MAX_SHINGLE_SIZE / 2),
310+
randomIntBetween(1, 1000),
311+
null,
312+
null,
313+
null,
314+
null,
315+
true,
316+
detector.getLastBreakingUIChangeTime()
317+
);
318+
Response updateResponse = TestHelpers
319+
.makeRequest(
320+
client(),
321+
"PUT",
322+
TestHelpers.AD_BASE_DETECTORS_URI + "/" + id + "?refresh=true",
323+
ImmutableMap.of(),
324+
TestHelpers.toHttpEntity(newDetector),
325+
null
326+
);
327+
assertEquals("Update anomaly detector failed", RestStatus.OK, TestHelpers.restStatus(updateResponse));
328+
Response getPipelineResponse = TestHelpers.makeRequest(client(), "GET", getIngestPipelineEndpoint, ImmutableMap.of(), "", null);
329+
assertEquals(
330+
"Expected 200 response but got: " + getPipelineResponse.getStatusLine().getStatusCode(),
331+
200,
332+
getPipelineResponse.getStatusLine().getStatusCode()
333+
);
259334
}
260335

261336
public void testUpdateAnomalyDetector_disableFlattenResultIndex_shouldDeletePipeline() throws Exception {
@@ -273,8 +348,8 @@ public void testUpdateAnomalyDetector_disableFlattenResultIndex_shouldDeletePipe
273348
assertEquals("Create anomaly detector with flattened result index failed", RestStatus.CREATED, TestHelpers.restStatus(response));
274349
Map<String, Object> responseMap = entityAsMap(response);
275350
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));
351+
// String expectedFlattenedIndex = String
352+
// .format(Locale.ROOT, "opensearch-ad-plugin-result-test_flattened_%s", id.toLowerCase(Locale.ROOT));
278353
String expectedPipelineId = String.format(Locale.ROOT, "flatten_result_index_ingest_pipeline%s", id.toLowerCase(Locale.ROOT));
279354
String getIngestPipelineEndpoint = String.format(Locale.ROOT, "_ingest/pipeline/%s", expectedPipelineId);
280355
Response getPipelineResponse = TestHelpers.makeRequest(client(), "GET", getIngestPipelineEndpoint, ImmutableMap.of(), "", null);

0 commit comments

Comments
 (0)