-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathEmployeePostTest.java
More file actions
99 lines (81 loc) · 3.24 KB
/
EmployeePostTest.java
File metadata and controls
99 lines (81 loc) · 3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package ch.etml.es.payroll.controllers;
import ch.etml.es.payroll.entities.Employee;
import ch.etml.es.payroll.PayrollApplication;
import ch.etml.es.payroll.repositories.EmployeeRepository;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import static org.assertj.core.api.Assertions.assertThat;
@SpringBootTest(
classes = PayrollApplication.class,
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT
)
@ActiveProfiles("test")
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
class EmployeePostTest {
private static final String BASE_URL = "/v1/employees";
@Autowired
private TestRestTemplate restTemplate;
@Autowired
private EmployeeRepository employeeRepository;
@BeforeEach
void given_an_empty_employee_database() {
// GIVEN
employeeRepository.deleteAll();
employeeRepository.flush();
}
@Test
void when_posting_new_employee_then_employee_is_created_and_persisted() {
// GIVEN
Employee newEmployee = new Employee("Doe", "Supervisor");
HttpEntity<Employee> request = new HttpEntity<>(newEmployee);
// WHEN
ResponseEntity<Employee> response =
restTemplate.postForEntity(
BASE_URL,
request,
Employee.class
);
// THEN (HTTP)
assertThat(response.getStatusCode())
.isEqualTo(HttpStatus.CREATED);
// THEN (body)
Employee createdEmployee = response.getBody();
assertThat(createdEmployee).isNotNull();
assertThat(createdEmployee.getId()).isNotNull();
assertThat(createdEmployee.getName()).isEqualTo("Doe");
assertThat(createdEmployee.getRole()).isEqualTo("SUPERVISOR");
// THEN (database)
assertThat(employeeRepository.findById(createdEmployee.getId()))
.isPresent();
}
@Test
void when_posting_existing_employee_then_conflict_is_returned() {
// GIVEN
Employee existingEmployee = new Employee("Doe", "Supervisor");
employeeRepository.save(existingEmployee);
Employee duplicateEmployee = new Employee("Doe", "Supervisor");
HttpEntity<Employee> request = new HttpEntity<>(duplicateEmployee);
// WHEN
ResponseEntity<String> response =
restTemplate.postForEntity(
BASE_URL,
request,
String.class
);
// THEN (HTTP)
assertThat(response.getStatusCode())
.isEqualTo(HttpStatus.CONFLICT);
// THEN (error message)
assertThat(response.getBody())
.isNotNull()
.contains("Employee "+ existingEmployee.getName() + " already exists");
}
}