Class: |
net.hrider.api.usecases.CRUDEmployees.java |
Example of how to create, update and delete employees. After creating them, we will
update one of them to see that the changes have been saved by printing a list of the
employees before and after.
As in the previous use case, create an instance of
HRiderService '
service' that
will perform all the code for the request and answers with our Jersey client.
After creating a fictional company 'company', create thee employees:
Link employeesLink = company.getLinks().getEmployees();
Employee emp1 = service.create(employeesLink, buildEmployee("EMP1", "Employee 1", "[email protected]"));
Employee emp2 = service.create(employeesLink, buildEmployee("EMP2", "Employee 2", "[email protected]"));
Employee emp3 = service.create(employeesLink, buildEmployee("EMP3", "Employee 3", "[email protected]"));
To update an employee, we modify their attributes and perform a PUT to their link.
In this case, we can take advantage from the
self-link that all the resources have
to change the employee code to 'NEW':
emp1.setCode("NEW");
service.put(emp1.getLinks().getSelf(), emp1);
To confirm that the changes have been saved, obtain all the employees and print them.
To get the employees perform a GET request to the link 'employees' of the compny:
EmployeesCollection collection = service.get(EmployeesCollection.class,
company.getLinks().getEmployees());
printEmployees(collection.getEmbedded().getEmployees());
The obtained result is the expected: the code 'Employee' has been replaced for 'NEW':
ID Code Email Name
------------------- --------- ------------------ ------------------
184908605578806810 NEW [email protected] EMPLOYEE 1
184908605705684507 EMP2 [email protected] EMPLOYEE 2
184908605795862044 EMP3 [email protected] EMPLOYEE 3
Now delete EMPLOYEE1 and print the employees again:
service.delete(emp1.getLinks().getSelf());
collection = service.get(EmployeesCollection.class, company.getLinks().getEmployees());
printEmployees(collection.getEmbedded().getEmployees());
The obtained result is the expected: the employee with 'NEW' code does not appear:
ID Code Email Name
------------------- --------- ------------------ ------------------
184908605705684507 EMP2 [email protected] EMPLOYEE 2
184908605795862044 EMP3 [email protected] EMPLOYEE 3