DefaultWorldpayConnector implements WorldpayConnector { @Override public ServiceReply send(final PaymentService outboundPaymentService, final MerchantInfo merchantInfo, final String cookie) throws WorldpayException { final AtomicReference<ResponseEntity<String>> responseXML = new AtomicReference<>(); final Single<ResponseEntity<String>> response = sendOutboundXML(outboundPaymentService, merchantInfo, cookie); response.subscribe(responseXML::set); return processResponseXML(responseXML.get()); } DefaultWorldpayConnector(PaymentServiceMarshaller paymentServiceMarshaller, ConfigurationService configurationService, RestTemplate restTemplate); @Override ServiceReply send(final PaymentService outboundPaymentService, final MerchantInfo merchantInfo, final String cookie); @Override void logXMLOut(final PaymentService paymentService);  }
@Test public void shouldSendThePaymentServiceTransformedInXML() throws Exception { when(responseEntityMock.getHeaders()).thenReturn(httpHeadersMock); when(httpHeadersMock.get("Set-Cookie")).thenReturn(ImmutableList.of("cookie")); when(responseEntityMock.getBody()).thenReturn("someValue"); when(paymentServiceMarshallerMock.unmarshal(IOUtils.toInputStream("someValue"))).thenReturn(paymentServiceReplyMock); when(paymentServiceMarshallerMock.marshalAsFragment(paymentServiceRequestMock)).thenReturn("someXML"); when(restTemplateMock.postForEntity(uriArgumentCaptor.capture(), httpEntityArgumentCaptor.capture(), eq(String.class))).thenReturn(responseEntityMock); testObj.send(paymentServiceRequestMock, merchantInfoMock, "cookie"); final URI uri = uriArgumentCaptor.getValue(); assertThat(uri.toString()).isEqualTo(ENDPOINT); final HttpEntity<String> request = httpEntityArgumentCaptor.getValue(); final byte[] plainCreds = ("merchantCode" + ":" + "merchantPassword").getBytes(StandardCharsets.UTF_8); assertThat(request.getHeaders()).containsEntry(HttpHeaders.AUTHORIZATION, Collections.singletonList("Basic " + new String(Base64.getEncoder().encode(plainCreds)))); assertThat(request.getHeaders()).containsEntry(HttpHeaders.HOST, Collections.singletonList(uri.getHost())); assertThat(request.getHeaders()).containsEntry(HttpHeaders.COOKIE, Collections.singletonList("cookie")); assertThat(request.getBody()).startsWith(XML_HEADER); assertThat(request.getBody()).contains("someXML"); }