use of com.quorum.tessera.data.PrivacyGroupEntity in project tessera by ConsenSys.
the class PrivacyGroupManagerImpl method saveResidentGroup.
@Override
public PrivacyGroup saveResidentGroup(String name, String description, List<PublicKey> members) {
final PrivacyGroup privacyGroup = PrivacyGroup.Builder.buildResidentGroup(name, description, members);
final byte[] lookupId = privacyGroupUtil.generateLookupId(members);
final byte[] encodedData = privacyGroupUtil.encode(privacyGroup);
privacyGroupDAO.update(new PrivacyGroupEntity(name.getBytes(), lookupId, encodedData));
return privacyGroup;
}
use of com.quorum.tessera.data.PrivacyGroupEntity in project tessera by ConsenSys.
the class PrivacyGroupManagerImpl method deletePrivacyGroup.
@Override
public PrivacyGroup deletePrivacyGroup(PublicKey from, PrivacyGroup.Id privacyGroupId) {
final PrivacyGroup retrieved = retrievePrivacyGroup(privacyGroupId);
if (!retrieved.getMembers().contains(from)) {
throw new PrivacyViolationException("Sender of request does not belong to this privacy group");
}
final PrivacyGroup updated = PrivacyGroup.Builder.create().from(retrieved).withState(PrivacyGroup.State.DELETED).build();
final byte[] updatedData = privacyGroupUtil.encode(updated);
final byte[] lookupId = privacyGroupUtil.generateLookupId(updated.getMembers());
final PrivacyGroupEntity updatedEt = new PrivacyGroupEntity(updated.getId().getBytes(), lookupId, updatedData);
final Set<PublicKey> localKeys = enclave.getPublicKeys();
final List<PublicKey> forwardingMembers = updated.getMembers().stream().filter(Predicate.not(localKeys::contains)).collect(Collectors.toList());
privacyGroupDAO.update(updatedEt, () -> {
publisher.publishPrivacyGroup(updatedData, forwardingMembers);
return null;
});
return updated;
}
use of com.quorum.tessera.data.PrivacyGroupEntity in project tessera by ConsenSys.
the class PrivacyGroupManagerImplTest method testCreatePrivacyGroup.
@Test
public void testCreatePrivacyGroup() {
when(privacyGroupUtil.generateId(anyList(), any(byte[].class))).thenReturn("generatedId".getBytes());
when(privacyGroupUtil.generateLookupId(anyList())).thenReturn("lookup".getBytes());
when(privacyGroupUtil.encode(any())).thenReturn("encoded".getBytes());
PublicKey recipient1 = mock(PublicKey.class);
PublicKey recipient2 = mock(PublicKey.class);
final List<PublicKey> members = List.of(localKey, recipient1, recipient2);
doAnswer(invocation -> {
Callable callable = invocation.getArgument(1);
callable.call();
return mock(PrivacyGroupEntity.class);
}).when(privacyGroupDAO).save(any(), any());
final PrivacyGroup privacyGroup = privacyGroupManager.createPrivacyGroup("name", "description", localKey, members, new byte[1]);
// Verify entity being saved has the correct values
ArgumentCaptor<PrivacyGroupEntity> argCaptor = ArgumentCaptor.forClass(PrivacyGroupEntity.class);
verify(privacyGroupDAO).save(argCaptor.capture(), any());
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 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("encoded".getBytes());
assertThat(recipientsCaptor.getValue()).containsExactlyInAnyOrder(recipient1, recipient2);
// Verify generated privacy group has the correct values
assertThat(privacyGroup).isNotNull();
assertThat(privacyGroup.getId().getBytes()).isEqualTo("generatedId".getBytes());
assertThat(privacyGroup.getName()).isEqualTo("name");
assertThat(privacyGroup.getDescription()).isEqualTo("description");
assertThat(privacyGroup.getMembers()).containsAll(members);
assertThat(privacyGroup.getType()).isEqualTo(PrivacyGroup.Type.PANTHEON);
assertThat(privacyGroup.getState()).isEqualTo(PrivacyGroup.State.ACTIVE);
}
use of com.quorum.tessera.data.PrivacyGroupEntity in project tessera by ConsenSys.
the class PrivacyGroupManagerImplTest method testDeletePrivacyGroupFromKeyNotBelong.
@Test
public void testDeletePrivacyGroupFromKeyNotBelong() {
PublicKey from = PublicKey.from("local".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);
assertThatThrownBy(() -> privacyGroupManager.deletePrivacyGroup(from, PrivacyGroup.Id.fromBytes("id".getBytes()))).isInstanceOf(PrivacyViolationException.class);
verify(privacyGroupDAO).retrieve("id".getBytes());
}
use of com.quorum.tessera.data.PrivacyGroupEntity in project tessera by ConsenSys.
the class PrivacyGroupManagerImplTest method testStorePrivacyGroup.
@Test
public void testStorePrivacyGroup() {
final PrivacyGroup mockPrivacyGroup = mock(PrivacyGroup.class);
when(mockPrivacyGroup.getId()).thenReturn(PrivacyGroup.Id.fromBytes("id".getBytes()));
final byte[] encoded = "encoded".getBytes();
when(privacyGroupUtil.decode(encoded)).thenReturn(mockPrivacyGroup);
when(privacyGroupUtil.generateLookupId(anyList())).thenReturn("lookup".getBytes());
when(privacyGroupDAO.retrieve("id".getBytes())).thenReturn(Optional.empty());
privacyGroupManager.storePrivacyGroup(encoded);
ArgumentCaptor<PrivacyGroupEntity> argCaptor = ArgumentCaptor.forClass(PrivacyGroupEntity.class);
verify(privacyGroupDAO).save(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());
}
Aggregations