From f82ad5a6b5c9def488c6afc4502aab16fb1aedf0 Mon Sep 17 00:00:00 2001 From: Lorenzo Bettini Date: Fri, 15 Dec 2017 12:13:24 +0100 Subject: [PATCH] implemented REST POST, PUT and DELETE --- .../controllers/EmployeeRestControllerIT.java | 95 ++++++++++++++++++- .../controllers/EmployeeRestController.java | 26 ++++- .../spring/demo/services/EmployeeService.java | 4 + ...EmployeeRestControllerRestAssuredTest.java | 75 ++++++++++++++- 4 files changed, 194 insertions(+), 6 deletions(-) diff --git a/demo-spring-testing/src/it/java/com/examples/spring/demo/controllers/EmployeeRestControllerIT.java b/demo-spring-testing/src/it/java/com/examples/spring/demo/controllers/EmployeeRestControllerIT.java index 9751d1a..d92dfbc 100644 --- a/demo-spring-testing/src/it/java/com/examples/spring/demo/controllers/EmployeeRestControllerIT.java +++ b/demo-spring-testing/src/it/java/com/examples/spring/demo/controllers/EmployeeRestControllerIT.java @@ -1,6 +1,7 @@ package com.examples.spring.demo.controllers; import static io.restassured.RestAssured.given; +import static org.assertj.core.api.Assertions.assertThat; import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.Matchers.isEmptyOrNullString; @@ -16,6 +17,7 @@ import org.springframework.boot.context.embedded.LocalServerPort; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; +import org.springframework.http.MediaType; import org.springframework.test.context.junit4.SpringRunner; import com.examples.spring.demo.model.Employee; @@ -47,6 +49,10 @@ public void setup() { url = "http://localhost:" + port; employeeRepository.deleteAll(); employeeRepository.flush(); + showCurrentEmployeesOnTheLog(); + } + + private void showCurrentEmployeesOnTheLog() { LOGGER.info("employees: " + employeeRepository.findAll()); } @@ -57,7 +63,7 @@ public void testAllEmployees() throws Exception { new Employee(null, "second", 5000) )); employeeRepository.flush(); - LOGGER.info("employees: " + employeeRepository.findAll()); + showCurrentEmployeesOnTheLog(); given(). when(). @@ -85,7 +91,7 @@ public void testFindByIdWithExistingEmployee() throws Exception { Employee saved = employeeRepository.save( new Employee(null, "first", 1000)); employeeRepository.flush(); - LOGGER.info("employees: " + employeeRepository.findAll()); + showCurrentEmployeesOnTheLog(); given(). when(). @@ -102,7 +108,7 @@ public void testFindByIdWithExistingEmployee() throws Exception { @Test public void testFindByIdWithNonExistingEmployee() throws Exception { - LOGGER.info("employees: " + employeeRepository.findAll()); + showCurrentEmployeesOnTheLog(); given(). when(). @@ -111,4 +117,87 @@ public void testFindByIdWithNonExistingEmployee() throws Exception { statusCode(200). contentType(isEmptyOrNullString()); } + + @Test + public void testNewEmployee() throws Exception { + given(). + contentType(MediaType.APPLICATION_JSON_VALUE). + body(new Employee(null, "test", 1000)). + when(). + post(url + "/api/employees/new"). + then(). + statusCode(200); + + showCurrentEmployeesOnTheLog(); + assertThat(employeeRepository.findAll().toString()) + .matches( + "\\[Employee \\[id=([1-9][0-9]*), name=test, salary=1000\\]\\]"); + } + + @Test + public void testUpdateEmployee() throws Exception { + Employee saved = employeeRepository.save( + new Employee(null, "first", 100)); + employeeRepository.flush(); + showCurrentEmployeesOnTheLog(); + // the body for the update does not contain "id" + Employee updated = new Employee(null, "test", 1000); + + given(). + contentType(MediaType.APPLICATION_JSON_VALUE). + body(updated). + when(). + put(url + "/api/employees/update/" + saved.getId()). + then(). + statusCode(200); + + assertThat(employeeRepository.findAll().toString()) + .isEqualTo( + "[Employee [id=" + + saved.getId() + + ", name=test, salary=1000]]"); + } + + @Test + public void testUpdateEmployeeWithFakeId() throws Exception { + Employee saved = employeeRepository.save( + new Employee(null, "first", 100)); + employeeRepository.flush(); + showCurrentEmployeesOnTheLog(); + + // although we pass an Employee in the body with id 100... + Employee updated = new Employee(100L, "test", 1000); + given(). + contentType(MediaType.APPLICATION_JSON_VALUE). + body(updated). + when(). + // the id specified in the URL... + put(url + "/api/employees/update/" + saved.getId()). + then(). + statusCode(200); + + // has the precedence + assertThat(employeeRepository.findAll().toString()) + .isEqualTo( + "[Employee [id=" + + saved.getId() + + ", name=test, salary=1000]]"); + } + + @Test + public void testDeleteEmployee() throws Exception { + Employee saved = employeeRepository.save( + new Employee(null, "first", 100)); + employeeRepository.flush(); + showCurrentEmployeesOnTheLog(); + + given(). + when(). + delete(url + "/api/employees/delete/" + saved.getId()). + then(). + statusCode(200); + + assertThat(employeeRepository.findAll()) + .isEmpty(); + } } diff --git a/demo-spring-testing/src/main/java/com/examples/spring/demo/controllers/EmployeeRestController.java b/demo-spring-testing/src/main/java/com/examples/spring/demo/controllers/EmployeeRestController.java index f05a04a..d0a17ba 100644 --- a/demo-spring-testing/src/main/java/com/examples/spring/demo/controllers/EmployeeRestController.java +++ b/demo-spring-testing/src/main/java/com/examples/spring/demo/controllers/EmployeeRestController.java @@ -3,14 +3,20 @@ import java.util.List; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.examples.spring.demo.model.Employee; import com.examples.spring.demo.services.EmployeeService; @RestController +@RequestMapping("/api") public class EmployeeRestController { private EmployeeService employeeService; @@ -20,14 +26,30 @@ public EmployeeRestController(EmployeeService employeeService) { this.employeeService = employeeService; } - @GetMapping("/api/employees") + @GetMapping("/employees") public List allEmployees() { return employeeService.getAllEmployees(); } - @GetMapping("/api/employees/{id}") + @GetMapping("/employees/{id}") public Employee oneEmployee(@PathVariable long id) { return employeeService.getEmployeeById(id); } + @PostMapping("/employees/new") + public Employee newEmployee(@RequestBody Employee employee) { + return employeeService.saveEmployee(employee); + } + + @PutMapping("/employees/update/{id}") + public Employee updateEmployee(@PathVariable long id, @RequestBody Employee employee) { + // make sure the id is set, using the passed parameter + employee.setId(id); + return employeeService.saveEmployee(employee); + } + + @DeleteMapping("/employees/delete/{id}") + public void deleteEmployee(@PathVariable long id) { + employeeService.delete(id); + } } diff --git a/demo-spring-testing/src/main/java/com/examples/spring/demo/services/EmployeeService.java b/demo-spring-testing/src/main/java/com/examples/spring/demo/services/EmployeeService.java index 1a4ad89..ff647da 100644 --- a/demo-spring-testing/src/main/java/com/examples/spring/demo/services/EmployeeService.java +++ b/demo-spring-testing/src/main/java/com/examples/spring/demo/services/EmployeeService.java @@ -41,4 +41,8 @@ public void deleteAll() { public Employee saveEmployee(Employee employee) { return employeeRepository.save(employee); } + + public void delete(long id) { + employeeRepository.delete(id);; + } } diff --git a/demo-spring-testing/src/test/java/com/examples/spring/demo/controllers/EmployeeRestControllerRestAssuredTest.java b/demo-spring-testing/src/test/java/com/examples/spring/demo/controllers/EmployeeRestControllerRestAssuredTest.java index 60045cb..9b5307a 100644 --- a/demo-spring-testing/src/test/java/com/examples/spring/demo/controllers/EmployeeRestControllerRestAssuredTest.java +++ b/demo-spring-testing/src/test/java/com/examples/spring/demo/controllers/EmployeeRestControllerRestAssuredTest.java @@ -3,6 +3,7 @@ import static io.restassured.module.mockmvc.RestAssuredMockMvc.given; import static io.restassured.module.mockmvc.RestAssuredMockMvc.standaloneSetup; import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.Matchers.isEmptyOrNullString; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; @@ -12,6 +13,7 @@ import org.junit.Before; import org.junit.Test; +import org.springframework.http.MediaType; import com.examples.spring.demo.model.Employee; import com.examples.spring.demo.services.EmployeeService; @@ -74,6 +76,77 @@ public void testFindByIdWithExistingEmployee() throws Exception { verify(employeeService, times(1)).getEmployeeById(1); } - // other tests similar to EmployeeRestControllerTest + @Test + public void testFindByIdWithNonExistingEmployee() throws Exception { + given(). + when(). + get("/api/employees/100"). + then(). + statusCode(200). + contentType(isEmptyOrNullString()); + + verify(employeeService, times(1)).getEmployeeById(100); + } + + @Test + public void testNewEmployee() throws Exception { + given(). + contentType(MediaType.APPLICATION_JSON_VALUE). + body(new Employee(null, "test", 1000)). + when(). + post("/api/employees/new"). + then(). + statusCode(200); + + verify(employeeService, times(1)). + saveEmployee(new Employee(null, "test", 1000)); + } + + @Test + public void testUpdateEmployee() throws Exception { + when(employeeService.getEmployeeById(1)). + thenReturn(new Employee(1L, "first", 100)); + Employee updated = new Employee(1L, "test", 1000); + + given(). + contentType(MediaType.APPLICATION_JSON_VALUE). + body(updated). + when(). + put("/api/employees/update/1"). + then(). + statusCode(200); + + verify(employeeService, times(1)). + saveEmployee(updated); + } + + @Test + public void testUpdateEmployeeWithFakeId() throws Exception { + // although we pass an Employee in the body with id 100... + Employee updated = new Employee(100L, "test", 1000); + given(). + contentType(MediaType.APPLICATION_JSON_VALUE). + body(updated). + when(). + // the id specified in the URL... + put("/api/employees/update/1"). + then(). + statusCode(200); + + // has the precedence + verify(employeeService, times(1)). + saveEmployee(new Employee(1L, "test", 1000)); + } + @Test + public void testDeleteEmployee() throws Exception { + given(). + when(). + delete("/api/employees/delete/1"). + then(). + statusCode(200); + + verify(employeeService, times(1)). + delete(1L); + } }