Skip to content

Commit

Permalink
chore: add GridWatcherManagerImpl test
Browse files Browse the repository at this point in the history
  • Loading branch information
raoulvdberge committed Jan 2, 2024
1 parent eea3eee commit 3867bfd
Show file tree
Hide file tree
Showing 2 changed files with 200 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

// TODO: test.
@API(status = API.Status.STABLE, since = "2.0.0-milestone.3.3")
public class GridWatcherManagerImpl implements GridWatcherManager {
private static final Logger LOGGER = LoggerFactory.getLogger(GridWatcherManagerImpl.class);
Expand Down
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";
}
}
}

0 comments on commit 3867bfd

Please sign in to comment.