-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAbstractEmployeeRouteControllerTest.kt
191 lines (164 loc) · 7.74 KB
/
AbstractEmployeeRouteControllerTest.kt
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package com.example.employee_api.routers
import com.example.employee_api.db.entities.EmployeeEntity
import com.example.employee_api.db.repositories.EmployeesRepository
import com.example.employee_api.dto.StandardResponse
import com.example.employee_api.utils.LocalDateTypeAdapter
import com.google.gson.GsonBuilder
import com.google.gson.reflect.TypeToken
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.core.io.ClassPathResource
import org.springframework.http.MediaType
import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.httpBasic
import org.springframework.test.web.servlet.MockMvc
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.*
import java.io.File
import java.io.FileReader
import java.time.LocalDate
@AutoConfigureMockMvc
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class AbstractEmployeeRouteControllerTest
@Autowired constructor(private var mockMvc: MockMvc, private var employeesRepository: EmployeesRepository) {
companion object {
private val gson = GsonBuilder()
.registerTypeAdapter(LocalDate::class.java, LocalDateTypeAdapter())
.create()
lateinit var testUsers: Array<EmployeeEntity>
lateinit var testUsersJson: String
@JvmStatic
@BeforeAll
fun readTestUsers(): Unit {
val arrayOfEmployeesType = object : TypeToken<Array<EmployeeEntity>>() {}.type
val testUsersFile: File = ClassPathResource("test_users/test_users.json").file
testUsersJson = FileReader(testUsersFile).readText()
testUsers = gson.fromJson(testUsersJson, arrayOfEmployeesType)
}
}
@AfterEach
fun removeEntities() {
employeesRepository.deleteAll()
}
@Test
fun `Given there are no employees in the db, when a get all employees request is made, an empty employee array is returned`() {
mockMvc.perform(
get("/api/employees")
.with(httpBasic("user", "password"))
)
.andExpect(status().isOk)
.andExpect(jsonPath("$.entity").isArray)
.andExpect(jsonPath("$.entity").isEmpty)
}
@Test
fun `Given there are no users in the DB, when a find user by id request is sent, then it responds with an empty response`() {
mockMvc.perform(
get("/api/employees/1")
.with(httpBasic("user", "password"))
)
.andExpect(status().isOk)
.andExpect(content().json(Companion.gson.toJson(StandardResponse(null, "No entity for ID: 1 could be located."))))
}
@Test
fun `Given a user is loaded into the DB, when a find user by id request is made with a valid id, then the user is returned`() {
val testUserToAdd = testUsers[0]
employeesRepository.save(testUserToAdd)
mockMvc.perform(get("/api/employees/1")
.with(httpBasic("user", "password"))
)
.andExpect(status().isOk)
.andExpect(jsonPath("$").isMap)
}
@Test
fun `Given there is at least one employee in the db, when a get all employees request is made, an array with one employee is returned`() {
val testUserToAdd = testUsers[0]
employeesRepository.save(testUserToAdd)
mockMvc.perform(
get("/api/employees")
.with(httpBasic("user", "password"))
)
.andExpect(status().isOk)
.andExpect(jsonPath("$.entity[0].lastName").value(testUserToAdd.lastName))
}
@Test
fun `Given an employee entity, when a create employee request is made, then the api responds with a success response message`() {
val testUserToAdd = testUsers[0]
mockMvc.perform(
post("/api/employees/create")
.with(httpBasic("user", "password"))
.contentType(MediaType.APPLICATION_JSON)
.content(gson.toJson(testUserToAdd))
)
.andExpect(status().isOk)
.andExpect(jsonPath("$.entity.email").value(testUserToAdd.email))
}
@Test
fun `Given an employee entity exists in the db, when an update employee request is made with a valid employee, then the api responds with an updated entity message`() {
val testEmployeeToUpdate = testUsers[0]
val savedEmployee = employeesRepository.save(testEmployeeToUpdate)
val updatedFirstName = "NewFirstName"
testEmployeeToUpdate.firstName = updatedFirstName
mockMvc.perform(
patch("/api/employees/update/${savedEmployee.id}")
.with(httpBasic("user", "password"))
.contentType(MediaType.APPLICATION_JSON)
.content(gson.toJson(testEmployeeToUpdate))
)
.andExpect(status().isOk)
.andExpect(jsonPath("$.entity.firstName").value(updatedFirstName))
}
@Test
fun `Given there are no employees in the db, when a batch create request is made, then the api adds all employees to the db`() {
val testEmployeeToCheck = testUsers[1]
mockMvc.perform(
post("/api/employees/batch/create")
.with(httpBasic("user", "password"))
.contentType(MediaType.APPLICATION_JSON)
.content(testUsersJson)
)
.andExpect(status().isOk)
.andExpect(jsonPath("$.entity[1].email").value(testEmployeeToCheck.email))
}
@Test
fun `Given an employee in the DB, when a partial update request is made, partially updates the entity`() {
val originalEmployee = employeesRepository.save(testUsers[0])
val partialEmployeeUpdate = EmployeeEntity()
partialEmployeeUpdate.id = originalEmployee.id
partialEmployeeUpdate.firstName = "NewFirstName"
mockMvc.perform(
patch("/api/employees/update/${originalEmployee.id}")
.with(httpBasic("user", "password"))
.contentType(MediaType.APPLICATION_JSON)
.content(gson.toJson(partialEmployeeUpdate))
)
.andExpect(status().isOk)
.andExpect(jsonPath("$.entity.firstName").value("NewFirstName"))
.andExpect(jsonPath("$.entity.email").value(originalEmployee.email))
}
@Test
fun `Given there are employees in the DB, when a batch update request is made, the API responds with updated entities`() {
val testEmployeesFromDb = employeesRepository.saveAll(testUsers.toMutableList())
val employee1 = EmployeeEntity()
employee1.id = testEmployeesFromDb[0].id
employee1.email = testEmployeesFromDb[1].email
val employee2 = EmployeeEntity()
employee2.id = testEmployeesFromDb[1].id
employee2.email = testEmployeesFromDb[0].email
val employeeUpdateDTOs = mutableListOf(employee1, employee2)
val testEmployeeToCheck = testUsers[0]
mockMvc.perform(
patch("/api/employees/batch/update")
.with(httpBasic("user", "password"))
.contentType(MediaType.APPLICATION_JSON)
.content(gson.toJson(employeeUpdateDTOs))
)
.andExpect(status().isOk)
.andExpect(jsonPath("$.entity[1].email").value(testEmployeeToCheck.email))
val updatedEmployee = employee1.id?.let { employeesRepository.findById(it) }
Assertions.assertEquals(testUsers[0].firstName, updatedEmployee?.get()?.firstName)
}
}