Upgrade to Spring Boot + Spring Data.
Update this repository to use modern Spring practices including: * Spring Boot * Spring Data JPA * Spring Framework's Java configuration
This commit is contained in:
12
tutorial/src/main/java/com/mycompany/hr/HrApplication.java
Normal file
12
tutorial/src/main/java/com/mycompany/hr/HrApplication.java
Normal file
@@ -0,0 +1,12 @@
|
||||
package com.mycompany.hr;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class HrApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(HrApplication.class, args);
|
||||
}
|
||||
}
|
||||
@@ -1,24 +1,20 @@
|
||||
package com.mycompany.hr.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.ws.config.annotation.EnableWs;
|
||||
import org.springframework.ws.wsdl.wsdl11.DefaultWsdl11Definition;
|
||||
import org.springframework.xml.xsd.commons.CommonsXsdSchemaCollection;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
*/
|
||||
@EnableWs
|
||||
@Configuration
|
||||
@ComponentScan("com.mycompany.hr")
|
||||
public class HRConfiguration {
|
||||
|
||||
@Bean
|
||||
public DefaultWsdl11Definition holiday() {
|
||||
|
||||
DefaultWsdl11Definition definition = new DefaultWsdl11Definition();
|
||||
definition.setPortTypeName("HumanResource");
|
||||
definition.setLocationUri("/holidayService/");
|
||||
@@ -29,12 +25,9 @@ public class HRConfiguration {
|
||||
|
||||
@Bean
|
||||
public CommonsXsdSchemaCollection holidayXsd() {
|
||||
CommonsXsdSchemaCollection collection =
|
||||
new CommonsXsdSchemaCollection(new Resource[] { new ClassPathResource("/hr.xsd")});
|
||||
|
||||
CommonsXsdSchemaCollection collection = new CommonsXsdSchemaCollection(new ClassPathResource("/hr.xsd"));
|
||||
collection.setInline(true);
|
||||
return collection;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,30 +0,0 @@
|
||||
package com.mycompany.hr.config;
|
||||
|
||||
import org.springframework.ws.transport.http.support.AbstractAnnotationConfigMessageDispatcherServletInitializer;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
*/
|
||||
public class HRServletInitializer
|
||||
extends AbstractAnnotationConfigMessageDispatcherServletInitializer {
|
||||
|
||||
@Override
|
||||
protected Class<?>[] getRootConfigClasses() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Class<?>[] getServletConfigClasses() {
|
||||
return new Class<?>[]{HRConfiguration.class};
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isTransformWsdlLocations() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String[] getServletMappings() {
|
||||
return new String[]{"/*"};
|
||||
}
|
||||
}
|
||||
@@ -18,21 +18,19 @@ package com.mycompany.hr.ws;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Arrays;
|
||||
import java.util.Date;
|
||||
|
||||
import org.jdom2.Element;
|
||||
import org.jdom2.Namespace;
|
||||
import org.jdom2.filter.Filters;
|
||||
import org.jdom2.xpath.XPathExpression;
|
||||
import org.jdom2.xpath.XPathFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.ws.server.endpoint.annotation.Endpoint;
|
||||
import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
|
||||
import org.springframework.ws.server.endpoint.annotation.RequestPayload;
|
||||
|
||||
import com.mycompany.hr.service.HumanResourceService;
|
||||
import org.jdom2.Element;
|
||||
import org.jdom2.JDOMException;
|
||||
import org.jdom2.Namespace;
|
||||
import org.jdom2.filter.Filters;
|
||||
import org.jdom2.xpath.XPathExpression;
|
||||
import org.jdom2.xpath.XPathFactory;
|
||||
|
||||
/**
|
||||
* This endpoint handles holiday requests. It uses a combination of JDOM and XPath to extract interesting pieces of XML
|
||||
@@ -43,46 +41,51 @@ import org.jdom2.xpath.XPathFactory;
|
||||
@Endpoint
|
||||
public class HolidayEndpoint {
|
||||
|
||||
private static final String NAMESPACE_URI = "http://mycompany.com/hr/schemas";
|
||||
private static final String NAMESPACE_URI = "http://mycompany.com/hr/schemas";
|
||||
|
||||
private XPathExpression<Element> startDateExpression;
|
||||
private XPathExpression<Element> startDateExpression;
|
||||
|
||||
private XPathExpression<Element> endDateExpression;
|
||||
private XPathExpression<Element> endDateExpression;
|
||||
|
||||
private XPathExpression<Element> firstNameExpression;
|
||||
private XPathExpression<Element> firstNameExpression;
|
||||
|
||||
private XPathExpression<Element> lastNameExpression;
|
||||
private XPathExpression<Element> lastNameExpression;
|
||||
|
||||
private HumanResourceService humanResourceService;
|
||||
private HumanResourceService humanResourceService;
|
||||
|
||||
@Autowired
|
||||
public HolidayEndpoint(HumanResourceService humanResourceService) throws JDOMException {
|
||||
this.humanResourceService = humanResourceService;
|
||||
Namespace namespace = Namespace.getNamespace("hr", NAMESPACE_URI);
|
||||
XPathFactory xPathFactory = XPathFactory.instance();
|
||||
startDateExpression = xPathFactory.compile("//hr:StartDate", Filters.element(), null, namespace);
|
||||
endDateExpression = xPathFactory.compile("//hr:EndDate", Filters.element(), null, namespace);
|
||||
firstNameExpression = xPathFactory.compile("//hr:FirstName", Filters.element(), null, namespace);
|
||||
lastNameExpression = xPathFactory.compile("//hr:LastName", Filters.element(), null, namespace);
|
||||
}
|
||||
@Autowired
|
||||
public HolidayEndpoint(HumanResourceService humanResourceService) {
|
||||
|
||||
@PayloadRoot(namespace = NAMESPACE_URI, localPart = "HolidayRequest")
|
||||
public void handleHolidayRequest(@RequestPayload Element holidayRequest) throws Exception {
|
||||
Date startDate = parseDate(startDateExpression, holidayRequest);
|
||||
Date endDate = parseDate(endDateExpression, holidayRequest);
|
||||
String name = firstNameExpression.evaluateFirst(holidayRequest).getText() + " " + lastNameExpression.evaluateFirst(holidayRequest).getText();
|
||||
this.humanResourceService = humanResourceService;
|
||||
Namespace namespace = Namespace.getNamespace("hr", NAMESPACE_URI);
|
||||
XPathFactory xPathFactory = XPathFactory.instance();
|
||||
startDateExpression = xPathFactory.compile("//hr:StartDate", Filters.element(), null, namespace);
|
||||
endDateExpression = xPathFactory.compile("//hr:EndDate", Filters.element(), null, namespace);
|
||||
firstNameExpression = xPathFactory.compile("//hr:FirstName", Filters.element(), null, namespace);
|
||||
lastNameExpression = xPathFactory.compile("//hr:LastName", Filters.element(), null, namespace);
|
||||
}
|
||||
|
||||
humanResourceService.bookHoliday(startDate, endDate, name);
|
||||
}
|
||||
@PayloadRoot(namespace = NAMESPACE_URI, localPart = "HolidayRequest")
|
||||
public void handleHolidayRequest(@RequestPayload Element holidayRequest) throws Exception {
|
||||
|
||||
private Date parseDate(XPathExpression<Element> expression, Element element) throws ParseException {
|
||||
Element result = expression.evaluateFirst(element);
|
||||
if (result != null) {
|
||||
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
|
||||
return dateFormat.parse(result.getText());
|
||||
} else {
|
||||
throw new IllegalArgumentException("Could not evaluate [" + expression + "] on [" + element + "]");
|
||||
}
|
||||
}
|
||||
Date startDate = parseDate(startDateExpression, holidayRequest);
|
||||
Date endDate = parseDate(endDateExpression, holidayRequest);
|
||||
String name = firstNameExpression.evaluateFirst(holidayRequest).getText() + " "
|
||||
+ lastNameExpression.evaluateFirst(holidayRequest).getText();
|
||||
|
||||
humanResourceService.bookHoliday(startDate, endDate, name);
|
||||
}
|
||||
|
||||
private Date parseDate(XPathExpression<Element> expression, Element element) throws ParseException {
|
||||
|
||||
Element result = expression.evaluateFirst(element);
|
||||
|
||||
if (result != null) {
|
||||
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
|
||||
return dateFormat.parse(result.getText());
|
||||
} else {
|
||||
throw new IllegalArgumentException("Could not evaluate [" + expression + "] on [" + element + "]");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -16,56 +16,58 @@
|
||||
|
||||
package com.mycompany.hr.ws;
|
||||
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.util.Calendar;
|
||||
|
||||
import com.mycompany.hr.service.HumanResourceService;
|
||||
import org.jdom2.Document;
|
||||
import org.jdom2.input.SAXBuilder;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.easymock.EasyMock.*;
|
||||
import com.mycompany.hr.service.HumanResourceService;
|
||||
|
||||
public class HolidayEndpointTest {
|
||||
|
||||
private Document holidayRequest;
|
||||
private Document holidayRequest;
|
||||
|
||||
private HolidayEndpoint endpoint;
|
||||
private HolidayEndpoint endpoint;
|
||||
|
||||
private HumanResourceService serviceMock;
|
||||
private HumanResourceService serviceMock;
|
||||
|
||||
private Calendar startCalendar;
|
||||
private Calendar startCalendar;
|
||||
|
||||
private Calendar endCalendar;
|
||||
private Calendar endCalendar;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
serviceMock = createMock(HumanResourceService.class);
|
||||
SAXBuilder builder = new SAXBuilder();
|
||||
InputStream is = getClass().getResourceAsStream("holidayRequest.xml");
|
||||
try {
|
||||
holidayRequest = builder.build(is);
|
||||
}
|
||||
finally {
|
||||
is.close();
|
||||
}
|
||||
endpoint = new HolidayEndpoint(serviceMock);
|
||||
startCalendar = Calendar.getInstance();
|
||||
startCalendar.clear();
|
||||
startCalendar.set(2006, Calendar.JULY, 3);
|
||||
endCalendar = Calendar.getInstance();
|
||||
endCalendar.clear();
|
||||
endCalendar.set(2006, Calendar.JULY, 7);
|
||||
}
|
||||
@BeforeEach
|
||||
public void setUp() throws Exception {
|
||||
|
||||
@Test
|
||||
public void handleHolidayRequest() throws Exception {
|
||||
serviceMock.bookHoliday(startCalendar.getTime(), endCalendar.getTime(), "John Doe");
|
||||
replay(serviceMock);
|
||||
endpoint.handleHolidayRequest(holidayRequest.getRootElement());
|
||||
verify(serviceMock);
|
||||
}
|
||||
serviceMock = mock(HumanResourceService.class);
|
||||
SAXBuilder builder = new SAXBuilder();
|
||||
InputStream is = getClass().getResourceAsStream("holidayRequest.xml");
|
||||
try {
|
||||
holidayRequest = builder.build(is);
|
||||
} finally {
|
||||
is.close();
|
||||
}
|
||||
endpoint = new HolidayEndpoint(serviceMock);
|
||||
startCalendar = Calendar.getInstance();
|
||||
startCalendar.clear();
|
||||
startCalendar.set(2006, Calendar.JULY, 3);
|
||||
endCalendar = Calendar.getInstance();
|
||||
endCalendar.clear();
|
||||
endCalendar.set(2006, Calendar.JULY, 7);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void handleHolidayRequest() throws Exception {
|
||||
|
||||
serviceMock.bookHoliday(startCalendar.getTime(), endCalendar.getTime(), "John Doe");
|
||||
|
||||
endpoint.handleHolidayRequest(holidayRequest.getRootElement());
|
||||
|
||||
verify(serviceMock);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user