Search in sources :

Example 21 with PrivacyGroupEntity

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

the class PrivacyGroupManagerImplTest method findPrivacyGroupByType.

@Test
public void findPrivacyGroupByType() {
    final PrivacyGroupEntity mockResult1 = mock(PrivacyGroupEntity.class);
    final PrivacyGroup mockPrivacyGroup1 = mock(PrivacyGroup.class);
    when(mockPrivacyGroup1.getState()).thenReturn(PrivacyGroup.State.ACTIVE);
    when(mockPrivacyGroup1.getType()).thenReturn(PrivacyGroup.Type.RESIDENT);
    when(mockResult1.getData()).thenReturn("data1".getBytes());
    final PrivacyGroupEntity mockResult2 = mock(PrivacyGroupEntity.class);
    final PrivacyGroup mockPrivacyGroup2 = mock(PrivacyGroup.class);
    when(mockPrivacyGroup2.getState()).thenReturn(PrivacyGroup.State.DELETED);
    when(mockPrivacyGroup2.getType()).thenReturn(PrivacyGroup.Type.RESIDENT);
    when(mockResult2.getData()).thenReturn("data2".getBytes());
    final PrivacyGroupEntity mockResult3 = mock(PrivacyGroupEntity.class);
    final PrivacyGroup mockPrivacyGroup3 = mock(PrivacyGroup.class);
    when(mockPrivacyGroup3.getState()).thenReturn(PrivacyGroup.State.ACTIVE);
    when(mockPrivacyGroup3.getType()).thenReturn(PrivacyGroup.Type.PANTHEON);
    when(mockResult3.getData()).thenReturn("data3".getBytes());
    when(privacyGroupUtil.decode("data1".getBytes())).thenReturn(mockPrivacyGroup1);
    when(privacyGroupUtil.decode("data2".getBytes())).thenReturn(mockPrivacyGroup2);
    when(privacyGroupUtil.decode("data3".getBytes())).thenReturn(mockPrivacyGroup3);
    when(privacyGroupDAO.findAll()).thenReturn(List.of(mockResult1, mockResult2, mockResult3));
    final List<PrivacyGroup> result = privacyGroupManager.findPrivacyGroupByType(PrivacyGroup.Type.RESIDENT);
    assertThat(result).isNotNull();
    assertThat(result).containsExactly(mockPrivacyGroup1);
    verify(privacyGroupDAO).findAll();
}
Also used : PrivacyGroupEntity(com.quorum.tessera.data.PrivacyGroupEntity) PrivacyGroup(com.quorum.tessera.enclave.PrivacyGroup) Test(org.junit.Test)

Example 22 with PrivacyGroupEntity

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

the class PrivacyGroupManagerImplTest method testDeletePrivacyGroup.

@Test
public void testDeletePrivacyGroup() {
    PublicKey from = PublicKey.from("r1".getBytes());
    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(List.of(PublicKey.from("r1".getBytes()), PublicKey.from("r2".getBytes())));
    when(mockPG.getState()).thenReturn(PrivacyGroup.State.ACTIVE);
    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());
    when(privacyGroupUtil.generateLookupId(any())).thenReturn("lookup".getBytes());
    doAnswer(invocation -> {
        Callable callable = invocation.getArgument(1);
        callable.call();
        return mock(PrivacyGroupEntity.class);
    }).when(privacyGroupDAO).update(any(), any());
    PrivacyGroup result = privacyGroupManager.deletePrivacyGroup(from, PrivacyGroup.Id.fromBytes("id".getBytes()));
    assertThat(result.getState()).isEqualTo(PrivacyGroup.State.DELETED);
    verify(privacyGroupDAO).retrieve("id".getBytes());
    verify(privacyGroupDAO).update(any(), any());
    // Verify payload being distributed has the correct values
    ArgumentCaptor<byte[]> payloadCaptor = ArgumentCaptor.forClass(byte[].class);
    ArgumentCaptor<List<PublicKey>> recipientsCaptor = ArgumentCaptor.forClass(List.class);
    verify(publisher).publishPrivacyGroup(payloadCaptor.capture(), recipientsCaptor.capture());
    assertThat(payloadCaptor.getValue()).isEqualTo("deletedData".getBytes());
    assertThat(recipientsCaptor.getValue()).containsAll(List.of(PublicKey.from("r1".getBytes()), PublicKey.from("r2".getBytes())));
    ArgumentCaptor<PrivacyGroup> argCaptor = ArgumentCaptor.forClass(PrivacyGroup.class);
    verify(privacyGroupUtil).encode(argCaptor.capture());
    PrivacyGroup deletedPg = argCaptor.getValue();
    assertThat(deletedPg.getId()).isEqualTo(PrivacyGroup.Id.fromBytes("id".getBytes()));
    assertThat(deletedPg.getState()).isEqualTo(PrivacyGroup.State.DELETED);
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) PrivacyGroupEntity(com.quorum.tessera.data.PrivacyGroupEntity) List(java.util.List) PrivacyGroup(com.quorum.tessera.enclave.PrivacyGroup) Callable(java.util.concurrent.Callable) Test(org.junit.Test)

Example 23 with PrivacyGroupEntity

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

the class PrivacyGroupManagerImplTest method testRetrievePrivacyGroup.

@Test
public void testRetrievePrivacyGroup() {
    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.ACTIVE);
    when(mockResult.getData()).thenReturn("data".getBytes());
    when(privacyGroupUtil.decode("data".getBytes())).thenReturn(mockPrivacyGroup);
    when(privacyGroupDAO.retrieve("id".getBytes())).thenReturn(Optional.of(mockResult));
    final PrivacyGroup result = privacyGroupManager.retrievePrivacyGroup(id);
    assertThat(result).isNotNull();
    assertThat(result).isEqualTo(mockPrivacyGroup);
    verify(privacyGroupDAO).retrieve("id".getBytes());
}
Also used : PrivacyGroupEntity(com.quorum.tessera.data.PrivacyGroupEntity) PrivacyGroup(com.quorum.tessera.enclave.PrivacyGroup) Test(org.junit.Test)

Example 24 with PrivacyGroupEntity

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

the class PrivacyGroupManagerImplTest method testStoreUpdatedPrivacyGroup.

@Test
public void testStoreUpdatedPrivacyGroup() {
    final PrivacyGroup mockPrivacyGroup = mock(PrivacyGroup.class);
    when(mockPrivacyGroup.getId()).thenReturn(PrivacyGroup.Id.fromBytes("id".getBytes()));
    when(mockPrivacyGroup.getState()).thenReturn(PrivacyGroup.State.DELETED);
    final byte[] encoded = "encoded".getBytes();
    when(privacyGroupUtil.decode(encoded)).thenReturn(mockPrivacyGroup);
    when(privacyGroupUtil.generateLookupId(anyList())).thenReturn("lookup".getBytes());
    PrivacyGroupEntity existing = new PrivacyGroupEntity("id".getBytes(), "lookup".getBytes(), "old".getBytes());
    when(privacyGroupDAO.retrieve("id".getBytes())).thenReturn(Optional.of(existing));
    privacyGroupManager.storePrivacyGroup(encoded);
    ArgumentCaptor<PrivacyGroupEntity> argCaptor = ArgumentCaptor.forClass(PrivacyGroupEntity.class);
    verify(privacyGroupDAO).retrieve("id".getBytes());
    verify(privacyGroupDAO).update(argCaptor.capture());
    final PrivacyGroupEntity saved = argCaptor.getValue();
    assertThat(saved).isNotNull();
    assertThat(saved.getId()).isEqualTo("id".getBytes());
    assertThat(saved.getLookupId()).isEqualTo("lookup".getBytes());
    assertThat(saved.getData()).isEqualTo("encoded".getBytes());
}
Also used : PrivacyGroupEntity(com.quorum.tessera.data.PrivacyGroupEntity) PrivacyGroup(com.quorum.tessera.enclave.PrivacyGroup) Test(org.junit.Test)

Example 25 with PrivacyGroupEntity

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

the class PrivacyGroupManagerImpl method storePrivacyGroup.

@Override
public void storePrivacyGroup(byte[] encodedData) {
    final PrivacyGroup privacyGroup = privacyGroupUtil.decode(encodedData);
    if (privacyGroup.getState() == PrivacyGroup.State.DELETED) {
        privacyGroupDAO.retrieve(privacyGroup.getId().getBytes()).ifPresent(et -> {
            et.setData(encodedData);
            privacyGroupDAO.update(et);
        });
        return;
    }
    final byte[] id = privacyGroup.getId().getBytes();
    final byte[] lookupId = privacyGroupUtil.generateLookupId(privacyGroup.getMembers());
    final PrivacyGroupEntity newEntity = new PrivacyGroupEntity(id, lookupId, encodedData);
    privacyGroupDAO.save(newEntity);
}
Also used : 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