Search in sources :

Example 1 with BatchWorkflow

use of com.quorum.tessera.recovery.workflow.BatchWorkflow in project tessera by ConsenSys.

the class BatchResendManagerImplTest method useMaxResultsAlsoWhenBatchSizeTooLarge.

@Test
public void useMaxResultsAlsoWhenBatchSizeTooLarge() {
    final ResendBatchRequest request = ResendBatchRequest.Builder.create().withBatchSize(10000000).withPublicKey(KEY_STRING).build();
    List<EncryptedTransaction> transactions = IntStream.range(0, 5).mapToObj(i -> mock(EncryptedTransaction.class)).collect(Collectors.toUnmodifiableList());
    when(encryptedTransactionDAO.transactionCount()).thenReturn(101L);
    when(encryptedTransactionDAO.retrieveTransactions(lt(100), anyInt())).thenReturn(transactions);
    when(encryptedTransactionDAO.retrieveTransactions(gt(99), anyInt())).thenReturn(singletonList(mock(EncryptedTransaction.class)));
    final BatchWorkflow batchWorkflow = mock(BatchWorkflow.class);
    when(batchWorkflow.getPublishedMessageCount()).thenReturn(999L);
    when(batchWorkflowFactory.create(101L)).thenReturn(batchWorkflow);
    final ResendBatchResponse result = manager.resendBatch(request);
    assertThat(result.getTotal()).isEqualTo(999L);
    verify(batchWorkflow, times(101)).execute(any(BatchWorkflowContext.class));
    verify(encryptedTransactionDAO, times(21)).retrieveTransactions(anyInt(), anyInt());
    verify(encryptedTransactionDAO).transactionCount();
    verify(batchWorkflowFactory).create(101L);
}
Also used : AdditionalMatchers.lt(org.mockito.AdditionalMatchers.lt) ResendBatchRequest(com.quorum.tessera.recovery.resend.ResendBatchRequest) IntStream(java.util.stream.IntStream) PublicKey(com.quorum.tessera.encryption.PublicKey) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) BatchWorkflowFactory(com.quorum.tessera.recovery.workflow.BatchWorkflowFactory) BatchResendManager(com.quorum.tessera.recovery.workflow.BatchResendManager) Base64Codec(com.quorum.tessera.base64.Base64Codec) EncryptedTransaction(com.quorum.tessera.data.EncryptedTransaction) BatchWorkflow(com.quorum.tessera.recovery.workflow.BatchWorkflow) Collections.singletonList(java.util.Collections.singletonList) PushBatchRequest(com.quorum.tessera.recovery.resend.PushBatchRequest) BatchWorkflowContext(com.quorum.tessera.recovery.workflow.BatchWorkflowContext) StagingEntityDAO(com.quorum.tessera.data.staging.StagingEntityDAO) EncryptedTransactionDAO(com.quorum.tessera.data.EncryptedTransactionDAO) After(org.junit.After) Before(org.junit.Before) Collections.emptyMap(java.util.Collections.emptyMap) AdditionalMatchers.gt(org.mockito.AdditionalMatchers.gt) com.quorum.tessera.enclave(com.quorum.tessera.enclave) Nonce(com.quorum.tessera.encryption.Nonce) ServiceLoader(java.util.ServiceLoader) Test(org.junit.Test) StagingTransaction(com.quorum.tessera.data.staging.StagingTransaction) Collectors(java.util.stream.Collectors) Mockito(org.mockito.Mockito) List(java.util.List) ResendBatchResponse(com.quorum.tessera.recovery.resend.ResendBatchResponse) Optional(java.util.Optional) ResendBatchResponse(com.quorum.tessera.recovery.resend.ResendBatchResponse) ResendBatchRequest(com.quorum.tessera.recovery.resend.ResendBatchRequest) BatchWorkflow(com.quorum.tessera.recovery.workflow.BatchWorkflow) BatchWorkflowContext(com.quorum.tessera.recovery.workflow.BatchWorkflowContext) EncryptedTransaction(com.quorum.tessera.data.EncryptedTransaction) Test(org.junit.Test)

Example 2 with BatchWorkflow

use of com.quorum.tessera.recovery.workflow.BatchWorkflow in project tessera by ConsenSys.

the class BatchResendManagerImplTest method resendBatch.

@Test
public void resendBatch() {
    ResendBatchRequest request = ResendBatchRequest.Builder.create().withBatchSize(3).withPublicKey(KEY_STRING).build();
    List<EncryptedTransaction> transactions = IntStream.range(0, 5).mapToObj(i -> mock(EncryptedTransaction.class)).collect(Collectors.toUnmodifiableList());
    when(encryptedTransactionDAO.transactionCount()).thenReturn(101L);
    when(encryptedTransactionDAO.retrieveTransactions(lt(100), anyInt())).thenReturn(transactions);
    when(encryptedTransactionDAO.retrieveTransactions(gt(99), anyInt())).thenReturn(singletonList(mock(EncryptedTransaction.class)));
    BatchWorkflow batchWorkflow = mock(BatchWorkflow.class);
    when(batchWorkflow.getPublishedMessageCount()).thenReturn(999L);
    when(batchWorkflowFactory.create(101L)).thenReturn(batchWorkflow);
    final ResendBatchResponse result = manager.resendBatch(request);
    assertThat(result.getTotal()).isEqualTo(999L);
    verify(batchWorkflow).getPublishedMessageCount();
    verify(batchWorkflow, times(101)).execute(any(BatchWorkflowContext.class));
    verify(encryptedTransactionDAO, times(21)).retrieveTransactions(anyInt(), anyInt());
    verify(encryptedTransactionDAO).transactionCount();
    verify(batchWorkflowFactory).create(101L);
}
Also used : AdditionalMatchers.lt(org.mockito.AdditionalMatchers.lt) ResendBatchRequest(com.quorum.tessera.recovery.resend.ResendBatchRequest) IntStream(java.util.stream.IntStream) PublicKey(com.quorum.tessera.encryption.PublicKey) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) BatchWorkflowFactory(com.quorum.tessera.recovery.workflow.BatchWorkflowFactory) BatchResendManager(com.quorum.tessera.recovery.workflow.BatchResendManager) Base64Codec(com.quorum.tessera.base64.Base64Codec) EncryptedTransaction(com.quorum.tessera.data.EncryptedTransaction) BatchWorkflow(com.quorum.tessera.recovery.workflow.BatchWorkflow) Collections.singletonList(java.util.Collections.singletonList) PushBatchRequest(com.quorum.tessera.recovery.resend.PushBatchRequest) BatchWorkflowContext(com.quorum.tessera.recovery.workflow.BatchWorkflowContext) StagingEntityDAO(com.quorum.tessera.data.staging.StagingEntityDAO) EncryptedTransactionDAO(com.quorum.tessera.data.EncryptedTransactionDAO) After(org.junit.After) Before(org.junit.Before) Collections.emptyMap(java.util.Collections.emptyMap) AdditionalMatchers.gt(org.mockito.AdditionalMatchers.gt) com.quorum.tessera.enclave(com.quorum.tessera.enclave) Nonce(com.quorum.tessera.encryption.Nonce) ServiceLoader(java.util.ServiceLoader) Test(org.junit.Test) StagingTransaction(com.quorum.tessera.data.staging.StagingTransaction) Collectors(java.util.stream.Collectors) Mockito(org.mockito.Mockito) List(java.util.List) ResendBatchResponse(com.quorum.tessera.recovery.resend.ResendBatchResponse) Optional(java.util.Optional) ResendBatchResponse(com.quorum.tessera.recovery.resend.ResendBatchResponse) ResendBatchRequest(com.quorum.tessera.recovery.resend.ResendBatchRequest) BatchWorkflow(com.quorum.tessera.recovery.workflow.BatchWorkflow) BatchWorkflowContext(com.quorum.tessera.recovery.workflow.BatchWorkflowContext) EncryptedTransaction(com.quorum.tessera.data.EncryptedTransaction) Test(org.junit.Test)

Example 3 with BatchWorkflow

use of com.quorum.tessera.recovery.workflow.BatchWorkflow in project tessera by ConsenSys.

the class BatchResendManagerImpl method resendBatch.

@Override
public ResendBatchResponse resendBatch(ResendBatchRequest request) {
    final int batchSize = validateRequestBatchSize(request.getBatchSize());
    final byte[] publicKeyData = Base64.getDecoder().decode(request.getPublicKey());
    final PublicKey recipientPublicKey = PublicKey.from(publicKeyData);
    final long transactionCount = encryptedTransactionDAO.transactionCount();
    final long batchCount = calculateBatchCount(maxResults, transactionCount);
    final BatchWorkflow batchWorkflow = batchWorkflowFactory.create(transactionCount);
    IntStream.range(0, (int) batchCount).map(i -> i * maxResults).mapToObj(offset -> encryptedTransactionDAO.retrieveTransactions(offset, maxResults)).flatMap(List::stream).forEach(encryptedTransaction -> {
        final BatchWorkflowContext context = new BatchWorkflowContext();
        context.setEncryptedTransaction(encryptedTransaction);
        context.setEncodedPayload(encryptedTransaction.getPayload());
        context.setRecipientKey(recipientPublicKey);
        context.setBatchSize(batchSize);
        batchWorkflow.execute(context);
    });
    return ResendBatchResponse.from(batchWorkflow.getPublishedMessageCount());
}
Also used : ResendBatchRequest(com.quorum.tessera.recovery.resend.ResendBatchRequest) IntStream(java.util.stream.IntStream) PublicKey(com.quorum.tessera.encryption.PublicKey) BatchWorkflowFactory(com.quorum.tessera.recovery.workflow.BatchWorkflowFactory) BatchResendManager(com.quorum.tessera.recovery.workflow.BatchResendManager) BatchWorkflow(com.quorum.tessera.recovery.workflow.BatchWorkflow) Objects(java.util.Objects) StagingTransactionUtils(com.quorum.tessera.data.staging.StagingTransactionUtils) PushBatchRequest(com.quorum.tessera.recovery.resend.PushBatchRequest) BatchWorkflowContext(com.quorum.tessera.recovery.workflow.BatchWorkflowContext) Base64(java.util.Base64) List(java.util.List) StagingEntityDAO(com.quorum.tessera.data.staging.StagingEntityDAO) ResendBatchResponse(com.quorum.tessera.recovery.resend.ResendBatchResponse) EncryptedTransactionDAO(com.quorum.tessera.data.EncryptedTransactionDAO) PublicKey(com.quorum.tessera.encryption.PublicKey) BatchWorkflow(com.quorum.tessera.recovery.workflow.BatchWorkflow) BatchWorkflowContext(com.quorum.tessera.recovery.workflow.BatchWorkflowContext)

Example 4 with BatchWorkflow

use of com.quorum.tessera.recovery.workflow.BatchWorkflow in project tessera by ConsenSys.

the class BatchWorkflowFactoryImplTest method workflowExecutedReturnFalse.

@Test
public void workflowExecutedReturnFalse() {
    BatchWorkflowFactoryImpl batchWorkflowFactory = new BatchWorkflowFactoryImpl(enclave, discovery, resendBatchPublisher);
    BatchWorkflow batchWorkflow = batchWorkflowFactory.create(999L);
    assertThat(batchWorkflow).isNotNull();
    BatchWorkflowContext batchWorkflowContext = new BatchWorkflowContext();
    PublicKey publicKey = mock(PublicKey.class);
    batchWorkflowContext.setRecipientKey(publicKey);
    EncryptedTransaction encryptedTransaction = mock(EncryptedTransaction.class);
    batchWorkflowContext.setEncryptedTransaction(encryptedTransaction);
    batchWorkflowContext.setEncodedPayload(mock(EncodedPayload.class));
    when(enclave.status()).thenReturn(Service.Status.STARTED);
    assertThat(batchWorkflow.execute(batchWorkflowContext)).isFalse();
    assertThat(batchWorkflow.getPublishedMessageCount()).isZero();
    verify(enclave).status();
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) EncodedPayload(com.quorum.tessera.enclave.EncodedPayload) BatchWorkflow(com.quorum.tessera.recovery.workflow.BatchWorkflow) BatchWorkflowContext(com.quorum.tessera.recovery.workflow.BatchWorkflowContext) EncryptedTransaction(com.quorum.tessera.data.EncryptedTransaction) Test(org.junit.Test)

Example 5 with BatchWorkflow

use of com.quorum.tessera.recovery.workflow.BatchWorkflow in project tessera by ConsenSys.

the class BatchWorkflowFactoryImplTest method createBatchWorkflowFactoryImplAndExecuteWorkflow.

@Test
public void createBatchWorkflowFactoryImplAndExecuteWorkflow() {
    BatchWorkflowFactoryImpl batchWorkflowFactory = new BatchWorkflowFactoryImpl(enclave, discovery, resendBatchPublisher);
    BatchWorkflow batchWorkflow = batchWorkflowFactory.create(1L);
    assertThat(batchWorkflow).isNotNull();
    BatchWorkflowContext batchWorkflowContext = new BatchWorkflowContext();
    PublicKey recipientKey = mock(PublicKey.class);
    batchWorkflowContext.setRecipientKey(recipientKey);
    PublicKey ownedKey = mock(PublicKey.class);
    EncodedPayload encodedPayload = mock(EncodedPayload.class);
    when(encodedPayload.getSenderKey()).thenReturn(ownedKey);
    when(encodedPayload.getRecipientKeys()).thenReturn(List.of(recipientKey));
    EncryptedTransaction encryptedTransaction = mock(EncryptedTransaction.class);
    when(encryptedTransaction.getPayload()).thenReturn(encodedPayload);
    batchWorkflowContext.setEncryptedTransaction(encryptedTransaction);
    batchWorkflowContext.setEncodedPayload(encodedPayload);
    batchWorkflowContext.setBatchSize(100);
    when(mockPayloadBuilder.build()).thenReturn(encodedPayload);
    when(enclave.status()).thenReturn(Service.Status.STARTED);
    when(enclave.getPublicKeys()).thenReturn(Set.of(ownedKey));
    NodeInfo nodeInfo = mock(NodeInfo.class);
    when(nodeInfo.getRecipients()).thenReturn(Set.of(Recipient.of(recipientKey, "url")));
    when(discovery.getCurrent()).thenReturn(nodeInfo);
    assertThat(batchWorkflow.execute(batchWorkflowContext)).isTrue();
    assertThat(batchWorkflow.getPublishedMessageCount()).isOne();
    verify(enclave).status();
    verify(enclave, times(2)).getPublicKeys();
    mockStaticPayloadBuilder.verify(() -> EncodedPayload.Builder.forRecipient(any(), any()));
    verify(mockPayloadBuilder).build();
    verify(discovery).getCurrent();
    verify(resendBatchPublisher).publishBatch(any(), any());
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) NodeInfo(com.quorum.tessera.partyinfo.node.NodeInfo) EncodedPayload(com.quorum.tessera.enclave.EncodedPayload) BatchWorkflow(com.quorum.tessera.recovery.workflow.BatchWorkflow) BatchWorkflowContext(com.quorum.tessera.recovery.workflow.BatchWorkflowContext) EncryptedTransaction(com.quorum.tessera.data.EncryptedTransaction) Test(org.junit.Test)

Aggregations

PublicKey (com.quorum.tessera.encryption.PublicKey)6 BatchWorkflow (com.quorum.tessera.recovery.workflow.BatchWorkflow)6 BatchWorkflowContext (com.quorum.tessera.recovery.workflow.BatchWorkflowContext)6 EncryptedTransaction (com.quorum.tessera.data.EncryptedTransaction)5 Test (org.junit.Test)5 EncryptedTransactionDAO (com.quorum.tessera.data.EncryptedTransactionDAO)4 StagingEntityDAO (com.quorum.tessera.data.staging.StagingEntityDAO)4 PushBatchRequest (com.quorum.tessera.recovery.resend.PushBatchRequest)4 ResendBatchRequest (com.quorum.tessera.recovery.resend.ResendBatchRequest)4 ResendBatchResponse (com.quorum.tessera.recovery.resend.ResendBatchResponse)4 BatchResendManager (com.quorum.tessera.recovery.workflow.BatchResendManager)4 BatchWorkflowFactory (com.quorum.tessera.recovery.workflow.BatchWorkflowFactory)4 List (java.util.List)4 IntStream (java.util.stream.IntStream)4 Base64Codec (com.quorum.tessera.base64.Base64Codec)3 StagingTransaction (com.quorum.tessera.data.staging.StagingTransaction)3 com.quorum.tessera.enclave (com.quorum.tessera.enclave)3 Nonce (com.quorum.tessera.encryption.Nonce)3 Collections.emptyMap (java.util.Collections.emptyMap)3 Collections.singletonList (java.util.Collections.singletonList)3