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