@@ -194,7 +194,7 @@ public void testCreateAnomalyDetectorWithDuplicateName() throws Exception {
194
194
);
195
195
}
196
196
197
- public void testCreateAnomalyDetectorWithFlattenedResultIndex () throws Exception {
197
+ public void testCreateAnomalyDetector_withFlattenedResultIndex () throws Exception {
198
198
AnomalyDetector detector = createIndexAndGetAnomalyDetector (
199
199
INDEX_NAME ,
200
200
ImmutableList .of (TestHelpers .randomFeature ("feature_bytes" , "agg" , true )),
@@ -258,6 +258,79 @@ public void testCreateAnomalyDetectorWithFlattenedResultIndex() throws Exception
258
258
assertTrue ("Flattened field 'feature_data_feature_bytes' does not exist" , properties .containsKey ("feature_data_feature_bytes" ));
259
259
}
260
260
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
+ ResponseException responseException = expectThrows (
327
+ ResponseException .class ,
328
+ () -> TestHelpers .makeRequest (client (), "GET" , getIngestPipelineEndpoint , ImmutableMap .of (), "" , null )
329
+ );
330
+ int statusCode = responseException .getResponse ().getStatusLine ().getStatusCode ();
331
+ assertEquals ("Expected 404 response but got: " + statusCode , 404 , statusCode );
332
+ }
333
+
261
334
public void testCreateAnomalyDetector () throws Exception {
262
335
AnomalyDetector detector = createIndexAndGetAnomalyDetector (INDEX_NAME );
263
336
updateClusterSettings (ADEnabledSetting .AD_ENABLED , false );
0 commit comments