|
| 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