View Javadoc
1   package petclinic.pets;
2   
3   import static java.util.Arrays.asList;
4   
5   import static org.junit.jupiter.api.Assertions.assertEquals;
6   import static org.junit.jupiter.api.Assertions.assertNotNull;
7   import static org.junit.jupiter.api.Assertions.assertNull;
8   import static org.junit.jupiter.api.Assertions.assertSame;
9   import static org.junit.jupiter.api.Assertions.assertThrows;
10  import static org.junit.jupiter.api.Assertions.assertTrue;
11  import static org.junit.jupiter.api.Assumptions.assumeTrue;
12  
13  import java.util.Calendar;
14  import java.util.Date;
15  import java.util.GregorianCalendar;
16  import java.util.List;
17  
18  import javax.validation.ValidationException;
19  
20  import org.junit.jupiter.api.Test;
21  
22  import petclinic.owners.Owner;
23  import petclinic.owners.OwnerData;
24  import petclinic.util.SUT;
25  import petclinic.util.TestUtil;
26  
27  /**
28   * Integration tests for {@link Pet}-related operations, at the application service level. Each test runs in a database
29   * transaction that is rolled back at the end of the test.
30   */
31  final class PetScreenTest {
32      @TestUtil
33      OwnerData ownerData;
34      @TestUtil
35      PetData petData;
36      @SUT
37      PetScreen petScreen;
38  
39      @Test
40      void findAllPetTypes() {
41          PetType type1 = petData.createType("type1");
42          PetType type2 = petData.createType("Another type");
43  
44          List<PetType> petTypes = petScreen.getTypes();
45          List<PetType> petTypesAgain = petScreen.getTypes();
46  
47          petTypes.retainAll(asList(type1, type2));
48          assertSame(type1, petTypes.get(1));
49          assertSame(type2, petTypes.get(0));
50          assertSame(petTypes, petTypesAgain);
51      }
52  
53      @Test
54      void createPetWithGeneratedId() {
55          String petName = "bowser";
56          Owner owner = ownerData.create("The Owner");
57          assumeTrue(owner.getPet(petName) == null);
58          petScreen.selectOwner(owner.getId());
59  
60          PetType type = petData.findOrCreatePetType("dog");
61          assertEquals("dog", type.getName());
62  
63          petScreen.requestNewPet();
64          Pet pet = petScreen.getPet();
65          pet.setName(petName);
66          pet.setType(type);
67          pet.setBirthDate(new Date());
68          petScreen.createOrUpdatePet();
69  
70          assertNotNull(pet.getId());
71          assertSame(owner, pet.getOwner());
72          assertEquals(1, owner.getPets().size());
73          assertSame(pet, owner.getPet(petName));
74      }
75  
76      @Test
77      void attemptToCreatePetWithDuplicateNameForSameOwner() {
78          Owner owner = ownerData.create("The Owner");
79          petScreen.selectOwner(owner.getId());
80          Date birthDate = new GregorianCalendar(2005, Calendar.AUGUST, 6).getTime();
81          Pet ownedPet = petData.create(owner, "Buck", birthDate, "dog");
82  
83          petScreen.requestNewPet();
84          Pet secondPet = petScreen.getPet();
85          secondPet.setName(ownedPet.getName());
86  
87          ValidationException thrown = assertThrows(ValidationException.class, () -> petScreen.createOrUpdatePet());
88  
89          assertTrue(thrown.getMessage().contains("owner already has a pet with this name"));
90      }
91  
92      @Test
93      void attemptToCreatePetWithoutAnOwnerHavingBeenSelected() {
94          petScreen.createOrUpdatePet();
95  
96          assertNull(petScreen.getPet());
97      }
98  
99      @Test
100     void updatePetName() {
101         Date birthDate = new GregorianCalendar(2005, Calendar.AUGUST, 6).getTime();
102         Pet pet = petData.create("Pet", birthDate, "cat");
103         petScreen.selectPet(pet.getId());
104 
105         String oldName = pet.getName();
106         String newName = oldName + "X";
107         pet.setName(newName);
108         petScreen.createOrUpdatePet();
109 
110         Pet petUpdated = petScreen.getPet();
111         petData.refresh(petUpdated);
112         assertEquals(newName, petUpdated.getName());
113         assertEquals(pet.getBirthDate(), petUpdated.getBirthDate());
114         assertEquals(pet.getType(), petUpdated.getType());
115     }
116 }