WorldpayVoidOrderAction extends AbstractAction<WorldpayVoidProcessModel> { @Override public String execute(final WorldpayVoidProcessModel worldpayVoidProcessModel) { final OrderModel order = worldpayVoidProcessModel.getOrder(); boolean allCancelled = true; for (final PaymentTransactionModel paymentTransactionModel : order.getPaymentTransactions()) { boolean canCancel = true; for (final PaymentTransactionEntryModel paymentTransactionEntryModel : paymentTransactionModel.getEntries()) { if (!AUTHORIZATION.equals(paymentTransactionEntryModel.getType()) && !CAPTURE.equals(paymentTransactionEntryModel.getType())) { canCancel = false; } } if (canCancel) { paymentService.cancel(paymentTransactionModel.getEntries().get(0)); } else { allCancelled = false; } } return allCancelled ? "OK" : "NOK"; }  @Override String execute(final WorldpayVoidProcessModel worldpayVoidProcessModel); @Override Set<String> getTransitions(); @Required void setPaymentService(PaymentService paymentService);  }
@Test public void testExecuteShouldSendVoidCommand() throws Exception { when(worldpayVoidProcessModelMock.getOrder()).thenReturn(orderModelMock); when(orderModelMock.getPaymentTransactions()).thenReturn(singletonList(paymentTransactionMock)); when(paymentTransactionMock.getEntries()).thenReturn(singletonList(paymentTransactionEntryMock)); when(paymentTransactionEntryMock.getType()).thenReturn(AUTHORIZATION); final String result = testObj.execute(worldpayVoidProcessModelMock); verify(paymentServiceMock).cancel(paymentTransactionEntryMock); assertEquals(OK.toString(), result); }
@Test public void testExecuteShouldNotSendVoidCommandIfAnyEntryIsNotAuthOrCapture() throws Exception { when(worldpayVoidProcessModelMock.getOrder()).thenReturn(orderModelMock); when(orderModelMock.getPaymentTransactions()).thenReturn(singletonList(paymentTransactionMock)); when(paymentTransactionMock.getEntries()).thenReturn(asList(paymentTransactionEntryMock, paymentTransactionEntryMock)); when(paymentTransactionEntryMock.getType()).thenReturn(AUTHORIZATION).thenReturn(REFUND_FOLLOW_ON); final String result = testObj.execute(worldpayVoidProcessModelMock); verify(paymentServiceMock, never()).cancel(paymentTransactionEntryMock); assertEquals(NOK.toString(), result); }
@Test public void testExecuteShouldSendVoidCommandIfAllEntriesAreAuthOrCapture() throws Exception { when(worldpayVoidProcessModelMock.getOrder()).thenReturn(orderModelMock); when(orderModelMock.getPaymentTransactions()).thenReturn(singletonList(paymentTransactionMock)); when(paymentTransactionMock.getEntries()).thenReturn(asList(paymentTransactionEntryMock, paymentTransactionEntryMock)); when(paymentTransactionEntryMock.getType()).thenReturn(AUTHORIZATION).thenReturn(CAPTURE); final String result = testObj.execute(worldpayVoidProcessModelMock); verify(paymentServiceMock).cancel(paymentTransactionEntryMock); assertEquals(OK.toString(), result); }