|
| 1 | +package org.bounswe.jobboardbackend.mentorship.controller; |
| 2 | + |
| 3 | +import org.bounswe.jobboardbackend.auth.service.UserDetailsImpl; |
| 4 | +import org.bounswe.jobboardbackend.mentorship.dto.*; |
| 5 | +import org.bounswe.jobboardbackend.mentorship.service.MentorshipService; |
| 6 | +import org.junit.jupiter.api.Test; |
| 7 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 8 | +import org.mockito.*; |
| 9 | +import org.springframework.http.HttpStatus; |
| 10 | +import org.springframework.http.ResponseEntity; |
| 11 | +import org.springframework.security.core.Authentication; |
| 12 | +import org.springframework.web.multipart.MultipartFile; |
| 13 | + |
| 14 | +import java.util.List; |
| 15 | + |
| 16 | +import static org.junit.jupiter.api.Assertions.*; |
| 17 | +import static org.mockito.Mockito.*; |
| 18 | + |
| 19 | +@ExtendWith(org.mockito.junit.jupiter.MockitoExtension.class) |
| 20 | +class MentorshipControllerTest { |
| 21 | + |
| 22 | + @Mock |
| 23 | + private MentorshipService mentorshipService; |
| 24 | + |
| 25 | + @InjectMocks |
| 26 | + private MentorshipController mentorshipController; |
| 27 | + |
| 28 | + // ---------------------------------------------------------------------- |
| 29 | + // File upload / resume endpoints |
| 30 | + // ---------------------------------------------------------------------- |
| 31 | + |
| 32 | + @Test |
| 33 | + void uploadResumeFile_returnsOkAndDelegatesToService() { |
| 34 | + Long resumeReviewId = 1L; |
| 35 | + MultipartFile file = mock(MultipartFile.class); |
| 36 | + ResumeFileResponseDTO responseDto = mock(ResumeFileResponseDTO.class); |
| 37 | + |
| 38 | + when(mentorshipService.uploadResumeFile(resumeReviewId, file)) |
| 39 | + .thenReturn(responseDto); |
| 40 | + |
| 41 | + ResponseEntity<ResumeFileResponseDTO> response = |
| 42 | + mentorshipController.uploadResumeFile(resumeReviewId, file); |
| 43 | + |
| 44 | + assertEquals(HttpStatus.OK, response.getStatusCode()); |
| 45 | + assertSame(responseDto, response.getBody()); |
| 46 | + verify(mentorshipService).uploadResumeFile(resumeReviewId, file); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + void getResumeFileUrl_returnsOkAndBody() { |
| 51 | + Long resumeReviewId = 2L; |
| 52 | + ResumeFileUrlDTO dto = mock(ResumeFileUrlDTO.class); |
| 53 | + |
| 54 | + when(mentorshipService.getResumeFileUrl(resumeReviewId)) |
| 55 | + .thenReturn(dto); |
| 56 | + |
| 57 | + ResponseEntity<ResumeFileUrlDTO> response = |
| 58 | + mentorshipController.getResumeFileUrl(resumeReviewId); |
| 59 | + |
| 60 | + assertEquals(HttpStatus.OK, response.getStatusCode()); |
| 61 | + assertSame(dto, response.getBody()); |
| 62 | + verify(mentorshipService).getResumeFileUrl(resumeReviewId); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + void getResumeReview_returnsOkAndBody() { |
| 67 | + Long resumeReviewId = 3L; |
| 68 | + ResumeReviewDTO dto = mock(ResumeReviewDTO.class); |
| 69 | + |
| 70 | + when(mentorshipService.getResumeReview(resumeReviewId)) |
| 71 | + .thenReturn(dto); |
| 72 | + |
| 73 | + ResponseEntity<ResumeReviewDTO> response = |
| 74 | + mentorshipController.getResumeReview(resumeReviewId); |
| 75 | + |
| 76 | + assertEquals(HttpStatus.OK, response.getStatusCode()); |
| 77 | + assertSame(dto, response.getBody()); |
| 78 | + verify(mentorshipService).getResumeReview(resumeReviewId); |
| 79 | + } |
| 80 | + |
| 81 | + // ---------------------------------------------------------------------- |
| 82 | + // Mentor search / profile endpoints |
| 83 | + // ---------------------------------------------------------------------- |
| 84 | + |
| 85 | + @Test |
| 86 | + void searchMentors_returnsListFromService() { |
| 87 | + List<MentorProfileDetailDTO> mentors = List.of( |
| 88 | + mock(MentorProfileDetailDTO.class), |
| 89 | + mock(MentorProfileDetailDTO.class) |
| 90 | + ); |
| 91 | + |
| 92 | + when(mentorshipService.searchMentors()).thenReturn(mentors); |
| 93 | + |
| 94 | + ResponseEntity<List<MentorProfileDetailDTO>> response = |
| 95 | + mentorshipController.searchMentors(); |
| 96 | + |
| 97 | + assertEquals(HttpStatus.OK, response.getStatusCode()); |
| 98 | + assertSame(mentors, response.getBody()); |
| 99 | + verify(mentorshipService).searchMentors(); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + void createMentorProfile_usesAuthenticatedUserId() { |
| 104 | + Authentication auth = mock(Authentication.class); |
| 105 | + UserDetailsImpl principal = mock(UserDetailsImpl.class); |
| 106 | + when(auth.getPrincipal()).thenReturn(principal); |
| 107 | + when(principal.getId()).thenReturn(10L); |
| 108 | + |
| 109 | + CreateMentorProfileDTO createDTO = mock(CreateMentorProfileDTO.class); |
| 110 | + MentorProfileDTO returned = mock(MentorProfileDTO.class); |
| 111 | + |
| 112 | + when(mentorshipService.createMentorProfile(10L, createDTO)) |
| 113 | + .thenReturn(returned); |
| 114 | + |
| 115 | + ResponseEntity<MentorProfileDTO> response = |
| 116 | + mentorshipController.createMentorProfile(createDTO, auth); |
| 117 | + |
| 118 | + assertEquals(HttpStatus.CREATED, response.getStatusCode()); |
| 119 | + assertSame(returned, response.getBody()); |
| 120 | + verify(mentorshipService).createMentorProfile(10L, createDTO); |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + void updateMentorProfile_callsServiceWithPathVariable() { |
| 125 | + Long userId = 5L; |
| 126 | + UpdateMentorProfileDTO updateDTO = mock(UpdateMentorProfileDTO.class); |
| 127 | + MentorProfileDTO returned = mock(MentorProfileDTO.class); |
| 128 | + |
| 129 | + when(mentorshipService.updateMentorProfile(userId, updateDTO)) |
| 130 | + .thenReturn(returned); |
| 131 | + |
| 132 | + ResponseEntity<MentorProfileDTO> response = |
| 133 | + mentorshipController.updateMentorProfile(updateDTO, userId); |
| 134 | + |
| 135 | + assertEquals(HttpStatus.OK, response.getStatusCode()); |
| 136 | + assertSame(returned, response.getBody()); |
| 137 | + verify(mentorshipService).updateMentorProfile(userId, updateDTO); |
| 138 | + } |
| 139 | + |
| 140 | + @Test |
| 141 | + void getMentorProfile_returnsProfile() { |
| 142 | + Long userId = 5L; |
| 143 | + MentorProfileDetailDTO dto = mock(MentorProfileDetailDTO.class); |
| 144 | + |
| 145 | + when(mentorshipService.getMentorProfile(userId)) |
| 146 | + .thenReturn(dto); |
| 147 | + |
| 148 | + ResponseEntity<MentorProfileDetailDTO> response = |
| 149 | + mentorshipController.getMentorProfile(userId); |
| 150 | + |
| 151 | + assertEquals(HttpStatus.OK, response.getStatusCode()); |
| 152 | + assertSame(dto, response.getBody()); |
| 153 | + verify(mentorshipService).getMentorProfile(userId); |
| 154 | + } |
| 155 | + |
| 156 | + @Test |
| 157 | + void deleteMentorProfile_returnsOkAndCallsService() { |
| 158 | + Long userId = 6L; |
| 159 | + |
| 160 | + ResponseEntity<MentorProfileDTO> response = |
| 161 | + mentorshipController.deleteMentorProfile(userId); |
| 162 | + |
| 163 | + assertEquals(HttpStatus.OK, response.getStatusCode()); |
| 164 | + assertNull(response.getBody()); |
| 165 | + verify(mentorshipService).deleteMentorProfile(userId); |
| 166 | + } |
| 167 | + |
| 168 | + // ---------------------------------------------------------------------- |
| 169 | + // Complete / close mentorship |
| 170 | + // ---------------------------------------------------------------------- |
| 171 | + |
| 172 | + @Test |
| 173 | + void completeMentorship_callsServiceAndReturnsOk() { |
| 174 | + Long reviewId = 7L; |
| 175 | + Authentication auth = mock(Authentication.class); |
| 176 | + |
| 177 | + ResponseEntity<Void> response = |
| 178 | + mentorshipController.completeMentorship(reviewId, auth); |
| 179 | + |
| 180 | + assertEquals(HttpStatus.OK, response.getStatusCode()); |
| 181 | + verify(mentorshipService).completeMentorship(reviewId, auth); |
| 182 | + } |
| 183 | + |
| 184 | + @Test |
| 185 | + void closeMentorship_callsServiceAndReturnsOk() { |
| 186 | + Long reviewId = 8L; |
| 187 | + Authentication auth = mock(Authentication.class); |
| 188 | + |
| 189 | + ResponseEntity<Void> response = |
| 190 | + mentorshipController.closeMentorship(reviewId, auth); |
| 191 | + |
| 192 | + assertEquals(HttpStatus.OK, response.getStatusCode()); |
| 193 | + verify(mentorshipService).closeMentorship(reviewId, auth); |
| 194 | + } |
| 195 | + |
| 196 | + // ---------------------------------------------------------------------- |
| 197 | + // Mentorship request endpoints |
| 198 | + // ---------------------------------------------------------------------- |
| 199 | + |
| 200 | + @Test |
| 201 | + void createMentorshipRequest_usesAuthenticatedUserId() { |
| 202 | + Authentication auth = mock(Authentication.class); |
| 203 | + UserDetailsImpl principal = mock(UserDetailsImpl.class); |
| 204 | + when(auth.getPrincipal()).thenReturn(principal); |
| 205 | + when(principal.getId()).thenReturn(20L); |
| 206 | + |
| 207 | + CreateMentorshipRequestDTO requestDTO = mock(CreateMentorshipRequestDTO.class); |
| 208 | + MentorshipRequestDTO returned = mock(MentorshipRequestDTO.class); |
| 209 | + |
| 210 | + when(mentorshipService.createMentorshipRequest(requestDTO, 20L)) |
| 211 | + .thenReturn(returned); |
| 212 | + |
| 213 | + ResponseEntity<MentorshipRequestDTO> response = |
| 214 | + mentorshipController.createMentorshipRequest(requestDTO, auth); |
| 215 | + |
| 216 | + assertEquals(HttpStatus.CREATED, response.getStatusCode()); |
| 217 | + assertSame(returned, response.getBody()); |
| 218 | + verify(mentorshipService).createMentorshipRequest(requestDTO, 20L); |
| 219 | + } |
| 220 | + |
| 221 | + @Test |
| 222 | + void getMentorshipRequestOfMentor_usesAuthenticatedUserId() { |
| 223 | + Long mentorId = 30L; |
| 224 | + Authentication auth = mock(Authentication.class); |
| 225 | + UserDetailsImpl principal = mock(UserDetailsImpl.class); |
| 226 | + when(auth.getPrincipal()).thenReturn(principal); |
| 227 | + when(principal.getId()).thenReturn(30L); |
| 228 | + |
| 229 | + List<MentorshipRequestDTO> requests = List.of(mock(MentorshipRequestDTO.class)); |
| 230 | + |
| 231 | + when(mentorshipService.getMentorshipRequestsOfMentor(mentorId, 30L)) |
| 232 | + .thenReturn(requests); |
| 233 | + |
| 234 | + ResponseEntity<List<MentorshipRequestDTO>> response = |
| 235 | + mentorshipController.getMentorshipRequestOfMentor(mentorId, auth); |
| 236 | + |
| 237 | + assertEquals(HttpStatus.OK, response.getStatusCode()); |
| 238 | + assertSame(requests, response.getBody()); |
| 239 | + verify(mentorshipService).getMentorshipRequestsOfMentor(mentorId, 30L); |
| 240 | + } |
| 241 | + |
| 242 | + @Test |
| 243 | + void getMyMentorshipDetails_forMenteeReturnsList() { |
| 244 | + Long menteeId = 40L; |
| 245 | + List<MentorshipDetailsDTO> details = List.of(mock(MentorshipDetailsDTO.class)); |
| 246 | + |
| 247 | + when(mentorshipService.getMentorshipDetailsForMentee(menteeId, menteeId)) |
| 248 | + .thenReturn(details); |
| 249 | + |
| 250 | + ResponseEntity<List<MentorshipDetailsDTO>> response = |
| 251 | + mentorshipController.getMyMentorshipDetails(menteeId); |
| 252 | + |
| 253 | + assertEquals(HttpStatus.OK, response.getStatusCode()); |
| 254 | + assertSame(details, response.getBody()); |
| 255 | + verify(mentorshipService).getMentorshipDetailsForMentee(menteeId, menteeId); |
| 256 | + } |
| 257 | + |
| 258 | + @Test |
| 259 | + void getMentorshipRequest_usesAuthenticatedUserId() { |
| 260 | + Long requestId = 50L; |
| 261 | + Authentication auth = mock(Authentication.class); |
| 262 | + UserDetailsImpl principal = mock(UserDetailsImpl.class); |
| 263 | + when(auth.getPrincipal()).thenReturn(principal); |
| 264 | + when(principal.getId()).thenReturn(99L); |
| 265 | + |
| 266 | + MentorshipRequestDTO dto = mock(MentorshipRequestDTO.class); |
| 267 | + |
| 268 | + when(mentorshipService.getMentorshipRequest(requestId, 99L)) |
| 269 | + .thenReturn(dto); |
| 270 | + |
| 271 | + ResponseEntity<MentorshipRequestDTO> response = |
| 272 | + mentorshipController.getMentorshipRequest(requestId, auth); |
| 273 | + |
| 274 | + assertEquals(HttpStatus.OK, response.getStatusCode()); |
| 275 | + assertSame(dto, response.getBody()); |
| 276 | + verify(mentorshipService).getMentorshipRequest(requestId, 99L); |
| 277 | + } |
| 278 | + |
| 279 | + @Test |
| 280 | + void respondToMentorshipRequest_usesAcceptFlagAndUserId() { |
| 281 | + Long requestId = 60L; |
| 282 | + Authentication auth = mock(Authentication.class); |
| 283 | + UserDetailsImpl principal = mock(UserDetailsImpl.class); |
| 284 | + when(auth.getPrincipal()).thenReturn(principal); |
| 285 | + when(principal.getId()).thenReturn(11L); |
| 286 | + |
| 287 | + RespondToRequestDTO responseDTO = mock(RespondToRequestDTO.class); |
| 288 | + when(responseDTO.accept()).thenReturn(true); |
| 289 | + |
| 290 | + MentorshipRequestDTO returned = mock(MentorshipRequestDTO.class); |
| 291 | + |
| 292 | + when(mentorshipService.respondToMentorshipRequest(requestId, true, 11L)) |
| 293 | + .thenReturn(returned); |
| 294 | + |
| 295 | + ResponseEntity<MentorshipRequestDTO> response = |
| 296 | + mentorshipController.respondToMentorshipRequest(requestId, responseDTO, auth); |
| 297 | + |
| 298 | + assertEquals(HttpStatus.OK, response.getStatusCode()); |
| 299 | + assertSame(returned, response.getBody()); |
| 300 | + verify(mentorshipService).respondToMentorshipRequest(requestId, true, 11L); |
| 301 | + } |
| 302 | + |
| 303 | + // ---------------------------------------------------------------------- |
| 304 | + // Rating endpoint |
| 305 | + // ---------------------------------------------------------------------- |
| 306 | + |
| 307 | + @Test |
| 308 | + void rateMentor_usesAuthenticatedJobSeekerId() { |
| 309 | + Authentication auth = mock(Authentication.class); |
| 310 | + UserDetailsImpl principal = mock(UserDetailsImpl.class); |
| 311 | + when(auth.getPrincipal()).thenReturn(principal); |
| 312 | + when(principal.getId()).thenReturn(77L); |
| 313 | + |
| 314 | + CreateRatingDTO ratingDTO = mock(CreateRatingDTO.class); |
| 315 | + |
| 316 | + ResponseEntity<Void> response = |
| 317 | + mentorshipController.rateMentor(ratingDTO, auth); |
| 318 | + |
| 319 | + assertEquals(HttpStatus.CREATED, response.getStatusCode()); |
| 320 | + verify(mentorshipService).rateMentor(ratingDTO, 77L); |
| 321 | + } |
| 322 | +} |
0 commit comments