diff --git a/refinedstorage2-storage-api/src/main/java/com/refinedmods/refinedstorage2/api/storage/StateTrackedStorage.java b/refinedstorage2-storage-api/src/main/java/com/refinedmods/refinedstorage2/api/storage/StateTrackedStorage.java index 6bda55a9a..980d7dd4d 100644 --- a/refinedstorage2-storage-api/src/main/java/com/refinedmods/refinedstorage2/api/storage/StateTrackedStorage.java +++ b/refinedstorage2-storage-api/src/main/java/com/refinedmods/refinedstorage2/api/storage/StateTrackedStorage.java @@ -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(); } diff --git a/refinedstorage2-storage-api/src/test/java/com/refinedmods/refinedstorage2/api/storage/StateTrackedStorageTest.java b/refinedstorage2-storage-api/src/test/java/com/refinedmods/refinedstorage2/api/storage/StateTrackedStorageTest.java index 4c6a7bcab..4815bcc67 100644 --- a/refinedstorage2-storage-api/src/test/java/com/refinedmods/refinedstorage2/api/storage/StateTrackedStorageTest.java +++ b/refinedstorage2-storage-api/src/test/java/com/refinedmods/refinedstorage2/api/storage/StateTrackedStorageTest.java @@ -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; @@ -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 underlyingStorage = new LimitedStorageImpl<>(100); + underlyingStorage.insert("A", amount, Action.EXECUTE, EmptyActor.INSTANCE); + final StateTrackedStorage sut = new StateTrackedStorage<>(underlyingStorage, listener); + + // Act + final StorageState state = sut.getState(); + + // Assert + assertThat(state).isEqualTo(expectedState); + } + + private static Stream 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 underlyingStorage = new InMemoryStorageImpl<>(); + underlyingStorage.insert("A", 75, Action.EXECUTE, EmptyActor.INSTANCE); + final StateTrackedStorage 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 underlyingStorage = new LimitedStorageImpl<>(100); @@ -58,10 +110,11 @@ void shouldCallStateChangeListenerWhenExtracting(final Action action) { final StateTrackedStorage 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(); @@ -77,10 +130,11 @@ void shouldCallStateChangeListenerWhenInserting(final Action action) { final StateTrackedStorage 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(); }