Skip to content

Commit 47dfe6b

Browse files
Euniceadusunilmogadati
authored andcommitted
* 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
1 parent ea345fa commit 47dfe6b

10 files changed

Lines changed: 356 additions & 0 deletions

File tree

‎jee7/pom.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
<maven-compiler-plugin.version>3.6.0</maven-compiler-plugin.version>
3030
<maven-war-plugin.version>2.6</maven-war-plugin.version>
31+
<mockito.version>1.10.19</mockito.version>
3132
</properties>
3233

3334
<prerequisites>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.baeldung.jaxws;
2+
3+
import com.baeldung.jaxws.exception.EmployeeAlreadyExists;
4+
import com.baeldung.jaxws.exception.EmployeeNotFound;
5+
import com.baeldung.jaxws.model.Employee;
6+
7+
import javax.jws.WebMethod;
8+
import javax.jws.WebService;
9+
import java.util.List;
10+
11+
@WebService
12+
public interface EmployeeService {
13+
14+
@WebMethod
15+
Employee getEmployee(int id) throws EmployeeNotFound;
16+
17+
@WebMethod
18+
Employee updateEmployee(int id, String name) throws EmployeeNotFound;
19+
20+
@WebMethod
21+
boolean deleteEmployee(int id) throws EmployeeNotFound;
22+
23+
@WebMethod
24+
Employee addEmployee(int id, String name) throws EmployeeAlreadyExists;
25+
26+
@WebMethod
27+
int countEmployees();
28+
29+
@WebMethod
30+
List<Employee> getAllEmployees();
31+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.baeldung.jaxws;
2+
3+
import com.baeldung.jaxws.exception.EmployeeAlreadyExists;
4+
import com.baeldung.jaxws.exception.EmployeeNotFound;
5+
import com.baeldung.jaxws.model.Employee;
6+
import com.baeldung.jaxws.repository.EmployeeRepository;
7+
8+
import javax.inject.Inject;
9+
import javax.jws.WebMethod;
10+
import javax.jws.WebService;
11+
import java.util.List;
12+
13+
@WebService(serviceName = "EmployeeService", endpointInterface = "com.baeldung.jaxws.EmployeeService")
14+
public class EmployeeServiceImpl implements EmployeeService {
15+
16+
@Inject
17+
private EmployeeRepository employeeRepositoryImpl;
18+
19+
@WebMethod
20+
public Employee getEmployee(int id) throws EmployeeNotFound {
21+
return employeeRepositoryImpl.getEmployee(id);
22+
}
23+
24+
@WebMethod
25+
public Employee updateEmployee(int id, String name) throws EmployeeNotFound {
26+
return employeeRepositoryImpl.updateEmployee(id, name);
27+
}
28+
29+
@WebMethod
30+
public boolean deleteEmployee(int id) throws EmployeeNotFound {
31+
return employeeRepositoryImpl.deleteEmployee(id);
32+
}
33+
34+
@WebMethod
35+
public Employee addEmployee(int id, String name) throws EmployeeAlreadyExists {
36+
return employeeRepositoryImpl.addEmployee(id, name);
37+
}
38+
39+
@WebMethod
40+
public int countEmployees() {
41+
return employeeRepositoryImpl.count();
42+
}
43+
44+
@WebMethod
45+
public List<Employee> getAllEmployees() {
46+
return employeeRepositoryImpl.getAllEmployees();
47+
}
48+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.baeldung.jaxws.config;
2+
3+
import com.baeldung.jaxws.EmployeeServiceImpl;
4+
5+
import javax.xml.ws.Endpoint;
6+
7+
public class EmployeeServicePublisher {
8+
9+
public static void main(String[] args) {
10+
Endpoint.publish("http://localhost:8080/employeeservice", new EmployeeServiceImpl());
11+
}
12+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.baeldung.jaxws.exception;
2+
3+
import javax.xml.ws.WebFault;
4+
import java.io.Serializable;
5+
6+
@WebFault
7+
public class EmployeeAlreadyExists extends Exception implements Serializable {
8+
9+
public EmployeeAlreadyExists() {
10+
super("This employee already exist");
11+
}
12+
13+
public EmployeeAlreadyExists(String str) {
14+
super(str);
15+
}
16+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.baeldung.jaxws.exception;
2+
3+
import javax.xml.ws.WebFault;
4+
import java.io.Serializable;
5+
6+
@WebFault
7+
public class EmployeeNotFound extends Exception implements Serializable {
8+
9+
public EmployeeNotFound() {
10+
super("The specified employee does not exist");
11+
}
12+
13+
public EmployeeNotFound(String str) {
14+
super(str);
15+
}
16+
17+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.baeldung.jaxws.model;
2+
3+
import java.io.Serializable;
4+
5+
public class Employee implements Serializable {
6+
private int id;
7+
private String firstName;
8+
9+
public Employee() {
10+
11+
}
12+
13+
public Employee(int id, String firstName) {
14+
this.id = id;
15+
this.firstName = firstName;
16+
}
17+
18+
public int getId() {
19+
return id;
20+
}
21+
22+
public void setId(int id) {
23+
this.id = id;
24+
}
25+
26+
public String getFirstName() {
27+
return firstName;
28+
}
29+
30+
public void setFirstName(String firstName) {
31+
this.firstName = firstName;
32+
}
33+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.jaxws.repository;
2+
3+
import com.baeldung.jaxws.exception.EmployeeAlreadyExists;
4+
import com.baeldung.jaxws.exception.EmployeeNotFound;
5+
import com.baeldung.jaxws.model.Employee;
6+
7+
import java.util.List;
8+
9+
public interface EmployeeRepository {
10+
11+
List<Employee> getAllEmployees();
12+
13+
Employee getEmployee(int id) throws EmployeeNotFound;
14+
15+
Employee updateEmployee(int id, String name) throws EmployeeNotFound;
16+
17+
boolean deleteEmployee(int id) throws EmployeeNotFound;
18+
19+
Employee addEmployee(int id, String name) throws EmployeeAlreadyExists;
20+
21+
int count();
22+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.baeldung.jaxws.repository;
2+
3+
import com.baeldung.jaxws.exception.EmployeeAlreadyExists;
4+
import com.baeldung.jaxws.exception.EmployeeNotFound;
5+
import com.baeldung.jaxws.model.Employee;
6+
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
10+
public class EmployeeRepositoryImpl implements EmployeeRepository {
11+
private List<Employee> employeeList;
12+
13+
public EmployeeRepositoryImpl() {
14+
employeeList = new ArrayList<>();
15+
employeeList.add(new Employee(1, "Jane"));
16+
employeeList.add(new Employee(2, "Jack"));
17+
employeeList.add(new Employee(3, "George"));
18+
}
19+
20+
public List<Employee> getAllEmployees() {
21+
return employeeList;
22+
}
23+
24+
public Employee getEmployee(int id) throws EmployeeNotFound {
25+
for (Employee emp : employeeList) {
26+
if (emp.getId() == id) {
27+
return emp;
28+
}
29+
}
30+
throw new EmployeeNotFound();
31+
}
32+
33+
public Employee updateEmployee(int id, String name) throws EmployeeNotFound {
34+
for (Employee employee1 : employeeList) {
35+
if (employee1.getId() == id) {
36+
employee1.setId(id);
37+
employee1.setFirstName(name);
38+
return employee1;
39+
}
40+
}
41+
throw new EmployeeNotFound();
42+
}
43+
44+
public boolean deleteEmployee(int id) throws EmployeeNotFound {
45+
for (Employee emp : employeeList) {
46+
if (emp.getId() == id) {
47+
employeeList.remove(emp);
48+
return true;
49+
}
50+
}
51+
throw new EmployeeNotFound();
52+
}
53+
54+
public Employee addEmployee(int id, String name) throws EmployeeAlreadyExists {
55+
for (Employee emp : employeeList) {
56+
if (emp.getId() == id) {
57+
throw new EmployeeAlreadyExists();
58+
}
59+
}
60+
Employee employee = new Employee(id, name);
61+
employeeList.add(employee);
62+
return employee;
63+
}
64+
65+
public int count() {
66+
return employeeList.size();
67+
}
68+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package com.baeldung.jaxws;
2+
3+
import com.baeldung.jaxws.exception.EmployeeAlreadyExists;
4+
import com.baeldung.jaxws.exception.EmployeeNotFound;
5+
import com.baeldung.jaxws.model.Employee;
6+
import com.baeldung.jaxws.repository.EmployeeRepository;
7+
import org.jboss.arquillian.container.test.api.Deployment;
8+
import org.jboss.arquillian.junit.Arquillian;
9+
import org.jboss.arquillian.test.api.ArquillianResource;
10+
import org.jboss.shrinkwrap.api.ShrinkWrap;
11+
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
12+
import org.jboss.shrinkwrap.api.spec.WebArchive;
13+
import org.junit.Before;
14+
import org.junit.Test;
15+
import org.junit.runner.RunWith;
16+
17+
import javax.xml.namespace.QName;
18+
import javax.xml.ws.Service;
19+
import java.net.MalformedURLException;
20+
import java.net.URL;
21+
import java.util.List;
22+
23+
import static org.junit.Assert.assertEquals;
24+
25+
@RunWith(Arquillian.class)
26+
public class EmployeeServiceLiveTest {
27+
28+
private static final String APP_NAME = "jee7";
29+
private static final String WSDL_PATH = "EmployeeService?wsdl";
30+
private static QName SERVICE_NAME = new QName("http://jaxws.baeldung.com/", "EmployeeService");
31+
private static URL wsdlUrl;
32+
33+
@ArquillianResource
34+
private URL deploymentUrl;
35+
36+
private EmployeeService employeeServiceProxy;
37+
38+
@Deployment(testable = false)
39+
public static WebArchive createDeployment() {
40+
return ShrinkWrap.create(WebArchive.class, APP_NAME + ".war").addPackage(EmployeeService.class.getPackage()).addPackage(Employee.class.getPackage()).addPackage(EmployeeNotFound.class.getPackage()).addPackage(EmployeeRepository.class.getPackage())
41+
.addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml");
42+
}
43+
44+
@Before
45+
public void setUp() {
46+
try {
47+
wsdlUrl = new URL(deploymentUrl, WSDL_PATH);
48+
} catch (MalformedURLException e) {
49+
e.printStackTrace();
50+
}
51+
52+
Service service = Service.create(wsdlUrl, SERVICE_NAME);
53+
employeeServiceProxy = service.getPort(EmployeeService.class);
54+
}
55+
56+
@Test
57+
public void givenGetAllEmployees_thenCorrectNumberOfEmployeesReturned() {
58+
int employeeCount = employeeServiceProxy.countEmployees();
59+
List<Employee> employeeList = employeeServiceProxy.getAllEmployees();
60+
assertEquals(employeeList.size(), employeeCount);
61+
}
62+
63+
@Test
64+
public void givenEmployeeId_whenEmployeeExists_thenCorrectEmployeeReturned() throws EmployeeNotFound {
65+
Employee employee = employeeServiceProxy.getEmployee(2);
66+
assertEquals(employee.getFirstName(), "Jack");
67+
}
68+
69+
@Test(expected = EmployeeNotFound.class)
70+
public void givenEmployeeId_whenEmployeeNotExists_thenEmployeeNotFoundExceptionReturned() throws EmployeeNotFound {
71+
employeeServiceProxy.getEmployee(20);
72+
}
73+
74+
@Test
75+
public void givenAddEmployee_whenEmployeeDoesntAlreadyExist_thenEmployeeCountIncreased() throws EmployeeAlreadyExists {
76+
int employeeCount = employeeServiceProxy.countEmployees();
77+
employeeServiceProxy.addEmployee(4, "Anna");
78+
assertEquals(employeeServiceProxy.countEmployees(), employeeCount + 1);
79+
}
80+
81+
@Test(expected = EmployeeAlreadyExists.class)
82+
public void givenAddEmployee_whenEmployeeAlreadyExist_thenEmployeeAlreadyExistsExceptionReturned() throws EmployeeAlreadyExists {
83+
employeeServiceProxy.addEmployee(1, "Anna");
84+
}
85+
86+
@Test
87+
public void givenUpdateEmployee_whenEmployeeExists_thenUpdatedEmployeeReturned() throws EmployeeNotFound {
88+
Employee updated = employeeServiceProxy.updateEmployee(1, "Joan");
89+
assertEquals(updated.getFirstName(), "Joan");
90+
}
91+
92+
@Test(expected = EmployeeNotFound.class)
93+
public void givenUpdateEmployee_whenEmployeeNotExists_thenUpdatedEmployeeReturned() throws EmployeeNotFound {
94+
employeeServiceProxy.updateEmployee(20, "Joan");
95+
}
96+
97+
@Test
98+
public void givenDeleteEmployee_whenEmployeeExists_thenCorrectStatusReturned() throws EmployeeNotFound {
99+
boolean deleteEmployee = employeeServiceProxy.deleteEmployee(3);
100+
assertEquals(deleteEmployee, true);
101+
}
102+
103+
@Test(expected = EmployeeNotFound.class)
104+
public void givenDeleteEmployee_whenEmployeeNotExists_thenEmployeeNotFoundExceptionReturned() throws EmployeeNotFound {
105+
employeeServiceProxy.deleteEmployee(20);
106+
}
107+
108+
}

0 commit comments

Comments
 (0)