CreateTokenResponseTransformer extends AbstractServiceResponseTransformer { @Override public ServiceResponse transform(final PaymentService paymentService) throws WorldpayModelTransformationException { final CreateTokenResponse createTokenResponse = new CreateTokenResponse(); final Object responseType = paymentService.getSubmitOrModifyOrInquiryOrReplyOrNotifyOrVerify().get(0); if (responseType == null) { throw new WorldpayModelTransformationException("No reply message in Worldpay create 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); if (getServiceResponseTransformerHelper().checkForError(createTokenResponse, intReply)) { return createTokenResponse; } else if (response instanceof Token) { createTokenResponse.setToken(getServiceResponseTransformerHelper().buildTokenReply((Token) response)); } return createTokenResponse; }  @Override ServiceResponse transform(final PaymentService paymentService);  }
@Test public void shouldRaiseErrorIfPaymentServiceReplyIsNull() throws WorldpayModelTransformationException { thrown.expect(WorldpayModelTransformationException.class); thrown.expectMessage("No reply message in Worldpay create token response"); when(paymentServiceReplyMock.getSubmitOrModifyOrInquiryOrReplyOrNotifyOrVerify()).thenReturn(singletonList(null)); testObj.transform(paymentServiceReplyMock); }
@Test public void shouldRaiseErrorIfResponseTypeIsNotReply() throws WorldpayModelTransformationException { thrown.expect(WorldpayModelTransformationException.class); thrown.expectMessage("Reply type from Worldpay not the expected type"); when(paymentServiceReplyMock.getSubmitOrModifyOrInquiryOrReplyOrNotifyOrVerify()).thenReturn(singletonList(new Submit())); testObj.transform(paymentServiceReplyMock); }
@Test public void shouldReturnCreateTokenResponseForCard() throws WorldpayModelTransformationException { when(serviceResponseTransformerHelperMock.buildTokenReply(any(Token.class))).thenReturn(tokenReplyMock); final CreateTokenResponse result = (CreateTokenResponse) testObj.transform(createServiceReplyCard()); assertEquals(tokenReplyMock, result.getToken()); }
@Test public void shouldReturnCreateTokenResponseForPaypal() throws WorldpayModelTransformationException { when(serviceResponseTransformerHelperMock.buildTokenReply(any(Token.class))).thenReturn(tokenReplyMock); final CreateTokenResponse result = (CreateTokenResponse) testObj.transform(createServiceReplyPaypal()); assertEquals(tokenReplyMock, result.getToken()); }
@Test public void shouldReturnTokenResponseWithErrorWhenErrorOccurs() throws WorldpayModelTransformationException { final PaymentService paymentServiceReply = new PaymentService(); final Reply reply = new Reply(); final List<Object> responses = reply.getOrderStatusOrBatchStatusOrErrorOrAddressCheckResponseOrRefundableAmountOrAccountBatchOrShopperOrOkOrFuturePayAgreementStatusOrShopperAuthenticationResultOrFuturePayPaymentResultOrPricePointOrCheckCardResponseOrPaymentOptionOrToken(); final Error error = new Error(); error.setCode(ERROR_CODE); error.setvalue(ERROR_VALUE); responses.add(error); paymentServiceReply.getSubmitOrModifyOrInquiryOrReplyOrNotifyOrVerify().add(reply); when(serviceResponseTransformerHelperMock.checkForError(any(ServiceResponse.class), eq(reply))).thenReturn(true); final ServiceResponse result = testObj.transform(paymentServiceReply); verify(serviceResponseTransformerHelperMock).checkForError(result, reply); }