Search in sources :

Example 26 with PrivacyGroupEntity

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

the class PrivacyGroupManagerImpl method createPrivacyGroup.

@Override
public PrivacyGroup createPrivacyGroup(String name, String description, PublicKey from, List<PublicKey> members, byte[] seed) {
    final Set<PublicKey> localKeys = enclave.getPublicKeys();
    if (!members.contains(from)) {
        throw new PrivacyViolationException("The list of members in a privacy group should include self");
    }
    final byte[] groupIdBytes = privacyGroupUtil.generateId(members, seed);
    final PrivacyGroup created = PrivacyGroup.Builder.create().withPrivacyGroupId(groupIdBytes).withName(name).withDescription(description).withMembers(members).withSeed(seed).withType(PrivacyGroup.Type.PANTHEON).withState(PrivacyGroup.State.ACTIVE).build();
    final byte[] lookupId = privacyGroupUtil.generateLookupId(members);
    final byte[] encodedData = privacyGroupUtil.encode(created);
    final List<PublicKey> forwardingMembers = members.stream().filter(Predicate.not(localKeys::contains)).collect(Collectors.toList());
    privacyGroupDAO.save(new PrivacyGroupEntity(groupIdBytes, lookupId, encodedData), () -> {
        publisher.publishPrivacyGroup(encodedData, forwardingMembers);
        return null;
    });
    return created;
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) PrivacyViolationException(com.quorum.tessera.transaction.exception.PrivacyViolationException) PrivacyGroupEntity(com.quorum.tessera.data.PrivacyGroupEntity) PrivacyGroup(com.quorum.tessera.enclave.PrivacyGroup)

Example 27 with PrivacyGroupEntity

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

the class PrivacyGroupDAOTest method saveAndFindByLookupId.

@Test
public void saveAndFindByLookupId() {
    final List<PrivacyGroupEntity> shouldBeEmpty = privacyGroupDAO.findByLookupId("lookup".getBytes());
    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.findByLookupId("lookup".getBytes());
    assertThat(pgs).isNotEmpty();
    assertThat(pgs).containsExactlyInAnyOrder(entity, another);
}
Also used : PrivacyGroupEntity(com.quorum.tessera.data.PrivacyGroupEntity)

Example 28 with PrivacyGroupEntity

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

the class PrivacyGroupDAOTest method updatePrivacyGroupWithCallbackException.

@Test
public void updatePrivacyGroupWithCallbackException() throws Exception {
    final PrivacyGroupEntity entity = new PrivacyGroupEntity("id".getBytes(), "lookup".getBytes(), "data".getBytes());
    Callable<Void> callback = mock(Callable.class);
    when(callback.call()).thenThrow(new Exception("OUCH"));
    try {
        privacyGroupDAO.update(entity, callback);
        failBecauseExceptionWasNotThrown(PersistenceException.class);
    } catch (PersistenceException ex) {
        assertThat(ex).isNotNull().hasMessageContaining("OUCH");
    }
    EntityManager entityManager = ENTITY_MANAGER.get();
    final PrivacyGroupEntity result = entityManager.find(PrivacyGroupEntity.class, "id".getBytes());
    assertThat(result).isNull();
    verify(callback).call();
}
Also used : EntityManager(jakarta.persistence.EntityManager) PrivacyGroupEntity(com.quorum.tessera.data.PrivacyGroupEntity) PersistenceException(jakarta.persistence.PersistenceException) PersistenceException(jakarta.persistence.PersistenceException)

Example 29 with PrivacyGroupEntity

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

the class PrivacyGroupDAOTest method retrieveOrSave.

@Test
public void retrieveOrSave() {
    final List<PrivacyGroupEntity> shouldBeEmpty = privacyGroupDAO.findByLookupId("lookup".getBytes());
    assertThat(shouldBeEmpty).isEmpty();
    final PrivacyGroupEntity entity = new PrivacyGroupEntity("id".getBytes(), "lookup".getBytes(), "data".getBytes());
    privacyGroupDAO.retrieveOrSave(entity);
    Optional<PrivacyGroupEntity> retrieved = privacyGroupDAO.retrieve("id".getBytes());
    assertThat(retrieved).isPresent();
}
Also used : PrivacyGroupEntity(com.quorum.tessera.data.PrivacyGroupEntity)

Example 30 with PrivacyGroupEntity

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

the class PrivacyGroupDAOTest method savePrivacyGroupWithRuntimeException.

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

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