11package dev .findfirst .users .service ;
22
3- import static org .junit .Assert .assertNull ;
43import static org .junit .jupiter .api .Assertions .assertEquals ;
54import static org .junit .jupiter .api .Assertions .assertNotNull ;
5+ import static org .junit .jupiter .api .Assertions .assertNull ;
6+ import static org .junit .jupiter .api .Assertions .assertTrue ;
67import static org .mockito .Mockito .eq ;
78import static org .mockito .Mockito .times ;
89import static org .mockito .Mockito .verify ;
910import static org .mockito .Mockito .when ;
1011
12+ import java .io .File ;
13+ import java .io .IOException ;
1114import java .util .Optional ;
1215
16+ import dev .findfirst .security .jwt .service .RefreshTokenService ;
17+ import dev .findfirst .security .userauth .context .UserContext ;
1318import dev .findfirst .users .controller .UserController ;
1419import dev .findfirst .users .model .user .User ;
1520
21+ import org .junit .jupiter .api .BeforeEach ;
1622import org .junit .jupiter .api .Disabled ;
1723import org .junit .jupiter .api .Test ;
24+ import org .junit .jupiter .api .extension .ExtendWith ;
1825import org .mockito .ArgumentCaptor ;
26+ import org .mockito .Captor ;
1927import org .mockito .InjectMocks ;
2028import org .mockito .Mock ;
21- import org .mockito .MockitoAnnotations ;
29+ import org .mockito .junit . jupiter . MockitoExtension ;
2230import org .springframework .http .HttpStatus ;
2331import org .springframework .http .ResponseEntity ;
2432import org .springframework .mock .web .MockMultipartFile ;
33+ import org .springframework .test .util .ReflectionTestUtils ;
2534
26- @ Disabled
35+ @ ExtendWith ( MockitoExtension . class )
2736class UserServiceTest {
2837
2938 @ Mock
3039 private UserManagementService userManagementService ;
3140
41+ @ Mock
42+ private UserContext userContext ;
43+
44+ @ Mock
45+ private RegistrationService registrationService ;
46+
47+ @ Mock
48+ private ForgotPasswordService forgotPasswordService ;
49+
50+ @ Mock
51+ private RefreshTokenService refreshTokenService ;
52+
3253 @ InjectMocks
3354 private UserController userController ;
3455
35- public UserServiceTest () {
36- MockitoAnnotations .openMocks (this );
56+ @ Captor
57+ private ArgumentCaptor <User > userArgumentCaptor ;
58+
59+ private User mockUser ;
60+
61+ @ BeforeEach
62+ void setUp () {
63+ mockUser = new User ();
64+ mockUser .setUserId (1 );
65+ mockUser .setUsername ("testUser" );
66+ mockUser .setUserPhoto ("uploads/profile-pictures/test.jpg" );
67+
68+ // Mocking the allowedTypes field
69+ ReflectionTestUtils .setField (userController , "allowedTypes" ,
70+ new String [] {"image/jpeg" , "image/png" });
3771 }
3872
3973 @ Test
74+ @ Disabled
4075 void testUploadProfilePicture_Success () throws Exception {
76+
4177 MockMultipartFile file =
4278 new MockMultipartFile ("file" , "test.jpg" , "image/jpeg" , "dummy content" .getBytes ());
43- int userId = 1 ;
4479
45- User user = new User ();
46- user .setUserId (userId );
47- user .setUsername ("testUser" );
48- when (userManagementService .getUserById (userId )).thenReturn (Optional .of (user ));
80+ when (userManagementService .getUserById (mockUser .getUserId ())).thenReturn (Optional .of (mockUser ));
4981
5082 ResponseEntity <?> response = userController .uploadProfilePicture (file );
83+ System .out .println (response .getBody ());
5184
5285 assertEquals (HttpStatus .OK , response .getStatusCode ());
5386 assertEquals ("File uploaded successfully." , response .getBody ());
54- verify (userManagementService , times (1 )).changeUserPhoto (eq (user ), file );
87+
88+ verify (userManagementService , times (1 )).changeUserPhoto (eq (mockUser ), eq (file ));
5589 }
5690
5791 @ Test
58- void testRemoveUserPhoto_Success () {
59- User user = new User ();
60- user .setUserId (1 );
61- user .setUsername ("testUser" );
62- user .setUserPhoto ("uploads/profile-pictures/test.jpg" );
92+ @ Disabled
93+ void testRemoveUserPhoto_Success () throws Exception {
6394
64- when (userManagementService .getUserById (user .getUserId ())).thenReturn (Optional .of (user ));
95+ MockMultipartFile file =
96+ new MockMultipartFile ("file" , "test.jpg" , "image/jpeg" , "dummy content" .getBytes ());
6597
66- userManagementService .removeUserPhoto ( user );
98+ when ( userManagementService .getUserById ( mockUser . getUserId ())). thenReturn ( Optional . of ( mockUser ) );
6799
68- ArgumentCaptor <User > userCaptor = ArgumentCaptor .forClass (User .class );
69- verify (userManagementService , times (1 )).saveUser (userCaptor .capture ());
70- assertNull (userCaptor .getValue ().getUserPhoto ());
100+ ResponseEntity <?> response = userController .uploadProfilePicture (file );
101+
102+ assertEquals (HttpStatus .OK , response .getStatusCode ());
103+ assertEquals ("File uploaded successfully." , response .getBody ());
104+
105+ verify (userManagementService , times (1 )).changeUserPhoto (eq (mockUser ), eq (file ));
106+ verify (userManagementService , times (1 )).removeUserPhoto (eq (mockUser ));
107+
108+ assertNull (mockUser .getUserPhoto ());
71109 }
72110
73111 @ Test
74112 void testUploadProfilePicture_FileSizeExceedsLimit () throws Exception {
113+
75114 byte [] largeContent = new byte [3 * 1024 * 1024 ]; // 3 MB
76- MockMultipartFile file = new MockMultipartFile ("file" , "large.jpg" , "image/jpeg" , largeContent );
77- int userId = 1 ;
78115
79- User user = new User ();
80- user .setUserId (userId );
81- user .setUsername ("testUser" );
82- when (userManagementService .getUserById (userId )).thenReturn (Optional .of (user ));
116+ MockMultipartFile file = new MockMultipartFile ("file" , "large.jpg" , "image/jpeg" , largeContent );
83117
84118 ResponseEntity <?> response = userController .uploadProfilePicture (file );
85119
86120 assertEquals (HttpStatus .BAD_REQUEST , response .getStatusCode ());
87121 assertEquals ("File size exceeds the maximum limit of 2 MB." , response .getBody ());
122+
88123 }
89124
90125 @ Test
91126 void testUploadProfilePicture_InvalidFileType () throws Exception {
127+
92128 MockMultipartFile file =
93129 new MockMultipartFile ("file" , "test.txt" , "text/plain" , "dummy content" .getBytes ());
94- int userId = 1 ;
95-
96- User user = new User ();
97- user .setUserId (userId );
98- user .setUsername ("testUser" );
99- when (userManagementService .getUserById (userId )).thenReturn (Optional .of (user ));
100130
101131 ResponseEntity <?> response = userController .uploadProfilePicture (file );
102132
@@ -106,30 +136,30 @@ void testUploadProfilePicture_InvalidFileType() throws Exception {
106136
107137 @ Test
108138 void testGetUserProfilePicture_NotFound () {
109- int userId = 1 ;
110139
111- User user = new User ();
112- user .setUserId (userId );
113- user .setUsername ("testUser" );
114- user .setUserPhoto (null );
115- when (userManagementService .getUserById (userId )).thenReturn (Optional .of (user ));
140+ mockUser .setUserPhoto (null );
141+
142+ when (userManagementService .getUserById (mockUser .getUserId ())).thenReturn (Optional .of (mockUser ));
116143
117- ResponseEntity <?> response = userController .getUserProfilePicture (userId );
144+ ResponseEntity <?> response = userController .getUserProfilePicture (mockUser . getUserId () );
118145
119146 assertEquals (HttpStatus .NOT_FOUND , response .getStatusCode ());
120147 }
121148
122149 @ Test
123- void testGetUserProfilePicture_Success () {
124- int userId = 1 ;
150+ void testGetUserProfilePicture_Success () throws IOException {
151+
152+ // Create a temporary file to simulate an existing user photo
153+ File tempFile = File .createTempFile ("test" , ".jpg" );
154+ String tempFilePath = tempFile .getAbsolutePath ();
155+
156+ assertTrue (tempFile .exists ());
157+
158+ mockUser .setUserPhoto (tempFilePath );
125159
126- User user = new User ();
127- user .setUserId (userId );
128- user .setUsername ("testUser" );
129- user .setUserPhoto ("uploads/profile-pictures/test.jpg" );
130- when (userManagementService .getUserById (userId )).thenReturn (Optional .of (user ));
160+ when (userManagementService .getUserById (mockUser .getUserId ())).thenReturn (Optional .of (mockUser ));
131161
132- ResponseEntity <?> response = userController .getUserProfilePicture (userId );
162+ ResponseEntity <?> response = userController .getUserProfilePicture (mockUser . getUserId () );
133163
134164 assertEquals (HttpStatus .OK , response .getStatusCode ());
135165 assertNotNull (response .getBody ());
0 commit comments