Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ public class ApplicantServiceImpl implements ApplicantService {
private final EmailService emailService;
private final GoogleApiService googleApiService;

@Value("${google.spreadsheet.id}")
private String spreadSheetId;

// 지원자 저장
@Override
public void apply(final ApplicantCreateRequestDto request) {
Expand Down Expand Up @@ -82,9 +85,20 @@ public ApplicantDetailsResponseDto getApplicant(final Long id) {
@Override
public ApplicantDetailsResponseDto updateApplicantStatus(final Long id, final ApplicantStatusUpdateRequestDto request) {
final Applicant applicant = findById(id);

// 지원자 상태 변경
applicant.updateStatus(request.getStatus());
googleApiService.updateSheet(List.of(applicant));

// Google Sheets에서 학번(Student No)을 기준으로 사용자 존재 여부 확인
int rowIndex = googleApiService.findRowIndexByStudentNo(spreadSheetId, "Sheet1", applicant.getStudentNo());
if (rowIndex == -1) {
// Google Sheets에 사용자 추가
googleApiService.appendToSheet(List.of(applicant));
log.info("지원자가 Google Sheets에 없어서 새로 추가되었습니다: {}", applicant.getStudentNo());
} else {
// Google Sheets에서 사용자 상태 업데이트
googleApiService.updateSheet(List.of(applicant));
log.info("Google Sheets에서 지원자 상태가 업데이트되었습니다: {}", applicant.getStudentNo());
}

return applicant.toApplicantDetailsResponse();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,13 @@ public void updateSheet(List<Applicant> applicants) {
processSheetsUpdate(applicants, false);
}

private int findRowIndexByStudentNo(String spreadSheetId, String sheetName, String studentNo){
public int findRowIndexByStudentNo(String spreadSheetId, String sheetName, String studentNo){
try {
List<List<Object>> rows = readSheet(spreadSheetId, sheetName + "!A:L"); // A열부터 L열까지 읽기

for (int i = 0; i < rows.size(); i++){
List<Object> row = rows.get(i);
if(!row.isEmpty() && row.get(2).equals(studentNo)){
if(!row.isEmpty() && row.get(2).equals(studentNo)){ // 학번(Student No)이 3번째 열(A=0 기준)
return i + 1;
}
}
Expand Down Expand Up @@ -175,4 +175,6 @@ public void processSheetsUpdate(List<Applicant> applicants, boolean isAppend) {
}
}



}
107 changes: 107 additions & 0 deletions src/test/java/dmu/dasom/api/domain/applicant/ApplicantServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import dmu.dasom.api.domain.applicant.dto.ApplicantCreateRequestDto;
import dmu.dasom.api.domain.applicant.dto.ApplicantDetailsResponseDto;
import dmu.dasom.api.domain.applicant.dto.ApplicantResponseDto;
import dmu.dasom.api.domain.applicant.dto.ApplicantStatusUpdateRequestDto;
import dmu.dasom.api.domain.applicant.entity.Applicant;
import dmu.dasom.api.domain.applicant.enums.ApplicantStatus;
import dmu.dasom.api.domain.applicant.repository.ApplicantRepository;
Expand All @@ -14,6 +15,7 @@
import dmu.dasom.api.domain.google.service.GoogleApiService;
import dmu.dasom.api.global.dto.PageResponse;
import jakarta.mail.MessagingException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand All @@ -23,6 +25,7 @@
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.test.util.ReflectionTestUtils;

import java.time.LocalDateTime;
import java.util.Collections;
Expand All @@ -47,6 +50,11 @@ class ApplicantServiceTest {
@Mock
private GoogleApiService googleApiService;

@BeforeEach
void setup() {
ReflectionTestUtils.setField(applicantService, "spreadSheetId", "test-spreadsheet-id");
}

@Test
@DisplayName("지원자 저장 - 성공")
void apply_success() {
Expand Down Expand Up @@ -241,4 +249,103 @@ void getApplicantByStudentNo_fail() {
assertEquals(ErrorCode.ARGUMENT_NOT_VALID, exception.getErrorCode());
}

@Test
@DisplayName("지원자 상태 변경 - Google Sheets에 없는 사용자 추가")
void updateApplicantStatus_addToGoogleSheets() {
// given
Long applicantId = 1L;
String testSpreadsheetId = "test-spreadsheet-id";

ApplicantStatusUpdateRequestDto request = mock(ApplicantStatusUpdateRequestDto.class);
when(request.getStatus()).thenReturn(ApplicantStatus.INTERVIEW_PASSED);

// Mock 지원자 생성
Applicant mockApplicant = Applicant.builder()
.name("홍길동")
.studentNo("20210000")
.contact("010-1234-5678")
.email("[email protected]")
.grade(2)
.reasonForApply("팀 활동 경험을 쌓고 싶습니다.")
.activityWish("프로그래밍 스터디 참여")
.isPrivacyPolicyAgreed(true)
.status(ApplicantStatus.PENDING)
.createdAt(LocalDateTime.now())
.updatedAt(LocalDateTime.now())
.build();

when(applicantRepository.findById(applicantId)).thenReturn(Optional.of(mockApplicant));

// 모든 인자를 Matchers로 설정
when(googleApiService.findRowIndexByStudentNo(eq(testSpreadsheetId), eq("Sheet1"), eq("20210000")))
.thenReturn(-1);

doNothing().when(googleApiService).appendToSheet(anyList());

// when
applicantService.updateApplicantStatus(applicantId, request);

// then
verify(applicantRepository).findById(applicantId);
verify(googleApiService).appendToSheet(List.of(mockApplicant));
}

@Test
@DisplayName("지원자 상태 변경 - Google Sheets에서 상태 업데이트")
void updateApplicantStatus_updateInGoogleSheets() {
// given
Long applicantId = 1L;

// 요청 DTO 설정
ApplicantStatusUpdateRequestDto request = mock(ApplicantStatusUpdateRequestDto.class);
when(request.getStatus()).thenReturn(ApplicantStatus.INTERVIEW_PASSED);

// Mock 지원자 생성
Applicant mockApplicant = Applicant.builder()
.name("홍길동")
.studentNo("20210000")
.contact("010-1234-5678")
.email("[email protected]")
.grade(2)
.reasonForApply("팀 활동 경험을 쌓고 싶습니다.")
.activityWish("프로그래밍 스터디 참여")
.isPrivacyPolicyAgreed(true)
.status(ApplicantStatus.PENDING)
.createdAt(LocalDateTime.now())
.updatedAt(LocalDateTime.now())
.build();

// Repository와 GoogleApiService의 동작 모킹
when(applicantRepository.findById(applicantId)).thenReturn(Optional.of(mockApplicant));
when(googleApiService.findRowIndexByStudentNo(anyString(), eq("Sheet1"), eq("20210000"))).thenReturn(5); // Google Sheets에 있음
doNothing().when(googleApiService).updateSheet(anyList());

// when
applicantService.updateApplicantStatus(applicantId, request);

// then
verify(applicantRepository).findById(applicantId);
verify(googleApiService).updateSheet(List.of(mockApplicant));
}

@Test
@DisplayName("지원자 저장 - 덮어쓰기 (Google Sheets 연동 포함)")
void apply_overwrite_withGoogleSheets() {
// given
ApplicantCreateRequestDto request = mock(ApplicantCreateRequestDto.class);
when(request.getStudentNo()).thenReturn("20210000");

Applicant existingApplicant = mock(Applicant.class); // 기존 Applicant 객체 모킹
when(applicantRepository.findByStudentNo("20210000")).thenReturn(Optional.of(existingApplicant));

when(request.getIsOverwriteConfirmed()).thenReturn(true);

// when
applicantService.apply(request);

// then
verify(existingApplicant).overwrite(request);
verify(googleApiService).updateSheet(List.of(existingApplicant));
}

}