Search in sources :

Example 1 with PayloadEncoder

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");
        }
    }
}
Also used : PayloadEncoder(com.quorum.tessera.enclave.PayloadEncoder) EncodedPayload(com.quorum.tessera.enclave.EncodedPayload)

Example 2 with PayloadEncoder

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);
}
Also used : PayloadEncoder(com.quorum.tessera.enclave.PayloadEncoder) EncodedPayload(com.quorum.tessera.enclave.EncodedPayload)

Example 3 with 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);
}
Also used : PayloadEncoder(com.quorum.tessera.enclave.PayloadEncoder) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) EncodedPayload(com.quorum.tessera.enclave.EncodedPayload)

Example 4 with PayloadEncoder

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);
}
Also used : PayloadEncoder(com.quorum.tessera.enclave.PayloadEncoder) EncodedPayload(com.quorum.tessera.enclave.EncodedPayload)

Example 5 with PayloadEncoder

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);
}
Also used : PayloadEncoder(com.quorum.tessera.enclave.PayloadEncoder) EncodedPayload(com.quorum.tessera.enclave.EncodedPayload)

Aggregations

PayloadEncoder (com.quorum.tessera.enclave.PayloadEncoder)27 EncodedPayload (com.quorum.tessera.enclave.EncodedPayload)23 EncodedPayloadCodec (com.quorum.tessera.enclave.EncodedPayloadCodec)10 Response (jakarta.ws.rs.core.Response)6 Collectors (java.util.stream.Collectors)5 MessageHash (com.quorum.tessera.data.MessageHash)3 ResendRequest (com.quorum.tessera.p2p.resend.ResendRequest)3 PublishPayloadException (com.quorum.tessera.transaction.publish.PublishPayloadException)3 Operation (io.swagger.v3.oas.annotations.Operation)3 ApiResponse (io.swagger.v3.oas.annotations.responses.ApiResponse)3 java.util (java.util)3 IntStream (java.util.stream.IntStream)3 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)3 Test (org.junit.Test)3 RunWith (org.junit.runner.RunWith)3 Parameterized (org.junit.runners.Parameterized)3 Mockito (org.mockito.Mockito)3 PublicKey (com.quorum.tessera.encryption.PublicKey)2 ResendBatchResponse (com.quorum.tessera.recovery.resend.ResendBatchResponse)2 BatchResendManager (com.quorum.tessera.recovery.workflow.BatchResendManager)2