Skip to content
Open
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 @@ -12,6 +12,7 @@
import com.jobnote.domain.document.service.DocumentService;
import com.jobnote.domain.schedule.dto.ScheduleResponse;
import com.jobnote.domain.schedule.service.ScheduleService;
import com.jobnote.domain.user.domain.UserFixture;
import com.jobnote.global.exception.JobNoteException;
import com.jobnote.domain.user.domain.User;
import com.jobnote.domain.user.service.UserService;
Expand Down Expand Up @@ -40,31 +41,19 @@
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;

class ApplicationFormServiceTest extends ServiceUnitTest {

@InjectMocks
private ApplicationFormService applicationFormService;
@InjectMocks private ApplicationFormService applicationFormService;

@Mock
private UserService userService;
@Mock private UserService userService;
@Mock private ScheduleService scheduleService;
@Mock private DocumentService documentService;
@Mock private ApplicationFormDocumentService applicationFormDocumentService;

@Mock
private ScheduleService scheduleService;

@Mock
private DocumentService documentService;

@Mock
private ApplicationFormDocumentService applicationFormDocumentService;

@Mock
private ApplicationFormRepository applicationFormRepository;

@Mock
private ApplicationFormDocumentRepository applicationFormDocumentRepository;
@Mock private ApplicationFormRepository applicationFormRepository;
@Mock private ApplicationFormDocumentRepository applicationFormDocumentRepository;

private User user;
private ApplicationForm applicationForm;
Expand All @@ -74,115 +63,129 @@ class ApplicationFormServiceTest extends ServiceUnitTest {

@BeforeEach
void setUp() {
user = mock(User.class);
applicationForm = mock(ApplicationForm.class);
user = UserFixture.createMember(userId, "test@gmail.com", "1234", "test");
applicationForm = ApplicationForm.builder()
.user(user)
.companyName("네이버")
.companyAddress("경기도 성남시 분당구")
.companyUrl("https://naver.com")
.companyScale("대기업")
.position("백엔드 개발자")
.status(APPLIED)
.build();
}

@DisplayName("지원서 목록을 전체 조회한다.")
@Test
void getAllApplicationForms() {
// given
ApplicationForm form1 = ApplicationForm.builder().user(user).companyName("네이버").status(APPLIED).build();
ApplicationForm form2 = ApplicationForm.builder().user(user).companyName("카카오").status(DOCUMENT_PASSED).build();
List<ApplicationForm> expectedResult = Arrays.asList(form1, form2);
@Nested
@DisplayName("지원서 다건 조회")
class GetAll {

Page<ApplicationForm> pageExpectedResult = new PageImpl<>(expectedResult);
given(applicationFormRepository.findAllByUserId(eq(userId), any(Pageable.class))).willReturn(pageExpectedResult);
@DisplayName("지원서 목록을 전체 조회한다.")
@Test
void getAllApplicationForms() {
// given
ApplicationForm form1 = ApplicationForm.builder().user(user).companyName("네이버").status(APPLIED).build();
ApplicationForm form2 = ApplicationForm.builder().user(user).companyName("카카오").status(DOCUMENT_PASSED).build();
List<ApplicationForm> expectedResult = Arrays.asList(form1, form2);

// when
Page<ApplicationFormResponse> pageActualResult = applicationFormService.getAll(userId, Pageable.unpaged());
List<ApplicationFormResponse> actualResult = pageActualResult.getContent();
Page<ApplicationForm> pageExpectedResult = new PageImpl<>(expectedResult);
given(applicationFormRepository.findAllByUserId(eq(userId), any(Pageable.class))).willReturn(pageExpectedResult);

// then
assertThat(actualResult).hasSize(2);
assertThat(actualResult.get(0).companyName()).isEqualTo("네이버");
then(applicationFormRepository).should().findAllByUserId(userId, Pageable.unpaged());
}
// when
Page<ApplicationFormResponse> pageActualResult = applicationFormService.getAll(userId, Pageable.unpaged());
List<ApplicationFormResponse> actualResult = pageActualResult.getContent();

@Test
@DisplayName("지원서마다 일정들이 그룹핑되어 함께 반환된다")
void getAllApplicationForms_withSchedules() {
// given
ApplicationForm form1 = ApplicationForm.builder().user(user).companyName("네이버").status(APPLIED).build();
ApplicationForm form2 = ApplicationForm.builder().user(user).companyName("카카오").status(DOCUMENT_PASSED).build();
ReflectionTestUtils.setField(form1, "id", 1L);
ReflectionTestUtils.setField(form2, "id", 2L);
List<ApplicationForm> forms = List.of(form1, form2);
// then
assertThat(actualResult).hasSize(2);
assertThat(actualResult.get(0).companyName()).isEqualTo("네이버");
then(applicationFormRepository).should().findAllByUserId(userId, Pageable.unpaged());
}

Page<ApplicationForm> pageForms = new PageImpl<>(forms);
given(applicationFormRepository.findAllByUserId(eq(userId), any(Pageable.class))).willReturn(pageForms);
@Test
@DisplayName("지원서마다 일정들이 그룹핑되어 함께 반환된다")
void getAllApplicationForms_withSchedules() {
// given
ApplicationForm form1 = ApplicationForm.builder().user(user).companyName("네이버").status(APPLIED).build();
ApplicationForm form2 = ApplicationForm.builder().user(user).companyName("카카오").status(DOCUMENT_PASSED).build();
ReflectionTestUtils.setField(form1, "id", 1L);
ReflectionTestUtils.setField(form2, "id", 2L);
List<ApplicationForm> forms = List.of(form1, form2);

ScheduleResponse schedule1 = new ScheduleResponse(101L, "지원서 제출", "오전", LocalDateTime.of(2025, 8, 1, 10, 0), PLANNED);
ScheduleResponse schedule2 = new ScheduleResponse(102L, "코딩테스트", "연습문제 풀이", LocalDateTime.of(2025, 8, 2, 9, 0), PLANNED);
ScheduleResponse schedule3 = new ScheduleResponse(103L, "2차 면접", "예상질문지 복습", LocalDateTime.of(2025, 8, 5, 9, 0), PLANNED);
Page<ApplicationForm> pageForms = new PageImpl<>(forms);
given(applicationFormRepository.findAllByUserId(eq(userId), any(Pageable.class))).willReturn(pageForms);

given(scheduleService.getAllGroupedByApplicationFormIds(eq(userId), eq(List.of(1L, 2L))))
.willReturn(Map.of(
1L, List.of(schedule1),
2L, List.of(schedule2, schedule3)
));
ScheduleResponse schedule1 = new ScheduleResponse(101L, "지원서 제출", "오전", LocalDateTime.of(2025, 8, 1, 10, 0), PLANNED);
ScheduleResponse schedule2 = new ScheduleResponse(102L, "코딩테스트", "연습문제 풀이", LocalDateTime.of(2025, 8, 2, 9, 0), PLANNED);
ScheduleResponse schedule3 = new ScheduleResponse(103L, "2차 면접", "예상질문지 복습", LocalDateTime.of(2025, 8, 5, 9, 0), PLANNED);

// when
Page<ApplicationFormResponse> actualResult = applicationFormService.getAll(userId, Pageable.unpaged());
given(scheduleService.getAllGroupedByApplicationFormIds(eq(userId), eq(List.of(1L, 2L))))
.willReturn(Map.of(
1L, List.of(schedule1),
2L, List.of(schedule2, schedule3)
));

// then
assertThat(actualResult).hasSize(2);
// when
Page<ApplicationFormResponse> actualResult = applicationFormService.getAll(userId, Pageable.unpaged());

ApplicationFormResponse result1 = actualResult.getContent().get(0);
assertThat(result1.companyName()).isEqualTo("네이버");
assertThat(result1.schedules()).hasSize(1);
assertThat(result1.schedules().get(0).title()).isEqualTo("지원서 제출");
// then
assertThat(actualResult).hasSize(2);

ApplicationFormResponse result2 = actualResult.getContent().get(1);
assertThat(result2.companyName()).isEqualTo("카카오");
assertThat(result2.schedules()).hasSize(2);
assertThat(result2.schedules().get(0).title()).isEqualTo("코딩테스트");
ApplicationFormResponse result1 = actualResult.getContent().get(0);
assertThat(result1.companyName()).isEqualTo("네이버");
assertThat(result1.schedules()).hasSize(1);
assertThat(result1.schedules().get(0).title()).isEqualTo("지원서 제출");

then(applicationFormRepository).should().findAllByUserId(eq(userId), any(Pageable.class));
then(scheduleService).should().getAllGroupedByApplicationFormIds(eq(userId), eq(List.of(1L, 2L)));
}
ApplicationFormResponse result2 = actualResult.getContent().get(1);
assertThat(result2.companyName()).isEqualTo("카카오");
assertThat(result2.schedules()).hasSize(2);
assertThat(result2.schedules().get(0).title()).isEqualTo("코딩테스트");

@Test
@DisplayName("지원서마다 문서들이 그룹핑되어 함께 반환된다")
void getAllApplicationForms_withDocuments() {
// given
ApplicationForm form1 = ApplicationForm.builder().user(user).companyName("네이버").status(APPLIED).build();
ApplicationForm form2 = ApplicationForm.builder().user(user).companyName("카카오").status(DOCUMENT_PASSED).build();
ReflectionTestUtils.setField(form1, "id", 1L);
ReflectionTestUtils.setField(form2, "id", 2L);
List<ApplicationForm> forms = List.of(form1, form2);
then(applicationFormRepository).should().findAllByUserId(eq(userId), any(Pageable.class));
then(scheduleService).should().getAllGroupedByApplicationFormIds(eq(userId), eq(List.of(1L, 2L)));
}

Page<ApplicationForm> pageForms = new PageImpl<>(forms);
given(applicationFormRepository.findAllByUserId(eq(userId), any(Pageable.class))).willReturn(pageForms);
@Test
@DisplayName("지원서마다 문서들이 그룹핑되어 함께 반환된다")
void getAllApplicationForms_withDocuments() {
// given
ApplicationForm form1 = ApplicationForm.builder().user(user).companyName("네이버").status(APPLIED).build();
ApplicationForm form2 = ApplicationForm.builder().user(user).companyName("카카오").status(DOCUMENT_PASSED).build();
ReflectionTestUtils.setField(form1, "id", 1L);
ReflectionTestUtils.setField(form2, "id", 2L);
List<ApplicationForm> forms = List.of(form1, form2);

DocumentSimpleResponse document1 = new DocumentSimpleResponse(101L, DocumentType.RESUME, "네이버 이력서");
DocumentSimpleResponse document2 = new DocumentSimpleResponse(102L, DocumentType.COVER_LETTER, "네이버 자소서");
DocumentSimpleResponse document3 = new DocumentSimpleResponse(103L, DocumentType.RESUME, "카카오 이력서");
Page<ApplicationForm> pageForms = new PageImpl<>(forms);
given(applicationFormRepository.findAllByUserId(eq(userId), any(Pageable.class))).willReturn(pageForms);

given(applicationFormDocumentService.getAllSimpleGroupedByApplicationFormIds(eq(userId), eq(List.of(1L, 2L))))
.willReturn(Map.of(
1L, List.of(document1, document2),
2L, List.of(document3)
));
DocumentSimpleResponse document1 = new DocumentSimpleResponse(101L, DocumentType.RESUME, "네이버 이력서");
DocumentSimpleResponse document2 = new DocumentSimpleResponse(102L, DocumentType.COVER_LETTER, "네이버 자소서");
DocumentSimpleResponse document3 = new DocumentSimpleResponse(103L, DocumentType.RESUME, "카카오 이력서");

// when
Page<ApplicationFormResponse> actualResult = applicationFormService.getAll(userId, Pageable.unpaged());
given(applicationFormDocumentService.getAllSimpleGroupedByApplicationFormIds(eq(userId), eq(List.of(1L, 2L))))
.willReturn(Map.of(
1L, List.of(document1, document2),
2L, List.of(document3)
));

// then
assertThat(actualResult).hasSize(2);
// when
Page<ApplicationFormResponse> actualResult = applicationFormService.getAll(userId, Pageable.unpaged());

// then
assertThat(actualResult).hasSize(2);

ApplicationFormResponse result1 = actualResult.getContent().get(0);
assertThat(result1.companyName()).isEqualTo("네이버");
assertThat(result1.documents()).hasSize(2);
assertThat(result1.documents().get(0).title()).isEqualTo("네이버 이력서");

ApplicationFormResponse result1 = actualResult.getContent().get(0);
assertThat(result1.companyName()).isEqualTo("네이버");
assertThat(result1.documents()).hasSize(2);
assertThat(result1.documents().get(0).title()).isEqualTo("네이버 이력서");
ApplicationFormResponse result2 = actualResult.getContent().get(1);
assertThat(result2.companyName()).isEqualTo("카카오");
assertThat(result2.documents()).hasSize(1);
assertThat(result2.documents().get(0).title()).isEqualTo("카카오 이력서");

ApplicationFormResponse result2 = actualResult.getContent().get(1);
assertThat(result2.companyName()).isEqualTo("카카오");
assertThat(result2.documents()).hasSize(1);
assertThat(result2.documents().get(0).title()).isEqualTo("카카오 이력서");
then(applicationFormRepository).should().findAllByUserId(eq(userId), any(Pageable.class));
then(applicationFormDocumentService).should().getAllSimpleGroupedByApplicationFormIds(eq(userId), eq(List.of(1L, 2L)));
}

then(applicationFormRepository).should().findAllByUserId(eq(userId), any(Pageable.class));
then(applicationFormDocumentService).should().getAllSimpleGroupedByApplicationFormIds(eq(userId), eq(List.of(1L, 2L)));
}

@Nested
Expand All @@ -193,7 +196,6 @@ class GetById {
void success() {
// given
given(applicationFormRepository.findById(formId)).willReturn(Optional.of(applicationForm));
willDoNothing().given(applicationForm).validateOwner(userId);

// when
applicationFormService.getById(userId, formId);
Expand All @@ -219,12 +221,9 @@ void fail_notFound() {
void fail_forbidden() {
// given
given(applicationFormRepository.findById(formId)).willReturn(Optional.of(applicationForm));
doThrow(new JobNoteException(FORBIDDEN))
.when(applicationForm)
.validateOwner(userId);

// when & then
assertThatThrownBy(() -> applicationFormService.getById(userId, formId))
assertThatThrownBy(() -> applicationFormService.getById(2L, formId))
.isInstanceOf(JobNoteException.class)
.hasMessage(FORBIDDEN.getMessage());
}
Expand All @@ -246,7 +245,7 @@ void saveApplicationForm() {
}

@Nested
@DisplayName("지원서 수정")
@DisplayName("지원서 업데이트")
class Update {
ApplicationFormRequest request = new ApplicationFormRequest("카카오", "경기도 성남시", null, null, null, APPLIED, null, null);

Expand All @@ -255,14 +254,13 @@ class Update {
void success() {
// given
given(applicationFormRepository.findById(formId)).willReturn(Optional.of(applicationForm));
willDoNothing().given(applicationForm).validateOwner(userId);

// when
applicationFormService.update(userId, formId, request);

// then
then(applicationFormRepository).should().findById(formId);
then(applicationForm).should().update(request);
ApplicationFormResponse form = applicationFormService.getById(userId, formId);
assertThat(form.companyName()).isEqualTo("카카오");
}

@DisplayName("실패 - 지원서를 찾을 수 없음")
Expand All @@ -275,23 +273,18 @@ void fail_notFound() {
assertThatThrownBy(() -> applicationFormService.update(userId, formId, request))
.isInstanceOf(JobNoteException.class)
.hasMessage(NOT_FOUND_APPLICATION_FORM.getMessage());
then(applicationForm).should(never()).update(request);
}

@DisplayName("실패 - 권한 없음")
@Test
void fail_forbidden() {
// given
given(applicationFormRepository.findById(formId)).willReturn(Optional.of(applicationForm));
doThrow(new JobNoteException(FORBIDDEN))
.when(applicationForm)
.validateOwner(userId);

// when & then
assertThatThrownBy(() -> applicationFormService.update(userId, formId, request))
assertThatThrownBy(() -> applicationFormService.update(2L, formId, request))
.isInstanceOf(JobNoteException.class)
.hasMessage(FORBIDDEN.getMessage());
then(applicationForm).should(never()).update(request);
}
}

Expand All @@ -303,7 +296,6 @@ class Delete {
void success() {
// given
given(applicationFormRepository.findById(formId)).willReturn(Optional.of(applicationForm));
willDoNothing().given(applicationForm).validateOwner(userId);

// when
applicationFormService.delete(userId, formId);
Expand All @@ -330,12 +322,9 @@ void fail_notFound() {
void fail_forbidden() {
// given
given(applicationFormRepository.findById(formId)).willReturn(Optional.of(applicationForm));
doThrow(new JobNoteException(FORBIDDEN))
.when(applicationForm)
.validateOwner(userId);

// when & then
assertThatThrownBy(() -> applicationFormService.delete(userId, formId))
assertThatThrownBy(() -> applicationFormService.delete(2L, formId))
.isInstanceOf(JobNoteException.class)
.hasMessage(FORBIDDEN.getMessage());
then(applicationFormRepository).should(never()).delete(any(ApplicationForm.class));
Expand Down
Loading
Loading