[TESTS] Introduce MapStoreServiceFake

Now that we want to make changes to the MapStoreService,
it isn't sufficient to have a Fake of the ProtectedDataStoreService.

Tests now use a REAL ProtectedDataStoreService and a FAKE MapStoreService
to exercise more of the production code and allow future testing of
changes to MapStoreService.
This commit is contained in:
Julian Knutsen 2019-11-19 18:23:16 -08:00
parent 685824b0d9
commit 66f71e59f8
No known key found for this signature in database
GPG Key ID: D85F536DB3615B2D
2 changed files with 35 additions and 14 deletions

View File

@ -29,12 +29,13 @@ import bisq.network.p2p.storage.messages.RemoveDataMessage;
import bisq.network.p2p.storage.messages.RemoveMailboxDataMessage;
import bisq.network.p2p.storage.mocks.AppendOnlyDataStoreServiceFake;
import bisq.network.p2p.storage.mocks.ClockFake;
import bisq.network.p2p.storage.mocks.ProtectedDataStoreServiceFake;
import bisq.network.p2p.storage.mocks.MapStoreServiceFake;
import bisq.network.p2p.storage.payload.MailboxStoragePayload;
import bisq.network.p2p.storage.payload.PersistableNetworkPayload;
import bisq.network.p2p.storage.payload.ProtectedMailboxStorageEntry;
import bisq.network.p2p.storage.payload.ProtectedStorageEntry;
import bisq.network.p2p.storage.persistence.AppendOnlyDataStoreListener;
import bisq.network.p2p.storage.persistence.MapStoreService;
import bisq.network.p2p.storage.persistence.ProtectedDataStoreListener;
import bisq.network.p2p.storage.persistence.ProtectedDataStoreService;
import bisq.network.p2p.storage.persistence.ResourceDataStoreService;
@ -80,7 +81,7 @@ public class TestState {
this.mockBroadcaster = mock(Broadcaster.class);
this.mockSeqNrStorage = mock(Storage.class);
this.clockFake = new ClockFake();
this.protectedDataStoreService = new ProtectedDataStoreServiceFake();
this.protectedDataStoreService = new ProtectedDataStoreService();
this.mockedStorage = new P2PDataStorage(mock(NetworkNode.class),
this.mockBroadcaster,
@ -91,6 +92,7 @@ public class TestState {
this.appendOnlyDataStoreListener = mock(AppendOnlyDataStoreListener.class);
this.protectedDataStoreListener = mock(ProtectedDataStoreListener.class);
this.hashMapChangedListener = mock(HashMapChangedListener.class);
this.protectedDataStoreService.addService(new MapStoreServiceFake());
this.mockedStorage = createP2PDataStorageForTest(
this.mockBroadcaster,

View File

@ -19,33 +19,52 @@ package bisq.network.p2p.storage.mocks;
import bisq.network.p2p.storage.P2PDataStorage;
import bisq.network.p2p.storage.payload.ProtectedStorageEntry;
import bisq.network.p2p.storage.persistence.ProtectedDataStoreService;
import bisq.network.p2p.storage.persistence.MapStoreService;
import bisq.common.proto.persistable.PersistableEnvelope;
import bisq.common.proto.persistable.PersistablePayload;
import bisq.common.storage.Storage;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import lombok.Getter;
import static org.mockito.Mockito.mock;
/**
* Implementation of an in-memory ProtectedDataStoreService that can be used in tests. Removes overhead
* Implementation of an in-memory MapStoreService that can be used in tests. Removes overhead
* involving files, resources, and services for tests that don't need it.
*
* @see <a href="https://martinfowler.com/articles/mocksArentStubs.html#TheDifferenceBetweenMocksAndStubs">Reference</a>
*/
public class ProtectedDataStoreServiceFake extends ProtectedDataStoreService {
public class MapStoreServiceFake extends MapStoreService {
@Getter
private final Map<P2PDataStorage.ByteArray, ProtectedStorageEntry> map;
public ProtectedDataStoreServiceFake() {
super();
map = new HashMap<>();
public MapStoreServiceFake() {
super(mock(File.class), mock(Storage.class));
this.map = new HashMap<>();
}
public Map<P2PDataStorage.ByteArray, ProtectedStorageEntry> getMap() {
return map;
@Override
public String getFileName() {
return null;
}
public void put(P2PDataStorage.ByteArray hashAsByteArray, ProtectedStorageEntry entry) {
map.put(hashAsByteArray, entry);
@Override
protected PersistableEnvelope createStore() {
return null;
}
public ProtectedStorageEntry remove(P2PDataStorage.ByteArray hash, ProtectedStorageEntry protectedStorageEntry) {
return map.remove(hash);
@Override
public boolean canHandle(PersistablePayload payload) {
return true;
}
protected void readFromResources(String postFix) {
// do nothing. This Fake only supports in-memory storage.
}
}