Skip to content

Commit 9b59314

Browse files
committed
Removed Mockito from MemberPhotoServiceImplTest.
1 parent ab7461e commit 9b59314

File tree

3 files changed

+52
-79
lines changed

3 files changed

+52
-79
lines changed

server/src/main/java/com/objectcomputing/checkins/services/memberprofile/memberphoto/GooglePhotoAccessorImpl.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
import java.util.Optional;
1919

2020
@Singleton
21-
class GooglePhotoAccessorImpl implements GooglePhotoAccessor {
21+
// Public so that this class can be replaced during testing.
22+
public class GooglePhotoAccessorImpl implements GooglePhotoAccessor {
2223

2324
private static final Logger LOG = LoggerFactory.getLogger(GooglePhotoAccessorImpl.class);
2425

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.objectcomputing.checkins.services;
2+
3+
import com.google.api.services.directory.model.UserPhoto;
4+
import com.objectcomputing.checkins.services.memberprofile.memberphoto.GooglePhotoAccessorImpl;
5+
import com.objectcomputing.checkins.services.memberprofile.memberphoto.GooglePhotoAccessor;
6+
7+
import java.util.List;
8+
import java.util.Map;
9+
import java.util.HashMap;
10+
import java.util.Base64;
11+
12+
import jakarta.inject.Singleton;
13+
import io.micronaut.core.util.StringUtils;
14+
import io.micronaut.context.annotation.Replaces;
15+
import io.micronaut.context.annotation.Requires;
16+
17+
@Singleton
18+
@Replaces(GooglePhotoAccessorImpl.class)
19+
@Requires(property = "replace.googlephotoaccessorimpl", value = StringUtils.TRUE)
20+
public class GooglePhotoAccessorImplReplacement implements GooglePhotoAccessor {
21+
Map<String, UserPhoto> photos = new HashMap<>();
22+
23+
public void reset() {
24+
photos.clear();
25+
}
26+
27+
public void setUserPhoto(String email, UserPhoto photo) {
28+
photos.put(email, photo);
29+
}
30+
31+
@Override
32+
public byte[] getPhotoData(String workEmail) {
33+
UserPhoto photo = photos.get(workEmail);
34+
return photo == null
35+
? new byte[0]
36+
: Base64.getUrlDecoder().decode(photo.getPhotoData().getBytes());
37+
}
38+
}
Lines changed: 12 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,35 @@
11
package com.objectcomputing.checkins.services.memberprofile.memberphoto;
22

33
import com.google.api.client.googleapis.json.GoogleJsonResponseException;
4-
import com.google.api.services.directory.Directory;
54
import com.google.api.services.directory.model.UserPhoto;
65
import com.objectcomputing.checkins.services.TestContainersSuite;
6+
import com.objectcomputing.checkins.services.GooglePhotoAccessorImplReplacement;
77
import 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;
1210
import org.junit.jupiter.api.BeforeEach;
1311
import 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

1914
import java.io.IOException;
2015
import java.nio.charset.StandardCharsets;
2116
import java.util.Base64;
2217

2318
import static org.junit.jupiter.api.Assertions.assertEquals;
2419
import 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)
3322
class 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

Comments
 (0)