Skip to content

Commit

Permalink
IdentityServiceTest: Flush data stores after each test
Browse files Browse the repository at this point in the history
  • Loading branch information
alvasw committed Nov 14, 2023
1 parent 72c87e1 commit ee69b1a
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 127 deletions.
8 changes: 4 additions & 4 deletions identity/src/main/java/bisq/identity/IdentityService.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public Identity getOrCreateDefaultIdentity() {
synchronized (lock) {
persistableStore.setDefaultIdentity(Optional.of(identity));
}
persist().join();
persist();
return identity;
});
}
Expand All @@ -128,7 +128,7 @@ public Identity createAndInitializeNewActiveIdentity(String tag) {
synchronized (lock) {
getActiveIdentityByTag().put(tag, identity);
}
persist().join();
persist();

return identity;
}
Expand All @@ -143,7 +143,7 @@ public boolean retireActiveIdentity(String tag) {
}
}
if (wasRemoved) {
persist().join();
persist();
}
return wasRemoved;
}
Expand Down Expand Up @@ -205,7 +205,7 @@ private Identity createAndInitializeNewActiveIdentity(String tag, String keyId,
synchronized (lock) {
getActiveIdentityByTag().put(tag, identity);
}
persist().join();
persist();

return identity;
}
Expand Down
156 changes: 35 additions & 121 deletions identity/src/test/java/bisq/identity/IdentityServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import bisq.persistence.PersistenceService;
import bisq.security.KeyPairService;
import bisq.security.PubKey;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

Expand All @@ -39,16 +41,29 @@
import static org.mockito.Mockito.when;

public class IdentityServiceTest {
@Test
void getOrCreateIdentityTest(@TempDir Path tempDir) {
var persistenceService = new PersistenceService(tempDir.toAbsolutePath().toString());
var keyPairService = new KeyPairService(persistenceService);

@TempDir
private Path tempDir;
private KeyPairService keyPairService;
private IdentityService identityService;

@BeforeEach
void setUp() {
PersistenceService persistenceService = new PersistenceService(tempDir.toAbsolutePath().toString());
NetworkService networkService = mock(NetworkService.class);
when(networkService.getSupportedTransportTypes()).thenReturn(Set.of(TransportType.TOR));

var identityService = new IdentityService(persistenceService, keyPairService, networkService);
keyPairService = new KeyPairService(persistenceService);
identityService = new IdentityService(persistenceService, keyPairService, networkService);
}

@AfterEach
void tearDown() {
keyPairService.getPersistence().flush().join();
identityService.getPersistence().flush().join();
}

@Test
void getOrCreateIdentityTest() {
String myTag = "myTag";
Identity activeIdentity = identityService.getOrCreateIdentity(myTag);
Identity persistedActiveIdentity = identityService.getOrCreateIdentity(myTag);
Expand All @@ -59,15 +74,7 @@ void getOrCreateIdentityTest(@TempDir Path tempDir) {
}

@Test
void getOrCreateIdentityWithAllArguments(@TempDir Path tempDir) {
var persistenceService = new PersistenceService(tempDir.toAbsolutePath().toString());
var keyPairService = new KeyPairService(persistenceService);

NetworkService networkService = mock(NetworkService.class);
when(networkService.getSupportedTransportTypes()).thenReturn(Set.of(TransportType.TOR));

var identityService = new IdentityService(persistenceService, keyPairService, networkService);

void getOrCreateIdentityWithAllArguments() {
String myTag = "myTag";
KeyPair keyPair = keyPairService.getOrCreateKeyPair(myTag);
Identity activeIdentity = identityService.getOrCreateIdentity(myTag, myTag, keyPair);
Expand All @@ -84,15 +91,7 @@ void getOrCreateIdentityWithAllArguments(@TempDir Path tempDir) {
}

@Test
void getOrCreateDefaultIdentityTest(@TempDir Path tempDir) {
var persistenceService = new PersistenceService(tempDir.toAbsolutePath().toString());
var keyPairService = new KeyPairService(persistenceService);

NetworkService networkService = mock(NetworkService.class);
when(networkService.getSupportedTransportTypes()).thenReturn(Set.of(TransportType.TOR));

var identityService = new IdentityService(persistenceService, keyPairService, networkService);

void getOrCreateDefaultIdentityTest() {
Identity firstDefaultIdentity = identityService.getOrCreateDefaultIdentity();
assertThat(firstDefaultIdentity.getTag())
.isEqualTo(IdentityService.DEFAULT_IDENTITY_TAG);
Expand All @@ -102,15 +101,7 @@ void getOrCreateDefaultIdentityTest(@TempDir Path tempDir) {
}

@Test
void createNewIdentity(@TempDir Path tempDir) {
var persistenceService = new PersistenceService(tempDir.toAbsolutePath().toString());
var keyPairService = new KeyPairService(persistenceService);

NetworkService networkService = mock(NetworkService.class);
when(networkService.getSupportedTransportTypes()).thenReturn(Set.of(TransportType.TOR));

var identityService = new IdentityService(persistenceService, keyPairService, networkService);

void createNewIdentity() {
String myTag = "myTag";
Identity activeIdentity = identityService.createAndInitializeNewActiveIdentity(myTag);
Identity anotherActiveIdentity = identityService.createAndInitializeNewActiveIdentity(myTag);
Expand All @@ -119,25 +110,13 @@ void createNewIdentity(@TempDir Path tempDir) {
}

@Test
void retireInvalidIdentity(@TempDir Path tempDir) {
var persistenceService = new PersistenceService(tempDir.toAbsolutePath().toString());
var keyPairService = new KeyPairService(persistenceService);
NetworkService networkService = mock(NetworkService.class);

var identityService = new IdentityService(persistenceService, keyPairService, networkService);
void retireInvalidIdentity() {
boolean isRemoved = identityService.retireActiveIdentity("tag");
assertThat(isRemoved).isFalse();
}

@Test
void retireActiveIdentity(@TempDir Path tempDir) {
var persistenceService = new PersistenceService(tempDir.toAbsolutePath().toString());
var keyPairService = new KeyPairService(persistenceService);

NetworkService networkService = mock(NetworkService.class);
when(networkService.getSupportedTransportTypes()).thenReturn(Set.of(TransportType.TOR));

var identityService = new IdentityService(persistenceService, keyPairService, networkService);
void retireActiveIdentity() {
String myTag = "myTag";
Identity identity = identityService.getOrCreateIdentity(myTag);

Expand All @@ -156,47 +135,26 @@ void retireActiveIdentity(@TempDir Path tempDir) {
}

@Test
void findInvalidIdentity(@TempDir Path tempDir) {
var persistenceService = new PersistenceService(tempDir.toAbsolutePath().toString());
var keyPairService = new KeyPairService(persistenceService);

NetworkService networkService = mock(NetworkService.class);
when(networkService.getSupportedTransportTypes()).thenReturn(Set.of(TransportType.TOR));

var identityService = new IdentityService(persistenceService, keyPairService, networkService);
void findInvalidIdentity() {
Optional<Identity> activeIdentity = identityService.findActiveIdentity("tag");
assertThat(activeIdentity).isEmpty();
}

@Test
void findInvalidIdentityByNetworkId(@TempDir Path tempDir) {
var persistenceService = new PersistenceService(tempDir.toAbsolutePath().toString());
var keyPairService = new KeyPairService(persistenceService);

NetworkService networkService = mock(NetworkService.class);
when(networkService.getSupportedTransportTypes()).thenReturn(Set.of(TransportType.TOR));

void findInvalidIdentityByNetworkId() {
AddressByTransportTypeMap addressByTransportTypeMap = new AddressByTransportTypeMap(
Map.of(TransportType.CLEAR, Address.localHost(1234)));

KeyPair keyPair = keyPairService.getOrCreateKeyPair("keyId");
var pubKey = new PubKey(keyPair.getPublic(), "keyId");
var networkId = new NetworkId(addressByTransportTypeMap, pubKey);

var identityService = new IdentityService(persistenceService, keyPairService, networkService);
Optional<Identity> activeIdentity = identityService.findActiveIdentityByNetworkId(networkId);
assertThat(activeIdentity).isEmpty();
}

@Test
void findActiveIdentity(@TempDir Path tempDir) {
var persistenceService = new PersistenceService(tempDir.toAbsolutePath().toString());
var keyPairService = new KeyPairService(persistenceService);

NetworkService networkService = mock(NetworkService.class);
when(networkService.getSupportedTransportTypes()).thenReturn(Set.of(TransportType.TOR));

var identityService = new IdentityService(persistenceService, keyPairService, networkService);
void findActiveIdentity() {
String myTag = "myTag";
Identity identity = identityService.getOrCreateIdentity(myTag);

Expand All @@ -205,14 +163,7 @@ void findActiveIdentity(@TempDir Path tempDir) {
}

@Test
void findActiveIdentityByNetworkId(@TempDir Path tempDir) {
var persistenceService = new PersistenceService(tempDir.toAbsolutePath().toString());
var keyPairService = new KeyPairService(persistenceService);

NetworkService networkService = mock(NetworkService.class);
when(networkService.getSupportedTransportTypes()).thenReturn(Set.of(TransportType.TOR));

var identityService = new IdentityService(persistenceService, keyPairService, networkService);
void findActiveIdentityByNetworkId() {
String myTag = "myTag";
Identity identity = identityService.getOrCreateIdentity(myTag);

Expand All @@ -221,15 +172,7 @@ void findActiveIdentityByNetworkId(@TempDir Path tempDir) {
}

@Test
void findInvalidRetiredIdentity(@TempDir Path tempDir) {
var persistenceService = new PersistenceService(tempDir.toAbsolutePath().toString());
var keyPairService = new KeyPairService(persistenceService);

NetworkService networkService = mock(NetworkService.class);
when(networkService.getSupportedTransportTypes()).thenReturn(Set.of(TransportType.TOR));

var identityService = new IdentityService(persistenceService, keyPairService, networkService);

void findInvalidRetiredIdentity() {
AddressByTransportTypeMap addressByTransportTypeMap = new AddressByTransportTypeMap(
Map.of(TransportType.CLEAR, Address.localHost(1234)));

Expand All @@ -242,14 +185,7 @@ void findInvalidRetiredIdentity(@TempDir Path tempDir) {
}

@Test
void findRetiredIdentity(@TempDir Path tempDir) {
var persistenceService = new PersistenceService(tempDir.toAbsolutePath().toString());
var keyPairService = new KeyPairService(persistenceService);

NetworkService networkService = mock(NetworkService.class);
when(networkService.getSupportedTransportTypes()).thenReturn(Set.of(TransportType.TOR));

var identityService = new IdentityService(persistenceService, keyPairService, networkService);
void findRetiredIdentity() {
Identity identity = identityService.getOrCreateIdentity("tag");
identityService.retireActiveIdentity("tag");

Expand All @@ -258,30 +194,15 @@ void findRetiredIdentity(@TempDir Path tempDir) {
}

@Test
void findDefaultIdentityInFindAnyIdentity(@TempDir Path tempDir) {
var persistenceService = new PersistenceService(tempDir.toAbsolutePath().toString());
var keyPairService = new KeyPairService(persistenceService);

NetworkService networkService = mock(NetworkService.class);
when(networkService.getSupportedTransportTypes()).thenReturn(Set.of(TransportType.TOR));

var identityService = new IdentityService(persistenceService, keyPairService, networkService);
void findDefaultIdentityInFindAnyIdentity() {
Identity defaultIdentity = identityService.getOrCreateDefaultIdentity();

Optional<Identity> identityByNetworkId = identityService
.findAnyIdentityByNetworkId(defaultIdentity.getNetworkId());
assertThat(identityByNetworkId).hasValue(defaultIdentity);
}

@Test
void findActiveIdentityInFindAnyIdentity(@TempDir Path tempDir) {
var persistenceService = new PersistenceService(tempDir.toAbsolutePath().toString());
var keyPairService = new KeyPairService(persistenceService);

NetworkService networkService = mock(NetworkService.class);
when(networkService.getSupportedTransportTypes()).thenReturn(Set.of(TransportType.TOR));

var identityService = new IdentityService(persistenceService, keyPairService, networkService);
void findActiveIdentityInFindAnyIdentity() {
String myTag = "myTag";
Identity identity = identityService.getOrCreateIdentity(myTag);

Expand All @@ -291,14 +212,7 @@ void findActiveIdentityInFindAnyIdentity(@TempDir Path tempDir) {
}

@Test
void findRetiredIdentityInFindAnyIdentity(@TempDir Path tempDir) {
var persistenceService = new PersistenceService(tempDir.toAbsolutePath().toString());
var keyPairService = new KeyPairService(persistenceService);

NetworkService networkService = mock(NetworkService.class);
when(networkService.getSupportedTransportTypes()).thenReturn(Set.of(TransportType.TOR));

var identityService = new IdentityService(persistenceService, keyPairService, networkService);
void findRetiredIdentityInFindAnyIdentity() {
Identity identity = identityService.getOrCreateIdentity("tag");
identityService.retireActiveIdentity("tag");

Expand Down
4 changes: 4 additions & 0 deletions persistence/src/main/java/bisq/persistence/Persistence.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ public CompletableFuture<Void> persistAsync(T serializable) {
}, executorService);
}

public CompletableFuture<Void> flush() {
return CompletableFuture.runAsync(() -> Thread.currentThread().setName("Flush-Persistence.persist-" + storePath), executorService);
}

protected void persist(T persistableStore) {
persistableStoreReaderWriter.write(persistableStore);
}
Expand Down
4 changes: 2 additions & 2 deletions security/src/main/java/bisq/security/KeyPairService.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public void persistKeyPair(String keyId, KeyPair keyPair) {
synchronized (persistableStore) {
persistableStore.put(keyId, keyPair);
}
persist().join();
persist();
}

public CompletableFuture<KeyPair> getOrCreateKeyPairAsync(String keyId) {
Expand All @@ -95,7 +95,7 @@ public CompletableFuture<KeyPair> getOrCreateKeyPairAsync(String keyId) {
synchronized (persistableStore) {
persistableStore.put(keyId, keyPair);
}
persist().join();
persist();
return keyPair;
} catch (GeneralSecurityException e) {
e.printStackTrace();
Expand Down

0 comments on commit ee69b1a

Please sign in to comment.