11package com .objectcomputing .checkins .services .memberprofile .memberphoto ;
22
33import com .google .api .client .googleapis .json .GoogleJsonResponseException ;
4- import com .google .api .services .directory .Directory ;
54import com .google .api .services .directory .model .UserPhoto ;
65import com .objectcomputing .checkins .services .TestContainersSuite ;
6+ import com .objectcomputing .checkins .services .GooglePhotoAccessorImplReplacement ;
77import com .objectcomputing .checkins .services .memberprofile .MemberProfileServices ;
8- import com .objectcomputing .checkins .util .googleapiaccess .GoogleApiAccess ;
9- import io .micronaut .context .env .Environment ;
10- import org .junit .jupiter .api .AfterAll ;
11- import org .junit .jupiter .api .BeforeAll ;
8+ import io .micronaut .core .util .StringUtils ;
9+ import io .micronaut .context .annotation .Property ;
1210import org .junit .jupiter .api .BeforeEach ;
1311import org .junit .jupiter .api .Test ;
14- import org .junit .jupiter .api .condition .DisabledInNativeImage ;
15- import org .mockito .InjectMocks ;
16- import org .mockito .Mock ;
17- import org .mockito .MockitoAnnotations ;
12+ import jakarta .inject .Inject ;
1813
1914import java .io .IOException ;
2015import java .nio .charset .StandardCharsets ;
2116import java .util .Base64 ;
2217
2318import static org .junit .jupiter .api .Assertions .assertEquals ;
2419import static org .junit .jupiter .api .Assertions .assertNotNull ;
25- import static org .mockito .Mockito .reset ;
26- import static org .mockito .Mockito .times ;
27- import static org .mockito .Mockito .verify ;
28- import static org .mockito .Mockito .when ;
2920
30- // Disabled in nativeTest, as we get an exception from Mockito
31- // => java.lang.NoClassDefFoundError: Could not initialize class org.mockito.internal.configuration.plugins.Plugins
32- @ DisabledInNativeImage
21+ @ Property (name = "replace.googlephotoaccessorimpl" , value = StringUtils .TRUE )
3322class MemberPhotoServiceImplTest extends TestContainersSuite {
3423
35- @ Mock
36- private MemberProfileServices mockMemberProfileServices ;
37-
38- @ Mock
39- private GoogleApiAccess mockGoogleApiAccess ;
40-
41- @ Mock
42- private Directory mockDirectory ;
43-
44- @ Mock
45- private Directory .Users mockUsers ;
46-
47- @ Mock
48- private Directory .Users .Photos mockPhotos ;
49-
50- @ Mock
51- private Directory .Users .Photos .Get mockGet ;
52-
53- @ Mock
54- private Environment mockEnvironment ;
55-
56- @ InjectMocks
57- private GooglePhotoAccessorImpl accessor ;
24+ @ Inject
25+ private GooglePhotoAccessorImplReplacement googlePhotoAccessorImpl ;
5826
27+ @ Inject
5928 private MemberPhotoServiceImpl service ;
6029
61- private AutoCloseable mockFinalizer ;
62-
63- @ BeforeAll
64- void initMocks () {
65- mockFinalizer = MockitoAnnotations .openMocks (this );
66- }
67-
68- @ AfterAll
69- void close () throws Exception {
70- mockFinalizer .close ();
71- }
72-
7330 @ BeforeEach
74- void resetMocks () {
75- reset (mockMemberProfileServices );
76- reset (mockGoogleApiAccess );
77- reset (mockDirectory );
78- reset (mockUsers );
79- reset (mockPhotos );
80- reset (mockGet );
81- reset (mockEnvironment );
82- service = new MemberPhotoServiceImpl (accessor );
31+ void reset () {
32+ googlePhotoAccessorImpl .reset ();
8333 }
8434
8535 // happy path
@@ -96,36 +46,20 @@ void testGetImageByEmailAddress() throws IOException {
9646 testUserPhoto .setKind ("test.kind" );
9747 testUserPhoto .setMimeType ("test.mime.type" );
9848 testUserPhoto .setPhotoData (new String (testData ));
99-
100- when (mockGoogleApiAccess .getDirectory ()).thenReturn (mockDirectory );
101- when (mockDirectory .users ()).thenReturn (mockUsers );
102- when (mockUsers .photos ()).thenReturn (mockPhotos );
103- when (mockPhotos .get (testEmail )).thenReturn (mockGet );
104- when (mockGet .execute ()).thenReturn (testUserPhoto );
105-
49+ googlePhotoAccessorImpl .setUserPhoto (testEmail , testUserPhoto );
10650 final byte [] result = service .getImageByEmailAddress (testEmail );
10751
10852 assertNotNull (result );
10953 assertEquals (testPhotoData , new String (result , StandardCharsets .UTF_8 ));
110- verify (mockGoogleApiAccess , times (1 )).getDirectory ();
111- verify (mockGet , times (1 )).execute ();
11254 }
11355
11456 @ Test
11557 void testDirectoryServiceThrowsGoogleJsonResponseException () throws IOException {
116- String testEmail =
"[email protected] " ;
117-
118- when (mockGoogleApiAccess .getDirectory ()).thenReturn (mockDirectory );
119- when (mockDirectory .users ()).thenReturn (mockUsers );
120- when (mockUsers .photos ()).thenReturn (mockPhotos );
121- when (mockPhotos .get (testEmail )).thenReturn (mockGet );
122- when (mockGet .execute ()).thenThrow (GoogleJsonResponseException .class );
58+ String testEmail =
"[email protected] " ;
12359
12460 final byte [] result = service .getImageByEmailAddress (testEmail );
12561
12662 assertNotNull (result );
12763 assertEquals ("" , new String (result , StandardCharsets .UTF_8 ));
128- verify (mockGoogleApiAccess , times (1 )).getDirectory ();
129- verify (mockGet , times (1 )).execute ();
13064 }
13165}
0 commit comments