From a4c47ef6b2916fe615bc9ce6b99193adaabbca9b Mon Sep 17 00:00:00 2001 From: kimdozzi Date: Wed, 10 Jul 2024 10:17:29 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20topic=20service=20->=20facade=20?= =?UTF-8?q?=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=BD=94=EB=93=9C=20=EB=A6=AC?= =?UTF-8?q?=ED=8E=99=ED=86=A0=EB=A7=81=20-=20describe=20context=20it=20?= =?UTF-8?q?=ED=8C=A8=ED=84=B4=20=EC=A0=81=EC=9A=A9=20-=20topicServiceTest?= =?UTF-8?q?=20->=20topicFacadeTest=EB=A1=9C=20=EB=B3=80=EA=B2=BD=20-=20fac?= =?UTF-8?q?ade=20pattern=EC=97=90=20=EB=A7=9E=EA=B2=8C=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../topic/repository/TopicRepositoryTest.java | 6 +- .../admin/topic/service/TopicFacadeTest.java | 135 ++++++++++++++++++ .../admin/topic/service/TopicServiceTest.java | 113 --------------- 3 files changed, 138 insertions(+), 116 deletions(-) create mode 100644 src/test/java/com/genius/gitget/admin/topic/service/TopicFacadeTest.java delete mode 100644 src/test/java/com/genius/gitget/admin/topic/service/TopicServiceTest.java diff --git a/src/test/java/com/genius/gitget/admin/topic/repository/TopicRepositoryTest.java b/src/test/java/com/genius/gitget/admin/topic/repository/TopicRepositoryTest.java index 9594c5de..4babf210 100644 --- a/src/test/java/com/genius/gitget/admin/topic/repository/TopicRepositoryTest.java +++ b/src/test/java/com/genius/gitget/admin/topic/repository/TopicRepositoryTest.java @@ -48,7 +48,7 @@ public void setup() { } @Nested - @DisplayName("save 메소드는") + @DisplayName("save 메서드는") class Describe_save { @BeforeEach @@ -75,7 +75,7 @@ public void it_returns_the_saved_obj_if_saving_an_obj_succeeds() { @Nested - @DisplayName("search 메소드는") + @DisplayName("search 메서드는") class Describe_search { @Nested @@ -129,7 +129,7 @@ public void init() { @Test @DisplayName("생성된 인스턴스가 존재하면 description만 수정할 수 있고, 없다면 모든 항목을 수정할 수 있다.") - public void it_returns_2XX_if_the_obj_is_updated_successfully() { + public void it_returns_updated_obj() { Topic topic = topicRepository.findById(topicA.getId()).orElse(null); diff --git a/src/test/java/com/genius/gitget/admin/topic/service/TopicFacadeTest.java b/src/test/java/com/genius/gitget/admin/topic/service/TopicFacadeTest.java new file mode 100644 index 00000000..46419b25 --- /dev/null +++ b/src/test/java/com/genius/gitget/admin/topic/service/TopicFacadeTest.java @@ -0,0 +1,135 @@ +package com.genius.gitget.admin.topic.service; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.genius.gitget.global.util.exception.BusinessException; +import com.genius.gitget.topic.domain.Topic; +import com.genius.gitget.topic.dto.TopicCreateRequest; +import com.genius.gitget.topic.dto.TopicDetailResponse; +import com.genius.gitget.topic.dto.TopicUpdateRequest; +import com.genius.gitget.topic.serviceFacade.TopicFacade; +import jakarta.transaction.Transactional; +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.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.annotation.Rollback; + +@SpringBootTest +@Transactional +@Rollback +public class TopicFacadeTest { + Topic topicA, topicB; + String fileType; + + @Autowired + TopicFacade topicFacade; + + @BeforeEach + public void setup() { + topicA = Topic.builder() + .title("1일 1알고리즘") + .description("하루에 한 문제씩 문제를 해결합니다.") + .tags("BE, FE, CS") + .pointPerPerson(100) + .build(); + + topicB = Topic.builder() + .title("1일 2알고리즘") + .description("하루에 한 문제씩 문제를 해결합니다.") + .tags("BE, FE, CS") + .pointPerPerson(300) + .build(); + + fileType = "topic"; + } + + private TopicCreateRequest getTopicCreateRequest() { + return TopicCreateRequest.builder() + .title(topicA.getTitle()) + .description(topicA.getDescription()) + .tags(topicA.getTags()) + .pointPerPerson(topicA.getPointPerPerson()) + .notice(topicA.getNotice()) + .build(); + } + + @Nested + @DisplayName("토픽 생성 메서드는") + class Describe_topic_create { + + @Nested + @DisplayName("topicCreateRequestDto가 들어오면") + class Context_with_a_topicCreateRequestDto { + + @Test + @DisplayName("토픽을 생성한다.") + public void it_returns_2XX_if_the_topic_was_created_successfully() { + TopicCreateRequest topicCreateRequest = getTopicCreateRequest(); + + Long savedTopicId = topicFacade.create(topicCreateRequest); + + TopicDetailResponse topicById = topicFacade.findOne(savedTopicId); + + Assertions.assertThat(topicById.title()).isEqualTo(topicCreateRequest.title()); + } + } + } + + @Nested + @DisplayName("토픽 수정 메서드는") + class Describe_topic_update { + + @Nested + @DisplayName("TopicUpdateRequestDto가 들어오면") + class Context_with_a_TopicUpdateRequestDto { + + @Test + @DisplayName("토픽 내용을 수정한다.") + public void it_returns_2XX_if_the_topic_is_modified() { + TopicCreateRequest topicCreateRequest = getTopicCreateRequest(); + Long savedTopicId = topicFacade.create(topicCreateRequest); + + TopicUpdateRequest topicUpdateRequest = TopicUpdateRequest.builder() + .title("1일 5커밋") + .description(topicA.getDescription()) + .tags(topicA.getTags()) + .pointPerPerson(topicA.getPointPerPerson()) + .notice(topicA.getNotice()).build(); + + topicFacade.update(savedTopicId, topicUpdateRequest); + + TopicDetailResponse findTopic = topicFacade.findOne(savedTopicId); + Assertions.assertThat(findTopic.title()).isEqualTo("1일 5커밋"); + } + } + } + + @Nested + @DisplayName("토픽 삭제 메서드는") + class Describe_topic_delete { + + @Nested + @DisplayName("삭제할 토픽 Id가 주어질 때") + class Context_with_a_TopicUpdateRequestDto { + + @Test + @DisplayName("해당 토픽을 삭제한다.") + public void it_returns_2XX_if_the_topic_is_successfully_deleted() throws Exception { + TopicCreateRequest topicCreateRequest = getTopicCreateRequest(); + Long savedTopicId = topicFacade.create(topicCreateRequest); + + topicFacade.delete(savedTopicId); + + try { + topicFacade.findOne(savedTopicId); + } catch (BusinessException e) { + assertEquals("해당 토픽을 찾을 수 없습니다.", e.getMessage()); + } + } + } + } +} diff --git a/src/test/java/com/genius/gitget/admin/topic/service/TopicServiceTest.java b/src/test/java/com/genius/gitget/admin/topic/service/TopicServiceTest.java deleted file mode 100644 index af3f7233..00000000 --- a/src/test/java/com/genius/gitget/admin/topic/service/TopicServiceTest.java +++ /dev/null @@ -1,113 +0,0 @@ -package com.genius.gitget.admin.topic.service; - -import com.genius.gitget.global.util.exception.BusinessException; -import com.genius.gitget.topic.domain.Topic; -import com.genius.gitget.topic.dto.TopicCreateRequest; -import com.genius.gitget.topic.dto.TopicDetailResponse; -import com.genius.gitget.topic.dto.TopicUpdateRequest; -import com.genius.gitget.topic.repository.TopicRepository; -import com.genius.gitget.topic.serviceFacade.TopicFacade; -import jakarta.transaction.Transactional; -import java.util.Optional; -import org.assertj.core.api.Assertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.annotation.Rollback; - -@SpringBootTest -@Transactional -@Rollback -public class TopicServiceTest { - Topic topic, topicA; - String fileType; - @Autowired - TopicFacade topicFacade; - - @Autowired - TopicRepository topicRepository; - - - @BeforeEach - public void setup() { - topic = Topic.builder() - .title("1일 1알고리즘") - .description("하루에 한 문제씩 문제를 해결합니다.") - .tags("BE, FE, CS") - .pointPerPerson(100) - .build(); - - topicA = Topic.builder() - .title("1일 2알고리즘") - .description("하루에 한 문제씩 문제를 해결합니다.") - .tags("BE, FE, CS") - .pointPerPerson(300) - .build(); - - fileType = "topic"; - } - - @Test - public void 토픽_생성() throws Exception { - //given - TopicCreateRequest topicCreateRequest = getTopicCreateRequest(); - - Long savedTopicId = topicFacade.create(topicCreateRequest); - - //when - TopicDetailResponse topicById = topicFacade.findOne(savedTopicId); - - //then - Assertions.assertThat(topicById.title()).isEqualTo(topicCreateRequest.title()); - } - - @Test - public void 토픽_수정() throws Exception { - //given - TopicCreateRequest topicCreateRequest = getTopicCreateRequest(); - Long savedTopicId = topicFacade.create(topicCreateRequest); - - //when - TopicUpdateRequest topicUpdateRequest = TopicUpdateRequest.builder() - .title("1일 5커밋") - .description(topic.getDescription()) - .tags(topic.getTags()) - .pointPerPerson(topic.getPointPerPerson()) - .notice(topic.getNotice()).build(); - - topicFacade.update(savedTopicId, topicUpdateRequest); - - //then - Optional findTopic = topicRepository.findById(savedTopicId); - Topic findUpdatedTopic = findTopic.get(); - Assertions.assertThat(findUpdatedTopic.getTitle()).isEqualTo("1일 5커밋"); - } - - @Test - public void 토픽_삭제() throws Exception { - //given - TopicCreateRequest topicCreateRequest = getTopicCreateRequest(); - Long savedTopicId = topicFacade.create(topicCreateRequest); - - //when - topicFacade.delete(savedTopicId); - - //then - try { - topicFacade.findOne(savedTopicId); - } catch (BusinessException e) { - org.junit.jupiter.api.Assertions.assertEquals("해당 토픽을 찾을 수 없습니다.", e.getMessage()); - } - } - - private TopicCreateRequest getTopicCreateRequest() { - return TopicCreateRequest.builder() - .title(topic.getTitle()) - .description(topic.getDescription()) - .tags(topic.getTags()) - .pointPerPerson(topic.getPointPerPerson()) - .notice(topic.getNotice()) - .build(); - } -}