DeleteTokenResponseTransformer extends AbstractServiceResponseTransformer { @Override public ServiceResponse transform(final PaymentService paymentService) throws WorldpayModelTransformationException { final Object responseType = paymentService.getSubmitOrModifyOrInquiryOrReplyOrNotifyOrVerify().get(0); if (responseType == null) { throw new WorldpayModelTransformationException("No reply message in Worldpay delete token response"); } if (!(responseType instanceof Reply)) { throw new WorldpayModelTransformationException("Reply type from Worldpay not the expected type"); } final Reply intReply = (Reply) responseType; final Object response = intReply.getOrderStatusOrBatchStatusOrErrorOrAddressCheckResponseOrRefundableAmountOrAccountBatchOrShopperOrOkOrFuturePayAgreementStatusOrShopperAuthenticationResultOrFuturePayPaymentResultOrPricePointOrCheckCardResponseOrPaymentOptionOrToken().get(0); final DeleteTokenResponse deleteTokenResponse = new DeleteTokenResponse(); if (getServiceResponseTransformerHelper().checkForError(deleteTokenResponse, intReply)) { return deleteTokenResponse; } if (!(response instanceof Ok)) { throw new WorldpayModelTransformationException("DeleteTokenResponse did not contain an OK object"); } final Ok okResponse = (Ok) response; final Object deleteTokenReceived = okResponse.getCancelReceivedOrVoidReceivedOrCaptureReceivedOrRevokeReceivedOrRefundReceivedOrBackofficeCodeReceivedOrAuthorisationCodeReceivedOrDefenceReceivedOrUpdateTokenReceivedOrDeleteTokenReceivedOrExtendExpiryDateReceivedOrOrderReceivedOrCancelRetryDoneOrVoidSaleReceived().get(0); if (deleteTokenReceived instanceof DeleteTokenReceived) { deleteTokenResponse.setDeleteTokenResponse(getServiceResponseTransformerHelper().buildDeleteTokenReply((DeleteTokenReceived) deleteTokenReceived)); } return deleteTokenResponse; }  @Override ServiceResponse transform(final PaymentService paymentService);  }
@Test public void shouldTransformPaymentServiceIntoDeleteTokenResponse() throws Exception { when(serviceResponseTransformerHelperMock.buildDeleteTokenReply(any(DeleteTokenReceived.class))).thenReturn(deleteTokenResponseMock); final DeleteTokenResponse result = (DeleteTokenResponse) testObj.transform(paymentServiceMock); assertEquals(deleteTokenResponseMock, result.getDeleteTokenReply()); }
@Test public void shouldRaiseWorldpayModelTransformationExceptionWhenThereIsNoReplyInThePaymentService() throws Exception { thrown.expect(WorldpayModelTransformationException.class); thrown.expectMessage("No reply message in Worldpay delete token response"); when(paymentServiceMock.getSubmitOrModifyOrInquiryOrReplyOrNotifyOrVerify()).thenReturn(singletonList(null)); testObj.transform(paymentServiceMock); }
@Test public void shouldRaiseWorldpayModelTransformationExceptionWhenResponseIsNotAReply() throws Exception { thrown.expect(WorldpayModelTransformationException.class); thrown.expectMessage("Reply type from Worldpay not the expected type"); when(paymentServiceMock.getSubmitOrModifyOrInquiryOrReplyOrNotifyOrVerify()).thenReturn(singletonList(new Modify())); testObj.transform(paymentServiceMock); }
@Test public void shouldThrowExceptionWhenResponseIsNotOk() throws Exception { thrown.expect(WorldpayModelTransformationException.class); thrown.expectMessage("DeleteTokenResponse did not contain an OK object"); when(replyMock.getOrderStatusOrBatchStatusOrErrorOrAddressCheckResponseOrRefundableAmountOrAccountBatchOrShopperOrOkOrFuturePayAgreementStatusOrShopperAuthenticationResultOrFuturePayPaymentResultOrPricePointOrCheckCardResponseOrPaymentOptionOrToken()). thenReturn(singletonList(new Shopper())); testObj.transform(paymentServiceMock); }
@Test public void shouldReturnDeleteTokenResponseWithErrorDetails() throws Exception { when(serviceResponseTransformerHelperMock.checkForError(any(DeleteTokenResponse.class), eq(replyMock))).thenReturn(true); when(replyMock.getOrderStatusOrBatchStatusOrErrorOrAddressCheckResponseOrRefundableAmountOrAccountBatchOrShopperOrOkOrFuturePayAgreementStatusOrShopperAuthenticationResultOrFuturePayPaymentResultOrPricePointOrCheckCardResponseOrPaymentOptionOrToken()) .thenReturn(singletonList(errorMock)); final ServiceResponse result = testObj.transform(paymentServiceMock); verify(serviceResponseTransformerHelperMock).checkForError(deleteTokenResponseArgumentCaptor.capture(), eq(replyMock)); assertEquals(result, deleteTokenResponseArgumentCaptor.getValue()); }