WorldpayCancelPaymentAction extends AbstractSimpleDecisionAction<OrderProcessModel> { @Override public Transition executeAction(final OrderProcessModel orderProcessModel) { final OrderModel order = orderProcessModel.getOrder(); final List<PaymentTransactionModel> paymentTransactions = order.getPaymentTransactions(); if (firstElementExists(paymentTransactions)) { final List<PaymentTransactionEntryModel> paymentTransactionEntryModels = worldpayPaymentTransactionService.filterPaymentTransactionEntriesOfType(paymentTransactions.get(0), AUTHORIZATION); if (firstElementExists(paymentTransactionEntryModels)) { final PaymentTransactionEntryModel cancelledPaymentTransactionEntry = paymentService.cancel(paymentTransactionEntryModels.get(0)); if (isSuccessful(cancelledPaymentTransactionEntry)) { return Transition.OK; } } } return Transition.NOK; }  @Override Transition executeAction(final OrderProcessModel orderProcessModel); @Required void setPaymentService(PaymentService paymentService); @Required void setWorldpayPaymentTransactionService(WorldpayPaymentTransactionService worldpayPaymentTransactionService);  }
@Test public void testExecuteShouldReturnOKWhenCancelIsSuccessful() { when(cancellationPaymentTransactionEntryModelMock.getTransactionStatus()).thenReturn(TransactionStatus.ACCEPTED.name()); when(cancellationPaymentTransactionEntryModelMock.getTransactionStatusDetails()).thenReturn(TransactionStatusDetails.SUCCESFULL.name()); final Transition result = testObj.executeAction(orderProcessModelMock); assertEquals(Transition.OK, result); }
@Test public void testExecuteShouldReturnNOKWhenCancelIsNotSuccessful() { when(cancellationPaymentTransactionEntryModelMock.getTransactionStatus()).thenReturn(TransactionStatus.ERROR.name()); when(cancellationPaymentTransactionEntryModelMock.getTransactionStatusDetails()).thenReturn(TransactionStatusDetails.COMMUNICATION_PROBLEM.name()); final Transition result = testObj.executeAction(orderProcessModelMock); assertEquals(Transition.NOK, result); }
@Test public void testExecuteShouldReturnNOKWhenOrderHasNoTransactions() { when(orderModelMock.getPaymentTransactions()).thenReturn(emptyList()); final Transition result = testObj.executeAction(orderProcessModelMock); assertEquals(Transition.NOK, result); }
@Test public void testExecuteShouldReturnNOKWhenOrderNullTransaction() { when(orderModelMock.getPaymentTransactions()).thenReturn(singletonList(null)); final Transition result = testObj.executeAction(orderProcessModelMock); assertEquals(Transition.NOK, result); }
@Test public void testExecuteShouldReturnNOKWhenOrderHasNoTransactionEntries() { when(worldpayPaymentTransactionServiceMock.filterPaymentTransactionEntriesOfType(paymentTransactionModelMock, AUTHORIZATION)).thenReturn(emptyList()); final Transition result = testObj.executeAction(orderProcessModelMock); assertEquals(Transition.NOK, result); }
@Test public void testExecuteShouldReturnNOKWhenOrderHasNullTransactionEntry() { when(worldpayPaymentTransactionServiceMock.filterPaymentTransactionEntriesOfType(paymentTransactionModelMock, AUTHORIZATION)).thenReturn(singletonList(null)); final Transition result = testObj.executeAction(orderProcessModelMock); assertEquals(Transition.NOK, result); }