Skip to content

Commit

Permalink
add more tests
Browse files Browse the repository at this point in the history
Signed-off-by: Jackie Han <[email protected]>
  • Loading branch information
jackiehanyang committed May 31, 2024
1 parent 42b9427 commit b0a7d91
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 33 deletions.
18 changes: 3 additions & 15 deletions src/main/java/org/opensearch/ad/model/AnomalyDetector.java
Original file line number Diff line number Diff line change
Expand Up @@ -348,21 +348,9 @@ public void writeTo(StreamOutput output) throws IOException {
} else {
output.writeBoolean(false);
}
if (customResultIndexMinSize != null) {
output.writeOptionalInt(customResultIndexMinSize);
} else {
output.writeBoolean(false);
}
if (customResultIndexMinSize != null) {
output.writeOptionalInt(customResultIndexMinAge);
} else {
output.writeBoolean(false);
}
if (customResultIndexMinSize != null) {
output.writeOptionalInt(customResultIndexTTL);
} else {
output.writeBoolean(false);
}
output.writeOptionalInt(customResultIndexMinSize);
output.writeOptionalInt(customResultIndexMinAge);
output.writeOptionalInt(customResultIndexTTL);
}

@Override
Expand Down
24 changes: 6 additions & 18 deletions src/main/java/org/opensearch/timeseries/model/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -309,17 +309,17 @@ public Config(StreamInput input) throws IOException {
if (input.readBoolean()) {
this.customResultIndexMinSize = input.readInt();
} else {
customResultIndexMinSize = null;
this.customResultIndexMinSize = null;
}
if (input.readBoolean()) {
this.customResultIndexMinAge = input.readInt();
} else {
customResultIndexMinAge = null;
this.customResultIndexMinAge = null;
}
if (input.readBoolean()) {
this.customResultIndexTTL = input.readInt();
} else {
customResultIndexTTL = null;
this.customResultIndexTTL = null;
}
}

Expand Down Expand Up @@ -370,21 +370,9 @@ public void writeTo(StreamOutput output) throws IOException {
output.writeInt(recencyEmphasis);
output.writeInt(seasonIntervals);
output.writeInt(historyIntervals);
if (customResultIndexMinSize != null) {
output.writeInt(customResultIndexMinSize);
} else {
output.writeBoolean(false);
}
if (customResultIndexMinSize != null) {
output.writeInt(customResultIndexMinAge);
} else {
output.writeBoolean(false);
}
if (customResultIndexMinSize != null) {
output.writeInt(customResultIndexTTL);
} else {
output.writeBoolean(false);
}
output.writeOptionalInt(customResultIndexMinSize);
output.writeOptionalInt(customResultIndexMinAge);
output.writeOptionalInt(customResultIndexTTL);
}

public boolean invalidShingleSizeRange(Integer shingleSizeToTest) {
Expand Down
27 changes: 27 additions & 0 deletions src/test/java/org/opensearch/ad/model/AnomalyDetectorTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -844,4 +844,31 @@ public void testParseAnomalyDetectorWithNoDescription() throws IOException {
AnomalyDetector parsedDetector = AnomalyDetector.parse(TestHelpers.parser(detectorString), "id", 1L, null, null);
assertEquals(parsedDetector.getDescription(), "");
}

public void testParseAnomalyDetector_withCustomIndex_withCustomResultIndexMinSize() throws IOException {
String detectorString = "{\"name\":\"AhtYYGWTgqkzairTchcs\",\"description\":\"iIiAVPMyFgnFlEniLbMyfJxyoGvJAl\","
+ "\"time_field\":\"HmdFH\",\"indices\":[\"ffsBF\"],\"filter_query\":{\"bool\":{\"filter\":[{\"exists\":"
+ "{\"field\":\"value\",\"boost\":1}}],\"adjust_pure_negative\":true,\"boost\":1}},\"window_delay\":"
+ "{\"period\":{\"interval\":2,\"unit\":\"Minutes\"}},\"shingle_size\":8,\"schema_version\":-512063255,"
+ "\"feature_attributes\":[{\"feature_id\":\"OTYJs\",\"feature_name\":\"eYYCM\",\"feature_enabled\":false,"
+ "\"aggregation_query\":{\"XzewX\":{\"value_count\":{\"field\":\"ok\"}}}}],\"recency_emphasis\":3342,"
+ "\"history\":62,\"last_update_time\":1717192049845,\"category_field\":[\"Tcqcb\"],\"result_index\":"
+ "\"opensearch-ad-plugin-result-test\",\"imputation_option\":{\"method\":\"FIXED_VALUES\",\"defaultFill\""
+ ":[],\"integerSensitive\":false},\"suggested_seasonality\":64,\"detection_interval\":{\"period\":"
+ "{\"interval\":5,\"unit\":\"Minutes\"}},\"detector_type\":\"MULTI_ENTITY\",\"rules\":[],\"result_index_min_size\":1500}";
AnomalyDetector parsedDetector = AnomalyDetector.parse(TestHelpers.parser(detectorString), "id", 1L, null, null);
assertEquals(1500, (int) parsedDetector.getCustomResultIndexMinSize());
}

public void testParseAnomalyDetector_withCustomIndex_withNullCustomResultIndexMinSize() throws IOException {
String detectorString = "{\"name\":\"todagtCMkwpcaedpyYUM\",\"time_field\":\"dJRwh\",\"indices\":[\"eIrgWMqAED\"],"
+ "\"feature_attributes\":[{\"feature_id\":\"lxYRN\",\"feature_name\":\"eqSeU\",\"feature_enabled\""
+ ":true,\"aggregation_query\":{\"aa\":{\"value_count\":{\"field\":\"ok\"}}}}],\"detection_interval\":"
+ "{\"period\":{\"interval\":425,\"unit\":\"Minutes\"}},\"window_delay\":{\"period\":{\"interval\":973,"
+ "\"unit\":\"Minutes\"}},\"shingle_size\":4,\"schema_version\":-1203962153,\"ui_metadata\":{\"JbAaV\":{\"feature_id\":"
+ "\"rIFjS\",\"feature_name\":\"QXCmS\",\"feature_enabled\":false,\"aggregation_query\":{\"aa\":"
+ "{\"value_count\":{\"field\":\"ok\"}}}}},\"last_update_time\":1568396089028}";
AnomalyDetector parsedDetector = AnomalyDetector.parse(TestHelpers.parser(detectorString), "id", 1L, null, null);
assertEquals(null, parsedDetector.getCustomResultIndexMinSize());
}
}
51 changes: 51 additions & 0 deletions src/test/java/org/opensearch/forecast/model/ForecasterTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -436,4 +436,55 @@ public void testParseNullImpute() throws IOException {
Forecaster parsedForecaster = Forecaster.parse(TestHelpers.parser(forecasterString));
assertEquals("Parsing forecaster doesn't work", forecaster, parsedForecaster);
}

public void testParseNullCustomResultIndex_nullCustomResultIndexConditions() throws IOException {
Forecaster forecaster = TestHelpers.ForecasterBuilder
.newInstance()
.setCustomResultIndex(null)
.setCustomResultIndexMinSize(null)
.setCustomResultIndexMinAge(null)
.setCustomResultIndexTTL(null)
.build();
String forecasterString = TestHelpers
.xContentBuilderToString(forecaster.toXContent(TestHelpers.builder(), ToXContent.EMPTY_PARAMS));
LOG.info(forecasterString);
Forecaster parsedForecaster = Forecaster.parse(TestHelpers.parser(forecasterString));
assertEquals("Parsing forecaster doesn't work", forecaster, parsedForecaster);
}

public void testValidCustomResultIndex_withIndexConditions() {
String resultIndex = ForecastCommonName.CUSTOM_RESULT_INDEX_PREFIX + "test";

var forecaster = new Forecaster(
forecasterId,
version,
name,
description,
timeField,
indices,
features,
filterQuery,
forecastInterval,
windowDelay,
shingleSize,
uiMetadata,
schemaVersion,
lastUpdateTime,
categoryFields,
user,
resultIndex,
horizon,
TestHelpers.randomImputationOption(0),
recencyEmphasis,
seasonality,
randomIntBetween(1, 1000),
20000,
7,
null
);

assertEquals(20000, (int) forecaster.getCustomResultIndexMinSize());
assertEquals(7, (int) forecaster.getCustomResultIndexMinAge());
assertEquals(null, forecaster.getCustomResultIndexTTL());
}
}
21 changes: 21 additions & 0 deletions src/test/java/org/opensearch/timeseries/TestHelpers.java
Original file line number Diff line number Diff line change
Expand Up @@ -1705,6 +1705,9 @@ public static class ForecasterBuilder {
String resultIndex;
Integer horizon;
ImputationOption imputationOption;
Integer customResultIndexMinSize;
Integer customResultIndexMinAge;
Integer customResultIndexTTL;

ForecasterBuilder() throws IOException {
forecasterId = randomAlphaOfLength(10);
Expand All @@ -1726,6 +1729,9 @@ public static class ForecasterBuilder {
resultIndex = null;
horizon = randomIntBetween(1, 20);
imputationOption = randomImputationOption((int) features.stream().filter(Feature::getEnabled).count());
customResultIndexMinSize = null;
customResultIndexMinAge = null;
customResultIndexTTL = null;
}

public static ForecasterBuilder newInstance() throws IOException {
Expand Down Expand Up @@ -1817,6 +1823,21 @@ public ForecasterBuilder setCustomResultIndex(String resultIndex) {
return this;
}

public ForecasterBuilder setCustomResultIndexMinSize(Integer customResultIndexMinSize) {
this.customResultIndexMinSize = customResultIndexMinSize;
return this;
}

public ForecasterBuilder setCustomResultIndexMinAge(Integer customResultIndexMinAge) {
this.customResultIndexMinAge = customResultIndexMinAge;
return this;
}

public ForecasterBuilder setCustomResultIndexTTL(Integer customResultIndexTTL) {
this.customResultIndexTTL = customResultIndexTTL;
return this;
}

public ForecasterBuilder setNullImputationOption() {
this.imputationOption = null;
return this;
Expand Down

0 comments on commit b0a7d91

Please sign in to comment.