From 74f13a51abcad06de6eadc3e60f860b999041730 Mon Sep 17 00:00:00 2001 From: amaran-th Date: Mon, 7 Aug 2023 22:02:56 +0900 Subject: [PATCH 1/5] =?UTF-8?q?test:=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20?= =?UTF-8?q?=EC=BD=94=EB=93=9C=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #232 --- .../member/exception/MemberExceptionType.java | 6 ++ .../MemberActivityServiceTest.java | 57 +++++++++++++++++++ 2 files changed, 63 insertions(+) diff --git a/backend/emm-sale/src/main/java/com/emmsale/member/exception/MemberExceptionType.java b/backend/emm-sale/src/main/java/com/emmsale/member/exception/MemberExceptionType.java index b846c4920..5c61feaa1 100644 --- a/backend/emm-sale/src/main/java/com/emmsale/member/exception/MemberExceptionType.java +++ b/backend/emm-sale/src/main/java/com/emmsale/member/exception/MemberExceptionType.java @@ -14,6 +14,12 @@ public enum MemberExceptionType implements BaseExceptionType { HttpStatus.BAD_REQUEST, "요청한 activity id들 중에 유효하지 않은 값이 존재합니다" ), + + ALREADY_EXIST_ACTIVITY( + HttpStatus.BAD_REQUEST, + "요청한 activity id는 이미 존재합니다." + ), + NULL_DESCRIPTION( HttpStatus.BAD_REQUEST, "한 줄 자기소개는 null이 될 수 없습니다." diff --git a/backend/emm-sale/src/test/java/com/emmsale/member/application/MemberActivityServiceTest.java b/backend/emm-sale/src/test/java/com/emmsale/member/application/MemberActivityServiceTest.java index ef4f4629e..b9d445a5a 100644 --- a/backend/emm-sale/src/test/java/com/emmsale/member/application/MemberActivityServiceTest.java +++ b/backend/emm-sale/src/test/java/com/emmsale/member/application/MemberActivityServiceTest.java @@ -17,6 +17,7 @@ import com.emmsale.member.exception.MemberException; import com.emmsale.member.exception.MemberExceptionType; import java.util.List; +import org.assertj.core.api.ThrowableAssert.ThrowingCallable; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; @@ -49,6 +50,31 @@ void registerActivities() throws Exception { ); } + @Test + @DisplayName("특정 사용자에 대해 이미 등록되어 있는 Activity를 등록하려고 하면 예외를 반환한다.") + void registerActivities_fail() throws Exception { + //given + final List activityIds = List.of(1L, 2L, 3L, 4L); + final long savedMemberId = 1L; + + final Member member = memberRepository.findById(savedMemberId).get(); + final String updateName = "우르"; + + final MemberActivityInitialRequest request = new MemberActivityInitialRequest(updateName, + activityIds); + + // when + memberActivityService.registerActivities(member, request); + final ThrowingCallable actual = () -> memberActivityService.registerActivities(member, + request); + + // then + assertThatThrownBy(actual) + .isInstanceOf(MemberException.class) + .hasMessage(MemberExceptionType.ALREADY_ONBOARDING.errorMessage()); + + } + @Test @DisplayName("Activity의 id를 통해서, 사용자의 Activity에 추가할 수 있다.") void addActivity() throws Exception { @@ -104,6 +130,37 @@ void test_addActivity_invalid_activity_ids_Exception() throws Exception { .hasMessage(MemberExceptionType.INVALID_ACTIVITY_IDS.errorMessage()); } + @Test + @DisplayName("addActivity() : 이미 존재하는 activityId들이 있으면 ALREADY_EXIST_ACTIVITY Exception이 발생합니다.") + void test_addActivity_ALREADY_EXIST_ACTIVITY_Exception_duplicate_try() throws Exception { + //given + final Member savedMember = memberRepository.findById(1L).get(); + final List activityIds = List.of(1L, 2L, 7L); + final MemberActivityAddRequest request = new MemberActivityAddRequest(activityIds); + + // when + memberActivityService.addActivity(savedMember, request); + + // then + assertThatThrownBy(() -> memberActivityService.addActivity(savedMember, request)) + .isInstanceOf(MemberException.class) + .hasMessage(MemberExceptionType.ALREADY_EXIST_ACTIVITY.errorMessage()); + } + + @Test + @DisplayName("addActivity() : 중복되는 Activity Id가 포함되어 있으면 ALREADY_EXIST_ACTIVITY Exception이 발생합니다.") + void test_addActivity_ALREADY_EXIST_ACTIVITY_Exception_duplicate_input() throws Exception { + //given + final Member savedMember = memberRepository.findById(1L).get(); + final List activityIds = List.of(1L, 2L, 1L); + final MemberActivityAddRequest request = new MemberActivityAddRequest(activityIds); + + // when, then + assertThatThrownBy(() -> memberActivityService.addActivity(savedMember, request)) + .isInstanceOf(MemberException.class) + .hasMessage(MemberExceptionType.ALREADY_EXIST_ACTIVITY.errorMessage()); + } + @Test @DisplayName("deleteActivity() : member Id와 삭제할 activity Id를 통해서 사용자의 Activity를 삭제할 수 있다.") void test_deleteActivity() throws Exception { From d02814e8c794281dd982ceeaa4415dfa1cc51524 Mon Sep 17 00:00:00 2001 From: amaran-th Date: Mon, 7 Aug 2023 22:47:59 +0900 Subject: [PATCH 2/5] =?UTF-8?q?fix:=20=ED=94=84=EB=A1=9C=ED=95=84=EC=97=90?= =?UTF-8?q?=20=EB=8F=99=EC=9D=BC=ED=95=9C=20=ED=99=9C=EB=8F=99=EC=9D=84=20?= =?UTF-8?q?=EC=A4=91=EB=B3=B5=ED=95=B4=EC=84=9C=20=EB=93=B1=EB=A1=9D?= =?UTF-8?q?=ED=95=A0=20=EC=88=98=20=EC=97=86=EB=8F=84=EB=A1=9D=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 온보딩을 완료한 사용자가 온보딩을 할 경우 예외 처리 - 활동 id 중 이미 존재하는 활동의 id가 포함되어 있을 경우 예외 반환 - 활동 id 목록에 중복 id가 포함되어 있을 경우 예외 반환 #232 --- .../application/MemberActivityService.java | 22 +++++++++++++++++++ .../member/exception/MemberExceptionType.java | 5 +++++ .../MemberActivityServiceTest.java | 10 +++------ .../emm-sale/src/test/resources/data-test.sql | 2 +- 4 files changed, 31 insertions(+), 8 deletions(-) diff --git a/backend/emm-sale/src/main/java/com/emmsale/member/application/MemberActivityService.java b/backend/emm-sale/src/main/java/com/emmsale/member/application/MemberActivityService.java index d5e1947e9..4aeb2e3b9 100644 --- a/backend/emm-sale/src/main/java/com/emmsale/member/application/MemberActivityService.java +++ b/backend/emm-sale/src/main/java/com/emmsale/member/application/MemberActivityService.java @@ -31,6 +31,9 @@ public void registerActivities( final Member member, final MemberActivityInitialRequest memberActivityInitialRequest ) { + if (member.isOnboarded()) { + throw new MemberException(MemberExceptionType.ALREADY_ONBOARDING); + } final List activityIds = memberActivityInitialRequest.getActivityIds(); saveMemberActivities(member, activityIds); @@ -62,11 +65,30 @@ public List addActivity( final MemberActivityAddRequest memberActivityAddRequest ) { final List activityIds = memberActivityAddRequest.getActivityIds(); + final List memberActivities = memberActivityRepository.findAllByMember(member); + if (isAlreadyExistActivity(memberActivities, activityIds) || hasDuplicateId(memberActivities, + activityIds)) { + throw new MemberException(MemberExceptionType.ALREADY_EXIST_ACTIVITY); + } saveMemberActivities(member, activityIds); return MemberActivityResponses.from(memberActivityRepository.findAllByMember(member)); } + private boolean isAlreadyExistActivity(final List memberActivities, + final List activityIds) { + return memberActivities + .stream() + .anyMatch(memberActivity -> + activityIds.contains(memberActivity.getActivity().getId()) + ); + } + + private boolean hasDuplicateId(final List memberActivities, + final List activityIds) { + return activityIds.stream().distinct().count() != memberActivities.size(); + } + public List deleteActivity( final Member member, final MemberActivityDeleteRequest memberActivityDeleteRequest diff --git a/backend/emm-sale/src/main/java/com/emmsale/member/exception/MemberExceptionType.java b/backend/emm-sale/src/main/java/com/emmsale/member/exception/MemberExceptionType.java index 5c61feaa1..9a98bb11a 100644 --- a/backend/emm-sale/src/main/java/com/emmsale/member/exception/MemberExceptionType.java +++ b/backend/emm-sale/src/main/java/com/emmsale/member/exception/MemberExceptionType.java @@ -10,6 +10,11 @@ public enum MemberExceptionType implements BaseExceptionType { "해당 멤버는 존재하지 않습니다." ), + ALREADY_ONBOARDING( + HttpStatus.BAD_REQUEST, + "이미 온보딩을 완료한 사용자입니다." + ), + INVALID_ACTIVITY_IDS( HttpStatus.BAD_REQUEST, "요청한 activity id들 중에 유효하지 않은 값이 존재합니다" diff --git a/backend/emm-sale/src/test/java/com/emmsale/member/application/MemberActivityServiceTest.java b/backend/emm-sale/src/test/java/com/emmsale/member/application/MemberActivityServiceTest.java index b9d445a5a..551ead007 100644 --- a/backend/emm-sale/src/test/java/com/emmsale/member/application/MemberActivityServiceTest.java +++ b/backend/emm-sale/src/test/java/com/emmsale/member/application/MemberActivityServiceTest.java @@ -108,7 +108,6 @@ void addActivity() throws Exception { //when final List actual = memberActivityService.addActivity(member, request); - //then assertThat(expected) .usingRecursiveComparison() @@ -121,7 +120,7 @@ void addActivity() throws Exception { void test_addActivity_invalid_activity_ids_Exception() throws Exception { //given final Member savedMember = memberRepository.findById(1L).get(); - final List activityIds = List.of(1L, 2L, 7L); + final List activityIds = List.of(4L, 5L, 7L); final MemberActivityAddRequest request = new MemberActivityAddRequest(activityIds); //when & then @@ -138,10 +137,7 @@ void test_addActivity_ALREADY_EXIST_ACTIVITY_Exception_duplicate_try() throws Ex final List activityIds = List.of(1L, 2L, 7L); final MemberActivityAddRequest request = new MemberActivityAddRequest(activityIds); - // when - memberActivityService.addActivity(savedMember, request); - - // then + // when, then assertThatThrownBy(() -> memberActivityService.addActivity(savedMember, request)) .isInstanceOf(MemberException.class) .hasMessage(MemberExceptionType.ALREADY_EXIST_ACTIVITY.errorMessage()); @@ -152,7 +148,7 @@ void test_addActivity_ALREADY_EXIST_ACTIVITY_Exception_duplicate_try() throws Ex void test_addActivity_ALREADY_EXIST_ACTIVITY_Exception_duplicate_input() throws Exception { //given final Member savedMember = memberRepository.findById(1L).get(); - final List activityIds = List.of(1L, 2L, 1L); + final List activityIds = List.of(4L, 4L, 5L); final MemberActivityAddRequest request = new MemberActivityAddRequest(activityIds); // when, then diff --git a/backend/emm-sale/src/test/resources/data-test.sql b/backend/emm-sale/src/test/resources/data-test.sql index 22365ae5a..4fa6785ae 100644 --- a/backend/emm-sale/src/test/resources/data-test.sql +++ b/backend/emm-sale/src/test/resources/data-test.sql @@ -28,7 +28,7 @@ insert into activity(id, type, name) values (6, 'JOB', 'Backend'); insert into member(id, name, image_url, open_profile_url, github_id, created_at, updated_at) -values (1, 'member1', 'https://imageurl.com', 'https://openprofileurl.com', 1, CURRENT_TIMESTAMP(), +values (1, null, 'https://imageurl.com', 'https://openprofileurl.com', 1, CURRENT_TIMESTAMP(), CURRENT_TIMESTAMP()); insert into member(id, name, image_url, open_profile_url, github_id, created_at, updated_at) From ad76664daecc78aa197b66ff9842f310ec7d0ef8 Mon Sep 17 00:00:00 2001 From: amaran-th Date: Tue, 8 Aug 2023 10:17:44 +0900 Subject: [PATCH 3/5] =?UTF-8?q?fix:=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20?= =?UTF-8?q?=EC=8B=A4=ED=8C=A8=20=EC=BD=94=EB=93=9C=20fix?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 사용자 정보가 변경됨에 따라 깨진 테스트 코드를 일부 수정하였음. #232 --- .../emmsale/member/application/MemberQueryServiceTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/backend/emm-sale/src/test/java/com/emmsale/member/application/MemberQueryServiceTest.java b/backend/emm-sale/src/test/java/com/emmsale/member/application/MemberQueryServiceTest.java index e26038ee8..2be30c077 100644 --- a/backend/emm-sale/src/test/java/com/emmsale/member/application/MemberQueryServiceTest.java +++ b/backend/emm-sale/src/test/java/com/emmsale/member/application/MemberQueryServiceTest.java @@ -24,7 +24,7 @@ class MemberQueryServiceTest extends ServiceIntegrationTestHelper { @DisplayName("사용자를 조회하고 조회 결과를 반환한다.") void findOrCreateMemberTest() { //given - final MemberQueryResponse expectResponse = new MemberQueryResponse(1L, true); + final MemberQueryResponse expectResponse = new MemberQueryResponse(1L, false); final GithubProfileResponse githubProfileFromGithub = new GithubProfileResponse("1", "name", "username", "https://imageUrl.com"); @@ -67,7 +67,7 @@ class FindProfile { void findProfile_success() { //given final Long memberId = 1L; - final MemberProfileResponse expectResponse = new MemberProfileResponse(memberId, "member1", + final MemberProfileResponse expectResponse = new MemberProfileResponse(memberId, null, "", "https://imageurl.com"); //when From a284a4661286586bd9373e690fd59c2cc3b77481 Mon Sep 17 00:00:00 2001 From: amaran-th Date: Tue, 8 Aug 2023 14:02:00 +0900 Subject: [PATCH 4/5] =?UTF-8?q?refactor:=20=EC=BB=A4=EC=8A=A4=ED=85=80=20?= =?UTF-8?q?=EC=98=88=EC=99=B8=20=EB=AC=B8=EA=B5=AC=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #232 --- .../java/com/emmsale/member/exception/MemberExceptionType.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/emm-sale/src/main/java/com/emmsale/member/exception/MemberExceptionType.java b/backend/emm-sale/src/main/java/com/emmsale/member/exception/MemberExceptionType.java index 9a98bb11a..f1fbb65de 100644 --- a/backend/emm-sale/src/main/java/com/emmsale/member/exception/MemberExceptionType.java +++ b/backend/emm-sale/src/main/java/com/emmsale/member/exception/MemberExceptionType.java @@ -22,7 +22,7 @@ public enum MemberExceptionType implements BaseExceptionType { ALREADY_EXIST_ACTIVITY( HttpStatus.BAD_REQUEST, - "요청한 activity id는 이미 존재합니다." + "이미 등록된 활동입니다." ), NULL_DESCRIPTION( From 14a321fb932ed71286a1d795c28ca6ff811339a4 Mon Sep 17 00:00:00 2001 From: amaran-th Date: Tue, 8 Aug 2023 15:37:47 +0900 Subject: [PATCH 5/5] =?UTF-8?q?refactor:=20distinct()=20=EC=97=B0=EC=82=B0?= =?UTF-8?q?=EC=9D=84=20HashSet=EC=9D=84=20=EC=82=AC=EC=9A=A9=ED=95=98?= =?UTF-8?q?=EB=8A=94=20=EA=B2=83=EC=9C=BC=EB=A1=9C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #232 --- .../member/application/MemberActivityService.java | 9 ++++++--- .../emmsale/member/exception/MemberExceptionType.java | 5 +++++ .../member/application/MemberActivityServiceTest.java | 4 ++-- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/backend/emm-sale/src/main/java/com/emmsale/member/application/MemberActivityService.java b/backend/emm-sale/src/main/java/com/emmsale/member/application/MemberActivityService.java index 4aeb2e3b9..128457ade 100644 --- a/backend/emm-sale/src/main/java/com/emmsale/member/application/MemberActivityService.java +++ b/backend/emm-sale/src/main/java/com/emmsale/member/application/MemberActivityService.java @@ -13,6 +13,7 @@ import com.emmsale.member.domain.MemberRepository; import com.emmsale.member.exception.MemberException; import com.emmsale.member.exception.MemberExceptionType; +import java.util.HashSet; import java.util.List; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; @@ -66,8 +67,10 @@ public List addActivity( ) { final List activityIds = memberActivityAddRequest.getActivityIds(); final List memberActivities = memberActivityRepository.findAllByMember(member); - if (isAlreadyExistActivity(memberActivities, activityIds) || hasDuplicateId(memberActivities, - activityIds)) { + if (hasDuplicateId(memberActivities, activityIds)) { + throw new MemberException(MemberExceptionType.DUPLICATE_ACTIVITY); + } + if (isAlreadyExistActivity(memberActivities, activityIds)) { throw new MemberException(MemberExceptionType.ALREADY_EXIST_ACTIVITY); } saveMemberActivities(member, activityIds); @@ -86,7 +89,7 @@ private boolean isAlreadyExistActivity(final List memberActiviti private boolean hasDuplicateId(final List memberActivities, final List activityIds) { - return activityIds.stream().distinct().count() != memberActivities.size(); + return new HashSet<>(activityIds).size() != memberActivities.size(); } public List deleteActivity( diff --git a/backend/emm-sale/src/main/java/com/emmsale/member/exception/MemberExceptionType.java b/backend/emm-sale/src/main/java/com/emmsale/member/exception/MemberExceptionType.java index f1fbb65de..9d98ed427 100644 --- a/backend/emm-sale/src/main/java/com/emmsale/member/exception/MemberExceptionType.java +++ b/backend/emm-sale/src/main/java/com/emmsale/member/exception/MemberExceptionType.java @@ -25,6 +25,11 @@ public enum MemberExceptionType implements BaseExceptionType { "이미 등록된 활동입니다." ), + DUPLICATE_ACTIVITY( + HttpStatus.BAD_REQUEST, + "요청에 중복된 활동 ID가 포함되어 있습니다." + ), + NULL_DESCRIPTION( HttpStatus.BAD_REQUEST, "한 줄 자기소개는 null이 될 수 없습니다." diff --git a/backend/emm-sale/src/test/java/com/emmsale/member/application/MemberActivityServiceTest.java b/backend/emm-sale/src/test/java/com/emmsale/member/application/MemberActivityServiceTest.java index 551ead007..c4a3d203a 100644 --- a/backend/emm-sale/src/test/java/com/emmsale/member/application/MemberActivityServiceTest.java +++ b/backend/emm-sale/src/test/java/com/emmsale/member/application/MemberActivityServiceTest.java @@ -34,7 +34,7 @@ class MemberActivityServiceTest extends ServiceIntegrationTestHelper { @DisplayName("Activity의 id를 통해서, 사용자의 Activity를 등록하고 사용자의 이름을 수정할 수 있다.") void registerActivities() throws Exception { //given - final List activityIds = List.of(1L, 2L, 3L, 4L); + final List activityIds = List.of(1L, 2L, 3L); final long savedMemberId = 1L; final Member member = memberRepository.findById(savedMemberId).get(); @@ -154,7 +154,7 @@ void test_addActivity_ALREADY_EXIST_ACTIVITY_Exception_duplicate_input() throws // when, then assertThatThrownBy(() -> memberActivityService.addActivity(savedMember, request)) .isInstanceOf(MemberException.class) - .hasMessage(MemberExceptionType.ALREADY_EXIST_ACTIVITY.errorMessage()); + .hasMessage(MemberExceptionType.DUPLICATE_ACTIVITY.errorMessage()); } @Test