From f66a26731106731c91f6bb48d766cb87522a8854 Mon Sep 17 00:00:00 2001 From: vectorch Date: Sun, 31 Dec 2023 12:47:39 +0900 Subject: [PATCH] =?UTF-8?q?test:=20=EA=B4=80=EA=B3=84=20=EB=8F=84=EB=A9=94?= =?UTF-8?q?=EC=9D=B8=20=EC=84=9C=EB=B9=84=EC=8A=A4=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=EB=A5=BC=20=EC=9E=91=EC=84=B1=ED=95=9C=EB=8B=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../application/DeleteFollowServiceTest.java | 45 +++++++++++++++++ .../DeleteFollowerServiceTest.java | 45 +++++++++++++++++ .../application/GetFollowersServiceTest.java | 40 +++++++++++++++ .../application/GetFollowingsServiceTest.java | 40 +++++++++++++++ .../application/SaveFollowServiceTest.java | 49 +++++++++++++++++++ 5 files changed, 219 insertions(+) create mode 100644 src/test/java/daybyquest/relation/application/DeleteFollowServiceTest.java create mode 100644 src/test/java/daybyquest/relation/application/DeleteFollowerServiceTest.java create mode 100644 src/test/java/daybyquest/relation/application/GetFollowersServiceTest.java create mode 100644 src/test/java/daybyquest/relation/application/GetFollowingsServiceTest.java create mode 100644 src/test/java/daybyquest/relation/application/SaveFollowServiceTest.java diff --git a/src/test/java/daybyquest/relation/application/DeleteFollowServiceTest.java b/src/test/java/daybyquest/relation/application/DeleteFollowServiceTest.java new file mode 100644 index 0000000..156ed36 --- /dev/null +++ b/src/test/java/daybyquest/relation/application/DeleteFollowServiceTest.java @@ -0,0 +1,45 @@ +package daybyquest.relation.application; + +import static daybyquest.global.error.ExceptionCode.NOT_FOLLOWING_USER; +import static daybyquest.support.fixture.UserFixtures.BOB; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import daybyquest.global.error.exception.InvalidDomainException; +import daybyquest.relation.domain.Follow; +import daybyquest.support.test.ServiceTest; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; + +public class DeleteFollowServiceTest extends ServiceTest { + + @Autowired + private DeleteFollowService deleteFollowService; + + @Test + void 팔로우를_취소한다() { + // given + final Long aliceId = ALICE를_저장한다(); + final Long bobId = BOB을_저장한다(); + follows.save(new Follow(aliceId, bobId)); + + // when + deleteFollowService.invoke(aliceId, BOB.username); + + // then + assertThatThrownBy(() -> follows.getByUserIdAndTargetId(aliceId, bobId)) + .isInstanceOf(InvalidDomainException.class) + .hasMessageContaining(NOT_FOLLOWING_USER.getMessage()); + } + + @Test + void 팔로우하지_않은_대상은_취소_할_수_없다() { + // given + final Long aliceId = ALICE를_저장한다(); + BOB을_저장한다(); + + // when & then + assertThatThrownBy(() -> deleteFollowService.invoke(aliceId, BOB.username)) + .isInstanceOf(InvalidDomainException.class) + .hasMessageContaining(NOT_FOLLOWING_USER.getMessage()); + } +} diff --git a/src/test/java/daybyquest/relation/application/DeleteFollowerServiceTest.java b/src/test/java/daybyquest/relation/application/DeleteFollowerServiceTest.java new file mode 100644 index 0000000..3a36a02 --- /dev/null +++ b/src/test/java/daybyquest/relation/application/DeleteFollowerServiceTest.java @@ -0,0 +1,45 @@ +package daybyquest.relation.application; + +import static daybyquest.global.error.ExceptionCode.NOT_FOLLOWING_USER; +import static daybyquest.support.fixture.UserFixtures.ALICE; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import daybyquest.global.error.exception.InvalidDomainException; +import daybyquest.relation.domain.Follow; +import daybyquest.support.test.ServiceTest; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; + +public class DeleteFollowerServiceTest extends ServiceTest { + + @Autowired + private DeleteFollowerService deleteFollowerService; + + @Test + void 팔로워를_삭제한다() { + // given + final Long aliceId = ALICE를_저장한다(); + final Long bobId = BOB을_저장한다(); + follows.save(new Follow(aliceId, bobId)); + + // when + deleteFollowerService.invoke(bobId, ALICE.username); + + // then + assertThatThrownBy(() -> follows.getByUserIdAndTargetId(aliceId, bobId)) + .isInstanceOf(InvalidDomainException.class) + .hasMessageContaining(NOT_FOLLOWING_USER.getMessage()); + } + + @Test + void 팔로워가_아닌_대상은_삭제_할_수_없다() { + // given + ALICE를_저장한다(); + final Long bobId = BOB을_저장한다(); + + // when & then + assertThatThrownBy(() -> deleteFollowerService.invoke(bobId, ALICE.username)) + .isInstanceOf(InvalidDomainException.class) + .hasMessageContaining(NOT_FOLLOWING_USER.getMessage()); + } +} diff --git a/src/test/java/daybyquest/relation/application/GetFollowersServiceTest.java b/src/test/java/daybyquest/relation/application/GetFollowersServiceTest.java new file mode 100644 index 0000000..2ee2a89 --- /dev/null +++ b/src/test/java/daybyquest/relation/application/GetFollowersServiceTest.java @@ -0,0 +1,40 @@ +package daybyquest.relation.application; + +import static daybyquest.support.fixture.UserFixtures.BOB; +import static daybyquest.support.fixture.UserFixtures.CHARLIE; +import static org.assertj.core.api.Assertions.assertThat; + +import daybyquest.relation.domain.Follow; +import daybyquest.support.test.ServiceTest; +import daybyquest.user.dto.response.PageProfilesResponse; +import daybyquest.user.dto.response.ProfileResponse; +import java.util.List; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; + +public class GetFollowersServiceTest extends ServiceTest { + + @Autowired + private GetFollowersService getFollowersService; + + @Test + void 팔로워_목록을_조회한다() { + // given + final Long aliceId = ALICE를_저장한다(); + final Long bobId = BOB을_저장한다(); + final Long charlieId = CHARLIE를_저장한다(); + final Long davidId = DAVID를_저장한다(); + follows.save(new Follow(bobId, aliceId)); + follows.save(new Follow(charlieId, aliceId)); + follows.save(new Follow(aliceId, davidId)); + + final List expected = List.of(BOB.username, CHARLIE.username); + + // when + final PageProfilesResponse response = getFollowersService.invoke(aliceId, 페이지()); + final List actual = response.users().stream().map(ProfileResponse::username).toList(); + + // then + assertThat(actual).containsExactlyInAnyOrderElementsOf(expected); + } +} diff --git a/src/test/java/daybyquest/relation/application/GetFollowingsServiceTest.java b/src/test/java/daybyquest/relation/application/GetFollowingsServiceTest.java new file mode 100644 index 0000000..1341919 --- /dev/null +++ b/src/test/java/daybyquest/relation/application/GetFollowingsServiceTest.java @@ -0,0 +1,40 @@ +package daybyquest.relation.application; + +import static daybyquest.support.fixture.UserFixtures.BOB; +import static daybyquest.support.fixture.UserFixtures.CHARLIE; +import static org.assertj.core.api.Assertions.assertThat; + +import daybyquest.relation.domain.Follow; +import daybyquest.support.test.ServiceTest; +import daybyquest.user.dto.response.PageProfilesResponse; +import daybyquest.user.dto.response.ProfileResponse; +import java.util.List; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; + +public class GetFollowingsServiceTest extends ServiceTest { + + @Autowired + private GetFollowingsService getFollowingsService; + + @Test + void 팔로잉_목록을_조회한다() { + // given + final Long aliceId = ALICE를_저장한다(); + final Long bobId = BOB을_저장한다(); + final Long charlieId = CHARLIE를_저장한다(); + final Long davidId = DAVID를_저장한다(); + follows.save(new Follow(aliceId, bobId)); + follows.save(new Follow(aliceId, charlieId)); + follows.save(new Follow(davidId, aliceId)); + + final List expected = List.of(BOB.username, CHARLIE.username); + + // when + final PageProfilesResponse response = getFollowingsService.invoke(aliceId, 페이지()); + final List actual = response.users().stream().map(ProfileResponse::username).toList(); + + // then + assertThat(actual).containsExactlyInAnyOrderElementsOf(expected); + } +} diff --git a/src/test/java/daybyquest/relation/application/SaveFollowServiceTest.java b/src/test/java/daybyquest/relation/application/SaveFollowServiceTest.java new file mode 100644 index 0000000..8d7564e --- /dev/null +++ b/src/test/java/daybyquest/relation/application/SaveFollowServiceTest.java @@ -0,0 +1,49 @@ +package daybyquest.relation.application; + +import static daybyquest.global.error.ExceptionCode.ALREADY_FOLLOWING_USER; +import static daybyquest.support.fixture.UserFixtures.BOB; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.jupiter.api.Assertions.assertAll; + +import daybyquest.global.error.exception.InvalidDomainException; +import daybyquest.relation.domain.Follow; +import daybyquest.support.test.ServiceTest; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; + +public class SaveFollowServiceTest extends ServiceTest { + + @Autowired + private SaveFollowService saveFollowService; + + @Test + void 팔로우를_저장한다() { + // given + final Long aliceId = ALICE를_저장한다(); + final Long bobId = BOB을_저장한다(); + + // when + saveFollowService.invoke(aliceId, BOB.username); + + // then + final Follow follow = follows.getByUserIdAndTargetId(aliceId, bobId); + assertAll(() -> { + assertThat(follow.getUserId()).isEqualTo(aliceId); + assertThat(follow.getTargetId()).isEqualTo(bobId); + }); + } + + @Test + void 이미_팔로우한_대상은_팔로우_할_수_없다() { + // given + final Long aliceId = ALICE를_저장한다(); + final Long bobId = BOB을_저장한다(); + follows.save(new Follow(aliceId, bobId)); + + // when & then + assertThatThrownBy(() -> saveFollowService.invoke(aliceId, BOB.username)) + .isInstanceOf(InvalidDomainException.class) + .hasMessageContaining(ALREADY_FOLLOWING_USER.getMessage()); + } +}