Search in sources :

Example 16 with PayloadEncoder

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);
}
Also used : IntStream(java.util.stream.IntStream) java.util(java.util) EncodedPayload(com.quorum.tessera.enclave.EncodedPayload) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) jakarta.persistence(jakarta.persistence) RunWith(org.junit.runner.RunWith) Callable(java.util.concurrent.Callable) Mockito.when(org.mockito.Mockito.when) PayloadEncoder(com.quorum.tessera.enclave.PayloadEncoder) Collectors(java.util.stream.Collectors) Mockito(org.mockito.Mockito) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Assertions(org.assertj.core.api.Assertions) com.quorum.tessera.data(com.quorum.tessera.data) org.junit(org.junit) EncodedPayloadCodec(com.quorum.tessera.enclave.EncodedPayloadCodec) Parameterized(org.junit.runners.Parameterized) PayloadEncoder(com.quorum.tessera.enclave.PayloadEncoder) EncodedPayload(com.quorum.tessera.enclave.EncodedPayload)

Example 17 with PayloadEncoder

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

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

Example 19 with PayloadEncoder

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

Example 20 with 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);
}
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