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 2ef790cf..9594c5de 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 @@ -1,13 +1,16 @@ package com.genius.gitget.admin.topic.repository; +import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertEquals; import com.genius.gitget.topic.domain.Topic; import com.genius.gitget.topic.repository.TopicRepository; import jakarta.transaction.Transactional; import java.util.Optional; -import org.assertj.core.api.Assertions; +import org.junit.jupiter.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; @@ -15,24 +18,28 @@ import org.springframework.data.domain.PageRequest; import org.springframework.test.annotation.Rollback; +//@ExtendWith(SpringExtension.class) +//@DataJpaTest @SpringBootTest @Transactional @Rollback +@DisplayName("TopicRepository") public class TopicRepositoryTest { - public Topic topic, topicA; + Topic topicA, topicB; + @Autowired private TopicRepository topicRepository; @BeforeEach public void setup() { - topic = Topic.builder() + topicA = Topic.builder() .title("1일 1알고리즘") .description("하루에 한 문제씩 문제를 해결합니다.") .tags("BE, FE, CS") .pointPerPerson(100) .build(); - topicA = Topic.builder() + topicB = Topic.builder() .title("1일 2알고리즘") .description("하루에 한 문제씩 문제를 해결합니다.") .tags("BE, FE, CS") @@ -40,57 +47,148 @@ public void setup() { .build(); } - @Test - public void 토픽_저장() { - Topic savedTopic = topicRepository.save(topic); - assertEquals(topic.getId(), savedTopic.getId()); - assertEquals(topic.getTitle(), savedTopic.getTitle()); - assertEquals(topic.getDescription(), savedTopic.getDescription()); - } + @Nested + @DisplayName("save 메소드는") + class Describe_save { - @Test - public void 토픽_수정() { - Topic savedTopic = topicRepository.save(topic); - if (!topic.getInstanceList().isEmpty()) { - savedTopic.updateExistInstance("(수정) 하루에 두 문제씩 문제를 해결합니다."); - } else { - savedTopic.updateNotExistInstance("1일 1커밋", "하루에 1커밋 하기", "CS", "유의사항", 300); + @BeforeEach + public void init() { + topicRepository.deleteAll(); } - assertEquals(topic.getId(), savedTopic.getId()); - assertEquals(topic.getTitle(), savedTopic.getTitle()); - assertEquals(topic.getDescription(), savedTopic.getDescription()); - } + @Nested + @DisplayName("토픽 객체가 주어질 때") + class Context_with_a_topic { + + @Test +// it_returns_4XX_if_saving_the_obj_fails + @DisplayName("객체 저장에 성공하면 저장된 객체를 반환합니다.") + public void it_returns_the_saved_obj_if_saving_an_obj_succeeds() { + Topic savedTopic = topicRepository.save(topicA); - @Test - public void 토픽_삭제() { - Topic savedTopic = topicRepository.save(topic); - topicRepository.delete(savedTopic); - Optional byId = topicRepository.findById(1L); - Assertions.assertThat(byId).isNotPresent(); + assertEquals(topicA.getId(), savedTopic.getId()); + assertEquals(topicA.getTitle(), savedTopic.getTitle()); + assertEquals(topicA.getDescription(), savedTopic.getDescription()); + } + } } - @Test - public void 토픽_리스트_조회() { - for (int i = 1; i <= 10; i++) { - topicRepository.save( - Topic.builder().title("user" + i + "L").build() - ); + @Nested + @DisplayName("search 메소드는") + class Describe_search { + + @Nested + @DisplayName("조회 조건에 따라") + class Context_with_a_topic { + + @BeforeEach + public void prepare() { + topicRepository.save(topicA); + topicRepository.save(topicB); + } + + @Test + @DisplayName("토픽 전체를 반환합니다.") + public void it_returns_topic_obj_list() { + Page topics = topicRepository.findAllById(PageRequest.of(0, 5)); + int topicCount = 0; + + for (Topic topic : topics) { + if (topic != null) { + topicCount++; + } + } + + assertThat(topicCount).isEqualTo(2); + } + + @Test + @DisplayName("특정 토픽을 반환합니다.") + public void it_returns_topic_obj() { + Optional topic = topicRepository.findById(topicA.getId()); + + assertThat(topic.get().getTitle()).isEqualTo("1일 1알고리즘"); + } } - Page allById = topicRepository.findAllById(PageRequest.of(0, 5)); - Assertions.assertThat(allById.getSize()).isEqualTo(5); + } + + + @Nested + @DisplayName("update 메서드는") + class Describe_update { + + @Nested + @DisplayName("토픽 정보를 수정하려고 할 때") + class Context_with_a_topic { + + @BeforeEach + public void init() { + topicRepository.save(topicA); + } + + @Test + @DisplayName("생성된 인스턴스가 존재하면 description만 수정할 수 있고, 없다면 모든 항목을 수정할 수 있다.") + public void it_returns_2XX_if_the_obj_is_updated_successfully() { + + Topic topic = topicRepository.findById(topicA.getId()).orElse(null); - for (Topic topic1 : allById) { - System.out.println("topic1.getTitle() = " + topic1.getTitle()); + boolean hasInstance = false; + if (!topic.getInstanceList().isEmpty()) { + hasInstance = true; + topic.updateExistInstance("(수정) 하루에 두 문제씩 문제를 해결합니다."); + } else { + topic.updateNotExistInstance("1일 2알고리즘", "(수정) 하루에 두 문제씩 문제를 해결합니다.", "CS", "유의사항", 30000); + } + + Topic savedTopic = topicRepository.save(topic); + + if (!hasInstance) { + assertEquals(topic.getId(), savedTopic.getId()); + assertEquals("(수정) 하루에 두 문제씩 문제를 해결합니다.", savedTopic.getDescription()); + } else { + assertEquals(topic.getId(), savedTopic.getId()); + assertEquals("(수정) 하루에 두 문제씩 문제를 해결합니다.", savedTopic.getDescription()); + assertEquals(30000, savedTopic.getPointPerPerson()); + } + } } } - @Test - public void 토픽_단건_조회() { - Topic savedTopic = topicRepository.save(topic); - Optional byId = topicRepository.findById(savedTopic.getId()); - Assertions.assertThat(byId.get().getTitle()).isEqualTo("1일 1알고리즘"); + @Nested + @DisplayName("delete 메서드는") + class Describe_delete { + + @Nested + @DisplayName("삭제하려는 토픽 ID가 주어질 때") + class Context_with_a_topic { + + @BeforeEach + public void init() { + topicRepository.save(topicA); + } + + @Test + @DisplayName("객체가 성공적으로 삭제되면, 다시 조회할 수 없다.") + public void it_cannot_be_retrieved_once_an_obj_is_successfully_deleted() { + Topic topic = topicRepository.findById(topicA.getId()).orElse(null); + assert topic != null; + topicRepository.delete(topic); + Topic findTopic = topicRepository.findById(topicA.getId()).orElse(null); + Assertions.assertThrows(NullPointerException.class, () -> { + findTopic.getId(); + }); + } + + @Test + @DisplayName("DB에 해당 객체가 없으면, 삭제할 수 없다.") + public void it_cannot_be_deleted_if_the_obj_does_not_exist() { + Assertions.assertThrows(Exception.class, () -> { + Topic topic = topicRepository.findById(topicB.getId()).orElse(null); + topicRepository.delete(topic); + }); + } + } } -} +} \ No newline at end of file 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 index 78c8af1c..af3f7233 100644 --- a/src/test/java/com/genius/gitget/admin/topic/service/TopicServiceTest.java +++ b/src/test/java/com/genius/gitget/admin/topic/service/TopicServiceTest.java @@ -6,7 +6,7 @@ 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.service.TopicService; +import com.genius.gitget.topic.serviceFacade.TopicFacade; import jakarta.transaction.Transactional; import java.util.Optional; import org.assertj.core.api.Assertions; @@ -23,10 +23,12 @@ public class TopicServiceTest { Topic topic, topicA; String fileType; @Autowired - TopicService topicService; + TopicFacade topicFacade; + @Autowired TopicRepository topicRepository; + @BeforeEach public void setup() { topic = Topic.builder() @@ -51,10 +53,10 @@ public void setup() { //given TopicCreateRequest topicCreateRequest = getTopicCreateRequest(); - Long savedTopicId = topicService.createTopic(topicCreateRequest); + Long savedTopicId = topicFacade.create(topicCreateRequest); //when - TopicDetailResponse topicById = topicService.getOneTopic(savedTopicId); + TopicDetailResponse topicById = topicFacade.findOne(savedTopicId); //then Assertions.assertThat(topicById.title()).isEqualTo(topicCreateRequest.title()); @@ -64,7 +66,7 @@ public void setup() { public void 토픽_수정() throws Exception { //given TopicCreateRequest topicCreateRequest = getTopicCreateRequest(); - Long savedTopicId = topicService.createTopic(topicCreateRequest); + Long savedTopicId = topicFacade.create(topicCreateRequest); //when TopicUpdateRequest topicUpdateRequest = TopicUpdateRequest.builder() @@ -74,7 +76,7 @@ public void setup() { .pointPerPerson(topic.getPointPerPerson()) .notice(topic.getNotice()).build(); - topicService.updateTopic(savedTopicId, topicUpdateRequest); + topicFacade.update(savedTopicId, topicUpdateRequest); //then Optional findTopic = topicRepository.findById(savedTopicId); @@ -86,14 +88,14 @@ public void setup() { public void 토픽_삭제() throws Exception { //given TopicCreateRequest topicCreateRequest = getTopicCreateRequest(); - Long savedTopicId = topicService.createTopic(topicCreateRequest); + Long savedTopicId = topicFacade.create(topicCreateRequest); //when - topicService.deleteTopic(savedTopicId); + topicFacade.delete(savedTopicId); //then try { - topicService.getOneTopic(savedTopicId); + topicFacade.findOne(savedTopicId); } catch (BusinessException e) { org.junit.jupiter.api.Assertions.assertEquals("해당 토픽을 찾을 수 없습니다.", e.getMessage()); }