Skip to content

Commit

Permalink
Materials plugin unit tests (#11)
Browse files Browse the repository at this point in the history
* unit tests for materials action support

* unit tests for testbatchconinfo

* unit tests for materials producer prop id

* unit tests for materials producer prop def init

* wip unit tests for materials producer const data

* unit tests for materials producer const data

* fix unit test names
made builder class private contructor

* stubbing out test methods
changed visibility of Builder constuctor to private
change visibility of handleEvent to private

* unit tests for batch prop def init

* unit tests for batch const info

* unit tests for materials producer prop def event

* unit tests for materials producer add event

* unit tests for material id add event

* unit tests for batch prop def event

* unit tests for stage report

* unit tests for materials producer resource report

* unit tests for materials producer prop report

* unit tests for batch status report

* unit test for convertStageToResource

* fix compile error

* remove implement event

* unit tests for materials data manager

* add missing annotation

* materials data manager unit test fixes

* remove explicit types from Pair<>

* use builder chaining

* use random prop value
use nextInt for random long
  • Loading branch information
bischoffz authored Dec 15, 2022
1 parent 0dd8c45 commit 196f699
Show file tree
Hide file tree
Showing 28 changed files with 2,865 additions and 701 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public void init(final ActorContext actorContext) {
}
}

public void handleMaterialsProducerAdditionEvent(ActorContext actorContext, MaterialsProducerAdditionEvent materialsProducerAdditionEvent) {
private void handleMaterialsProducerAdditionEvent(ActorContext actorContext, MaterialsProducerAdditionEvent materialsProducerAdditionEvent) {
MaterialsProducerId materialsProducerId = materialsProducerAdditionEvent.getMaterialsProducerId();
ResourcesDataManager resourcesDataManager = actorContext.getDataManager(ResourcesDataManager.class);
MaterialsDataManager materialsDataManager = actorContext.getDataManager(MaterialsDataManager.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1697,8 +1697,6 @@ private void destroyBatch(final BatchId batchId) {
* property definition</li>
* <li>{@linkplain MaterialsError#OFFERED_STAGE_UNALTERABLE} if
* the batch in on an offered stage</li>
* <li>{@linkplain MaterialsError#MATERIALS_OWNERSHIP} if the
* requesting agent is not the owning materials producer</li>
*
*
*
Expand Down Expand Up @@ -1797,8 +1795,6 @@ private void validateMaterialProducerPropertyValueNotNull(final Object propertyV
* is null</li>
* <li>{@linkplain MaterialsError#UNKNOWN_BATCH_ID} if the batch
* id is unknown</li>
* <li>{@linkplain MaterialsError#MATERIALS_OWNERSHIP} if the
* requesting agent is not the owning materials producer</li>
* <li>{@linkplain MaterialsError#BATCH_NOT_STAGED} if the batch
* is not staged</li>
* <li>{@linkplain MaterialsError#OFFERED_STAGE_UNALTERABLE } if
Expand Down Expand Up @@ -1855,8 +1851,6 @@ private void validateBatchIsStaged(final BatchId batchId) {
* <li>{@linkplain MaterialsError#BATCH_STAGED_TO_DIFFERENT_OWNER}
* if batch and stage do not have the same owning materials
* producer</li>
* <li>{@linkplain MaterialsError#MATERIALS_OWNERSHIP} if the
* requesting agent is not the owning material producer</li>
*
*
*/
Expand Down Expand Up @@ -2153,8 +2147,6 @@ public void setStageOfferState(StageId stageId, boolean offer) {
* is null</li>
* <li>{@linkplain MaterialsError#UNKNOWN_STAGE_ID} if stage id
* is unknown</li>
* <li>{@linkplain MaterialsError#MATERIALS_OWNERSHIP} if the
* requesting agent is not the owning materials producer</li>
* <li>{@linkplain MaterialsError#OFFERED_STAGE_UNALTERABLE} if
* the stage is offered</li>
* <li>{@linkplain MaterialsError#NON_FINITE_MATERIAL_AMOUNT} if
Expand Down Expand Up @@ -2236,8 +2228,6 @@ public BatchId convertStageToBatch(StageId stageId, MaterialId materialId, doubl
* id is unknown</li>
* <li>{@linkplain MaterialsError#OFFERED_STAGE_UNALTERABLE} if
* the stage is offered</li>
* <li>{@linkplain MaterialsError#MATERIALS_OWNERSHIP} if the
* requesting agent is not the owning materials producer</li>
* <li>{@linkplain ResourceError#NEGATIVE_RESOURCE_AMOUNT} if
* the the resource amount is negative</li>
* <li>{@linkplain ResourceError#RESOURCE_ARITHMETIC_EXCEPTION}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public static class Builder {

private Data data = new Data();

private Builder(){}

private void validate() {
if (data.materialsProducerId == null) {
throw new ContractException(MaterialsError.NULL_MATERIALS_PRODUCER_ID);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public MaterialsProducerPropertyDefinitionEvent(MaterialsProducerPropertyId mate
/**
* Returns the property id of the added property definition
*/
public MaterialsProducerPropertyId getPersonPropertyId() {
public MaterialsProducerPropertyId getProducerPropertyId() {
return materialsProducerPropertyId;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public static Builder builder() {
public static class Builder {
private Data data = new Data();

private Builder() {}

private void validate() {
if (data.materialsProducerId == null) {
throw new ContractException(MaterialsError.NULL_MATERIALS_PRODUCER_ID);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
*/
public class MaterialsActionSupport {

private MaterialsActionSupport(){}

/**
* Creates an action plugin with an agent that will execute the given
* consumer at time 0. The action plugin and the remaining arguments are
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

public class TestBatchConstructionInfo {

private TestBatchConstructionInfo(){}

public static BatchConstructionInfo getBatchConstructionInfo(MaterialsProducerId materialsProducerId, MaterialId materialId, double amount, RandomGenerator randomGenerator) {
BatchConstructionInfo.Builder builder = //
BatchConstructionInfo .builder()//
Expand Down
4 changes: 2 additions & 2 deletions gcm4/src/test/java/nucleus/AT_EventFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public void testGetFunctionValuePairs() {
.addFunctionValuePair(xFunction, 2)//
.build();
functionValuePairs = eventFilter.getFunctionValuePairs();
expectedFunctionValuePairs.add(new Pair<IdentifiableFunction<?>, Object>(xFunction, 2));
expectedFunctionValuePairs.add(new Pair<>(xFunction, 2));
assertNotNull(functionValuePairs);
assertEquals(expectedFunctionValuePairs, functionValuePairs);

Expand All @@ -110,7 +110,7 @@ public void testGetFunctionValuePairs() {
.addFunctionValuePair(yFunction, 3.0)//
.build();
functionValuePairs = eventFilter.getFunctionValuePairs();
expectedFunctionValuePairs.add(new Pair<IdentifiableFunction<?>, Object>(yFunction, 3.0));
expectedFunctionValuePairs.add(new Pair<>(yFunction, 3.0));
assertNotNull(functionValuePairs);
assertEquals(expectedFunctionValuePairs, functionValuePairs);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public void testGetPropertyValues() {
GroupId groupId = new GroupId(10000 + i);
for (int j = 0; j < 3; j++) {
String value = Integer.toString(randomGenerator.nextInt(100));
Pair<GroupId, String> propertyValue = new Pair<GroupId, String>(groupId, value);
Pair<GroupId, String> propertyValue = new Pair<>(groupId, value);
expectedListOfPropertyValues.add(propertyValue);
definitionInitializationBuilder.addPropertyValue(groupId, value);
}
Expand Down Expand Up @@ -328,7 +328,7 @@ public void testAddPropertyValue() {
GroupId groupId = new GroupId(10000 + i);
for (int j = 0; j < 3; j++) {
String value = Integer.toString(randomGenerator.nextInt(100));
Pair<GroupId, String> propertyValue = new Pair<GroupId, String>(groupId, value);
Pair<GroupId, String> propertyValue = new Pair<>(groupId, value);
expectedListOfPropertyValues.add(propertyValue);
definitionInitializationBuilder.addPropertyValue(groupId, value);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package plugins.materials.actors;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import java.util.ArrayList;
import java.util.Collections;
Expand Down Expand Up @@ -36,7 +37,9 @@
import plugins.reports.support.ReportItem.Builder;
import plugins.reports.support.SimpleReportId;
import plugins.stochastics.StochasticsDataManager;
import tools.annotations.UnitTag;
import tools.annotations.UnitTest;
import tools.annotations.UnitTestConstructor;
import tools.annotations.UnitTestMethod;

@UnitTest(target = BatchStatusReport.class)
Expand All @@ -54,16 +57,15 @@ private ReportItem getReportItemFromBatch(ActorContext agentContext, BatchId bat
stageString = optionalStageId.get().toString();
}

List<Object> elements = new ArrayList<>();
List<Object> elements = new ArrayList<>();

elements.add(agentContext.getTime());
elements.add(batchId);
elements.add(batchProducer);
elements.add(stageString);
elements.add(batchMaterialId);
elements.add(amount);



for (MaterialId materialId : materialsDataManager.getMaterialIds()) {
boolean matchingMaterial = batchMaterialId.equals(materialId);
Set<BatchPropertyId> batchPropertyIds = materialsDataManager.getBatchPropertyIds(materialId);
Expand All @@ -75,15 +77,22 @@ private ReportItem getReportItemFromBatch(ActorContext agentContext, BatchId bat
}
}
}

ReportItem reportItem = getReportItem(elements);



return reportItem;
}

@Test
@UnitTestMethod(name = "init", args = {ActorContext.class})
@UnitTestConstructor(args = { ReportId.class })
public void testConstructor() {
BatchStatusReport report = new BatchStatusReport(REPORT_ID);

assertNotNull(report);
}

@Test
@UnitTestMethod(name = "init", args = { ActorContext.class }, tags = { UnitTag.INCOMPLETE })
public void testInit() {

Set<ReportItem> expectedReportItems = new LinkedHashSet<>();
Expand All @@ -103,7 +112,8 @@ public void testInit() {
for (int i = 0; i < 20; i++) {
TestMaterialId materialId = TestMaterialId.getRandomMaterialId(randomGenerator);
double amount = randomGenerator.nextDouble();
BatchConstructionInfo batchConstructionInfo = TestBatchConstructionInfo.getBatchConstructionInfo(testMaterialsProducerId, materialId, amount, randomGenerator);
BatchConstructionInfo batchConstructionInfo = TestBatchConstructionInfo
.getBatchConstructionInfo(testMaterialsProducerId, materialId, amount, randomGenerator);
BatchId batchId = materialsDataManager.addBatch(batchConstructionInfo);
expectedReportItems.add(getReportItemFromBatch(c, batchId));
}
Expand All @@ -118,7 +128,8 @@ public void testInit() {
RandomGenerator randomGenerator = stochasticsDataManager.getRandomGenerator();

for (TestMaterialId testMaterialId : TestMaterialId.values()) {
List<BatchId> batches = materialsDataManager.getInventoryBatchesByMaterialId(testMaterialsProducerId, testMaterialId);
List<BatchId> batches = materialsDataManager
.getInventoryBatchesByMaterialId(testMaterialsProducerId, testMaterialId);

if (batches.size() > 1) {
for (int i = 0; i < batches.size(); i++) {
Expand Down Expand Up @@ -166,7 +177,8 @@ public void testInit() {

for (BatchId batchId : inventoryBatches) {
TestMaterialId materialId = materialsDataManager.getBatchMaterial(batchId);
TestBatchPropertyId propertyId = TestBatchPropertyId.getRandomMutableBatchPropertyId(materialId, randomGenerator);
TestBatchPropertyId propertyId = TestBatchPropertyId.getRandomMutableBatchPropertyId(materialId,
randomGenerator);
Object value = propertyId.getRandomPropertyValue(randomGenerator);
materialsDataManager.setBatchPropertyValue(batchId, propertyId, value);
expectedReportItems.add(getReportItemFromBatch(c, batchId));
Expand Down Expand Up @@ -219,17 +231,17 @@ public void testInit() {

TestPluginData testPluginData = pluginBuilder.build();
Plugin testPlugin = TestPlugin.getTestPlugin(testPluginData);
//Set<ReportItem> actualReportItems = MaterialsActionSupport.testConsumers(8914112012010329946L, testPlugin, new BatchStatusReport(REPORT_ID)::init);
Set<ReportItem> actualReportItems = MaterialsActionSupport.testConsumers(2819236410498978100L, testPlugin, new BatchStatusReport(REPORT_ID)::init);

// Set<ReportItem> actualReportItems =
// MaterialsActionSupport.testConsumers(8914112012010329946L, testPlugin, new
// BatchStatusReport(REPORT_ID)::init);
Set<ReportItem> actualReportItems = MaterialsActionSupport.testConsumers(2819236410498978100L, testPlugin,
new BatchStatusReport(REPORT_ID)::init);

assertEquals(expectedReportItems, actualReportItems);
}

private static ReportItem getReportItem(List<Object> values) {
Builder builder = ReportItem.builder();
builder.setReportId(REPORT_ID);
builder.setReportHeader(REPORT_HEADER);
Builder builder = ReportItem.builder().setReportId(REPORT_ID).setReportHeader(REPORT_HEADER);
for (Object value : values) {
builder.addValue(value);
}
Expand All @@ -242,16 +254,17 @@ private static ReportItem getReportItem(List<Object> values) {

private static ReportHeader getReportHeader() {

ReportHeader.Builder builder = ReportHeader .builder()//
.add("time")//
.add("batch")//
.add("materials_producer")//
.add("stage")//
.add("material")//
.add("amount");//
ReportHeader.Builder builder = ReportHeader.builder()//
.add("time")//
.add("batch")//
.add("materials_producer")//
.add("stage")//
.add("material")//
.add("amount");//

for (TestMaterialId testMaterialId : TestMaterialId.values()) {
for (TestBatchPropertyId testBatchPropertyId : TestBatchPropertyId.getTestBatchPropertyIds(testMaterialId)) {
for (TestBatchPropertyId testBatchPropertyId : TestBatchPropertyId
.getTestBatchPropertyIds(testMaterialId)) {
builder.add(testMaterialId + "." + testBatchPropertyId);
}
}
Expand Down
Loading

0 comments on commit 196f699

Please sign in to comment.