use of com.quorum.tessera.recovery.resend.ResendBatchRequest 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);
}
use of com.quorum.tessera.recovery.resend.ResendBatchRequest 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);
}
use of com.quorum.tessera.recovery.resend.ResendBatchRequest 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());
}
use of com.quorum.tessera.recovery.resend.ResendBatchRequest in project tessera by ConsenSys.
the class BatchResendManagerImplTest method useMaxResultsWhenBatchSizeNotProvided.
@Test
public void useMaxResultsWhenBatchSizeNotProvided() {
final ResendBatchRequest request = ResendBatchRequest.Builder.create().withPublicKey(KEY_STRING).build();
List<EncryptedTransaction> transactions = IntStream.range(0, 5).mapToObj(i -> mock(EncryptedTransaction.class)).collect(Collectors.toUnmodifiableList());
when(encryptedTransactionDAO.transactionCount()).thenReturn(101L);
BatchWorkflow batchWorkflow = mock(BatchWorkflow.class);
when(batchWorkflow.getPublishedMessageCount()).thenReturn(// arbitary total that's returned as result.getTotal()
999L);
when(batchWorkflowFactory.create(101L)).thenReturn(batchWorkflow);
when(encryptedTransactionDAO.retrieveTransactions(lt(100), anyInt())).thenReturn(transactions);
when(encryptedTransactionDAO.retrieveTransactions(gt(99), anyInt())).thenReturn(List.of(mock(EncryptedTransaction.class)));
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);
}
Aggregations