Skip to content

Commit

Permalink
test: 수거함 목록 조회 기능 단위 테스트 작성 (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hanjaemo authored Apr 28, 2024
1 parent add5f32 commit d259c3a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package contest.collectingbox.module.collectingbox.application;

import static contest.collectingbox.global.exception.ErrorCode.*;

import contest.collectingbox.global.exception.CollectingBoxException;
import contest.collectingbox.global.exception.ErrorCode;
import contest.collectingbox.global.utils.GeometryUtil;
Expand All @@ -20,6 +18,8 @@
import java.util.List;
import java.util.stream.Collectors;

import static contest.collectingbox.global.exception.ErrorCode.NOT_FOUND_COLLECTING_BOX;

@Service
@RequiredArgsConstructor
public class CollectingBoxService {
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/config
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package contest.collectingbox.module.collectingbox.application;

import contest.collectingbox.global.exception.CollectingBoxException;
import contest.collectingbox.global.exception.ErrorCode;
import contest.collectingbox.global.utils.GeometryUtil;
import contest.collectingbox.module.collectingbox.domain.CollectingBox;
import contest.collectingbox.module.collectingbox.domain.CollectingBoxRepository;
import contest.collectingbox.module.collectingbox.domain.Tag;
import contest.collectingbox.module.collectingbox.dto.CollectingBoxDetailResponse;
import contest.collectingbox.module.collectingbox.dto.CollectingBoxResponse;
import contest.collectingbox.module.location.domain.Location;
import java.util.Optional;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
Expand All @@ -18,25 +20,25 @@
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.beans.factory.annotation.Value;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

import static contest.collectingbox.module.collectingbox.domain.Tag.CLOTHES;
import static org.assertj.core.api.Assertions.*;
import static contest.collectingbox.module.collectingbox.domain.Tag.*;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
class CollectingBoxServiceTest {

private final double latitude = 37.4888953606578;
private final double longitude = 126.901185398046;
private final double LATITUDE = 37.4888953606578;
private final double LONGITUDE = 126.901185398046;

private Point center;

@Value("${collecting-box.search.radius.meter}")
private int radius;


@InjectMocks
private CollectingBoxService collectingBoxService;

Expand All @@ -45,14 +47,14 @@ class CollectingBoxServiceTest {

@BeforeEach
void setUp() {
center = GeometryUtil.toPoint(longitude, latitude);
center = GeometryUtil.toPoint(LONGITUDE, LATITUDE);
}

@Test
@DisplayName("위도와 경도를 기준으로 특정 반경에 위치한 수거함 목록 조회 성공")
void findCollectingBoxesWithinArea_Success_withinArea() {
// given
List<Tag> tags = List.of(CLOTHES);
List<Tag> tags = List.of(CLOTHES, LAMP, BATTERY, MEDICINE, TRASH);

CollectingBox box = CollectingBox.builder()
.id(1L)
Expand All @@ -61,15 +63,24 @@ void findCollectingBoxesWithinArea_Success_withinArea() {
.build();

// when
when(collectingBoxRepository.findAllWithinArea(center, radius, tags)).thenReturn(Arrays.asList(box));
when(collectingBoxRepository.findAllWithinArea(center, radius, tags)).thenReturn(Collections.singletonList(box));

List<CollectingBoxResponse> result =
collectingBoxService.findCollectingBoxesWithinArea(latitude, longitude, tags);
collectingBoxService.findCollectingBoxesWithinArea(LATITUDE, LONGITUDE, tags);

// then
assertThat(result.get(0).getId()).isEqualTo(box.getId());
}

@Test
@DisplayName("요청 파라미터로 넘어온 태그가 비어 있는 경우 예외 발생")
void findCollectingBoxesWithinArea_Fail_ByTagIsEmpty() {
// when, then
Assertions.assertThatThrownBy(() -> collectingBoxService.findCollectingBoxesWithinArea(LATITUDE, LONGITUDE, List.of()))
.isInstanceOf(CollectingBoxException.class)
.hasMessageContaining(ErrorCode.NOT_SELECTED_TAG.getMessage());
}

@Test
@DisplayName("수거함 id로 수거함 상세 정보 조회")
void findBoxDetail_Success_ById() {
Expand Down

0 comments on commit d259c3a

Please sign in to comment.