22
33import com .objectcomputing .checkins .exceptions .BadArgException ;
44import com .objectcomputing .checkins .services .TestContainersSuite ;
5+ import com .objectcomputing .checkins .services .fixture .MemberProfileFixture ;
6+ import com .objectcomputing .checkins .services .fixture .CheckInDocumentFixture ;
7+ import com .objectcomputing .checkins .services .fixture .CheckInFixture ;
58import com .objectcomputing .checkins .services .checkins .CheckIn ;
6- import com .objectcomputing .checkins .services .checkins .CheckInRepository ;
7- import com .objectcomputing .checkins .services .memberprofile .currentuser .CurrentUserServices ;
8- import org .junit .jupiter .api .AfterAll ;
9- import org .junit .jupiter .api .BeforeAll ;
9+ import com .objectcomputing .checkins .services .memberprofile .MemberProfile ;
10+ import com .objectcomputing .checkins .services .role .RoleType ;
11+
1012import org .junit .jupiter .api .BeforeEach ;
1113import org .junit .jupiter .api .Test ;
12- import org .junit .jupiter .api .condition .DisabledInNativeImage ;
13- import org .mockito .InjectMocks ;
14- import org .mockito .Mock ;
15- import org .mockito .MockitoAnnotations ;
1614
17- import java .util .Optional ;
15+ import jakarta .inject .Inject ;
16+ import io .micronaut .context .annotation .Property ;
17+ import io .micronaut .core .util .StringUtils ;
18+
1819import java .util .Set ;
1920import java .util .UUID ;
21+ import java .util .ArrayList ;
2022
2123import static org .junit .jupiter .api .Assertions .assertEquals ;
2224import static org .junit .jupiter .api .Assertions .assertNull ;
2325import static org .junit .jupiter .api .Assertions .assertThrows ;
2426import static org .junit .jupiter .api .Assertions .assertTrue ;
25- import static org .mockito .Mockito .any ;
26- import static org .mockito .Mockito .eq ;
27- import static org .mockito .Mockito .never ;
28- import static org .mockito .Mockito .reset ;
29- import static org .mockito .Mockito .times ;
30- import static org .mockito .Mockito .verify ;
31- import static org .mockito .Mockito .when ;
32-
33- // Disabled in nativeTest, as we get an exception from Mockito
34- // => org.graalvm.nativeimage.MissingReflectionRegistrationError: The program tried to reflectively access the proxy class
35- // inheriting [org.mockito.plugins.MockMaker] without it being registered for runtime reflection
36- @ DisabledInNativeImage
37- class CheckinDocumentServiceImplTest extends TestContainersSuite {
38-
39- @ Mock
40- private CheckInRepository checkinRepository ;
41-
42- @ Mock
43- private CheckinDocumentRepository checkinDocumentRepository ;
44-
45- @ Mock
46- private CurrentUserServices currentUserServices ;
47-
48- @ InjectMocks
49- private CheckinDocumentServicesImpl services ;
5027
51- private AutoCloseable mockFinalizer ;
28+ class CheckinDocumentServiceImplTest extends TestContainersSuite
29+ implements MemberProfileFixture , CheckInFixture , CheckInDocumentFixture {
30+ @ Inject
31+ private CheckinDocumentServicesImpl services ;
5232
53- @ BeforeAll
54- void initMocks () {
55- mockFinalizer = MockitoAnnotations .openMocks (this );
56- }
33+ private MemberProfile pdl ;
34+ private MemberProfile member ;
35+ private CheckIn checkIn ;
5736
5837 @ BeforeEach
59- void resetMocks () {
60- reset (checkinRepository , checkinDocumentRepository , currentUserServices );
61- }
62-
63- @ AfterAll
64- void close () throws Exception {
65- mockFinalizer .close ();
38+ void reset () {
39+ pdl = createADefaultMemberProfile ();
40+ member = createADefaultMemberProfileForPdl (pdl );
41+ checkIn = createADefaultCheckIn (member , pdl );
6642 }
6743
6844 @ Test
6945 void testRead () {
70- CheckinDocument cd = new CheckinDocument (UUID .randomUUID (), UUID .randomUUID (), "exampleDocId" );
71-
7246 Set <CheckinDocument > checkinDocumentSet = Set .of (
73- new CheckinDocument ( UUID . randomUUID (), UUID . randomUUID () , "doc1" ),
74- new CheckinDocument ( UUID . randomUUID (), UUID . randomUUID () , "doc2" ),
75- new CheckinDocument ( UUID . randomUUID (), UUID . randomUUID () , "doc3" )
47+ createACustomCheckInDocument ( checkIn , "doc1" ),
48+ createACustomCheckInDocument ( checkIn , "doc2" ),
49+ createACustomCheckInDocument ( checkIn , "doc3" )
7650 );
7751
78- when (checkinDocumentRepository .findByCheckinsId (cd .getCheckinsId ())).thenReturn (checkinDocumentSet );
79-
80- assertEquals (checkinDocumentSet , services .read (cd .getCheckinsId ()));
81-
82- verify (checkinDocumentRepository , times (1 )).findByCheckinsId (any (UUID .class ));
52+ assertEquals (checkinDocumentSet , services .read (checkIn .getId ()));
8353 }
8454
8555 @ Test
8656 void testReadNullId () {
8757 assertTrue (services .read (null ).isEmpty ());
88-
89- verify (checkinDocumentRepository , never ()).findByCheckinsId (any (UUID .class ));
9058 }
9159
9260 @ Test
9361 void testFindByUploadDocId () {
94-
95- CheckinDocument cd = new CheckinDocument (UUID .randomUUID (), UUID .randomUUID (), "exampleDocId" );
96- when (checkinDocumentRepository .findByUploadDocId (any (String .class ))).thenReturn (Optional .of (cd ));
62+ CheckinDocument cd = createADefaultCheckInDocument (checkIn );
9763 assertEquals (cd , services .getFindByUploadDocId (cd .getUploadDocId ()));
98- verify (checkinDocumentRepository , times (1 )).findByUploadDocId (any (String .class ));
9964 }
10065
10166 @ Test
10267 void testFindByUploadDocIdWhenRecordDoesNotExist () {
103-
10468 String id = "some.id" ;
105- when (checkinDocumentRepository .findByUploadDocId (any (String .class ))).thenReturn (Optional .empty ());
10669 BadArgException exception = assertThrows (BadArgException .class , () -> services .getFindByUploadDocId (id ));
10770 assertEquals (String .format ("CheckinDocument with document id %s does not exist" , id ), exception .getMessage ());
108- verify (checkinDocumentRepository , times (1 )).findByUploadDocId (any (String .class ));
10971 }
11072
11173 @ Test
11274 void testSave () {
113- CheckinDocument cd = new CheckinDocument (UUID .randomUUID (), "docId" );
114- CheckIn checkin = new CheckIn ();
115-
116- when (checkinRepository .findById (cd .getCheckinsId ())).thenReturn (Optional .of (checkin ));
117- when (checkinDocumentRepository .save (cd )).thenReturn (cd );
118-
75+ CheckinDocument cd = new CheckinDocument (checkIn .getId (), "doc1" );
11976 assertEquals (cd , services .save (cd ));
120-
121- verify (checkinRepository , times (1 )).findById (any (UUID .class ));
122- verify (checkinDocumentRepository , times (1 )).save (any (CheckinDocument .class ));
12377 }
12478
12579 @ Test
12680 void testSaveWithId () {
127- CheckinDocument cd = new CheckinDocument ( UUID . randomUUID (), UUID . randomUUID (), "docId" );
81+ CheckinDocument cd = createADefaultCheckInDocument ( checkIn );
12882
12983 BadArgException exception = assertThrows (BadArgException .class , () -> services .save (cd ));
13084 assertEquals (String .format ("Found unexpected CheckinDocument id %s, please try updating instead" , cd .getId ()), exception .getMessage ());
131-
132- verify (checkinDocumentRepository , never ()).save (any (CheckinDocument .class ));
133- verify (checkinRepository , never ()).findById (any (UUID .class ));
13485 }
13586
13687 @ Test
@@ -139,85 +90,51 @@ void testSaveCheckinDocumentNullCheckinsId() {
13990
14091 BadArgException exception = assertThrows (BadArgException .class , () -> services .save (cd ));
14192 assertEquals (String .format ("Invalid CheckinDocument %s" , cd ), exception .getMessage ());
142-
143- verify (checkinDocumentRepository , never ()).save (any (CheckinDocument .class ));
144- verify (checkinRepository , never ()).findById (any (UUID .class ));
14593 }
14694
14795 @ Test
14896 void testSaveCheckinDocumentNullUploadDocId () {
149- CheckinDocument cd = new CheckinDocument (UUID . randomUUID (), null );
97+ CheckinDocument cd = new CheckinDocument (checkIn . getId (), null );
15098
15199 BadArgException exception = assertThrows (BadArgException .class , () -> services .save (cd ));
152100 assertEquals (String .format ("Invalid CheckinDocument %s" , cd ), exception .getMessage ());
153-
154- verify (checkinDocumentRepository , never ()).save (any (CheckinDocument .class ));
155- verify (checkinRepository , never ()).findById (any (UUID .class ));
156101 }
157102
158103 @ Test
159104 void testSaveNullCheckinDocument () {
160105 assertNull (services .save (null ));
161-
162- verify (checkinDocumentRepository , never ()).save (any (CheckinDocument .class ));
163- verify (checkinRepository , never ()).findById (any (UUID .class ));
164106 }
165107
166108 @ Test
167109 void testSaveCheckinDocumentNonExistingCheckIn () {
168110 CheckinDocument cd = new CheckinDocument (UUID .randomUUID (), "docId" );
169111
170- when (checkinRepository .findById (cd .getCheckinsId ())).thenReturn (Optional .empty ());
171-
172112 BadArgException exception = assertThrows (BadArgException .class , () -> services .save (cd ));
173113 assertEquals (String .format ("CheckIn %s doesn't exist" , cd .getCheckinsId ()), exception .getMessage ());
174-
175- verify (checkinDocumentRepository , never ()).save (any (CheckinDocument .class ));
176- verify (checkinRepository , times (1 )).findById (any (UUID .class ));
177114 }
178115
179116 @ Test
180117 void testSaveCheckinDocumentExistingUploadDocId () {
181- CheckinDocument cd = new CheckinDocument (UUID .randomUUID (), "docId" );
182-
183- CheckIn checkin = new CheckIn ();
184-
185- when (checkinRepository .findById (cd .getCheckinsId ())).thenReturn (Optional .of (checkin ));
186- when (checkinDocumentRepository .findByUploadDocId (cd .getUploadDocId ())).thenReturn (Optional .of (cd ));
118+ String docId = "doc1" ;
119+ CheckinDocument existing = createACustomCheckInDocument (checkIn , docId );
187120
121+ CheckinDocument cd = new CheckinDocument (checkIn .getId (), docId );
188122 BadArgException exception = assertThrows (BadArgException .class , () -> services .save (cd ));
189123 assertEquals (String .format ("CheckinDocument with document ID %s already exists" , cd .getUploadDocId ()), exception .getMessage ());
190-
191- verify (checkinDocumentRepository , never ()).save (any (CheckinDocument .class ));
192- verify (checkinRepository , times (1 )).findById (any (UUID .class ));
193124 }
194125
195126 @ Test
196127 void testUpdate () {
197- CheckinDocument cd = new CheckinDocument (UUID .randomUUID (), UUID .randomUUID (), "docId" );
198- CheckIn checkin = new CheckIn ();
199-
200- when (checkinRepository .findById (eq (cd .getCheckinsId ()))).thenReturn (Optional .of (checkin ));
201- when (checkinDocumentRepository .findById (cd .getId ())).thenReturn (Optional .of (cd ));
202- when (checkinDocumentRepository .update (eq (cd ))).thenReturn (cd );
203-
128+ CheckinDocument cd = createADefaultCheckInDocument (checkIn );
204129 assertEquals (cd , services .update (cd ));
205-
206- verify (checkinRepository , times (1 )).findById (any (UUID .class ));
207- verify (checkinDocumentRepository , times (1 )).findById (cd .getId ());
208- verify (checkinDocumentRepository , times (1 )).update (any (CheckinDocument .class ));
209130 }
210131
211132 @ Test
212133 void testUpdateWithoutId () {
213- CheckinDocument cd = new CheckinDocument (UUID . randomUUID (), "docId" );
134+ CheckinDocument cd = new CheckinDocument (checkIn . getId (), "docId" );
214135
215136 BadArgException exception = assertThrows (BadArgException .class , () -> services .update (cd ));
216137 assertEquals (String .format ("CheckinDocument id %s not found, please try inserting instead" , cd .getId ()), exception .getMessage ());
217-
218- verify (checkinRepository , never ()).findById (any (UUID .class ));
219- verify (checkinDocumentRepository , never ()).update (any (CheckinDocument .class ));
220- verify (checkinDocumentRepository , never ()).findById (any (UUID .class ));
221138 }
222139
223140 @ Test
@@ -226,106 +143,65 @@ void testUpdateCheckinDocumentNullCheckinsId() {
226143
227144 BadArgException exception = assertThrows (BadArgException .class , () -> services .update (cd ));
228145 assertEquals (String .format ("Invalid CheckinDocument %s" , cd ), exception .getMessage ());
229-
230- verify (checkinRepository , never ()).findById (any (UUID .class ));
231- verify (checkinDocumentRepository , never ()).update (any (CheckinDocument .class ));
232- verify (checkinDocumentRepository , never ()).findById (any (UUID .class ));
233146 }
234147
235148 @ Test
236149 void testUpdateCheckinDocumentNullUploadDocId () {
237- CheckinDocument cd = new CheckinDocument (UUID . randomUUID (), null );
150+ CheckinDocument cd = new CheckinDocument (checkIn . getId (), null );
238151
239152 BadArgException exception = assertThrows (BadArgException .class , () -> services .update (cd ));
240153 assertEquals (String .format ("Invalid CheckinDocument %s" , cd ), exception .getMessage ());
241-
242- verify (checkinRepository , never ()).findById (any (UUID .class ));
243- verify (checkinDocumentRepository , never ()).update (any (CheckinDocument .class ));
244- verify (checkinDocumentRepository , never ()).findById (any (UUID .class ));
245154 }
246155
247156 @ Test
248157 void testUpdateCheckinDocumentDoesNotExist () {
249158 CheckinDocument cd = new CheckinDocument (UUID .randomUUID (), UUID .randomUUID (), "docId" );
250- when (checkinDocumentRepository .findById (cd .getCheckinsId ())).thenReturn (Optional .empty ());
251159
252160 BadArgException exception = assertThrows (BadArgException .class , () -> services .update (cd ));
253161 assertEquals (String .format ("CheckinDocument id %s not found, please try inserting instead" , cd .getId ()), exception .getMessage ());
254-
255- verify (checkinRepository , never ()).findById (any (UUID .class ));
256- verify (checkinDocumentRepository , never ()).update (any (CheckinDocument .class ));
257- verify (checkinDocumentRepository , times (1 )).findById (any (UUID .class ));
258162 }
259163
260164 @ Test
261165 void testUpdateCheckInDoesNotExist () {
262- CheckinDocument cd = new CheckinDocument ( UUID . randomUUID (), UUID . randomUUID (), "docId" );
263- when ( checkinDocumentRepository . findById ( cd . getId ())). thenReturn ( Optional . of ( cd ));
264- when ( checkinRepository . findById ( cd . getCheckinsId ())). thenReturn ( Optional . empty () );
166+ CheckinDocument existing = createADefaultCheckInDocument ( checkIn );
167+ CheckinDocument cd = new CheckinDocument ( existing . getId (),
168+ UUID . randomUUID (), "docId" );
265169
266170 BadArgException exception = assertThrows (BadArgException .class , () -> services .update (cd ));
267171 assertEquals (String .format ("CheckIn %s doesn't exist" , cd .getCheckinsId ()), exception .getMessage ());
268-
269- verify (checkinRepository , times (1 )).findById (any (UUID .class ));
270- verify (checkinDocumentRepository , never ()).update (any (CheckinDocument .class ));
271- verify (checkinDocumentRepository , times (1 )).findById (any (UUID .class ));
272172 }
273173
274174 @ Test
275175 void testUpdateNullCheckinDocument () {
276176 assertNull (services .update (null ));
277-
278- verify (checkinRepository , never ()).findById (any (UUID .class ));
279- verify (checkinDocumentRepository , never ()).update (any (CheckinDocument .class ));
280- verify (checkinDocumentRepository , never ()).findById (any (UUID .class ));
281177 }
282178
283179 @ Test
284180 void testDeleteByCheckinId () {
285- when (checkinDocumentRepository .existsByCheckinsId (any (UUID .class ))).thenReturn (true );
286- when (currentUserServices .isAdmin ()).thenReturn (true );
287-
288- services .deleteByCheckinId (UUID .randomUUID ());
289-
290- verify (checkinDocumentRepository , times (1 )).deleteByCheckinsId (any (UUID .class ));
181+ CheckinDocument toBeDeleted = createADefaultCheckInDocument (checkIn );
182+ services .deleteByCheckinId (checkIn .getId ());
291183 }
292184
293185 @ Test
294186 void testDeleteNonExistingCheckinsId () {
295187 UUID uuid = UUID .randomUUID ();
296188
297- when (checkinDocumentRepository .existsByCheckinsId (any (UUID .class ))).thenReturn (false );
298- when (currentUserServices .isAdmin ()).thenReturn (true );
299-
300189 BadArgException exception = assertThrows (BadArgException .class , () -> services .deleteByCheckinId (uuid ));
301190 assertEquals (String .format ("CheckinDocument with CheckinsId %s does not exist" , uuid ), exception .getMessage ());
302-
303- verify (checkinDocumentRepository , times (0 )).deleteByCheckinsId (any (UUID .class ));
304191 }
305192
306193 @ Test
307194 void testDeleteByUploadDocId () {
308-
309- when (checkinDocumentRepository .existsByUploadDocId (any (String .class ))).thenReturn (true );
310- when (currentUserServices .isAdmin ()).thenReturn (true );
311-
312- services .deleteByUploadDocId ("Test.Upload.Doc.Id" );
313-
314- verify (checkinDocumentRepository , times (1 )).deleteByUploadDocId (any (String .class ));
315- verify (checkinDocumentRepository , times (1 )).existsByUploadDocId (any (String .class ));
195+ CheckinDocument toBeDeleted = createADefaultCheckInDocument (checkIn );
196+ services .deleteByUploadDocId (toBeDeleted .getUploadDocId ());
316197 }
317198
318199 @ Test
319200 void testDeleteNonExistingUploadDocId () {
320201 String id = "Test.Id" ;
321- when (checkinDocumentRepository .existsByUploadDocId (any (String .class ))).thenReturn (false );
322- when (currentUserServices .isAdmin ()).thenReturn (true );
323-
324202 BadArgException exception = assertThrows (BadArgException .class , () -> services .deleteByUploadDocId (id ));
325203
326204 assertEquals (String .format ("CheckinDocument with uploadDocId %s does not exist" , id ), exception .getMessage ());
327- verify (checkinDocumentRepository , times (0 )).deleteByUploadDocId (any (String .class ));
328- verify (checkinDocumentRepository , times (1 )).existsByUploadDocId (any (String .class ));
329205 }
330206}
331207
0 commit comments