forked from alecron/lab3-restful-ws
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AddressBookServiceTest.kt
321 lines (254 loc) · 12.9 KB
/
AddressBookServiceTest.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
package rest.addressbook
import com.fasterxml.jackson.databind.ObjectMapper
import org.junit.jupiter.api.Assertions.*
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.context.SpringBootTest.*
import org.springframework.boot.test.web.client.TestRestTemplate
import org.springframework.boot.web.server.LocalServerPort
import org.springframework.http.HttpEntity
import org.springframework.http.HttpMethod
import org.springframework.http.MediaType
import java.net.URI
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
class AddressBookServiceTest {
@LocalServerPort
var port = 0
@Autowired
lateinit var restTemplate: TestRestTemplate
@BeforeEach
fun cleanRepository() {
addressBook.clear()
}
@Test
fun serviceIsAlive() {
// saving the state of the addres book
val initialState =addressBook.personList ;
//check if the request is idempotent
for (i in 1..2) {
// Request the address book
val response = restTemplate.getForEntity("http://localhost:$port/contacts", Array<Person>::class.java)
//check correct response
assertEquals(200, response.statusCode.value())
assertEquals(0, response.body?.size)
}
// check if the request is safe (adress book didn't chenge)
assertEquals(initialState, addressBook.personList)
}
@Test
fun createUser() {
// saving the state of the addres book
var initialSize =addressBook.personList.size;
var oldId = addressBook.nextId
// Prepare data
val juan = Person(name = "Juan")
val juanURI: URI = URI.create("http://localhost:$port/contacts/person/1")
// Create a new user
var response = restTemplate.postForEntity("http://localhost:$port/contacts", juan, Person::class.java)
assertEquals(201, response.statusCode.value())
assertEquals(juanURI, response.headers.location)
assertEquals(MediaType.APPLICATION_JSON, response.headers.contentType)
var juanUpdated = response.body
assertEquals(juan.name, juanUpdated?.name)
assertEquals(1, juanUpdated?.id)
assertEquals(juanURI, juanUpdated?.href)
// Check that the new user exists
response = restTemplate.getForEntity(juanURI, Person::class.java)
assertEquals(200, response.statusCode.value())
assertEquals(MediaType.APPLICATION_JSON, response.headers.contentType)
juanUpdated = response.body
assertEquals(juan.name, juanUpdated?.name)
assertEquals(1, juanUpdated?.id)
assertEquals(juanURI, juanUpdated?.href)
//////////////////////////////////////////////////////////////////////
// Verify that POST /contacts is well implemented by the service, i.e
// complete the test to ensure that it is not safe and not idempotent
//////////////////////////////////////////////////////////////////////
// check if the request is not safe (adress book should chenge)
assertNotEquals(addressBook.personList.size, initialSize)
assertNotEquals(addressBook.nextId, oldId)
//checking if is idempotent
//saving the current state
initialSize =addressBook.personList.size;
oldId = addressBook.nextId
//doing the request
var response2 = restTemplate.postForEntity("http://localhost:$port/contacts", juan, Person::class.java)
//chequing the response is different
assertNotEquals(response,response2)
//check the state has changed
assertNotEquals(addressBook.personList.size, initialSize)
assertNotEquals(addressBook.nextId, oldId)
}
@Test
fun createUsers() {
// Prepare server
val salvador = Person(id = addressBook.nextId(), name = "Salvador")
addressBook.personList.add(salvador)
// Prepare data
val juan = Person(name = "Juan")
val juanURI = URI.create("http://localhost:$port/contacts/person/2")
val maria = Person(name = "Maria")
val mariaURI = URI.create("http://localhost:$port/contacts/person/3")
// Create a new user
var response = restTemplate.postForEntity("http://localhost:$port/contacts", juan, Person::class.java)
assertEquals(201, response.statusCode.value())
assertEquals(juanURI, response.headers.location)
// Create a second user
response = restTemplate.postForEntity("http://localhost:$port/contacts", maria, Person::class.java)
assertEquals(201, response.statusCode.value())
assertEquals(mariaURI, response.headers.location)
assertEquals(MediaType.APPLICATION_JSON, response.headers.contentType)
var mariaUpdated = response.body
assertEquals(maria.name, mariaUpdated?.name)
assertEquals(3, mariaUpdated?.id)
assertEquals(mariaURI, mariaUpdated?.href)
//saving the current state
var initialId = addressBook.nextId
var initialListSize = addressBook.personList.size
// Check that the new user exists
response = restTemplate.getForEntity(mariaURI, Person::class.java)
assertEquals(200, response.statusCode.value())
assertEquals(MediaType.APPLICATION_JSON, response.headers.contentType)
mariaUpdated = response.body
assertEquals(maria.name, mariaUpdated?.name)
assertEquals(3, mariaUpdated?.id)
assertEquals(mariaURI, mariaUpdated?.href)
//////////////////////////////////////////////////////////////////////
// Verify that GET /contacts/person/3 is well implemented by the service, i.e
// complete the test to ensure that it is safe and idempotent
//////////////////////////////////////////////////////////////////////
//checking it is safe
assertEquals(addressBook.personList.size, initialListSize)
assertEquals(addressBook.nextId, initialId)
var response2 = restTemplate.getForEntity(mariaURI, Person::class.java)
//checking it is idempotent
assertEquals(response,response2)
}
@Test
fun listUsers() {
// Prepare server
val salvador = Person(name = "Salvador", id = addressBook.nextId())
val juan = Person(name = "Juan", id = addressBook.nextId())
addressBook.personList.add(salvador)
addressBook.personList.add(juan)
//saving the current state
var initialId = addressBook.nextId
var initialListSize = addressBook.personList.size
// Test list of contacts
val response = restTemplate.getForEntity("http://localhost:$port/contacts", Array<Person>::class.java)
assertEquals(200, response.statusCode.value())
assertEquals(MediaType.APPLICATION_JSON, response.headers.contentType)
assertEquals(2, response.body?.size)
assertEquals(juan.name, response.body?.get(1)?.name)
//////////////////////////////////////////////////////////////////////
// Verify that GET /contacts is well implemented by the service, i.e
// complete the test to ensure that it is safe and idempotent
//////////////////////////////////////////////////////////////////////
//checking it is safe
assertEquals(addressBook.personList.size, initialListSize)
assertEquals(addressBook.nextId, initialId)
var response2 = restTemplate.getForEntity("http://localhost:$port/contacts", Array<Person>::class.java)
//checking it is idempotent
assertEquals(response,response2)
}
@Test
fun updateUsers() {
// Prepare server
val salvador = Person(name = "Salvador", id = addressBook.nextId())
val juan = Person(name = "Juan", id = addressBook.nextId())
val juanURI = URI.create("http://localhost:$port/contacts/person/2")
addressBook.personList.add(salvador)
addressBook.personList.add(juan)
// Update Maria
val maria = Person(name = "Maria")
//Saving the current state
val initialState = addressBook.personList.get(1)
var response = restTemplate.exchange(juanURI, HttpMethod.PUT, HttpEntity(maria), Person::class.java)
assertEquals(204, response.statusCode.value())
//saving the response
var response1 = response
// Verify that the update is real
response = restTemplate.getForEntity(juanURI, Person::class.java)
assertEquals(200, response.statusCode.value())
assertEquals(MediaType.APPLICATION_JSON, response.headers.contentType)
val updatedMaria = response.body
assertEquals(maria.name, updatedMaria?.name)
assertEquals(2, updatedMaria?.id)
assertEquals(juanURI, updatedMaria?.href)
// Verify that only can be updated existing values
restTemplate.execute("http://localhost:$port/contacts/person/3", HttpMethod.PUT,
{
it.headers.contentType = MediaType.APPLICATION_JSON
ObjectMapper().writeValue(it.body, maria)
},
{ assertEquals(404, it.statusCode.value()) }
)
//////////////////////////////////////////////////////////////////////
// Verify that PUT /contacts/person/2 is well implemented by the service, i.e
// complete the test to ensure that it is idempotent but not safe
//////////////////////////////////////////////////////////////////////
//Check that is not safe, the state should have changed
assertNotEquals(addressBook.personList.get(1).name, initialState.name)
//Checking it is idempontent, the response shuold be the same
//Saving the current state
var oldId = addressBook.nextId
var response2 = restTemplate.exchange(juanURI, HttpMethod.PUT, HttpEntity(maria), Person::class.java)
assertEquals(response1,response2)
}
@Test
fun deleteUsers() {
// Prepare server
val salvador = Person(name = "Salvador", id = addressBook.nextId())
val juan = Person(name = "Juan", id = addressBook.nextId())
val juanURI = URI.create("http://localhost:$port/contacts/person/2")
addressBook.personList.add(salvador)
addressBook.personList.add(juan)
//save initial state
var initialState = addressBook.personList.size
// Delete a user
restTemplate.execute(juanURI, HttpMethod.DELETE, {}, { assertEquals(204, it.statusCode.value()) })
// Verify that the user has been deleted
restTemplate.execute(juanURI, HttpMethod.GET, {}, { assertEquals(404, it.statusCode.value()) })
//////////////////////////////////////////////////////////////////////
// Verify that DELETE /contacts/person/2 is well implemented by the service, i.e
// complete the test to ensure that it is idempotent but not safe
//////////////////////////////////////////////////////////////////////
//checking it is not safe, the vauke should have changed
assertNotEquals(addressBook.personList.size, initialState)
//Save teh new state
var stateAfter = addressBook.personList.size
//checkin it is idempotent, the sate should be equal after a second request
restTemplate.execute(juanURI, HttpMethod.DELETE, {}, { assertEquals(204, it.statusCode.value()) })
assertEquals(addressBook.personList.size, stateAfter)
}
@Test
fun findUsers() {
// Prepare server
val salvador = Person(name = "Salvador", id = addressBook.nextId())
val juan = Person(name = "Juan", id = addressBook.nextId())
val salvadorURI = URI.create("http://localhost:$port/contacts/person/1")
val juanURI = URI.create("http://localhost:$port/contacts/person/2")
addressBook.personList.add(salvador)
addressBook.personList.add(juan)
// Test user 1 exists
var response = restTemplate.getForEntity(salvadorURI, Person::class.java)
assertEquals(200, response.statusCode.value())
assertEquals(MediaType.APPLICATION_JSON, response.headers.contentType)
var person = response.body
assertEquals(salvador.name, person?.name)
assertEquals(salvador.id, person?.id)
assertEquals(salvador.href, person?.href)
// Test user 2 exists
response = restTemplate.getForEntity(juanURI, Person::class.java)
assertEquals(200, response.statusCode.value())
assertEquals(MediaType.APPLICATION_JSON, response.headers.contentType)
person = response.body
assertEquals(juan.name, person?.name)
assertEquals(juan.id, person?.id)
assertEquals(juan.href, person?.href)
// Test user 3 doesn't exist
restTemplate.execute("http://localhost:$port/contacts/person/3", HttpMethod.GET, {}, { assertEquals(404, it.statusCode.value()) })
}
}