-
Notifications
You must be signed in to change notification settings - Fork 0
feat: ES 장애 시 약 검색 MySQL 자동 폴백 #95
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
...est/java/com/piuda/callcare/domain/druginfo/service/query/DrugSearchQueryServiceTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| package com.piuda.callcare.domain.druginfo.service.query; | ||
|
|
||
| import com.piuda.callcare.domain.druginfo.converter.DrugInfoConverter; | ||
| import com.piuda.callcare.domain.druginfo.document.DrugDocument; | ||
| import com.piuda.callcare.domain.druginfo.dto.response.DrugSearchResponse; | ||
| import com.piuda.callcare.domain.druginfo.repository.DrugSearchRepository; | ||
| import com.piuda.callcare.global.exception.CallCareException; | ||
| import com.piuda.callcare.global.exception.ErrorCode; | ||
| import org.junit.jupiter.api.DisplayName; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.extension.ExtendWith; | ||
| import org.mockito.InjectMocks; | ||
| import org.mockito.Mock; | ||
| import org.mockito.junit.jupiter.MockitoExtension; | ||
| import org.springframework.dao.DataAccessResourceFailureException; | ||
| import org.springframework.data.domain.PageRequest; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
| import static org.mockito.ArgumentMatchers.any; | ||
| import static org.mockito.ArgumentMatchers.anyString; | ||
| import static org.mockito.BDDMockito.given; | ||
| import static org.mockito.BDDMockito.then; | ||
| import static org.mockito.Mockito.never; | ||
|
|
||
| @ExtendWith(MockitoExtension.class) | ||
| @DisplayName("DrugSearchQueryService 단위 테스트 — ES 장애 시 MySQL 자동 폴백") | ||
| class DrugSearchQueryServiceTest { | ||
|
|
||
| @InjectMocks | ||
| private DrugSearchQueryService drugSearchQueryService; | ||
|
|
||
| @Mock private DrugSearchRepository drugSearchRepository; | ||
| @Mock private DrugInfoQueryService drugInfoQueryService; | ||
| @Mock private DrugInfoConverter drugInfoConverter; | ||
|
|
||
| @Test | ||
| @DisplayName("정상 케이스: ES가 정상이면 MySQL 폴백을 타지 않는다") | ||
| void search_uses_es_when_healthy() { | ||
| DrugDocument document = DrugDocument.builder().itemSeq("1").itemName("타이레놀").build(); | ||
| DrugSearchResponse response = new DrugSearchResponse("1", "타이레놀", null, null, null, null); | ||
| given(drugSearchRepository.searchByItemName(anyString(), any())).willReturn(List.of(document)); | ||
| given(drugInfoConverter.toSearchResponse(document)).willReturn(response); | ||
|
|
||
| List<DrugSearchResponse> result = drugSearchQueryService.search("타이레놀"); | ||
|
|
||
| assertThat(result).containsExactly(response); | ||
| then(drugInfoQueryService).should(never()).searchByKeyword(anyString()); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("장애 케이스: ES 접근 예외가 나면 MySQL LIKE 검색으로 폴백한다") | ||
| void search_falls_back_to_mysql_when_es_fails() { | ||
| DrugSearchResponse fallbackResponse = new DrugSearchResponse("1", "타이레놀", null, null, null, null); | ||
| given(drugSearchRepository.searchByItemName(anyString(), any())) | ||
| .willThrow(new DataAccessResourceFailureException("ES 연결 실패")); | ||
| given(drugInfoQueryService.searchByKeyword("타이레놀")).willReturn(List.of(fallbackResponse)); | ||
|
|
||
| List<DrugSearchResponse> result = drugSearchQueryService.search("타이레놀"); | ||
|
|
||
| assertThat(result).containsExactly(fallbackResponse); | ||
| then(drugInfoQueryService).should().searchByKeyword("타이레놀"); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("예외 케이스: 빈 키워드는 폴백 없이 즉시 INVALID_PARAMETER") | ||
| void search_throws_on_blank_keyword_without_fallback() { | ||
| assertThatThrownBy(() -> drugSearchQueryService.search(" ")) | ||
| .isInstanceOf(CallCareException.class) | ||
| .hasFieldOrPropertyWithValue("errorCode", ErrorCode.INVALID_PARAMETER); | ||
|
|
||
| then(drugSearchRepository).should(never()).searchByItemName(anyString(), any()); | ||
| then(drugInfoQueryService).should(never()).searchByKeyword(anyString()); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("경계: 검색어 앞뒤 공백은 잘라서 폴백 서비스에도 그대로 전달된다") | ||
| void search_trims_keyword_before_fallback() { | ||
| given(drugSearchRepository.searchByItemName(anyString(), any())) | ||
| .willThrow(new DataAccessResourceFailureException("ES 연결 실패")); | ||
| given(drugInfoQueryService.searchByKeyword("타이레놀")).willReturn(List.of()); | ||
|
|
||
| drugSearchQueryService.search(" 타이레놀 "); | ||
|
|
||
| then(drugSearchRepository).should().searchByItemName("타이레놀", PageRequest.of(0, 20)); | ||
| then(drugInfoQueryService).should().searchByKeyword("타이레놀"); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔒 Security & Privacy | 🟡 Minor | ⚡ Quick win
Sensitive Data Exposure (CWE-532): Insertion of Sensitive Information into Log File
Reachability: External · Exploitability: Moderate
검색어 원문을 로그에 기록하지 마세요.
DataAccessException발생 시 외부 검색어인trimmed가WARN로그에 기록됩니다. 검색어가 건강 정보를 포함할 수 있으므로 로그 메시지에서 검색어를 제거하고 예외 stack trace만 기록하세요.🧰 Tools
🪛 PMD (7.26.0)
[Low] 47-47: InvalidLogMessageFormat (Error Prone): Too many arguments, expected 1 argument but found 2
(InvalidLogMessageFormat (Error Prone))
🤖 Prompt for AI Agents