RedirectAuthoriseResponseTransformer extends AbstractServiceResponseTransformer { @Override public ServiceResponse transform(final PaymentService paymentServiceReply) throws WorldpayModelTransformationException { final RedirectAuthoriseServiceResponse authResponse = new RedirectAuthoriseServiceResponse(); final Object responseType = paymentServiceReply.getSubmitOrModifyOrInquiryOrReplyOrNotifyOrVerify().get(0); if (responseType == null) { throw new WorldpayModelTransformationException("No reply message in Worldpay response"); } if (!(responseType instanceof Reply)) { throw new WorldpayModelTransformationException("Reply type from Worldpay not the expected type"); } final Reply intReply = (Reply) responseType; if (getServiceResponseTransformerHelper().checkForError(authResponse, intReply)) { return authResponse; } final OrderStatus intOrderStatus = (OrderStatus) intReply.getOrderStatusOrBatchStatusOrErrorOrAddressCheckResponseOrRefundableAmountOrAccountBatchOrShopperOrOkOrFuturePayAgreementStatusOrShopperAuthenticationResultOrFuturePayPaymentResultOrPricePointOrCheckCardResponseOrPaymentOptionOrToken().get(0); if (intOrderStatus == null) { throw new WorldpayModelTransformationException("No order status returned in Worldpay reply message"); } authResponse.setOrderCode(intOrderStatus.getOrderCode()); final List<Object> intOrderData = intOrderStatus.getReferenceOrBankAccountOrApmEnrichedDataOrErrorOrPaymentOrQrCodeOrCardBalanceOrPaymentAdditionalDetailsOrBillingAddressDetailsOrExemptionResponseOrOrderModificationOrJournalOrRequestInfoOrChallengeRequiredOrFxApprovalRequiredOrPbbaRTPOrContentOrJournalTypeDetailOrTokenOrDateOrEchoDataOrPayAsOrderUseNewOrderCodeOrAuthenticateResponse(); intOrderData.stream() .filter(Token.class::isInstance) .map(Token.class::cast) .findAny() .map(getServiceResponseTransformerHelper()::buildTokenReply) .ifPresent(authResponse::setToken); final Reference reference = intOrderData.stream() .filter(Reference.class::isInstance) .map(Reference.class::cast) .findAny() .orElseThrow(() -> new WorldpayModelTransformationException("Order status type returned in Worldpay reply message is not one of the expected types for redirect authorise")); final RedirectReference redirectReference = new RedirectReference(reference.getId(), reference.getvalue()); authResponse.setRedirectReference(redirectReference); return authResponse; }  @Override ServiceResponse transform(final PaymentService paymentServiceReply);  }
@Test public void transformShouldReturnARedirectAuthoriseServiceResponse() throws Exception { final PaymentService paymentServiceReply = new PaymentService(); final List<Object> responseType = paymentServiceReply.getSubmitOrModifyOrInquiryOrReplyOrNotifyOrVerify(); final Reply paymentReply = new Reply(); responseType.add(paymentReply); final List<Object> statusElements = paymentReply.getOrderStatusOrBatchStatusOrErrorOrAddressCheckResponseOrRefundableAmountOrAccountBatchOrShopperOrOkOrFuturePayAgreementStatusOrShopperAuthenticationResultOrFuturePayPaymentResultOrPricePointOrCheckCardResponseOrPaymentOptionOrToken(); final OrderStatus intOrderStatus = new OrderStatus(); intOrderStatus.setOrderCode(ORDER_CODE); final List<Object> referenceElements = intOrderStatus.getReferenceOrBankAccountOrApmEnrichedDataOrErrorOrPaymentOrQrCodeOrCardBalanceOrPaymentAdditionalDetailsOrBillingAddressDetailsOrExemptionResponseOrOrderModificationOrJournalOrRequestInfoOrChallengeRequiredOrFxApprovalRequiredOrPbbaRTPOrContentOrJournalTypeDetailOrTokenOrDateOrEchoDataOrPayAsOrderUseNewOrderCodeOrAuthenticateResponse(); final Reference reference = new Reference(); reference.setId(REFERENCE_ID); reference.setvalue(REFERENCE_VALUE); referenceElements.add(reference); final Token token = new Token(); token.setTokenEventReference(TOKEN_REFERENCE); referenceElements.add(token); statusElements.add(intOrderStatus); when(serviceResponseTransformerHelperMock.buildTokenReply(token)).thenReturn(tokenReplyMock); final RedirectAuthoriseServiceResponse result = (RedirectAuthoriseServiceResponse) testObj.transform(paymentServiceReply); assertEquals(ORDER_CODE, result.getOrderCode()); assertEquals(tokenReplyMock, result.getToken()); assertEquals(REFERENCE_ID, result.getRedirectReference().getId()); assertEquals(REFERENCE_VALUE, result.getRedirectReference().getValue()); }
@Test public void shouldThrowWorldpayModelTransformationExceptionWhenResponseTypeIsNull() throws WorldpayModelTransformationException { thrown.expect(WorldpayModelTransformationException.class); thrown.expectMessage("No reply message in Worldpay response"); final PaymentService paymentServiceReply = new PaymentService(); final List<Object> responseType = paymentServiceReply.getSubmitOrModifyOrInquiryOrReplyOrNotifyOrVerify(); responseType.add(null); testObj.transform(paymentServiceReply); }
@Test public void shouldThrowWorldpayModelTransformationExceptionWhenResponseTypeIsNotReply() throws WorldpayModelTransformationException { thrown.expect(WorldpayModelTransformationException.class); thrown.expectMessage("Reply type from Worldpay not the expected type"); final PaymentService paymentServiceReply = new PaymentService(); final List<Object> responseType = paymentServiceReply.getSubmitOrModifyOrInquiryOrReplyOrNotifyOrVerify(); responseType.add(new Submit()); testObj.transform(paymentServiceReply); }
@Test public void responseShouldContainErrorDetailWhenReplyTypeIsError() throws WorldpayModelTransformationException { final PaymentService paymentServiceReply = new PaymentService(); final List<Object> responseType = paymentServiceReply.getSubmitOrModifyOrInquiryOrReplyOrNotifyOrVerify(); final Reply reply = new Reply(); final List<Object> replyElements = reply.getOrderStatusOrBatchStatusOrErrorOrAddressCheckResponseOrRefundableAmountOrAccountBatchOrShopperOrOkOrFuturePayAgreementStatusOrShopperAuthenticationResultOrFuturePayPaymentResultOrPricePointOrCheckCardResponseOrPaymentOptionOrToken(); final Error intError = new Error(); intError.setCode(ERROR_CODE); intError.setvalue(ERROR_DETAIL); replyElements.add(intError); responseType.add(reply); when(serviceResponseTransformerHelperMock.checkForError(any(ServiceResponse.class), eq(reply))).thenReturn(true); final RedirectAuthoriseServiceResponse result = (RedirectAuthoriseServiceResponse) testObj.transform(paymentServiceReply); verify(serviceResponseTransformerHelperMock).checkForError(result, reply); }
@Test public void shouldThrowWorldpayModelTransformationExceptionWhenOrderStatusIsNull() throws WorldpayModelTransformationException { thrown.expect(WorldpayModelTransformationException.class); thrown.expectMessage("No order status returned in Worldpay reply message"); final PaymentService paymentServiceReply = new PaymentService(); final List<Object> responseType = paymentServiceReply.getSubmitOrModifyOrInquiryOrReplyOrNotifyOrVerify(); final Reply reply = new Reply(); final List<Object> replyElements = reply.getOrderStatusOrBatchStatusOrErrorOrAddressCheckResponseOrRefundableAmountOrAccountBatchOrShopperOrOkOrFuturePayAgreementStatusOrShopperAuthenticationResultOrFuturePayPaymentResultOrPricePointOrCheckCardResponseOrPaymentOptionOrToken(); replyElements.add(null); responseType.add(reply); testObj.transform(paymentServiceReply); }
@Test public void shouldThrowWorldpayModelTransformationExceptionWhenOrderStatusTypeIsNotReference() throws WorldpayModelTransformationException { thrown.expect(WorldpayModelTransformationException.class); thrown.expectMessage("Order status type returned in Worldpay reply message is not one of the expected types for redirect authorise"); final PaymentService paymentServiceReply = new PaymentService(); final List<Object> responseType = paymentServiceReply.getSubmitOrModifyOrInquiryOrReplyOrNotifyOrVerify(); final Reply reply = new Reply(); final List<Object> replyElements = reply.getOrderStatusOrBatchStatusOrErrorOrAddressCheckResponseOrRefundableAmountOrAccountBatchOrShopperOrOkOrFuturePayAgreementStatusOrShopperAuthenticationResultOrFuturePayPaymentResultOrPricePointOrCheckCardResponseOrPaymentOptionOrToken(); final OrderStatus orderStatus = new OrderStatus(); final List<Object> orderStatusElements = orderStatus.getReferenceOrBankAccountOrApmEnrichedDataOrErrorOrPaymentOrQrCodeOrCardBalanceOrPaymentAdditionalDetailsOrBillingAddressDetailsOrExemptionResponseOrOrderModificationOrJournalOrRequestInfoOrChallengeRequiredOrFxApprovalRequiredOrPbbaRTPOrContentOrJournalTypeDetailOrTokenOrDateOrEchoDataOrPayAsOrderUseNewOrderCodeOrAuthenticateResponse(); orderStatusElements.add(new BankAccount()); replyElements.add(orderStatus); responseType.add(reply); testObj.transform(paymentServiceReply); }