11package com .capgemini .training .appointmentbooking .dataaccess .repository ;
22
3+ import com .capgemini .training .appointmentbooking .common .BaseTest ;
34import com .capgemini .training .appointmentbooking .common .datatype .AppointmentStatus ;
45import com .capgemini .training .appointmentbooking .dataaccess .entity .AppointmentEntity ;
56import com .capgemini .training .appointmentbooking .dataaccess .entity .ClientEntity ;
1920import java .util .List ;
2021import java .util .Optional ;
2122
22- import static org .assertj .core .api .Assertions .assertThat ;
23- import static org .junit .jupiter .api .Assertions .*;
24-
2523@ DataJpaTest
26- public class AppointmentRepositoryTest {
24+ public class AppointmentRepositoryTest extends BaseTest {
2725
2826 @ Autowired
2927 private AppointmentRepository appointmentRepository ;
@@ -32,38 +30,44 @@ public class AppointmentRepositoryTest {
3230 private EntityManager entityManager ;
3331
3432 @ Test
35- void testFindAll () {
33+ void shouldFindAll () {
3634 // given
3735 // when
3836 List <AppointmentEntity > result = appointmentRepository .findAll ();
3937
4038 // then
41- assertThat (result ).isNotEmpty ();
4239 assertThat (result ).hasSize (20 );
4340 }
4441
4542 @ Test
46- void testFindByCriteria_findAppointmentsByTreatmentName () {
43+ void shouldFindAppointmentsByTreatmentName () {
4744 // given
48- AppointmentCriteria criteria = new AppointmentCriteria ("Konsultacja pediatryczna" , null , null , null , null , null );
45+ AppointmentCriteria criteria = AppointmentCriteria
46+ .builder ()
47+ .treatmentName ("Konsultacja pediatryczna" )
48+ .build ();
4949
5050 // when
5151 List <AppointmentEntity > result = appointmentRepository .findByCriteria (criteria , entityManager );
5252
5353 // then
54- assertThat (result ).isNotEmpty (). hasSize (2 );
54+ assertThat (result ).hasSize (2 );
5555 }
5656
5757 @ Test
58- void testFindByCriteria_findAppointmentsByTreatmentNameAndStartDate () {
58+ void shouldFindAppointmentsByTreatmentNameAndStartDate () {
5959 // given
60- AppointmentCriteria criteria = new AppointmentCriteria ("Konsultacja pediatryczna" , toInstant ("2024-03-03 14:00:00" ), null , null , null , null );
60+ AppointmentCriteria criteria = AppointmentCriteria
61+ .builder ()
62+ .treatmentName ("Konsultacja pediatryczna" )
63+ .startDate (toInstant ("2024-03-03 14:00:00" ))
64+ .build ();
6165
6266 // when
6367 List <AppointmentEntity > result = appointmentRepository .findByCriteria (criteria , entityManager );
6468
6569 // then
66- assertThat (result ).isNotEmpty (). hasSize (2 );
70+ assertThat (result ).hasSize (2 );
6771 }
6872
6973 private Instant toInstant (String date ) {
@@ -72,59 +76,74 @@ private Instant toInstant(String date) {
7276 }
7377
7478 @ Test
75- void testFindByCriteria_findAppointmentsByTreatmentNameAndBetweenStartDateAndEndDate () {
79+ void shouldFindAppointmentsByTreatmentNameAndBetweenStartDateAndEndDate () {
7680 // given
77- AppointmentCriteria criteria = new AppointmentCriteria ("Konsultacja pediatryczna" , toInstant ("2024-03-03 14:00:00" ), toInstant ("2024-03-03 15:00:00" ), null , null , null );
81+ AppointmentCriteria criteria = AppointmentCriteria
82+ .builder ()
83+ .treatmentName ("Konsultacja pediatryczna" )
84+ .startDate (toInstant ("2024-03-03 14:00:00" ))
85+ .endDate (toInstant ("2024-03-03 15:00:00" ))
86+ .build ();
7887
7988 // when
8089 List <AppointmentEntity > result = appointmentRepository .findByCriteria (criteria , entityManager );
8190
8291 // then
83- assertThat (result ).isNotEmpty (). hasSize (1 );
92+ assertThat (result ).hasSize (1 );
8493 }
8594
8695 @ Test
87- void testFindByCriteria_findAppointmentsByTreatmentNameAndAppointmentStatus () {
96+ void shouldFindAppointmentsByTreatmentNameAndAppointmentStatus () {
8897 // given
89- AppointmentCriteria criteria = new AppointmentCriteria ("Konsultacja pediatryczna" , null , null , AppointmentStatus .CANCELLED , null , null );
98+ AppointmentCriteria criteria = AppointmentCriteria
99+ .builder ()
100+ .treatmentName ("Konsultacja pediatryczna" )
101+ .status (AppointmentStatus .CANCELLED )
102+ .build ();
90103
91104 // when
92105 List <AppointmentEntity > result = appointmentRepository .findByCriteria (criteria , entityManager );
93106
94107 // then
95- assertThat (result ).isNotEmpty (). hasSize (2 );
108+ assertThat (result ).hasSize (2 );
96109 }
97110
98111 @ Test
99- void testFindByCriteria_findAppointmentsByClientId () {
112+ void shouldFindAppointmentsByClientId () {
100113 // given
101114 Long clientId = -1L ;
102- AppointmentCriteria criteria = new AppointmentCriteria (null , null , null , null , clientId , null );
115+ AppointmentCriteria criteria = AppointmentCriteria
116+ .builder ()
117+ .clientId (clientId )
118+ .build ();
103119
104120 // when
105121 List <AppointmentEntity > result = appointmentRepository .findByCriteria (criteria , entityManager );
106122
107123 // then
108- assertThat (result ).isNotEmpty (). hasSize (5 );
124+ assertThat (result ).hasSize (5 );
109125 assertThat (result ).extracting (AppointmentEntity ::getClient ).extracting (ClientEntity ::getId ).containsOnly (clientId );
110126 }
111127
112128 @ Test
113- void testFindByCriteria_findAppointmentsBySpecialistId () {
129+ void shouldFindAppointmentsBySpecialistId () {
114130 // given
115131 Long specialistId = -1L ;
116- AppointmentCriteria criteria = new AppointmentCriteria (null , null , null , null , null , specialistId );
132+ AppointmentCriteria criteria = AppointmentCriteria
133+ .builder ()
134+ .specialistId (specialistId )
135+ .build ();
117136
118137 // when
119138 List <AppointmentEntity > result = appointmentRepository .findByCriteria (criteria , entityManager );
120139
121140 // then
122- assertThat (result ).isNotEmpty (). hasSize (4 );
141+ assertThat (result ).hasSize (4 );
123142 assertThat (result ).extracting (AppointmentEntity ::getTreatment ).extracting (TreatmentEntity ::getSpecialist ).extracting (SpecialistEntity ::getId ).containsOnly (specialistId );
124143 }
125144
126145 @ Test
127- void testFindByDateTimeBetweenAndStatus () {
146+ void shouldFindAppointmentsByDateTimeBetweenAndStatus () {
128147 // given
129148 Instant startDate = toInstant ("2024-03-10 00:00:00" );
130149 Instant endDate = toInstant ("2024-03-14 23:59:59" );
@@ -134,58 +153,56 @@ void testFindByDateTimeBetweenAndStatus() {
134153 List <AppointmentEntity > result = appointmentRepository .findByDateTimeBetweenAndStatus (startDate , endDate , status );
135154
136155 // then
137- assertThat (result ).isNotEmpty (). hasSize (3 );
156+ assertThat (result ).hasSize (3 );
138157 assertThat (result ).extracting (AppointmentEntity ::getStatus ).containsOnly (status );
139158 }
140159
141160 @ Test
142- void testFindById () {
161+ void shouldFindAppointmentById () {
143162 // given
144163 Long appointmentId = -1L ;
145164
146165 // when
147166 Optional <AppointmentEntity > result = appointmentRepository .findById (appointmentId );
148167
149168 // then
150- assertTrue (result .isPresent (), "AppointmentEntity should be present" );
151-
152- AppointmentEntity appointment = result .get ();
153- assertNotNull (appointment );
154- assertEquals (appointmentId , appointment .getId ());
155- assertEquals (AppointmentStatus .SCHEDULED , appointment .getStatus ());
156- assertNotNull (appointment .getClient ());
157- assertNotNull (appointment .getTreatment ());
158- assertEquals (toInstant ("2024-03-01 09:00:00" ), appointment .getDateTime ());
169+ assertThat (result ).isPresent ().hasValueSatisfying (appointment -> {
170+ assertThat (appointment .getId ()).isEqualTo (appointmentId );
171+ assertThat (appointment .getStatus ()).isEqualTo (AppointmentStatus .SCHEDULED );
172+ assertThat (appointment .getClient ()).isNotNull ();
173+ assertThat (appointment .getTreatment ()).isNotNull ();
174+ assertThat (appointment .getDateTime ()).isEqualTo (toInstant ("2024-03-01 09:00:00" ));
175+ });
159176 }
160177
161178 @ Test
162- void testFindAppointmentsBySpecialistIdBeforeDate () {
163- // Given
179+ void shouldFindAppointmentsBySpecialistIdBeforeDate () {
180+ // given
164181 Long specialistId = -1L ;
165182 Instant date = toInstant ("2024-03-12 09:00:00" );
166183
167- // When
184+ // when
168185 List <AppointmentEntity > appointments = appointmentRepository .findAppointmentsBySpecialistIdBeforeDate (specialistId , date );
169186
170- // Then
171- assertNotNull (appointments , "Appointments list should not be null" );
172- assertEquals ( 2 , appointments . size (), "Expected 2 past appointments" );
173- appointments . forEach (a -> {
174- assertEquals ( specialistId , a . getTreatment (). getSpecialist (). getId (), "Appointment belongs to correct specialist" );
175- assertTrue ( a .getDateTime ().isBefore (date ), "Appointment should be in the past" );
176- } );
187+ // then
188+ assertThat (appointments )
189+ . hasSize ( 2 )
190+ . allMatch (a -> a . getTreatment (). getSpecialist (). getId (). equals ( specialistId ),
191+ "All appointments belong to the correct specialist" )
192+ . allMatch ( a -> a .getDateTime ().isBefore (date ),
193+ "All appointments should be before the given date" );
177194 }
178195
179196 @ Test
180- void testFindConflictedAppointments () {
181- // Given
197+ void shouldFindConflictedAppointment () {
198+ // given
182199 Long specialistId = -1L ;
183200 Instant date = toInstant ("2024-03-05 11:45:00" );
184201
185- // When
202+ // when
186203 boolean conflict = appointmentRepository .hasConflictingAppointmentBySpecialistIdAndDateTime (specialistId , date );
187204
188- // Then
189- assertTrue (conflict , "Should find conflicted appointments" );
205+ // then
206+ assertThat (conflict ). isTrue ( );
190207 }
191208}
0 commit comments