Search in sources :

Example 11 with PrivacyGroupEntity

use of com.quorum.tessera.data.PrivacyGroupEntity in project tessera by ConsenSys.

the class PrivacyGroupManagerImplTest method testDeleteDeletedPrivacyGroup.

@Test
public void testDeleteDeletedPrivacyGroup() {
    PrivacyGroupEntity retrievedEt = mock(PrivacyGroupEntity.class);
    when(retrievedEt.getData()).thenReturn("data".getBytes());
    PrivacyGroup mockPG = mock(PrivacyGroup.class);
    when(mockPG.getId()).thenReturn(PrivacyGroup.Id.fromBytes("id".getBytes()));
    when(mockPG.getMembers()).thenReturn(Collections.emptyList());
    when(mockPG.getState()).thenReturn(PrivacyGroup.State.DELETED);
    when(mockPG.getType()).thenReturn(PrivacyGroup.Type.PANTHEON);
    when(privacyGroupDAO.retrieve("id".getBytes())).thenReturn(Optional.of(retrievedEt));
    when(privacyGroupUtil.decode("data".getBytes())).thenReturn(mockPG);
    when(privacyGroupUtil.encode(any())).thenReturn("deletedData".getBytes());
    assertThatThrownBy(() -> privacyGroupManager.deletePrivacyGroup(mock(PublicKey.class), PrivacyGroup.Id.fromBytes("id".getBytes()))).isInstanceOf(PrivacyGroupNotFoundException.class);
    verify(privacyGroupDAO).retrieve("id".getBytes());
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) PrivacyGroupEntity(com.quorum.tessera.data.PrivacyGroupEntity) PrivacyGroup(com.quorum.tessera.enclave.PrivacyGroup) Test(org.junit.Test)

Example 12 with PrivacyGroupEntity

use of com.quorum.tessera.data.PrivacyGroupEntity in project tessera by ConsenSys.

the class PrivacyGroupManagerImplTest method testRetrievePrivacyGroupDeleted.

@Test
public void testRetrievePrivacyGroupDeleted() {
    final PrivacyGroup.Id id = PrivacyGroup.Id.fromBytes("id".getBytes());
    final PrivacyGroupEntity mockResult = mock(PrivacyGroupEntity.class);
    final PrivacyGroup mockPrivacyGroup = mock(PrivacyGroup.class);
    when(mockPrivacyGroup.getState()).thenReturn(PrivacyGroup.State.DELETED);
    when(mockResult.getData()).thenReturn("data".getBytes());
    when(privacyGroupUtil.decode("data".getBytes())).thenReturn(mockPrivacyGroup);
    when(privacyGroupDAO.retrieve("id".getBytes())).thenReturn(Optional.of(mockResult));
    try {
        privacyGroupManager.retrievePrivacyGroup(id);
        failBecauseExceptionWasNotThrown(any());
    } catch (Exception ex) {
        assertThat(ex).isInstanceOf(PrivacyGroupNotFoundException.class);
    }
    verify(privacyGroupDAO).retrieve("id".getBytes());
}
Also used : PrivacyGroupNotFoundException(com.quorum.tessera.privacygroup.exception.PrivacyGroupNotFoundException) PrivacyGroupEntity(com.quorum.tessera.data.PrivacyGroupEntity) PrivacyGroup(com.quorum.tessera.enclave.PrivacyGroup) PrivacyViolationException(com.quorum.tessera.transaction.exception.PrivacyViolationException) PrivacyGroupNotFoundException(com.quorum.tessera.privacygroup.exception.PrivacyGroupNotFoundException) Test(org.junit.Test)

Example 13 with PrivacyGroupEntity

use of com.quorum.tessera.data.PrivacyGroupEntity in project tessera by ConsenSys.

the class PrivacyGroupManagerImplTest method testFindPrivacyGroup.

@Test
public void testFindPrivacyGroup() {
    final PrivacyGroupEntity et1 = mock(PrivacyGroupEntity.class);
    when(et1.getData()).thenReturn("data1".getBytes());
    final PrivacyGroupEntity et2 = mock(PrivacyGroupEntity.class);
    when(et2.getData()).thenReturn("data2".getBytes());
    final PrivacyGroupEntity et3 = mock(PrivacyGroupEntity.class);
    when(et3.getData()).thenReturn("data3".getBytes());
    final List<PrivacyGroupEntity> dbResult = List.of(et1, et2, et3);
    final PrivacyGroup pg1 = mock(PrivacyGroup.class);
    final PrivacyGroup pg2 = mock(PrivacyGroup.class);
    final PrivacyGroup pg3 = mock(PrivacyGroup.class);
    when(pg1.getState()).thenReturn(PrivacyGroup.State.DELETED);
    when(pg2.getState()).thenReturn(PrivacyGroup.State.ACTIVE);
    when(pg3.getState()).thenReturn(PrivacyGroup.State.ACTIVE);
    when(privacyGroupDAO.findByLookupId("lookup".getBytes())).thenReturn(dbResult);
    when(privacyGroupUtil.generateLookupId(anyList())).thenReturn("lookup".getBytes());
    when(privacyGroupUtil.decode("data1".getBytes())).thenReturn(pg1);
    when(privacyGroupUtil.decode("data2".getBytes())).thenReturn(pg2);
    when(privacyGroupUtil.decode("data3".getBytes())).thenReturn(pg3);
    final List<PrivacyGroup> privacyGroups = privacyGroupManager.findPrivacyGroup(List.of());
    assertThat(privacyGroups).isNotEmpty();
    assertThat(privacyGroups).contains(pg2, pg3);
    verify(privacyGroupDAO).findByLookupId("lookup".getBytes());
}
Also used : PrivacyGroupEntity(com.quorum.tessera.data.PrivacyGroupEntity) PrivacyGroup(com.quorum.tessera.enclave.PrivacyGroup) Test(org.junit.Test)

Example 14 with PrivacyGroupEntity

use of com.quorum.tessera.data.PrivacyGroupEntity in project tessera by ConsenSys.

the class PrivacyGroupManagerImplTest method testCreateLegacyPrivacyGroup.

@Test
public void testCreateLegacyPrivacyGroup() {
    final List<PublicKey> members = List.of(mock(PublicKey.class), mock(PublicKey.class));
    when(privacyGroupUtil.generateId(anyList())).thenReturn("generatedId".getBytes());
    when(privacyGroupUtil.generateLookupId(anyList())).thenReturn("lookup".getBytes());
    when(privacyGroupUtil.encode(any())).thenReturn("encoded".getBytes());
    when(privacyGroupDAO.retrieve("generatedId".getBytes())).thenReturn(Optional.empty());
    final PrivacyGroup privacyGroup = privacyGroupManager.createLegacyPrivacyGroup(localKey, members);
    // Verify entity being saved has the correct values
    ArgumentCaptor<PrivacyGroupEntity> argCaptor = ArgumentCaptor.forClass(PrivacyGroupEntity.class);
    verify(privacyGroupDAO).retrieveOrSave(argCaptor.capture());
    PrivacyGroupEntity savedEntity = argCaptor.getValue();
    assertThat(savedEntity).isNotNull();
    assertThat(savedEntity.getId()).isEqualTo("generatedId".getBytes());
    assertThat(savedEntity.getLookupId()).isEqualTo("lookup".getBytes());
    assertThat(savedEntity.getData()).isEqualTo("encoded".getBytes());
    // Verify generated privacy group has the correct values
    assertThat(privacyGroup).isNotNull();
    assertThat(privacyGroup.getId().getBytes()).isEqualTo("generatedId".getBytes());
    assertThat(privacyGroup.getName()).isEqualTo("legacy");
    assertThat(privacyGroup.getDescription()).isEqualTo("Privacy groups to support the creation of groups by privateFor and privateFrom");
    assertThat(privacyGroup.getMembers()).containsAll(members).contains(localKey);
    assertThat(privacyGroup.getType()).isEqualTo(PrivacyGroup.Type.LEGACY);
    assertThat(privacyGroup.getState()).isEqualTo(PrivacyGroup.State.ACTIVE);
    verify(privacyGroupDAO).retrieveOrSave(any());
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) PrivacyGroupEntity(com.quorum.tessera.data.PrivacyGroupEntity) PrivacyGroup(com.quorum.tessera.enclave.PrivacyGroup) Test(org.junit.Test)

Example 15 with PrivacyGroupEntity

use of com.quorum.tessera.data.PrivacyGroupEntity in project tessera by ConsenSys.

the class PrivacyGroupManagerImpl method createLegacyPrivacyGroup.

@Override
public PrivacyGroup createLegacyPrivacyGroup(PublicKey from, List<PublicKey> recipients) {
    final List<PublicKey> members = new ArrayList<>();
    members.add(from);
    members.addAll(recipients);
    final String name = "legacy";
    final String description = "Privacy groups to support the creation of groups by privateFor and privateFrom";
    final byte[] groupIdBytes = privacyGroupUtil.generateId(members);
    final PrivacyGroup created = PrivacyGroup.Builder.create().withPrivacyGroupId(groupIdBytes).withName(name).withDescription(description).withMembers(members).withType(PrivacyGroup.Type.LEGACY).withState(PrivacyGroup.State.ACTIVE).build();
    final byte[] lookupId = privacyGroupUtil.generateLookupId(members);
    final byte[] encodedData = privacyGroupUtil.encode(created);
    privacyGroupDAO.retrieveOrSave(new PrivacyGroupEntity(groupIdBytes, lookupId, encodedData));
    return created;
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) ArrayList(java.util.ArrayList) PrivacyGroupEntity(com.quorum.tessera.data.PrivacyGroupEntity) PrivacyGroup(com.quorum.tessera.enclave.PrivacyGroup)

Aggregations

PrivacyGroupEntity (com.quorum.tessera.data.PrivacyGroupEntity)35 PrivacyGroup (com.quorum.tessera.enclave.PrivacyGroup)17 Test (org.junit.Test)12 PublicKey (com.quorum.tessera.encryption.PublicKey)8 EntityManager (jakarta.persistence.EntityManager)7 PersistenceException (jakarta.persistence.PersistenceException)6 PrivacyViolationException (com.quorum.tessera.transaction.exception.PrivacyViolationException)3 Callable (java.util.concurrent.Callable)3 EntityManagerTemplate (com.quorum.tessera.data.EntityManagerTemplate)2 List (java.util.List)2 PrivacyGroupDAO (com.quorum.tessera.data.PrivacyGroupDAO)1 TestConfig (com.quorum.tessera.data.TestConfig)1 PrivacyGroupNotFoundException (com.quorum.tessera.privacygroup.exception.PrivacyGroupNotFoundException)1 EntityManagerFactory (jakarta.persistence.EntityManagerFactory)1 Persistence (jakarta.persistence.Persistence)1 java.util (java.util)1 ArrayList (java.util.ArrayList)1 ExecutorService (java.util.concurrent.ExecutorService)1 Executors (java.util.concurrent.Executors)1 Supplier (java.util.function.Supplier)1