Skip to content

Commit

Permalink
Test(Problem): 자격증마다 인덱스를 분리한 변경에 따른 테스트 코드 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
morenow98 committed Sep 3, 2024
1 parent 821f977 commit df97d5e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.jabiseo.problem.application.usecase;

import com.jabiseo.certificate.domain.Certificate;
import com.jabiseo.opensearch.SimilarProblemsProvider;
import com.jabiseo.problem.domain.Problem;
import com.jabiseo.problem.domain.ProblemRepository;
import com.jabiseo.problem.dto.FindSimilarProblemResponse;
import com.jabiseo.problem.dto.ProblemWithBookmarkSummaryQueryDto;
Expand All @@ -12,7 +14,10 @@
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.List;
import java.util.Optional;

import static fixture.CertificateFixture.createCertificate;
import static fixture.ProblemFixture.createProblem;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given;

Expand All @@ -31,21 +36,26 @@ class FindSimilarProblemsUseCaseTest {

@Test
@DisplayName("유사 문제 조회를 성공한다.")
void givenMemberIdAndProblemId_whenFindingSimilarProblems_thenFindSimilarProblems() throws Exception {
void givenMemberIdAndProblemId_whenFindingSimilarProblems_thenFindSimilarProblems() {
//given
Long memberId = 1L;
Long problemId = 2L;
Long examId = 3L;
Long subjectId = 4L;
Long certificateId = 5L;
List<Long> similarProblemIds = List.of(5L, 6L, 7L);

Certificate certificate = createCertificate(certificateId);
Problem problem = createProblem(problemId, certificate);
List<ProblemWithBookmarkSummaryQueryDto> problemWithBookmarkSummaryQueryDtos =
similarProblemIds.stream()
.map(id -> new ProblemWithBookmarkSummaryQueryDto(
id, "문제", false, examId, "설명", subjectId, "이름", 1
))
.toList();
.map(id -> new ProblemWithBookmarkSummaryQueryDto(
id, "문제", false, examId, "설명", subjectId, "이름", 1
))
.toList();

given(similarProblemsProvider.getSimilarProblems(problemId, 3)).willReturn(similarProblemIds);
given(similarProblemsProvider.getSimilarProblems(problemId, certificateId, 3)).willReturn(similarProblemIds);
given(problemRepository.findById(problemId)).willReturn(Optional.of(problem));
given(problemRepository.findSummaryByIdsInWithBookmark(memberId, similarProblemIds)).willReturn(problemWithBookmarkSummaryQueryDtos);

//when
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,14 @@ class SimilarProblemsProviderTest {
@DisplayName("redis 캐시에 유사 문제 id가 저장되어 있는 경우 Opensearch와 통신하지 않는다.")
void givenCachedProblemId_whenFindSimilarProblemIds_thenDontConnectWithOpenSearch() throws Exception {
//given
Long problemId = 1L;
Long certificateId = 1L; //정보처리기사
Long problemId = 2L;
int similarProblemsSize = 3;
given(similarProblemIdCacheRepository.findById(problemId))
.willReturn(Optional.of(new SimilarProblemIdCache(problemId, List.of(2L, 3L, 4L))));

//when
sut.getSimilarProblems(problemId, similarProblemsSize);
sut.getSimilarProblems(problemId, certificateId, similarProblemsSize);

//then
verify(openSearchClient, never()).get((GetRequest) any(), any());
Expand All @@ -58,14 +59,15 @@ void givenCachedProblemId_whenFindSimilarProblemIds_thenDontConnectWithOpenSearc
@DisplayName("opensearch에 문제 벡터 조회 시 오류가 발생하면 NetworkApiException을 던진다.")
void givenErrorInOpenSearchConnection_whenFindSimilarProblems_thenThrowNetworkApiException() throws Exception {
//given
Long problemId = 1L;
Long certificateId = 1L; //정보처리기사
Long problemId = 2L;
int similarProblemsSize = 3;

given(similarProblemIdCacheRepository.findById(problemId)).willReturn(Optional.empty());
given(openSearchClient.get((GetRequest) any(), any())).willThrow(new IOException());

//when & then
assertThatThrownBy(() -> sut.getSimilarProblems(problemId, similarProblemsSize))
assertThatThrownBy(() -> sut.getSimilarProblems(problemId, certificateId, similarProblemsSize))
.isInstanceOf(NetworkApiException.class)
.hasFieldOrPropertyWithValue("errorCode", NetworkApiErrorCode.OPENSEARCH_API_FAIL);
}
Expand Down

0 comments on commit df97d5e

Please sign in to comment.