diff --git a/src/test/java/org/opensearch/ad/rest/AnomalyDetectorRestApiIT.java b/src/test/java/org/opensearch/ad/rest/AnomalyDetectorRestApiIT.java index 220778e18..cd87f5e89 100644 --- a/src/test/java/org/opensearch/ad/rest/AnomalyDetectorRestApiIT.java +++ b/src/test/java/org/opensearch/ad/rest/AnomalyDetectorRestApiIT.java @@ -122,19 +122,23 @@ private AnomalyDetector createIndexAndGetAnomalyDetector(String indexName, List< } private AnomalyDetector createIndexAndGetAnomalyDetector(String indexName, List features, boolean useDateNanos) - throws IOException { + throws IOException { return createIndexAndGetAnomalyDetector(indexName, features, useDateNanos, false); } - private AnomalyDetector createIndexAndGetAnomalyDetector(String indexName, List features, boolean useDateNanos, - boolean useFlattenResultIndex) throws IOException { + private AnomalyDetector createIndexAndGetAnomalyDetector( + String indexName, + List features, + boolean useDateNanos, + boolean useFlattenResultIndex + ) throws IOException { TestHelpers.createIndexWithTimeField(client(), indexName, TIME_FIELD, useDateNanos); String testIndexData = "{\"keyword-field\": \"field-1\", \"ip-field\": \"1.2.3.4\", \"timestamp\": 1}"; TestHelpers.ingestDataToIndex(client(), indexName, TestHelpers.toHttpEntity(testIndexData)); AnomalyDetector detector = useFlattenResultIndex - ? TestHelpers.randomAnomalyDetectorWithFlattenResultIndex(TIME_FIELD, indexName, features) - : TestHelpers.randomAnomalyDetector(TIME_FIELD, indexName, features); + ? TestHelpers.randomAnomalyDetectorWithFlattenResultIndex(TIME_FIELD, indexName, features) + : TestHelpers.randomAnomalyDetector(TIME_FIELD, indexName, features); return detector; } @@ -190,29 +194,33 @@ public void testCreateAnomalyDetectorWithDuplicateName() throws Exception { } public void testCreateAnomalyDetectorWithFlattenedResultIndex() throws Exception { - AnomalyDetector detector = createIndexAndGetAnomalyDetector(INDEX_NAME, - ImmutableList.of(TestHelpers.randomFeature(true)), false, true); + AnomalyDetector detector = createIndexAndGetAnomalyDetector( + INDEX_NAME, + ImmutableList.of(TestHelpers.randomFeature(true)), + false, + true + ); // test behavior when AD is disabled updateClusterSettings(ADEnabledSetting.AD_ENABLED, false); Exception ex = expectThrows( - ResponseException.class, - () -> TestHelpers - .makeRequest( - client(), - "POST", - TestHelpers.AD_BASE_DETECTORS_URI, - ImmutableMap.of(), - TestHelpers.toHttpEntity(detector), - null - ) + ResponseException.class, + () -> TestHelpers + .makeRequest( + client(), + "POST", + TestHelpers.AD_BASE_DETECTORS_URI, + ImmutableMap.of(), + TestHelpers.toHttpEntity(detector), + null + ) ); assertThat(ex.getMessage(), containsString(ADCommonMessages.DISABLED_ERR_MSG)); // 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); + .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"); @@ -220,10 +228,7 @@ public void testCreateAnomalyDetectorWithFlattenedResultIndex() throws Exception assertNotEquals("response is missing Id", AnomalyDetector.NO_ID, id); assertTrue("incorrect version", version > 0); // ensure the flattened result index was created - String expectedFlattenedIndex = String.format( - "opensearch-ad-plugin-result-test_flattened_%s", - id.toLowerCase(Locale.ROOT) - ); + String expectedFlattenedIndex = String.format("opensearch-ad-plugin-result-test_flattened_%s", id.toLowerCase(Locale.ROOT)); boolean indexExists = indexExists(expectedFlattenedIndex); assertTrue(indexExists); } diff --git a/src/test/java/org/opensearch/timeseries/TestHelpers.java b/src/test/java/org/opensearch/timeseries/TestHelpers.java index 4168cfed2..e53f0bfe4 100644 --- a/src/test/java/org/opensearch/timeseries/TestHelpers.java +++ b/src/test/java/org/opensearch/timeseries/TestHelpers.java @@ -511,37 +511,38 @@ public static AnomalyDetector randomAnomalyDetector(String timefield, String ind ); } - public static AnomalyDetector randomAnomalyDetectorWithFlattenResultIndex(String timefield, String indexName, List features) throws IOException { + public static AnomalyDetector randomAnomalyDetectorWithFlattenResultIndex(String timefield, String indexName, List features) + throws IOException { return new AnomalyDetector( - randomAlphaOfLength(10), - randomLong(), - randomAlphaOfLength(20), - randomAlphaOfLength(30), - timefield, - ImmutableList.of(indexName.toLowerCase(Locale.ROOT)), - features, - randomQuery(), - randomIntervalTimeConfiguration(), - randomIntervalTimeConfiguration(), - randomIntBetween(1, TimeSeriesSettings.MAX_SHINGLE_SIZE), - null, - randomInt(), - Instant.now(), - null, - randomUser(), - ADCommonName.CUSTOM_RESULT_INDEX_PREFIX + "test", - TestHelpers.randomImputationOption(features), - // timeDecay (reverse of recencyEmphasis) should be less than 1. - // so we start with 2. - randomIntBetween(2, 10000), - randomInt(TimeSeriesSettings.MAX_SHINGLE_SIZE / 2), - randomIntBetween(1, 1000), - null, - null, - null, - null, - true, - Instant.now() + randomAlphaOfLength(10), + randomLong(), + randomAlphaOfLength(20), + randomAlphaOfLength(30), + timefield, + ImmutableList.of(indexName.toLowerCase(Locale.ROOT)), + features, + randomQuery(), + randomIntervalTimeConfiguration(), + randomIntervalTimeConfiguration(), + randomIntBetween(1, TimeSeriesSettings.MAX_SHINGLE_SIZE), + null, + randomInt(), + Instant.now(), + null, + randomUser(), + ADCommonName.CUSTOM_RESULT_INDEX_PREFIX + "test", + TestHelpers.randomImputationOption(features), + // timeDecay (reverse of recencyEmphasis) should be less than 1. + // so we start with 2. + randomIntBetween(2, 10000), + randomInt(TimeSeriesSettings.MAX_SHINGLE_SIZE / 2), + randomIntBetween(1, 1000), + null, + null, + null, + null, + true, + Instant.now() ); }