Bring airline sample up to date

This commit is contained in:
Arjen Poutsma
2011-01-05 14:45:26 +00:00
parent 7bb66cb750
commit 462ad0a27f
18 changed files with 152 additions and 912 deletions

View File

@@ -58,7 +58,7 @@
</execution>
</executions>
<configuration>
<mainClass>org.springframework.ws.samples.airline.client.saaj.GetFlights</mainClass>
<mainClass>org.springframework.ws.samples.airline.client.saaj.Driver</mainClass>
</configuration>
</plugin>
</plugins>

View File

@@ -0,0 +1,41 @@
/*
* Copyright 2005-2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.ws.samples.airline.client.saaj;
/**
* @author Arjen Poutsma
*/
public class Driver {
public static void main(String[] args) throws Exception {
String url = "http://localhost:8080/airline-server/services";
if (args.length > 0) {
url = args[0];
}
GetFlights getFlights = new GetFlights(url);
getFlights.getFlights();
if (!System.getProperty("java.version").startsWith("1.5")) {
return;
}
String username = "john";
String password = "changeme";
GetFrequentFlyerMileage getMileage = new GetFrequentFlyerMileage(url);
getMileage.getMileage(username, password);
}
}

View File

@@ -1,11 +1,11 @@
/*
* Copyright 2006 the original author or authors.
* Copyright 2005-2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -20,7 +20,6 @@ import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Iterator;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.Name;
import javax.xml.soap.SOAPBodyElement;
@@ -115,13 +114,4 @@ public class GetFlights {
transformer.transform(source, new StreamResult(System.out));
}
}
public static void main(String[] args) throws Exception {
String url = "http://localhost:8080/airline-server/services";
if (args.length > 0) {
url = args[0];
}
GetFlights getFlights = new GetFlights(url);
getFlights.getFlights();
}
}

View File

@@ -1,11 +1,11 @@
/*
* Copyright 2006 the original author or authors.
* Copyright 2005-2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -20,7 +20,6 @@ import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.UnsupportedCallbackException;
@@ -126,16 +125,4 @@ public class GetFrequentFlyerMileage {
System.err.println("SOAP Fault String: " + fault.getFaultString());
}
}
public static void main(String[] args) throws Exception {
if (!System.getProperty("java.version").startsWith("1.5")) {
System.out.println("This sample will only run under JDK 1.5");
return;
}
String url = "http://localhost:8080/airline-server/services";
String username = "john";
String password = "changeme";
GetFrequentFlyerMileage getMileage = new GetFrequentFlyerMileage(url);
getMileage.getMileage(username, password);
}
}

View File

@@ -52,7 +52,7 @@
</execution>
</executions>
<configuration>
<mainClass>org.springframework.ws.samples.airline.client.sws.GetFlights</mainClass>
<mainClass>org.springframework.ws.samples.airline.client.sws.Driver</mainClass>
</configuration>
</plugin>
</plugins>

View File

@@ -0,0 +1,39 @@
/*
* Copyright 2005-2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.ws.samples.airline.client.sws;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author Arjen Poutsma
*/
public class Driver {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
"org/springframework/ws/samples/airline/client/sws/applicationContext.xml");
GetFlights getFlights = applicationContext.getBean("getFlights", GetFlights.class);
getFlights.getFlights();
GetFrequentFlyerMileage getFrequentFlyerMileage = applicationContext
.getBean("getFrequentFlyerMileage", GetFrequentFlyerMileage.class);
getFrequentFlyerMileage.getFrequentFlyerMileage();
}
}

View File

@@ -1,11 +1,11 @@
/*
* Copyright 2005-2010 the original author or authors.
* Copyright 2005-2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -19,8 +19,6 @@ package org.springframework.ws.samples.airline.client.sws;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.springWs.samples.airline.schemas.messages.BookFlightRequestDocument;
import org.springframework.springWs.samples.airline.schemas.messages.BookFlightResponseDocument;
import org.springframework.springWs.samples.airline.schemas.messages.GetFlightsRequestDocument;
@@ -101,11 +99,4 @@ public class GetFlights extends WebServiceGatewaySupport {
System.out.println("\t" + flight.getTo().getCity());
}
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
"org/springframework/ws/samples/airline/client/sws/applicationContext.xml");
GetFlights getFlights = applicationContext.getBean("getFlights", GetFlights.class);
getFlights.getFlights();
}
}

View File

@@ -1,11 +1,11 @@
/*
* Copyright 2008 the original author or authors.
* Copyright 2005-2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -16,15 +16,11 @@
package org.springframework.ws.samples.airline.client.sws;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamResult;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.ws.WebServiceMessageFactory;
import org.springframework.ws.client.core.support.WebServiceGatewaySupport;
import org.springframework.xml.transform.StringResult;
import org.springframework.xml.transform.StringSource;
public class GetFrequentFlyerMileage extends WebServiceGatewaySupport {
@@ -41,14 +37,4 @@ public class GetFrequentFlyerMileage extends WebServiceGatewaySupport {
}
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
"org/springframework/ws/samples/airline/client/sws/applicationContext.xml");
GetFrequentFlyerMileage getFrequentFlyerMileage = applicationContext
.getBean("getFrequentFlyerMileage", GetFrequentFlyerMileage.class);
getFrequentFlyerMileage.getFrequentFlyerMileage();
}
}

View File

@@ -22,6 +22,8 @@ import java.util.List;
import javax.xml.bind.JAXBElement;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.XMLGregorianCalendar;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
@@ -49,6 +51,8 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import static org.springframework.ws.samples.airline.ws.AirlineWebServiceConstants.*;
@@ -65,6 +69,8 @@ public class AirlineEndpoint {
private final ObjectFactory objectFactory = new ObjectFactory();
private final DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
private final AirlineService airlineService;
@Autowired
@@ -156,5 +162,18 @@ public class AirlineEndpoint {
return SchemaConversionUtils.toSchemaType(domainTicket);
}
@PayloadRoot(localPart = GET_FREQUENT_FLYER_MILEAGE_REQUEST, namespace = MESSAGES_NAMESPACE)
@ResponsePayload
public Element getFrequentFlyerMileage() throws Exception {
if (logger.isDebugEnabled()) {
logger.debug("Received GetFrequentFlyerMileageRequest");
}
int mileage = airlineService.getFrequentFlyerMileage();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document document = documentBuilder.newDocument();
Element response = document.createElementNS(MESSAGES_NAMESPACE, GET_FREQUENT_FLYER_MILEAGE_RESPONSE);
response.setTextContent(Integer.toString(mileage));
return response;
}
}

View File

@@ -1,47 +0,0 @@
/*
* Copyright 2005-2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.ws.samples.airline.ws;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ws.samples.airline.service.AirlineService;
import org.springframework.ws.server.endpoint.AbstractDomPayloadEndpoint;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
/**
* Endpoint that returns the amount of frequent flyer miles for the currently logged in user. Secured via a WS-Security
* UsernameToken
*
* @author Arjen Poutsma
*/
public class GetFrequentFlyerMileageEndpoint extends AbstractDomPayloadEndpoint implements AirlineWebServiceConstants {
private final AirlineService airlineService;
@Autowired
public GetFrequentFlyerMileageEndpoint(AirlineService airlineService) {
this.airlineService = airlineService;
}
protected Element invokeInternal(Element ignored, Document responseDocument) throws Exception {
int mileage = airlineService.getFrequentFlyerMileage();
Element response = responseDocument.createElementNS(MESSAGES_NAMESPACE, GET_FREQUENT_FLYER_MILEAGE_RESPONSE);
response.setTextContent(Integer.toString(mileage));
return response;
}
}

View File

@@ -1,150 +0,0 @@
/*
* Copyright 2005-2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.ws.samples.airline.ws;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.xml.bind.JAXBElement;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.XMLGregorianCalendar;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.ws.samples.airline.domain.FrequentFlyer;
import org.springframework.ws.samples.airline.domain.Passenger;
import org.springframework.ws.samples.airline.schema.BookFlightRequest;
import org.springframework.ws.samples.airline.schema.Flight;
import org.springframework.ws.samples.airline.schema.GetFlightsRequest;
import org.springframework.ws.samples.airline.schema.GetFlightsResponse;
import org.springframework.ws.samples.airline.schema.Name;
import org.springframework.ws.samples.airline.schema.ObjectFactory;
import org.springframework.ws.samples.airline.schema.ServiceClass;
import org.springframework.ws.samples.airline.schema.Ticket;
import org.springframework.ws.samples.airline.schema.support.SchemaConversionUtils;
import org.springframework.ws.samples.airline.service.AirlineService;
import org.springframework.ws.samples.airline.service.NoSeatAvailableException;
import org.springframework.ws.samples.airline.service.NoSuchFlightException;
import org.springframework.ws.samples.airline.service.NoSuchFrequentFlyerException;
import org.springframework.ws.server.endpoint.annotation.Endpoint;
import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.joda.time.DateTime;
import org.joda.time.LocalDate;
/**
* Endpoint that handles the Airline Web Service messages using JAXB2 marshalling.
* <p/>
* This endpoint contains exactly the same logic as the {@link XPathAirlineEndpoint}, and is only provided as an
* example. Typically, you will not have two endpoints with the same logic.
*
* @author Arjen Poutsma
*/
@Endpoint
public class MarshallingAirlineEndpoint implements AirlineWebServiceConstants {
private static final Log logger = LogFactory.getLog(MarshallingAirlineEndpoint.class);
private final AirlineService airlineService;
private ObjectFactory objectFactory = new ObjectFactory();
@Autowired
public MarshallingAirlineEndpoint(AirlineService airlineService) {
Assert.notNull(airlineService, "airlineService must not be null");
this.airlineService = airlineService;
}
/**
* This endpoint method uses marshalling to handle message with a <code>&lt;GetFlightsRequest&gt;</code> payload.
*
* @param request the JAXB2 representation of a <code>&lt;GetFlightsRequest&gt;</code>
*/
@PayloadRoot(localPart = GET_FLIGHTS_REQUEST, namespace = MESSAGES_NAMESPACE)
public GetFlightsResponse getFlights(GetFlightsRequest request) throws DatatypeConfigurationException {
if (logger.isDebugEnabled()) {
logger.debug("Received GetFlightsRequest '" + request.getFrom() + "' to '" + request.getTo() + "' on " +
request.getDepartureDate());
}
List<Flight> flights = getSchemaFlights(request.getFrom(), request.getTo(), request.getDepartureDate(),
request.getServiceClass());
GetFlightsResponse response = new GetFlightsResponse();
for (Flight flight : flights) {
response.getFlight().add(flight);
}
return response;
}
/** Converts between the domain and schema types. */
private List<Flight> getSchemaFlights(String from,
String to,
XMLGregorianCalendar xmlDepartureDate,
ServiceClass xmlServiceClass) throws DatatypeConfigurationException {
LocalDate domainDepartureDate = SchemaConversionUtils.toLocalDate(xmlDepartureDate);
org.springframework.ws.samples.airline.domain.ServiceClass domainServiceClass =
SchemaConversionUtils.toDomainType(xmlServiceClass);
List<org.springframework.ws.samples.airline.domain.Flight> domainFlights =
airlineService.getFlights(from, to, domainDepartureDate, domainServiceClass);
return SchemaConversionUtils.toSchemaType(domainFlights);
}
/**
* This endpoint method uses marshalling to handle message with a <code>&lt;BookFlightRequest&gt;</code> payload.
*
* @param request the JAXB2 representation of a <code>&lt;BookFlightRequest&gt;</code>
* @return the JAXB2 representation of a <code>&lt;BookFlightResponse&gt;</code>
*/
@PayloadRoot(localPart = BOOK_FLIGHT_REQUEST, namespace = MESSAGES_NAMESPACE)
public JAXBElement<Ticket> bookFlight(BookFlightRequest request) throws NoSeatAvailableException,
DatatypeConfigurationException, NoSuchFlightException, NoSuchFrequentFlyerException {
if (logger.isDebugEnabled()) {
logger.debug("Received BookingFlightRequest '" + request.getFlightNumber() + "' on '" +
request.getDepartureTime() + "' for " + request.getPassengers().getPassengerOrUsername());
}
Ticket ticket = bookSchemaFlight(request.getFlightNumber(), request.getDepartureTime(),
request.getPassengers().getPassengerOrUsername());
return objectFactory.createBookFlightResponse(ticket);
}
/** Converts between the domain and schema types. */
private Ticket bookSchemaFlight(String flightNumber,
XMLGregorianCalendar xmlDepartureTime,
List<Object> passengerOrUsernameList) throws NoSeatAvailableException,
NoSuchFlightException, NoSuchFrequentFlyerException, DatatypeConfigurationException {
DateTime departureTime = SchemaConversionUtils.toDateTime(xmlDepartureTime);
List<Passenger> passengers = new ArrayList<Passenger>(passengerOrUsernameList.size());
for (Iterator<Object> iterator = passengerOrUsernameList.iterator(); iterator.hasNext();) {
Object passengerOrUsername = iterator.next();
if (passengerOrUsername instanceof Name) {
Name passengerName = (Name) passengerOrUsername;
Passenger passenger = new Passenger(passengerName.getFirst(), passengerName.getLast());
passengers.add(passenger);
}
else if (passengerOrUsername instanceof String) {
String frequentFlyerUsername = (String) passengerOrUsername;
FrequentFlyer frequentFlyer = new FrequentFlyer(frequentFlyerUsername);
passengers.add(frequentFlyer);
}
}
org.springframework.ws.samples.airline.domain.Ticket domainTicket =
airlineService.bookFlight(flightNumber, departureTime, passengers);
return SchemaConversionUtils.toSchemaType(domainTicket);
}
}

View File

@@ -1,172 +0,0 @@
/*
* Copyright 2005-2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.ws.samples.airline.ws;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.transform.Source;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.oxm.Marshaller;
import org.springframework.oxm.support.MarshallingSource;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.ws.samples.airline.domain.Flight;
import org.springframework.ws.samples.airline.domain.FrequentFlyer;
import org.springframework.ws.samples.airline.domain.Passenger;
import org.springframework.ws.samples.airline.domain.ServiceClass;
import org.springframework.ws.samples.airline.domain.Ticket;
import org.springframework.ws.samples.airline.schema.GetFlightsResponse;
import org.springframework.ws.samples.airline.schema.ObjectFactory;
import org.springframework.ws.samples.airline.schema.support.SchemaConversionUtils;
import org.springframework.ws.samples.airline.service.AirlineService;
import org.springframework.ws.samples.airline.service.NoSeatAvailableException;
import org.springframework.ws.samples.airline.service.NoSuchFlightException;
import org.springframework.ws.samples.airline.service.NoSuchFrequentFlyerException;
import org.springframework.ws.server.endpoint.annotation.Endpoint;
import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
import org.springframework.ws.server.endpoint.annotation.XPathParam;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
/**
* Endpoint that handles the Airline Web Service request messages using XPath expressions, and creates response messages
* using JAXB2.
* <p/>
* This endpoint contains exactly the same logic as the {@link MarshallingAirlineEndpoint}, and is only provided as an
* example. Typically, you will not have two endpoints with the same logic.
*
* @author Arjen Poutsma
*/
@Endpoint
public class XPathAirlineEndpoint implements AirlineWebServiceConstants {
private static final Log logger = LogFactory.getLog(XPathAirlineEndpoint.class);
private final AirlineService airlineService;
private ObjectFactory objectFactory = new ObjectFactory();
private final Marshaller marshaller;
@Autowired
public XPathAirlineEndpoint(AirlineService airlineService, Marshaller marshaller) {
Assert.notNull(airlineService, "airlineService must not be null");
Assert.notNull(marshaller, "'marshaller' must not be null");
this.airlineService = airlineService;
this.marshaller = marshaller;
}
/**
* This endpoint method uses XPath to handle message with a <code>&lt;GetFlightsRequest&gt;</code> payload.
*
* @param from the from airport
* @param to the to airport
* @param departureDateString the string representation of the departure date
* @param serviceClassString the string representation of the service class
*/
@PayloadRoot(localPart = GET_FLIGHTS_REQUEST, namespace = MESSAGES_NAMESPACE)
public Source getFlights(@XPathParam("//messages:from")String from,
@XPathParam("//messages:to")String to,
@XPathParam("//messages:departureDate")String departureDateString,
@XPathParam("//messages:serviceClass")String serviceClassString)
throws DatatypeConfigurationException {
if (logger.isDebugEnabled()) {
logger.debug("Received GetFlightsRequest '" + from + "' to '" + to + "' on " + departureDateString);
}
LocalDate departureDate = new LocalDate(departureDateString);
ServiceClass serviceClass = null;
if (StringUtils.hasLength(serviceClassString)) {
serviceClass = ServiceClass.valueOf(serviceClassString.toUpperCase());
}
List<Flight> flights = airlineService.getFlights(from, to, departureDate, serviceClass);
GetFlightsResponse response = objectFactory.createGetFlightsResponse();
for (Flight domainFlight : flights) {
response.getFlight().add(SchemaConversionUtils.toSchemaType(domainFlight));
}
return new MarshallingSource(marshaller, response);
}
/**
* This endpoint method uses XPath to handle message with a <code>&lt;BookFlightRequest&gt;</code> payload.
*
* @param flightNumber the flight number
* @param departureTimeString the string representation of the departure time
* @param passengerNodes the passenger nodes
* @param frequentFlyerNodes the frequent flyer nodes
*/
@PayloadRoot(localPart = BOOK_FLIGHT_REQUEST, namespace = MESSAGES_NAMESPACE)
public Source bookFlight(@XPathParam("//messages:flightNumber")String flightNumber,
@XPathParam("//messages:departureTime")String departureTimeString,
@XPathParam("//messages:passengers/messages:passenger")NodeList passengerNodes,
@XPathParam("//messages:passengers/messages:username")NodeList frequentFlyerNodes) throws
NoSeatAvailableException, NoSuchFlightException, NoSuchFrequentFlyerException,
DatatypeConfigurationException, JAXBException {
if (logger.isDebugEnabled()) {
logger.debug("Received BookingFlightRequest '" + flightNumber + "' on '" + departureTimeString + "' for " +
passengerNodes.getLength() + " passengers and " + frequentFlyerNodes.getLength() +
" frequent flyers");
}
DateTime departureTime = new DateTime(departureTimeString);
List<Passenger> passengers = new ArrayList<Passenger>();
parsePassengers(passengerNodes, passengers);
parseFrequentFlyers(frequentFlyerNodes, passengers);
Ticket domainTicket = airlineService.bookFlight(flightNumber, departureTime, passengers);
JAXBElement<org.springframework.ws.samples.airline.schema.Ticket> response =
objectFactory.createBookFlightResponse(SchemaConversionUtils.toSchemaType(domainTicket));
return new MarshallingSource(marshaller, response);
}
private void parsePassengers(NodeList passengerNodes, List<Passenger> passengers) {
for (int i = 0; i < passengerNodes.getLength(); i++) {
if (passengerNodes.item(i).getNodeType() != Node.ELEMENT_NODE) {
continue;
}
Element passengerElement = (Element) passengerNodes.item(i);
Element firstNameElement =
(Element) passengerElement.getElementsByTagNameNS(MESSAGES_NAMESPACE, "first").item(0);
Element lastNameElement =
(Element) passengerElement.getElementsByTagNameNS(MESSAGES_NAMESPACE, "last").item(0);
Passenger passenger = new Passenger(firstNameElement.getTextContent(), lastNameElement.getTextContent());
passengers.add(passenger);
}
}
private void parseFrequentFlyers(NodeList frequentFlyerNodes, List<Passenger> passengers) {
for (int i = 0; i < frequentFlyerNodes.getLength(); i++) {
if (frequentFlyerNodes.item(i).getNodeType() != Node.ELEMENT_NODE) {
continue;
}
Element frequentFlyerElement = (Element) frequentFlyerNodes.item(i);
FrequentFlyer frequentFlyer = new FrequentFlyer(frequentFlyerElement.getTextContent());
passengers.add(frequentFlyer);
}
}
}

View File

@@ -22,20 +22,5 @@
</description>
</bean>
<bean id="wsSecurityInterceptor" class="org.springframework.ws.soap.security.xwss.XwsSecurityInterceptor">
<description>
This interceptor validates incoming messages according to the policy defined in 'securityPolicy.xml'.
The policy defines that all incoming requests must have a UsernameToken with a password digest in it.
The actual authentication is performed by the Spring Security callback handler.
</description>
<property name="secureResponse" value="false"/>
<property name="policyConfiguration"
value="classpath:org/springframework/ws/samples/airline/security/securityPolicy.xml"/>
<property name="callbackHandler">
<bean class="org.springframework.ws.soap.security.xwss.callback.SpringDigestPasswordValidationCallbackHandler">
<property name="userDetailsService" ref="securityService"/>
</bean>
</property>
</bean>
</beans>

View File

@@ -1,14 +1,37 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:oxm="http://www.springframework.org/schema/oxm"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:sws="http://www.springframework.org/schema/web-services"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/web-services http://www.springframework.org/schema/web-services/web-services-2.0.xsd">
<sws:annotation-driven />
<sws:interceptors>
<bean class="org.springframework.ws.server.endpoint.interceptor.PayloadLoggingInterceptor"/>
<bean class="org.springframework.ws.soap.server.endpoint.interceptor.PayloadValidatingInterceptor">
<property name="xsdSchemaCollection" ref="schemaCollection"/>
<property name="validateRequest" value="true"/>
<property name="validateResponse" value="true"/>
</bean>
<sws:payloadRoot localPart="GetFrequentFlyerMileageRequest" namespaceUri="http://www.springframework.org/spring-ws/samples/airline/schemas/messages">
<bean class="org.springframework.ws.soap.security.xwss.XwsSecurityInterceptor">
<property name="secureResponse" value="false"/>
<property name="policyConfiguration"
value="classpath:org/springframework/ws/samples/airline/security/securityPolicy.xml"/>
<property name="callbackHandler">
<bean class="org.springframework.ws.soap.security.xwss.callback.SpringDigestPasswordValidationCallbackHandler">
<property name="userDetailsService" ref="securityService"/>
</bean>
</property>
</bean>
</sws:payloadRoot>
</sws:interceptors>
<context:component-scan base-package="org.springframework.ws.samples.airline.ws"/>
<bean id="messageFactory" class="org.springframework.ws.soap.saaj.SaajSoapMessageFactory"/>
<bean id="messageReceiver" class="org.springframework.ws.soap.server.SoapMessageDispatcher"/>
@@ -21,148 +44,4 @@
<property name="inline" value="true"/>
</bean>
<!-- ===================== ENDPOINTS ===================================== -->
<bean id="airlineEndpoint" class="org.springframework.ws.samples.airline.ws.AirlineEndpoint"/>
<!--
The marshallingEndpoint and xpathEndpoint handle the same messages. So, you can only use one of them at the
same time. This is done for illustration purposes only, typically you would not create two endpoints which
handle the same messages.
-->
<!--
<bean id="marshallingEndpoint" class="org.springframework.ws.samples.airline.ws.MarshallingAirlineEndpoint">
<description>
This endpoint handles the Airline Web Service messages using JAXB2 marshalling.
</description>
</bean>
-->
<!--
<bean id="xpathEndpoint" class="org.springframework.ws.samples.airline.ws.XPathAirlineEndpoint">
<description>
This endpoint handles the Airline Web Service messages using XPath expressions and JAXB2 marshalling.
</description>
</bean>
-->
<bean id="getFrequentFlyerMileageEndpoint"
class="org.springframework.ws.samples.airline.ws.GetFrequentFlyerMileageEndpoint">
<description>
This endpoint handles get frequent flyer mileage requests.
</description>
</bean>
<oxm:jaxb2-marshaller id="marshaller" contextPath="org.springframework.ws.samples.airline.schema"/>
<!-- ===================== ENDPOINT MAPPINGS ============================== -->
<!--
The endpoint mappings map from a request to an endpoint. Because we only want the security interception to
occur for the GetFrequentFlyerMileageEndpoint, we define two mappings: one with the securityInterceptor, and
a general one without it.
-->
<bean id="annotationMapping"
class="org.springframework.ws.server.endpoint.mapping.PayloadRootAnnotationMethodEndpointMapping">
<description>
Detects @PayloadRoot annotations on @Endpoint bean methods. The MarshallingAirlineEndpoint
has such annotations. It uses two interceptors: one that logs the message payload, and the other validates
it accoring to the 'airline.xsd' schema file.
</description>
<property name="interceptors">
<list>
<bean class="org.springframework.ws.server.endpoint.interceptor.PayloadLoggingInterceptor"/>
<bean class="org.springframework.ws.soap.server.endpoint.interceptor.PayloadValidatingInterceptor">
<property name="xsdSchemaCollection" ref="schemaCollection"/>
<property name="validateRequest" value="true"/>
<property name="validateResponse" value="true"/>
</bean>
</list>
</property>
<property name="order" value="1"/>
</bean>
<bean id="secureMapping" class="org.springframework.ws.server.endpoint.mapping.PayloadRootQNameEndpointMapping">
<description>
This endpoint mapping is used for endpoints that are secured via WS-Security. It uses a
securityInterceptor, defined in applicationContext-security.xml, to validate incoming messages.
</description>
<property name="mappings">
<props>
<prop key="{http://www.springframework.org/spring-ws/samples/airline/schemas/messages}GetFrequentFlyerMileageRequest">
getFrequentFlyerMileageEndpoint
</prop>
</props>
</property>
<property name="interceptors">
<list>
<bean class="org.springframework.ws.soap.server.endpoint.interceptor.SoapEnvelopeLoggingInterceptor"/>
<ref bean="wsSecurityInterceptor"/>
</list>
</property>
<property name="order" value="2"/>
</bean>
<!-- ===================== ENDPOINT ADAPTERS ============================== -->
<!--
Endpoint adapters adapt from the incoming message to a specific object or method signature. Because this
example application uses three different endpoint programming models, we have to define three adapters. This
is done for illustration purposes only, typically you would use one adapter, for instance the
MarshallingMethodEndpointAdapter.
-->
<!--
<sws:marshalling-endpoints/>
<sws:xpath-endpoints>
<sws:namespace prefix="messages"
uri="http://www.springframework.org/spring-ws/samples/airline/schemas/messages"/>
</sws:xpath-endpoints>
-->
<bean class="org.springframework.ws.server.endpoint.adapter.PayloadEndpointAdapter">
<description>
This adapter allows for endpoints which implement the PayloadEndpoint interface. The Get
FrequentFlyerMileageEndpoint implements this interface.
</description>
</bean>
<!-- ===================== ENDPOINT EXCEPTION RESOLVER ===================== -->
<!--
Endpoint exception resolvers can handle exceptions as they occur in the Web service. We have two sorts of
exceptions we want to handle: the business logic exceptions NoSeatAvailableException and NoSuchFlightException,
which both have a @SoapFault annotation, and other exceptions, which don't have the annotation. Therefore, we
have two exception resolvers here.
-->
<bean class="org.springframework.ws.soap.server.endpoint.SoapFaultAnnotationExceptionResolver">
<description>
This exception resolver maps exceptions with the @SoapFault annotation to SOAP Faults. The business logic
exceptions NoSeatAvailableException and NoSuchFlightException have these.
</description>
<property name="order" value="1"/>
</bean>
<bean class="org.springframework.ws.soap.server.endpoint.SoapFaultMappingExceptionResolver">
<description>
This exception resolver maps other exceptions to SOAP Faults. Both UnmarshallingException and
ValidationFailureException are mapped to a SOAP Fault with a "Client" fault code.
All other exceptions are mapped to a "Server" error code, the default.
</description>
<property name="defaultFault" value="SERVER"/>
<property name="exceptionMappings">
<props>
<prop key="org.springframework.oxm.UnmarshallingFailureException">CLIENT,Invalid request</prop>
<prop key="org.springframework.oxm.ValidationFailureException">CLIENT,Invalid request</prop>
</props>
</property>
<property name="order" value="2"/>
</bean>
</beans>

View File

@@ -43,8 +43,10 @@ import org.joda.time.LocalDate;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.w3c.dom.Element;
import static org.easymock.EasyMock.*;
import static org.junit.Assert.assertNotNull;
public class AirlineEndpointTest {
@@ -110,7 +112,7 @@ public class AirlineEndpointTest {
}
@Test
public void testBookFlightPassenger() throws Exception {
public void bookFlightPassenger() throws Exception {
BookFlightRequest request = objectFactory.createBookFlightRequest();
request.setDepartureTime(datatypeFactory.newXMLGregorianCalendar(2007, 6, 13, 12, 0, 0, 0, 0));
request.setFlightNumber("ABC1234");
@@ -169,8 +171,9 @@ public class AirlineEndpointTest {
domainTicket.setPassengers(domainPassengers);
List<Passenger> domainPassengerList = new ArrayList<Passenger>(domainPassengers);
expect(airlineServiceMock.bookFlight("ABC1234", new DateTime(2007, 6, 13, 12, 0, 0, 0, DateTimeZone.UTC),
domainPassengerList)).andReturn(domainTicket);
expect(airlineServiceMock
.bookFlight("ABC1234", new DateTime(2007, 6, 13, 12, 0, 0, 0, DateTimeZone.UTC), domainPassengerList))
.andReturn(domainTicket);
replay(airlineServiceMock);
@@ -189,4 +192,16 @@ public class AirlineEndpointTest {
verify(airlineServiceMock);
}
@Test
public void testGetFrequentFlyerMileage() throws Exception {
expect(airlineServiceMock.getFrequentFlyerMileage()).andReturn(42);
replay(airlineServiceMock);
Element response = endpoint.getFrequentFlyerMileage();
assertNotNull("Invalid response", response);
verify(airlineServiceMock);
}
}

View File

@@ -1,56 +0,0 @@
/*
* Copyright 2005-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.ws.samples.airline.ws;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import junit.framework.TestCase;
import static org.easymock.EasyMock.*;
import org.springframework.ws.samples.airline.service.AirlineService;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
public class GetFrequentFlyerMileageEndpointTest extends TestCase {
private GetFrequentFlyerMileageEndpoint endpoint;
private AirlineService airlineServiceMock;
private Document document;
@Override
protected void setUp() throws Exception {
airlineServiceMock = createMock(AirlineService.class);
endpoint = new GetFrequentFlyerMileageEndpoint(airlineServiceMock);
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
document = documentBuilder.newDocument();
}
public void testGetFrequentFlyerMileage() throws Exception {
expect(airlineServiceMock.getFrequentFlyerMileage()).andReturn(42);
replay(airlineServiceMock);
Element response = endpoint.invokeInternal(null, document);
assertNotNull("Invalid response", response);
verify(airlineServiceMock);
}
}

View File

@@ -1,193 +0,0 @@
/*
* Copyright 2005-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.ws.samples.airline.ws;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.xml.bind.JAXBElement;
import javax.xml.datatype.DatatypeConstants;
import javax.xml.datatype.DatatypeFactory;
import junit.framework.TestCase;
import static org.easymock.EasyMock.*;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.LocalDate;
import org.springframework.ws.samples.airline.domain.Airport;
import org.springframework.ws.samples.airline.domain.FrequentFlyer;
import org.springframework.ws.samples.airline.domain.Passenger;
import org.springframework.ws.samples.airline.schema.BookFlightRequest;
import org.springframework.ws.samples.airline.schema.Flight;
import org.springframework.ws.samples.airline.schema.GetFlightsRequest;
import org.springframework.ws.samples.airline.schema.GetFlightsResponse;
import org.springframework.ws.samples.airline.schema.Name;
import org.springframework.ws.samples.airline.schema.ObjectFactory;
import org.springframework.ws.samples.airline.schema.ServiceClass;
import org.springframework.ws.samples.airline.schema.Ticket;
import org.springframework.ws.samples.airline.service.AirlineService;
public class MarshallingAirlineEndpointTest extends TestCase {
private MarshallingAirlineEndpoint endpoint;
private AirlineService airlineServiceMock;
private DatatypeFactory datatypeFactory;
private ObjectFactory objectFactory;
@Override
protected void setUp() throws Exception {
airlineServiceMock = createMock(AirlineService.class);
endpoint = new MarshallingAirlineEndpoint(airlineServiceMock);
datatypeFactory = DatatypeFactory.newInstance();
objectFactory = new ObjectFactory();
}
public void testGetFlights() throws Exception {
GetFlightsRequest request = objectFactory.createGetFlightsRequest();
request.setDepartureDate(
datatypeFactory.newXMLGregorianCalendarDate(2007, 6, 13, DatatypeConstants.FIELD_UNDEFINED));
request.setFrom("ABC");
request.setTo("DEF");
request.setServiceClass(ServiceClass.FIRST);
org.springframework.ws.samples.airline.domain.Flight domainFlight = createDomainFlight();
expect(airlineServiceMock.getFlights("ABC", "DEF", new LocalDate(2007, 6, 13),
org.springframework.ws.samples.airline.domain.ServiceClass.FIRST))
.andReturn(Collections.singletonList(domainFlight));
replay(airlineServiceMock);
GetFlightsResponse response = endpoint.getFlights(request);
assertEquals("Invalid amount of flights received", 1, response.getFlight().size());
Flight schemaFlight = response.getFlight().get(0);
verifySchemaFlight(schemaFlight);
verify(airlineServiceMock);
}
private void verifySchemaFlight(Flight schemaFlight) {
assertEquals("Invalid number", "ABC1234", schemaFlight.getNumber());
assertEquals("Invalid departure time", datatypeFactory.newXMLGregorianCalendar(2007, 6, 13, 12, 0, 0, 0, 0),
schemaFlight.getDepartureTime());
assertEquals("Invalid from code", "ABC", schemaFlight.getFrom().getCode());
assertEquals("Invalid from name", "ABC Airport", schemaFlight.getFrom().getName());
assertEquals("Invalid from city", "ABC City", schemaFlight.getFrom().getCity());
assertEquals("Invalid arrival time", datatypeFactory.newXMLGregorianCalendar(2007, 6, 13, 14, 0, 0, 0, 0),
schemaFlight.getArrivalTime());
assertEquals("Invalid to code", "DEF", schemaFlight.getTo().getCode());
assertEquals("Invalid to name", "DEF Airport", schemaFlight.getTo().getName());
assertEquals("Invalid to city", "DEF City", schemaFlight.getTo().getCity());
assertEquals("Invalid service class", ServiceClass.FIRST, schemaFlight.getServiceClass());
}
private org.springframework.ws.samples.airline.domain.Flight createDomainFlight() {
org.springframework.ws.samples.airline.domain.Flight domainFlight =
new org.springframework.ws.samples.airline.domain.Flight();
domainFlight.setNumber("ABC1234");
domainFlight.setDepartureTime(new DateTime(2007, 6, 13, 12, 0, 0, 0, DateTimeZone.UTC));
domainFlight.setFrom(new Airport("ABC", "ABC Airport", "ABC City"));
domainFlight.setArrivalTime(new DateTime(2007, 6, 13, 14, 0, 0, 0, DateTimeZone.UTC));
domainFlight.setTo(new Airport("DEF", "DEF Airport", "DEF City"));
domainFlight.setServiceClass(org.springframework.ws.samples.airline.domain.ServiceClass.FIRST);
return domainFlight;
}
public void testBookFlightPassenger() throws Exception {
BookFlightRequest request = objectFactory.createBookFlightRequest();
request.setDepartureTime(datatypeFactory.newXMLGregorianCalendar(2007, 6, 13, 12, 0, 0, 0, 0));
request.setFlightNumber("ABC1234");
Name passengerName = new Name();
passengerName.setFirst("John");
passengerName.setLast("Doe");
BookFlightRequest.Passengers passengers = new BookFlightRequest.Passengers();
passengers.getPassengerOrUsername().add(passengerName);
request.setPassengers(passengers);
Passenger domainPassenger = new Passenger("John", "Doe");
org.springframework.ws.samples.airline.domain.Ticket domainTicket =
new org.springframework.ws.samples.airline.domain.Ticket(42L);
domainTicket.setFlight(createDomainFlight());
domainTicket.setIssueDate(new LocalDate(2007, 6, 13));
domainTicket.setPassengers(Collections.singleton(domainPassenger));
expect(airlineServiceMock.bookFlight("ABC1234", new DateTime(2007, 6, 13, 12, 0, 0, 0, DateTimeZone.UTC),
Collections.singletonList(domainPassenger))).andReturn(domainTicket);
replay(airlineServiceMock);
JAXBElement<Ticket> response = endpoint.bookFlight(request);
Ticket schemaTicket = response.getValue();
assertEquals("Invalid id", 42L, schemaTicket.getId());
assertEquals("Invalid issue date",
datatypeFactory.newXMLGregorianCalendarDate(2007, 6, 13, DatatypeConstants.FIELD_UNDEFINED),
schemaTicket.getIssueDate());
assertEquals("Invalid amount of passengers", 1, schemaTicket.getPassengers().getPassenger().size());
Name schemaPassenger = schemaTicket.getPassengers().getPassenger().get(0);
assertEquals("Invalid passenger first name", "John", schemaPassenger.getFirst());
assertEquals("Invalid passenger first name", "Doe", schemaPassenger.getLast());
verifySchemaFlight(schemaTicket.getFlight());
verify(airlineServiceMock);
}
public void testBookFlightFrequentFlyer() throws Exception {
BookFlightRequest request = objectFactory.createBookFlightRequest();
request.setDepartureTime(datatypeFactory.newXMLGregorianCalendar(2007, 6, 13, 12, 0, 0, 0, 0));
request.setFlightNumber("ABC1234");
BookFlightRequest.Passengers passengers = new BookFlightRequest.Passengers();
passengers.getPassengerOrUsername().add("john");
request.setPassengers(passengers);
FrequentFlyer domainFrequentFlyer = new FrequentFlyer("John", "Doe", "john", "changeme");
Set<Passenger> domainPassengers = new HashSet<Passenger>();
domainPassengers.add(domainFrequentFlyer);
org.springframework.ws.samples.airline.domain.Ticket domainTicket =
new org.springframework.ws.samples.airline.domain.Ticket(42L);
domainTicket.setFlight(createDomainFlight());
domainTicket.setIssueDate(new LocalDate(2007, 6, 13));
domainTicket.setPassengers(domainPassengers);
List<Passenger> domainPassengerList = new ArrayList<Passenger>(domainPassengers);
expect(airlineServiceMock.bookFlight("ABC1234", new DateTime(2007, 6, 13, 12, 0, 0, 0, DateTimeZone.UTC),
domainPassengerList)).andReturn(domainTicket);
replay(airlineServiceMock);
JAXBElement<Ticket> response = endpoint.bookFlight(request);
Ticket schemaTicket = response.getValue();
assertEquals("Invalid id", 42L, schemaTicket.getId());
assertEquals("Invalid issue date",
datatypeFactory.newXMLGregorianCalendarDate(2007, 6, 13, DatatypeConstants.FIELD_UNDEFINED),
schemaTicket.getIssueDate());
assertEquals("Invalid amount of passengers", 1, schemaTicket.getPassengers().getPassenger().size());
Name schemaPassenger = schemaTicket.getPassengers().getPassenger().get(0);
assertEquals("Invalid passenger first name", "John", schemaPassenger.getFirst());
assertEquals("Invalid passenger first name", "Doe", schemaPassenger.getLast());
verifySchemaFlight(schemaTicket.getFlight());
verify(airlineServiceMock);
}
}

View File

@@ -1,74 +0,0 @@
/*
* Copyright 2005-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.ws.samples.airline.ws;
import java.util.Collections;
import javax.xml.transform.Source;
import junit.framework.TestCase;
import static org.easymock.EasyMock.*;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.LocalDate;
import org.springframework.oxm.Marshaller;
import org.springframework.ws.samples.airline.domain.Airport;
import org.springframework.ws.samples.airline.service.AirlineService;
public class XPathAirlineEndpointTest extends TestCase {
private XPathAirlineEndpoint endpoint;
private AirlineService airlineServiceMock;
private Marshaller marshallerMock;
@Override
protected void setUp() throws Exception {
airlineServiceMock = createMock(AirlineService.class);
marshallerMock = createMock(Marshaller.class);
endpoint = new XPathAirlineEndpoint(airlineServiceMock, marshallerMock);
}
public void testGetFlights() throws Exception {
org.springframework.ws.samples.airline.domain.Flight domainFlight = createDomainFlight();
expect(airlineServiceMock.getFlights("ABC", "DEF", new LocalDate(2007, 6, 13),
org.springframework.ws.samples.airline.domain.ServiceClass.FIRST))
.andReturn(Collections.singletonList(domainFlight));
replay(airlineServiceMock, marshallerMock);
Source response = endpoint.getFlights("ABC", "DEF", "2007-06-13", "first");
assertNotNull("No response received", response);
verify(airlineServiceMock, marshallerMock);
}
private org.springframework.ws.samples.airline.domain.Flight createDomainFlight() {
org.springframework.ws.samples.airline.domain.Flight domainFlight =
new org.springframework.ws.samples.airline.domain.Flight();
domainFlight.setNumber("ABC1234");
domainFlight.setDepartureTime(new DateTime(2007, 6, 13, 12, 0, 0, 0, DateTimeZone.UTC));
domainFlight.setFrom(new Airport("ABC", "ABC Airport", "ABC City"));
domainFlight.setArrivalTime(new DateTime(2007, 6, 13, 14, 0, 0, 0, DateTimeZone.UTC));
domainFlight.setTo(new Airport("DEF", "DEF Airport", "DEF City"));
domainFlight.setServiceClass(org.springframework.ws.samples.airline.domain.ServiceClass.FIRST);
return domainFlight;
}
}