Skip to content

Commit

Permalink
Merge pull request #493 from refinedmods/fix/GH-456/testhardening
Browse files Browse the repository at this point in the history
refactor: test hardening
  • Loading branch information
raoulvdberge authored Mar 14, 2024
2 parents 971b44e + 4d9a264 commit 9ffd4e4
Show file tree
Hide file tree
Showing 20 changed files with 141 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.refinedmods.refinedstorage2.api.network.impl.component.EnergyNetworkComponentImpl;
import com.refinedmods.refinedstorage2.api.network.impl.component.GraphNetworkComponentImpl;
import com.refinedmods.refinedstorage2.api.network.impl.component.StorageNetworkComponentImpl;
import com.refinedmods.refinedstorage2.api.resource.list.ResourceListImpl;

public final class NetworkTestFixtures {
public static final ComponentMapFactory<NetworkComponent, Network> NETWORK_COMPONENT_MAP_FACTORY =
Expand All @@ -25,7 +26,7 @@ public final class NetworkTestFixtures {
);
NETWORK_COMPONENT_MAP_FACTORY.addFactory(
StorageNetworkComponent.class,
network -> new StorageNetworkComponentImpl()
network -> new StorageNetworkComponentImpl(new ResourceListImpl())
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@
public class StorageNetworkComponentImpl extends StorageChannelImpl implements StorageNetworkComponent {
private static final Logger LOGGER = LoggerFactory.getLogger(StorageNetworkComponentImpl.class);

public StorageNetworkComponentImpl() {
}

protected StorageNetworkComponentImpl(final ResourceList list) {
public StorageNetworkComponentImpl(final ResourceList list) {
super(list);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,4 @@ public boolean transfer(final ResourceKey resource, final Actor actor, final Net
}
return false;
}

@Override
public String toString() {
return "CompositeExporterTransferStrategy{"
+ "strategies=" + strategies
+ '}';
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,37 @@
import com.refinedmods.refinedstorage2.api.resource.filter.FilterMode;
import com.refinedmods.refinedstorage2.api.storage.Actor;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.function.UnaryOperator;
import javax.annotation.Nullable;

public class ImporterNetworkNode extends AbstractNetworkNode {
private long energyUsage;
private final Filter filter = new Filter();
private final Actor actor = new NetworkNodeActor(this);
@Nullable
private ImporterTransferStrategy transferStrategy;
private final List<ImporterTransferStrategy> transferStrategies = new ArrayList<>();

public ImporterNetworkNode(final long energyUsage) {
this.energyUsage = energyUsage;
}

public void setTransferStrategy(@Nullable final ImporterTransferStrategy transferStrategy) {
this.transferStrategy = transferStrategy;
public void setTransferStrategies(final List<ImporterTransferStrategy> transferStrategies) {
this.transferStrategies.clear();
this.transferStrategies.addAll(transferStrategies);
}

@Override
public void doWork() {
super.doWork();
if (network == null || !isActive() || transferStrategy == null) {
if (network == null || !isActive()) {
return;
}
transferStrategy.transfer(filter, actor, network);
for (final ImporterTransferStrategy transferStrategy : transferStrategies) {
if (transferStrategy.transfer(filter, actor, network)) {
return;
}
}
}

public FilterMode getFilterMode() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.refinedmods.refinedstorage2.api.network.node.importer;
package com.refinedmods.refinedstorage2.api.network.impl.node.importer;

import com.refinedmods.refinedstorage2.api.resource.ResourceKey;
import com.refinedmods.refinedstorage2.api.storage.ExtractableStorage;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.refinedmods.refinedstorage2.api.network.node.importer;
package com.refinedmods.refinedstorage2.api.network.impl.node.importer;

import com.refinedmods.refinedstorage2.api.network.Network;
import com.refinedmods.refinedstorage2.api.network.component.StorageNetworkComponent;
import com.refinedmods.refinedstorage2.api.network.node.importer.ImporterTransferStrategy;
import com.refinedmods.refinedstorage2.api.resource.ResourceKey;
import com.refinedmods.refinedstorage2.api.resource.filter.Filter;
import com.refinedmods.refinedstorage2.api.storage.Actor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.refinedmods.refinedstorage2.api.network.impl.node.storage.StorageNetworkNode;
import com.refinedmods.refinedstorage2.api.network.node.container.NetworkNodeContainer;
import com.refinedmods.refinedstorage2.api.resource.ResourceAmount;
import com.refinedmods.refinedstorage2.api.resource.list.ResourceListImpl;
import com.refinedmods.refinedstorage2.api.storage.EmptyActor;
import com.refinedmods.refinedstorage2.api.storage.TrackedResourceAmount;
import com.refinedmods.refinedstorage2.api.storage.limited.LimitedStorageImpl;
Expand Down Expand Up @@ -35,7 +36,7 @@ class StorageNetworkComponentImplTest {

@BeforeEach
void setUp() {
sut = new StorageNetworkComponentImpl();
sut = new StorageNetworkComponentImpl(new ResourceListImpl());

storage1 = new StorageNetworkNode(0);
storage1.setNetwork(new NetworkImpl(NetworkTestFixtures.NETWORK_COMPONENT_MAP_FACTORY));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.refinedmods.refinedstorage2.api.core.Action;
import com.refinedmods.refinedstorage2.api.network.energy.EnergyStorage;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;

Expand Down Expand Up @@ -340,4 +341,26 @@ void shouldExtractFromMultipleSourcesCompletelyMoreThanIsAvailable(final Action

assertThat(sut.getCapacity()).isEqualTo(15);
}

@Test
void shouldNotExceedLongMax() {
// Arrange
final EnergyStorage a = new EnergyStorageImpl(Long.MAX_VALUE);
final EnergyStorage b = new EnergyStorageImpl(Long.MAX_VALUE);

a.receive(Long.MAX_VALUE, Action.EXECUTE);
b.receive(Long.MAX_VALUE, Action.EXECUTE);

final CompositeEnergyStorage sut = new CompositeEnergyStorage();
sut.addSource(a);
sut.addSource(b);

// Act
final long stored = sut.getStored();
final long capacity = sut.getCapacity();

// Assert
assertThat(stored).isEqualTo(Long.MAX_VALUE);
assertThat(capacity).isEqualTo(Long.MAX_VALUE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,21 @@ void shouldExtractEnergyCompletely(final Action action) {
assertThat(changeCount).isZero();
}
}

@ParameterizedTest
@EnumSource(Action.class)
void shouldNotExtractEnergyWhenThereIsNone(final Action action) {
// Arrange
sut.receive(100, Action.EXECUTE);
sut.extract(100, Action.EXECUTE);
changeCount = 0;

// Act
final long extracted = sut.extract(1, action);

// Assert
assertThat(extracted).isZero();
assertThat(sut.getStored()).isZero();
assertThat(changeCount).isZero();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import com.refinedmods.refinedstorage2.api.network.node.exporter.ExporterTransferStrategy;
import com.refinedmods.refinedstorage2.api.network.node.task.TaskExecutor;
import com.refinedmods.refinedstorage2.api.resource.ResourceAmount;
import com.refinedmods.refinedstorage2.api.resource.ResourceKey;
import com.refinedmods.refinedstorage2.api.storage.Actor;
import com.refinedmods.refinedstorage2.api.storage.EmptyActor;
import com.refinedmods.refinedstorage2.api.storage.InMemoryStorageImpl;
import com.refinedmods.refinedstorage2.api.storage.InsertableStorage;
Expand Down Expand Up @@ -58,22 +60,62 @@ void shouldUseFirstSuccessfulStrategy(
// Arrange
storageChannel.addSource(new InMemoryStorageImpl());
storageChannel.insert(A, 100, Action.EXECUTE, EmptyActor.INSTANCE);
storageChannel.insert(B, 100, Action.EXECUTE, EmptyActor.INSTANCE);

final Storage failingDestination = new LimitedStorageImpl(0);
final Storage destination = new LimitedStorageImpl(100);

sut.setTransferStrategy(new CompositeExporterTransferStrategy(List.of(
createTransferStrategy(destination, 10),
createTransferStrategy(failingDestination, 10),
createTransferStrategy(destination, 10),
createTransferStrategy(destination, 10)
)));
sut.setFilters(List.of(A));
sut.setFilters(List.of(A, B));

// Act
sut.doWork();

// Assert
assertThat(storageChannel.getAll()).usingRecursiveFieldByFieldElementComparator().containsExactlyInAnyOrder(
new ResourceAmount(A, 90)
new ResourceAmount(A, 90),
new ResourceAmount(B, 100)
);
assertThat(destination.getAll()).usingRecursiveFieldByFieldElementComparator().containsExactlyInAnyOrder(
new ResourceAmount(A, 10)
);
}

@Test
void shouldUseFirstSuccessfulResourceInTheStrategy(
@InjectNetworkStorageChannel final StorageChannel storageChannel
) {
// Arrange
storageChannel.addSource(new InMemoryStorageImpl());
storageChannel.insert(A, 100, Action.EXECUTE, EmptyActor.INSTANCE);
storageChannel.insert(B, 100, Action.EXECUTE, EmptyActor.INSTANCE);

final Storage destination = new LimitedStorageImpl(100) {
@Override
public long insert(final ResourceKey resource, final long amount, final Action action, final Actor actor) {
if (resource != A) {
return 0;
}
return super.insert(resource, amount, action, actor);
}
};

sut.setTransferStrategy(new CompositeExporterTransferStrategy(List.of(
createTransferStrategy(destination, 10)
)));
sut.setFilters(List.of(B, A));

// Act
sut.doWork();

// Assert
assertThat(storageChannel.getAll()).usingRecursiveFieldByFieldElementComparator().containsExactlyInAnyOrder(
new ResourceAmount(A, 90),
new ResourceAmount(B, 100)
);
assertThat(destination.getAll()).usingRecursiveFieldByFieldElementComparator().containsExactlyInAnyOrder(
new ResourceAmount(A, 10)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.refinedmods.refinedstorage2.api.network.impl.node.importer;

import com.refinedmods.refinedstorage2.api.core.Action;
import com.refinedmods.refinedstorage2.api.network.node.importer.ImporterSource;
import com.refinedmods.refinedstorage2.api.resource.ResourceAmount;
import com.refinedmods.refinedstorage2.api.resource.ResourceKey;
import com.refinedmods.refinedstorage2.api.storage.Actor;
Expand Down
Loading

0 comments on commit 9ffd4e4

Please sign in to comment.