forked from opendistro-for-elasticsearch/anomaly-detection
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
opendistro-for-elasticsearch#405 adding unit test for model
- Loading branch information
Rahul Kadam
committed
Aug 1, 2021
1 parent
09eebb5
commit 45bff9a
Showing
7 changed files
with
332 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
src/test/java/com/amazon/opendistroforelasticsearch/ad/model/ADTaskProfileTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/test/java/com/amazon/opendistroforelasticsearch/ad/model/DetectorInternalStateTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
35 changes: 35 additions & 0 deletions
35
src/test/java/com/amazon/opendistroforelasticsearch/ad/model/DetectorProfileTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
src/test/java/com/amazon/opendistroforelasticsearch/ad/model/EntityTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
41 changes: 41 additions & 0 deletions
41
src/test/java/com/amazon/opendistroforelasticsearch/ad/model/InitProgressProfileTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/test/java/com/amazon/opendistroforelasticsearch/ad/model/ModelProfileTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
|
||
|
||
} |