-
Notifications
You must be signed in to change notification settings - Fork 860
/
AbstractClinicServiceTests.java
502 lines (436 loc) · 17.5 KB
/
AbstractClinicServiceTests.java
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
/*
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.samples.petclinic.service.clinicService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.samples.petclinic.model.*;
import org.springframework.samples.petclinic.service.ClinicService;
import org.springframework.samples.petclinic.util.EntityUtils;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDate;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import static org.assertj.core.api.Assertions.assertThat;
/**
* <p> Base class for {@link ClinicService} integration tests. </p> <p> Subclasses should specify Spring context
* configuration using {@link ContextConfiguration @ContextConfiguration} annotation </p> <p>
* AbstractclinicServiceTests and its subclasses benefit from the following services provided by the Spring
* TestContext Framework: </p> <ul> <li><strong>Spring IoC container caching</strong> which spares us unnecessary set up
* time between test execution.</li> <li><strong>Dependency Injection</strong> of test fixture instances, meaning that
* we don't need to perform application context lookups. See the use of {@link Autowired @Autowired} on the <code>{@link
* AbstractClinicServiceTests#clinicService clinicService}</code> instance variable, which uses autowiring <em>by
* type</em>. <li><strong>Transaction management</strong>, meaning each test method is executed in its own transaction,
* which is automatically rolled back by default. Thus, even if tests insert or otherwise change database state, there
* is no need for a teardown or cleanup script. <li> An {@link org.springframework.context.ApplicationContext
* ApplicationContext} is also inherited and can be used for explicit bean lookup if necessary. </li> </ul>
*
* @author Ken Krebs
* @author Rod Johnson
* @author Juergen Hoeller
* @author Sam Brannen
* @author Michael Isvy
* @author Vitaliy Fedoriv
*/
abstract class AbstractClinicServiceTests {
@Autowired
protected ClinicService clinicService;
@Test
void shouldFindOwnersByLastName() {
Collection<Owner> owners = this.clinicService.findOwnerByLastName("Davis");
assertThat(owners.size()).isEqualTo(2);
owners = this.clinicService.findOwnerByLastName("Daviss");
assertThat(owners.isEmpty()).isTrue();
}
@Test
void shouldFindSingleOwnerWithPet() {
Owner owner = this.clinicService.findOwnerById(1);
assertThat(owner.getLastName()).startsWith("Franklin");
assertThat(owner.getPets().size()).isEqualTo(1);
assertThat(owner.getPets().get(0).getType()).isNotNull();
assertThat(owner.getPets().get(0).getType().getName()).isEqualTo("cat");
}
@Test
@Transactional
void shouldInsertOwner() {
Collection<Owner> owners = this.clinicService.findOwnerByLastName("Schultz");
int found = owners.size();
Owner owner = new Owner();
owner.setFirstName("Sam");
owner.setLastName("Schultz");
owner.setAddress("4, Evans Street");
owner.setCity("Wollongong");
owner.setTelephone("4444444444");
this.clinicService.saveOwner(owner);
assertThat(owner.getId().longValue()).isNotEqualTo(0);
assertThat(owner.getPet("null value")).isNull();
owners = this.clinicService.findOwnerByLastName("Schultz");
assertThat(owners.size()).isEqualTo(found + 1);
}
@Test
@Transactional
void shouldUpdateOwner() {
Owner owner = this.clinicService.findOwnerById(1);
String oldLastName = owner.getLastName();
String newLastName = oldLastName + "X";
owner.setLastName(newLastName);
this.clinicService.saveOwner(owner);
// retrieving new name from database
owner = this.clinicService.findOwnerById(1);
assertThat(owner.getLastName()).isEqualTo(newLastName);
}
@Test
void shouldFindPetWithCorrectId() {
Pet pet7 = this.clinicService.findPetById(7);
assertThat(pet7.getName()).startsWith("Samantha");
assertThat(pet7.getOwner().getFirstName()).isEqualTo("Jean");
}
// @Test
// void shouldFindAllPetTypes() {
// Collection<PetType> petTypes = this.clinicService.findPetTypes();
//
// PetType petType1 = EntityUtils.getById(petTypes, PetType.class, 1);
// assertThat(petType1.getName()).isEqualTo("cat");
// PetType petType4 = EntityUtils.getById(petTypes, PetType.class, 4);
// assertThat(petType4.getName()).isEqualTo("snake");
// }
@Test
@Transactional
void shouldInsertPetIntoDatabaseAndGenerateId() {
Owner owner6 = this.clinicService.findOwnerById(6);
int found = owner6.getPets().size();
Pet pet = new Pet();
pet.setName("bowser");
Collection<PetType> types = this.clinicService.findPetTypes();
pet.setType(EntityUtils.getById(types, PetType.class, 2));
pet.setBirthDate(LocalDate.now());
owner6.addPet(pet);
assertThat(owner6.getPets().size()).isEqualTo(found + 1);
this.clinicService.savePet(pet);
this.clinicService.saveOwner(owner6);
owner6 = this.clinicService.findOwnerById(6);
assertThat(owner6.getPets().size()).isEqualTo(found + 1);
// checks that id has been generated
assertThat(pet.getId()).isNotNull();
}
@Test
@Transactional
void shouldUpdatePetName() throws Exception {
Pet pet7 = this.clinicService.findPetById(7);
String oldName = pet7.getName();
String newName = oldName + "X";
pet7.setName(newName);
this.clinicService.savePet(pet7);
pet7 = this.clinicService.findPetById(7);
assertThat(pet7.getName()).isEqualTo(newName);
}
@Test
void shouldFindVets() {
Collection<Vet> vets = this.clinicService.findVets();
Vet vet = EntityUtils.getById(vets, Vet.class, 3);
assertThat(vet.getLastName()).isEqualTo("Douglas");
assertThat(vet.getNrOfSpecialties()).isEqualTo(2);
assertThat(vet.getSpecialties().get(0).getName()).isEqualTo("dentistry");
assertThat(vet.getSpecialties().get(1).getName()).isEqualTo("surgery");
}
@Test
@Transactional
void shouldAddNewVisitForPet() {
Pet pet7 = this.clinicService.findPetById(7);
int found = pet7.getVisits().size();
Visit visit = new Visit();
pet7.addVisit(visit);
visit.setDescription("test");
this.clinicService.saveVisit(visit);
this.clinicService.savePet(pet7);
pet7 = this.clinicService.findPetById(7);
assertThat(pet7.getVisits().size()).isEqualTo(found + 1);
assertThat(visit.getId()).isNotNull();
}
@Test
void shouldFindVisitsByPetId() throws Exception {
Collection<Visit> visits = this.clinicService.findVisitsByPetId(7);
assertThat(visits.size()).isEqualTo(2);
Visit[] visitArr = visits.toArray(new Visit[visits.size()]);
assertThat(visitArr[0].getPet()).isNotNull();
assertThat(visitArr[0].getDate()).isNotNull();
assertThat(visitArr[0].getPet().getId()).isEqualTo(7);
}
@Test
void shouldFindAllPets(){
Collection<Pet> pets = this.clinicService.findAllPets();
Pet pet1 = EntityUtils.getById(pets, Pet.class, 1);
assertThat(pet1.getName()).isEqualTo("Leo");
Pet pet3 = EntityUtils.getById(pets, Pet.class, 3);
assertThat(pet3.getName()).isEqualTo("Rosy");
}
@Test
@Transactional
void shouldDeletePet(){
Pet pet = this.clinicService.findPetById(1);
this.clinicService.deletePet(pet);
try {
pet = this.clinicService.findPetById(1);
} catch (Exception e) {
pet = null;
}
assertThat(pet).isNull();
}
@Test
void shouldFindVisitDyId(){
Visit visit = this.clinicService.findVisitById(1);
assertThat(visit.getId()).isEqualTo(1);
assertThat(visit.getPet().getName()).isEqualTo("Samantha");
}
@Test
void shouldFindAllVisits(){
Collection<Visit> visits = this.clinicService.findAllVisits();
Visit visit1 = EntityUtils.getById(visits, Visit.class, 1);
assertThat(visit1.getPet().getName()).isEqualTo("Samantha");
Visit visit3 = EntityUtils.getById(visits, Visit.class, 3);
assertThat(visit3.getPet().getName()).isEqualTo("Max");
}
@Test
@Transactional
void shouldInsertVisit() {
Collection<Visit> visits = this.clinicService.findAllVisits();
int found = visits.size();
Pet pet = this.clinicService.findPetById(1);
Visit visit = new Visit();
visit.setPet(pet);
visit.setDate(LocalDate.now());
visit.setDescription("new visit");
this.clinicService.saveVisit(visit);
assertThat(visit.getId().longValue()).isNotEqualTo(0);
visits = this.clinicService.findAllVisits();
assertThat(visits.size()).isEqualTo(found + 1);
}
@Test
@Transactional
void shouldUpdateVisit(){
Visit visit = this.clinicService.findVisitById(1);
String oldDesc = visit.getDescription();
String newDesc = oldDesc + "X";
visit.setDescription(newDesc);
this.clinicService.saveVisit(visit);
visit = this.clinicService.findVisitById(1);
assertThat(visit.getDescription()).isEqualTo(newDesc);
}
@Test
@Transactional
void shouldDeleteVisit(){
Visit visit = this.clinicService.findVisitById(1);
this.clinicService.deleteVisit(visit);
try {
visit = this.clinicService.findVisitById(1);
} catch (Exception e) {
visit = null;
}
assertThat(visit).isNull();
}
@Test
void shouldFindVetDyId(){
Vet vet = this.clinicService.findVetById(1);
assertThat(vet.getFirstName()).isEqualTo("James");
assertThat(vet.getLastName()).isEqualTo("Carter");
}
@Test
@Transactional
void shouldInsertVet() {
Collection<Vet> vets = this.clinicService.findAllVets();
int found = vets.size();
Vet vet = new Vet();
vet.setFirstName("John");
vet.setLastName("Dow");
this.clinicService.saveVet(vet);
assertThat(vet.getId().longValue()).isNotEqualTo(0);
vets = this.clinicService.findAllVets();
assertThat(vets.size()).isEqualTo(found + 1);
}
@Test
@Transactional
void shouldUpdateVet(){
Vet vet = this.clinicService.findVetById(1);
String oldLastName = vet.getLastName();
String newLastName = oldLastName + "X";
vet.setLastName(newLastName);
this.clinicService.saveVet(vet);
vet = this.clinicService.findVetById(1);
assertThat(vet.getLastName()).isEqualTo(newLastName);
}
@Test
@Transactional
void shouldDeleteVet(){
Vet vet = this.clinicService.findVetById(1);
this.clinicService.deleteVet(vet);
try {
vet = this.clinicService.findVetById(1);
} catch (Exception e) {
vet = null;
}
assertThat(vet).isNull();
}
@Test
void shouldFindAllOwners(){
Collection<Owner> owners = this.clinicService.findAllOwners();
Owner owner1 = EntityUtils.getById(owners, Owner.class, 1);
assertThat(owner1.getFirstName()).isEqualTo("George");
Owner owner3 = EntityUtils.getById(owners, Owner.class, 3);
assertThat(owner3.getFirstName()).isEqualTo("Eduardo");
}
@Test
@Transactional
void shouldDeleteOwner(){
Owner owner = this.clinicService.findOwnerById(1);
this.clinicService.deleteOwner(owner);
try {
owner = this.clinicService.findOwnerById(1);
} catch (Exception e) {
owner = null;
}
assertThat(owner).isNull();
}
@Test
void shouldFindPetTypeById(){
PetType petType = this.clinicService.findPetTypeById(1);
assertThat(petType.getName()).isEqualTo("cat");
}
@Test
void shouldFindAllPetTypes(){
Collection<PetType> petTypes = this.clinicService.findAllPetTypes();
PetType petType1 = EntityUtils.getById(petTypes, PetType.class, 1);
assertThat(petType1.getName()).isEqualTo("cat");
PetType petType3 = EntityUtils.getById(petTypes, PetType.class, 3);
assertThat(petType3.getName()).isEqualTo("lizard");
}
@Test
@Transactional
void shouldInsertPetType() {
Collection<PetType> petTypes = this.clinicService.findAllPetTypes();
int found = petTypes.size();
PetType petType = new PetType();
petType.setName("tiger");
this.clinicService.savePetType(petType);
assertThat(petType.getId().longValue()).isNotEqualTo(0);
petTypes = this.clinicService.findAllPetTypes();
assertThat(petTypes.size()).isEqualTo(found + 1);
}
@Test
@Transactional
void shouldUpdatePetType(){
PetType petType = this.clinicService.findPetTypeById(1);
String oldLastName = petType.getName();
String newLastName = oldLastName + "X";
petType.setName(newLastName);
this.clinicService.savePetType(petType);
petType = this.clinicService.findPetTypeById(1);
assertThat(petType.getName()).isEqualTo(newLastName);
}
@Test
@Transactional
void shouldDeletePetType(){
PetType petType = this.clinicService.findPetTypeById(1);
this.clinicService.deletePetType(petType);
try {
petType = this.clinicService.findPetTypeById(1);
} catch (Exception e) {
petType = null;
}
assertThat(petType).isNull();
}
@Test
void shouldFindSpecialtyById(){
Specialty specialty = this.clinicService.findSpecialtyById(1);
assertThat(specialty.getName()).isEqualTo("radiology");
}
@Test
void shouldFindAllSpecialtys(){
Collection<Specialty> specialties = this.clinicService.findAllSpecialties();
Specialty specialty1 = EntityUtils.getById(specialties, Specialty.class, 1);
assertThat(specialty1.getName()).isEqualTo("radiology");
Specialty specialty3 = EntityUtils.getById(specialties, Specialty.class, 3);
assertThat(specialty3.getName()).isEqualTo("dentistry");
}
@Test
@Transactional
void shouldInsertSpecialty() {
Collection<Specialty> specialties = this.clinicService.findAllSpecialties();
int found = specialties.size();
Specialty specialty = new Specialty();
specialty.setName("dermatologist");
this.clinicService.saveSpecialty(specialty);
assertThat(specialty.getId().longValue()).isNotEqualTo(0);
specialties = this.clinicService.findAllSpecialties();
assertThat(specialties.size()).isEqualTo(found + 1);
}
@Test
@Transactional
void shouldUpdateSpecialty(){
Specialty specialty = this.clinicService.findSpecialtyById(1);
String oldLastName = specialty.getName();
String newLastName = oldLastName + "X";
specialty.setName(newLastName);
this.clinicService.saveSpecialty(specialty);
specialty = this.clinicService.findSpecialtyById(1);
assertThat(specialty.getName()).isEqualTo(newLastName);
}
@Test
@Transactional
void shouldDeleteSpecialty(){
Specialty specialty = new Specialty();
specialty.setName("test");
this.clinicService.saveSpecialty(specialty);
Integer specialtyId = specialty.getId();
assertThat(specialtyId).isNotNull();
specialty = this.clinicService.findSpecialtyById(specialtyId);
assertThat(specialty).isNotNull();
this.clinicService.deleteSpecialty(specialty);
try {
specialty = this.clinicService.findSpecialtyById(specialtyId);
} catch (Exception e) {
specialty = null;
}
assertThat(specialty).isNull();
}
@Test
@Transactional
void shouldFindSpecialtiesByNameIn() {
Specialty specialty1 = new Specialty();
specialty1.setName("radiology");
specialty1.setId(1);
Specialty specialty2 = new Specialty();
specialty2.setName("surgery");
specialty2.setId(2);
Specialty specialty3 = new Specialty();
specialty3.setName("dentistry");
specialty3.setId(3);
List<Specialty> expectedSpecialties = List.of(specialty1, specialty2, specialty3);
Set<String> specialtyNames = expectedSpecialties.stream()
.map(Specialty::getName)
.collect(Collectors.toSet());
Collection<Specialty> actualSpecialties = this.clinicService.findSpecialtiesByNameIn(specialtyNames);
assertThat(actualSpecialties).isNotNull();
assertThat(actualSpecialties.size()).isEqualTo(expectedSpecialties.size());
for (Specialty expected : expectedSpecialties) {
assertThat(actualSpecialties.stream()
.anyMatch(
actual -> actual.getName().equals(expected.getName())
&& actual.getId().equals(expected.getId()))).isTrue();
}
}
}