From bd95df95a48e26af67e6df73009b6b17bf0f51a2 Mon Sep 17 00:00:00 2001 From: Alva Swanson Date: Sat, 21 Dec 2024 00:11:08 +0000 Subject: [PATCH] Write tests for HistoricalDataStoreService.getMapSinceVersion(...) --- .../AccountAgeWitnessStorageServiceTest.java | 151 ++++++++++++++++++ .../DummyAccountAgeWitnessFactory.java | 39 +++++ .../DummyHistoricalDataStoreService.java | 51 ++++++ .../HistoricalDataStoreService.java | 2 +- 4 files changed, 242 insertions(+), 1 deletion(-) create mode 100644 core/src/test/java/bisq/core/account/witness/AccountAgeWitnessStorageServiceTest.java create mode 100644 core/src/test/java/bisq/core/account/witness/DummyAccountAgeWitnessFactory.java create mode 100644 core/src/test/java/bisq/core/account/witness/DummyHistoricalDataStoreService.java diff --git a/core/src/test/java/bisq/core/account/witness/AccountAgeWitnessStorageServiceTest.java b/core/src/test/java/bisq/core/account/witness/AccountAgeWitnessStorageServiceTest.java new file mode 100644 index 00000000000..3e1e0cab0d9 --- /dev/null +++ b/core/src/test/java/bisq/core/account/witness/AccountAgeWitnessStorageServiceTest.java @@ -0,0 +1,151 @@ +package bisq.core.account.witness; + +import bisq.network.p2p.storage.P2PDataStorage; +import bisq.network.p2p.storage.payload.PersistableNetworkPayload; +import bisq.network.p2p.storage.persistence.PersistableNetworkPayloadStore; + +import bisq.common.app.Version; + +import java.nio.file.Path; + +import java.io.File; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.anEmptyMap; +import static org.hamcrest.Matchers.is; + + +public class AccountAgeWitnessStorageServiceTest { + private DummyHistoricalDataStoreService storageService; + + @BeforeEach + void setup(@TempDir Path tempDirPath) { + File tempDir = tempDirPath.toFile(); + storageService = new DummyHistoricalDataStoreService(tempDir); + } + + @Test + void emptyStore() { + Map mapSinceVersion = + storageService.getMapSinceVersion(Version.VERSION); + assertThat(mapSinceVersion, is(anEmptyMap())); + } + + @Test + void testOnlyLiveData() { + Map liveDataMap = storageService.getStore().getMap(); + DummyAccountAgeWitnessFactory.addNewAccountAgeWitnessesToMap(liveDataMap, 2); + + Map mapSinceVersion = + storageService.getMapSinceVersion(Version.VERSION); + + P2PDataStorage.ByteArray firstByteArray = new P2PDataStorage.ByteArray(new byte[]{0}); + P2PDataStorage.ByteArray secondByteArray = new P2PDataStorage.ByteArray(new byte[]{1}); + + Map expected = Map.of( + firstByteArray, liveDataMap.get(firstByteArray), + secondByteArray, liveDataMap.get(secondByteArray)); + + assertThat(mapSinceVersion, is(expected)); + } + + @Test + void testOnlyStoreData() { + AccountAgeWitnessStore versionStore = DummyAccountAgeWitnessFactory + .createAccountAgeWitnessStore(1, 100); + + Map> storeByVersion = Map.of( + "1.8.0", versionStore); + + storageService.setStoresByVersion(storeByVersion); + + Map mapSinceVersion = + storageService.getMapSinceVersion("1.7.0"); + + Map expected = new HashMap<>(versionStore.getMap()); + assertThat(mapSinceVersion, is(expected)); + } + + @Test + void testRequesterVersionNull() { + Map liveDataMap = storageService.getStore().getMap(); + DummyAccountAgeWitnessFactory.addNewAccountAgeWitnessToMap(liveDataMap); + + AccountAgeWitnessStore firstVersionStore = DummyAccountAgeWitnessFactory + .createAccountAgeWitnessStore(1, 100); + AccountAgeWitnessStore secondVersionStore = DummyAccountAgeWitnessFactory + .createAccountAgeWitnessStore(1, 200); + + Map> storeByVersion = Map.of( + "1.8.0", firstVersionStore, + "1.9.0", secondVersionStore); + + storageService.setStoresByVersion(storeByVersion); + + Map mapSinceVersion = + storageService.getMapSinceVersion(null); + + Map expected = new HashMap<>(); + expected.putAll(liveDataMap); + expected.putAll(firstVersionStore.getMap()); + expected.putAll(secondVersionStore.getMap()); + + assertThat(mapSinceVersion, is(expected)); + } + + @Test + void testRequesterVersionIsOlder() { + Map liveDataMap = storageService.getStore().getMap(); + DummyAccountAgeWitnessFactory.addNewAccountAgeWitnessToMap(liveDataMap); + + AccountAgeWitnessStore firstVersionStore = DummyAccountAgeWitnessFactory. + createAccountAgeWitnessStore(1, 100); + AccountAgeWitnessStore secondVersionStore = DummyAccountAgeWitnessFactory. + createAccountAgeWitnessStore(1, 200); + + Map> storeByVersion = Map.of( + "1.8.0", firstVersionStore, + "1.9.0", secondVersionStore); + + storageService.setStoresByVersion(storeByVersion); + + Map mapSinceVersion = + storageService.getMapSinceVersion("1.8.5"); + + Map expected = new HashMap<>(); + expected.putAll(liveDataMap); + expected.putAll(secondVersionStore.getMap()); + + assertThat(mapSinceVersion, is(expected)); + } + + @Test + void testRequesterVersionIsNewer() { + Map liveDataMap = storageService.getStore().getMap(); + DummyAccountAgeWitnessFactory.addNewAccountAgeWitnessToMap(liveDataMap); + + AccountAgeWitnessStore firstVersionStore = DummyAccountAgeWitnessFactory. + createAccountAgeWitnessStore(1, 100); + AccountAgeWitnessStore secondVersionStore = DummyAccountAgeWitnessFactory. + createAccountAgeWitnessStore(1, 200); + + Map> storeByVersion = Map.of( + "1.8.0", firstVersionStore, + "1.9.0", secondVersionStore); + + storageService.setStoresByVersion(storeByVersion); + + Map mapSinceVersion = + storageService.getMapSinceVersion("1.9.5"); + + Map expected = new HashMap<>(liveDataMap); + assertThat(mapSinceVersion, is(expected)); + } +} diff --git a/core/src/test/java/bisq/core/account/witness/DummyAccountAgeWitnessFactory.java b/core/src/test/java/bisq/core/account/witness/DummyAccountAgeWitnessFactory.java new file mode 100644 index 00000000000..47eaaec5719 --- /dev/null +++ b/core/src/test/java/bisq/core/account/witness/DummyAccountAgeWitnessFactory.java @@ -0,0 +1,39 @@ +package bisq.core.account.witness; + +import bisq.network.p2p.storage.P2PDataStorage; +import bisq.network.p2p.storage.payload.PersistableNetworkPayload; + +import java.util.Map; + +public class DummyAccountAgeWitnessFactory { + + static AccountAgeWitnessStore createAccountAgeWitnessStore(int numberOfWitnesses, int startKeyOffset) { + AccountAgeWitnessStore accountAgeWitnessStore = new AccountAgeWitnessStore(); + Map storeMap = accountAgeWitnessStore.getMap(); + addNewAccountAgeWitnessesToMap(storeMap, numberOfWitnesses, startKeyOffset); + return accountAgeWitnessStore; + } + + static void addNewAccountAgeWitnessesToMap(Map liveDataMap, + int numberOfWitnesses) { + addNewAccountAgeWitnessesToMap(liveDataMap, numberOfWitnesses, 0); + } + + private static void addNewAccountAgeWitnessesToMap(Map liveDataMap, + int numberOfWitnesses, + int startKeyOffset) { + for (int i = 0; i < numberOfWitnesses; i++) { + addNewAccountAgeWitnessToMap(liveDataMap, startKeyOffset + i); + } + } + + static void addNewAccountAgeWitnessToMap(Map liveDataMap) { + addNewAccountAgeWitnessToMap(liveDataMap, liveDataMap.size()); + } + + private static void addNewAccountAgeWitnessToMap(Map liveDataMap, int key) { + P2PDataStorage.ByteArray byteArray = new P2PDataStorage.ByteArray(new byte[]{(byte) key}); + AccountAgeWitness accountAgeWitness = new AccountAgeWitness(new byte[]{(byte) key}, key + 1); + liveDataMap.put(byteArray, accountAgeWitness); + } +} diff --git a/core/src/test/java/bisq/core/account/witness/DummyHistoricalDataStoreService.java b/core/src/test/java/bisq/core/account/witness/DummyHistoricalDataStoreService.java new file mode 100644 index 00000000000..ec18de88615 --- /dev/null +++ b/core/src/test/java/bisq/core/account/witness/DummyHistoricalDataStoreService.java @@ -0,0 +1,51 @@ +package bisq.core.account.witness; + +import bisq.network.p2p.storage.payload.PersistableNetworkPayload; +import bisq.network.p2p.storage.persistence.HistoricalDataStoreService; +import bisq.network.p2p.storage.persistence.PersistableNetworkPayloadStore; + +import com.google.common.collect.ImmutableMap; + +import java.io.File; + +import java.util.Collections; +import java.util.Map; + +public class DummyHistoricalDataStoreService extends HistoricalDataStoreService { + + public DummyHistoricalDataStoreService(File storageDir) { + super(storageDir, null); + store = new AccountAgeWitnessStore(); + storesByVersion = ImmutableMap.copyOf(Collections.emptyMap()); + } + + @Override + public boolean canHandle(PersistableNetworkPayload payload) { + return false; + } + + @Override + public String getFileName() { + return ""; + } + + @Override + protected void initializePersistenceManager() { + + } + + @Override + protected AccountAgeWitnessStore createStore() { + return null; + } + + public AccountAgeWitnessStore getStore() { + return store; + } + + public void setStoresByVersion(Map> + storesByVersion) { + this.storesByVersion = ImmutableMap.copyOf(storesByVersion); + } +} + diff --git a/p2p/src/main/java/bisq/network/p2p/storage/persistence/HistoricalDataStoreService.java b/p2p/src/main/java/bisq/network/p2p/storage/persistence/HistoricalDataStoreService.java index ce86e2001ef..266b75a1805 100644 --- a/p2p/src/main/java/bisq/network/p2p/storage/persistence/HistoricalDataStoreService.java +++ b/p2p/src/main/java/bisq/network/p2p/storage/persistence/HistoricalDataStoreService.java @@ -42,7 +42,7 @@ */ @Slf4j public abstract class HistoricalDataStoreService> extends MapStoreService { - private ImmutableMap> storesByVersion; + protected ImmutableMap> storesByVersion; // Cache to avoid that we have to recreate the historical data at each request private ImmutableMap allHistoricalPayloads;