-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Write tests for HistoricalDataStoreService.getMapSinceVersion(...)
- Loading branch information
Showing
4 changed files
with
242 additions
and
1 deletion.
There are no files selected for viewing
151 changes: 151 additions & 0 deletions
151
core/src/test/java/bisq/core/account/witness/AccountAgeWitnessStorageServiceTest.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,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<P2PDataStorage.ByteArray, PersistableNetworkPayload> mapSinceVersion = | ||
storageService.getMapSinceVersion(Version.VERSION); | ||
assertThat(mapSinceVersion, is(anEmptyMap())); | ||
} | ||
|
||
@Test | ||
void testOnlyLiveData() { | ||
Map<P2PDataStorage.ByteArray, PersistableNetworkPayload> liveDataMap = storageService.getStore().getMap(); | ||
DummyAccountAgeWitnessFactory.addNewAccountAgeWitnessesToMap(liveDataMap, 2); | ||
|
||
Map<P2PDataStorage.ByteArray, PersistableNetworkPayload> mapSinceVersion = | ||
storageService.getMapSinceVersion(Version.VERSION); | ||
|
||
P2PDataStorage.ByteArray firstByteArray = new P2PDataStorage.ByteArray(new byte[]{0}); | ||
P2PDataStorage.ByteArray secondByteArray = new P2PDataStorage.ByteArray(new byte[]{1}); | ||
|
||
Map<P2PDataStorage.ByteArray, PersistableNetworkPayload> 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<String, PersistableNetworkPayloadStore<? extends PersistableNetworkPayload>> storeByVersion = Map.of( | ||
"1.8.0", versionStore); | ||
|
||
storageService.setStoresByVersion(storeByVersion); | ||
|
||
Map<P2PDataStorage.ByteArray, PersistableNetworkPayload> mapSinceVersion = | ||
storageService.getMapSinceVersion("1.7.0"); | ||
|
||
Map<P2PDataStorage.ByteArray, PersistableNetworkPayload> expected = new HashMap<>(versionStore.getMap()); | ||
assertThat(mapSinceVersion, is(expected)); | ||
} | ||
|
||
@Test | ||
void testRequesterVersionNull() { | ||
Map<P2PDataStorage.ByteArray, PersistableNetworkPayload> liveDataMap = storageService.getStore().getMap(); | ||
DummyAccountAgeWitnessFactory.addNewAccountAgeWitnessToMap(liveDataMap); | ||
|
||
AccountAgeWitnessStore firstVersionStore = DummyAccountAgeWitnessFactory | ||
.createAccountAgeWitnessStore(1, 100); | ||
AccountAgeWitnessStore secondVersionStore = DummyAccountAgeWitnessFactory | ||
.createAccountAgeWitnessStore(1, 200); | ||
|
||
Map<String, PersistableNetworkPayloadStore<? extends PersistableNetworkPayload>> storeByVersion = Map.of( | ||
"1.8.0", firstVersionStore, | ||
"1.9.0", secondVersionStore); | ||
|
||
storageService.setStoresByVersion(storeByVersion); | ||
|
||
Map<P2PDataStorage.ByteArray, PersistableNetworkPayload> mapSinceVersion = | ||
storageService.getMapSinceVersion(null); | ||
|
||
Map<P2PDataStorage.ByteArray, PersistableNetworkPayload> expected = new HashMap<>(); | ||
expected.putAll(liveDataMap); | ||
expected.putAll(firstVersionStore.getMap()); | ||
expected.putAll(secondVersionStore.getMap()); | ||
|
||
assertThat(mapSinceVersion, is(expected)); | ||
} | ||
|
||
@Test | ||
void testRequesterVersionIsOlder() { | ||
Map<P2PDataStorage.ByteArray, PersistableNetworkPayload> liveDataMap = storageService.getStore().getMap(); | ||
DummyAccountAgeWitnessFactory.addNewAccountAgeWitnessToMap(liveDataMap); | ||
|
||
AccountAgeWitnessStore firstVersionStore = DummyAccountAgeWitnessFactory. | ||
createAccountAgeWitnessStore(1, 100); | ||
AccountAgeWitnessStore secondVersionStore = DummyAccountAgeWitnessFactory. | ||
createAccountAgeWitnessStore(1, 200); | ||
|
||
Map<String, PersistableNetworkPayloadStore<? extends PersistableNetworkPayload>> storeByVersion = Map.of( | ||
"1.8.0", firstVersionStore, | ||
"1.9.0", secondVersionStore); | ||
|
||
storageService.setStoresByVersion(storeByVersion); | ||
|
||
Map<P2PDataStorage.ByteArray, PersistableNetworkPayload> mapSinceVersion = | ||
storageService.getMapSinceVersion("1.8.5"); | ||
|
||
Map<P2PDataStorage.ByteArray, PersistableNetworkPayload> expected = new HashMap<>(); | ||
expected.putAll(liveDataMap); | ||
expected.putAll(secondVersionStore.getMap()); | ||
|
||
assertThat(mapSinceVersion, is(expected)); | ||
} | ||
|
||
@Test | ||
void testRequesterVersionIsNewer() { | ||
Map<P2PDataStorage.ByteArray, PersistableNetworkPayload> liveDataMap = storageService.getStore().getMap(); | ||
DummyAccountAgeWitnessFactory.addNewAccountAgeWitnessToMap(liveDataMap); | ||
|
||
AccountAgeWitnessStore firstVersionStore = DummyAccountAgeWitnessFactory. | ||
createAccountAgeWitnessStore(1, 100); | ||
AccountAgeWitnessStore secondVersionStore = DummyAccountAgeWitnessFactory. | ||
createAccountAgeWitnessStore(1, 200); | ||
|
||
Map<String, PersistableNetworkPayloadStore<? extends PersistableNetworkPayload>> storeByVersion = Map.of( | ||
"1.8.0", firstVersionStore, | ||
"1.9.0", secondVersionStore); | ||
|
||
storageService.setStoresByVersion(storeByVersion); | ||
|
||
Map<P2PDataStorage.ByteArray, PersistableNetworkPayload> mapSinceVersion = | ||
storageService.getMapSinceVersion("1.9.5"); | ||
|
||
Map<P2PDataStorage.ByteArray, PersistableNetworkPayload> expected = new HashMap<>(liveDataMap); | ||
assertThat(mapSinceVersion, is(expected)); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
core/src/test/java/bisq/core/account/witness/DummyAccountAgeWitnessFactory.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,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<P2PDataStorage.ByteArray, PersistableNetworkPayload> storeMap = accountAgeWitnessStore.getMap(); | ||
addNewAccountAgeWitnessesToMap(storeMap, numberOfWitnesses, startKeyOffset); | ||
return accountAgeWitnessStore; | ||
} | ||
|
||
static void addNewAccountAgeWitnessesToMap(Map<P2PDataStorage.ByteArray, PersistableNetworkPayload> liveDataMap, | ||
int numberOfWitnesses) { | ||
addNewAccountAgeWitnessesToMap(liveDataMap, numberOfWitnesses, 0); | ||
} | ||
|
||
private static void addNewAccountAgeWitnessesToMap(Map<P2PDataStorage.ByteArray, PersistableNetworkPayload> liveDataMap, | ||
int numberOfWitnesses, | ||
int startKeyOffset) { | ||
for (int i = 0; i < numberOfWitnesses; i++) { | ||
addNewAccountAgeWitnessToMap(liveDataMap, startKeyOffset + i); | ||
} | ||
} | ||
|
||
static void addNewAccountAgeWitnessToMap(Map<P2PDataStorage.ByteArray, PersistableNetworkPayload> liveDataMap) { | ||
addNewAccountAgeWitnessToMap(liveDataMap, liveDataMap.size()); | ||
} | ||
|
||
private static void addNewAccountAgeWitnessToMap(Map<P2PDataStorage.ByteArray, PersistableNetworkPayload> 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); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
core/src/test/java/bisq/core/account/witness/DummyHistoricalDataStoreService.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,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<AccountAgeWitnessStore> { | ||
|
||
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<String, PersistableNetworkPayloadStore<? extends PersistableNetworkPayload>> | ||
storesByVersion) { | ||
this.storesByVersion = ImmutableMap.copyOf(storesByVersion); | ||
} | ||
} | ||
|
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