Skip to content

Commit

Permalink
implemented REST POST, PUT and DELETE
Browse files Browse the repository at this point in the history
  • Loading branch information
LorenzoBettini committed Dec 16, 2017
1 parent 1094cc0 commit f82ad5a
Show file tree
Hide file tree
Showing 4 changed files with 194 additions and 6 deletions.
@@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -47,6 +49,10 @@ public void setup() {
url = "http://localhost:" + port;
employeeRepository.deleteAll();
employeeRepository.flush();
showCurrentEmployeesOnTheLog();
}

private void showCurrentEmployeesOnTheLog() {
LOGGER.info("employees: " + employeeRepository.findAll());
}

Expand All @@ -57,7 +63,7 @@ public void testAllEmployees() throws Exception {
new Employee(null, "second", 5000)
));
employeeRepository.flush();
LOGGER.info("employees: " + employeeRepository.findAll());
showCurrentEmployeesOnTheLog();

given().
when().
Expand Down Expand Up @@ -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().
Expand All @@ -102,7 +108,7 @@ public void testFindByIdWithExistingEmployee() throws Exception {

@Test
public void testFindByIdWithNonExistingEmployee() throws Exception {
LOGGER.info("employees: " + employeeRepository.findAll());
showCurrentEmployeesOnTheLog();

given().
when().
Expand All @@ -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();
}
}
Expand Up @@ -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;
Expand All @@ -20,14 +26,30 @@ public EmployeeRestController(EmployeeService employeeService) {
this.employeeService = employeeService;
}

@GetMapping("/api/employees")
@GetMapping("/employees")
public List<Employee> 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);
}
}
Expand Up @@ -41,4 +41,8 @@ public void deleteAll() {
public Employee saveEmployee(Employee employee) {
return employeeRepository.save(employee);
}

public void delete(long id) {
employeeRepository.delete(id);;
}
}
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
}

0 comments on commit f82ad5a

Please sign in to comment.