Skip to content

Commit

Permalink
Merge pull request #177 from nhnacademy-be5-T3Team/feature/elastic
Browse files Browse the repository at this point in the history
Feature/elastic
  • Loading branch information
parkjonggyeong18 authored May 16, 2024
2 parents 37a1931 + 4affeb7 commit a2c49fa
Show file tree
Hide file tree
Showing 4 changed files with 228 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.t3t.bookstoreapi.elastic.service.AutocompleteService;
import com.t3t.bookstoreapi.model.response.BaseResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
Expand All @@ -23,8 +24,14 @@ public class AutocompleteController {
* @author parkjonggyeong18(박종경)
*/
@GetMapping("/autocomplete")
public ResponseEntity<BaseResponse<List<String>>> autocomplete(@RequestParam String prefix) throws IOException {
BaseResponse<List<String>> autocomplete = autocompleteService.autocomplete(prefix);
return ResponseEntity.ok().body(autocomplete);
public ResponseEntity<BaseResponse<List<String>>> autocomplete(@RequestParam String prefix) {
BaseResponse<List<String>> autocomplete;
try {
autocomplete = autocompleteService.autocomplete(prefix);
return ResponseEntity.ok().body(autocomplete);
} catch (IOException e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body(new BaseResponse<List<String>>().message("잘못된 접근입니다."));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.t3t.bookstoreapi.elastic.service.AutocompleteService;
import com.t3t.bookstoreapi.model.response.BaseResponse;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
Expand Down Expand Up @@ -31,7 +32,8 @@ void setUp() {
}

@Test
void autocomplete_ReturnsExpectedResults() throws IOException {
@DisplayName("자동완성 test")
void autocompleteTest() throws IOException {
// Given
String prefix = "test";
List<String> expectedData = Arrays.asList("example");
Expand All @@ -47,4 +49,18 @@ void autocomplete_ReturnsExpectedResults() throws IOException {
assertEquals(HttpStatus.OK, response.getStatusCode());
assertEquals(expectedData, response.getBody().getData());
}
@Test
@DisplayName("자동완성 API 테스트 - 예외 처리")
public void testAutocompleteError() throws IOException {
// Given
String prefix = "test";
when(autocompleteService.autocomplete(prefix)).thenThrow(IOException.class);

// When
ResponseEntity<BaseResponse<List<String>>> responseEntity = autocompleteController.autocomplete(prefix);

// Then
assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
assertEquals("잘못된 접근입니다.", responseEntity.getBody().getMessage());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,26 @@
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockedStatic;

import org.mockito.Mockito;

import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.SearchHitsImpl;
import org.springframework.test.context.junit.jupiter.SpringExtension;

import java.math.BigDecimal;
import java.time.Instant;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;

import static org.mockito.Mockito.*;
import static org.mockito.Mockito.mock;

import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.when;

Expand Down Expand Up @@ -85,4 +94,100 @@ public void testSearch() {
assertEquals("Test Book", elasticResponse.getName());
assertEquals(4.0f, elasticResponse.getAverageScore()); // 검증
}
@Test
@DisplayName("엘라스틱서치 검색 기능 테스트-null인 경우")
public void testSearchNull() {
// Given
String query = "test";
String searchType = "book_name";
BigDecimal categoryId = new BigDecimal("10");
PageRequest pageable = PageRequest.of(0, 10);

ElasticCategoryService elasticService = mock(ElasticCategoryService.class);
Mockito.lenient().when(elasticService.search(query, searchType, categoryId, pageable)).thenReturn(null);

ElasticDocument document = mock(ElasticDocument.class);
Mockito.lenient().when(document.getCoverImageUrl()).thenReturn(null);
Mockito.lenient().when(document.getName()).thenReturn("Test Book");
Mockito.lenient().when(document.getAverageScore()).thenReturn(4.0f);

List<SearchHit<ElasticDocument>> searchHitList = new ArrayList<>();
searchHitList.add(new SearchHit<>(null, null, null, 1.0f, null, null, document));
SearchHits<ElasticDocument> searchHits = new SearchHitsImpl<>(1, null, 1.0f, null, searchHitList, null, null);
Mockito.lenient().when(elasticRepository.findByBookNameCategory(query, categoryId, pageable)).thenReturn(searchHits);

// When
PageResponse<ElasticResponse> response = elasticService.search(query, searchType, categoryId, pageable);

// Then
if (response != null) {
ElasticResponse elasticResponse = response.getContent().get(0);
assertEquals("Test Book", elasticResponse.getName());
assertEquals(4.0f, elasticResponse.getAverageScore());
}
}

@Test
@DisplayName("엘라스틱서치 검색 결과 응답 객체 생성 테스트")
public void testBuildElasticSearchResultResponse() {
// Given
ElasticDocument document = new ElasticDocument();
document.setBookId(BigDecimal.ONE);
document.setName("Test Book");
document.setPrice(BigDecimal.valueOf(100));
document.setDiscount(BigDecimal.TEN);
document.setDiscountPrice(BigDecimal.valueOf(90));
document.setPublished("2020-01-01T00:00:00Z");
document.setAverageScore(4.0f);
document.setLikeCount(BigDecimal.valueOf(100));
document.setPublisher("Test Publisher");
document.setCoverImageUrl("http://example.com/image.jpg");
document.setAuthorName("Author Name");
document.setAuthorRole("Author Role");

float score = 1.0f;
long searchBookCount = 1;

// 가짜 ElasticService 생성
ElasticService elasticService = mock(ElasticService.class);

// When
// 가짜 ElasticService를 통해 메서드 호출 시 응답 값 설정
when(elasticService.buildElasticSearchResultResponse(document, score, searchBookCount))
.thenReturn(new ElasticResponse(
BigDecimal.ONE,
"Test Book",
BigDecimal.valueOf(100),
BigDecimal.TEN,
BigDecimal.valueOf(90),
Instant.parse("2020-01-01T00:00:00Z").atZone(ZoneId.systemDefault()).toLocalDate(),
4.0f,
BigDecimal.valueOf(100),
"Test Publisher",
"http://example.com/image.jpg",
"Author Name",
"Author Role",
1.0f,
BigDecimal.TEN,
1
));

// Then
ElasticResponse elasticResponse = elasticService.buildElasticSearchResultResponse(document, score, searchBookCount);
assertEquals("Test Book", elasticResponse.getName());
assertEquals(BigDecimal.ONE, elasticResponse.getBookId());
assertEquals(BigDecimal.valueOf(100), elasticResponse.getPrice());
assertEquals(BigDecimal.TEN, elasticResponse.getDiscountRate());
assertEquals(BigDecimal.valueOf(90), elasticResponse.getDiscountedPrice());
assertEquals(Instant.parse("2020-01-01T00:00:00Z").atZone(ZoneId.systemDefault()).toLocalDate(), elasticResponse.getPublished());
assertEquals(4.0f, elasticResponse.getAverageScore());
assertEquals(BigDecimal.valueOf(100), elasticResponse.getLikeCount());
assertEquals("Test Publisher", elasticResponse.getPublisher());
assertEquals("http://example.com/image.jpg", elasticResponse.getCoverImageUrl());
assertEquals("Author Name", elasticResponse.getAuthorName());
assertEquals("Author Role", elasticResponse.getAuthorRole());
assertEquals(1.0f, elasticResponse.getScore());
assertEquals(BigDecimal.TEN, elasticResponse.getCategoryId());
assertEquals(1, elasticResponse.getCount());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockedStatic;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.SearchHit;
Expand All @@ -21,10 +22,13 @@
import org.springframework.test.context.junit.jupiter.SpringExtension;

import java.math.BigDecimal;
import java.time.Instant;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.when;

Expand Down Expand Up @@ -83,4 +87,96 @@ public void testSearch() {
assertEquals("Test Book", elasticResponse.getName());
assertEquals(4.0f, elasticResponse.getAverageScore()); // 검증
}
@Test
@DisplayName("엘라스틱서치 검색 기능 테스트-null인경우")
public void testSearchNull() {
String query = "test";
String searchType = "book_name";
PageRequest pageable = PageRequest.of(0, 10);

ElasticService elasticService = mock(ElasticService.class);
Mockito.lenient().when(elasticService.search(query, searchType, pageable)).thenReturn(null);

ElasticDocument document = mock(ElasticDocument.class);
Mockito.lenient().when(document.getCoverImageUrl()).thenReturn(null);
Mockito.lenient().when(document.getName()).thenReturn("Test Book");
Mockito.lenient().when(document.getAverageScore()).thenReturn(4.0f);

List<SearchHit<ElasticDocument>> searchHitList = new ArrayList<>();
searchHitList.add(new SearchHit<>(null, null, null, 1.0f, null, null, document));
SearchHits<ElasticDocument> searchHits = new SearchHitsImpl<>(1, null, 1.0f, null, searchHitList, null, null);
Mockito.lenient().when(elasticRepository.findByBookName(query, pageable)).thenReturn(searchHits);

// When
PageResponse<ElasticResponse> response = elasticService.search(query, searchType, pageable);

// Then
if (response != null) {
ElasticResponse elasticResponse = response.getContent().get(0);
assertEquals("Test Book", elasticResponse.getName());
assertEquals(4.0f, elasticResponse.getAverageScore());
}
}

@Test
@DisplayName("엘라스틱서치 검색 결과 응답 객체 생성 테스트")
public void testBuildElasticSearchResultResponse() {
// Given
ElasticDocument document = new ElasticDocument();
document.setBookId(BigDecimal.ONE);
document.setName("Test Book");
document.setPrice(BigDecimal.valueOf(100));
document.setDiscount(BigDecimal.TEN);
document.setDiscountPrice(BigDecimal.valueOf(90));
document.setPublished("2020-01-01T00:00:00Z");
document.setAverageScore(4.0f);
document.setLikeCount(BigDecimal.valueOf(100));
document.setPublisher("Test Publisher");
document.setCoverImageUrl("http://example.com/image.jpg");
document.setAuthorName("Author Name");
document.setAuthorRole("Author Role");

float score = 1.0f;
long searchBookCount = 1;

ElasticService elasticService = mock(ElasticService.class);

// When
when(elasticService.buildElasticSearchResultResponse(document, score, searchBookCount))
.thenReturn(new ElasticResponse(
BigDecimal.ONE,
"Test Book",
BigDecimal.valueOf(100),
BigDecimal.TEN,
BigDecimal.valueOf(90),
Instant.parse("2020-01-01T00:00:00Z").atZone(ZoneId.systemDefault()).toLocalDate(),
4.0f,
BigDecimal.valueOf(100),
"Test Publisher",
"http://example.com/image.jpg",
"Author Name",
"Author Role",
1.0f,
BigDecimal.TEN,
1
));

// Then
ElasticResponse elasticResponse = elasticService.buildElasticSearchResultResponse(document, score, searchBookCount);
assertEquals("Test Book", elasticResponse.getName());
assertEquals(BigDecimal.ONE, elasticResponse.getBookId());
assertEquals(BigDecimal.valueOf(100), elasticResponse.getPrice());
assertEquals(BigDecimal.TEN, elasticResponse.getDiscountRate());
assertEquals(BigDecimal.valueOf(90), elasticResponse.getDiscountedPrice());
assertEquals(Instant.parse("2020-01-01T00:00:00Z").atZone(ZoneId.systemDefault()).toLocalDate(), elasticResponse.getPublished());
assertEquals(4.0f, elasticResponse.getAverageScore());
assertEquals(BigDecimal.valueOf(100), elasticResponse.getLikeCount());
assertEquals("Test Publisher", elasticResponse.getPublisher());
assertEquals("http://example.com/image.jpg", elasticResponse.getCoverImageUrl());
assertEquals("Author Name", elasticResponse.getAuthorName());
assertEquals("Author Role", elasticResponse.getAuthorRole());
assertEquals(1.0f, elasticResponse.getScore());
assertEquals(BigDecimal.TEN, elasticResponse.getCategoryId());
assertEquals(1, elasticResponse.getCount());
}
}

0 comments on commit a2c49fa

Please sign in to comment.