33import dmu .dasom .api .domain .applicant .dto .ApplicantCreateRequestDto ;
44import dmu .dasom .api .domain .applicant .dto .ApplicantDetailsResponseDto ;
55import dmu .dasom .api .domain .applicant .dto .ApplicantResponseDto ;
6+ import dmu .dasom .api .domain .applicant .dto .ApplicantStatusUpdateRequestDto ;
67import dmu .dasom .api .domain .applicant .entity .Applicant ;
78import dmu .dasom .api .domain .applicant .enums .ApplicantStatus ;
89import dmu .dasom .api .domain .applicant .repository .ApplicantRepository ;
1415import dmu .dasom .api .domain .google .service .GoogleApiService ;
1516import dmu .dasom .api .global .dto .PageResponse ;
1617import jakarta .mail .MessagingException ;
18+ import org .junit .jupiter .api .BeforeEach ;
1719import org .junit .jupiter .api .DisplayName ;
1820import org .junit .jupiter .api .Test ;
1921import org .junit .jupiter .api .extension .ExtendWith ;
2325import org .springframework .data .domain .Page ;
2426import org .springframework .data .domain .PageImpl ;
2527import org .springframework .data .domain .PageRequest ;
28+ import org .springframework .test .util .ReflectionTestUtils ;
2629
2730import java .time .LocalDateTime ;
2831import java .util .Collections ;
@@ -47,6 +50,11 @@ class ApplicantServiceTest {
4750 @ Mock
4851 private GoogleApiService googleApiService ;
4952
53+ @ BeforeEach
54+ void setup () {
55+ ReflectionTestUtils .setField (applicantService , "spreadSheetId" , "test-spreadsheet-id" );
56+ }
57+
5058 @ Test
5159 @ DisplayName ("지원자 저장 - 성공" )
5260 void apply_success () {
@@ -241,4 +249,103 @@ void getApplicantByStudentNo_fail() {
241249 assertEquals (ErrorCode .ARGUMENT_NOT_VALID , exception .getErrorCode ());
242250 }
243251
252+ @ Test
253+ @ DisplayName ("지원자 상태 변경 - Google Sheets에 없는 사용자 추가" )
254+ void updateApplicantStatus_addToGoogleSheets () {
255+ // given
256+ Long applicantId = 1L ;
257+ String testSpreadsheetId = "test-spreadsheet-id" ;
258+
259+ ApplicantStatusUpdateRequestDto request = mock (ApplicantStatusUpdateRequestDto .class );
260+ when (request .getStatus ()).thenReturn (ApplicantStatus .INTERVIEW_PASSED );
261+
262+ // Mock 지원자 생성
263+ Applicant mockApplicant = Applicant .builder ()
264+ .name ("홍길동" )
265+ .studentNo ("20210000" )
266+ .contact ("010-1234-5678" )
267+ 268+ .grade (2 )
269+ .reasonForApply ("팀 활동 경험을 쌓고 싶습니다." )
270+ .activityWish ("프로그래밍 스터디 참여" )
271+ .isPrivacyPolicyAgreed (true )
272+ .status (ApplicantStatus .PENDING )
273+ .createdAt (LocalDateTime .now ())
274+ .updatedAt (LocalDateTime .now ())
275+ .build ();
276+
277+ when (applicantRepository .findById (applicantId )).thenReturn (Optional .of (mockApplicant ));
278+
279+ // 모든 인자를 Matchers로 설정
280+ when (googleApiService .findRowIndexByStudentNo (eq (testSpreadsheetId ), eq ("Sheet1" ), eq ("20210000" )))
281+ .thenReturn (-1 );
282+
283+ doNothing ().when (googleApiService ).appendToSheet (anyList ());
284+
285+ // when
286+ applicantService .updateApplicantStatus (applicantId , request );
287+
288+ // then
289+ verify (applicantRepository ).findById (applicantId );
290+ verify (googleApiService ).appendToSheet (List .of (mockApplicant ));
291+ }
292+
293+ @ Test
294+ @ DisplayName ("지원자 상태 변경 - Google Sheets에서 상태 업데이트" )
295+ void updateApplicantStatus_updateInGoogleSheets () {
296+ // given
297+ Long applicantId = 1L ;
298+
299+ // 요청 DTO 설정
300+ ApplicantStatusUpdateRequestDto request = mock (ApplicantStatusUpdateRequestDto .class );
301+ when (request .getStatus ()).thenReturn (ApplicantStatus .INTERVIEW_PASSED );
302+
303+ // Mock 지원자 생성
304+ Applicant mockApplicant = Applicant .builder ()
305+ .name ("홍길동" )
306+ .studentNo ("20210000" )
307+ .contact ("010-1234-5678" )
308+ 309+ .grade (2 )
310+ .reasonForApply ("팀 활동 경험을 쌓고 싶습니다." )
311+ .activityWish ("프로그래밍 스터디 참여" )
312+ .isPrivacyPolicyAgreed (true )
313+ .status (ApplicantStatus .PENDING )
314+ .createdAt (LocalDateTime .now ())
315+ .updatedAt (LocalDateTime .now ())
316+ .build ();
317+
318+ // Repository와 GoogleApiService의 동작 모킹
319+ when (applicantRepository .findById (applicantId )).thenReturn (Optional .of (mockApplicant ));
320+ when (googleApiService .findRowIndexByStudentNo (anyString (), eq ("Sheet1" ), eq ("20210000" ))).thenReturn (5 ); // Google Sheets에 있음
321+ doNothing ().when (googleApiService ).updateSheet (anyList ());
322+
323+ // when
324+ applicantService .updateApplicantStatus (applicantId , request );
325+
326+ // then
327+ verify (applicantRepository ).findById (applicantId );
328+ verify (googleApiService ).updateSheet (List .of (mockApplicant ));
329+ }
330+
331+ @ Test
332+ @ DisplayName ("지원자 저장 - 덮어쓰기 (Google Sheets 연동 포함)" )
333+ void apply_overwrite_withGoogleSheets () {
334+ // given
335+ ApplicantCreateRequestDto request = mock (ApplicantCreateRequestDto .class );
336+ when (request .getStudentNo ()).thenReturn ("20210000" );
337+
338+ Applicant existingApplicant = mock (Applicant .class ); // 기존 Applicant 객체 모킹
339+ when (applicantRepository .findByStudentNo ("20210000" )).thenReturn (Optional .of (existingApplicant ));
340+
341+ when (request .getIsOverwriteConfirmed ()).thenReturn (true );
342+
343+ // when
344+ applicantService .apply (request );
345+
346+ // then
347+ verify (existingApplicant ).overwrite (request );
348+ verify (googleApiService ).updateSheet (List .of (existingApplicant ));
349+ }
350+
244351}
0 commit comments