use of com.quorum.tessera.enclave.PayloadEncoder in project tessera by ConsenSys.
the class EncryptedTransactionDAOTest method saveDoesntAllowNullHash.
@Test
public void saveDoesntAllowNullHash() {
PayloadEncoder payloadEncoder = mock(PayloadEncoder.class);
EncodedPayload encodedPayload = mock(EncodedPayload.class);
byte[] payloadData = "payloadData".getBytes();
when(payloadEncoder.encode(encodedPayload)).thenReturn(payloadData);
try (var createEncoderFunction = mockStatic(PayloadEncoder.class)) {
createEncoderFunction.when(() -> PayloadEncoder.create(EncodedPayloadCodec.current())).thenReturn(payloadEncoder);
EncryptedTransaction encryptedTransaction = new EncryptedTransaction();
encryptedTransaction.setPayload(encodedPayload);
try {
encryptedTransactionDAO.save(encryptedTransaction);
failBecauseExceptionWasNotThrown(PersistenceException.class);
} catch (PersistenceException ex) {
String expectedMessage = String.format(testConfig.getRequiredFieldColumnTemplate(), "HASH");
assertThat(ex).isInstanceOf(PersistenceException.class).hasMessageContaining(expectedMessage).hasMessageContaining("HASH");
}
}
}
use of com.quorum.tessera.enclave.PayloadEncoder in project tessera by ConsenSys.
the class EncryptedTransactionDAOTest method fetchingAllTransactionsReturnsAll.
@Test
public void fetchingAllTransactionsReturnsAll() {
PayloadEncoder payloadEncoder = mock(PayloadEncoder.class);
final byte[] payloadData = "I Love Sparrows".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);
EntityManager entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();
final List<EncryptedTransaction> payloads = IntStream.range(0, 50).mapToObj(i -> UUID.randomUUID().toString().getBytes()).map(MessageHash::new).map(hash -> {
EncryptedTransaction encryptedTransaction = new EncryptedTransaction();
encryptedTransaction.setHash(hash);
encryptedTransaction.setPayload(encodedPayload);
return encryptedTransaction;
}).peek(entityManager::persist).collect(Collectors.toList());
entityManager.getTransaction().commit();
final List<EncryptedTransaction> retrievedList = encryptedTransactionDAO.retrieveTransactions(0, Integer.MAX_VALUE);
assertThat(encryptedTransactionDAO.transactionCount()).isEqualTo(payloads.size());
assertThat(retrievedList).hasSameSizeAs(payloads);
assertThat(retrievedList).hasSameElementsAs(payloads);
}
verify(payloadEncoder, times(50)).decode(payloadData);
verify(payloadEncoder, times(50)).encode(encodedPayload);
verifyNoMoreInteractions(payloadEncoder);
}
use of com.quorum.tessera.enclave.PayloadEncoder in project tessera by ConsenSys.
the class EncryptedTransactionDAOTest method callBackShouldNotBeExecutedIfSaveFails.
@Test
public void callBackShouldNotBeExecutedIfSaveFails() {
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(new byte[] { 1 });
final EncryptedTransaction encryptedTransaction = new EncryptedTransaction();
encryptedTransaction.setPayload(encodedPayload);
encryptedTransaction.setHash(messageHash);
EntityManager entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();
entityManager.persist(encryptedTransaction);
entityManager.getTransaction().commit();
entityManager.clear();
assertThat(encryptedTransaction.getEncodedPayloadCodec()).isEqualTo(EncodedPayloadCodec.current());
assertThat(encryptedTransaction.getEncodedPayload()).containsExactly(payloadData);
final EncryptedTransaction duplicateTransaction = new EncryptedTransaction();
duplicateTransaction.setPayload(encodedPayload);
duplicateTransaction.setHash(messageHash);
AtomicInteger count = new AtomicInteger(0);
try {
encryptedTransactionDAO.save(encryptedTransaction, () -> {
count.incrementAndGet();
return true;
});
failBecauseExceptionWasNotThrown(PersistenceException.class);
} catch (PersistenceException ex) {
assertThat(ex).isInstanceOf(PersistenceException.class).hasMessageContaining(testConfig.getUniqueConstraintViolationMessage());
assertThat(count.get()).isEqualTo(0);
}
}
verify(payloadEncoder, times(2)).encode(encodedPayload);
}
use of com.quorum.tessera.enclave.PayloadEncoder in project tessera by ConsenSys.
the class EncryptedTransactionDAOTest method saveTransactionWithCallback.
@Test
public void saveTransactionWithCallback() throws Exception {
PayloadEncoder payloadEncoder = mock(PayloadEncoder.class);
final byte[] payloadData = UUID.randomUUID().toString().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 transactionHash = new MessageHash(UUID.randomUUID().toString().getBytes());
EncryptedTransaction transaction = new EncryptedTransaction();
transaction.setHash(transactionHash);
transaction.setPayload(encodedPayload);
Callable<Void> callback = mock(Callable.class);
encryptedTransactionDAO.save(transaction, callback);
EntityManager entityManager = entityManagerFactory.createEntityManager();
final EncryptedTransaction result = entityManager.find(EncryptedTransaction.class, transactionHash);
assertThat(result).isNotNull();
assertThat(result.getEncodedPayload()).containsExactly(payloadData);
assertThat(result.getHash()).isEqualTo(transactionHash);
verify(callback).call();
}
verify(payloadEncoder).encode(encodedPayload);
verify(payloadEncoder).decode(payloadData);
}
use of com.quorum.tessera.enclave.PayloadEncoder in project tessera by ConsenSys.
the class EncryptedTransactionDAOTest method retrieveByHashFindsTransactionThatIsPresent.
@Test
public void retrieveByHashFindsTransactionThatIsPresent() {
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);
// put a transaction in the database
MessageHash messageHash = new MessageHash(UUID.randomUUID().toString().getBytes());
final EncryptedTransaction encryptedTransaction = new EncryptedTransaction();
encryptedTransaction.setHash(messageHash);
encryptedTransaction.setPayload(encodedPayload);
EntityManager entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();
entityManager.persist(encryptedTransaction);
entityManager.getTransaction().commit();
final Optional<EncryptedTransaction> retrieved = encryptedTransactionDAO.retrieveByHash(messageHash);
assertThat(retrieved.isPresent()).isTrue();
assertThat(retrieved.get()).usingRecursiveComparison().isEqualTo(encryptedTransaction);
}
verify(payloadEncoder).encode(encodedPayload);
verify(payloadEncoder).decode(payloadData);
verifyNoMoreInteractions(payloadEncoder);
}
Aggregations