From 34b4ca9fc7b6c52b8775af47971c19b437daec2a Mon Sep 17 00:00:00 2001 From: Jangan Lee Date: Fri, 8 Nov 2024 16:12:54 +0900 Subject: [PATCH] =?UTF-8?q?[Weekly/10/Test/Refactor-MemberId]=20test=20?= =?UTF-8?q?=EA=B4=80=EB=A0=A8=20member=20=EB=8F=84=EB=A9=94=EC=9D=B8?= =?UTF-8?q?=EC=9D=98=20=EC=95=84=EC=9D=B4=EB=94=94=20=EC=83=81=EC=88=98?= =?UTF-8?q?=ED=99=94=20=EC=9D=B4=EC=99=B8=201=EA=B0=9C=20(#86)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * refactor: test 관련 memberId 상수화 * chore: 이제는 github action시 테스트도 수행함 --- .github/workflows/gradle.yml | 7 +- .../{global => _global}/TestData.java | 93 ++--- .../{global => _global}/TestUtil.java | 2 +- .../mockMember/WithMockCurator.java | 6 +- .../mockMember/WithMockCustomUser.java | 4 +- ...hMockCustomUserSecurityContextFactory.java | 2 +- .../mockMember/WithMockHost.java | 6 +- .../mockMember/WithMockMember.java | 6 +- .../mockMember/WithMockWelcomeMember.java | 12 + .../curation/CurationControllerUnitTest.java | 12 +- .../event/EventControllerUnitTest.java | 6 +- .../event/EventServiceUnitTest.java | 2 +- .../member/api/MemberControllerUnitTest.java | 19 + .../BaseMemberServiceUnitTest.java | 22 +- .../application/CuratorServiceUnitTest.java | 10 +- .../application/HostServiceUnitTest.java | 370 +++++++++--------- .../application/MemberServiceUnitTest.java | 8 +- 17 files changed, 314 insertions(+), 273 deletions(-) rename src/test/java/org/ktc2/cokaen/wouldyouin/{global => _global}/TestData.java (92%) rename src/test/java/org/ktc2/cokaen/wouldyouin/{global => _global}/TestUtil.java (84%) rename src/test/java/org/ktc2/cokaen/wouldyouin/{global => _global}/mockMember/WithMockCurator.java (51%) rename src/test/java/org/ktc2/cokaen/wouldyouin/{global => _global}/mockMember/WithMockCustomUser.java (82%) rename src/test/java/org/ktc2/cokaen/wouldyouin/{global => _global}/mockMember/WithMockCustomUserSecurityContextFactory.java (95%) rename src/test/java/org/ktc2/cokaen/wouldyouin/{global => _global}/mockMember/WithMockHost.java (51%) rename src/test/java/org/ktc2/cokaen/wouldyouin/{global => _global}/mockMember/WithMockMember.java (51%) create mode 100644 src/test/java/org/ktc2/cokaen/wouldyouin/_global/mockMember/WithMockWelcomeMember.java diff --git a/.github/workflows/gradle.yml b/.github/workflows/gradle.yml index 50aee2a3..e1ea7e26 100644 --- a/.github/workflows/gradle.yml +++ b/.github/workflows/gradle.yml @@ -32,11 +32,16 @@ jobs: restore-keys: | ${{ runner.os }}-gradle- - - name: Build With Gradle (exclude test) + - name: Build Application only using gradlew run: | chmod +x gradlew ./gradlew clean build -x test + - name: Test Application using gradlew + run: | + chmod +x gradlew + ./gradlew test + - name: Docker Hub Login if: github.event_name == 'push' uses: docker/login-action@v3 diff --git a/src/test/java/org/ktc2/cokaen/wouldyouin/global/TestData.java b/src/test/java/org/ktc2/cokaen/wouldyouin/_global/TestData.java similarity index 92% rename from src/test/java/org/ktc2/cokaen/wouldyouin/global/TestData.java rename to src/test/java/org/ktc2/cokaen/wouldyouin/_global/TestData.java index a8b6d04e..5d58e098 100644 --- a/src/test/java/org/ktc2/cokaen/wouldyouin/global/TestData.java +++ b/src/test/java/org/ktc2/cokaen/wouldyouin/_global/TestData.java @@ -1,4 +1,4 @@ -package org.ktc2.cokaen.wouldyouin.global; +package org.ktc2.cokaen.wouldyouin._global; import java.time.LocalDateTime; import java.util.List; @@ -66,13 +66,13 @@ public static CurationImage createValidCurationImage(Long id) { public static class MemberDomain { - public static final Long memberId = 1L; - public static final Long curatorId = 5L; - public static final Long hostId = 3L; - public static final Long welcomeMemberId = 4L; + public static final long validMemberId = 1L; + public static final long validCuratorId = 2L; + public static final long validHostId = 3L; + public static final long validWelcomeMemberId = 4L; public static Member createValidMember() { - MemberImage memberImage = ImageDomain.createValidMemberImage(1L); + MemberImage memberImage = ImageDomain.createValidMemberImage(validMemberId); Member ret = Member.builder() .accountType(AccountType.kakao) .email("member1@example.com") @@ -83,31 +83,33 @@ public static Member createValidMember() { .gender("Men") .socialId("100100100100100") .build(); - ReflectionTestUtils.setField(ret, "Id", 1L); + ReflectionTestUtils.setField(ret, "Id", validMemberId); ReflectionTestUtils.setField(ret, "memberType", MemberType.normal); ReflectionTestUtils.setField(memberImage, "baseMember", ret); return ret; } - public static Member createValidWelcomeMember() { - MemberImage memberImage = ImageDomain.createValidMemberImage(4L); - Member ret = Member.builder() - .accountType(AccountType.kakao) - .email("member2@example.com") - .nickname("nick_normal_333") - .phone("010-4414-1144") + public static Curator createValidCurator() { + MemberImage memberImage = ImageDomain.createValidMemberImage(validCuratorId); + Curator ret = Curator.curatorBuilder() + .accountType(AccountType.google) + .email("curator1@example.com") + .nickname("nick_curator_12") + .phone("010-4545-6767") .profileImage(memberImage) - .area(Area.서울) - .gender("Men") - .socialId("456456456456") + .area(Area.광주) + .gender("Women") + .socialId("200200200200200") .build(); - ReflectionTestUtils.setField(ret, "Id", 4L); + ReflectionTestUtils.setField(ret, "Id", validCuratorId); + ReflectionTestUtils.setField(ret, "intro", "큐레이터 자기소개입니다."); + ReflectionTestUtils.setField(ret, "hashtag", "#큐레이터#해시태그#입니다"); ReflectionTestUtils.setField(memberImage, "baseMember", ret); return ret; } public static Host createValidHost() { - MemberImage memberImage = ImageDomain.createValidMemberImage(3L); + MemberImage memberImage = ImageDomain.createValidMemberImage(validHostId); Host ret = Host.builder() .email("curator1@example.com") .nickname("nick_curator_12") @@ -115,43 +117,29 @@ public static Host createValidHost() { .hashedPassword("hashed_password") .profileImage(memberImage) .build(); - ReflectionTestUtils.setField(ret, "Id", 3L); + ReflectionTestUtils.setField(ret, "Id", validHostId); ReflectionTestUtils.setField(ret, "intro", "주최자 자기소개입니다."); ReflectionTestUtils.setField(ret, "hashtag", "#주최자#해시태그#입니다"); ReflectionTestUtils.setField(memberImage, "baseMember", ret); return ret; } - public static Curator createValidCurator() { - MemberImage memberImage = ImageDomain.createValidMemberImage(2L); - Curator ret = Curator.curatorBuilder() - .accountType(AccountType.google) - .email("curator1@example.com") - .nickname("nick_curator_12") - .phone("010-4545-6767") + public static Member createValidWelcomeMember() { + MemberImage memberImage = ImageDomain.createValidMemberImage(validWelcomeMemberId); + Member ret = Member.builder() + .accountType(AccountType.kakao) + .email("member2@example.com") + .nickname("nick_normal_333") + .phone("010-4414-1144") .profileImage(memberImage) - .area(Area.광주) - .gender("Women") - .socialId("200200200200200") + .area(Area.서울) + .gender("Men") + .socialId("456456456456") .build(); - ReflectionTestUtils.setField(ret, "Id", 2L); - ReflectionTestUtils.setField(ret, "intro", "큐레이터 자기소개입니다."); - ReflectionTestUtils.setField(ret, "hashtag", "#큐레이터#해시태그#입니다"); + ReflectionTestUtils.setField(ret, "Id", validWelcomeMemberId); ReflectionTestUtils.setField(memberImage, "baseMember", ret); return ret; } - - public static CurationCuratorResponse createCurationCuratorResponse() { - return CurationCuratorResponse.builder() - .nickname("nick_curator_12") - .email("curator1@example.com") - .phone("010-4545-6767") - .profileImageUrl("image2.com") - .intro("큐레이터 자기소개입니다.") - .likes(0) - .hashtags(List.of("#큐레이터", "#해시태그", "#입니다")) - .build(); - } } public static class EventDomain { @@ -302,9 +290,22 @@ public static CurationEditRequest createValidCurationEditRequest() { .build(); } + public static CurationCuratorResponse createCurationCuratorResponse() { + Curator curator = MemberDomain.createValidCurator(); + return CurationCuratorResponse.builder() + .nickname(curator.getNickname()) + .email(curator.getEmail()) + .phone(curator.getPhone()) + .profileImageUrl(curator.getProfileImageUrl()) + .intro(curator.getIntro()) + .likes(curator.getLikes()) + .hashtags(curator.getHashTagList()) + .build(); + } + public static CurationResponse createValidCurationResponse() { return CurationResponse.builder() - .curator(MemberDomain.createCurationCuratorResponse()) + .curator(createCurationCuratorResponse()) .title("title") .content("content") .curationCards(List.of(createCurationCardResponse1())) diff --git a/src/test/java/org/ktc2/cokaen/wouldyouin/global/TestUtil.java b/src/test/java/org/ktc2/cokaen/wouldyouin/_global/TestUtil.java similarity index 84% rename from src/test/java/org/ktc2/cokaen/wouldyouin/global/TestUtil.java rename to src/test/java/org/ktc2/cokaen/wouldyouin/_global/TestUtil.java index 36a22feb..50cbb345 100644 --- a/src/test/java/org/ktc2/cokaen/wouldyouin/global/TestUtil.java +++ b/src/test/java/org/ktc2/cokaen/wouldyouin/_global/TestUtil.java @@ -1,4 +1,4 @@ -package org.ktc2.cokaen.wouldyouin.global; +package org.ktc2.cokaen.wouldyouin._global; public class TestUtil { diff --git a/src/test/java/org/ktc2/cokaen/wouldyouin/global/mockMember/WithMockCurator.java b/src/test/java/org/ktc2/cokaen/wouldyouin/_global/mockMember/WithMockCurator.java similarity index 51% rename from src/test/java/org/ktc2/cokaen/wouldyouin/global/mockMember/WithMockCurator.java rename to src/test/java/org/ktc2/cokaen/wouldyouin/_global/mockMember/WithMockCurator.java index a548df35..58488b23 100644 --- a/src/test/java/org/ktc2/cokaen/wouldyouin/global/mockMember/WithMockCurator.java +++ b/src/test/java/org/ktc2/cokaen/wouldyouin/_global/mockMember/WithMockCurator.java @@ -1,12 +1,12 @@ -package org.ktc2.cokaen.wouldyouin.global.mockMember; +package org.ktc2.cokaen.wouldyouin._global.mockMember; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; -import org.ktc2.cokaen.wouldyouin.global.TestData; +import org.ktc2.cokaen.wouldyouin._global.TestData.MemberDomain; import org.ktc2.cokaen.wouldyouin.member.persist.MemberType; @Retention(RetentionPolicy.RUNTIME) -@WithMockCustomUser(memberId = 5L, memberType = MemberType.curator) +@WithMockCustomUser(memberId = MemberDomain.validCuratorId, memberType = MemberType.curator) public @interface WithMockCurator { } diff --git a/src/test/java/org/ktc2/cokaen/wouldyouin/global/mockMember/WithMockCustomUser.java b/src/test/java/org/ktc2/cokaen/wouldyouin/_global/mockMember/WithMockCustomUser.java similarity index 82% rename from src/test/java/org/ktc2/cokaen/wouldyouin/global/mockMember/WithMockCustomUser.java rename to src/test/java/org/ktc2/cokaen/wouldyouin/_global/mockMember/WithMockCustomUser.java index 89734fe9..742a7324 100644 --- a/src/test/java/org/ktc2/cokaen/wouldyouin/global/mockMember/WithMockCustomUser.java +++ b/src/test/java/org/ktc2/cokaen/wouldyouin/_global/mockMember/WithMockCustomUser.java @@ -1,4 +1,4 @@ -package org.ktc2.cokaen.wouldyouin.global.mockMember; +package org.ktc2.cokaen.wouldyouin._global.mockMember; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @@ -8,6 +8,6 @@ @Retention(RetentionPolicy.RUNTIME) @WithSecurityContext(factory = WithMockCustomUserSecurityContextFactory.class) public @interface WithMockCustomUser { - long memberId() default 1L; + long memberId(); MemberType memberType(); } diff --git a/src/test/java/org/ktc2/cokaen/wouldyouin/global/mockMember/WithMockCustomUserSecurityContextFactory.java b/src/test/java/org/ktc2/cokaen/wouldyouin/_global/mockMember/WithMockCustomUserSecurityContextFactory.java similarity index 95% rename from src/test/java/org/ktc2/cokaen/wouldyouin/global/mockMember/WithMockCustomUserSecurityContextFactory.java rename to src/test/java/org/ktc2/cokaen/wouldyouin/_global/mockMember/WithMockCustomUserSecurityContextFactory.java index ae50756a..4c164a2b 100644 --- a/src/test/java/org/ktc2/cokaen/wouldyouin/global/mockMember/WithMockCustomUserSecurityContextFactory.java +++ b/src/test/java/org/ktc2/cokaen/wouldyouin/_global/mockMember/WithMockCustomUserSecurityContextFactory.java @@ -1,4 +1,4 @@ -package org.ktc2.cokaen.wouldyouin.global.mockMember; +package org.ktc2.cokaen.wouldyouin._global.mockMember; import org.ktc2.cokaen.wouldyouin.auth.MemberIdentifier; import org.ktc2.cokaen.wouldyouin.auth.persist.CustomUserDetails; diff --git a/src/test/java/org/ktc2/cokaen/wouldyouin/global/mockMember/WithMockHost.java b/src/test/java/org/ktc2/cokaen/wouldyouin/_global/mockMember/WithMockHost.java similarity index 51% rename from src/test/java/org/ktc2/cokaen/wouldyouin/global/mockMember/WithMockHost.java rename to src/test/java/org/ktc2/cokaen/wouldyouin/_global/mockMember/WithMockHost.java index 7cd3062a..1805cbf8 100644 --- a/src/test/java/org/ktc2/cokaen/wouldyouin/global/mockMember/WithMockHost.java +++ b/src/test/java/org/ktc2/cokaen/wouldyouin/_global/mockMember/WithMockHost.java @@ -1,12 +1,12 @@ -package org.ktc2.cokaen.wouldyouin.global.mockMember; +package org.ktc2.cokaen.wouldyouin._global.mockMember; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; -import org.ktc2.cokaen.wouldyouin.global.TestData; +import org.ktc2.cokaen.wouldyouin._global.TestData.MemberDomain; import org.ktc2.cokaen.wouldyouin.member.persist.MemberType; @Retention(RetentionPolicy.RUNTIME) -@WithMockCustomUser(memberId = 3L, memberType = MemberType.host) +@WithMockCustomUser(memberId = MemberDomain.validHostId, memberType = MemberType.host) public @interface WithMockHost { } diff --git a/src/test/java/org/ktc2/cokaen/wouldyouin/global/mockMember/WithMockMember.java b/src/test/java/org/ktc2/cokaen/wouldyouin/_global/mockMember/WithMockMember.java similarity index 51% rename from src/test/java/org/ktc2/cokaen/wouldyouin/global/mockMember/WithMockMember.java rename to src/test/java/org/ktc2/cokaen/wouldyouin/_global/mockMember/WithMockMember.java index ac7b65d0..0bbaf38c 100644 --- a/src/test/java/org/ktc2/cokaen/wouldyouin/global/mockMember/WithMockMember.java +++ b/src/test/java/org/ktc2/cokaen/wouldyouin/_global/mockMember/WithMockMember.java @@ -1,12 +1,12 @@ -package org.ktc2.cokaen.wouldyouin.global.mockMember; +package org.ktc2.cokaen.wouldyouin._global.mockMember; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; -import org.ktc2.cokaen.wouldyouin.global.TestData; +import org.ktc2.cokaen.wouldyouin._global.TestData.MemberDomain; import org.ktc2.cokaen.wouldyouin.member.persist.MemberType; @Retention(RetentionPolicy.RUNTIME) -@WithMockCustomUser(memberId = 1L, memberType = MemberType.normal) +@WithMockCustomUser(memberId = MemberDomain.validMemberId, memberType = MemberType.normal) public @interface WithMockMember { } \ No newline at end of file diff --git a/src/test/java/org/ktc2/cokaen/wouldyouin/_global/mockMember/WithMockWelcomeMember.java b/src/test/java/org/ktc2/cokaen/wouldyouin/_global/mockMember/WithMockWelcomeMember.java new file mode 100644 index 00000000..1f8e877b --- /dev/null +++ b/src/test/java/org/ktc2/cokaen/wouldyouin/_global/mockMember/WithMockWelcomeMember.java @@ -0,0 +1,12 @@ +package org.ktc2.cokaen.wouldyouin._global.mockMember; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import org.ktc2.cokaen.wouldyouin._global.TestData.MemberDomain; +import org.ktc2.cokaen.wouldyouin.member.persist.MemberType; + +@Retention(RetentionPolicy.RUNTIME) +@WithMockCustomUser(memberId = MemberDomain.validWelcomeMemberId, memberType = MemberType.welcome) +public @interface WithMockWelcomeMember { + +} diff --git a/src/test/java/org/ktc2/cokaen/wouldyouin/curation/CurationControllerUnitTest.java b/src/test/java/org/ktc2/cokaen/wouldyouin/curation/CurationControllerUnitTest.java index 499a9a99..959803e9 100644 --- a/src/test/java/org/ktc2/cokaen/wouldyouin/curation/CurationControllerUnitTest.java +++ b/src/test/java/org/ktc2/cokaen/wouldyouin/curation/CurationControllerUnitTest.java @@ -26,11 +26,11 @@ import org.ktc2.cokaen.wouldyouin.curation.api.CurationController; import org.ktc2.cokaen.wouldyouin.curation.api.dto.CurationCreateRequest; import org.ktc2.cokaen.wouldyouin.curation.application.CurationService; -import org.ktc2.cokaen.wouldyouin.global.TestData.CurationDomain; -import org.ktc2.cokaen.wouldyouin.global.TestData.MemberDomain; -import org.ktc2.cokaen.wouldyouin.global.mockMember.WithMockCurator; -import org.ktc2.cokaen.wouldyouin.global.mockMember.WithMockHost; -import org.ktc2.cokaen.wouldyouin.global.mockMember.WithMockMember; +import org.ktc2.cokaen.wouldyouin._global.TestData.CurationDomain; +import org.ktc2.cokaen.wouldyouin._global.TestData.MemberDomain; +import org.ktc2.cokaen.wouldyouin._global.mockMember.WithMockCurator; +import org.ktc2.cokaen.wouldyouin._global.mockMember.WithMockHost; +import org.ktc2.cokaen.wouldyouin._global.mockMember.WithMockMember; import org.mockito.ArgumentCaptor; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; @@ -159,7 +159,7 @@ void createCuration1() throws Exception { .andExpect(status().isCreated()); // then - then(curationService).should(times(1)).create(eq(MemberDomain.curatorId), captor.capture()); + then(curationService).should(times(1)).create(eq(MemberDomain.validCuratorId), captor.capture()); assertThat(captor.getValue()).isEqualTo(request); } diff --git a/src/test/java/org/ktc2/cokaen/wouldyouin/event/EventControllerUnitTest.java b/src/test/java/org/ktc2/cokaen/wouldyouin/event/EventControllerUnitTest.java index cfada297..e9267358 100644 --- a/src/test/java/org/ktc2/cokaen/wouldyouin/event/EventControllerUnitTest.java +++ b/src/test/java/org/ktc2/cokaen/wouldyouin/event/EventControllerUnitTest.java @@ -1,7 +1,7 @@ package org.ktc2.cokaen.wouldyouin.event; -import static org.ktc2.cokaen.wouldyouin.global.TestData.MemberDomain.createValidHost; +import static org.ktc2.cokaen.wouldyouin._global.TestData.MemberDomain.createValidHost; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.BDDMockito.given; @@ -34,8 +34,8 @@ import org.ktc2.cokaen.wouldyouin.event.api.dto.EventEditRequest; import org.ktc2.cokaen.wouldyouin.event.api.dto.EventSliceResponse; import org.ktc2.cokaen.wouldyouin.event.application.EventService; -import org.ktc2.cokaen.wouldyouin.global.TestData.EventDomain; -import org.ktc2.cokaen.wouldyouin.global.mockMember.WithMockHost; +import org.ktc2.cokaen.wouldyouin._global.TestData.EventDomain; +import org.ktc2.cokaen.wouldyouin._global.mockMember.WithMockHost; import org.ktc2.cokaen.wouldyouin.member.application.MemberService; import org.ktc2.cokaen.wouldyouin.member.persist.Host; import org.springframework.beans.factory.annotation.Autowired; diff --git a/src/test/java/org/ktc2/cokaen/wouldyouin/event/EventServiceUnitTest.java b/src/test/java/org/ktc2/cokaen/wouldyouin/event/EventServiceUnitTest.java index 5c7c1349..30000624 100644 --- a/src/test/java/org/ktc2/cokaen/wouldyouin/event/EventServiceUnitTest.java +++ b/src/test/java/org/ktc2/cokaen/wouldyouin/event/EventServiceUnitTest.java @@ -25,7 +25,7 @@ import org.ktc2.cokaen.wouldyouin.event.application.EventService; import org.ktc2.cokaen.wouldyouin.event.persist.Event; import org.ktc2.cokaen.wouldyouin.event.persist.EventRepository; -import org.ktc2.cokaen.wouldyouin.global.TestData.EventDomain; +import org.ktc2.cokaen.wouldyouin._global.TestData.EventDomain; import org.ktc2.cokaen.wouldyouin.member.application.HostService; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; diff --git a/src/test/java/org/ktc2/cokaen/wouldyouin/member/api/MemberControllerUnitTest.java b/src/test/java/org/ktc2/cokaen/wouldyouin/member/api/MemberControllerUnitTest.java index 1f78e8d9..f47537a6 100644 --- a/src/test/java/org/ktc2/cokaen/wouldyouin/member/api/MemberControllerUnitTest.java +++ b/src/test/java/org/ktc2/cokaen/wouldyouin/member/api/MemberControllerUnitTest.java @@ -4,12 +4,31 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.ktc2.cokaen.wouldyouin.auth.application.CustomUserDetailsService; +import org.ktc2.cokaen.wouldyouin.auth.application.JwtService; +import org.ktc2.cokaen.wouldyouin.member.application.BaseMemberService; +import org.ktc2.cokaen.wouldyouin.member.application.HostService; +import org.ktc2.cokaen.wouldyouin.member.application.MemberService; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.boot.test.mock.mockito.MockBean; @WebMvcTest(MemberController.class) class MemberControllerUnitTest { + @MockBean + private CustomUserDetailsService customUserDetailsService; + @MockBean + private JwtService jwtService; + + @MockBean + private BaseMemberService baseMemberService; + + @MockBean + private MemberService memberService; + + @MockBean + private HostService hostService; @BeforeEach void setUp() { diff --git a/src/test/java/org/ktc2/cokaen/wouldyouin/member/application/BaseMemberServiceUnitTest.java b/src/test/java/org/ktc2/cokaen/wouldyouin/member/application/BaseMemberServiceUnitTest.java index 2f14e7a0..8a3d0abc 100644 --- a/src/test/java/org/ktc2/cokaen/wouldyouin/member/application/BaseMemberServiceUnitTest.java +++ b/src/test/java/org/ktc2/cokaen/wouldyouin/member/application/BaseMemberServiceUnitTest.java @@ -2,10 +2,14 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.ktc2.cokaen.wouldyouin.global.TestData.MemberDomain.createValidCurator; -import static org.ktc2.cokaen.wouldyouin.global.TestData.MemberDomain.createValidHost; -import static org.ktc2.cokaen.wouldyouin.global.TestData.MemberDomain.createValidMember; -import static org.ktc2.cokaen.wouldyouin.global.TestData.MemberDomain.createValidWelcomeMember; +import static org.ktc2.cokaen.wouldyouin._global.TestData.MemberDomain.createValidCurator; +import static org.ktc2.cokaen.wouldyouin._global.TestData.MemberDomain.createValidHost; +import static org.ktc2.cokaen.wouldyouin._global.TestData.MemberDomain.createValidMember; +import static org.ktc2.cokaen.wouldyouin._global.TestData.MemberDomain.createValidWelcomeMember; +import static org.ktc2.cokaen.wouldyouin._global.TestData.MemberDomain.validCuratorId; +import static org.ktc2.cokaen.wouldyouin._global.TestData.MemberDomain.validHostId; +import static org.ktc2.cokaen.wouldyouin._global.TestData.MemberDomain.validMemberId; +import static org.ktc2.cokaen.wouldyouin._global.TestData.MemberDomain.validWelcomeMemberId; import static org.mockito.ArgumentMatchers.any; import static org.mockito.BDDMockito.given; import static org.mockito.BDDMockito.then; @@ -88,7 +92,7 @@ MemberType welcomeTypeMapping(MemberType memberType) { } @ParameterizedTest - @ValueSource(longs = {1L, 2L, 3L, 4L}) + @ValueSource(longs = {validMemberId, validCuratorId, validHostId, validWelcomeMemberId}) @DisplayName("임의의 사용자를 찾는 메서드 테스트") void getByIdOrThrow(long id) { // when @@ -101,7 +105,7 @@ void getByIdOrThrow(long id) { } @ParameterizedTest - @ValueSource(longs = {1L, 2L, 3L, 4L}) + @ValueSource(longs = {validMemberId, validCuratorId, validHostId, validWelcomeMemberId}) @DisplayName("임의의 사용자의 타입을 반환하는 메서드 테스트") void getMemberType(long id) { // when @@ -114,7 +118,7 @@ void getMemberType(long id) { } @ParameterizedTest - @ValueSource(longs = {1L, 2L, 3L, 4L}) + @ValueSource(longs = {validMemberId, validCuratorId, validHostId, validWelcomeMemberId}) @DisplayName("각 유형의 사용자를 찾고 응답 반환하는 메서드 테스트") void findById(long id) { // given @@ -136,7 +140,7 @@ void findById(long id) { } @ParameterizedTest - @ValueSource(longs = {1L, 2L, 3L, 4L}) + @ValueSource(longs = {validMemberId, validCuratorId, validHostId, validWelcomeMemberId}) @DisplayName("중복 있는 이메일 있는 경우 테스트") void checkUniqueEmailOrThrow(long id) { // given @@ -165,7 +169,7 @@ void checkUniqueEmailOrThrow_Unique() { } @ParameterizedTest - @ValueSource(longs = {1L, 2L, 3L, 4L}) + @ValueSource(longs = {validMemberId, validCuratorId, validHostId, validWelcomeMemberId}) @DisplayName("각 유형의 사용자를 제거하는 메서드 테스트") void deleteById(long id) { // given diff --git a/src/test/java/org/ktc2/cokaen/wouldyouin/member/application/CuratorServiceUnitTest.java b/src/test/java/org/ktc2/cokaen/wouldyouin/member/application/CuratorServiceUnitTest.java index 0587bc6e..735d8c4a 100644 --- a/src/test/java/org/ktc2/cokaen/wouldyouin/member/application/CuratorServiceUnitTest.java +++ b/src/test/java/org/ktc2/cokaen/wouldyouin/member/application/CuratorServiceUnitTest.java @@ -1,9 +1,9 @@ package org.ktc2.cokaen.wouldyouin.member.application; import static org.assertj.core.api.Assertions.assertThat; -import static org.ktc2.cokaen.wouldyouin.global.TestData.ImageDomain.createValidMemberImage; -import static org.ktc2.cokaen.wouldyouin.global.TestData.MemberDomain.createValidCurator; -import static org.ktc2.cokaen.wouldyouin.global.TestData.MemberDomain.createValidMember; +import static org.ktc2.cokaen.wouldyouin._global.TestData.ImageDomain.createValidMemberImage; +import static org.ktc2.cokaen.wouldyouin._global.TestData.MemberDomain.createValidCurator; +import static org.ktc2.cokaen.wouldyouin._global.TestData.MemberDomain.createValidMember; import static org.mockito.ArgumentMatchers.any; import static org.mockito.BDDMockito.given; import static org.mockito.BDDMockito.then; @@ -17,7 +17,7 @@ import org.ktc2.cokaen.wouldyouin.Image.application.MemberImageService; import org.ktc2.cokaen.wouldyouin.Image.persist.MemberImage; import org.ktc2.cokaen.wouldyouin._common.vo.Area; -import org.ktc2.cokaen.wouldyouin.global.TestUtil; +import org.ktc2.cokaen.wouldyouin._global.TestUtil; import org.ktc2.cokaen.wouldyouin.member.application.dto.request.edit.CuratorEditRequest; import org.ktc2.cokaen.wouldyouin.member.persist.BaseMemberRepository; import org.ktc2.cokaen.wouldyouin.member.persist.Curator; @@ -81,7 +81,7 @@ void updateCurator() { CuratorEditRequest editRequest = CuratorEditRequest.curatorEditRequestBuilder() .nickname(TestUtil.getOrNull("newNickname")) .phoneNumber(TestUtil.getOrNull("010-1010-8888")) - .profileImageId(TestUtil.getOrNull(5L)) + .profileImageId(TestUtil.getOrNull(newProfileImageId)) .area(Area.광주) .intro(TestUtil.getOrNull("new intro")) .build(); diff --git a/src/test/java/org/ktc2/cokaen/wouldyouin/member/application/HostServiceUnitTest.java b/src/test/java/org/ktc2/cokaen/wouldyouin/member/application/HostServiceUnitTest.java index 1b6045e4..a4052dd2 100644 --- a/src/test/java/org/ktc2/cokaen/wouldyouin/member/application/HostServiceUnitTest.java +++ b/src/test/java/org/ktc2/cokaen/wouldyouin/member/application/HostServiceUnitTest.java @@ -1,185 +1,185 @@ -//package org.ktc2.cokaen.wouldyouin.member.application; -// -//import static org.assertj.core.api.Assertions.assertThat; -//import static org.ktc2.cokaen.wouldyouin.global.TestData.MemberDomain.createValidHost; -//import static org.ktc2.cokaen.wouldyouin.global.TestData.MemberDomain.createValidMemberImage; -//import static org.mockito.BDDMockito.given; -//import static org.mockito.BDDMockito.then; -//import static org.mockito.Mockito.times; -// -//import java.util.Optional; -//import org.junit.jupiter.api.BeforeEach; -//import org.junit.jupiter.api.DisplayName; -//import org.junit.jupiter.api.Test; -//import org.junit.jupiter.api.extension.ExtendWith; -//import org.ktc2.cokaen.wouldyouin.Image.application.MemberImageService; -//import org.ktc2.cokaen.wouldyouin.Image.persist.MemberImage; -//import org.ktc2.cokaen.wouldyouin.auth.application.dto.LocalLoginRequest; -//import org.ktc2.cokaen.wouldyouin.global.TestUtil; -//import org.ktc2.cokaen.wouldyouin.member.application.dto.request.create.HostCreateRequest; -//import org.ktc2.cokaen.wouldyouin.member.application.dto.request.edit.HostEditRequest; -//import org.ktc2.cokaen.wouldyouin.member.persist.Host; -//import org.ktc2.cokaen.wouldyouin.member.persist.HostRepository; -//import org.ktc2.cokaen.wouldyouin.member.persist.MemberType; -//import org.mockito.InjectMocks; -//import org.mockito.Mock; -//import org.mockito.junit.jupiter.MockitoExtension; -//import org.mockito.junit.jupiter.MockitoSettings; -//import org.mockito.quality.Strictness; -//import org.springframework.security.crypto.password.PasswordEncoder; -// -//@ExtendWith(MockitoExtension.class) -//@MockitoSettings(strictness = Strictness.LENIENT) -//class HostServiceUnitTest { -// -// @Mock -// private HostRepository hostRepository; -// @Mock -// private PasswordEncoder passwordEncoder; -// @Mock -// private MemberImageService memberImageService; -// -// @Mock -// private HostCreateRequest hostCreateRequest; -// -// @InjectMocks -// private HostService hostService; -// -// private Host validHost; -// -// @BeforeEach -// void setUp() { -// validHost = createValidHost(); -// } -// -// @Test -// @DisplayName("호스트 사용자 생성 테스트") -// void createHost() { -// // given -// String password = "host0password"; -// String hashedPassword = validHost.getHashedPassword(); -// MemberImage profileImage = validHost.getProfileImage(); -// Long profileImageId = profileImage.getId(); -// -// given(hostCreateRequest.getPassword()).willReturn(password); -// given(hostCreateRequest.getProfileImageId()).willReturn(profileImageId); -// given(passwordEncoder.encode(password)).willReturn(hashedPassword); -// given(memberImageService.getById(profileImageId)).willReturn(profileImage); -// given(hostCreateRequest.toEntity(hashedPassword, profileImage)).willReturn(validHost); -// given(hostRepository.save(validHost)).willReturn(validHost); -// -// // when -// hostService.createHost(hostCreateRequest); -// -// // then -// var ignore1 = then(hostCreateRequest).should(times(1)).getPassword(); -// var ignore2 = then(hostCreateRequest).should(times(1)).getProfileImageId(); -// then(passwordEncoder).should(times(1)).encode(password); -// then(memberImageService).should(times(1)).getById(profileImageId); -// then(hostCreateRequest).should(times(1)) -// .toEntity(hashedPassword, profileImage); -// then(hostRepository).should(times(1)).save(validHost); -// then(memberImageService).should(times(1)).setBaseMember(profileImage, validHost); -// } -// -// @Test -// @DisplayName("호스트 업데이트 테스트") -// void updateHost() { -// // given -// Long newProfileImageId = 5L; -// MemberImage newProfileImage = createValidMemberImage(newProfileImageId); -// newProfileImage.setBaseMember(validHost); -// HostEditRequest editRequest = HostEditRequest.builder() -// .nickname(TestUtil.getOrNull("newNickname")) -// .phoneNumber(TestUtil.getOrNull("010-1010-8888")) -// .profileImageId(TestUtil.getOrNull(5L)) -// .intro(TestUtil.getOrNull("new intro")) -// .hashtag(TestUtil.getOrNull("#new#hashtag")) -// .build(); -// -// given(hostRepository.findById(validHost.getId())).willReturn(Optional.of(validHost)); -// given(memberImageService.getById(newProfileImageId)).willReturn(newProfileImage); -// -// // when -// hostService.updateHost(validHost.getId(), editRequest); -// -// // then -// then(hostRepository).should(times(1)).findById(validHost.getId()); -// int times = 1; -// if (editRequest.getProfileImageId() == null) { -// times = 0; -// } -// then(memberImageService).should(times(times)).getById(newProfileImageId); -// } -// -// @Test -// @DisplayName("호스트 삭제 테스트") -// void deleteById() { -// // given -// given(hostRepository.findById(validHost.getId())).willReturn(Optional.of(validHost)); -// -// // when -// hostService.deleteById(validHost.getId()); -// -// // then -// then(hostRepository).should(times(1)).delete(validHost); -// } -// -// @Test -// @DisplayName("id로 사용자 응답 생성 테스트") -// void getMemberResponseById() { -// // given -// given(hostRepository.findById(validHost.getId())).willReturn(Optional.of(validHost)); -// -// // when -// hostService.getMemberResponseById(validHost.getId()); -// -// // then -// then(hostRepository).should(times(1)).findById(validHost.getId()); -// } -// -// @Test -// @DisplayName("로그인 요청으로 사용자 응답 테스트") -// void getMemberResponseBy() { -// // given -// String password = "host0password"; -// LocalLoginRequest loginRequest = new LocalLoginRequest(validHost.getEmail(), password); -// given(passwordEncoder.encode(password)).willReturn(validHost.getHashedPassword()); -// given(hostRepository.findByEmailAndHashedPassword( -// validHost.getEmail(), validHost.getHashedPassword())) -// .willReturn(Optional.of(validHost)); -// -// // when -// hostService.getMemberResponseBy(loginRequest); -// -// // then -// then(passwordEncoder).should(times(1)).encode(password); -// then(hostRepository).should(times(1)).findByEmailAndHashedPassword( -// validHost.getEmail(), validHost.getHashedPassword()); -// } -// -// @Test -// @DisplayName("id로 호스트 얻어오기 테스트") -// void getByIdOrThrow() { -// // given -// given(hostRepository.findById(validHost.getId())).willReturn(Optional.of(validHost)); -// -// // when -// hostService.getById(validHost.getId()); -// -// // then -// then(hostRepository).should(times(1)).findById(validHost.getId()); -// } -// -// @Test -// @DisplayName("대상 사용자 유형 확인 테스트") -// void getTargetMemberType() { -// assertThat(hostService.getTargetMemberType()).isEqualTo(MemberType.host); -// } -// -// @Test -// @DisplayName("LikeableMemberService 얻어오기 테스트") -// void getLikeableMemberService() { -// assertThat(hostService.getLikeableMemberService()).isEqualTo(hostService); -// } -//} \ No newline at end of file +package org.ktc2.cokaen.wouldyouin.member.application; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.ktc2.cokaen.wouldyouin._global.TestData.MemberDomain.createValidHost; +import static org.ktc2.cokaen.wouldyouin._global.TestData.ImageDomain.createValidMemberImage; +import static org.mockito.BDDMockito.given; +import static org.mockito.BDDMockito.then; +import static org.mockito.Mockito.times; + +import java.util.Optional; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.ktc2.cokaen.wouldyouin.Image.application.MemberImageService; +import org.ktc2.cokaen.wouldyouin.Image.persist.MemberImage; +import org.ktc2.cokaen.wouldyouin.auth.application.dto.LocalLoginRequest; +import org.ktc2.cokaen.wouldyouin._global.TestUtil; +import org.ktc2.cokaen.wouldyouin.member.application.dto.request.create.HostCreateRequest; +import org.ktc2.cokaen.wouldyouin.member.application.dto.request.edit.HostEditRequest; +import org.ktc2.cokaen.wouldyouin.member.persist.Host; +import org.ktc2.cokaen.wouldyouin.member.persist.HostRepository; +import org.ktc2.cokaen.wouldyouin.member.persist.MemberType; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.mockito.junit.jupiter.MockitoSettings; +import org.mockito.quality.Strictness; +import org.springframework.security.crypto.password.PasswordEncoder; + +@ExtendWith(MockitoExtension.class) +@MockitoSettings(strictness = Strictness.LENIENT) +class HostServiceUnitTest { + + @Mock + private HostRepository hostRepository; + @Mock + private PasswordEncoder passwordEncoder; + @Mock + private MemberImageService memberImageService; + + @Mock + private HostCreateRequest hostCreateRequest; + + @InjectMocks + private HostService hostService; + + private Host validHost; + + @BeforeEach + void setUp() { + validHost = createValidHost(); + } + + @Test + @DisplayName("호스트 사용자 생성 테스트") + void createHost() { + // given + String password = "host0password"; + String hashedPassword = validHost.getHashedPassword(); + MemberImage profileImage = validHost.getProfileImage(); + Long profileImageId = profileImage.getId(); + + given(hostCreateRequest.getPassword()).willReturn(password); + given(hostCreateRequest.getProfileImageId()).willReturn(profileImageId); + given(passwordEncoder.encode(password)).willReturn(hashedPassword); + given(memberImageService.getById(profileImageId)).willReturn(profileImage); + given(hostCreateRequest.toEntity(hashedPassword, profileImage)).willReturn(validHost); + given(hostRepository.save(validHost)).willReturn(validHost); + + // when + hostService.createHost(hostCreateRequest); + + // then + var ignore1 = then(hostCreateRequest).should(times(1)).getPassword(); + var ignore2 = then(hostCreateRequest).should(times(1)).getProfileImageId(); + then(passwordEncoder).should(times(1)).encode(password); + then(memberImageService).should(times(1)).getById(profileImageId); + then(hostCreateRequest).should(times(1)) + .toEntity(hashedPassword, profileImage); + then(hostRepository).should(times(1)).save(validHost); + then(memberImageService).should(times(1)).setBaseMember(profileImage, validHost); + } + + @Test + @DisplayName("호스트 업데이트 테스트") + void updateHost() { + // given + Long newProfileImageId = 5L; + MemberImage newProfileImage = createValidMemberImage(newProfileImageId); + newProfileImage.setBaseMember(validHost); + HostEditRequest editRequest = HostEditRequest.builder() + .nickname(TestUtil.getOrNull("newNickname")) + .phoneNumber(TestUtil.getOrNull("010-1010-8888")) + .profileImageId(TestUtil.getOrNull(newProfileImageId)) + .intro(TestUtil.getOrNull("new intro")) + .hashtag(TestUtil.getOrNull("#new#hashtag")) + .build(); + + given(hostRepository.findById(validHost.getId())).willReturn(Optional.of(validHost)); + given(memberImageService.getById(newProfileImageId)).willReturn(newProfileImage); + + // when + hostService.updateHost(validHost.getId(), editRequest); + + // then + then(hostRepository).should(times(1)).findById(validHost.getId()); + int times = 1; + if (editRequest.getProfileImageId() == null) { + times = 0; + } + then(memberImageService).should(times(times)).getById(newProfileImageId); + } + + @Test + @DisplayName("호스트 삭제 테스트") + void deleteById() { + // given + given(hostRepository.findById(validHost.getId())).willReturn(Optional.of(validHost)); + + // when + hostService.deleteById(validHost.getId()); + + // then + then(hostRepository).should(times(1)).delete(validHost); + } + + @Test + @DisplayName("id로 사용자 응답 생성 테스트") + void getMemberResponseById() { + // given + given(hostRepository.findById(validHost.getId())).willReturn(Optional.of(validHost)); + + // when + hostService.getMemberResponseById(validHost.getId()); + + // then + then(hostRepository).should(times(1)).findById(validHost.getId()); + } + + @Test + @DisplayName("로그인 요청으로 사용자 응답 테스트") + void getMemberResponseBy() { + // given + String password = "host0password"; + LocalLoginRequest loginRequest = new LocalLoginRequest(validHost.getEmail(), password); + given(passwordEncoder.encode(password)).willReturn(validHost.getHashedPassword()); + given(hostRepository.findByEmailAndHashedPassword( + validHost.getEmail(), validHost.getHashedPassword())) + .willReturn(Optional.of(validHost)); + + // when + hostService.getMemberResponseBy(loginRequest); + + // then + then(passwordEncoder).should(times(1)).encode(password); + then(hostRepository).should(times(1)).findByEmailAndHashedPassword( + validHost.getEmail(), validHost.getHashedPassword()); + } + + @Test + @DisplayName("id로 호스트 얻어오기 테스트") + void getByIdOrThrow() { + // given + given(hostRepository.findById(validHost.getId())).willReturn(Optional.of(validHost)); + + // when + hostService.getByIdOrThrow(validHost.getId()); + + // then + then(hostRepository).should(times(1)).findById(validHost.getId()); + } + + @Test + @DisplayName("대상 사용자 유형 확인 테스트") + void getTargetMemberType() { + assertThat(hostService.getTargetMemberType()).isEqualTo(MemberType.host); + } + + @Test + @DisplayName("LikeableMemberService 얻어오기 테스트") + void getLikeableMemberService() { + assertThat(hostService.getLikeableMemberService()).isEqualTo(hostService); + } +} \ No newline at end of file diff --git a/src/test/java/org/ktc2/cokaen/wouldyouin/member/application/MemberServiceUnitTest.java b/src/test/java/org/ktc2/cokaen/wouldyouin/member/application/MemberServiceUnitTest.java index 691d0c1f..038ec16b 100644 --- a/src/test/java/org/ktc2/cokaen/wouldyouin/member/application/MemberServiceUnitTest.java +++ b/src/test/java/org/ktc2/cokaen/wouldyouin/member/application/MemberServiceUnitTest.java @@ -2,9 +2,9 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.ktc2.cokaen.wouldyouin.global.TestData.ImageDomain.createValidMemberImage; -import static org.ktc2.cokaen.wouldyouin.global.TestData.MemberDomain.createValidMember; -import static org.ktc2.cokaen.wouldyouin.global.TestData.MemberDomain.createValidWelcomeMember; +import static org.ktc2.cokaen.wouldyouin._global.TestData.ImageDomain.createValidMemberImage; +import static org.ktc2.cokaen.wouldyouin._global.TestData.MemberDomain.createValidMember; +import static org.ktc2.cokaen.wouldyouin._global.TestData.MemberDomain.createValidWelcomeMember; import static org.mockito.BDDMockito.given; import static org.mockito.BDDMockito.then; import static org.mockito.Mockito.never; @@ -17,7 +17,7 @@ import org.junit.jupiter.api.extension.ExtendWith; import org.ktc2.cokaen.wouldyouin.Image.application.MemberImageService; import org.ktc2.cokaen.wouldyouin.Image.persist.MemberImage; -import org.ktc2.cokaen.wouldyouin.global.TestUtil; +import org.ktc2.cokaen.wouldyouin._global.TestUtil; import org.ktc2.cokaen.wouldyouin.member.application.dto.request.MemberAdditionalInfoRequest; import org.ktc2.cokaen.wouldyouin.member.application.dto.request.create.MemberCreateRequest; import org.ktc2.cokaen.wouldyouin.member.application.dto.request.edit.MemberEditRequest;