-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add GridWatcherManagerImpl test
- Loading branch information
1 parent
eea3eee
commit 3867bfd
Showing
2 changed files
with
200 additions
and
1 deletion.
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
200 changes: 200 additions & 0 deletions
200
...est/java/com/refinedmods/refinedstorage2/api/grid/watcher/GridWatcherManagerImplTest.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,200 @@ | ||
package com.refinedmods.refinedstorage2.api.grid.watcher; | ||
|
||
import com.refinedmods.refinedstorage2.api.core.Action; | ||
import com.refinedmods.refinedstorage2.api.storage.Actor; | ||
import com.refinedmods.refinedstorage2.api.storage.InMemoryStorageImpl; | ||
import com.refinedmods.refinedstorage2.api.storage.channel.StorageChannel; | ||
import com.refinedmods.refinedstorage2.api.storage.channel.StorageChannelImpl; | ||
import com.refinedmods.refinedstorage2.api.storage.channel.StorageChannelType; | ||
|
||
import java.util.Set; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.InOrder; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.mockito.Mockito.inOrder; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.times; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.verifyNoInteractions; | ||
import static org.mockito.Mockito.verifyNoMoreInteractions; | ||
|
||
class GridWatcherManagerImplTest { | ||
GridWatcherManager sut; | ||
StorageChannelType<String> storageChannelType = StorageChannelImpl::new; | ||
StorageChannel<String> storageChannel; | ||
GridStorageChannelProvider storageChannelProvider; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
sut = new GridWatcherManagerImpl(); | ||
storageChannel = new StorageChannelImpl<>(); | ||
storageChannel.addSource(new InMemoryStorageImpl<>()); | ||
storageChannelProvider = new GridStorageChannelProvider() { | ||
@Override | ||
public Set<StorageChannelType<?>> getStorageChannelTypes() { | ||
return Set.of(storageChannelType); | ||
} | ||
|
||
@Override | ||
@SuppressWarnings("unchecked") | ||
public <T> StorageChannel<T> getStorageChannel(final StorageChannelType<T> type) { | ||
if (type == storageChannelType) { | ||
return (StorageChannel<T>) storageChannel; | ||
} | ||
throw new IllegalArgumentException(); | ||
} | ||
}; | ||
} | ||
|
||
@Test | ||
void shouldAddWatcherAndNotifyOfChanges() { | ||
// Arrange | ||
final GridWatcher watcher = mock(GridWatcher.class); | ||
storageChannel.insert("A", 10, Action.EXECUTE, FakeActor.INSTANCE); | ||
|
||
// Act | ||
sut.addWatcher(watcher, FakeActor.class, storageChannelProvider); | ||
storageChannel.insert("B", 5, Action.EXECUTE, FakeActor.INSTANCE); | ||
|
||
// Assert | ||
verify(watcher, times(1)).onChanged(storageChannelType, "B", 5, null); | ||
verifyNoMoreInteractions(watcher); | ||
} | ||
|
||
@Test | ||
void shouldNotAddDuplicateWatcher() { | ||
// Arrange | ||
final GridWatcher watcher = mock(GridWatcher.class); | ||
sut.addWatcher(watcher, FakeActor.class, storageChannelProvider); | ||
|
||
// Act & assert | ||
assertThrows( | ||
IllegalArgumentException.class, | ||
() -> sut.addWatcher(watcher, FakeActor.class, storageChannelProvider), | ||
"Watcher is already registered" | ||
); | ||
} | ||
|
||
@Test | ||
void shouldRemoveWatcher() { | ||
// Arrange | ||
final GridWatcher watcher = mock(GridWatcher.class); | ||
storageChannel.insert("A", 10, Action.EXECUTE, FakeActor.INSTANCE); | ||
sut.addWatcher(watcher, FakeActor.class, storageChannelProvider); | ||
|
||
// Act | ||
sut.removeWatcher(watcher, storageChannelProvider); | ||
storageChannel.insert("B", 5, Action.EXECUTE, FakeActor.INSTANCE); | ||
|
||
// Assert | ||
verifyNoInteractions(watcher); | ||
} | ||
|
||
@Test | ||
void shouldNotRemoveWatcherThatIsNotRegistered() { | ||
// Arrange | ||
final GridWatcher watcher = mock(GridWatcher.class); | ||
|
||
// Act & assert | ||
assertThrows( | ||
IllegalArgumentException.class, | ||
() -> sut.removeWatcher(watcher, storageChannelProvider), | ||
"Watcher is not registered" | ||
); | ||
} | ||
|
||
@Test | ||
void shouldAddAndRemoveAndAddWatcherAgain() { | ||
// Arrange | ||
final GridWatcher watcher = mock(GridWatcher.class); | ||
storageChannel.insert("A", 10, Action.EXECUTE, FakeActor.INSTANCE); | ||
|
||
// Act | ||
sut.addWatcher(watcher, FakeActor.class, storageChannelProvider); | ||
storageChannel.insert("B", 5, Action.EXECUTE, FakeActor.INSTANCE); | ||
sut.removeWatcher(watcher, storageChannelProvider); | ||
storageChannel.insert("C", 4, Action.EXECUTE, FakeActor.INSTANCE); | ||
sut.addWatcher(watcher, FakeActor.class, storageChannelProvider); | ||
storageChannel.insert("D", 3, Action.EXECUTE, FakeActor.INSTANCE); | ||
|
||
// Assert | ||
verify(watcher, times(1)).onChanged(storageChannelType, "B", 5, null); | ||
verify(watcher, times(1)).onChanged(storageChannelType, "D", 3, null); | ||
verifyNoMoreInteractions(watcher); | ||
} | ||
|
||
@Test | ||
void shouldDetachAll() { | ||
// Arrange | ||
final GridWatcher watcher = mock(GridWatcher.class); | ||
storageChannel.insert("A", 10, Action.EXECUTE, FakeActor.INSTANCE); | ||
sut.addWatcher(watcher, FakeActor.class, storageChannelProvider); | ||
|
||
// Act | ||
sut.detachAll(storageChannelProvider); | ||
storageChannel.insert("B", 10, Action.EXECUTE, FakeActor.INSTANCE); | ||
assertThrows(IllegalArgumentException.class, () -> sut.addWatcher( | ||
watcher, | ||
FakeActor.class, | ||
storageChannelProvider | ||
), "Watcher is already registered"); | ||
|
||
// Assert | ||
verifyNoInteractions(watcher); | ||
} | ||
|
||
@Test | ||
void shouldAttachAll() { | ||
// Arrange | ||
final GridWatcher watcher = mock(GridWatcher.class); | ||
storageChannel.insert("A", 10, Action.EXECUTE, FakeActor.INSTANCE); | ||
sut.addWatcher(watcher, FakeActor.class, storageChannelProvider); | ||
sut.detachAll(storageChannelProvider); | ||
storageChannel.insert("B", 5, Action.EXECUTE, FakeActor.INSTANCE); | ||
|
||
// Act | ||
sut.attachAll(storageChannelProvider); | ||
storageChannel.insert("C", 4, Action.EXECUTE, FakeActor.INSTANCE); | ||
|
||
// Assert | ||
final InOrder inOrder = inOrder(watcher); | ||
inOrder.verify(watcher, times(1)).invalidate(); | ||
inOrder.verify(watcher, times(1)).onChanged(storageChannelType, "A", 10, null); | ||
inOrder.verify(watcher, times(1)).onChanged(storageChannelType, "B", 5, null); | ||
inOrder.verify(watcher, times(1)).onChanged(storageChannelType, "C", 4, null); | ||
inOrder.verifyNoMoreInteractions(); | ||
} | ||
|
||
@Test | ||
void shouldNotifyAboutActivenessChange() { | ||
// Arrange | ||
final GridWatcher watcher = mock(GridWatcher.class); | ||
sut.activeChanged(true); | ||
sut.addWatcher(watcher, FakeActor.class, storageChannelProvider); | ||
|
||
// Act | ||
sut.activeChanged(false); | ||
sut.activeChanged(true); | ||
|
||
// Assert | ||
final InOrder inOrder = inOrder(watcher); | ||
inOrder.verify(watcher, times(1)).onActiveChanged(false); | ||
inOrder.verify(watcher, times(1)).onActiveChanged(true); | ||
inOrder.verifyNoMoreInteractions(); | ||
} | ||
|
||
private static class FakeActor implements Actor { | ||
public static final FakeActor INSTANCE = new FakeActor(); | ||
|
||
private FakeActor() { | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return "Fake"; | ||
} | ||
} | ||
} |