From 4e4af6d5a24ce003c4ff37dcfbadafa71b4f1564 Mon Sep 17 00:00:00 2001 From: woosung1223 Date: Mon, 23 Oct 2023 14:13:59 +0900 Subject: [PATCH] =?UTF-8?q?test:=20=EC=A3=BC=EC=84=9D=EC=B2=98=EB=A6=AC?= =?UTF-8?q?=EB=90=9C=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EB=B3=B5=EC=9B=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../study/service/StudyServiceTest.java | 183 +++++++----------- 1 file changed, 70 insertions(+), 113 deletions(-) diff --git a/backend/src/test/java/harustudy/backend/study/service/StudyServiceTest.java b/backend/src/test/java/harustudy/backend/study/service/StudyServiceTest.java index 299e9be8..97b741b6 100644 --- a/backend/src/test/java/harustudy/backend/study/service/StudyServiceTest.java +++ b/backend/src/test/java/harustudy/backend/study/service/StudyServiceTest.java @@ -9,7 +9,13 @@ import harustudy.backend.participant.domain.Participant; import harustudy.backend.participant.domain.Step; import harustudy.backend.participant.exception.ParticipantIsNotHostException; +import harustudy.backend.participantcode.domain.GenerationStrategy; +import harustudy.backend.participantcode.domain.ParticipantCode; import harustudy.backend.study.domain.Study; +import harustudy.backend.study.dto.CreateStudyRequest; +import harustudy.backend.study.dto.StudyResponse; +import harustudy.backend.study.exception.ParticipantCodeNotFoundException; +import harustudy.backend.study.exception.StudyNotFoundException; import jakarta.persistence.EntityManager; import org.junit.jupiter.api.DisplayNameGeneration; import org.junit.jupiter.api.DisplayNameGenerator.ReplaceUnderscores; @@ -23,123 +29,74 @@ @Transactional @SpringBootTest class StudyServiceTest { -// + @Autowired private StudyService studyService; @Autowired private EntityManager entityManager; -// -// @Autowired -// private GenerationStrategy generationStrategy; -// -// @Test -// void 스터디_아이디로_스터디를_조회한다() { -// // given -// Study study = new Study("study", 8, 20); -// entityManager.persist(study); -// entityManager.flush(); -// entityManager.clear(); -// -// // when -// StudyResponse result = studyService.findPoudy(study.getId()); -// -// // then -// assertAll( -// () -> assertThat(result.studyId()).isEqualTo(study.getId()), -// () -> assertThat(result.name()).isEqualTo(study.getName()), -// () -> assertThat(result.totalCycle()).isEqualTo(study.getTotalCycle()), -// () -> assertThat(result.timePerCycle()).isEqualTo(study.getTimePerCycle()) -// ); -// } -// -// @Test -// void 룸_아이디로_룸_조회시_없으면_예외를_던진다() { -// // given -// Study study = new Study("study", 8, 20); -// entityManager.persist(study); -// entityManager.flush(); -// entityManager.clear(); -// -// // when, then -// assertThatThrownBy(() -> studyService.findPoudyWithFilter(99999L, null), null) -// .isInstanceOf(MemberNotFoundException.class); -// } -// -// @Test -// void 스터디를_생성한다() { -// // given -// CreateStudyRequest request = new CreateStudyRequest("study", 8, 40); -// -// // when -// CreateStudyResponse result = studyService.createStudy(request); -// -// // then -// assertAll( -// () -> assertThat(result.studyId()).isNotNull(), -// () -> assertThat(result.participantCode()).isNotNull() -// ); -// } -// -// @Test -// void 참여코드에_해당하는_스터디를_조회한다() { -// // given -// CreateStudyRequest request = new CreateStudyRequest("study", 8, 40); -// CreateStudyResponse response = studyService.createStudy(request); -// String participantCode = response.participantCode(); -// -// // when -// StudiesResponse result = studyService.findPoudyWithFilter(null, -// participantCode); -// -// // then -// assertAll( -// () -> assertThat(result.studies()).hasSize(1), -// () -> assertThat(result.studies().get(0).name()).isEqualTo(request.name()), -// () -> assertThat(result.studies().get(0).totalCycle()).isEqualTo( -// request.totalCycle()), -// () -> assertThat(result.studies().get(0).timePerCycle()).isEqualTo( -// request.timePerCycle()) -// ); -// } -// -// @Test -// void 참여코드에_해당하는_룸_조회시_참여코드가_없으면_예외를_던진다() { -// // given -// ParticipantCode participantCode = new ParticipantCode(generationStrategy); -// Study study = new Study("study", 8, 40, participantCode); -// entityManager.persist(participantCode); -// entityManager.persist(study); -// -// entityManager.flush(); -// entityManager.clear(); -// -// ParticipantCode notPersisted = new ParticipantCode(generationStrategy); -// -// // when, then -// assertThatThrownBy( -// () -> studyService.findPoudyWithFilter(null, notPersisted.getCode())) -// .isInstanceOf(ParticipantCodeNotFoundException.class); -// } -// -// @Test -// void 참여코드에_해당하는_룸_조회시_참여코드에_해당하는_룸이_없으면_예외를_던진다() { -// // given -// ParticipantCode participantCode = new ParticipantCode(generationStrategy); -// Study study = new Study("study", 8, 40, participantCode); -// entityManager.persist(participantCode); -// entityManager.persist(study); -// -// ParticipantCode notStudiesCode = new ParticipantCode(generationStrategy); -// entityManager.persist(notStudiesCode); -// -// entityManager.flush(); -// entityManager.clear(); -// -// // when, then -// assertThatThrownBy( -// () -> studyService.findStudyWithFilter(null, notStudiesCode.getCode())) -// .isInstanceOf(StudyNotFoundException.class); -// } + @Autowired + private GenerationStrategy generationStrategy; + + @Test + void 스터디_아이디로_스터디를_조회한다() { + // given + Study study = new Study("study", 8, 20); + entityManager.persist(study); + entityManager.flush(); + entityManager.clear(); + + // when + StudyResponse result = studyService.findStudy(study.getId()); + + // then + assertAll( + () -> assertThat(result.studyId()).isEqualTo(study.getId()), + () -> assertThat(result.name()).isEqualTo(study.getName()), + () -> assertThat(result.totalCycle()).isEqualTo(study.getTotalCycle()), + () -> assertThat(result.timePerCycle()).isEqualTo(study.getTimePerCycle()) + ); + } + + @Test + void 스터디_아이디로_스터디_조회시_없으면_예외를_던진다() { + // given + Study study = new Study("study", 8, 20); + entityManager.persist(study); + entityManager.flush(); + entityManager.clear(); + + // when, then + assertThatThrownBy(() -> studyService.findStudy(99999L)) + .isInstanceOf(StudyNotFoundException.class); + } + + @Test + void 참여코드에_해당하는_스터디_조회시_참여코드가_없으면_예외를_던진다() { + // given + Study study = new Study("study", 8, 40); + ParticipantCode participantCode = new ParticipantCode(study, generationStrategy); + entityManager.persist(study); + entityManager.persist(participantCode); + + entityManager.flush(); + entityManager.clear(); + + // when, then + assertThatThrownBy( + () -> studyService.findStudyWithFilter(null, "!@#!@#")) + .isInstanceOf(ParticipantCodeNotFoundException.class); + } + + void 스터디를_생성한다() { + // given + CreateStudyRequest request = new CreateStudyRequest("study", 8, 40); + + // when + Long studyId = studyService.createStudy(request); + + // then + assertThat(studyId).isNotNull(); + } @Test void 방장이_아니라면_스터디_단계를_넘길_수_없다() {