Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

#405 adding unit test for model #406

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
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.randomLongBetween;
import static org.powermock.api.mockito.PowerMockito.mock;
import static org.powermock.api.mockito.PowerMockito.when;

Expand Down Expand Up @@ -136,6 +137,11 @@
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.ad.model.ModelProfile;
import com.amazon.opendistroforelasticsearch.ad.model.DetectorProfile;
import com.amazon.opendistroforelasticsearch.ad.model.InitProgressProfile;
import com.amazon.opendistroforelasticsearch.ad.model.ADTaskProfile;
import com.amazon.opendistroforelasticsearch.ad.model.DetectorState;
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 +934,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);
}



}