Skip to content

Commit

Permalink
added a few more unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Amit Galitzky <amgalitz@amazon.com>
  • Loading branch information
amitgalitz committed Dec 7, 2021
1 parent 7edf3d7 commit 15b53e1
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public class CommonErrorMessages {
public static final String BUG_RESPONSE = "We might have bugs.";
public static final String INDEX_NOT_FOUND = "index does not exist";
public static final String NOT_EXISTENT_VALIDATION_TYPE = "The given validation type doesn't exist";
public static final String NON_SUPPORTED_PROFILE_TYPE = "Unsupported profile types";

private static final String TOO_MANY_CATEGORICAL_FIELD_ERR_MSG_FORMAT = "We can have only %d categorical field/s.";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.Set;

import org.opensearch.ad.Name;
import org.opensearch.ad.constant.CommonErrorMessages;
import org.opensearch.ad.constant.CommonName;

public enum DetectorProfileName implements Name {
Expand Down Expand Up @@ -68,7 +69,7 @@ public static DetectorProfileName getName(String name) {
case CommonName.AD_TASK:
return AD_TASK;
default:
throw new IllegalArgumentException("Unsupported profile types");
throw new IllegalArgumentException(CommonErrorMessages.NON_SUPPORTED_PROFILE_TYPE);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/

package org.opensearch.ad.model;

import java.io.IOException;
import java.time.Instant;
import java.util.Collection;

import org.opensearch.ad.AnomalyDetectorPlugin;
import org.opensearch.ad.TestHelpers;
import org.opensearch.common.io.stream.NamedWriteableRegistry;
import org.opensearch.common.xcontent.ToXContent;
import org.opensearch.plugins.Plugin;
import org.opensearch.test.InternalSettingsPlugin;
import org.opensearch.test.OpenSearchSingleNodeTestCase;

public class DetectorInternalStateTests extends OpenSearchSingleNodeTestCase {

@Override
protected Collection<Class<? extends Plugin>> getPlugins() {
return pluginList(InternalSettingsPlugin.class, AnomalyDetectorPlugin.class);
}

@Override
protected NamedWriteableRegistry writableRegistry() {
return getInstanceFromNode(NamedWriteableRegistry.class);
}

public void testToXContentDetectorInternalState() throws IOException {
DetectorInternalState internalState = new DetectorInternalState.Builder()
.lastUpdateTime(Instant.ofEpochMilli(100L))
.error("error-test")
.build();
String internalStateString = TestHelpers
.xContentBuilderToString(internalState.toXContent(TestHelpers.builder(), ToXContent.EMPTY_PARAMS));
DetectorInternalState parsedInternalState = DetectorInternalState.parse(TestHelpers.parser(internalStateString));
assertEquals(internalState, parsedInternalState);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import java.util.Map;

import org.opensearch.ad.TestHelpers;
import org.opensearch.ad.constant.CommonErrorMessages;
import org.opensearch.ad.constant.CommonName;
import org.opensearch.common.io.stream.BytesStreamOutput;
import org.opensearch.common.io.stream.NamedWriteableAwareStreamInput;
import org.opensearch.common.xcontent.XContentParser;
Expand Down Expand Up @@ -83,4 +85,10 @@ public void testDetectorProfileToXContent() throws IOException {
assertEquals(detectorProfile.getState().toString(), parsedMap.get("state"));
assertTrue(parsedMap.get("models").toString().contains(detectorProfile.getModelProfile()[0].getModelId()));
}

public void testDetectorProfileName() throws IllegalArgumentException {
DetectorProfileName.getName(CommonName.AD_TASK);
IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, () -> DetectorProfileName.getName("abc"));
assertEquals(exception.getMessage(), CommonErrorMessages.NON_SUPPORTED_PROFILE_TYPE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ public void testGetAnomalyDetectorProfileResponse() throws IOException {
Map<String, Object> map = TestHelpers.XContentBuilderToMap(builder);
Map<String, Object> parsedInitProgress = (Map<String, Object>) (map.get(CommonName.INIT_PROGRESS));
Assert.assertEquals(initProgress.getPercentage(), parsedInitProgress.get(InitProgressProfile.PERCENTAGE).toString());
assertTrue(initProgress.toString().contains("[percentage=99%,estimated_minutes_left=2,needed_shingles=2]"));
Assert
.assertEquals(
String.valueOf(initProgress.getEstimatedMinutesLeft()),
Expand Down
21 changes: 21 additions & 0 deletions src/test/java/org/opensearch/ad/util/ParseUtilsTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.opensearch.ad.common.exception.AnomalyDetectionException;
import org.opensearch.ad.model.AnomalyDetector;
import org.opensearch.ad.model.Feature;
import org.opensearch.common.ParsingException;
import org.opensearch.common.xcontent.XContentBuilder;
import org.opensearch.common.xcontent.XContentFactory;
import org.opensearch.common.xcontent.XContentParser;
Expand Down Expand Up @@ -85,6 +86,26 @@ public void testParseAggregatorsWithAggregationQueryString() throws IOException
assertEquals("test", agg.getAggregatorFactories().iterator().next().getName());
}

public void testParseAggregatorsWithInvalidAggregationName() throws IOException {
XContentParser parser = ParseUtils.parser("{\"aa\":{\"value_count\":{\"field\":\"ok\"}}}", TestHelpers.xContentRegistry());
Exception ex = expectThrows(ParsingException.class, () -> ParseUtils.parseAggregators(parser, 0, "#@?><:{"));
assertTrue(ex.getMessage().contains("Aggregation names must be alpha-numeric and can only contain '_' and '-'"));
}

public void testParseAggregatorsWithTwoAggregationTypes() throws IOException {
XContentParser parser = ParseUtils
.parser("{\"test\":{\"avg\":{\"field\":\"value\"},\"sum\":{\"field\":\"value\"}}}", TestHelpers.xContentRegistry());
Exception ex = expectThrows(ParsingException.class, () -> ParseUtils.parseAggregators(parser, 0, "test"));
assertTrue(ex.getMessage().contains("Found two aggregation type definitions in"));
}

public void testParseAggregatorsWithNullAggregationDefinition() throws IOException {
String aggName = "test";
XContentParser parser = ParseUtils.parser("{\"test\":{}}", TestHelpers.xContentRegistry());
Exception ex = expectThrows(ParsingException.class, () -> ParseUtils.parseAggregators(parser, 0, aggName));
assertTrue(ex.getMessage().contains("Missing definition for aggregation [" + aggName + "]"));
}

public void testParseAggregatorsWithAggregationQueryStringAndNullAggName() throws IOException {
AggregatorFactories.Builder agg = ParseUtils
.parseAggregators("{\"aa\":{\"value_count\":{\"field\":\"ok\"}}}", TestHelpers.xContentRegistry(), null);
Expand Down

0 comments on commit 15b53e1

Please sign in to comment.