use of com.quorum.tessera.enclave.EncodedPayload 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.EncodedPayload 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.EncodedPayload 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);
}
use of com.quorum.tessera.enclave.EncodedPayload in project tessera by ConsenSys.
the class StagingTransactionListener method onLoad.
@PostLoad
public void onLoad(StagingTransaction stagingTransaction) {
LOGGER.debug("onLoad[{}]", stagingTransaction);
final EncodedPayloadCodec encodedPayloadCodec = stagingTransaction.getEncodedPayloadCodec();
final byte[] encodedPayloadData = stagingTransaction.getPayload();
final PayloadEncoder payloadEncoder = PayloadEncoder.create(encodedPayloadCodec);
final EncodedPayload encodedPayload = payloadEncoder.decode(encodedPayloadData);
stagingTransaction.setEncodedPayload(encodedPayload);
}
use of com.quorum.tessera.enclave.EncodedPayload in project tessera by ConsenSys.
the class EncryptedTransactionListenerTest method onSave.
@Test
public void onSave() {
EncodedPayload encodedPayload = mock(EncodedPayload.class);
EncryptedTransaction encryptedTransaction = new EncryptedTransaction();
encryptedTransaction.setPayload(encodedPayload);
byte[] payloadData = "PayloadData".getBytes();
when(payloadEncoder.encode(encodedPayload)).thenReturn(payloadData);
encryptedTransactionListener.onSave(encryptedTransaction);
verify(payloadEncoder).encode(encodedPayload);
payloadEncoderFactoryFunction.verify(() -> PayloadEncoder.create(EncodedPayloadCodec.current()));
assertThat(encryptedTransaction.getEncodedPayload()).isEqualTo(payloadData);
}
Aggregations