Introduction to JAX-WS[http://jira.baeldung.com/browse/BAEL-611] (#1224)
* Introduction to JAX-WS[http://jira.baeldung.com/browse/BAEL-611] * Introduction to JAX-WS[http://jira.baeldung.com/browse/BAEL-611] * Removed unnecessary comment * Added mockito-core dependency * Introduction to JAX-WS[http://jira.baeldung.com/browse/BAEL-611] Added Exception test cases * Applied baeldung formatter in Eclipse
This commit is contained in:
parent
ea345fa246
commit
47dfe6b641
|
@ -28,6 +28,7 @@
|
|||
|
||||
<maven-compiler-plugin.version>3.6.0</maven-compiler-plugin.version>
|
||||
<maven-war-plugin.version>2.6</maven-war-plugin.version>
|
||||
<mockito.version>1.10.19</mockito.version>
|
||||
</properties>
|
||||
|
||||
<prerequisites>
|
||||
|
|
|
@ -0,0 +1,31 @@
|
|||
package com.baeldung.jaxws;
|
||||
|
||||
import com.baeldung.jaxws.exception.EmployeeAlreadyExists;
|
||||
import com.baeldung.jaxws.exception.EmployeeNotFound;
|
||||
import com.baeldung.jaxws.model.Employee;
|
||||
|
||||
import javax.jws.WebMethod;
|
||||
import javax.jws.WebService;
|
||||
import java.util.List;
|
||||
|
||||
@WebService
|
||||
public interface EmployeeService {
|
||||
|
||||
@WebMethod
|
||||
Employee getEmployee(int id) throws EmployeeNotFound;
|
||||
|
||||
@WebMethod
|
||||
Employee updateEmployee(int id, String name) throws EmployeeNotFound;
|
||||
|
||||
@WebMethod
|
||||
boolean deleteEmployee(int id) throws EmployeeNotFound;
|
||||
|
||||
@WebMethod
|
||||
Employee addEmployee(int id, String name) throws EmployeeAlreadyExists;
|
||||
|
||||
@WebMethod
|
||||
int countEmployees();
|
||||
|
||||
@WebMethod
|
||||
List<Employee> getAllEmployees();
|
||||
}
|
|
@ -0,0 +1,48 @@
|
|||
package com.baeldung.jaxws;
|
||||
|
||||
import com.baeldung.jaxws.exception.EmployeeAlreadyExists;
|
||||
import com.baeldung.jaxws.exception.EmployeeNotFound;
|
||||
import com.baeldung.jaxws.model.Employee;
|
||||
import com.baeldung.jaxws.repository.EmployeeRepository;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.jws.WebMethod;
|
||||
import javax.jws.WebService;
|
||||
import java.util.List;
|
||||
|
||||
@WebService(serviceName = "EmployeeService", endpointInterface = "com.baeldung.jaxws.EmployeeService")
|
||||
public class EmployeeServiceImpl implements EmployeeService {
|
||||
|
||||
@Inject
|
||||
private EmployeeRepository employeeRepositoryImpl;
|
||||
|
||||
@WebMethod
|
||||
public Employee getEmployee(int id) throws EmployeeNotFound {
|
||||
return employeeRepositoryImpl.getEmployee(id);
|
||||
}
|
||||
|
||||
@WebMethod
|
||||
public Employee updateEmployee(int id, String name) throws EmployeeNotFound {
|
||||
return employeeRepositoryImpl.updateEmployee(id, name);
|
||||
}
|
||||
|
||||
@WebMethod
|
||||
public boolean deleteEmployee(int id) throws EmployeeNotFound {
|
||||
return employeeRepositoryImpl.deleteEmployee(id);
|
||||
}
|
||||
|
||||
@WebMethod
|
||||
public Employee addEmployee(int id, String name) throws EmployeeAlreadyExists {
|
||||
return employeeRepositoryImpl.addEmployee(id, name);
|
||||
}
|
||||
|
||||
@WebMethod
|
||||
public int countEmployees() {
|
||||
return employeeRepositoryImpl.count();
|
||||
}
|
||||
|
||||
@WebMethod
|
||||
public List<Employee> getAllEmployees() {
|
||||
return employeeRepositoryImpl.getAllEmployees();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
package com.baeldung.jaxws.config;
|
||||
|
||||
import com.baeldung.jaxws.EmployeeServiceImpl;
|
||||
|
||||
import javax.xml.ws.Endpoint;
|
||||
|
||||
public class EmployeeServicePublisher {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Endpoint.publish("http://localhost:8080/employeeservice", new EmployeeServiceImpl());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package com.baeldung.jaxws.exception;
|
||||
|
||||
import javax.xml.ws.WebFault;
|
||||
import java.io.Serializable;
|
||||
|
||||
@WebFault
|
||||
public class EmployeeAlreadyExists extends Exception implements Serializable {
|
||||
|
||||
public EmployeeAlreadyExists() {
|
||||
super("This employee already exist");
|
||||
}
|
||||
|
||||
public EmployeeAlreadyExists(String str) {
|
||||
super(str);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
package com.baeldung.jaxws.exception;
|
||||
|
||||
import javax.xml.ws.WebFault;
|
||||
import java.io.Serializable;
|
||||
|
||||
@WebFault
|
||||
public class EmployeeNotFound extends Exception implements Serializable {
|
||||
|
||||
public EmployeeNotFound() {
|
||||
super("The specified employee does not exist");
|
||||
}
|
||||
|
||||
public EmployeeNotFound(String str) {
|
||||
super(str);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
package com.baeldung.jaxws.model;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public class Employee implements Serializable {
|
||||
private int id;
|
||||
private String firstName;
|
||||
|
||||
public Employee() {
|
||||
|
||||
}
|
||||
|
||||
public Employee(int id, String firstName) {
|
||||
this.id = id;
|
||||
this.firstName = firstName;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public void setFirstName(String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
package com.baeldung.jaxws.repository;
|
||||
|
||||
import com.baeldung.jaxws.exception.EmployeeAlreadyExists;
|
||||
import com.baeldung.jaxws.exception.EmployeeNotFound;
|
||||
import com.baeldung.jaxws.model.Employee;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface EmployeeRepository {
|
||||
|
||||
List<Employee> getAllEmployees();
|
||||
|
||||
Employee getEmployee(int id) throws EmployeeNotFound;
|
||||
|
||||
Employee updateEmployee(int id, String name) throws EmployeeNotFound;
|
||||
|
||||
boolean deleteEmployee(int id) throws EmployeeNotFound;
|
||||
|
||||
Employee addEmployee(int id, String name) throws EmployeeAlreadyExists;
|
||||
|
||||
int count();
|
||||
}
|
|
@ -0,0 +1,68 @@
|
|||
package com.baeldung.jaxws.repository;
|
||||
|
||||
import com.baeldung.jaxws.exception.EmployeeAlreadyExists;
|
||||
import com.baeldung.jaxws.exception.EmployeeNotFound;
|
||||
import com.baeldung.jaxws.model.Employee;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class EmployeeRepositoryImpl implements EmployeeRepository {
|
||||
private List<Employee> employeeList;
|
||||
|
||||
public EmployeeRepositoryImpl() {
|
||||
employeeList = new ArrayList<>();
|
||||
employeeList.add(new Employee(1, "Jane"));
|
||||
employeeList.add(new Employee(2, "Jack"));
|
||||
employeeList.add(new Employee(3, "George"));
|
||||
}
|
||||
|
||||
public List<Employee> getAllEmployees() {
|
||||
return employeeList;
|
||||
}
|
||||
|
||||
public Employee getEmployee(int id) throws EmployeeNotFound {
|
||||
for (Employee emp : employeeList) {
|
||||
if (emp.getId() == id) {
|
||||
return emp;
|
||||
}
|
||||
}
|
||||
throw new EmployeeNotFound();
|
||||
}
|
||||
|
||||
public Employee updateEmployee(int id, String name) throws EmployeeNotFound {
|
||||
for (Employee employee1 : employeeList) {
|
||||
if (employee1.getId() == id) {
|
||||
employee1.setId(id);
|
||||
employee1.setFirstName(name);
|
||||
return employee1;
|
||||
}
|
||||
}
|
||||
throw new EmployeeNotFound();
|
||||
}
|
||||
|
||||
public boolean deleteEmployee(int id) throws EmployeeNotFound {
|
||||
for (Employee emp : employeeList) {
|
||||
if (emp.getId() == id) {
|
||||
employeeList.remove(emp);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
throw new EmployeeNotFound();
|
||||
}
|
||||
|
||||
public Employee addEmployee(int id, String name) throws EmployeeAlreadyExists {
|
||||
for (Employee emp : employeeList) {
|
||||
if (emp.getId() == id) {
|
||||
throw new EmployeeAlreadyExists();
|
||||
}
|
||||
}
|
||||
Employee employee = new Employee(id, name);
|
||||
employeeList.add(employee);
|
||||
return employee;
|
||||
}
|
||||
|
||||
public int count() {
|
||||
return employeeList.size();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,108 @@
|
|||
package com.baeldung.jaxws;
|
||||
|
||||
import com.baeldung.jaxws.exception.EmployeeAlreadyExists;
|
||||
import com.baeldung.jaxws.exception.EmployeeNotFound;
|
||||
import com.baeldung.jaxws.model.Employee;
|
||||
import com.baeldung.jaxws.repository.EmployeeRepository;
|
||||
import org.jboss.arquillian.container.test.api.Deployment;
|
||||
import org.jboss.arquillian.junit.Arquillian;
|
||||
import org.jboss.arquillian.test.api.ArquillianResource;
|
||||
import org.jboss.shrinkwrap.api.ShrinkWrap;
|
||||
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
|
||||
import org.jboss.shrinkwrap.api.spec.WebArchive;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import javax.xml.namespace.QName;
|
||||
import javax.xml.ws.Service;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
@RunWith(Arquillian.class)
|
||||
public class EmployeeServiceLiveTest {
|
||||
|
||||
private static final String APP_NAME = "jee7";
|
||||
private static final String WSDL_PATH = "EmployeeService?wsdl";
|
||||
private static QName SERVICE_NAME = new QName("http://jaxws.baeldung.com/", "EmployeeService");
|
||||
private static URL wsdlUrl;
|
||||
|
||||
@ArquillianResource
|
||||
private URL deploymentUrl;
|
||||
|
||||
private EmployeeService employeeServiceProxy;
|
||||
|
||||
@Deployment(testable = false)
|
||||
public static WebArchive createDeployment() {
|
||||
return ShrinkWrap.create(WebArchive.class, APP_NAME + ".war").addPackage(EmployeeService.class.getPackage()).addPackage(Employee.class.getPackage()).addPackage(EmployeeNotFound.class.getPackage()).addPackage(EmployeeRepository.class.getPackage())
|
||||
.addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml");
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
try {
|
||||
wsdlUrl = new URL(deploymentUrl, WSDL_PATH);
|
||||
} catch (MalformedURLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
Service service = Service.create(wsdlUrl, SERVICE_NAME);
|
||||
employeeServiceProxy = service.getPort(EmployeeService.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenGetAllEmployees_thenCorrectNumberOfEmployeesReturned() {
|
||||
int employeeCount = employeeServiceProxy.countEmployees();
|
||||
List<Employee> employeeList = employeeServiceProxy.getAllEmployees();
|
||||
assertEquals(employeeList.size(), employeeCount);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmployeeId_whenEmployeeExists_thenCorrectEmployeeReturned() throws EmployeeNotFound {
|
||||
Employee employee = employeeServiceProxy.getEmployee(2);
|
||||
assertEquals(employee.getFirstName(), "Jack");
|
||||
}
|
||||
|
||||
@Test(expected = EmployeeNotFound.class)
|
||||
public void givenEmployeeId_whenEmployeeNotExists_thenEmployeeNotFoundExceptionReturned() throws EmployeeNotFound {
|
||||
employeeServiceProxy.getEmployee(20);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAddEmployee_whenEmployeeDoesntAlreadyExist_thenEmployeeCountIncreased() throws EmployeeAlreadyExists {
|
||||
int employeeCount = employeeServiceProxy.countEmployees();
|
||||
employeeServiceProxy.addEmployee(4, "Anna");
|
||||
assertEquals(employeeServiceProxy.countEmployees(), employeeCount + 1);
|
||||
}
|
||||
|
||||
@Test(expected = EmployeeAlreadyExists.class)
|
||||
public void givenAddEmployee_whenEmployeeAlreadyExist_thenEmployeeAlreadyExistsExceptionReturned() throws EmployeeAlreadyExists {
|
||||
employeeServiceProxy.addEmployee(1, "Anna");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUpdateEmployee_whenEmployeeExists_thenUpdatedEmployeeReturned() throws EmployeeNotFound {
|
||||
Employee updated = employeeServiceProxy.updateEmployee(1, "Joan");
|
||||
assertEquals(updated.getFirstName(), "Joan");
|
||||
}
|
||||
|
||||
@Test(expected = EmployeeNotFound.class)
|
||||
public void givenUpdateEmployee_whenEmployeeNotExists_thenUpdatedEmployeeReturned() throws EmployeeNotFound {
|
||||
employeeServiceProxy.updateEmployee(20, "Joan");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDeleteEmployee_whenEmployeeExists_thenCorrectStatusReturned() throws EmployeeNotFound {
|
||||
boolean deleteEmployee = employeeServiceProxy.deleteEmployee(3);
|
||||
assertEquals(deleteEmployee, true);
|
||||
}
|
||||
|
||||
@Test(expected = EmployeeNotFound.class)
|
||||
public void givenDeleteEmployee_whenEmployeeNotExists_thenEmployeeNotFoundExceptionReturned() throws EmployeeNotFound {
|
||||
employeeServiceProxy.deleteEmployee(20);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue