OrderHistoryClient { public OrderHistory getOrderHistory(String userId) throws IOException { ObjectFactory factory = new ObjectFactory(); GetOrderHistoryRequest getOrderHistoryRequest = factory.createGetOrderHistoryRequest(); getOrderHistoryRequest.setUserId(userId); WebServiceMessage request = webServiceTemplate.getMessageFactory().createWebServiceMessage(); MarshallingUtils.marshal(webServiceTemplate.getMarshaller(), getOrderHistoryRequest, request); DOMResult responseResult = new DOMResult(); webServiceTemplate.sendSourceAndReceiveToResult(request.getPayloadSource(), responseResult); List<Order> orders = orderXPath.evaluate(responseResult.getNode(), new NodeMapper<Order>() { @Override public Order mapNode(Node node, int nodeNum) { String orderId = orderIdXPath.evaluateAsString(node); return new Order(orderId); } }); OrderHistory result = new OrderHistory(); result.setOrders(orders); LOGGER.info("found '{}' orders for userId='{}'", result.getOrders().size(), userId); return result; }  OrderHistory getOrderHistory(String userId);  }
@Test public void testGetOrderHistory() throws IOException { Source requestPayload = new StringSource( "<ns1:getOrderHistoryRequest xmlns:ns1=\"http: + "<ns1:userId>abc123</ns1:userId>" + "</ns1:getOrderHistoryRequest>"); Source responsePayload = new StringSource( "<ns1:getOrderHistoryResponse xmlns:ns1=\"http: + "<ns1:orderHistory>" + "<ns1:orderList>" + "<ns1:order><ns1:orderId>order1</ns1:orderId></ns1:order>" + "<ns1:order><ns1:orderId>order2</ns1:orderId></ns1:order>" + "<ns1:order><ns1:orderId>order3</ns1:orderId></ns1:order>" + "</ns1:orderList>" + "</ns1:orderHistory>" + "</ns1:getOrderHistoryResponse>"); mockWebServiceServer.expect(payload(requestPayload)).andRespond(withPayload(responsePayload)); OrderHistory orderHistory = orderHistoryClient.getOrderHistory("abc123"); assertThat(orderHistory.getOrders().get(2).getOrderId()).isEqualTo("order3"); mockWebServiceServer.verify(); }
@Test public void testGetOrderHistoryOnlyNeededElements() throws IOException { Source requestPayload = new StringSource( "<ns1:getOrderHistoryRequest xmlns:ns1=\"http: + "<ns1:userId>def456</ns1:userId>" + "</ns1:getOrderHistoryRequest>"); Source responsePayload = new StringSource( "<ns1:getOrderHistoryResponse xmlns:ns1=\"http: + "<ns1:orderHistory>" + "<ns1:orderList>" + "<ns1:order><ns1:orderId>order4</ns1:orderId><ns1:orderName>order-name-1</ns1:orderName></ns1:order>" + "<ns1:order><ns1:orderId>order5</ns1:orderId><ns1:orderName>order-name-2</ns1:orderName></ns1:order>" + "<ns1:order><ns1:orderId>order6</ns1:orderId><ns1:orderName>order-name-3</ns1:orderName></ns1:order>" + "</ns1:orderList>" + "</ns1:orderHistory>" + "</ns1:getOrderHistoryResponse>"); mockWebServiceServer.expect(payload(requestPayload)).andRespond(withPayload(responsePayload)); OrderHistory orderHistory = orderHistoryClient.getOrderHistory("def456"); assertThat(orderHistory.getOrders().get(2).getOrderId()).isEqualTo("order6"); mockWebServiceServer.verify(); }
@Test public void testGetOrderHistoryMinimumAssumptions() throws IOException { Source requestPayload = new StringSource( "<ns1:getOrderHistoryRequest xmlns:ns1=\"http: + "<ns1:userId>ghi789</ns1:userId>" + "</ns1:getOrderHistoryRequest>"); Source responsePayload = new StringSource( "<ns1:getOrderHistoryResponse xmlns:ns1=\"http: + "<ns1:newWrapper>" + "<ns1:orderHistory>" + "<ns1:orderList>" + "<ns1:order><ns1:orderId>order7</ns1:orderId></ns1:order>" + "<ns1:order><ns1:orderId>order8</ns1:orderId></ns1:order>" + "<ns1:order><ns1:orderId>order9</ns1:orderId></ns1:order>" + "</ns1:orderList>" + "</ns1:orderHistory>" + "</ns1:newWrapper>" + "</ns1:getOrderHistoryResponse>"); mockWebServiceServer.expect(payload(requestPayload)).andRespond(withPayload(responsePayload)); OrderHistory orderHistory = orderHistoryClient.getOrderHistory("ghi789"); assertThat(orderHistory.getOrders().get(2).getOrderId()).isEqualTo("order9"); mockWebServiceServer.verify(); }