Skip to content

Commit

Permalink
opendistro-for-elasticsearch#405 adding unit test for model
Browse files Browse the repository at this point in the history
  • Loading branch information
Rahul Kadam committed Aug 1, 2021
1 parent 09eebb5 commit 45bff9a
Show file tree
Hide file tree
Showing 7 changed files with 332 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,7 @@
import static org.elasticsearch.common.xcontent.XContentParserUtils.ensureExpectedToken;
import static org.elasticsearch.index.query.AbstractQueryBuilder.parseInnerQueryBuilder;
import static org.elasticsearch.index.seqno.SequenceNumbers.UNASSIGNED_SEQ_NO;
import static org.elasticsearch.test.ESTestCase.buildNewFakeTransportAddress;
import static org.elasticsearch.test.ESTestCase.randomAlphaOfLength;
import static org.elasticsearch.test.ESTestCase.randomBoolean;
import static org.elasticsearch.test.ESTestCase.randomDouble;
import static org.elasticsearch.test.ESTestCase.randomInt;
import static org.elasticsearch.test.ESTestCase.randomIntBetween;
import static org.elasticsearch.test.ESTestCase.randomLong;
import static org.elasticsearch.test.ESTestCase.*;
import static org.powermock.api.mockito.PowerMockito.mock;
import static org.powermock.api.mockito.PowerMockito.when;

Expand All @@ -46,6 +40,7 @@
import java.util.function.Consumer;
import java.util.stream.IntStream;

import com.amazon.opendistroforelasticsearch.ad.model.*;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.entity.ContentType;
Expand Down Expand Up @@ -121,21 +116,6 @@
import com.amazon.opendistroforelasticsearch.ad.constant.CommonValue;
import com.amazon.opendistroforelasticsearch.ad.feature.Features;
import com.amazon.opendistroforelasticsearch.ad.ml.ThresholdingResult;
import com.amazon.opendistroforelasticsearch.ad.model.ADTask;
import com.amazon.opendistroforelasticsearch.ad.model.ADTaskState;
import com.amazon.opendistroforelasticsearch.ad.model.ADTaskType;
import com.amazon.opendistroforelasticsearch.ad.model.AnomalyDetector;
import com.amazon.opendistroforelasticsearch.ad.model.AnomalyDetectorExecutionInput;
import com.amazon.opendistroforelasticsearch.ad.model.AnomalyDetectorJob;
import com.amazon.opendistroforelasticsearch.ad.model.AnomalyDetectorType;
import com.amazon.opendistroforelasticsearch.ad.model.AnomalyResult;
import com.amazon.opendistroforelasticsearch.ad.model.DetectionDateRange;
import com.amazon.opendistroforelasticsearch.ad.model.DetectorInternalState;
import com.amazon.opendistroforelasticsearch.ad.model.Entity;
import com.amazon.opendistroforelasticsearch.ad.model.Feature;
import com.amazon.opendistroforelasticsearch.ad.model.FeatureData;
import com.amazon.opendistroforelasticsearch.ad.model.IntervalTimeConfiguration;
import com.amazon.opendistroforelasticsearch.ad.model.TimeConfiguration;
import com.amazon.opendistroforelasticsearch.commons.authuser.User;
import com.amazon.opendistroforelasticsearch.jobscheduler.spi.schedule.IntervalSchedule;
import com.google.common.collect.ImmutableList;
Expand Down Expand Up @@ -928,6 +908,96 @@ public static ADTask randomAdTask(String taskId, ADTaskState state, Instant exec
return task;
}

public static ADTaskProfile randomADTaskProfileWithAdTask(ADTask adTask) throws IOException{
ADTaskProfile adTaskProfile = new ADTaskProfile(
adTask,
randomInt(),
randomLong(),
randomBoolean(),
randomInt(),
randomLong(),
randomAlphaOfLength(5));
return adTaskProfile;
}

public static ADTaskProfile randomADTaskProfileWithoutAdTask() throws IOException{
ADTaskProfile adTaskProfile = new ADTaskProfile(
randomInt(),
randomLong(),
randomBoolean(),
randomInt(),
randomLong(),
randomAlphaOfLength(5));
return adTaskProfile;
}

public static Entity randomEntity() {
Entity entity = new Entity(
randomAlphaOfLength(5),
randomAlphaOfLength(5));
return entity;
}

public static Entity createEntity(String key, String value) {
Entity entity = new Entity(key, value);
return entity;
}

public static InitProgressProfile randomInitProgressProfile() {
InitProgressProfile initProgressProfile = new InitProgressProfile(
randomAlphaOfLength(4),
randomIntBetween(1,100),
randomIntBetween(1,100)
);
return initProgressProfile;
}

public static InitProgressProfile createInitProgressProfile(String percentage, long minleft, int shingle) {
InitProgressProfile initProgressProfile = new InitProgressProfile(
percentage, minleft, shingle);
return initProgressProfile;
}

public static DetectorProfile randomDetectorProfile() throws IOException {
DetectorProfile detectorProfile = new DetectorProfile
.Builder()
.state(DetectorState.INIT)
.error(randomAlphaOfLength(5))
.shingleSize(randomIntBetween(1,10))
.coordinatingNode(randomAlphaOfLength(6))
.totalSizeInBytes(randomLongBetween(10, 100))
.initProgress(randomInitProgressProfile())
.adTaskProfile(randomADTaskProfileWithoutAdTask())
.modelProfile(null)
.build();
return detectorProfile;
}

public static DetectorProfile randomDetectorProfileWithEntitiesCount(long active, long total) throws IOException {
DetectorProfile detectorProfile = randomDetectorProfile();
detectorProfile.setActiveEntities(active);
detectorProfile.setTotalEntities(total);
return detectorProfile;
}

public static DetectorInternalState randomDetectorInternalState() {
DetectorInternalState detectorInternalState = new DetectorInternalState
.Builder()
.lastUpdateTime(Instant.now().truncatedTo(ChronoUnit.MILLIS))
.error(randomAlphaOfLength(10))
.build();
return detectorInternalState;
}

public static ModelProfile randomModelProfile() {
ModelProfile modelProfile = new ModelProfile(
randomAlphaOfLength(5),
randomIntBetween(1,10),
randomAlphaOfLength(4)
);
return modelProfile;
}

public static HttpEntity toHttpEntity(ToXContentObject object) throws IOException {
return new StringEntity(toJsonString(object), APPLICATION_JSON);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.amazon.opendistroforelasticsearch.ad.model;

import com.amazon.opendistroforelasticsearch.ad.TestHelpers;
import org.elasticsearch.common.io.stream.BytesStreamOutput;
import org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput;
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.test.ESSingleNodeTestCase;

import java.io.IOException;

public class ADTaskProfileTests extends ESSingleNodeTestCase {

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

public void testAdTaskProfileSerializationWithAdTask() throws IOException {
ADTask adTask = TestHelpers.randomAdTask();
ADTaskProfile adTaskProfile = TestHelpers.randomADTaskProfileWithAdTask(adTask);
BytesStreamOutput output = new BytesStreamOutput();
adTaskProfile.writeTo(output);
NamedWriteableAwareStreamInput input = new NamedWriteableAwareStreamInput(output.bytes().streamInput(), writableRegistry());
ADTaskProfile parsedADTaskProfile = new ADTaskProfile(input);
assertEquals("AD task profile with AD Task serialization doesn't work", adTaskProfile, parsedADTaskProfile);
}

public void testAdTaskProfileSerializationWithNullAdTask() throws IOException {
ADTaskProfile adTaskProfile = TestHelpers.randomADTaskProfileWithAdTask(null);
BytesStreamOutput output = new BytesStreamOutput();
adTaskProfile.writeTo(output);
NamedWriteableAwareStreamInput input = new NamedWriteableAwareStreamInput(output.bytes().streamInput(), writableRegistry());
ADTaskProfile parsedADTaskProfile = new ADTaskProfile(input);
assertEquals("AD task profile with null AD task serialization doesn't work", adTaskProfile, parsedADTaskProfile);
}
public void testAdTaskProfileSerializationWithoutAdTask() throws IOException {
ADTaskProfile adTaskProfile = TestHelpers.randomADTaskProfileWithoutAdTask();
BytesStreamOutput output = new BytesStreamOutput();
adTaskProfile.writeTo(output);
NamedWriteableAwareStreamInput input = new NamedWriteableAwareStreamInput(output.bytes().streamInput(), writableRegistry());
ADTaskProfile parsedADTaskProfile = new ADTaskProfile(input);
assertEquals("AD task profile without Ad Task serialization doesn't work", adTaskProfile, parsedADTaskProfile);
}

public void testParseADTaskProfile() throws IOException {
ADTaskProfile adTaskProfile = TestHelpers.randomADTaskProfileWithoutAdTask();
adTaskProfile.setNodeId(randomAlphaOfLength(5));
String adTaskProfileString = TestHelpers.xContentBuilderToString(adTaskProfile.toXContent(TestHelpers.builder(), ToXContent.EMPTY_PARAMS));
ADTaskProfile parsedADTaskProfile = ADTaskProfile.parse(TestHelpers.parser(adTaskProfileString));
assertEquals("Parsing AD task Profile doesn't work", adTaskProfile, parsedADTaskProfile);
}

public void testParseADTaskProfileWithAdTask() throws IOException {
ADTask adTask = TestHelpers.randomAdTask();
ADTaskProfile adTaskProfile = TestHelpers.randomADTaskProfileWithAdTask(adTask);
adTaskProfile.setNodeId(randomAlphaOfLength(5));
String adTaskProfileString = TestHelpers.xContentBuilderToString(adTaskProfile.toXContent(TestHelpers.builder(), ToXContent.EMPTY_PARAMS));
ADTaskProfile parsedADTaskProfile = ADTaskProfile.parse(TestHelpers.parser(adTaskProfileString));
assertEquals("Parsing AD task Profile with Ad Task doesn't work", adTaskProfile, parsedADTaskProfile);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.amazon.opendistroforelasticsearch.ad.model;

import com.amazon.opendistroforelasticsearch.ad.TestHelpers;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.test.ESTestCase;

import java.io.IOException;

public class DetectorInternalStateTests extends ESTestCase {

public void testParseDetectorInternalState() throws IOException {
DetectorInternalState detectorInternalState = TestHelpers.randomDetectorInternalState();
String parsedEntityString = TestHelpers.xContentBuilderToString(
detectorInternalState.toXContent(TestHelpers.builder(), ToXContent.EMPTY_PARAMS));
DetectorInternalState parsedDetectorInternalState = DetectorInternalState.parse(TestHelpers.parser(parsedEntityString));
assertEquals("Parsing Detector Internal state doesn't work", detectorInternalState, parsedDetectorInternalState);
}

public void testCloneDetectorInternalState() {
DetectorInternalState detectorInternalState = TestHelpers.randomDetectorInternalState();
DetectorInternalState clonedInternalState = (DetectorInternalState)detectorInternalState.clone();
assertEquals("Cloning Detector Internal state doesn't work", detectorInternalState, clonedInternalState);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.amazon.opendistroforelasticsearch.ad.model;

import com.amazon.opendistroforelasticsearch.ad.TestHelpers;
import org.elasticsearch.common.io.stream.BytesStreamOutput;
import org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput;
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
import org.elasticsearch.test.ESSingleNodeTestCase;

import java.io.IOException;

public class DetectorProfileTests extends ESSingleNodeTestCase {

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

public void testDetectorProfileSerialization() throws IOException {
DetectorProfile detectorProfile = TestHelpers.randomDetectorProfile();
BytesStreamOutput output = new BytesStreamOutput();
detectorProfile.writeTo(output);
NamedWriteableAwareStreamInput input = new NamedWriteableAwareStreamInput(output.bytes().streamInput(), writableRegistry());
DetectorProfile parsedDetectorProfile = new DetectorProfile(input);
assertEquals("Detector profile serialization doesn't work", detectorProfile, parsedDetectorProfile);
}

public void testDetectorProfileMerge() throws IOException {
DetectorProfile detectorProfile = TestHelpers.randomDetectorProfile();
DetectorProfile mergeDetectorProfile = TestHelpers
.randomDetectorProfileWithEntitiesCount(randomLongBetween(1, 5), randomLongBetween(5, 10));
detectorProfile.merge(mergeDetectorProfile);
assertEquals(detectorProfile.getTotalEntities(), mergeDetectorProfile.getTotalEntities());
assertEquals(detectorProfile.getActiveEntities(), mergeDetectorProfile.getActiveEntities());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.amazon.opendistroforelasticsearch.ad.model;

import com.amazon.opendistroforelasticsearch.ad.TestHelpers;
import org.elasticsearch.common.io.stream.BytesStreamOutput;
import org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput;
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.test.ESSingleNodeTestCase;

import java.io.IOException;

public class EntityTests extends ESSingleNodeTestCase {

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

public void testEntitySerialization() throws IOException {
Entity entity = TestHelpers.randomEntity();
BytesStreamOutput output = new BytesStreamOutput();
entity.writeTo(output);
NamedWriteableAwareStreamInput input = new NamedWriteableAwareStreamInput(output.bytes().streamInput(), writableRegistry());
Entity parsedEntity = new Entity(input);
assertEquals("Entity serialization doesn't work", entity, parsedEntity);
}

public void testEntitySerializationWithNullValue() throws Exception {
TestHelpers.assertFailWith(
NullPointerException.class,
() -> {
Entity entity = TestHelpers.createEntity(randomAlphaOfLength(5), null);
BytesStreamOutput output = new BytesStreamOutput();
entity.writeTo(output);
return entity;
}
);
}

public void testParseEntity() throws IOException {
Entity entity = TestHelpers.randomEntity();
String parsedEntityString = TestHelpers.xContentBuilderToString(
entity.toXContent(TestHelpers.builder(), ToXContent.EMPTY_PARAMS));
Entity paredEntity = Entity.parse(TestHelpers.parser(parsedEntityString));
assertEquals("Parsing entity doesn't work", entity, paredEntity);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.amazon.opendistroforelasticsearch.ad.model;

import com.amazon.opendistroforelasticsearch.ad.TestHelpers;
import org.elasticsearch.common.io.stream.BytesStreamOutput;
import org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput;
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;

import org.elasticsearch.test.ESSingleNodeTestCase;

import java.io.IOException;

public class InitProgressProfileTests extends ESSingleNodeTestCase {

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

public void testInitProgressProfileSerialization() throws IOException {
InitProgressProfile initProgressProfile = TestHelpers.randomInitProgressProfile();
BytesStreamOutput output = new BytesStreamOutput();
initProgressProfile.writeTo(output);
NamedWriteableAwareStreamInput input = new NamedWriteableAwareStreamInput(output.bytes().streamInput(), writableRegistry());
InitProgressProfile parsedInitProgressProfile = new InitProgressProfile(input);
assertEquals("InitProgressProfile serialization doesn't work",
initProgressProfile, parsedInitProgressProfile);
}

public void testInitProgressProfileSerializationWithNegativeMinLeft() throws Exception {
TestHelpers.assertFailWith(
IllegalStateException.class,
"Negative longs unsupported",
() -> {
InitProgressProfile initProgressProfile = TestHelpers.createInitProgressProfile(randomAlphaOfLength(5), -2, randomInt());
BytesStreamOutput output = new BytesStreamOutput();
initProgressProfile.writeTo(output);
return initProgressProfile;
}
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.amazon.opendistroforelasticsearch.ad.model;

import com.amazon.opendistroforelasticsearch.ad.TestHelpers;
import org.elasticsearch.common.io.stream.BytesStreamOutput;
import org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput;
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
import org.elasticsearch.test.ESSingleNodeTestCase;

import java.io.IOException;

public class ModelProfileTests extends ESSingleNodeTestCase {

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

public void testModelProfileSerialization() throws IOException {
ModelProfile modelProfile = TestHelpers.randomModelProfile();
BytesStreamOutput output = new BytesStreamOutput();
modelProfile.writeTo(output);
NamedWriteableAwareStreamInput input = new NamedWriteableAwareStreamInput(output.bytes().streamInput(), writableRegistry());
ModelProfile parsedModelProfile = new ModelProfile(input);
assertEquals("Model Profile Serialization doesn't work", modelProfile, parsedModelProfile);
}



}

0 comments on commit 45bff9a

Please sign in to comment.