1
+ package org .symphonyoss .symphony .clients .impl ;
2
+
3
+ import org .junit .Before ;
4
+ import org .junit .Test ;
5
+ import org .junit .runner .RunWith ;
6
+ import org .mockito .Mock ;
7
+
8
+ import org .powermock .api .mockito .PowerMockito ;
9
+ import org .powermock .core .classloader .annotations .PrepareForTest ;
10
+ import org .powermock .modules .junit4 .PowerMockRunner ;
11
+ import org .slf4j .Logger ;
12
+ import org .slf4j .LoggerFactory ;
13
+ import org .symphonyoss .client .exceptions .UsersClientException ;
14
+ import org .symphonyoss .client .model .SymAuth ;
15
+ import org .symphonyoss .symphony .authenticator .model .Token ;
16
+ import org .symphonyoss .symphony .pod .api .UserApi ;
17
+ import org .symphonyoss .symphony .pod .invoker .ApiClient ;
18
+ import org .symphonyoss .symphony .pod .invoker .ApiException ;
19
+ import org .symphonyoss .symphony .pod .invoker .Configuration ;
20
+ import org .symphonyoss .symphony .pod .model .AvatarUpdate ;
21
+ import org .symphonyoss .symphony .pod .model .SuccessResponse ;
22
+
23
+ import java .util .Random ;
24
+
25
+ import static org .mockito .Mockito .times ;
26
+ import static org .mockito .Mockito .verify ;
27
+ import static org .mockito .Mockito .eq ;
28
+ import static org .mockito .Mockito .any ;
29
+ import static org .mockito .Mockito .anyString ;
30
+ import static org .powermock .api .mockito .PowerMockito .mockStatic ;
31
+ import static org .powermock .api .mockito .PowerMockito .verifyNew ;
32
+ import static org .powermock .api .mockito .PowerMockito .when ;
33
+ import static org .powermock .api .mockito .PowerMockito .whenNew ;
34
+
35
+ @ RunWith (PowerMockRunner .class )
36
+ @ PrepareForTest ({ LoggerFactory .class , Configuration .class , UsersClientImpl .class })
37
+ public class UsersClientImplTest {
38
+
39
+ private static final int IMAGE_LENGTH = 3 ;
40
+ private static final String TOKEN_STRING = "tokenString" ;
41
+ private static final String OK_RESPONSE = "OK" ;
42
+ private static final String NOT_OK_RESPONSE = "NOT_OK" ;
43
+ private static final long USER_ID = 123L ;
44
+ private static final String API_ERROR_COMMUNICATING_WITH_POD_WHILE_UPDATING_AVATAR = "API error communicating with POD, while updating avatar" ;
45
+ private static final String AVATAR_UPDATE_FAILED = "Avatar update failed" ;
46
+ private static final String POD_URL = "podUrl" ;
47
+
48
+ private static Logger LOG ;
49
+
50
+ private UsersClientImpl usersClient ;
51
+
52
+ @ Mock
53
+ private ApiClient apiClientMock ;
54
+
55
+ @ Mock
56
+ private SymAuth symAuthMock ;
57
+
58
+ @ Mock
59
+ private UserApi userApiMock ;
60
+
61
+ @ Mock
62
+ private AvatarUpdate avatarUpdateMock ;
63
+
64
+ @ Mock
65
+ private SuccessResponse successResponseMock ;
66
+
67
+ @ Before
68
+ public void before () throws Exception {
69
+ mockLogger ();
70
+ mockConfiguration ();
71
+
72
+ whenNew (UserApi .class ).withArguments (apiClientMock ).thenReturn (userApiMock );
73
+
74
+ whenNew (AvatarUpdate .class ).withNoArguments ().thenReturn (avatarUpdateMock );
75
+
76
+ usersClient = new UsersClientImpl (symAuthMock , POD_URL );
77
+ }
78
+
79
+ @ Test
80
+ public void avatarArryIsNull () throws Exception {
81
+ usersClient .updateUserAvatar (USER_ID , null );
82
+ verifyNew (UserApi .class , times (0 )).withArguments (eq (apiClientMock ));
83
+ }
84
+
85
+ @ Test
86
+ public void successfulAvatarUpdate () throws ApiException , UsersClientException {
87
+ mockSessionToken ();
88
+ mockUserApi (OK_RESPONSE );
89
+
90
+ usersClient .updateUserAvatar (USER_ID , generateImageData ());
91
+
92
+ verify (LOG , times (0 )).error (anyString (), any (Exception .class ));
93
+
94
+ }
95
+
96
+ @ Test (expected = UsersClientException .class )
97
+ public void unsuccessfulAvatarUpdate () throws ApiException , UsersClientException {
98
+ mockSessionToken ();
99
+ mockUserApi (NOT_OK_RESPONSE );
100
+
101
+ usersClient .updateUserAvatar (USER_ID , generateImageData ());
102
+
103
+ verify (LOG , times (1 )).error (eq (AVATAR_UPDATE_FAILED ), any (IllegalStateException .class ));
104
+ }
105
+
106
+ @ Test (expected = UsersClientException .class )
107
+ public void apiExceptionAvatarUpdate () throws ApiException , UsersClientException {
108
+ mockSessionToken ();
109
+ mockUserApiException ();
110
+
111
+ usersClient .updateUserAvatar (USER_ID , generateImageData ());
112
+
113
+ verify (LOG , times (1 )).error (eq (API_ERROR_COMMUNICATING_WITH_POD_WHILE_UPDATING_AVATAR ),
114
+ any (ApiException .class ));
115
+ }
116
+
117
+ private void mockUserApi (String response ) throws ApiException {
118
+ when (userApiMock .v1AdminUserUidAvatarUpdatePost (TOKEN_STRING , USER_ID , avatarUpdateMock ))
119
+ .thenReturn (successResponseMock );
120
+ when (successResponseMock .getMessage ()).thenReturn (response );
121
+ }
122
+
123
+ private void mockUserApiException () throws ApiException {
124
+ when (userApiMock .getApiClient ()).thenReturn (apiClientMock );
125
+ when (userApiMock .v1AdminUserUidAvatarUpdatePost (TOKEN_STRING , USER_ID , avatarUpdateMock ))
126
+ .thenThrow (new ApiException ());
127
+ }
128
+
129
+ private void mockSessionToken () {
130
+ Token token = new Token ();
131
+ token .setToken (TOKEN_STRING );
132
+ when (symAuthMock .getSessionToken ()).thenReturn (token );
133
+ }
134
+
135
+ private byte [] generateImageData () {
136
+ byte [] imageData = new byte [IMAGE_LENGTH ];
137
+ new Random ().nextBytes (imageData );
138
+ return imageData ;
139
+ }
140
+
141
+ private void mockConfiguration () {
142
+ mockStatic (Configuration .class );
143
+ when (Configuration .getDefaultApiClient ()).thenReturn (apiClientMock );
144
+ }
145
+
146
+ private void mockLogger () {
147
+ mockStatic (LoggerFactory .class );
148
+ LOG = PowerMockito .mock (Logger .class );
149
+ when (LoggerFactory .getLogger (UsersClientImpl .class )).thenReturn (LOG );
150
+ }
151
+
152
+ }
0 commit comments