Search in sources :

Example 6 with PrivacyGroupEntity

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

the class PrivacyGroupDAOTest method savePrivacyGroupWithCallback.

@Test
public void savePrivacyGroupWithCallback() throws Exception {
    final PrivacyGroupEntity entity = new PrivacyGroupEntity("id".getBytes(), "lookup".getBytes(), "data".getBytes());
    Callable<Void> callback = mock(Callable.class);
    privacyGroupDAO.save(entity, callback);
    EntityManager entityManager = ENTITY_MANAGER.get();
    final PrivacyGroupEntity result = entityManager.find(PrivacyGroupEntity.class, "id".getBytes());
    assertThat(result).isNotNull();
    verify(callback).call();
}
Also used : EntityManager(jakarta.persistence.EntityManager) PrivacyGroupEntity(com.quorum.tessera.data.PrivacyGroupEntity)

Example 7 with PrivacyGroupEntity

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

the class PrivacyGroupDAOTest method saveDoesNotAllowNullId.

@Test
public void saveDoesNotAllowNullId() {
    PrivacyGroupEntity privacyGroup = new PrivacyGroupEntity();
    try {
        privacyGroupDAO.save(privacyGroup);
        failBecauseExceptionWasNotThrown(PersistenceException.class);
    } catch (PersistenceException ex) {
        String expectedMessage = String.format(testConfig.getRequiredFieldColumnTemplate(), "ID");
        assertThat(ex).isInstanceOf(PersistenceException.class).hasMessageContaining(expectedMessage).hasMessageContaining("ID");
    }
}
Also used : PrivacyGroupEntity(com.quorum.tessera.data.PrivacyGroupEntity) PersistenceException(jakarta.persistence.PersistenceException)

Example 8 with PrivacyGroupEntity

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

the class PrivacyGroupDAOTest method retrieveOrSaveThrows.

@Test(expected = IllegalStateException.class)
public void retrieveOrSaveThrows() {
    EntityManagerTemplate template = new EntityManagerTemplate(ENTITY_MANAGER.get().getEntityManagerFactory());
    Supplier<PrivacyGroupEntity> mockRetriever = mock(Supplier.class);
    when(mockRetriever.get()).thenReturn(null);
    Supplier<PrivacyGroupEntity> mockFactory = mock(Supplier.class);
    when(mockFactory.get()).thenThrow(new IllegalStateException("OUCH"));
    template.retrieveOrSave(mockRetriever, mockFactory);
}
Also used : PrivacyGroupEntity(com.quorum.tessera.data.PrivacyGroupEntity) EntityManagerTemplate(com.quorum.tessera.data.EntityManagerTemplate)

Example 9 with PrivacyGroupEntity

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

the class PrivacyGroupDAOTest method saveAndFindAll.

@Test
public void saveAndFindAll() {
    final List<PrivacyGroupEntity> shouldBeEmpty = privacyGroupDAO.findAll();
    assertThat(shouldBeEmpty).isEmpty();
    final PrivacyGroupEntity entity = new PrivacyGroupEntity("id1".getBytes(), "lookup".getBytes(), "data".getBytes());
    privacyGroupDAO.save(entity);
    final PrivacyGroupEntity another = new PrivacyGroupEntity("id2".getBytes(), "lookup".getBytes(), "data".getBytes());
    privacyGroupDAO.save(another);
    final List<PrivacyGroupEntity> pgs = privacyGroupDAO.findAll();
    assertThat(pgs).isNotEmpty();
    assertThat(pgs).containsExactlyInAnyOrder(entity, another);
}
Also used : PrivacyGroupEntity(com.quorum.tessera.data.PrivacyGroupEntity)

Example 10 with PrivacyGroupEntity

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

the class PrivacyGroupManagerImplTest method testCreateResidentGroup.

@Test
public void testCreateResidentGroup() {
    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.saveResidentGroup("name", "desc", List.of(localKey));
    // Verify entity being saved has the correct values
    ArgumentCaptor<PrivacyGroupEntity> argCaptor = ArgumentCaptor.forClass(PrivacyGroupEntity.class);
    verify(privacyGroupDAO).update(argCaptor.capture());
    PrivacyGroupEntity savedEntity = argCaptor.getValue();
    assertThat(savedEntity).isNotNull();
    assertThat(savedEntity.getId()).isEqualTo("name".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("name".getBytes());
    assertThat(privacyGroup.getName()).isEqualTo("name");
    assertThat(privacyGroup.getDescription()).isEqualTo("desc");
    assertThat(privacyGroup.getMembers()).containsExactly(localKey);
    assertThat(privacyGroup.getType()).isEqualTo(PrivacyGroup.Type.RESIDENT);
    assertThat(privacyGroup.getState()).isEqualTo(PrivacyGroup.State.ACTIVE);
}
Also used : PrivacyGroupEntity(com.quorum.tessera.data.PrivacyGroupEntity) PrivacyGroup(com.quorum.tessera.enclave.PrivacyGroup) Test(org.junit.Test)

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