Search in sources :

Example 1 with EncodedPayload

use of com.quorum.tessera.enclave.EncodedPayload 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 EncodedPayload

use of com.quorum.tessera.enclave.EncodedPayload 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 EncodedPayload

use of com.quorum.tessera.enclave.EncodedPayload 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 EncodedPayload

use of com.quorum.tessera.enclave.EncodedPayload 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 EncodedPayload

use of com.quorum.tessera.enclave.EncodedPayload 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

EncodedPayload (com.quorum.tessera.enclave.EncodedPayload)91 Test (org.junit.Test)60 PublicKey (com.quorum.tessera.encryption.PublicKey)50 PayloadEncoder (com.quorum.tessera.enclave.PayloadEncoder)23 Response (jakarta.ws.rs.core.Response)20 MessageHash (com.quorum.tessera.data.MessageHash)13 Collectors (java.util.stream.Collectors)12 EncryptedTransaction (com.quorum.tessera.data.EncryptedTransaction)11 PrivacyMode (com.quorum.tessera.enclave.PrivacyMode)11 EncodedPayloadCodec (com.quorum.tessera.enclave.EncodedPayloadCodec)9 NodeInfo (com.quorum.tessera.partyinfo.node.NodeInfo)8 Recipient (com.quorum.tessera.partyinfo.node.Recipient)8 java.util (java.util)8 Invocation (jakarta.ws.rs.client.Invocation)6 WebTarget (jakarta.ws.rs.client.WebTarget)6 StagingTransaction (com.quorum.tessera.data.staging.StagingTransaction)5 PrivacyMetadata (com.quorum.tessera.enclave.PrivacyMetadata)5 RecipientBox (com.quorum.tessera.enclave.RecipientBox)5 TxHash (com.quorum.tessera.enclave.TxHash)5 ResendRequest (com.quorum.tessera.p2p.resend.ResendRequest)5