Skip to content

Commit

Permalink
refactor: improve StateTrackedStorage test
Browse files Browse the repository at this point in the history
  • Loading branch information
raoulvdberge committed Dec 31, 2023
1 parent e1f11ca commit ebc21f3
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,6 @@ private void notifyListener() {
@Override
public long extract(final T resource, final long amount, final Action action, final Actor actor) {
final long extracted = delegate.extract(resource, amount, action, actor);
// TODO: https://sonarcloud.io/component_measures?metric=new_coverage&selected=refinedmods_refinedstorage2%3
// Arefinedstorage2-storage-api%2Fsrc%2Fmain%2Fjava%2Fcom%2Frefinedmods%2Frefinedstorage2%2Fapi%2
// Fstorage%2FStateTrackedStorage.java&view=list&pullRequest=465&id=refinedmods_refinedstorage2
if (extracted > 0 && action == Action.EXECUTE) {
checkStateChanged();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package com.refinedmods.refinedstorage2.api.storage;

import com.refinedmods.refinedstorage2.api.core.Action;
import com.refinedmods.refinedstorage2.api.resource.ResourceAmount;
import com.refinedmods.refinedstorage2.api.storage.limited.LimitedStorageImpl;
import com.refinedmods.refinedstorage2.api.storage.tracked.TrackedStorageImpl;

import java.util.stream.Stream;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.EnumSource;
import org.junit.jupiter.params.provider.MethodSource;
import org.mockito.verification.VerificationMode;

import static org.assertj.core.api.Assertions.assertThat;
Expand All @@ -16,8 +21,55 @@
import static org.mockito.Mockito.verify;

class StateTrackedStorageTest {
@ParameterizedTest
@MethodSource("states")
void testStates(final long amount, final StorageState expectedState) {
// Arrange
final StateTrackedStorage.Listener listener = mock(StateTrackedStorage.Listener.class);
final Storage<String> underlyingStorage = new LimitedStorageImpl<>(100);
underlyingStorage.insert("A", amount, Action.EXECUTE, EmptyActor.INSTANCE);
final StateTrackedStorage<String> sut = new StateTrackedStorage<>(underlyingStorage, listener);

// Act
final StorageState state = sut.getState();

// Assert
assertThat(state).isEqualTo(expectedState);
}

private static Stream<Arguments> states() {
return Stream.of(
Arguments.of(1L, StorageState.NORMAL),
Arguments.of(74L, StorageState.NORMAL),
Arguments.of(75L, StorageState.NEAR_CAPACITY),
Arguments.of(99L, StorageState.NEAR_CAPACITY),
Arguments.of(100L, StorageState.FULL)
);
}

@Test
void shouldSetInitialState() {
// Arrange
final StateTrackedStorage.Listener listener = mock(StateTrackedStorage.Listener.class);
final Storage<String> underlyingStorage = new InMemoryStorageImpl<>();
underlyingStorage.insert("A", 75, Action.EXECUTE, EmptyActor.INSTANCE);
final StateTrackedStorage<String> sut = new StateTrackedStorage<>(underlyingStorage, listener);

// Act
final StorageState state = sut.getState();

// Assert
verify(listener, never()).onStorageStateChanged();
assertThat(state).isEqualTo(StorageState.NORMAL);
assertThat(sut.getAll()).usingRecursiveFieldByFieldElementComparator().containsExactly(
new ResourceAmount<>("A", 75)
);
assertThat(sut.getStored()).isEqualTo(75);
}


@Test
void shouldSetInitialStateForLimitedStorage() {
// Arrange
final StateTrackedStorage.Listener listener = mock(StateTrackedStorage.Listener.class);
final Storage<String> underlyingStorage = new LimitedStorageImpl<>(100);
Expand Down Expand Up @@ -58,10 +110,11 @@ void shouldCallStateChangeListenerWhenExtracting(final Action action) {
final StateTrackedStorage<String> sut = new StateTrackedStorage<>(underlyingStorage, listener);

// Act
sut.extract("A", 1, action, EmptyActor.INSTANCE);
final long extracted = sut.extract("A", 1, action, EmptyActor.INSTANCE);
sut.extract("A", 1, action, EmptyActor.INSTANCE);

// Assert
assertThat(extracted).isEqualTo(1);
final VerificationMode expectedTimes = action == Action.EXECUTE ? times(1) : never();
verify(listener, expectedTimes).onStorageStateChanged();
assertThat(sut.findTrackedResourceByActorType("A", EmptyActor.class)).isEmpty();
Expand All @@ -77,10 +130,11 @@ void shouldCallStateChangeListenerWhenInserting(final Action action) {
final StateTrackedStorage<String> sut = new StateTrackedStorage<>(underlyingStorage, listener);

// Act
sut.insert("A", 1, action, EmptyActor.INSTANCE);
final long inserted = sut.insert("A", 1, action, EmptyActor.INSTANCE);
sut.insert("A", 1, action, EmptyActor.INSTANCE);

// Assert
assertThat(inserted).isEqualTo(1);
final VerificationMode expectedTimes = action == Action.EXECUTE ? times(1) : never();
verify(listener, expectedTimes).onStorageStateChanged();
}
Expand Down

0 comments on commit ebc21f3

Please sign in to comment.