Skip to content

Commit

Permalink
주변 수거함 목록 조회 기능에 대한 단위 테스트 작성 (#118)
Browse files Browse the repository at this point in the history
* test: GeoPoint 단위 테스트 작성

* test: Tags 단위 테스트 작성

* test: 주변 수거함 목록 조회 기능 테스트 코드 개선
  • Loading branch information
Hanjaemo authored Jun 18, 2024
1 parent 98dfcca commit 0281799
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 50 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package contest.collectingbox.module.location.domain;

import contest.collectingbox.global.exception.ErrorCode;
import lombok.Getter;

@Getter
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
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.Tag;
import contest.collectingbox.module.collectingbox.domain.Tags;
import contest.collectingbox.module.collectingbox.domain.repository.CollectingBoxRepository;
import contest.collectingbox.module.collectingbox.dto.CollectingBoxDetailResponse;
import contest.collectingbox.module.collectingbox.dto.CollectingBoxResponse;
import contest.collectingbox.module.location.domain.GeoPoint;
import contest.collectingbox.module.location.domain.Location;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.locationtech.jts.geom.Point;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
Expand All @@ -25,7 +19,7 @@
import java.util.List;

import static contest.collectingbox.global.exception.ErrorCode.NOT_FOUND_COLLECTING_BOX;
import static contest.collectingbox.module.collectingbox.domain.Tag.*;
import static contest.collectingbox.module.collectingbox.domain.Tag.CLOTHES;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;
import static org.mockito.Mockito.when;
Expand All @@ -36,53 +30,12 @@ class CollectingBoxServiceTest {
@Value("${collecting-box.search.radius.meter}")
private int radius;

private GeoPoint center;

@InjectMocks
private CollectingBoxService collectingBoxService;

@Mock
private CollectingBoxRepository collectingBoxRepository;

@BeforeEach
void setUp() {
center = new GeoPoint(126.901185398046, 37.4888953606578);
}

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

CollectingBox box = CollectingBox.builder()
.id(1L)
.location(Location.builder().point(GeometryUtil.toPoint(126.902174727201, 37.4871190365551)).build())
.tag(CLOTHES)
.build();

// when
when(collectingBoxRepository.findAllWithinArea(center, radius, tags))
.thenReturn(List.of(
new CollectingBoxResponse(box.getId(), center.getLongitude(), center.getLatitude(), CLOTHES)));

List<CollectingBoxResponse> result =
collectingBoxService.findCollectingBoxesWithinArea(center, tags);

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

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

@Test
@DisplayName("수거함 id로 수거함 상세 정보 조회")
void findBoxDetail_Success_ById() {
Expand Down Expand Up @@ -120,4 +73,34 @@ void findBoxDetail_Fail_WhenBoxIdNotFound() {
.hasMessageContaining(NOT_FOUND_COLLECTING_BOX.getMessage());
}

@Nested
class FindCollectingBoxesWithinArea {

@Test
@DisplayName("위도와 경도를 기준으로 특정 반경에 위치한 수거함 목록 조회에 성공한다.")
void success_byWithinArea() {
// given
GeoPoint requestCenterPoint = new GeoPoint(126.901185398046, 37.4888953606578);
Tags requestTags = new Tags(List.of(CLOTHES));

CollectingBoxResponse response = CollectingBoxResponse.builder()
.id(1L)
.longitude(126.902174727201)
.latitude(37.4871190365551)
.tag(CLOTHES)
.build();

when(collectingBoxRepository.findAllWithinArea(requestCenterPoint, radius, requestTags))
.thenReturn(List.of(response));

// when
List<CollectingBoxResponse> result =
collectingBoxService.findCollectingBoxesWithinArea(requestCenterPoint, requestTags);

// then
assertThat(result).containsExactly(response);
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package contest.collectingbox.module.collectingbox.domain;

import contest.collectingbox.global.exception.CollectingBoxException;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.util.List;

import static org.assertj.core.api.Assertions.assertThatThrownBy;

class TagsTest {

@Test
@DisplayName("태그가 선택되지 않은 경우 실패한다.")
void fail_byNull() {
// when, then
assertThatThrownBy(() -> new Tags(List.of()))
.isInstanceOf(CollectingBoxException.class);
}

@Test
@DisplayName("tags를 수정하려고 시도하면 실패한다.")
void fail_byTryModifyTags() {
// given
Tags tags = new Tags(List.of(Tag.CLOTHES));

// when, then
assertThatThrownBy(() -> tags.getTags().add(Tag.LAMP_BATTERY))
.isInstanceOf(UnsupportedOperationException.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package contest.collectingbox.module.location.domain;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;

class GeoPointTest {

@Test
@DisplayName("경도가 -180보다 작은 경우 실패한다.")
void fail_byLongitudeLessThanMin() {
// when, then
assertThatThrownBy(() -> new GeoPoint(-202.902174727201, 37.4871190365551))
.isInstanceOf(IllegalArgumentException.class);
}

@Test
@DisplayName("경도가 180보다 큰 경우 실패한다.")
void fail_byLongitudeGreaterThanMax() {
// when, then
assertThatThrownBy(() -> new GeoPoint(202.902174727201, 37.4871190365551))
.isInstanceOf(IllegalArgumentException.class);
}

@Test
@DisplayName("위도가 -90보다 작은 경우 실패한다.")
void fail_byLatitudeLessThanMin() {
// when, then
assertThatThrownBy(() -> new GeoPoint(126.902174727201, -95.4871190365551))
.isInstanceOf(IllegalArgumentException.class);
}

@Test
@DisplayName("위도가 90보다 큰 경우 실패한다.")
void fail_byLatitudeGreaterThanMax() {
// when, then
assertThatThrownBy(() -> new GeoPoint(126.902174727201, 95.4871190365551))
.isInstanceOf(IllegalArgumentException.class);
}
}

0 comments on commit 0281799

Please sign in to comment.