UpdateTokenResponseTransformer 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 update 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 UpdateTokenResponse updateTokenResponse = new UpdateTokenResponse(); if (getServiceResponseTransformerHelper().checkForError(updateTokenResponse, intReply)) { return updateTokenResponse; } if (!(response instanceof Ok)) { throw new WorldpayModelTransformationException("UpdateTokenResponse did not contain an OK object"); } final Ok okResponse = (Ok) response; final Object updateTokenReceived = okResponse.getCancelReceivedOrVoidReceivedOrCaptureReceivedOrRevokeReceivedOrRefundReceivedOrBackofficeCodeReceivedOrAuthorisationCodeReceivedOrDefenceReceivedOrUpdateTokenReceivedOrDeleteTokenReceivedOrExtendExpiryDateReceivedOrOrderReceivedOrCancelRetryDoneOrVoidSaleReceived().get(0); if (updateTokenReceived instanceof UpdateTokenReceived) { updateTokenResponse.setUpdateTokenReply(getServiceResponseTransformerHelper().buildUpdateTokenReply((UpdateTokenReceived) updateTokenReceived)); } return updateTokenResponse; }  @Override ServiceResponse transform(final PaymentService paymentService);  }
@Test public void shouldTransformPaymentServiceIntoUpdateTokenResponse() throws Exception { when(serviceResponseTransformerHelper.buildUpdateTokenReply(updateTokenReceivedMock)).thenReturn(updateTokenReplyMock); when(updateTokenReplyMock.getPaymentTokenId()).thenReturn(PAYMENT_TOKEN_ID); final UpdateTokenResponse result = (UpdateTokenResponse) testObj.transform(paymentServiceMock); assertEquals(PAYMENT_TOKEN_ID, result.getUpdateTokenReply().getPaymentTokenId()); }
@Test public void shouldRaiseWorldpayModelTransformationExceptionWhenThereIsNoReplyInThePaymentService() throws Exception { thrown.expect(WorldpayModelTransformationException.class); thrown.expectMessage("No reply message in Worldpay update 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("UpdateTokenResponse did not contain an OK object"); when(replyMock.getOrderStatusOrBatchStatusOrErrorOrAddressCheckResponseOrRefundableAmountOrAccountBatchOrShopperOrOkOrFuturePayAgreementStatusOrShopperAuthenticationResultOrFuturePayPaymentResultOrPricePointOrCheckCardResponseOrPaymentOptionOrToken()). thenReturn(singletonList(new Shopper())); testObj.transform(paymentServiceMock); }
@Test public void shouldThrowExceptionWhenErrorInResponse() throws Exception { when(replyMock.getOrderStatusOrBatchStatusOrErrorOrAddressCheckResponseOrRefundableAmountOrAccountBatchOrShopperOrOkOrFuturePayAgreementStatusOrShopperAuthenticationResultOrFuturePayPaymentResultOrPricePointOrCheckCardResponseOrPaymentOptionOrToken()) .thenReturn(singletonList(errorMock)); when(errorMock.getCode()).thenReturn(ERROR_CODE); when(errorMock.getvalue()).thenReturn(ERROR_VALUE); when(serviceResponseTransformerHelper.checkForError(any(ServiceResponse.class), eq(replyMock))).thenReturn(true); final ServiceResponse result = testObj.transform(paymentServiceMock); verify(serviceResponseTransformerHelper).checkForError(result, replyMock); }