use of com.quorum.tessera.enclave.PayloadEncoder in project tessera by ConsenSys.
the class EncryptedTransactionDAOTest method findByHashes.
@Test
public void findByHashes() {
PayloadEncoder payloadEncoder = mock(PayloadEncoder.class);
final byte[] payloadData = "PAYLOADATA".getBytes();
EncodedPayload encodedPayload = mock(EncodedPayload.class);
when(payloadEncoder.encode(encodedPayload)).thenReturn(payloadData);
try (var createEncoderFunction = mockStatic(PayloadEncoder.class)) {
createEncoderFunction.when(() -> PayloadEncoder.create(EncodedPayloadCodec.current())).thenReturn(payloadEncoder);
EntityManager entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();
List<EncryptedTransaction> transactions = IntStream.range(0, 100).mapToObj(i -> UUID.randomUUID().toString().getBytes()).map(MessageHash::new).map(h -> {
EncryptedTransaction encryptedTransaction = new EncryptedTransaction();
encryptedTransaction.setHash(h);
encryptedTransaction.setPayload(encodedPayload);
entityManager.persist(encryptedTransaction);
return encryptedTransaction;
}).collect(Collectors.toList());
entityManager.getTransaction().commit();
Collection<MessageHash> hashes = transactions.stream().map(EncryptedTransaction::getHash).collect(Collectors.toList());
List<EncryptedTransaction> results = encryptedTransactionDAO.findByHashes(hashes);
assertThat(results).isNotEmpty().containsExactlyInAnyOrderElementsOf(transactions);
assertThat(results.stream().allMatch(r -> Arrays.equals(r.getEncodedPayload(), payloadData))).isTrue();
}
verify(payloadEncoder, times(100)).encode(encodedPayload);
}
use of com.quorum.tessera.enclave.PayloadEncoder in project tessera by ConsenSys.
the class EncryptedTransactionDAOTest method persistAddsTimestampToEntity.
@Test
public void persistAddsTimestampToEntity() {
PayloadEncoder payloadEncoder = mock(PayloadEncoder.class);
final byte[] payloadData = "PAYLOADATA".getBytes();
EncodedPayload encodedPayload = mock(EncodedPayload.class);
when(payloadEncoder.encode(encodedPayload)).thenReturn(payloadData);
when(payloadEncoder.decode(payloadData)).thenReturn(encodedPayload);
try (var createEncoderFunction = mockStatic(PayloadEncoder.class)) {
createEncoderFunction.when(() -> PayloadEncoder.create(EncodedPayloadCodec.current())).thenReturn(payloadEncoder);
MessageHash messageHash = new MessageHash(UUID.randomUUID().toString().getBytes());
final EncryptedTransaction encryptedTransaction = new EncryptedTransaction();
encryptedTransaction.setPayload(encodedPayload);
encryptedTransaction.setHash(messageHash);
encryptedTransactionDAO.save(encryptedTransaction);
EntityManager entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();
final EncryptedTransaction retrieved = entityManager.find(EncryptedTransaction.class, messageHash);
entityManager.getTransaction().commit();
assertThat(retrieved).isNotNull();
assertThat(retrieved.getTimestamp()).isNotZero().isGreaterThan(0L);
}
verify(payloadEncoder).decode(payloadData);
verify(payloadEncoder).encode(encodedPayload);
verifyNoMoreInteractions(payloadEncoder);
}
use of com.quorum.tessera.enclave.PayloadEncoder in project tessera by ConsenSys.
the class EncryptedTransactionDAOTest method validEncryptedTransactionCanBePersisted.
@Test
public void validEncryptedTransactionCanBePersisted() {
PayloadEncoder payloadEncoder = mock(PayloadEncoder.class);
final byte[] payloadData = "PAYLOADATA".getBytes();
EncodedPayload encodedPayload = mock(EncodedPayload.class);
when(payloadEncoder.encode(encodedPayload)).thenReturn(payloadData);
when(payloadEncoder.decode(payloadData)).thenReturn(encodedPayload);
try (var createEncoderFunction = mockStatic(PayloadEncoder.class)) {
createEncoderFunction.when(() -> PayloadEncoder.create(EncodedPayloadCodec.current())).thenReturn(payloadEncoder);
final EncryptedTransaction encryptedTransaction = new EncryptedTransaction();
encryptedTransaction.setPayload(encodedPayload);
encryptedTransaction.setHash(new MessageHash(new byte[] { 1 }));
encryptedTransactionDAO.save(encryptedTransaction);
EntityManager entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();
final EncryptedTransaction retrieved = entityManager.find(EncryptedTransaction.class, encryptedTransaction.getHash());
assertThat(retrieved).usingRecursiveComparison().isEqualTo(encryptedTransaction);
entityManager.getTransaction().rollback();
}
}
use of com.quorum.tessera.enclave.PayloadEncoder in project tessera by ConsenSys.
the class EncryptedTransactionDAOTest method updateTransaction.
@Test
public void updateTransaction() {
PayloadEncoder payloadEncoder = mock(PayloadEncoder.class);
final byte[] initialData = "DATA1".getBytes();
EncodedPayload encodedPayload = mock(EncodedPayload.class);
when(payloadEncoder.encode(encodedPayload)).thenReturn(initialData);
when(payloadEncoder.decode(initialData)).thenReturn(encodedPayload);
final byte[] updatedData = "DATA2".getBytes();
EncodedPayload updatedPayload = mock(EncodedPayload.class);
when(payloadEncoder.encode(updatedPayload)).thenReturn(updatedData);
when(payloadEncoder.decode(updatedData)).thenReturn(updatedPayload);
try (var createEncoderFunction = mockStatic(PayloadEncoder.class)) {
createEncoderFunction.when(() -> PayloadEncoder.create(EncodedPayloadCodec.current())).thenReturn(payloadEncoder);
final EncryptedTransaction encryptedTransaction = new EncryptedTransaction();
encryptedTransaction.setPayload(encodedPayload);
encryptedTransaction.setHash(new MessageHash(new byte[] { 1 }));
encryptedTransactionDAO.save(encryptedTransaction);
EntityManager entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();
final EncryptedTransaction retrieved = entityManager.find(EncryptedTransaction.class, encryptedTransaction.getHash());
assertThat(retrieved).isNotNull().usingRecursiveComparison().isEqualTo(encryptedTransaction);
encryptedTransaction.setPayload(updatedPayload);
encryptedTransaction.setEncodedPayload(new byte[] { 0 });
encryptedTransactionDAO.update(encryptedTransaction);
entityManager.getTransaction().rollback();
entityManager.getTransaction().begin();
final EncryptedTransaction after = entityManager.find(EncryptedTransaction.class, encryptedTransaction.getHash());
assertThat(after.getPayload()).isEqualTo(updatedPayload);
assertThat(after.getEncodedPayload()).isEqualTo(updatedData);
entityManager.getTransaction().rollback();
}
verify(payloadEncoder, times(2)).encode(any());
verify(payloadEncoder, times(3)).decode(any());
verifyNoMoreInteractions(payloadEncoder);
}
use of com.quorum.tessera.enclave.PayloadEncoder in project tessera by ConsenSys.
the class EncryptedTransactionDAOTest method deleteTransactionRemovesFromDatabaseAndReturnsTrue.
@Test
public void deleteTransactionRemovesFromDatabaseAndReturnsTrue() {
PayloadEncoder payloadEncoder = mock(PayloadEncoder.class);
final byte[] payloadData = "PAYLOADATA".getBytes();
EncodedPayload encodedPayload = mock(EncodedPayload.class);
when(payloadEncoder.encode(encodedPayload)).thenReturn(payloadData);
try (var createEncoderFunction = mockStatic(PayloadEncoder.class)) {
createEncoderFunction.when(() -> PayloadEncoder.create(EncodedPayloadCodec.current())).thenReturn(payloadEncoder);
final MessageHash messageHash = new MessageHash(UUID.randomUUID().toString().getBytes());
EntityManager entityManager = entityManagerFactory.createEntityManager();
// put a transaction in the database
final EncryptedTransaction encryptedTransaction = new EncryptedTransaction();
encryptedTransaction.setPayload(encodedPayload);
encryptedTransaction.setHash(messageHash);
entityManager.getTransaction().begin();
entityManager.persist(encryptedTransaction);
entityManager.getTransaction().commit();
Query countQuery = entityManager.createQuery("select count(t) from EncryptedTransaction t where t.hash = :hash");
Long result = (Long) countQuery.setParameter("hash", messageHash).getSingleResult();
assertThat(result).isEqualTo(1L);
encryptedTransactionDAO.delete(messageHash);
// check it is not longer in the database
Long result2 = (Long) countQuery.setParameter("hash", messageHash).getSingleResult();
assertThat(result2).isZero();
}
verify(payloadEncoder).decode(payloadData);
verify(payloadEncoder).encode(encodedPayload);
verifyNoMoreInteractions(payloadEncoder);
}
Aggregations