From 05c0e93fa169e7468ee7b7d44993921837411b59 Mon Sep 17 00:00:00 2001 From: JunRain Date: Wed, 22 May 2024 18:19:03 +0900 Subject: [PATCH 01/13] =?UTF-8?q?[#90]=20MemberDto=20=ED=8C=A8=ED=82=A4?= =?UTF-8?q?=EC=A7=80=20=EA=B5=AC=EC=A1=B0=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../memetory/domain/member/controller/MemberApi.java | 4 ++-- .../domain/member/controller/MemberController.java | 6 +++--- .../MemberUpdateRequest.java} | 6 ++++-- .../member/dto/{ => response}/MemberResponse.java | 7 +++++-- .../domain/memes/dto/response/MemesResponse.java | 4 +--- .../com/example/memetory/domain/auth/AuthTest.java | 12 ++++++------ .../member/controller/MemberControllerTest.java | 6 +++--- .../domain/memes/service/MemesServiceTest.java | 2 +- 8 files changed, 25 insertions(+), 22 deletions(-) rename backend/memetory/src/main/java/com/example/memetory/domain/member/dto/{MemberUpdateDto.java => request/MemberUpdateRequest.java} (80%) rename backend/memetory/src/main/java/com/example/memetory/domain/member/dto/{ => response}/MemberResponse.java (73%) diff --git a/backend/memetory/src/main/java/com/example/memetory/domain/member/controller/MemberApi.java b/backend/memetory/src/main/java/com/example/memetory/domain/member/controller/MemberApi.java index ac9d8f45..bd04c59a 100644 --- a/backend/memetory/src/main/java/com/example/memetory/domain/member/controller/MemberApi.java +++ b/backend/memetory/src/main/java/com/example/memetory/domain/member/controller/MemberApi.java @@ -2,7 +2,7 @@ import org.springframework.http.ResponseEntity; -import com.example.memetory.domain.member.dto.MemberUpdateDto; +import com.example.memetory.domain.member.dto.request.MemberUpdateRequest; import com.example.memetory.global.response.ResultResponse; import io.swagger.v3.oas.annotations.Operation; @@ -30,5 +30,5 @@ public interface MemberApi { description = "닉네임 중복" )} ) - ResponseEntity updateMember(@Parameter(hidden = true) String email, MemberUpdateDto memberUpdateDto); + ResponseEntity updateMember(@Parameter(hidden = true) String email, MemberUpdateRequest memberUpdateRequest); } diff --git a/backend/memetory/src/main/java/com/example/memetory/domain/member/controller/MemberController.java b/backend/memetory/src/main/java/com/example/memetory/domain/member/controller/MemberController.java index 21781973..6317723c 100644 --- a/backend/memetory/src/main/java/com/example/memetory/domain/member/controller/MemberController.java +++ b/backend/memetory/src/main/java/com/example/memetory/domain/member/controller/MemberController.java @@ -9,7 +9,7 @@ import org.springframework.web.bind.annotation.RestController; import com.example.memetory.domain.member.dto.MemberServiceDto; -import com.example.memetory.domain.member.dto.MemberUpdateDto; +import com.example.memetory.domain.member.dto.request.MemberUpdateRequest; import com.example.memetory.domain.member.service.MemberService; import com.example.memetory.global.annotation.LoginMemberEmail; import com.example.memetory.global.response.ResultResponse; @@ -24,8 +24,8 @@ public class MemberController implements MemberApi { @PostMapping public ResponseEntity updateMember(@LoginMemberEmail String email, - @RequestBody MemberUpdateDto memberUpdateDto) { - MemberServiceDto memberServiceDto = memberUpdateDto.toServiceDto(email); + @RequestBody MemberUpdateRequest memberUpdateRequest) { + MemberServiceDto memberServiceDto = memberUpdateRequest.toServiceDto(email); memberService.update(memberServiceDto); diff --git a/backend/memetory/src/main/java/com/example/memetory/domain/member/dto/MemberUpdateDto.java b/backend/memetory/src/main/java/com/example/memetory/domain/member/dto/request/MemberUpdateRequest.java similarity index 80% rename from backend/memetory/src/main/java/com/example/memetory/domain/member/dto/MemberUpdateDto.java rename to backend/memetory/src/main/java/com/example/memetory/domain/member/dto/request/MemberUpdateRequest.java index 6b2efa2a..a373330b 100644 --- a/backend/memetory/src/main/java/com/example/memetory/domain/member/dto/MemberUpdateDto.java +++ b/backend/memetory/src/main/java/com/example/memetory/domain/member/dto/request/MemberUpdateRequest.java @@ -1,4 +1,6 @@ -package com.example.memetory.domain.member.dto; +package com.example.memetory.domain.member.dto.request; + +import com.example.memetory.domain.member.dto.MemberServiceDto; import io.swagger.v3.oas.annotations.media.Schema; import lombok.AccessLevel; @@ -10,7 +12,7 @@ @NoArgsConstructor(access = AccessLevel.PRIVATE) @AllArgsConstructor @Schema(description = "멤버 업데이트 포맷") -public class MemberUpdateDto { +public class MemberUpdateRequest { @Schema(description = "변경할 닉네임") private String nickname; @Schema(description = "변경할 이미지 S3 Url") diff --git a/backend/memetory/src/main/java/com/example/memetory/domain/member/dto/MemberResponse.java b/backend/memetory/src/main/java/com/example/memetory/domain/member/dto/response/MemberResponse.java similarity index 73% rename from backend/memetory/src/main/java/com/example/memetory/domain/member/dto/MemberResponse.java rename to backend/memetory/src/main/java/com/example/memetory/domain/member/dto/response/MemberResponse.java index 776aa590..4753ab14 100644 --- a/backend/memetory/src/main/java/com/example/memetory/domain/member/dto/MemberResponse.java +++ b/backend/memetory/src/main/java/com/example/memetory/domain/member/dto/response/MemberResponse.java @@ -1,4 +1,4 @@ -package com.example.memetory.domain.member.dto; +package com.example.memetory.domain.member.dto.response; import java.time.LocalDateTime; @@ -14,12 +14,14 @@ public class MemberResponse { private Long memberId; private String nickName; + private String imageUrl; private LocalDateTime createdAt; @Builder - public MemberResponse(Long memberId, String nickName, LocalDateTime createdAt) { + public MemberResponse(Long memberId, String nickName, String imageUrl, LocalDateTime createdAt) { this.memberId = memberId; this.nickName = nickName; + this.imageUrl = imageUrl; this.createdAt = createdAt; } @@ -27,6 +29,7 @@ public static MemberResponse of(Member member) { return MemberResponse.builder() .memberId(member.getId()) .nickName(member.getNickname()) + .imageUrl(member.getImageUrl()) .createdAt(member.getCreatedAt()) .build(); } diff --git a/backend/memetory/src/main/java/com/example/memetory/domain/memes/dto/response/MemesResponse.java b/backend/memetory/src/main/java/com/example/memetory/domain/memes/dto/response/MemesResponse.java index cc1b1821..66448b6c 100644 --- a/backend/memetory/src/main/java/com/example/memetory/domain/memes/dto/response/MemesResponse.java +++ b/backend/memetory/src/main/java/com/example/memetory/domain/memes/dto/response/MemesResponse.java @@ -1,10 +1,8 @@ package com.example.memetory.domain.memes.dto.response; import java.time.LocalDateTime; -import java.util.List; -import com.example.memetory.domain.comment.dto.CommentInfo; -import com.example.memetory.domain.member.dto.MemberResponse; +import com.example.memetory.domain.member.dto.response.MemberResponse; import com.example.memetory.domain.memes.entity.Memes; import io.swagger.v3.oas.annotations.media.Schema; diff --git a/backend/memetory/src/test/java/com/example/memetory/domain/auth/AuthTest.java b/backend/memetory/src/test/java/com/example/memetory/domain/auth/AuthTest.java index 530e03f9..e00f8f6a 100644 --- a/backend/memetory/src/test/java/com/example/memetory/domain/auth/AuthTest.java +++ b/backend/memetory/src/test/java/com/example/memetory/domain/auth/AuthTest.java @@ -19,7 +19,7 @@ import com.auth0.jwt.JWT; import com.auth0.jwt.algorithms.Algorithm; import com.example.memetory.domain.member.controller.MemberController; -import com.example.memetory.domain.member.dto.MemberUpdateDto; +import com.example.memetory.domain.member.dto.request.MemberUpdateRequest; import com.example.memetory.domain.member.service.MemberService; import com.example.memetory.global.LoginTest; import com.example.memetory.global.security.jwt.dto.TokenResponse; @@ -37,7 +37,7 @@ public class AuthTest extends LoginTest { public void accessToken_인가_성공_with_Bearer() throws Exception { // when final ResultActions perform = mockMvc.perform(post("/member").contentType(MediaType.APPLICATION_JSON) - .content(toRequestBody(new MemberUpdateDto("junrain2", "imageUrl2"))) + .content(toRequestBody(new MemberUpdateRequest("junrain2", "imageUrl2"))) .header("Authorization", "Bearer " + accessToken)); // then @@ -49,7 +49,7 @@ public class AuthTest extends LoginTest { public void accessToken_인가_성공() throws Exception { // when final ResultActions perform = mockMvc.perform(post("/member").contentType(MediaType.APPLICATION_JSON) - .content(toRequestBody(new MemberUpdateDto("junrain2", "imageUrl2"))) + .content(toRequestBody(new MemberUpdateRequest("junrain2", "imageUrl2"))) .header("Authorization", accessToken)); // then @@ -69,7 +69,7 @@ public class AuthTest extends LoginTest { // when final ResultActions perform = mockMvc.perform(post("/member").contentType(MediaType.APPLICATION_JSON) - .content(toRequestBody(new MemberUpdateDto("junrain2", "imageUrl2"))) + .content(toRequestBody(new MemberUpdateRequest("junrain2", "imageUrl2"))) .header("Authorization", accessToken)); // then @@ -96,7 +96,7 @@ public class AuthTest extends LoginTest { // when final ResultActions perform = mockMvc.perform(post("/member").contentType(MediaType.APPLICATION_JSON) - .content(toRequestBody(new MemberUpdateDto("junrain2", "imageUrl2"))) + .content(toRequestBody(new MemberUpdateRequest("junrain2", "imageUrl2"))) .header("Authorization-refresh", refreshToken)).andDo(print()); // then @@ -117,7 +117,7 @@ public class AuthTest extends LoginTest { // when final ResultActions perform = mockMvc.perform(post("/member").contentType(MediaType.APPLICATION_JSON) - .content(toRequestBody(new MemberUpdateDto("junrain2", "imageUrl2"))) + .content(toRequestBody(new MemberUpdateRequest("junrain2", "imageUrl2"))) .header("Authorization-refresh", refreshToken)).andDo(print()); // then diff --git a/backend/memetory/src/test/java/com/example/memetory/domain/member/controller/MemberControllerTest.java b/backend/memetory/src/test/java/com/example/memetory/domain/member/controller/MemberControllerTest.java index 86b7bfdc..71a11d7f 100644 --- a/backend/memetory/src/test/java/com/example/memetory/domain/member/controller/MemberControllerTest.java +++ b/backend/memetory/src/test/java/com/example/memetory/domain/member/controller/MemberControllerTest.java @@ -14,7 +14,7 @@ import org.springframework.http.MediaType; import org.springframework.test.web.servlet.ResultActions; -import com.example.memetory.domain.member.dto.MemberUpdateDto; +import com.example.memetory.domain.member.dto.request.MemberUpdateRequest; import com.example.memetory.domain.member.exception.DuplicatedMemberException; import com.example.memetory.domain.member.service.MemberService; import com.example.memetory.global.LoginTest; @@ -30,7 +30,7 @@ public class MemberControllerTest extends LoginTest { public void 멤버_업데이트_성공() throws Exception { // when final ResultActions perform = mockMvc.perform(post("/member").contentType(MediaType.APPLICATION_JSON) - .content(toRequestBody(new MemberUpdateDto("junrain2", "imageUrl2"))) + .content(toRequestBody(new MemberUpdateRequest("junrain2", "imageUrl2"))) .header("Authorization", "Bearer " + accessToken)).andDo(print()); // then @@ -45,7 +45,7 @@ public class MemberControllerTest extends LoginTest { // when final ResultActions perform = mockMvc.perform(post("/member").contentType(MediaType.APPLICATION_JSON) - .content(toRequestBody(new MemberUpdateDto("junrain", "imageUrl"))) + .content(toRequestBody(new MemberUpdateRequest("junrain", "imageUrl"))) .header("Authorization", "Bearer " + accessToken)).andDo(print()); // then diff --git a/backend/memetory/src/test/java/com/example/memetory/domain/memes/service/MemesServiceTest.java b/backend/memetory/src/test/java/com/example/memetory/domain/memes/service/MemesServiceTest.java index 8b69cd2b..f678b419 100644 --- a/backend/memetory/src/test/java/com/example/memetory/domain/memes/service/MemesServiceTest.java +++ b/backend/memetory/src/test/java/com/example/memetory/domain/memes/service/MemesServiceTest.java @@ -17,7 +17,7 @@ import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; -import com.example.memetory.domain.member.dto.MemberResponse; +import com.example.memetory.domain.member.dto.response.MemberResponse; import com.example.memetory.domain.member.entity.Member; import com.example.memetory.domain.member.service.MemberService; import com.example.memetory.domain.meme.entity.Meme; From a25d34964f245fae5c30bf7b55344e39ac568585 Mon Sep 17 00:00:00 2001 From: JunRain Date: Wed, 22 May 2024 18:29:21 +0900 Subject: [PATCH 02/13] =?UTF-8?q?[#90]=20MemberController=20=EB=A9=A4?= =?UTF-8?q?=EB=B2=84=20=EB=8B=A8=EC=9D=BC=20=EC=A1=B0=ED=9A=8C=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 --- .../domain/member/controller/MemberApi.java | 10 +++++++++- .../domain/member/controller/MemberController.java | 13 ++++++++++++- .../domain/member/dto/MemberServiceDto.java | 6 ++++++ .../member/dto/request/MemberUpdateRequest.java | 2 +- .../memetory/global/response/ResultCode.java | 5 +++-- 5 files changed, 31 insertions(+), 5 deletions(-) diff --git a/backend/memetory/src/main/java/com/example/memetory/domain/member/controller/MemberApi.java b/backend/memetory/src/main/java/com/example/memetory/domain/member/controller/MemberApi.java index bd04c59a..d0e70dcd 100644 --- a/backend/memetory/src/main/java/com/example/memetory/domain/member/controller/MemberApi.java +++ b/backend/memetory/src/main/java/com/example/memetory/domain/member/controller/MemberApi.java @@ -30,5 +30,13 @@ public interface MemberApi { description = "닉네임 중복" )} ) - ResponseEntity updateMember(@Parameter(hidden = true) String email, MemberUpdateRequest memberUpdateRequest); + ResponseEntity updateMember(@Parameter(hidden = true) String email, + MemberUpdateRequest memberUpdateRequest); + + @Operation( + summary = "단일 멤버 조회", + description = "로그인한 멤버의 프로필을 조회하기 위한 용도", + security = {@SecurityRequirement(name = "access_token")} + ) + ResponseEntity getMember(@Parameter(hidden = true) String email); } diff --git a/backend/memetory/src/main/java/com/example/memetory/domain/member/controller/MemberController.java b/backend/memetory/src/main/java/com/example/memetory/domain/member/controller/MemberController.java index 6317723c..9d0a8bf2 100644 --- a/backend/memetory/src/main/java/com/example/memetory/domain/member/controller/MemberController.java +++ b/backend/memetory/src/main/java/com/example/memetory/domain/member/controller/MemberController.java @@ -3,6 +3,7 @@ import static com.example.memetory.global.response.ResultCode.*; import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; @@ -10,6 +11,7 @@ import com.example.memetory.domain.member.dto.MemberServiceDto; import com.example.memetory.domain.member.dto.request.MemberUpdateRequest; +import com.example.memetory.domain.member.dto.response.MemberResponse; import com.example.memetory.domain.member.service.MemberService; import com.example.memetory.global.annotation.LoginMemberEmail; import com.example.memetory.global.response.ResultResponse; @@ -25,10 +27,19 @@ public class MemberController implements MemberApi { @PostMapping public ResponseEntity updateMember(@LoginMemberEmail String email, @RequestBody MemberUpdateRequest memberUpdateRequest) { - MemberServiceDto memberServiceDto = memberUpdateRequest.toServiceDto(email); + MemberServiceDto memberServiceDto = memberUpdateRequest.toServiceDtoFromEmail(email); memberService.update(memberServiceDto); return ResponseEntity.ok(ResultResponse.of(UPDATE_MEMBER_SUCCESS)); } + + @GetMapping + public ResponseEntity getMember(@LoginMemberEmail String email) { + MemberServiceDto memberServiceDto = MemberServiceDto.createFromEmail(email); + + MemberResponse response = memberService.findMemberResponseByEmail(memberServiceDto); + + return ResponseEntity.ok(ResultResponse.of(GET_MEMBER_SUCCESS, response)); + } } diff --git a/backend/memetory/src/main/java/com/example/memetory/domain/member/dto/MemberServiceDto.java b/backend/memetory/src/main/java/com/example/memetory/domain/member/dto/MemberServiceDto.java index 41023830..9624fc46 100644 --- a/backend/memetory/src/main/java/com/example/memetory/domain/member/dto/MemberServiceDto.java +++ b/backend/memetory/src/main/java/com/example/memetory/domain/member/dto/MemberServiceDto.java @@ -11,4 +11,10 @@ public class MemberServiceDto { private String email; private String nickname; private String imageUrl; + + public static MemberServiceDto createFromEmail(String email) { + return MemberServiceDto.builder() + .email(email) + .build(); + } } diff --git a/backend/memetory/src/main/java/com/example/memetory/domain/member/dto/request/MemberUpdateRequest.java b/backend/memetory/src/main/java/com/example/memetory/domain/member/dto/request/MemberUpdateRequest.java index a373330b..02981b08 100644 --- a/backend/memetory/src/main/java/com/example/memetory/domain/member/dto/request/MemberUpdateRequest.java +++ b/backend/memetory/src/main/java/com/example/memetory/domain/member/dto/request/MemberUpdateRequest.java @@ -18,7 +18,7 @@ public class MemberUpdateRequest { @Schema(description = "변경할 이미지 S3 Url") private String imageUrl; - public MemberServiceDto toServiceDto(String email) { + public MemberServiceDto toServiceDtoFromEmail(String email) { return MemberServiceDto.builder() .email(email) .nickname(this.nickname) diff --git a/backend/memetory/src/main/java/com/example/memetory/global/response/ResultCode.java b/backend/memetory/src/main/java/com/example/memetory/global/response/ResultCode.java index 34ecf75c..aa4341b5 100644 --- a/backend/memetory/src/main/java/com/example/memetory/global/response/ResultCode.java +++ b/backend/memetory/src/main/java/com/example/memetory/global/response/ResultCode.java @@ -9,6 +9,7 @@ public enum ResultCode { // member UPDATE_MEMBER_SUCCESS(200,"멤버 업데이트 성공"), + GET_MEMBER_SUCCESS(200, "단일 멤버 조회 성공"), // meme CREATE_MEME_SUCCESS(201, "밈 생성 성공"), @@ -39,9 +40,9 @@ public enum ResultCode { // voice CREATE_VOICE_SUCCESS(201, "목소리 생성 성공"), GET_MEMBER_VOICE_SUCCESS(200, "멤버별 목소리 조회 성공"), - GET_VOICE_LIBRARY_SUCCESS(200, "기본 목소리 라이브러리 조회 성공") - + GET_VOICE_LIBRARY_SUCCESS(200, "기본 목소리 라이브러리 조회 성공"), ; + private final int status; private final String message; } From 07da259eff9e5a2d9a598425ce3a22cc60534744 Mon Sep 17 00:00:00 2001 From: JunRain Date: Wed, 22 May 2024 18:54:56 +0900 Subject: [PATCH 03/13] =?UTF-8?q?[#90]=20MemberService=20=EB=8B=A8?= =?UTF-8?q?=EC=9D=BC=20=EB=A9=A4=EB=B2=84=20=EC=A1=B0=ED=9A=8C=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 --- .../comment/service/CommentService.java | 2 +- .../complain/service/ComplainService.java | 2 +- .../domain/like/service/LikeService.java | 4 ++-- .../member/controller/MemberController.java | 4 ++-- .../repository/MemberQueryRepository.java | 2 +- .../repository/MemberQueryRepositoryImpl.java | 2 +- .../domain/member/service/MemberService.java | 22 +++++++++++++------ .../domain/meme/service/MemeService.java | 8 +++---- .../domain/memes/service/MemesService.java | 4 ++-- .../domain/voice/service/VoiceService.java | 4 ++-- .../domain/like/service/LikeServiceTest.java | 4 ++-- .../controller/MemberControllerTest.java | 2 +- .../repository/MemberRepositoryTest.java | 2 +- .../member/service/MemberServiceTest.java | 16 +++++++------- .../domain/meme/service/MemeServiceTest.java | 4 ++-- .../memes/service/MemesServiceTest.java | 6 ++--- 16 files changed, 48 insertions(+), 40 deletions(-) diff --git a/backend/memetory/src/main/java/com/example/memetory/domain/comment/service/CommentService.java b/backend/memetory/src/main/java/com/example/memetory/domain/comment/service/CommentService.java index 209f04e1..1edb3097 100644 --- a/backend/memetory/src/main/java/com/example/memetory/domain/comment/service/CommentService.java +++ b/backend/memetory/src/main/java/com/example/memetory/domain/comment/service/CommentService.java @@ -22,7 +22,7 @@ public class CommentService { @Transactional public void register(CommentServiceDto commentServiceDto) { - Member foundMember = memberService.findByEmail(commentServiceDto.getEmail()); + Member foundMember = memberService.findMemberFromEmail(commentServiceDto.getEmail()); Memes foundMemes = memesService.getMemesBetweenService(commentServiceDto.getMemesId()); foundMemes.addCommentCount(); diff --git a/backend/memetory/src/main/java/com/example/memetory/domain/complain/service/ComplainService.java b/backend/memetory/src/main/java/com/example/memetory/domain/complain/service/ComplainService.java index 249eb99a..e8d174fb 100644 --- a/backend/memetory/src/main/java/com/example/memetory/domain/complain/service/ComplainService.java +++ b/backend/memetory/src/main/java/com/example/memetory/domain/complain/service/ComplainService.java @@ -22,7 +22,7 @@ public class ComplainService { @Transactional public void register(ComplainServiceDto complainServiceDto) { - Member foundMember = memberService.findByEmail(complainServiceDto.getEmail()); + Member foundMember = memberService.findMemberFromEmail(complainServiceDto.getEmail()); Memes foundMemes = memesService.getMemesBetweenService(complainServiceDto.getMemesId()); Complain newComplain = complainServiceDto.toEntity(foundMember, foundMemes); diff --git a/backend/memetory/src/main/java/com/example/memetory/domain/like/service/LikeService.java b/backend/memetory/src/main/java/com/example/memetory/domain/like/service/LikeService.java index bff309f8..6bbe5d5f 100644 --- a/backend/memetory/src/main/java/com/example/memetory/domain/like/service/LikeService.java +++ b/backend/memetory/src/main/java/com/example/memetory/domain/like/service/LikeService.java @@ -22,7 +22,7 @@ public class LikeService { @Transactional public void register(LikeServiceDto likeServiceDto) { - Member member = memberService.findByEmail(likeServiceDto.getEmail()); + Member member = memberService.findMemberFromEmail(likeServiceDto.getEmail()); Memes memes = memesService.getMemesBetweenService(likeServiceDto.getMemesId()); memes.addLikeCount(); @@ -32,7 +32,7 @@ public void register(LikeServiceDto likeServiceDto) { @Transactional public void cancel(LikeServiceDto likeServiceDto) { - Member member = memberService.findByEmail(likeServiceDto.getEmail()); + Member member = memberService.findMemberFromEmail(likeServiceDto.getEmail()); Memes memes = memesService.getMemesBetweenService(likeServiceDto.getMemesId()); memes.cancelLikeCount(); diff --git a/backend/memetory/src/main/java/com/example/memetory/domain/member/controller/MemberController.java b/backend/memetory/src/main/java/com/example/memetory/domain/member/controller/MemberController.java index 9d0a8bf2..fcd236e2 100644 --- a/backend/memetory/src/main/java/com/example/memetory/domain/member/controller/MemberController.java +++ b/backend/memetory/src/main/java/com/example/memetory/domain/member/controller/MemberController.java @@ -29,7 +29,7 @@ public ResponseEntity updateMember(@LoginMemberEmail String emai @RequestBody MemberUpdateRequest memberUpdateRequest) { MemberServiceDto memberServiceDto = memberUpdateRequest.toServiceDtoFromEmail(email); - memberService.update(memberServiceDto); + memberService.updateMember(memberServiceDto); return ResponseEntity.ok(ResultResponse.of(UPDATE_MEMBER_SUCCESS)); } @@ -38,7 +38,7 @@ public ResponseEntity updateMember(@LoginMemberEmail String emai public ResponseEntity getMember(@LoginMemberEmail String email) { MemberServiceDto memberServiceDto = MemberServiceDto.createFromEmail(email); - MemberResponse response = memberService.findMemberResponseByEmail(memberServiceDto); + MemberResponse response = memberService.findMemberResponse(memberServiceDto); return ResponseEntity.ok(ResultResponse.of(GET_MEMBER_SUCCESS, response)); } diff --git a/backend/memetory/src/main/java/com/example/memetory/domain/member/repository/MemberQueryRepository.java b/backend/memetory/src/main/java/com/example/memetory/domain/member/repository/MemberQueryRepository.java index 63e044a7..9eec1c28 100644 --- a/backend/memetory/src/main/java/com/example/memetory/domain/member/repository/MemberQueryRepository.java +++ b/backend/memetory/src/main/java/com/example/memetory/domain/member/repository/MemberQueryRepository.java @@ -10,5 +10,5 @@ public interface MemberQueryRepository { Optional findByEmail(String email); - boolean existsMemberByNickname(String email); + boolean existMemberByNickname(String email); } diff --git a/backend/memetory/src/main/java/com/example/memetory/domain/member/repository/MemberQueryRepositoryImpl.java b/backend/memetory/src/main/java/com/example/memetory/domain/member/repository/MemberQueryRepositoryImpl.java index ec349da7..39375d00 100644 --- a/backend/memetory/src/main/java/com/example/memetory/domain/member/repository/MemberQueryRepositoryImpl.java +++ b/backend/memetory/src/main/java/com/example/memetory/domain/member/repository/MemberQueryRepositoryImpl.java @@ -30,7 +30,7 @@ public Optional findByEmail(String email) { } @Override - public boolean existsMemberByNickname(String nickname) { + public boolean existMemberByNickname(String nickname) { return jpaQueryFactory.selectFrom(member).where(member.nickname.eq(nickname)).fetchOne() != null; } } diff --git a/backend/memetory/src/main/java/com/example/memetory/domain/member/service/MemberService.java b/backend/memetory/src/main/java/com/example/memetory/domain/member/service/MemberService.java index 7ef65c86..bb131785 100644 --- a/backend/memetory/src/main/java/com/example/memetory/domain/member/service/MemberService.java +++ b/backend/memetory/src/main/java/com/example/memetory/domain/member/service/MemberService.java @@ -4,6 +4,7 @@ import org.springframework.transaction.annotation.Transactional; import com.example.memetory.domain.member.dto.MemberServiceDto; +import com.example.memetory.domain.member.dto.response.MemberResponse; import com.example.memetory.domain.member.entity.Member; import com.example.memetory.domain.member.exception.DuplicatedMemberException; import com.example.memetory.domain.member.exception.NotFoundMemberException; @@ -19,20 +20,27 @@ public class MemberService { private final MemberRepository memberRepository; @Transactional - public void update(MemberServiceDto memberServiceDto) { - if (memberRepository.existsMemberByNickname(memberServiceDto.getNickname())) { + public void updateMember(MemberServiceDto memberServiceDto) { + if (memberRepository.existMemberByNickname(memberServiceDto.getNickname())) { throw new DuplicatedMemberException(); } - findByEmail(memberServiceDto.getEmail()).update(memberServiceDto); + findMemberFromEmail(memberServiceDto.getEmail()).update(memberServiceDto); } @Transactional(readOnly = true) - public Member findByEmail(String email) { - return memberRepository.findByEmail(email).orElseThrow(NotFoundMemberException::new); + public Member findMemberFromId(Long id) { + return memberRepository.findById(id).orElseThrow(NotFoundMemberException::new); } @Transactional(readOnly = true) - public Member findById(Long id) { - return memberRepository.findById(id).orElseThrow(NotFoundMemberException::new); + public MemberResponse findMemberResponse(MemberServiceDto memberServiceDto) { + Member member = findMemberFromEmail(memberServiceDto.getEmail()); + + return MemberResponse.of(member); + } + + @Transactional(readOnly = true) + public Member findMemberFromEmail(String email) { + return memberRepository.findByEmail(email).orElseThrow(NotFoundMemberException::new); } } diff --git a/backend/memetory/src/main/java/com/example/memetory/domain/meme/service/MemeService.java b/backend/memetory/src/main/java/com/example/memetory/domain/meme/service/MemeService.java index e712a13c..5cb86c15 100644 --- a/backend/memetory/src/main/java/com/example/memetory/domain/meme/service/MemeService.java +++ b/backend/memetory/src/main/java/com/example/memetory/domain/meme/service/MemeService.java @@ -27,7 +27,7 @@ public class MemeService { @Transactional public MemeResponse register(MemeServiceDto memeServiceDto) { - Member member = memberService.findById(memeServiceDto.getMemberId()); + Member member = memberService.findMemberFromId(memeServiceDto.getMemberId()); Meme meme = memeServiceDto.toEntity(member); return MemeResponse.of(memeRepository.save(meme)); @@ -37,7 +37,7 @@ public MemeResponse register(MemeServiceDto memeServiceDto) { public String getAIServerSendJson(MemeServiceDto memeServiceDto) { Gson gson = new Gson(); - Member member = memberService.findByEmail(memeServiceDto.getEmail()); + Member member = memberService.findMemberFromEmail(memeServiceDto.getEmail()); AIServerSendDto aiServerSendDto = AIServerSendDto.builder() .memberId(member.getId()) @@ -49,7 +49,7 @@ public String getAIServerSendJson(MemeServiceDto memeServiceDto) { @Transactional(readOnly = true) public MemeResponse getMeme(MemeServiceDto memeServiceDto) { - Member member = memberService.findById(memeServiceDto.getMemberId()); + Member member = memberService.findMemberFromId(memeServiceDto.getMemberId()); Meme meme = memeRepository.findById(memeServiceDto.getMemeId()).orElseThrow(NotFoundMemeException::new); if (meme.getMember() != member) { @@ -61,7 +61,7 @@ public MemeResponse getMeme(MemeServiceDto memeServiceDto) { @Transactional public MemePageResponse getAllMeme(MemeServiceDto memeServiceDto, Pageable pageable) { - Member member = memberService.findByEmail(memeServiceDto.getEmail()); + Member member = memberService.findMemberFromEmail(memeServiceDto.getEmail()); Page memeList = memeRepository.findAllByMember(member, pageable); diff --git a/backend/memetory/src/main/java/com/example/memetory/domain/memes/service/MemesService.java b/backend/memetory/src/main/java/com/example/memetory/domain/memes/service/MemesService.java index a96dcd05..23c19964 100644 --- a/backend/memetory/src/main/java/com/example/memetory/domain/memes/service/MemesService.java +++ b/backend/memetory/src/main/java/com/example/memetory/domain/memes/service/MemesService.java @@ -33,7 +33,7 @@ public class MemesService { @Transactional public void register(MemesServiceDto memesServiceDto) { - Member member = memberService.findByEmail(memesServiceDto.getEmail()); + Member member = memberService.findMemberFromEmail(memesServiceDto.getEmail()); Meme meme = memeService.getMemeBetweenService(memesServiceDto.getMemeId()); Memes newMemes = memesServiceDto.toEntity(member, meme); @@ -43,7 +43,7 @@ public void register(MemesServiceDto memesServiceDto) { @Transactional public void delete(MemesServiceDto memesServiceDto) { Memes memes = findById(memesServiceDto.getMemesId()); - if (memes.getMember() != memberService.findByEmail(memesServiceDto.getEmail())) { + if (memes.getMember() != memberService.findMemberFromEmail(memesServiceDto.getEmail())) { throw new NotDeleteMemesException(); } memesRepository.delete(memes); diff --git a/backend/memetory/src/main/java/com/example/memetory/domain/voice/service/VoiceService.java b/backend/memetory/src/main/java/com/example/memetory/domain/voice/service/VoiceService.java index 9e6769ea..ff5a6b9b 100644 --- a/backend/memetory/src/main/java/com/example/memetory/domain/voice/service/VoiceService.java +++ b/backend/memetory/src/main/java/com/example/memetory/domain/voice/service/VoiceService.java @@ -32,7 +32,7 @@ public class VoiceService { @Transactional public void register(VoiceServiceDto voiceServiceDto) { - Member foundMember = memberService.findByEmail(voiceServiceDto.getEmail()); + Member foundMember = memberService.findMemberFromEmail(voiceServiceDto.getEmail()); Voice newVoice = voiceServiceDto.toEntity(foundMember); voiceRepository.save(newVoice); @@ -40,7 +40,7 @@ public void register(VoiceServiceDto voiceServiceDto) { @Transactional(readOnly = true) public String findVoiceByMemberId(VoiceServiceDto voiceServiceDto) { - Member foundMember = memberService.findByEmail(voiceServiceDto.getEmail()); + Member foundMember = memberService.findMemberFromEmail(voiceServiceDto.getEmail()); Voice foundVoice = voiceRepository.findByMemberId(foundMember.getId()).orElseThrow(NotFoundVoiceException::new); return foundVoice.getElevenlabsVoiceId(); diff --git a/backend/memetory/src/test/java/com/example/memetory/domain/like/service/LikeServiceTest.java b/backend/memetory/src/test/java/com/example/memetory/domain/like/service/LikeServiceTest.java index 2c25a09b..6c6e9533 100644 --- a/backend/memetory/src/test/java/com/example/memetory/domain/like/service/LikeServiceTest.java +++ b/backend/memetory/src/test/java/com/example/memetory/domain/like/service/LikeServiceTest.java @@ -52,7 +52,7 @@ void setUp() { @DisplayName("register 성공 테스트") void 좋아요_등록() { // given - given(memberService.findByEmail(likeServiceDto.getEmail())).willReturn(member); + given(memberService.findMemberFromEmail(likeServiceDto.getEmail())).willReturn(member); given(memesService.getMemesBetweenService(likeServiceDto.getMemesId())).willReturn(memes); // when @@ -68,7 +68,7 @@ void setUp() { void 좋아요_취소() { // given Like like = LIKE(member, memes); - given(memberService.findByEmail(likeServiceDto.getEmail())).willReturn(member); + given(memberService.findMemberFromEmail(likeServiceDto.getEmail())).willReturn(member); given(memesService.getMemesBetweenService(likeServiceDto.getMemesId())).willReturn(memes); given(likeRepository.findLikeByMemberAndMemes(member, memes)).willReturn(Optional.ofNullable(like)); diff --git a/backend/memetory/src/test/java/com/example/memetory/domain/member/controller/MemberControllerTest.java b/backend/memetory/src/test/java/com/example/memetory/domain/member/controller/MemberControllerTest.java index 71a11d7f..fa7274b9 100644 --- a/backend/memetory/src/test/java/com/example/memetory/domain/member/controller/MemberControllerTest.java +++ b/backend/memetory/src/test/java/com/example/memetory/domain/member/controller/MemberControllerTest.java @@ -41,7 +41,7 @@ public class MemberControllerTest extends LoginTest { @DisplayName("멤버 업데이트 실패(닉네임 중복)") public void 멤버_업데이트_실패_닉네임_중복() throws Exception { // given -> 결과에 대한 객체, Member 객체 저장할 필요 존재 - doThrow(new DuplicatedMemberException()).when(memberService).update(any()); + doThrow(new DuplicatedMemberException()).when(memberService).updateMember(any()); // when final ResultActions perform = mockMvc.perform(post("/member").contentType(MediaType.APPLICATION_JSON) diff --git a/backend/memetory/src/test/java/com/example/memetory/domain/member/repository/MemberRepositoryTest.java b/backend/memetory/src/test/java/com/example/memetory/domain/member/repository/MemberRepositoryTest.java index f1118771..ef714fa4 100644 --- a/backend/memetory/src/test/java/com/example/memetory/domain/member/repository/MemberRepositoryTest.java +++ b/backend/memetory/src/test/java/com/example/memetory/domain/member/repository/MemberRepositoryTest.java @@ -56,7 +56,7 @@ public class MemberRepositoryTest { Member savedMember = memberRepository.save(MEMBER()); // then 확인하기 - assertTrue(memberRepository.existsMemberByNickname(savedMember.getNickname())); + assertTrue(memberRepository.existMemberByNickname(savedMember.getNickname())); } @Test diff --git a/backend/memetory/src/test/java/com/example/memetory/domain/member/service/MemberServiceTest.java b/backend/memetory/src/test/java/com/example/memetory/domain/member/service/MemberServiceTest.java index f2e6a054..0d43dc9f 100644 --- a/backend/memetory/src/test/java/com/example/memetory/domain/member/service/MemberServiceTest.java +++ b/backend/memetory/src/test/java/com/example/memetory/domain/member/service/MemberServiceTest.java @@ -44,7 +44,7 @@ void setUp() { // when 실행 when(memberRepository.findByEmail(email)).thenReturn(Optional.ofNullable(member)); - Member result = memberService.findByEmail(email); + Member result = memberService.findMemberFromEmail(email); // then assertThat(result).isEqualTo(member); @@ -58,7 +58,7 @@ void setUp() { given(memberRepository.findByEmail(any())).willReturn(Optional.empty()); // then - assertThrows(NotFoundMemberException.class, () -> memberService.findByEmail(email)); + assertThrows(NotFoundMemberException.class, () -> memberService.findMemberFromEmail(email)); } @Test @@ -69,7 +69,7 @@ void setUp() { // when 실행 when(memberRepository.findById(id)).thenReturn(Optional.ofNullable(member)); - Member result = memberService.findById(id); + Member result = memberService.findMemberFromId(id); // then assertThat(result).isEqualTo(member); @@ -83,7 +83,7 @@ void setUp() { given(memberRepository.findById(id)).willReturn(Optional.empty()); // then - assertThrows(NotFoundMemberException.class, () -> memberService.findById(id)); + assertThrows(NotFoundMemberException.class, () -> memberService.findMemberFromId(id)); } @Test @@ -92,9 +92,9 @@ void setUp() { MemberServiceDto memberServiceDto = UPDATE_MEMBER_SERVICE_DTO(); // when 멤버 업데이트 - when(memberRepository.existsMemberByNickname(anyString())).thenReturn(false); + when(memberRepository.existMemberByNickname(anyString())).thenReturn(false); when(memberRepository.findByEmail(memberServiceDto.getEmail())).thenReturn(Optional.ofNullable(member)); - memberService.update(memberServiceDto); + memberService.updateMember(memberServiceDto); // then assertThat(member.getNickname()).isEqualTo(memberServiceDto.getNickname()); @@ -106,9 +106,9 @@ void setUp() { MemberServiceDto memberServiceDto = UPDATE_MEMBER_SERVICE_DTO(); // when 멤버 업데이트 - when(memberRepository.existsMemberByNickname(anyString())).thenReturn(true); + when(memberRepository.existMemberByNickname(anyString())).thenReturn(true); // then - assertThrows(DuplicatedMemberException.class, () -> memberService.update(memberServiceDto)); + assertThrows(DuplicatedMemberException.class, () -> memberService.updateMember(memberServiceDto)); } } diff --git a/backend/memetory/src/test/java/com/example/memetory/domain/meme/service/MemeServiceTest.java b/backend/memetory/src/test/java/com/example/memetory/domain/meme/service/MemeServiceTest.java index 21f6c3b3..3f39551b 100644 --- a/backend/memetory/src/test/java/com/example/memetory/domain/meme/service/MemeServiceTest.java +++ b/backend/memetory/src/test/java/com/example/memetory/domain/meme/service/MemeServiceTest.java @@ -57,7 +57,7 @@ void setUp() { MemeServiceDto memeServiceDto = MEME_SERVICE_DTO(); // when - when(memberService.findById(memeServiceDto.getMemberId())).thenReturn(member); + when(memberService.findMemberFromId(memeServiceDto.getMemberId())).thenReturn(member); when(memeRepository.save(any())).thenReturn(meme); MemeResponse result = memeService.register(memeServiceDto); @@ -75,7 +75,7 @@ void setUp() { // when when(memeRepository.findById(memeServiceDto.getMemeId())).thenReturn(Optional.ofNullable(meme)); - when(memberService.findById(memeServiceDto.getMemberId())).thenReturn(member); + when(memberService.findMemberFromId(memeServiceDto.getMemberId())).thenReturn(member); MemeResponse result = memeService.getMeme(memeServiceDto); // given diff --git a/backend/memetory/src/test/java/com/example/memetory/domain/memes/service/MemesServiceTest.java b/backend/memetory/src/test/java/com/example/memetory/domain/memes/service/MemesServiceTest.java index f678b419..01fb4487 100644 --- a/backend/memetory/src/test/java/com/example/memetory/domain/memes/service/MemesServiceTest.java +++ b/backend/memetory/src/test/java/com/example/memetory/domain/memes/service/MemesServiceTest.java @@ -57,7 +57,7 @@ void setUp() { @DisplayName("밈스 저장 성공") void 밈스_저장() { // when - when(memberService.findByEmail(memesServiceDto.getEmail())).thenReturn(member); + when(memberService.findMemberFromEmail(memesServiceDto.getEmail())).thenReturn(member); when(memeService.getMemeBetweenService(memesServiceDto.getMemeId())).thenReturn(meme); memesService.register(memesServiceDto); @@ -71,7 +71,7 @@ void setUp() { void 밈스_삭제() { // given given(memesRepository.findByMemesId(any())).willReturn(Optional.ofNullable(memes)); - given(memberService.findByEmail(any())).willReturn(member); + given(memberService.findMemberFromEmail(any())).willReturn(member); // when memesService.delete(memesServiceDto); @@ -85,7 +85,7 @@ void setUp() { void 밈스_삭제_실패() { // given given(memesRepository.findByMemesId(any())).willReturn(Optional.ofNullable(memes)); - given(memberService.findByEmail(any())).willReturn(any()); + given(memberService.findMemberFromEmail(any())).willReturn(any()); // when assertThrows(NotDeleteMemesException.class, () -> memesService.delete(memesServiceDto)); From 55e1f1d204464b91aed8e33eb6fd39dc1c53e536 Mon Sep 17 00:00:00 2001 From: JunRain Date: Wed, 22 May 2024 19:05:15 +0900 Subject: [PATCH 04/13] =?UTF-8?q?[#90]=20=EB=8B=A8=EC=9D=BC=20=EB=A9=A4?= =?UTF-8?q?=EB=B2=84=20=EC=A1=B0=ED=9A=8C=20Test=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../member/controller/MemberControllerTest.java | 14 ++++++++++++++ .../domain/member/service/MemberServiceTest.java | 16 ++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/backend/memetory/src/test/java/com/example/memetory/domain/member/controller/MemberControllerTest.java b/backend/memetory/src/test/java/com/example/memetory/domain/member/controller/MemberControllerTest.java index fa7274b9..ad4c9b03 100644 --- a/backend/memetory/src/test/java/com/example/memetory/domain/member/controller/MemberControllerTest.java +++ b/backend/memetory/src/test/java/com/example/memetory/domain/member/controller/MemberControllerTest.java @@ -52,4 +52,18 @@ public class MemberControllerTest extends LoginTest { perform.andExpect(status().isConflict()) .andExpect(jsonPath(ERROR_MESSAGE).value(NICKNAME_IS_DUPLICATED.getMessage())); } + + @Test + @DisplayName("단일 멤버 조회 성공") + public void 단일_멤버_조회_성공() throws Exception { + // given + given(memberService.findMemberFromEmail(anyString())).willReturn(loginMember); + + // when + final ResultActions perform = mockMvc.perform(get("/member").contentType(MediaType.APPLICATION_JSON) + .header("Authorization", "Bearer " + accessToken)).andDo(print()); + + // then + perform.andExpect(status().isOk()).andExpect(jsonPath(MESSAGE).value(GET_MEMBER_SUCCESS.getMessage())); + } } diff --git a/backend/memetory/src/test/java/com/example/memetory/domain/member/service/MemberServiceTest.java b/backend/memetory/src/test/java/com/example/memetory/domain/member/service/MemberServiceTest.java index 0d43dc9f..3a7d7f7f 100644 --- a/backend/memetory/src/test/java/com/example/memetory/domain/member/service/MemberServiceTest.java +++ b/backend/memetory/src/test/java/com/example/memetory/domain/member/service/MemberServiceTest.java @@ -16,6 +16,7 @@ import org.mockito.junit.jupiter.MockitoExtension; import com.example.memetory.domain.member.dto.MemberServiceDto; +import com.example.memetory.domain.member.dto.response.MemberResponse; import com.example.memetory.domain.member.entity.Member; import com.example.memetory.domain.member.exception.DuplicatedMemberException; import com.example.memetory.domain.member.exception.NotFoundMemberException; @@ -111,4 +112,19 @@ void setUp() { // then assertThrows(DuplicatedMemberException.class, () -> memberService.updateMember(memberServiceDto)); } + + @Test + @DisplayName("단일 멤버 조회 성공") + void member_조회_성공() { + // given + MemberServiceDto memberServiceDto = MEMBER_SERVICE_DTO(); + MemberResponse expectation = MemberResponse.of(member); + + // when + when(memberRepository.findByEmail(anyString())).thenReturn(Optional.ofNullable(member)); + MemberResponse result = memberService.findMemberResponse(memberServiceDto); + + // then + assertThat(expectation).usingRecursiveComparison().isEqualTo(result); + } } From 934c5d7c20bd0b0429128c6435e24c31f880a2a7 Mon Sep 17 00:00:00 2001 From: JunRain Date: Wed, 22 May 2024 23:41:45 +0900 Subject: [PATCH 05/13] =?UTF-8?q?[#90]=20MemberController=20findMember?= =?UTF-8?q?=EB=A1=9C=20=EB=A9=94=EC=84=9C=EB=93=9C=EB=AA=85=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../example/memetory/domain/member/controller/MemberApi.java | 2 +- .../memetory/domain/member/controller/MemberController.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/backend/memetory/src/main/java/com/example/memetory/domain/member/controller/MemberApi.java b/backend/memetory/src/main/java/com/example/memetory/domain/member/controller/MemberApi.java index d0e70dcd..e2ab3355 100644 --- a/backend/memetory/src/main/java/com/example/memetory/domain/member/controller/MemberApi.java +++ b/backend/memetory/src/main/java/com/example/memetory/domain/member/controller/MemberApi.java @@ -38,5 +38,5 @@ ResponseEntity updateMember(@Parameter(hidden = true) String ema description = "로그인한 멤버의 프로필을 조회하기 위한 용도", security = {@SecurityRequirement(name = "access_token")} ) - ResponseEntity getMember(@Parameter(hidden = true) String email); + ResponseEntity findMember(@Parameter(hidden = true) String email); } diff --git a/backend/memetory/src/main/java/com/example/memetory/domain/member/controller/MemberController.java b/backend/memetory/src/main/java/com/example/memetory/domain/member/controller/MemberController.java index fcd236e2..73a18746 100644 --- a/backend/memetory/src/main/java/com/example/memetory/domain/member/controller/MemberController.java +++ b/backend/memetory/src/main/java/com/example/memetory/domain/member/controller/MemberController.java @@ -35,7 +35,7 @@ public ResponseEntity updateMember(@LoginMemberEmail String emai } @GetMapping - public ResponseEntity getMember(@LoginMemberEmail String email) { + public ResponseEntity findMember(@LoginMemberEmail String email) { MemberServiceDto memberServiceDto = MemberServiceDto.createFromEmail(email); MemberResponse response = memberService.findMemberResponse(memberServiceDto); From 57f98c60e3e930ad799c7d41ebf5a2d782a5491c Mon Sep 17 00:00:00 2001 From: JunRain Date: Wed, 22 May 2024 23:42:38 +0900 Subject: [PATCH 06/13] =?UTF-8?q?[#90]=20MemberController=20updateMember?= =?UTF-8?q?=EC=97=90=20update=EB=90=9C=20=EB=A9=A4=EB=B2=84=20=EB=B0=98?= =?UTF-8?q?=ED=99=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../memetory/domain/member/controller/MemberController.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/backend/memetory/src/main/java/com/example/memetory/domain/member/controller/MemberController.java b/backend/memetory/src/main/java/com/example/memetory/domain/member/controller/MemberController.java index 73a18746..d9b14921 100644 --- a/backend/memetory/src/main/java/com/example/memetory/domain/member/controller/MemberController.java +++ b/backend/memetory/src/main/java/com/example/memetory/domain/member/controller/MemberController.java @@ -29,9 +29,9 @@ public ResponseEntity updateMember(@LoginMemberEmail String emai @RequestBody MemberUpdateRequest memberUpdateRequest) { MemberServiceDto memberServiceDto = memberUpdateRequest.toServiceDtoFromEmail(email); - memberService.updateMember(memberServiceDto); + MemberResponse response = memberService.updateMember(memberServiceDto); - return ResponseEntity.ok(ResultResponse.of(UPDATE_MEMBER_SUCCESS)); + return ResponseEntity.ok(ResultResponse.of(UPDATE_MEMBER_SUCCESS, response)); } @GetMapping From 9a1b69534a3caa0fcc12828213726a4fde04adfc Mon Sep 17 00:00:00 2001 From: JunRain Date: Thu, 23 May 2024 00:10:31 +0900 Subject: [PATCH 07/13] =?UTF-8?q?[#90]=20MemberController=20updateMember?= =?UTF-8?q?=EC=97=90=20update=EB=90=9C=20=EB=A9=A4=EB=B2=84=20=EB=B0=98?= =?UTF-8?q?=ED=99=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/member/service/MemberService.java | 22 ++++++++++++------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/backend/memetory/src/main/java/com/example/memetory/domain/member/service/MemberService.java b/backend/memetory/src/main/java/com/example/memetory/domain/member/service/MemberService.java index bb131785..ce8b8099 100644 --- a/backend/memetory/src/main/java/com/example/memetory/domain/member/service/MemberService.java +++ b/backend/memetory/src/main/java/com/example/memetory/domain/member/service/MemberService.java @@ -15,21 +15,27 @@ @Service @RequiredArgsConstructor -@Slf4j public class MemberService { private final MemberRepository memberRepository; @Transactional - public void updateMember(MemberServiceDto memberServiceDto) { - if (memberRepository.existMemberByNickname(memberServiceDto.getNickname())) { + public MemberResponse updateMember(MemberServiceDto memberServiceDto) { + if (isDuplicateNickname(memberServiceDto.getNickname())) { throw new DuplicatedMemberException(); } - findMemberFromEmail(memberServiceDto.getEmail()).update(memberServiceDto); + Member member = findMemberFromEmail(memberServiceDto.getEmail()); + member.update(memberServiceDto); + + return MemberResponse.of(member); + } + + private boolean isDuplicateNickname(String nickname) { + return memberRepository.existMemberByNickname(nickname); } @Transactional(readOnly = true) - public Member findMemberFromId(Long id) { - return memberRepository.findById(id).orElseThrow(NotFoundMemberException::new); + public Member findMemberFromEmail(String email) { + return memberRepository.findByEmail(email).orElseThrow(NotFoundMemberException::new); } @Transactional(readOnly = true) @@ -40,7 +46,7 @@ public MemberResponse findMemberResponse(MemberServiceDto memberServiceDto) { } @Transactional(readOnly = true) - public Member findMemberFromEmail(String email) { - return memberRepository.findByEmail(email).orElseThrow(NotFoundMemberException::new); + public Member findMemberFromId(Long id) { + return memberRepository.findById(id).orElseThrow(NotFoundMemberException::new); } } From 156403e16fd3fcd948e3b587aa0a834bc4a1cde6 Mon Sep 17 00:00:00 2001 From: JunRain Date: Thu, 23 May 2024 18:10:19 +0900 Subject: [PATCH 08/13] =?UTF-8?q?[#90]=20AuthTest=20=EB=A9=94=EC=84=9C?= =?UTF-8?q?=EB=93=9C=EB=AA=85=20=EB=A6=AC=ED=8C=A9=ED=86=A0=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/member/service/MemberService.java | 1 - .../memetory/domain/auth/AuthTest.java | 41 ++++++++----------- 2 files changed, 17 insertions(+), 25 deletions(-) diff --git a/backend/memetory/src/main/java/com/example/memetory/domain/member/service/MemberService.java b/backend/memetory/src/main/java/com/example/memetory/domain/member/service/MemberService.java index ce8b8099..2389ed6c 100644 --- a/backend/memetory/src/main/java/com/example/memetory/domain/member/service/MemberService.java +++ b/backend/memetory/src/main/java/com/example/memetory/domain/member/service/MemberService.java @@ -11,7 +11,6 @@ import com.example.memetory.domain.member.repository.MemberRepository; import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; @Service @RequiredArgsConstructor diff --git a/backend/memetory/src/test/java/com/example/memetory/domain/auth/AuthTest.java b/backend/memetory/src/test/java/com/example/memetory/domain/auth/AuthTest.java index e00f8f6a..a43b9be9 100644 --- a/backend/memetory/src/test/java/com/example/memetory/domain/auth/AuthTest.java +++ b/backend/memetory/src/test/java/com/example/memetory/domain/auth/AuthTest.java @@ -22,19 +22,17 @@ import com.example.memetory.domain.member.dto.request.MemberUpdateRequest; import com.example.memetory.domain.member.service.MemberService; import com.example.memetory.global.LoginTest; -import com.example.memetory.global.security.jwt.dto.TokenResponse; import com.example.memetory.global.security.jwt.refresh.domain.RefreshToken; @DisplayName("JWT 인증테스트의 ") @WebMvcTest(MemberController.class) public class AuthTest extends LoginTest { - @MockBean private MemberService memberService; @Test - @DisplayName("Access Token을 이용한 정상 인가(Bearer)") - public void accessToken_인가_성공_with_Bearer() throws Exception { + @DisplayName("Bearer+AccessToken을 통한 정상 인증") + public void Given_AccessTokenWithBearer_When_JwtFilter_Expect_Authorization() throws Exception { // when final ResultActions perform = mockMvc.perform(post("/member").contentType(MediaType.APPLICATION_JSON) .content(toRequestBody(new MemberUpdateRequest("junrain2", "imageUrl2"))) @@ -45,8 +43,8 @@ public class AuthTest extends LoginTest { } @Test - @DisplayName("Access Token을 이용한 정상 인가") - public void accessToken_인가_성공() throws Exception { + @DisplayName("AccessToken을 통한 정상 인증") + public void Given_AccessToken_When_JwtFilter_Expect_Authorization() throws Exception { // when final ResultActions perform = mockMvc.perform(post("/member").contentType(MediaType.APPLICATION_JSON) .content(toRequestBody(new MemberUpdateRequest("junrain2", "imageUrl2"))) @@ -57,11 +55,11 @@ public class AuthTest extends LoginTest { } @Test - @DisplayName("Access Token 기간 만료로 인한 멤버 업데이트 실패") - public void access_token_기간만료() throws Exception { - // given -> 시간이 만료된 accessToken 생성 + @DisplayName("만료된 AccessToken 인한 인증 실패") + public void Given_ExpiredAccessToken_When_JwtFilter_Expect_IsForbidden() throws Exception { + // given Date now = new Date(); - String accessToken = JWT.create() + String expiredAccessToken = JWT.create() .withSubject("AccessToken") .withExpiresAt(new Date(now.getTime() - 1000)) .withClaim("email", MEMBER().getEmail()) @@ -70,27 +68,22 @@ public class AuthTest extends LoginTest { // when final ResultActions perform = mockMvc.perform(post("/member").contentType(MediaType.APPLICATION_JSON) .content(toRequestBody(new MemberUpdateRequest("junrain2", "imageUrl2"))) - .header("Authorization", accessToken)); + .header("Authorization", expiredAccessToken)); // then perform.andExpect(status().isForbidden()); } @Test - @DisplayName("Refresh Token 전송으로 인한 access_token refresh_token 재발급") - public void refreshToken과_accessToken_재발급() throws Exception { - // given -> refresh Token 세팅 및 redis 에서 refresh Token이 있는지 조회 + @DisplayName("RefreshToken 전송으로 인한 AccessToken과 RefreshToken 재발급") + public void Given_RefreshToken_When_JwtFilter_Expect_ReissueAccessTokenAndRefreshToken() throws Exception { + // given Date now = new Date(); String refreshToken = JWT.create() .withSubject("RefreshToken") .withExpiresAt(new Date(now.getTime() + 18000)) .sign(Algorithm.HMAC512(secretKey)); - TokenResponse expect = TokenResponse.builder() - .accessToken(accessToken) - .refreshToken(refreshToken) - .build(); - RefreshToken token = new RefreshToken(MEMBER().getEmail()); given(refreshTokenService.findByToken(refreshToken)).willReturn(token); @@ -106,11 +99,11 @@ public class AuthTest extends LoginTest { } @Test - @DisplayName("Refresh Token 만료") - public void refreshToken_만료() throws Exception { - // given -> 만료된 refreshToken 설정 + @DisplayName("만료된 RefreshToken으로 인한 인증 실패") + public void Given_ExpiredRefreshToken_When_JwtFilter_Expect_IsUnauthorized() throws Exception { + // given Date now = new Date(); - String refreshToken = JWT.create() + String expiredRefreshToken = JWT.create() .withSubject("RefreshToken") .withExpiresAt(new Date(now.getTime() - 1000)) .sign(Algorithm.HMAC512(secretKey)); @@ -118,7 +111,7 @@ public class AuthTest extends LoginTest { // when final ResultActions perform = mockMvc.perform(post("/member").contentType(MediaType.APPLICATION_JSON) .content(toRequestBody(new MemberUpdateRequest("junrain2", "imageUrl2"))) - .header("Authorization-refresh", refreshToken)).andDo(print()); + .header("Authorization-refresh", expiredRefreshToken)).andDo(print()); // then perform.andExpect(status().isUnauthorized()); From 2c86020627e5adac2c7bc43094c5071d6b43bf23 Mon Sep 17 00:00:00 2001 From: JunRain Date: Thu, 23 May 2024 18:29:14 +0900 Subject: [PATCH 09/13] =?UTF-8?q?[#90]=20LikeController=EC=9D=98=20?= =?UTF-8?q?=EB=A9=94=EC=84=9C=EB=93=9C=20MemesController=EB=A1=9C=20?= =?UTF-8?q?=EC=9D=B4=EA=B4=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/like/controller/LikeApi.java | 50 -------------- .../like/controller/LikeController.java | 43 ------------ .../domain/memes/controller/MemesApi.java | 34 ++++++++++ .../memes/controller/MemesController.java | 21 ++++++ .../like/controller/LikeControllerTest.java | 67 ------------------- .../memes/controller/MemesControllerTest.java | 45 +++++++++++++ 6 files changed, 100 insertions(+), 160 deletions(-) delete mode 100644 backend/memetory/src/main/java/com/example/memetory/domain/like/controller/LikeApi.java delete mode 100644 backend/memetory/src/main/java/com/example/memetory/domain/like/controller/LikeController.java delete mode 100644 backend/memetory/src/test/java/com/example/memetory/domain/like/controller/LikeControllerTest.java diff --git a/backend/memetory/src/main/java/com/example/memetory/domain/like/controller/LikeApi.java b/backend/memetory/src/main/java/com/example/memetory/domain/like/controller/LikeApi.java deleted file mode 100644 index 96791c39..00000000 --- a/backend/memetory/src/main/java/com/example/memetory/domain/like/controller/LikeApi.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.example.memetory.domain.like.controller; - -import io.swagger.v3.oas.annotations.Operation; -import io.swagger.v3.oas.annotations.Parameter; -import io.swagger.v3.oas.annotations.enums.ParameterIn; -import io.swagger.v3.oas.annotations.responses.ApiResponse; -import io.swagger.v3.oas.annotations.responses.ApiResponses; -import io.swagger.v3.oas.annotations.security.SecurityRequirement; -import io.swagger.v3.oas.annotations.tags.Tag; -import org.springframework.http.ResponseEntity; - -import com.example.memetory.global.response.ResultResponse; - -@Tag(name = "Like") -public interface LikeApi { - - @Operation( - summary = "meme`s 좋아요 등록", - description = "공유된 meme`s 게시물에 좋아요를 등록한다.", - security = {@SecurityRequirement(name = "access_token")} - ) - @ApiResponses(value = { - @ApiResponse( - responseCode = "201", - description = "좋아요 등록!" - ) - }) - ResponseEntity register( - @Parameter(hidden = true) String email, - @Parameter(in = ParameterIn.PATH, description = "밈스 아이디", required = true) - Long memesId - ); - - @Operation( - summary = "meme`s 좋아요 취소", - description = "공유된 meme`s 게시물에 등록된 좋아요를 취소한다.", - security = {@SecurityRequirement(name = "access_token")} - ) - @ApiResponses(value = { - @ApiResponse( - responseCode = "200", - description = "좋아요 취소!" - ) - }) - ResponseEntity cancel( - @Parameter(hidden = true) String email, - @Parameter(in = ParameterIn.PATH, description = "밈스 아이디", required = true) - Long memesId - ); -} diff --git a/backend/memetory/src/main/java/com/example/memetory/domain/like/controller/LikeController.java b/backend/memetory/src/main/java/com/example/memetory/domain/like/controller/LikeController.java deleted file mode 100644 index 006172f6..00000000 --- a/backend/memetory/src/main/java/com/example/memetory/domain/like/controller/LikeController.java +++ /dev/null @@ -1,43 +0,0 @@ -package com.example.memetory.domain.like.controller; - -import static com.example.memetory.global.response.ResultCode.*; - -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.DeleteMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; - -import com.example.memetory.domain.like.dto.LikeServiceDto; -import com.example.memetory.domain.like.service.LikeService; -import com.example.memetory.global.annotation.LoginMemberEmail; -import com.example.memetory.global.response.ResultResponse; - -import lombok.RequiredArgsConstructor; - -@RestController -@RequiredArgsConstructor -@RequestMapping("/memes") -public class LikeController implements LikeApi { - private final LikeService likeService; - - @PostMapping("/{memesId}/like") - @Override - public ResponseEntity register(@LoginMemberEmail String email, @PathVariable Long memesId) { - LikeServiceDto likeServiceDto = LikeServiceDto.create(email, memesId); - likeService.register(likeServiceDto); - - return ResponseEntity.status(HttpStatus.CREATED).body(ResultResponse.of(CREATE_LIKE_SUCCESS)); - } - - @DeleteMapping("/{memesId}/like") - @Override - public ResponseEntity cancel(@LoginMemberEmail String email, @PathVariable Long memesId) { - LikeServiceDto likeServiceDto = LikeServiceDto.create(email, memesId); - likeService.cancel(likeServiceDto); - - return ResponseEntity.ok(ResultResponse.of(DELETE_LIKE_SUCCESS)); - } -} diff --git a/backend/memetory/src/main/java/com/example/memetory/domain/memes/controller/MemesApi.java b/backend/memetory/src/main/java/com/example/memetory/domain/memes/controller/MemesApi.java index 233c9762..277e3cc5 100644 --- a/backend/memetory/src/main/java/com/example/memetory/domain/memes/controller/MemesApi.java +++ b/backend/memetory/src/main/java/com/example/memetory/domain/memes/controller/MemesApi.java @@ -115,4 +115,38 @@ ResponseEntity findMemes( ) }) ResponseEntity findAllMemes(Pageable pageable); + + @Operation( + summary = "meme`s 좋아요 등록", + description = "공유된 meme`s 게시물에 좋아요를 등록한다.", + security = {@SecurityRequirement(name = "access_token")} + ) + @ApiResponses(value = { + @ApiResponse( + responseCode = "201", + description = "좋아요 등록!" + ) + }) + ResponseEntity registerLike( + @Parameter(hidden = true) String email, + @Parameter(in = ParameterIn.PATH, description = "밈스 아이디", required = true) + Long memesId + ); + + @Operation( + summary = "meme`s 좋아요 취소", + description = "공유된 meme`s 게시물에 등록된 좋아요를 취소한다.", + security = {@SecurityRequirement(name = "access_token")} + ) + @ApiResponses(value = { + @ApiResponse( + responseCode = "200", + description = "좋아요 취소!" + ) + }) + ResponseEntity cancelLike( + @Parameter(hidden = true) String email, + @Parameter(in = ParameterIn.PATH, description = "밈스 아이디", required = true) + Long memesId + ); } diff --git a/backend/memetory/src/main/java/com/example/memetory/domain/memes/controller/MemesController.java b/backend/memetory/src/main/java/com/example/memetory/domain/memes/controller/MemesController.java index a996bcdf..24d3734c 100644 --- a/backend/memetory/src/main/java/com/example/memetory/domain/memes/controller/MemesController.java +++ b/backend/memetory/src/main/java/com/example/memetory/domain/memes/controller/MemesController.java @@ -15,6 +15,8 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import com.example.memetory.domain.like.dto.LikeServiceDto; +import com.example.memetory.domain.like.service.LikeService; import com.example.memetory.domain.memes.dto.MemesServiceDto; import com.example.memetory.domain.memes.dto.request.GenerateMemesRequest; import com.example.memetory.domain.memes.dto.response.MemesInfo; @@ -31,6 +33,7 @@ @RequestMapping("/memes") public class MemesController implements MemesApi { private final MemesService memesService; + private final LikeService likeService; @PostMapping @Override @@ -85,4 +88,22 @@ public ResponseEntity findAllMemes(Pageable pageable) { return ResponseEntity.ok(ResultResponse.of(GET_ALL_MEMES_SUCCESS, response)); } + + @PostMapping("/{memesId}/like") + @Override + public ResponseEntity registerLike(@LoginMemberEmail String email, @PathVariable Long memesId) { + LikeServiceDto likeServiceDto = LikeServiceDto.create(email, memesId); + likeService.register(likeServiceDto); + + return ResponseEntity.status(HttpStatus.CREATED).body(ResultResponse.of(CREATE_LIKE_SUCCESS)); + } + + @DeleteMapping("/{memesId}/like") + @Override + public ResponseEntity cancelLike(@LoginMemberEmail String email, @PathVariable Long memesId) { + LikeServiceDto likeServiceDto = LikeServiceDto.create(email, memesId); + likeService.cancel(likeServiceDto); + + return ResponseEntity.ok(ResultResponse.of(DELETE_LIKE_SUCCESS)); + } } diff --git a/backend/memetory/src/test/java/com/example/memetory/domain/like/controller/LikeControllerTest.java b/backend/memetory/src/test/java/com/example/memetory/domain/like/controller/LikeControllerTest.java deleted file mode 100644 index 886503b3..00000000 --- a/backend/memetory/src/test/java/com/example/memetory/domain/like/controller/LikeControllerTest.java +++ /dev/null @@ -1,67 +0,0 @@ -package com.example.memetory.domain.like.controller; - -import static com.example.memetory.global.response.ErrorCode.*; -import static com.example.memetory.global.response.ResultCode.*; -import static org.mockito.BDDMockito.*; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; -import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.*; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; - -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; -import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; -import org.springframework.boot.test.mock.mockito.MockBean; -import org.springframework.http.MediaType; -import org.springframework.test.web.servlet.ResultActions; - -import com.example.memetory.domain.like.dto.LikeServiceDto; -import com.example.memetory.domain.like.exception.NotFoundLikeException; -import com.example.memetory.domain.like.service.LikeService; -import com.example.memetory.global.LoginTest; - -@DisplayName("Like 컨트롤러 테스트의 ") -@WebMvcTest(LikeController.class) -public class LikeControllerTest extends LoginTest { - @MockBean - private LikeService likeService; - - @DisplayName("좋아요 등록 성공") - @Test - void 좋아요_등록() throws Exception { - // when - final ResultActions perform = mockMvc.perform(post("/memes/-1/like").contentType(MediaType.APPLICATION_JSON) - .header("Authorization", "Bearer " + accessToken)).andDo(print()); - - // then - verify(likeService).register(any(LikeServiceDto.class)); - perform.andExpect(status().isCreated()) - .andExpect(jsonPath(MESSAGE).value(CREATE_LIKE_SUCCESS.getMessage())); - } - - @DisplayName("좋아요 취소 성공") - @Test - void 좋아요_취소() throws Exception { - // when - final ResultActions perform = mockMvc.perform(delete("/memes/-1/like").contentType(MediaType.APPLICATION_JSON) - .header("Authorization", "Bearer " + accessToken)).andDo(print()); - - // then - verify(likeService).cancel(any(LikeServiceDto.class)); - perform.andExpect(status().isOk()) - .andExpect(jsonPath(MESSAGE).value(DELETE_LIKE_SUCCESS.getMessage())); - } - - @DisplayName("좋아요 취소 실패") - @Test - void 좋아요_취소_실패() throws Exception { - // when - doThrow(new NotFoundLikeException()).when(likeService).cancel(any(LikeServiceDto.class)); - - final ResultActions perform = mockMvc.perform(delete("/memes/-1/like").contentType(MediaType.APPLICATION_JSON) - .header("Authorization", "Bearer " + accessToken)).andDo(print()); - - // then - perform.andExpect(status().isNotFound()) - .andExpect(jsonPath(ERROR_MESSAGE).value(LIKE_NOT_FOUND.getMessage())); - } -} diff --git a/backend/memetory/src/test/java/com/example/memetory/domain/memes/controller/MemesControllerTest.java b/backend/memetory/src/test/java/com/example/memetory/domain/memes/controller/MemesControllerTest.java index f168685e..ea44b801 100644 --- a/backend/memetory/src/test/java/com/example/memetory/domain/memes/controller/MemesControllerTest.java +++ b/backend/memetory/src/test/java/com/example/memetory/domain/memes/controller/MemesControllerTest.java @@ -18,6 +18,9 @@ import org.springframework.http.MediaType; import org.springframework.test.web.servlet.ResultActions; +import com.example.memetory.domain.like.dto.LikeServiceDto; +import com.example.memetory.domain.like.exception.NotFoundLikeException; +import com.example.memetory.domain.like.service.LikeService; import com.example.memetory.domain.meme.entity.Meme; import com.example.memetory.domain.memes.dto.request.GenerateMemesRequest; import com.example.memetory.domain.memes.dto.response.MemesInfo; @@ -32,6 +35,8 @@ public class MemesControllerTest extends LoginTest { @MockBean private MemesService memesService; + @MockBean + private LikeService likeService; @Test @DisplayName("Memes 생성") @@ -161,4 +166,44 @@ public class MemesControllerTest extends LoginTest { jsonPath(MESSAGE).value(GET_ALL_MEMES_SUCCESS.getMessage()), jsonPath("$.data.memesInfoList[0].title").value(response.getMemesInfoList().get(0).getTitle())); } + + @DisplayName("좋아요 등록 성공") + @Test + void 좋아요_등록() throws Exception { + // when + final ResultActions perform = mockMvc.perform(post("/memes/-1/like").contentType(MediaType.APPLICATION_JSON) + .header("Authorization", "Bearer " + accessToken)).andDo(print()); + + // then + verify(likeService).register(any(LikeServiceDto.class)); + perform.andExpect(status().isCreated()) + .andExpect(jsonPath(MESSAGE).value(CREATE_LIKE_SUCCESS.getMessage())); + } + + @DisplayName("좋아요 취소 성공") + @Test + void 좋아요_취소() throws Exception { + // when + final ResultActions perform = mockMvc.perform(delete("/memes/-1/like").contentType(MediaType.APPLICATION_JSON) + .header("Authorization", "Bearer " + accessToken)).andDo(print()); + + // then + verify(likeService).cancel(any(LikeServiceDto.class)); + perform.andExpect(status().isOk()) + .andExpect(jsonPath(MESSAGE).value(DELETE_LIKE_SUCCESS.getMessage())); + } + + @DisplayName("좋아요 취소 실패") + @Test + void 좋아요_취소_실패() throws Exception { + // when + doThrow(new NotFoundLikeException()).when(likeService).cancel(any(LikeServiceDto.class)); + + final ResultActions perform = mockMvc.perform(delete("/memes/-1/like").contentType(MediaType.APPLICATION_JSON) + .header("Authorization", "Bearer " + accessToken)).andDo(print()); + + // then + perform.andExpect(status().isNotFound()) + .andExpect(jsonPath(ERROR_MESSAGE).value(LIKE_NOT_FOUND.getMessage())); + } } From 782829f2addab06067613c42bae6e75f724db78d Mon Sep 17 00:00:00 2001 From: JunRain Date: Thu, 23 May 2024 19:50:50 +0900 Subject: [PATCH 10/13] =?UTF-8?q?[#90]=20Like=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EB=A9=94=EC=84=9C=EB=93=9C=EB=AA=85=20=EB=A6=AC?= =?UTF-8?q?=ED=8C=A9=ED=86=A0=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../exception/NotCreateLikeException.java | 11 ++++ .../domain/like/service/LikeService.java | 64 +++++++++++-------- .../memes/controller/MemesController.java | 4 +- .../memetory/global/response/ErrorCode.java | 1 + .../like/repository/LikeRepositoryTest.java | 26 ++++---- .../domain/like/service/LikeServiceTest.java | 31 +++++++-- .../memes/controller/MemesControllerTest.java | 6 +- 7 files changed, 90 insertions(+), 53 deletions(-) create mode 100644 backend/memetory/src/main/java/com/example/memetory/domain/like/exception/NotCreateLikeException.java diff --git a/backend/memetory/src/main/java/com/example/memetory/domain/like/exception/NotCreateLikeException.java b/backend/memetory/src/main/java/com/example/memetory/domain/like/exception/NotCreateLikeException.java new file mode 100644 index 00000000..45ee4b65 --- /dev/null +++ b/backend/memetory/src/main/java/com/example/memetory/domain/like/exception/NotCreateLikeException.java @@ -0,0 +1,11 @@ +package com.example.memetory.domain.like.exception; + +import com.example.memetory.global.exception.BusinessException; +import com.example.memetory.global.response.ErrorCode; + +public class NotCreateLikeException extends BusinessException { + + public NotCreateLikeException() { + super(ErrorCode.LIKE_NOT_CREATE); + } +} diff --git a/backend/memetory/src/main/java/com/example/memetory/domain/like/service/LikeService.java b/backend/memetory/src/main/java/com/example/memetory/domain/like/service/LikeService.java index 6bbe5d5f..bdceed30 100644 --- a/backend/memetory/src/main/java/com/example/memetory/domain/like/service/LikeService.java +++ b/backend/memetory/src/main/java/com/example/memetory/domain/like/service/LikeService.java @@ -1,43 +1,53 @@ package com.example.memetory.domain.like.service; +import org.springframework.dao.DataIntegrityViolationException; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + import com.example.memetory.domain.like.dto.LikeServiceDto; import com.example.memetory.domain.like.entity.Like; +import com.example.memetory.domain.like.exception.NotCreateLikeException; import com.example.memetory.domain.like.exception.NotFoundLikeException; import com.example.memetory.domain.like.repository.LikeRepository; import com.example.memetory.domain.member.entity.Member; import com.example.memetory.domain.member.service.MemberService; import com.example.memetory.domain.memes.entity.Memes; import com.example.memetory.domain.memes.service.MemesService; + import lombok.RequiredArgsConstructor; -import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; @Service @RequiredArgsConstructor public class LikeService { - - private final MemberService memberService; - private final MemesService memesService; - private final LikeRepository likeRepository; - - @Transactional - public void register(LikeServiceDto likeServiceDto) { - Member member = memberService.findMemberFromEmail(likeServiceDto.getEmail()); - Memes memes = memesService.getMemesBetweenService(likeServiceDto.getMemesId()); - memes.addLikeCount(); - - Like newLike = likeServiceDto.toEntity(member, memes); - likeRepository.save(newLike); - } - - @Transactional - public void cancel(LikeServiceDto likeServiceDto) { - Member member = memberService.findMemberFromEmail(likeServiceDto.getEmail()); - Memes memes = memesService.getMemesBetweenService(likeServiceDto.getMemesId()); - memes.cancelLikeCount(); - - Like like = likeRepository.findLikeByMemberAndMemes(member, memes).orElseThrow(NotFoundLikeException::new); - - likeRepository.delete(like); - } + private final MemberService memberService; + private final MemesService memesService; + private final LikeRepository likeRepository; + + @Transactional + public void registerLike(LikeServiceDto likeServiceDto) { + Member member = memberService.findMemberFromEmail(likeServiceDto.getEmail()); + Memes memes = memesService.getMemesBetweenService(likeServiceDto.getMemesId()); + + Like newLike = likeServiceDto.toEntity(member, memes); + saveLike(newLike); + memes.addLikeCount(); + } + + private void saveLike(Like like) { + try { + likeRepository.save(like); + } catch (DataIntegrityViolationException e) { + throw new NotCreateLikeException(); + } + } + + @Transactional + public void cancelLike(LikeServiceDto likeServiceDto) { + Member member = memberService.findMemberFromEmail(likeServiceDto.getEmail()); + Memes memes = memesService.getMemesBetweenService(likeServiceDto.getMemesId()); + Like like = likeRepository.findLikeByMemberAndMemes(member, memes).orElseThrow(NotFoundLikeException::new); + + likeRepository.delete(like); + memes.cancelLikeCount(); + } } diff --git a/backend/memetory/src/main/java/com/example/memetory/domain/memes/controller/MemesController.java b/backend/memetory/src/main/java/com/example/memetory/domain/memes/controller/MemesController.java index 24d3734c..a348a3e0 100644 --- a/backend/memetory/src/main/java/com/example/memetory/domain/memes/controller/MemesController.java +++ b/backend/memetory/src/main/java/com/example/memetory/domain/memes/controller/MemesController.java @@ -93,7 +93,7 @@ public ResponseEntity findAllMemes(Pageable pageable) { @Override public ResponseEntity registerLike(@LoginMemberEmail String email, @PathVariable Long memesId) { LikeServiceDto likeServiceDto = LikeServiceDto.create(email, memesId); - likeService.register(likeServiceDto); + likeService.registerLike(likeServiceDto); return ResponseEntity.status(HttpStatus.CREATED).body(ResultResponse.of(CREATE_LIKE_SUCCESS)); } @@ -102,7 +102,7 @@ public ResponseEntity registerLike(@LoginMemberEmail String emai @Override public ResponseEntity cancelLike(@LoginMemberEmail String email, @PathVariable Long memesId) { LikeServiceDto likeServiceDto = LikeServiceDto.create(email, memesId); - likeService.cancel(likeServiceDto); + likeService.cancelLike(likeServiceDto); return ResponseEntity.ok(ResultResponse.of(DELETE_LIKE_SUCCESS)); } diff --git a/backend/memetory/src/main/java/com/example/memetory/global/response/ErrorCode.java b/backend/memetory/src/main/java/com/example/memetory/global/response/ErrorCode.java index 8eb0a534..9af3178c 100644 --- a/backend/memetory/src/main/java/com/example/memetory/global/response/ErrorCode.java +++ b/backend/memetory/src/main/java/com/example/memetory/global/response/ErrorCode.java @@ -30,6 +30,7 @@ public enum ErrorCode { // Like LIKE_NOT_FOUND(404, "좋아요 찾기 실패"), + LIKE_NOT_CREATE(400, "중복된 좋아요"), // Complain COMPLAIN_NOT_FOUND(404, "신고 찾기 실패"), diff --git a/backend/memetory/src/test/java/com/example/memetory/domain/like/repository/LikeRepositoryTest.java b/backend/memetory/src/test/java/com/example/memetory/domain/like/repository/LikeRepositoryTest.java index 163ae0cb..9a07ac4f 100644 --- a/backend/memetory/src/test/java/com/example/memetory/domain/like/repository/LikeRepositoryTest.java +++ b/backend/memetory/src/test/java/com/example/memetory/domain/like/repository/LikeRepositoryTest.java @@ -12,6 +12,7 @@ import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.dao.DataIntegrityViolationException; import com.example.memetory.domain.like.entity.Like; import com.example.memetory.domain.member.entity.Member; @@ -35,28 +36,25 @@ public class LikeRepositoryTest { private MemeRepository memeRepository; @Test - @DisplayName("Member와 Memes로 Like 찾기") - void Like_찾기() { + @DisplayName("Member와 Memes를 통한 단일 Like 조회 성공") + void Given_MemberAndMemes_When_findLikeByMemberAndMemes_Then_Like() { // given - Member member = MEMBER(); - memberRepository.save(member); - Meme meme = MEME(member); - memeRepository.save(meme); - Memes memes = MEMES(member, meme); - memesRepository.save(memes); - Like like = LIKE(member, memes); - Like expect = likeRepository.save(like); + Member member = memberRepository.save(MEMBER()); + Meme meme = memeRepository.save(MEME(member)); + Memes memes = memesRepository.save(MEMES(member, meme)); + + Like expectedResult = likeRepository.save(LIKE(member, memes)); // when Optional result = likeRepository.findLikeByMemberAndMemes(member, memes); // then - assertThat(expect).isEqualTo(result.get()); + assertThat(expectedResult).isEqualTo(result.get()); } @Test - @DisplayName("밈스와 멤버 조합 중복 감지") - void 중복감지() { + @DisplayName("중복된 밈스와 멤버 테이블로 인한 DataIntegrityViolationException 반환") + void Given_Like_When_save_Throw_RuntimeException() { // given Member member = memberRepository.save(MEMBER()); Meme meme = memeRepository.save(MEME(member)); @@ -66,6 +64,6 @@ public class LikeRepositoryTest { likeRepository.save(LIKE(member, memes)); // then - assertThrows(RuntimeException.class, () -> likeRepository.save(LIKE(member, memes))); + assertThrows(DataIntegrityViolationException.class, () -> likeRepository.save(LIKE(member, memes))); } } diff --git a/backend/memetory/src/test/java/com/example/memetory/domain/like/service/LikeServiceTest.java b/backend/memetory/src/test/java/com/example/memetory/domain/like/service/LikeServiceTest.java index 6c6e9533..a557ac03 100644 --- a/backend/memetory/src/test/java/com/example/memetory/domain/like/service/LikeServiceTest.java +++ b/backend/memetory/src/test/java/com/example/memetory/domain/like/service/LikeServiceTest.java @@ -5,6 +5,7 @@ import static com.example.memetory.domain.meme.MemeFixture.*; import static com.example.memetory.domain.memes.MemesFixture.*; import static org.assertj.core.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.*; import static org.mockito.BDDMockito.*; import java.util.Optional; @@ -16,9 +17,11 @@ import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.dao.DataIntegrityViolationException; import com.example.memetory.domain.like.dto.LikeServiceDto; import com.example.memetory.domain.like.entity.Like; +import com.example.memetory.domain.like.exception.NotCreateLikeException; import com.example.memetory.domain.like.repository.LikeRepository; import com.example.memetory.domain.member.entity.Member; import com.example.memetory.domain.member.service.MemberService; @@ -49,14 +52,14 @@ void setUp() { } @Test - @DisplayName("register 성공 테스트") - void 좋아요_등록() { + @DisplayName("likeServiceDto로 인한 Like 등록 성공") + void Given_likeServiceDto_When_registerLike_Execute_likeRepositorySave() { // given given(memberService.findMemberFromEmail(likeServiceDto.getEmail())).willReturn(member); given(memesService.getMemesBetweenService(likeServiceDto.getMemesId())).willReturn(memes); // when - likeService.register(likeServiceDto); + likeService.registerLike(likeServiceDto); // then assertThat(memes.getLikeCount()).isEqualTo(2L); @@ -64,8 +67,22 @@ void setUp() { } @Test - @DisplayName("cancel 성공 테스트") - void 좋아요_취소() { + @DisplayName("Like 테이블의 중복된 Memes와 Member 칼럼으로 인한 NotCreateLikeException 발생") + void Given_likeServiceDto_When_registerLike_Throw_NotCreateLikeException() { + // given + given(memberService.findMemberFromEmail(likeServiceDto.getEmail())).willReturn(member); + given(memesService.getMemesBetweenService(likeServiceDto.getMemesId())).willReturn(memes); + given(likeRepository.save(any())).willThrow(new DataIntegrityViolationException(any())); + + // then + assertThrows(NotCreateLikeException.class, () -> likeService.registerLike(likeServiceDto)); + verify(likeRepository).save(any(Like.class)); + assertThat(memes.getLikeCount()).isEqualTo(1L); + } + + @Test + @DisplayName("likeServiceDto로 인한 Like 삭제 성공") + void Given_LikeServiceDto_when_cancelLike_Execute_likeRepositoryDelete() { // given Like like = LIKE(member, memes); given(memberService.findMemberFromEmail(likeServiceDto.getEmail())).willReturn(member); @@ -73,10 +90,10 @@ void setUp() { given(likeRepository.findLikeByMemberAndMemes(member, memes)).willReturn(Optional.ofNullable(like)); // when - likeService.cancel(likeServiceDto); + likeService.cancelLike(likeServiceDto); // then - assertThat(memes.getLikeCount()).isEqualTo(0L); + assertThat(memes.getLikeCount()).isZero(); verify(likeRepository).delete(like); } } diff --git a/backend/memetory/src/test/java/com/example/memetory/domain/memes/controller/MemesControllerTest.java b/backend/memetory/src/test/java/com/example/memetory/domain/memes/controller/MemesControllerTest.java index ea44b801..573112c6 100644 --- a/backend/memetory/src/test/java/com/example/memetory/domain/memes/controller/MemesControllerTest.java +++ b/backend/memetory/src/test/java/com/example/memetory/domain/memes/controller/MemesControllerTest.java @@ -175,7 +175,7 @@ public class MemesControllerTest extends LoginTest { .header("Authorization", "Bearer " + accessToken)).andDo(print()); // then - verify(likeService).register(any(LikeServiceDto.class)); + verify(likeService).registerLike(any(LikeServiceDto.class)); perform.andExpect(status().isCreated()) .andExpect(jsonPath(MESSAGE).value(CREATE_LIKE_SUCCESS.getMessage())); } @@ -188,7 +188,7 @@ public class MemesControllerTest extends LoginTest { .header("Authorization", "Bearer " + accessToken)).andDo(print()); // then - verify(likeService).cancel(any(LikeServiceDto.class)); + verify(likeService).cancelLike(any(LikeServiceDto.class)); perform.andExpect(status().isOk()) .andExpect(jsonPath(MESSAGE).value(DELETE_LIKE_SUCCESS.getMessage())); } @@ -197,7 +197,7 @@ public class MemesControllerTest extends LoginTest { @Test void 좋아요_취소_실패() throws Exception { // when - doThrow(new NotFoundLikeException()).when(likeService).cancel(any(LikeServiceDto.class)); + doThrow(new NotFoundLikeException()).when(likeService).cancelLike(any(LikeServiceDto.class)); final ResultActions perform = mockMvc.perform(delete("/memes/-1/like").contentType(MediaType.APPLICATION_JSON) .header("Authorization", "Bearer " + accessToken)).andDo(print()); From 10b8f123711dd98f50a4edfb07a3d087b2509df2 Mon Sep 17 00:00:00 2001 From: JunRain Date: Thu, 23 May 2024 22:35:19 +0900 Subject: [PATCH 11/13] =?UTF-8?q?[#90]=20Member=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EB=A9=94=EC=84=9C=EB=93=9C=EB=AA=85=20=EB=A6=AC?= =?UTF-8?q?=ED=8C=A9=ED=86=A0=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/like/service/LikeServiceTest.java | 2 +- .../controller/MemberControllerTest.java | 61 +++++++++++----- .../repository/MemberRepositoryTest.java | 53 +++++++------- .../member/service/MemberServiceTest.java | 69 ++++++++++--------- 4 files changed, 112 insertions(+), 73 deletions(-) diff --git a/backend/memetory/src/test/java/com/example/memetory/domain/like/service/LikeServiceTest.java b/backend/memetory/src/test/java/com/example/memetory/domain/like/service/LikeServiceTest.java index a557ac03..891405b7 100644 --- a/backend/memetory/src/test/java/com/example/memetory/domain/like/service/LikeServiceTest.java +++ b/backend/memetory/src/test/java/com/example/memetory/domain/like/service/LikeServiceTest.java @@ -67,7 +67,7 @@ void Given_likeServiceDto_When_registerLike_Execute_likeRepositorySave() { } @Test - @DisplayName("Like 테이블의 중복된 Memes와 Member 칼럼으로 인한 NotCreateLikeException 발생") + @DisplayName("Like 테이블의 중복된 Memes와 Member 칼럼으로 인한 NotCreateLikeException 반환") void Given_likeServiceDto_When_registerLike_Throw_NotCreateLikeException() { // given given(memberService.findMemberFromEmail(likeServiceDto.getEmail())).willReturn(member); diff --git a/backend/memetory/src/test/java/com/example/memetory/domain/member/controller/MemberControllerTest.java b/backend/memetory/src/test/java/com/example/memetory/domain/member/controller/MemberControllerTest.java index ad4c9b03..10f10462 100644 --- a/backend/memetory/src/test/java/com/example/memetory/domain/member/controller/MemberControllerTest.java +++ b/backend/memetory/src/test/java/com/example/memetory/domain/member/controller/MemberControllerTest.java @@ -1,5 +1,6 @@ package com.example.memetory.domain.member.controller; +import static com.example.memetory.domain.member.MemberFixture.*; import static com.example.memetory.global.response.ErrorCode.*; import static com.example.memetory.global.response.ResultCode.*; import static org.mockito.BDDMockito.*; @@ -14,7 +15,9 @@ import org.springframework.http.MediaType; import org.springframework.test.web.servlet.ResultActions; +import com.example.memetory.domain.member.dto.MemberServiceDto; import com.example.memetory.domain.member.dto.request.MemberUpdateRequest; +import com.example.memetory.domain.member.dto.response.MemberResponse; import com.example.memetory.domain.member.exception.DuplicatedMemberException; import com.example.memetory.domain.member.service.MemberService; import com.example.memetory.global.LoginTest; @@ -26,27 +29,42 @@ public class MemberControllerTest extends LoginTest { private MemberService memberService; @Test - @DisplayName("멤버 업데이트 성공") - public void 멤버_업데이트_성공() throws Exception { + @DisplayName("MemberUpdateRequest를 통한 멤버 업데이트 성공") + public void Given_MemberUpdateRequest_When_updateMember_Then_UPDATRE_MEMBER_SUCCESS() throws Exception { + // given + MemberUpdateRequest request = new MemberUpdateRequest("junrain2", "imageUrl2"); + MemberResponse expectedResult = MemberResponse.of(MEMBER()); + String expectedNickname = expectedResult.getNickName(); + + given(memberService.updateMember(any())).willReturn(expectedResult); + // when - final ResultActions perform = mockMvc.perform(post("/member").contentType(MediaType.APPLICATION_JSON) - .content(toRequestBody(new MemberUpdateRequest("junrain2", "imageUrl2"))) - .header("Authorization", "Bearer " + accessToken)).andDo(print()); + final ResultActions perform = mockMvc.perform(post("/member") + .contentType(MediaType.APPLICATION_JSON) + .content(toRequestBody(request)) + .header("Authorization", "Bearer " + accessToken)) + .andDo(print()); // then - perform.andExpect(status().isOk()).andExpect(jsonPath(MESSAGE).value(UPDATE_MEMBER_SUCCESS.getMessage())); + perform.andExpect(status().isOk()) + .andExpect(jsonPath(MESSAGE).value(UPDATE_MEMBER_SUCCESS.getMessage())) + .andExpect(jsonPath("$.data.nickName").value(expectedNickname)); } @Test - @DisplayName("멤버 업데이트 실패(닉네임 중복)") - public void 멤버_업데이트_실패_닉네임_중복() throws Exception { - // given -> 결과에 대한 객체, Member 객체 저장할 필요 존재 + @DisplayName("중복된 닉네임으로 인한 NICKNAME_IS_DUPLICATED 반환") + public void Given_MemberUpdateRequest_When_updateMember_Then_NICKNAME_IS_DUPLICATED() throws Exception { + // given + MemberUpdateRequest request = new MemberUpdateRequest("junrain2", "imageUrl2"); + doThrow(new DuplicatedMemberException()).when(memberService).updateMember(any()); // when - final ResultActions perform = mockMvc.perform(post("/member").contentType(MediaType.APPLICATION_JSON) - .content(toRequestBody(new MemberUpdateRequest("junrain", "imageUrl"))) - .header("Authorization", "Bearer " + accessToken)).andDo(print()); + final ResultActions perform = mockMvc.perform(post("/member") + .contentType(MediaType.APPLICATION_JSON) + .content(toRequestBody(request)) + .header("Authorization", "Bearer " + accessToken)) + .andDo(print()); // then perform.andExpect(status().isConflict()) @@ -54,16 +72,23 @@ public class MemberControllerTest extends LoginTest { } @Test - @DisplayName("단일 멤버 조회 성공") - public void 단일_멤버_조회_성공() throws Exception { + @DisplayName("email을 통한 MemberResponse 반환 성공") + public void Given_email_When_findMember_Then_MemberResponse() throws Exception { // given - given(memberService.findMemberFromEmail(anyString())).willReturn(loginMember); + MemberResponse expectedResult = MemberResponse.of(MEMBER()); + String expectedNickname = expectedResult.getNickName(); + + given(memberService.findMemberResponse(any(MemberServiceDto.class))).willReturn(expectedResult); // when - final ResultActions perform = mockMvc.perform(get("/member").contentType(MediaType.APPLICATION_JSON) - .header("Authorization", "Bearer " + accessToken)).andDo(print()); + final ResultActions perform = mockMvc.perform(get("/member") + .contentType(MediaType.APPLICATION_JSON) + .header("Authorization", "Bearer " + accessToken)) + .andDo(print()); // then - perform.andExpect(status().isOk()).andExpect(jsonPath(MESSAGE).value(GET_MEMBER_SUCCESS.getMessage())); + perform.andExpect(status().isOk()) + .andExpect(jsonPath(MESSAGE).value(GET_MEMBER_SUCCESS.getMessage())) + .andExpect(jsonPath("$.data.nickName").value(expectedNickname)); } } diff --git a/backend/memetory/src/test/java/com/example/memetory/domain/member/repository/MemberRepositoryTest.java b/backend/memetory/src/test/java/com/example/memetory/domain/member/repository/MemberRepositoryTest.java index ef714fa4..72029c28 100644 --- a/backend/memetory/src/test/java/com/example/memetory/domain/member/repository/MemberRepositoryTest.java +++ b/backend/memetory/src/test/java/com/example/memetory/domain/member/repository/MemberRepositoryTest.java @@ -12,71 +12,78 @@ import com.example.memetory.domain.member.dto.MemberServiceDto; import com.example.memetory.domain.member.entity.Member; +import com.example.memetory.domain.member.entity.SocialType; import com.example.memetory.global.RepositoryTest; import jakarta.transaction.Transactional; -@DisplayName("member 레포지토리 테스트의 ") +@DisplayName("Member 레포지토리 테스트의 ") @RepositoryTest public class MemberRepositoryTest { @Autowired private MemberRepository memberRepository; @Test - @DisplayName("이메일로 멤버 찾기") - public void 이메일로_Member_찾기() { - // given -> 멤버 저장하기 + @DisplayName("이메일을 통한 멤버 반환 성공") + public void Given_email_When_findByEmail_Then_Member() { + // given Member savedMember = memberRepository.save(MEMBER()); + String email = savedMember.getEmail(); // when 멤버 찾기 - Optional findMember = memberRepository.findByEmail(savedMember.getEmail()); + Optional findMember = memberRepository.findByEmail(email); // then 확인하기 assertThat(savedMember).isEqualTo(findMember.get()); } @Test - @DisplayName("SocialType과 SocialId로 멤버 찾기") - public void SocialType과_SocialId로_Member_찾기() { + @DisplayName("SocialType과 SocialId을 통한 멤버 반환 성공") + public void Given_SocialTypeAndSocialId_Then_findBySocialTypeAndSocialId_Then_Member() { // given -> 멤버 저장하기 Member savedMember = memberRepository.save(MEMBER()); + SocialType socialType = savedMember.getSocialType(); + String socialId = savedMember.getSocialId(); - // when 멤버 찾기 - Optional findMember = memberRepository - .findBySocialTypeAndSocialId(savedMember.getSocialType(), savedMember.getSocialId()); + // when + Optional findMember = memberRepository.findBySocialTypeAndSocialId(socialType, socialId); - // then 확인하기 + // then assertThat(savedMember).isEqualTo(findMember.get()); } @Test - @DisplayName("닉네임 존재하는지 확인하기") - public void 닉네임_존재_여부() { - // given -> 멤버 저장하기 + @DisplayName("닉네임이 존재해서 true 반환 성공") + public void Given_nickName_When_findByNickName_Then_True() { + // given Member savedMember = memberRepository.save(MEMBER()); + String nickName = savedMember.getNickname(); + + // when + boolean result = memberRepository.existMemberByNickname(nickName); // then 확인하기 - assertTrue(memberRepository.existMemberByNickname(savedMember.getNickname())); + assertTrue(result); } @Test - @DisplayName("멤버 업데이트 확인 테스트") - public void 멤버_업데이트() { + @DisplayName("MemberServiceDto를 통한 멤버 업데이트 성공") + public void Given_MemberServiceDto_When_update() { // given Member savedMember = memberRepository.save(MEMBER()); MemberServiceDto memberServiceDto = UPDATE_MEMBER_SERVICE_DTO(); // when - updatedMember(savedMember, memberServiceDto); + Member expectedMember = updatedMember(savedMember, memberServiceDto); + Member result = memberRepository.findById(savedMember.getId()).get(); // then - assertThat(memberRepository.findById(savedMember.getId()).get().getNickname()).isEqualTo( - memberServiceDto.getNickname()); - + assertThat(result).isEqualTo(expectedMember); } @Transactional - public void updatedMember(Member savedMember, MemberServiceDto memberServiceDto) { - savedMember.update(memberServiceDto); + public Member updatedMember(final Member member,final MemberServiceDto memberServiceDto) { + member.update(memberServiceDto); + return member; } } diff --git a/backend/memetory/src/test/java/com/example/memetory/domain/member/service/MemberServiceTest.java b/backend/memetory/src/test/java/com/example/memetory/domain/member/service/MemberServiceTest.java index 3a7d7f7f..7bd6c316 100644 --- a/backend/memetory/src/test/java/com/example/memetory/domain/member/service/MemberServiceTest.java +++ b/backend/memetory/src/test/java/com/example/memetory/domain/member/service/MemberServiceTest.java @@ -22,7 +22,7 @@ import com.example.memetory.domain.member.exception.NotFoundMemberException; import com.example.memetory.domain.member.repository.MemberRepository; -@DisplayName("memberService의 ") +@DisplayName("Member 서비스 테스트의 ") @ExtendWith(MockitoExtension.class) public class MemberServiceTest { @InjectMocks @@ -38,13 +38,13 @@ void setUp() { } @Test - @DisplayName("이메일을 통해서 멤버 가져오기 성공") - void 이메일을_통해서_Member_불러오기() { + @DisplayName("이메일을 통한 멤버 반환 성공") + void Given_email_When_findMember_Then_Member() { // given 멤버 저장 - final String email = "junrain@ourservice.com"; + final String email = member.getEmail(); + given(memberRepository.findByEmail(email)).willReturn(Optional.ofNullable(member)); // when 실행 - when(memberRepository.findByEmail(email)).thenReturn(Optional.ofNullable(member)); Member result = memberService.findMemberFromEmail(email); // then @@ -52,24 +52,26 @@ void setUp() { } @Test - @DisplayName("이메일을 통해서 멤버 가져오기 실패") - void 이메일을_통해서_Member_불러오기_실패() { + @DisplayName("존재하지 않는 이메일로 인한 NotFoundMemberException 반환") + void Given_NotExistEmail_When_findMember_Throw_NotFoundMemberException() { // given - final String email = "junrain@ourservice.com"; - given(memberRepository.findByEmail(any())).willReturn(Optional.empty()); + final String email = "NotExistEmail"; + + given(memberRepository.findByEmail(email)).willReturn(Optional.empty()); // then assertThrows(NotFoundMemberException.class, () -> memberService.findMemberFromEmail(email)); } @Test - @DisplayName("ID를 통해서 멤버 가져오기") - void ID를_통해서_Member_불러오기() { - // given 멤버 저장 + @DisplayName("id를 통한 멤버 반환 성공") + void Given_Id_When_findById_Then_Member() { + // given final Long id = -1L; - // when 실행 - when(memberRepository.findById(id)).thenReturn(Optional.ofNullable(member)); + given(memberRepository.findById(id)).willReturn(Optional.ofNullable(member)); + + // when Member result = memberService.findMemberFromId(id); // then @@ -77,9 +79,9 @@ void setUp() { } @Test - @DisplayName("ID를 통해서 멤버 가져오기 실패") - void ID를_통해서_Member_불러오기_실패() { - // given 멤버 저장 + @DisplayName("존재하지 않는 id로 인한 NotFoundMemberException 반환") + void Given_NotExistId_When_findById_Throw_NotFoundMemberException() { + // given final Long id = -1L; given(memberRepository.findById(id)).willReturn(Optional.empty()); @@ -88,43 +90,48 @@ void setUp() { } @Test - @DisplayName("Member의 업데이트 성공") - void member_업데이트() { + @DisplayName("MemberServiceDto로 인한 Member 업데이트 성공") + void Given_MemberServiceDto_When_updateMember_Then_Member() { MemberServiceDto memberServiceDto = UPDATE_MEMBER_SERVICE_DTO(); + String expectedNickname = memberServiceDto.getNickname(); + + given(memberRepository.existMemberByNickname(anyString())).willReturn(false); + given(memberRepository.findByEmail(memberServiceDto.getEmail())).willReturn(Optional.ofNullable(member)); // when 멤버 업데이트 - when(memberRepository.existMemberByNickname(anyString())).thenReturn(false); - when(memberRepository.findByEmail(memberServiceDto.getEmail())).thenReturn(Optional.ofNullable(member)); memberService.updateMember(memberServiceDto); + String resultNickname = member.getNickname(); // then - assertThat(member.getNickname()).isEqualTo(memberServiceDto.getNickname()); + assertThat(resultNickname).isEqualTo(expectedNickname); } @Test - @DisplayName("Member의 업데이트 실패") - void member_업데이트_실패() { + @DisplayName("중복된 닉네임으로 인한 DuplicatedMemberException 반환") + void Given_MemberServiceDto_When_updateMember_Throw_DuplicatedMemberException() { + // given MemberServiceDto memberServiceDto = UPDATE_MEMBER_SERVICE_DTO(); + String duplicatedNickname = memberServiceDto.getNickname(); - // when 멤버 업데이트 - when(memberRepository.existMemberByNickname(anyString())).thenReturn(true); + given(memberRepository.existMemberByNickname(duplicatedNickname)).willReturn(true); // then assertThrows(DuplicatedMemberException.class, () -> memberService.updateMember(memberServiceDto)); } @Test - @DisplayName("단일 멤버 조회 성공") - void member_조회_성공() { + @DisplayName("MemberServiceDto를 통한 MemberResponse 반환 성공") + void Given_MemberServiceDto_When_findMemberResponse_Then_MemberResponse() { // given MemberServiceDto memberServiceDto = MEMBER_SERVICE_DTO(); - MemberResponse expectation = MemberResponse.of(member); + MemberResponse expectedResult = MemberResponse.of(member); + + given(memberRepository.findByEmail(memberServiceDto.getEmail())).willReturn(Optional.ofNullable(member)); // when - when(memberRepository.findByEmail(anyString())).thenReturn(Optional.ofNullable(member)); MemberResponse result = memberService.findMemberResponse(memberServiceDto); // then - assertThat(expectation).usingRecursiveComparison().isEqualTo(result); + assertThat(expectedResult).usingRecursiveComparison().isEqualTo(result); } } From 3a48ea41c680b463f12caa56ad8dbb80058fd756 Mon Sep 17 00:00:00 2001 From: JunRain Date: Sat, 25 May 2024 00:40:17 +0900 Subject: [PATCH 12/13] =?UTF-8?q?[#90]=20Meme=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EC=BD=94=EB=93=9C=20=EB=A9=94=EC=84=9C=EB=93=9C=20?= =?UTF-8?q?=EB=AA=85=20=EB=A6=AC=ED=8C=A9=ED=86=A0=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/meme/controller/MemeApi.java | 6 +- .../meme/controller/MemeController.java | 26 +++--- .../domain/meme/dto/MemeServiceDto.java | 6 +- .../meme/dto/ShotStackCallBackRequest.java | 2 +- .../domain/meme/service/MemeService.java | 15 ++-- .../domain/memes/service/MemesService.java | 2 +- .../memetory/domain/member/MemberFixture.java | 4 +- .../repository/MemberRepositoryTest.java | 2 +- .../member/service/MemberServiceTest.java | 4 +- .../memetory/domain/meme/MemeFixture.java | 6 +- .../meme/controller/MemeControllerTest.java | 52 ++++++----- .../meme/repository/MemeRepositoryTest.java | 48 ++++++----- .../domain/meme/service/MemeServiceTest.java | 86 ++++++++++--------- .../memes/service/MemesServiceTest.java | 2 +- 14 files changed, 138 insertions(+), 123 deletions(-) diff --git a/backend/memetory/src/main/java/com/example/memetory/domain/meme/controller/MemeApi.java b/backend/memetory/src/main/java/com/example/memetory/domain/meme/controller/MemeApi.java index 6c5cd27c..bc63aaa1 100644 --- a/backend/memetory/src/main/java/com/example/memetory/domain/meme/controller/MemeApi.java +++ b/backend/memetory/src/main/java/com/example/memetory/domain/meme/controller/MemeApi.java @@ -45,7 +45,7 @@ ResponseEntity callBackMeme( description = "밈 생성" ) }) - ResponseEntity register( + ResponseEntity registerMeme( @Parameter(hidden = true) String email, GenerateMemeListRequest generateMemeListRequest ); @@ -61,7 +61,7 @@ ResponseEntity register( description = "밈 단일 조회" ) }) - ResponseEntity findMeme( + ResponseEntity findMemberMemeResponse( @Parameter(hidden = true) String email, @Parameter(in = ParameterIn.PATH, description = "밈 아이디", required = true) Long memeId ); @@ -77,7 +77,7 @@ ResponseEntity findMeme( description = "밈 전체 조회" ) }) - ResponseEntity findMemePage( + ResponseEntity findMemberMemePageResponse( @Parameter(hidden = true) String email, @Parameter Pageable pageable ); diff --git a/backend/memetory/src/main/java/com/example/memetory/domain/meme/controller/MemeController.java b/backend/memetory/src/main/java/com/example/memetory/domain/meme/controller/MemeController.java index dbaa4047..8ba6bf2e 100644 --- a/backend/memetory/src/main/java/com/example/memetory/domain/meme/controller/MemeController.java +++ b/backend/memetory/src/main/java/com/example/memetory/domain/meme/controller/MemeController.java @@ -17,6 +17,8 @@ import org.springframework.web.reactive.function.client.WebClient; import com.example.memetory.domain.meme.dto.GenerateMemeListRequest; +import com.example.memetory.domain.meme.dto.MemePageResponse; +import com.example.memetory.domain.meme.dto.MemeResponse; import com.example.memetory.domain.meme.dto.MemeServiceDto; import com.example.memetory.domain.meme.dto.ShotStackCallBackRequest; import com.example.memetory.domain.meme.service.MemeService; @@ -38,17 +40,16 @@ public class MemeController implements MemeApi { @Override public ResponseEntity callBackMeme(@PathVariable Long memberId, @RequestBody ShotStackCallBackRequest shotStackCallBackRequest) { + MemeServiceDto memeServiceDto = shotStackCallBackRequest.toServiceDtoFromMemeberId(memberId); - MemeServiceDto memeServiceDto = shotStackCallBackRequest.toServiceDto(memberId); - - memeService.register(memeServiceDto); + memeService.registerMeme(memeServiceDto); return ResponseEntity.status(HttpStatus.OK).build(); } @PostMapping @Override - public ResponseEntity register(@LoginMemberEmail String email, + public ResponseEntity registerMeme(@LoginMemberEmail String email, @RequestBody GenerateMemeListRequest generateMemeListRequest) { MemeServiceDto memeServiceDto = generateMemeListRequest.toServiceDto(email); @@ -67,19 +68,20 @@ public ResponseEntity register(@LoginMemberEmail String email, @GetMapping("/{memeId}") @Override - public ResponseEntity findMeme(@LoginMemberEmail String email, @PathVariable Long memeId) { - MemeServiceDto memeServiceDto = MemeServiceDto.create(email, memeId); + public ResponseEntity findMemberMemeResponse(@LoginMemberEmail String email, @PathVariable Long memeId) { + MemeServiceDto memeServiceDto = MemeServiceDto.fromEmailAndMemeId(email, memeId); + MemeResponse response = memeService.findMemberMemeResponse(memeServiceDto); - return ResponseEntity.ok( - ResultResponse.of(GET_ONE_MEME_SUCCESS, memeService.getMeme(memeServiceDto))); + return ResponseEntity.ok(ResultResponse.of(GET_ONE_MEME_SUCCESS, response)); } @GetMapping @Override - public ResponseEntity findMemePage(@LoginMemberEmail String email, Pageable pageable) { - MemeServiceDto memeServiceDto = MemeServiceDto.create(email); + public ResponseEntity findMemberMemePageResponse(@LoginMemberEmail String email, + Pageable pageable) { + MemeServiceDto memeServiceDto = MemeServiceDto.fromEmail(email); + MemePageResponse response = memeService.findMemberMemePageResponse(memeServiceDto, pageable); - return ResponseEntity.ok( - ResultResponse.of(GET_MEMBER_MEME_SUCCESS, memeService.getAllMeme(memeServiceDto, pageable))); + return ResponseEntity.ok(ResultResponse.of(GET_MEMBER_MEME_SUCCESS, response)); } } diff --git a/backend/memetory/src/main/java/com/example/memetory/domain/meme/dto/MemeServiceDto.java b/backend/memetory/src/main/java/com/example/memetory/domain/meme/dto/MemeServiceDto.java index 280b6f11..fc39214c 100644 --- a/backend/memetory/src/main/java/com/example/memetory/domain/meme/dto/MemeServiceDto.java +++ b/backend/memetory/src/main/java/com/example/memetory/domain/meme/dto/MemeServiceDto.java @@ -19,20 +19,20 @@ public class MemeServiceDto { private String email; private List scene; - public static MemeServiceDto create(String email, Long memeId) { + public static MemeServiceDto fromEmailAndMemeId(String email, Long memeId) { return MemeServiceDto.builder() .email(email) .memeId(memeId) .build(); } - public static MemeServiceDto create(String email) { + public static MemeServiceDto fromEmail(String email) { return MemeServiceDto.builder() .email(email) .build(); } - public Meme toEntity(Member member) { + public Meme toEntityFromMember(Member member) { return Meme.builder() .member(member) .s3Url(this.s3Url) diff --git a/backend/memetory/src/main/java/com/example/memetory/domain/meme/dto/ShotStackCallBackRequest.java b/backend/memetory/src/main/java/com/example/memetory/domain/meme/dto/ShotStackCallBackRequest.java index e4d26c49..324eb0ca 100644 --- a/backend/memetory/src/main/java/com/example/memetory/domain/meme/dto/ShotStackCallBackRequest.java +++ b/backend/memetory/src/main/java/com/example/memetory/domain/meme/dto/ShotStackCallBackRequest.java @@ -19,7 +19,7 @@ public class ShotStackCallBackRequest { @Schema(description = "에러") private String error; - public MemeServiceDto toServiceDto(Long memberId) { + public MemeServiceDto toServiceDtoFromMemeberId(Long memberId) { return MemeServiceDto.builder() .memberId(memberId) .s3Url(url) diff --git a/backend/memetory/src/main/java/com/example/memetory/domain/meme/service/MemeService.java b/backend/memetory/src/main/java/com/example/memetory/domain/meme/service/MemeService.java index 5cb86c15..ca70f14a 100644 --- a/backend/memetory/src/main/java/com/example/memetory/domain/meme/service/MemeService.java +++ b/backend/memetory/src/main/java/com/example/memetory/domain/meme/service/MemeService.java @@ -26,11 +26,13 @@ public class MemeService { private final MemeRepository memeRepository; @Transactional - public MemeResponse register(MemeServiceDto memeServiceDto) { + public MemeResponse registerMeme(MemeServiceDto memeServiceDto) { Member member = memberService.findMemberFromId(memeServiceDto.getMemberId()); - Meme meme = memeServiceDto.toEntity(member); + Meme meme = memeServiceDto.toEntityFromMember(member); - return MemeResponse.of(memeRepository.save(meme)); + Meme savedMeme = memeRepository.save(meme); + + return MemeResponse.of(savedMeme); } @Transactional(readOnly = true) @@ -48,7 +50,7 @@ public String getAIServerSendJson(MemeServiceDto memeServiceDto) { } @Transactional(readOnly = true) - public MemeResponse getMeme(MemeServiceDto memeServiceDto) { + public MemeResponse findMemberMemeResponse(MemeServiceDto memeServiceDto) { Member member = memberService.findMemberFromId(memeServiceDto.getMemberId()); Meme meme = memeRepository.findById(memeServiceDto.getMemeId()).orElseThrow(NotFoundMemeException::new); @@ -60,7 +62,7 @@ public MemeResponse getMeme(MemeServiceDto memeServiceDto) { } @Transactional - public MemePageResponse getAllMeme(MemeServiceDto memeServiceDto, Pageable pageable) { + public MemePageResponse findMemberMemePageResponse(MemeServiceDto memeServiceDto, Pageable pageable) { Member member = memberService.findMemberFromEmail(memeServiceDto.getEmail()); Page memeList = memeRepository.findAllByMember(member, pageable); @@ -72,9 +74,8 @@ public MemePageResponse getAllMeme(MemeServiceDto memeServiceDto, Pageable pagea .build(); } - // Service 계층 끼리의 밈 조회 @Transactional(readOnly = true) - public Meme getMemeBetweenService(Long memeId) { + public Meme findMemeFromId(Long memeId) { return memeRepository.findById(memeId).orElseThrow(NotFoundMemeException::new); } } diff --git a/backend/memetory/src/main/java/com/example/memetory/domain/memes/service/MemesService.java b/backend/memetory/src/main/java/com/example/memetory/domain/memes/service/MemesService.java index 23c19964..fa340015 100644 --- a/backend/memetory/src/main/java/com/example/memetory/domain/memes/service/MemesService.java +++ b/backend/memetory/src/main/java/com/example/memetory/domain/memes/service/MemesService.java @@ -34,7 +34,7 @@ public class MemesService { @Transactional public void register(MemesServiceDto memesServiceDto) { Member member = memberService.findMemberFromEmail(memesServiceDto.getEmail()); - Meme meme = memeService.getMemeBetweenService(memesServiceDto.getMemeId()); + Meme meme = memeService.findMemeFromId(memesServiceDto.getMemeId()); Memes newMemes = memesServiceDto.toEntity(member, meme); memesRepository.save(newMemes); diff --git a/backend/memetory/src/test/java/com/example/memetory/domain/member/MemberFixture.java b/backend/memetory/src/test/java/com/example/memetory/domain/member/MemberFixture.java index fc020364..842721b4 100644 --- a/backend/memetory/src/test/java/com/example/memetory/domain/member/MemberFixture.java +++ b/backend/memetory/src/test/java/com/example/memetory/domain/member/MemberFixture.java @@ -18,7 +18,7 @@ public final static Member MEMBER() { .build(); } - public final static Member SECOND_MEMBER() { + public final static Member OTHER_MEMBER() { return Member.builder() .email("junrain@ourservice.com") .role(Role.USER) @@ -38,7 +38,7 @@ public final static MemberServiceDto MEMBER_SERVICE_DTO() { .build(); } - public final static MemberServiceDto UPDATE_MEMBER_SERVICE_DTO() { + public final static MemberServiceDto UPDATED_MEMBER_SERVICE_DTO() { return MemberServiceDto.builder() .imageUrl("imageUrl2") .nickname("junRain2") diff --git a/backend/memetory/src/test/java/com/example/memetory/domain/member/repository/MemberRepositoryTest.java b/backend/memetory/src/test/java/com/example/memetory/domain/member/repository/MemberRepositoryTest.java index 72029c28..35622000 100644 --- a/backend/memetory/src/test/java/com/example/memetory/domain/member/repository/MemberRepositoryTest.java +++ b/backend/memetory/src/test/java/com/example/memetory/domain/member/repository/MemberRepositoryTest.java @@ -71,7 +71,7 @@ public void Given_nickName_When_findByNickName_Then_True() { public void Given_MemberServiceDto_When_update() { // given Member savedMember = memberRepository.save(MEMBER()); - MemberServiceDto memberServiceDto = UPDATE_MEMBER_SERVICE_DTO(); + MemberServiceDto memberServiceDto = UPDATED_MEMBER_SERVICE_DTO(); // when Member expectedMember = updatedMember(savedMember, memberServiceDto); diff --git a/backend/memetory/src/test/java/com/example/memetory/domain/member/service/MemberServiceTest.java b/backend/memetory/src/test/java/com/example/memetory/domain/member/service/MemberServiceTest.java index 7bd6c316..8e66cb2a 100644 --- a/backend/memetory/src/test/java/com/example/memetory/domain/member/service/MemberServiceTest.java +++ b/backend/memetory/src/test/java/com/example/memetory/domain/member/service/MemberServiceTest.java @@ -92,7 +92,7 @@ void Given_NotExistId_When_findById_Throw_NotFoundMemberException() { @Test @DisplayName("MemberServiceDto로 인한 Member 업데이트 성공") void Given_MemberServiceDto_When_updateMember_Then_Member() { - MemberServiceDto memberServiceDto = UPDATE_MEMBER_SERVICE_DTO(); + MemberServiceDto memberServiceDto = UPDATED_MEMBER_SERVICE_DTO(); String expectedNickname = memberServiceDto.getNickname(); given(memberRepository.existMemberByNickname(anyString())).willReturn(false); @@ -110,7 +110,7 @@ void Given_MemberServiceDto_When_updateMember_Then_Member() { @DisplayName("중복된 닉네임으로 인한 DuplicatedMemberException 반환") void Given_MemberServiceDto_When_updateMember_Throw_DuplicatedMemberException() { // given - MemberServiceDto memberServiceDto = UPDATE_MEMBER_SERVICE_DTO(); + MemberServiceDto memberServiceDto = UPDATED_MEMBER_SERVICE_DTO(); String duplicatedNickname = memberServiceDto.getNickname(); given(memberRepository.existMemberByNickname(duplicatedNickname)).willReturn(true); diff --git a/backend/memetory/src/test/java/com/example/memetory/domain/meme/MemeFixture.java b/backend/memetory/src/test/java/com/example/memetory/domain/meme/MemeFixture.java index 014da479..70da9e2d 100644 --- a/backend/memetory/src/test/java/com/example/memetory/domain/meme/MemeFixture.java +++ b/backend/memetory/src/test/java/com/example/memetory/domain/meme/MemeFixture.java @@ -9,14 +9,10 @@ public static Meme MEME(Member member) { return Meme.builder().s3Url("s3Url").member(member).build(); } - public static Meme SECOND_MEME(Member member) { + public static Meme OTHER_MEME(Member member) { return Meme.builder().s3Url("s3Url2").member(member).build(); } - public static Meme THIRD_MEME(Member member) { - return Meme.builder().s3Url("s3Url3").member(member).build(); - } - public static MemeServiceDto MEME_SERVICE_DTO() { return MemeServiceDto.builder() .memberId(-1L) diff --git a/backend/memetory/src/test/java/com/example/memetory/domain/meme/controller/MemeControllerTest.java b/backend/memetory/src/test/java/com/example/memetory/domain/meme/controller/MemeControllerTest.java index 5dc45c88..a6391411 100644 --- a/backend/memetory/src/test/java/com/example/memetory/domain/meme/controller/MemeControllerTest.java +++ b/backend/memetory/src/test/java/com/example/memetory/domain/meme/controller/MemeControllerTest.java @@ -20,7 +20,6 @@ import com.example.memetory.domain.meme.dto.MemePageResponse; import com.example.memetory.domain.meme.dto.MemeResponse; import com.example.memetory.domain.meme.dto.MemeServiceDto; -import com.example.memetory.domain.meme.entity.Meme; import com.example.memetory.domain.meme.exception.AccessDeniedMemeException; import com.example.memetory.domain.meme.exception.NotFoundMemeException; import com.example.memetory.domain.meme.service.MemeService; @@ -38,15 +37,17 @@ public class MemeControllerTest extends LoginTest { */ @Test - @DisplayName("단일 밈 조회 성공") - void 단일_밈_조회_성공() throws Exception { - // given 예상될 MemeResponse 구현 - Meme meme = MEME(loginMember); - given(memeService.getMeme(any())).willReturn(MemeResponse.of(meme)); + @DisplayName("이메일과 밈id를 통한 멤버의 MemeResponse 반환 성공") + void Given_emailAndMemeId_When_findMemberMemeResponse_Then_Member_MemeResponse() throws Exception { + // given + MemeResponse returnedMemeResponse = MemeResponse.of(MEME(loginMember)); + given(memeService.findMemberMemeResponse(any())).willReturn(returnedMemeResponse); // when final ResultActions perform = mockMvc.perform( - get("/meme/-1").contentType(MediaType.APPLICATION_JSON).header("Authorization", "Bearer " + accessToken)) + get("/meme/-1") + .contentType(MediaType.APPLICATION_JSON) + .header("Authorization", "Bearer " + accessToken)) .andDo(print()); // then @@ -56,14 +57,16 @@ public class MemeControllerTest extends LoginTest { } @Test - @DisplayName("단일 밈 조회 실패, 로그인한 유저의 밈이 아닐 경우") - void 단일_밈_조회_실패_유저인증_실패() throws Exception { - // given 예상될 MemeResponse 구현 - given(memeService.getMeme(any(MemeServiceDto.class))).willThrow(new AccessDeniedMemeException()); + @DisplayName("권한이 없는 멤버 이메일로 인한 AccessDeniedMemeException 반환") + void Given_NotPermissionMemberId_When_findMemberMemeResponse_Throw_AccessDeniedMemberMemeException() throws Exception { + // given + given(memeService.findMemberMemeResponse(any(MemeServiceDto.class))).willThrow(new AccessDeniedMemeException()); // when final ResultActions perform = mockMvc.perform( - get("/meme/-1").contentType(MediaType.APPLICATION_JSON).header("Authorization", "Bearer " + accessToken)) + get("/meme/-1") + .contentType(MediaType.APPLICATION_JSON) + .header("Authorization", "Bearer " + accessToken)) .andDo(print()); // then @@ -72,15 +75,18 @@ public class MemeControllerTest extends LoginTest { } @Test - @DisplayName("단일 밈 조회 실패, 존재하지 않는 밈일 경우") - void 단일_밈_조회_실패_존재하지_않는_밈() throws Exception { - // given 예상될 MemeResponse 구현 - Meme meme = MEME(loginMember); - given(memeService.getMeme(any())).willThrow(new NotFoundMemeException()); + @DisplayName("존재하지 않는 memeId로 인한 NotFoundMemeException 반환") + void Given_NotExistMemeId_Then_findMemberMemeResponse_Throw_NotFoundMemberMemeException() throws Exception { + // given + Long notExistMemeId = -1L; + + given(memeService.findMemberMemeResponse(any(MemeServiceDto.class))).willThrow(new NotFoundMemeException()); // when final ResultActions perform = mockMvc.perform( - get("/meme/-1").contentType(MediaType.APPLICATION_JSON).header("Authorization", "Bearer " + accessToken)) + get("/meme/" + notExistMemeId) + .contentType(MediaType.APPLICATION_JSON) + .header("Authorization", "Bearer " + accessToken)) .andDo(print()); // then @@ -89,16 +95,16 @@ public class MemeControllerTest extends LoginTest { } @Test - @DisplayName("전체 밈 조회 성공") - void 전체_밈_조회_성공() throws Exception { - // given 예상될 MemeResponse 구현 - List memeList = List.of(MEME(loginMember), SECOND_MEME(loginMember)) + @DisplayName("이메일과 Pageable을 통한 멤버의 MemePageResponse 반환 성공") + void Given_emailAndPageable_When_findMemberMemePageResponse_Then_Member_MemePageResponse() throws Exception { + // given + List memeList = List.of(MEME(loginMember), OTHER_MEME(loginMember)) .stream() .map(MemeResponse::of) .toList(); MemePageResponse memePageResponse = MemePageResponse.builder().memeList(memeList).build(); - given(memeService.getAllMeme(any(), any())).willReturn(memePageResponse); + given(memeService.findMemberMemePageResponse(any(), any())).willReturn(memePageResponse); // when final ResultActions perform = mockMvc.perform( diff --git a/backend/memetory/src/test/java/com/example/memetory/domain/meme/repository/MemeRepositoryTest.java b/backend/memetory/src/test/java/com/example/memetory/domain/meme/repository/MemeRepositoryTest.java index 84392039..71d9868b 100644 --- a/backend/memetory/src/test/java/com/example/memetory/domain/meme/repository/MemeRepositoryTest.java +++ b/backend/memetory/src/test/java/com/example/memetory/domain/meme/repository/MemeRepositoryTest.java @@ -4,8 +4,6 @@ import static com.example.memetory.domain.meme.MemeFixture.*; import static org.assertj.core.api.Assertions.*; -import java.util.List; - import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -28,41 +26,45 @@ public class MemeRepositoryTest { @Autowired private MemberRepository memberRepository; - private Member savedMember; + private Member member; @BeforeEach void setUp() { - savedMember = memberRepository.save(MEMBER()); + member = memberRepository.save(MEMBER()); } @Test - @DisplayName("Meme 저장 확인과 Meme Id를 통한 조회 확인") - public void Meme_저장() { + @DisplayName("id를 통한 밈 반환 성공") + public void Given_id_Then_findById_Return_Meme() { + // given + Meme savedMeme = memeRepository.save(MEME(member)); + // when 밈 저장하기 - Meme savedMeme = memeRepository.save(MEME(savedMember)); + Meme result = memeRepository.findById(savedMeme.getId()).get(); // then 밈이 저장됐는지 확인 - assertThat(memeRepository.findById(savedMeme.getId()).get()).isEqualTo(savedMeme); + assertThat(result).isEqualTo(savedMeme); } @Test - @DisplayName("Member를 통한 사용자 Meme 전체 조회") - public void 전체_Meme_조회() { - // given DB에 밈과 유저를 저장 - Member anthorMember = memberRepository.save(SECOND_MEMBER()); - Meme firstMeme = MEME(savedMember); - Meme secondtMeme = SECOND_MEME(savedMember); - Meme anotherMemberMeme = MEME(anthorMember); - Pageable pageable = PageRequest.of(0, 10); + @DisplayName("Member를 통한 사용자 Page 반환 성공") + public void Given_Member_Then_findAllByMember_Then_Page_MemesResponse() { + // given + final int memeCount = 18; + final int pageSize = 10; + Member anthorMember = memberRepository.save(OTHER_MEMBER()); + Pageable pageable = PageRequest.of(0, pageSize); - List savedMemes = List.of(firstMeme, secondtMeme, anotherMemberMeme); - memeRepository.saveAll(savedMemes); + for (int i = 0; i < memeCount; i++) + memeRepository.save(MEME(member)); + for (int i = 0; i < 2; i++) + memeRepository.save(MEME(anthorMember)); - // when MEMBER를 통해서 MEME을 조회 - Page findMemes = memeRepository.findAllByMember(savedMember, pageable); + // when + Page findMemes = memeRepository.findAllByMember(member, pageable); - // then 일치하는지 조회 - assertThat(findMemes.getContent()).usingRecursiveComparison() - .isEqualTo(List.of(firstMeme, secondtMeme).stream().map(MemeResponse::of).toList()); + // then + assertThat(findMemes.getContent()).hasSize(pageSize); + assertThat(findMemes.getTotalElements()).isEqualTo(memeCount); } } diff --git a/backend/memetory/src/test/java/com/example/memetory/domain/meme/service/MemeServiceTest.java b/backend/memetory/src/test/java/com/example/memetory/domain/meme/service/MemeServiceTest.java index 3f39551b..057ebfff 100644 --- a/backend/memetory/src/test/java/com/example/memetory/domain/meme/service/MemeServiceTest.java +++ b/backend/memetory/src/test/java/com/example/memetory/domain/meme/service/MemeServiceTest.java @@ -6,6 +6,7 @@ import static org.junit.jupiter.api.Assertions.*; import static org.mockito.BDDMockito.*; +import java.util.ArrayList; import java.util.List; import java.util.Optional; @@ -51,80 +52,87 @@ void setUp() { } @Test - @DisplayName("밈 저장 성공") - void 밈_저장() { + @DisplayName("MemesServiceDto를 통한 밈 저장 성공") + void Given_MemesServiceDto_When_registerMeme_Then_Meme() { // given Meme을 세팅 MemeServiceDto memeServiceDto = MEME_SERVICE_DTO(); + Long memberId = memeServiceDto.getMemberId(); + MemeResponse expectedResult = MemeResponse.of(meme); - // when - when(memberService.findMemberFromId(memeServiceDto.getMemberId())).thenReturn(member); - when(memeRepository.save(any())).thenReturn(meme); + given(memberService.findMemberFromId(memberId)).willReturn(member); + given(memeRepository.save(any(Meme.class))).willReturn(meme); - MemeResponse result = memeService.register(memeServiceDto); + // when + MemeResponse result = memeService.registerMeme(memeServiceDto); // then - assertThat(result).isInstanceOf(MemeResponse.class); + assertThat(result).usingRecursiveComparison().isEqualTo(expectedResult); } @Test - @DisplayName("Meme ID를 통해 MemeResponse를 반환") - void 단일_밈_조회() { + @DisplayName("id를 통한 멤버의 MemeResponse를 반환 성공") + void Given_id_When_findMemberMemeResponse_Then_Member_MemeResponse() { // given 밈 저장, memServiceDto, memeReponse 생성 - MemeResponse expected = MemeResponse.of(meme); + MemeResponse expectedResult = MemeResponse.of(meme); MemeServiceDto memeServiceDto = MEME_SERVICE_DTO(); + given(memberService.findMemberFromId(memeServiceDto.getMemberId())).willReturn(member); + given(memeRepository.findById(memeServiceDto.getMemeId())).willReturn(Optional.ofNullable(meme)); + // when - when(memeRepository.findById(memeServiceDto.getMemeId())).thenReturn(Optional.ofNullable(meme)); - when(memberService.findMemberFromId(memeServiceDto.getMemberId())).thenReturn(member); - MemeResponse result = memeService.getMeme(memeServiceDto); + MemeResponse result = memeService.findMemberMemeResponse(memeServiceDto); // given - assertThat(result).usingRecursiveComparison().isEqualTo(expected); + assertThat(result).usingRecursiveComparison().isEqualTo(expectedResult); } @Test - @DisplayName("Meme ID를 통해 MemeResponse를 반환 실패") - void 단일_밈_조회_실패() { - // given 밈 저장, memServiceDto, memeReponse 생성 + @DisplayName("존재하지 않는 id로 인한 NotFoundMemeException 반환") + void Given_notExistId_When_findMemberMemeResponse_Throw_NotFoundMemberMemeException() { + // given MemeServiceDto memeServiceDto = MEME_SERVICE_DTO(); + Long notExistId = memeServiceDto.getMemeId(); - // when - when(memeRepository.findById(memeServiceDto.getMemeId())).thenReturn(Optional.empty()); + given(memeRepository.findById(notExistId)).willReturn(Optional.empty()); // given - assertThrows(NotFoundMemeException.class, () -> memeService.getMeme(memeServiceDto)); + assertThrows(NotFoundMemeException.class, () -> memeService.findMemberMemeResponse(memeServiceDto)); } @Test - @DisplayName("Member를 통한 해당 멤버의 전체 밈 조회") - void 전체_밈_조회() { - // given 밈들 저장, MemeListResponse 세팅 - Pageable page = PageRequest.of(0, 10); + @DisplayName("Member를 통한 멤버의 Page 조회 성공") + void Given_Member_When_findMemberMemePageResponse_Then_Page_Member_MemeResponse() { + // given + final int pageSize = 10; + Pageable page = PageRequest.of(0, pageSize); MemeServiceDto memeServiceDto = MEME_SERVICE_DTO(); - Member otherMember = SECOND_MEMBER(); - Meme secondtMeme = SECOND_MEME(member); - Meme anotherMemberMeme = MEME(otherMember); - + List memeList = createMemeResponseList(pageSize); - List memeList = List.of(meme, secondtMeme, anotherMemberMeme) - .stream() - .map(MemeResponse::of) - .toList(); + Page returnedMemeResponse = new PageImpl<>(memeList); - Page returned = new PageImpl<>(memeList); - - MemePageResponse expected = MemePageResponse.builder() + MemePageResponse expectedResult = MemePageResponse.builder() .totalPage(1) .currentPage(0) .memeList(memeList) .build(); - + + given(memeRepository.findAllByMember(any(), any())).willReturn(returnedMemeResponse); + // when - when(memeRepository.findAllByMember(any(), any())).thenReturn(returned); - MemePageResponse result = memeService.getAllMeme(memeServiceDto, page); + MemePageResponse result = memeService.findMemberMemePageResponse(memeServiceDto, page); // then - assertThat(result).usingRecursiveComparison().isEqualTo(expected); + assertThat(result).usingRecursiveComparison().isEqualTo(expectedResult); + } + + private List createMemeResponseList(int pageSize) { + List memeResponseList = new ArrayList<>(); + + for (int i = 0; i < pageSize; i++) { + memeResponseList.add(MemeResponse.of(MEME(member))); + } + + return memeResponseList; } } diff --git a/backend/memetory/src/test/java/com/example/memetory/domain/memes/service/MemesServiceTest.java b/backend/memetory/src/test/java/com/example/memetory/domain/memes/service/MemesServiceTest.java index 01fb4487..4812a4f3 100644 --- a/backend/memetory/src/test/java/com/example/memetory/domain/memes/service/MemesServiceTest.java +++ b/backend/memetory/src/test/java/com/example/memetory/domain/memes/service/MemesServiceTest.java @@ -58,7 +58,7 @@ void setUp() { void 밈스_저장() { // when when(memberService.findMemberFromEmail(memesServiceDto.getEmail())).thenReturn(member); - when(memeService.getMemeBetweenService(memesServiceDto.getMemeId())).thenReturn(meme); + when(memeService.findMemeFromId(memesServiceDto.getMemeId())).thenReturn(meme); memesService.register(memesServiceDto); From 9be396ece96c0b34d76ddc6fbf7bb3094d4fa8ab Mon Sep 17 00:00:00 2001 From: JunRain Date: Sat, 25 May 2024 04:06:33 +0900 Subject: [PATCH 13/13] =?UTF-8?q?[#90]=20Memes=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EC=BD=94=EB=93=9C=20=EB=A9=94=EC=84=9C=EB=93=9C=20?= =?UTF-8?q?=EB=AA=85=20=EB=A6=AC=ED=8C=A9=ED=86=A0=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../comment/service/CommentService.java | 4 +- .../complain/service/ComplainService.java | 2 +- .../domain/like/dto/LikeServiceDto.java | 4 +- .../domain/like/service/LikeService.java | 6 +- .../domain/meme/service/MemeService.java | 13 +- .../domain/memes/controller/MemesApi.java | 6 +- .../memes/controller/MemesController.java | 65 +++++----- .../domain/memes/dto/MemesServiceDto.java | 56 ++++----- .../dto/request/GenerateMemesRequest.java | 2 +- ...{MemesInfo.java => MemesInfoResponse.java} | 8 +- .../dto/response/MemesInfoSliceResponse.java | 6 +- ...on.java => AccessDinedMemesException.java} | 6 +- .../memes/repository/MemesQDtoFactory.java | 10 +- .../repository/MemesQueryRepository.java | 8 +- .../repository/MemesQueryRepositoryImpl.java | 10 +- .../domain/memes/service/MemesService.java | 75 +++++++----- .../memetory/global/response/ErrorCode.java | 4 +- .../like/repository/LikeRepositoryTest.java | 2 +- .../domain/like/service/LikeServiceTest.java | 10 +- .../meme/controller/MemeControllerTest.java | 2 +- .../memetory/domain/memes/MemesFixture.java | 1 + .../memes/controller/MemesControllerTest.java | 115 ++++++++++++------ .../memes/repository/MemesRepositoryTest.java | 108 ++++++++-------- .../memes/service/MemesServiceTest.java | 76 ++++++++---- 24 files changed, 345 insertions(+), 254 deletions(-) rename backend/memetory/src/main/java/com/example/memetory/domain/memes/dto/response/{MemesInfo.java => MemesInfoResponse.java} (85%) rename backend/memetory/src/main/java/com/example/memetory/domain/memes/exception/{NotDeleteMemesException.java => AccessDinedMemesException.java} (58%) diff --git a/backend/memetory/src/main/java/com/example/memetory/domain/comment/service/CommentService.java b/backend/memetory/src/main/java/com/example/memetory/domain/comment/service/CommentService.java index 1edb3097..fe33e725 100644 --- a/backend/memetory/src/main/java/com/example/memetory/domain/comment/service/CommentService.java +++ b/backend/memetory/src/main/java/com/example/memetory/domain/comment/service/CommentService.java @@ -23,7 +23,7 @@ public class CommentService { @Transactional public void register(CommentServiceDto commentServiceDto) { Member foundMember = memberService.findMemberFromEmail(commentServiceDto.getEmail()); - Memes foundMemes = memesService.getMemesBetweenService(commentServiceDto.getMemesId()); + Memes foundMemes = memesService.findMemesFromMemesId(commentServiceDto.getMemesId()); foundMemes.addCommentCount(); Comment newComment = commentServiceDto.toEntity(foundMember, foundMemes); @@ -34,7 +34,7 @@ public void register(CommentServiceDto commentServiceDto) { @Transactional public void delete(CommentServiceDto commentServiceDto) { Comment foundComment = commentRepository.findById(commentServiceDto.getCommentId()).orElseThrow(NotFoundCommentException::new); - Memes foundMemes = memesService.getMemesBetweenService(foundComment.getMemes().getId()); + Memes foundMemes = memesService.findMemesFromMemesId(foundComment.getMemes().getId()); foundMemes.cancelCommentCount(); commentRepository.delete(foundComment); diff --git a/backend/memetory/src/main/java/com/example/memetory/domain/complain/service/ComplainService.java b/backend/memetory/src/main/java/com/example/memetory/domain/complain/service/ComplainService.java index e8d174fb..d0a89fa0 100644 --- a/backend/memetory/src/main/java/com/example/memetory/domain/complain/service/ComplainService.java +++ b/backend/memetory/src/main/java/com/example/memetory/domain/complain/service/ComplainService.java @@ -23,7 +23,7 @@ public class ComplainService { @Transactional public void register(ComplainServiceDto complainServiceDto) { Member foundMember = memberService.findMemberFromEmail(complainServiceDto.getEmail()); - Memes foundMemes = memesService.getMemesBetweenService(complainServiceDto.getMemesId()); + Memes foundMemes = memesService.findMemesFromMemesId(complainServiceDto.getMemesId()); Complain newComplain = complainServiceDto.toEntity(foundMember, foundMemes); complainRepository.save(newComplain); diff --git a/backend/memetory/src/main/java/com/example/memetory/domain/like/dto/LikeServiceDto.java b/backend/memetory/src/main/java/com/example/memetory/domain/like/dto/LikeServiceDto.java index 55c2ead4..0d7edfed 100644 --- a/backend/memetory/src/main/java/com/example/memetory/domain/like/dto/LikeServiceDto.java +++ b/backend/memetory/src/main/java/com/example/memetory/domain/like/dto/LikeServiceDto.java @@ -14,14 +14,14 @@ public class LikeServiceDto { private Long memesId; private String email; - public static LikeServiceDto create(String email, Long memesId) { + public static LikeServiceDto fromEmailAndMemesId(String email, Long memesId) { return LikeServiceDto.builder() .email(email) .memesId(memesId) .build(); } - public Like toEntity(Member member, Memes memes) { + public Like toEntityFromMemberAndMemes(Member member, Memes memes) { return Like.builder() .member(member) .memes(memes) diff --git a/backend/memetory/src/main/java/com/example/memetory/domain/like/service/LikeService.java b/backend/memetory/src/main/java/com/example/memetory/domain/like/service/LikeService.java index bdceed30..3960e3cb 100644 --- a/backend/memetory/src/main/java/com/example/memetory/domain/like/service/LikeService.java +++ b/backend/memetory/src/main/java/com/example/memetory/domain/like/service/LikeService.java @@ -26,9 +26,9 @@ public class LikeService { @Transactional public void registerLike(LikeServiceDto likeServiceDto) { Member member = memberService.findMemberFromEmail(likeServiceDto.getEmail()); - Memes memes = memesService.getMemesBetweenService(likeServiceDto.getMemesId()); + Memes memes = memesService.findMemesFromMemesId(likeServiceDto.getMemesId()); - Like newLike = likeServiceDto.toEntity(member, memes); + Like newLike = likeServiceDto.toEntityFromMemberAndMemes(member, memes); saveLike(newLike); memes.addLikeCount(); } @@ -44,7 +44,7 @@ private void saveLike(Like like) { @Transactional public void cancelLike(LikeServiceDto likeServiceDto) { Member member = memberService.findMemberFromEmail(likeServiceDto.getEmail()); - Memes memes = memesService.getMemesBetweenService(likeServiceDto.getMemesId()); + Memes memes = memesService.findMemesFromMemesId(likeServiceDto.getMemesId()); Like like = likeRepository.findLikeByMemberAndMemes(member, memes).orElseThrow(NotFoundLikeException::new); likeRepository.delete(like); diff --git a/backend/memetory/src/main/java/com/example/memetory/domain/meme/service/MemeService.java b/backend/memetory/src/main/java/com/example/memetory/domain/meme/service/MemeService.java index ca70f14a..227b81b1 100644 --- a/backend/memetory/src/main/java/com/example/memetory/domain/meme/service/MemeService.java +++ b/backend/memetory/src/main/java/com/example/memetory/domain/meme/service/MemeService.java @@ -51,16 +51,21 @@ public String getAIServerSendJson(MemeServiceDto memeServiceDto) { @Transactional(readOnly = true) public MemeResponse findMemberMemeResponse(MemeServiceDto memeServiceDto) { - Member member = memberService.findMemberFromId(memeServiceDto.getMemberId()); Meme meme = memeRepository.findById(memeServiceDto.getMemeId()).orElseThrow(NotFoundMemeException::new); - if (meme.getMember() != member) { - throw new AccessDeniedMemeException(); - } + Member loginMember = memberService.findMemberFromId(memeServiceDto.getMemberId()); + Member memeMember = meme.getMember(); + certifyMemeMember(memeMember, loginMember); return MemeResponse.of(meme); } + public static void certifyMemeMember(Member m1, Member m2) { + if (!m1.equals(m2)) { + throw new AccessDeniedMemeException(); + } + } + @Transactional public MemePageResponse findMemberMemePageResponse(MemeServiceDto memeServiceDto, Pageable pageable) { Member member = memberService.findMemberFromEmail(memeServiceDto.getEmail()); diff --git a/backend/memetory/src/main/java/com/example/memetory/domain/memes/controller/MemesApi.java b/backend/memetory/src/main/java/com/example/memetory/domain/memes/controller/MemesApi.java index 277e3cc5..deb8e5ea 100644 --- a/backend/memetory/src/main/java/com/example/memetory/domain/memes/controller/MemesApi.java +++ b/backend/memetory/src/main/java/com/example/memetory/domain/memes/controller/MemesApi.java @@ -28,7 +28,7 @@ public interface MemesApi { description = "밈스 생성!" ) }) - ResponseEntity register( + ResponseEntity registerMemes( @Parameter(hidden = true) String email, GenerateMemesRequest generateMemesRequest ); @@ -99,7 +99,7 @@ ResponseEntity deleteMemes( description = "밈스 단일 조회" ) }) - ResponseEntity findMemes( + ResponseEntity findMemesResponse( @Parameter(in = ParameterIn.PATH, description = "밈스 아이디", required = true) Long memesId ); @@ -114,7 +114,7 @@ ResponseEntity findMemes( description = "밈스 전체 조회" ) }) - ResponseEntity findAllMemes(Pageable pageable); + ResponseEntity findMemesInfoSliceResponse(Pageable pageable); @Operation( summary = "meme`s 좋아요 등록", diff --git a/backend/memetory/src/main/java/com/example/memetory/domain/memes/controller/MemesController.java b/backend/memetory/src/main/java/com/example/memetory/domain/memes/controller/MemesController.java index a348a3e0..8110eacb 100644 --- a/backend/memetory/src/main/java/com/example/memetory/domain/memes/controller/MemesController.java +++ b/backend/memetory/src/main/java/com/example/memetory/domain/memes/controller/MemesController.java @@ -19,7 +19,7 @@ import com.example.memetory.domain.like.service.LikeService; import com.example.memetory.domain.memes.dto.MemesServiceDto; import com.example.memetory.domain.memes.dto.request.GenerateMemesRequest; -import com.example.memetory.domain.memes.dto.response.MemesInfo; +import com.example.memetory.domain.memes.dto.response.MemesInfoResponse; import com.example.memetory.domain.memes.dto.response.MemesInfoSliceResponse; import com.example.memetory.domain.memes.dto.response.MemesResponse; import com.example.memetory.domain.memes.service.MemesService; @@ -35,64 +35,65 @@ public class MemesController implements MemesApi { private final MemesService memesService; private final LikeService likeService; - @PostMapping + @GetMapping @Override - public ResponseEntity register(@LoginMemberEmail String email, - @RequestBody GenerateMemesRequest generateMemesRequest) { - memesService.register(generateMemesRequest.toServiceDto(email)); - - return ResponseEntity.status(HttpStatus.CREATED).body(ResultResponse.of(CREATE_MEMES_SUCCESS)); - } + public ResponseEntity findMemesInfoSliceResponse(Pageable pageable) { + MemesInfoSliceResponse response = memesService.findMemesInfoSliceResponse(pageable); - @GetMapping("/like/all") - @Override - public ResponseEntity findTopMemesByLike() { - return ResponseEntity.ok(ResultResponse.of(GET_TOP_TEN_MEMES_SUCCESS, memesService.getTopMemesByLike())); + return ResponseEntity.ok(ResultResponse.of(GET_ALL_MEMES_SUCCESS, response)); } - @GetMapping("/like/month") + @PostMapping @Override - public ResponseEntity findTopMemesByLikeForMonth() { - List response = memesService.getTopMemesByLikeForMonth(); + public ResponseEntity registerMemes(@LoginMemberEmail String email, + @RequestBody GenerateMemesRequest generateMemesRequest) { + MemesResponse response = memesService.registerMemes(generateMemesRequest.toServiceDtoFromEmail(email)); - return ResponseEntity.ok(ResultResponse.of(GET_MONTH_TOP_TEN_MEMES_SUCCESS, response)); + return ResponseEntity.status(HttpStatus.CREATED).body(ResultResponse.of(CREATE_MEMES_SUCCESS, response)); } - @GetMapping("/like/week") + @GetMapping("/{memesId}") @Override - public ResponseEntity findTopMemesByLikeForWeek() { - List response = memesService.getTopMemesByLikeForWeek(); + public ResponseEntity findMemesResponse(@PathVariable Long memesId) { + MemesResponse response = memesService.findMemesResponse(MemesServiceDto.fromMemesId(memesId)); - return ResponseEntity.ok(ResultResponse.of(GET_WEEK_TOP_TEN_MEMES_SUCCESS, response)); + return ResponseEntity.ok(ResultResponse.of(GET_ONE_MEMES_SUCCESS, response)); } @DeleteMapping("/{memesId}") @Override public ResponseEntity deleteMemes(@LoginMemberEmail String email, @PathVariable Long memesId) { - memesService.delete(MemesServiceDto.create(memesId, email)); + MemesServiceDto memesServiceDto = MemesServiceDto.fromMemesIdAndEmail(memesId, email); + + memesService.deleteMemes(memesServiceDto); return ResponseEntity.ok(ResultResponse.of(DELETE_MEMES_SUCCESS)); } - @GetMapping("/{memesId}") + @GetMapping("/like/all") @Override - public ResponseEntity findMemes(@PathVariable Long memesId) { - MemesResponse response = memesService.getMemesResponse(MemesServiceDto.create(memesId)); - - return ResponseEntity.ok(ResultResponse.of(GET_ONE_MEMES_SUCCESS, response)); + public ResponseEntity findTopMemesByLike() { + List response = memesService.findTopMemesByLike(); + return ResponseEntity.ok(ResultResponse.of(GET_TOP_TEN_MEMES_SUCCESS, response)); } - @GetMapping + @GetMapping("/like/month") @Override - public ResponseEntity findAllMemes(Pageable pageable) { - MemesInfoSliceResponse response = memesService.getMemesInfoSliceResponse(pageable); + public ResponseEntity findTopMemesByLikeForMonth() { + List response = memesService.findTopMemesByLikeForMonth(); + return ResponseEntity.ok(ResultResponse.of(GET_MONTH_TOP_TEN_MEMES_SUCCESS, response)); + } - return ResponseEntity.ok(ResultResponse.of(GET_ALL_MEMES_SUCCESS, response)); + @GetMapping("/like/week") + @Override + public ResponseEntity findTopMemesByLikeForWeek() { + List response = memesService.findTopMemesByLikeForWeek(); + return ResponseEntity.ok(ResultResponse.of(GET_WEEK_TOP_TEN_MEMES_SUCCESS, response)); } @PostMapping("/{memesId}/like") @Override public ResponseEntity registerLike(@LoginMemberEmail String email, @PathVariable Long memesId) { - LikeServiceDto likeServiceDto = LikeServiceDto.create(email, memesId); + LikeServiceDto likeServiceDto = LikeServiceDto.fromEmailAndMemesId(email, memesId); likeService.registerLike(likeServiceDto); return ResponseEntity.status(HttpStatus.CREATED).body(ResultResponse.of(CREATE_LIKE_SUCCESS)); @@ -101,7 +102,7 @@ public ResponseEntity registerLike(@LoginMemberEmail String emai @DeleteMapping("/{memesId}/like") @Override public ResponseEntity cancelLike(@LoginMemberEmail String email, @PathVariable Long memesId) { - LikeServiceDto likeServiceDto = LikeServiceDto.create(email, memesId); + LikeServiceDto likeServiceDto = LikeServiceDto.fromEmailAndMemesId(email, memesId); likeService.cancelLike(likeServiceDto); return ResponseEntity.ok(ResultResponse.of(DELETE_LIKE_SUCCESS)); diff --git a/backend/memetory/src/main/java/com/example/memetory/domain/memes/dto/MemesServiceDto.java b/backend/memetory/src/main/java/com/example/memetory/domain/memes/dto/MemesServiceDto.java index 4f55fd20..34e5b306 100644 --- a/backend/memetory/src/main/java/com/example/memetory/domain/memes/dto/MemesServiceDto.java +++ b/backend/memetory/src/main/java/com/example/memetory/domain/memes/dto/MemesServiceDto.java @@ -3,6 +3,7 @@ import com.example.memetory.domain.member.entity.Member; import com.example.memetory.domain.meme.entity.Meme; import com.example.memetory.domain.memes.entity.Memes; + import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Getter; @@ -11,35 +12,34 @@ @Builder @AllArgsConstructor public class MemesServiceDto { - private Long memberId; - private Long memeId; - private Long memesId; - private String email; - private String title; - private Long likeCount; - private Long commentCount; - - public static MemesServiceDto create(Long memesId) { - return MemesServiceDto.builder() - .memesId(memesId) - .build(); - } + private Long memberId; + private Long memeId; + private Long memesId; + private String email; + private String title; + private Long likeCount; + private Long commentCount; + public static MemesServiceDto fromMemesId(Long memesId) { + return MemesServiceDto.builder() + .memesId(memesId) + .build(); + } - public static MemesServiceDto create(Long memesId, String email) { - return MemesServiceDto.builder() - .memesId(memesId) - .email(email) - .build(); - } + public static MemesServiceDto fromMemesIdAndEmail(Long memesId, String email) { + return MemesServiceDto.builder() + .memesId(memesId) + .email(email) + .build(); + } - public Memes toEntity(Member member, Meme meme) { - return Memes.builder() - .member(member) - .meme(meme) - .title(this.title) - .likeCount(0L) - .commentCount(0L) - .build(); - } + public Memes toEntityFromMemberAndMeme(Member member, Meme meme) { + return Memes.builder() + .member(member) + .meme(meme) + .title(this.title) + .likeCount(0L) + .commentCount(0L) + .build(); + } } diff --git a/backend/memetory/src/main/java/com/example/memetory/domain/memes/dto/request/GenerateMemesRequest.java b/backend/memetory/src/main/java/com/example/memetory/domain/memes/dto/request/GenerateMemesRequest.java index 49f99751..d4043250 100644 --- a/backend/memetory/src/main/java/com/example/memetory/domain/memes/dto/request/GenerateMemesRequest.java +++ b/backend/memetory/src/main/java/com/example/memetory/domain/memes/dto/request/GenerateMemesRequest.java @@ -19,7 +19,7 @@ public class GenerateMemesRequest { @Schema(description = "밈스 제목") private String title; - public MemesServiceDto toServiceDto(String email) { + public MemesServiceDto toServiceDtoFromEmail(String email) { return MemesServiceDto.builder() .email(email) .memeId(memeId) diff --git a/backend/memetory/src/main/java/com/example/memetory/domain/memes/dto/response/MemesInfo.java b/backend/memetory/src/main/java/com/example/memetory/domain/memes/dto/response/MemesInfoResponse.java similarity index 85% rename from backend/memetory/src/main/java/com/example/memetory/domain/memes/dto/response/MemesInfo.java rename to backend/memetory/src/main/java/com/example/memetory/domain/memes/dto/response/MemesInfoResponse.java index 852c5537..29142722 100644 --- a/backend/memetory/src/main/java/com/example/memetory/domain/memes/dto/response/MemesInfo.java +++ b/backend/memetory/src/main/java/com/example/memetory/domain/memes/dto/response/MemesInfoResponse.java @@ -13,7 +13,7 @@ @Getter @NoArgsConstructor @Schema(description = "밈스 미리보기 정보") -public class MemesInfo { +public class MemesInfoResponse { @Schema(description = "밈스 아이디") private Long memesId; @@ -35,7 +35,7 @@ public class MemesInfo { @QueryProjection @Builder - public MemesInfo(Long memesId, String memberNickname, String title, Long commentCount, Long likeCount, + public MemesInfoResponse(Long memesId, String memberNickname, String title, Long commentCount, Long likeCount, LocalDateTime createdAt) { this.memesId = memesId; this.memberNickname = memberNickname; @@ -45,8 +45,8 @@ public MemesInfo(Long memesId, String memberNickname, String title, Long comment this.createdAt = createdAt; } - public static MemesInfo of(Memes memes) { - return MemesInfo.builder() + public static MemesInfoResponse of(Memes memes) { + return MemesInfoResponse.builder() .memesId(memes.getId()) .memberNickname(memes.getMember().getNickname()) .title(memes.getTitle()) diff --git a/backend/memetory/src/main/java/com/example/memetory/domain/memes/dto/response/MemesInfoSliceResponse.java b/backend/memetory/src/main/java/com/example/memetory/domain/memes/dto/response/MemesInfoSliceResponse.java index 6ba10d9e..d4f6fc42 100644 --- a/backend/memetory/src/main/java/com/example/memetory/domain/memes/dto/response/MemesInfoSliceResponse.java +++ b/backend/memetory/src/main/java/com/example/memetory/domain/memes/dto/response/MemesInfoSliceResponse.java @@ -17,12 +17,12 @@ public class MemesInfoSliceResponse { private boolean hasNext; @Schema(description = "밈스 리스트") - private List memesInfoList; + private List memesInfoResponseList; @Builder - public MemesInfoSliceResponse(int currentPage, boolean hasNext, List memesInfoList) { + public MemesInfoSliceResponse(int currentPage, boolean hasNext, List memesInfoResponseList) { this.currentPage = currentPage; this.hasNext = hasNext; - this.memesInfoList = memesInfoList; + this.memesInfoResponseList = memesInfoResponseList; } } diff --git a/backend/memetory/src/main/java/com/example/memetory/domain/memes/exception/NotDeleteMemesException.java b/backend/memetory/src/main/java/com/example/memetory/domain/memes/exception/AccessDinedMemesException.java similarity index 58% rename from backend/memetory/src/main/java/com/example/memetory/domain/memes/exception/NotDeleteMemesException.java rename to backend/memetory/src/main/java/com/example/memetory/domain/memes/exception/AccessDinedMemesException.java index b7a9eedb..3224f4a6 100644 --- a/backend/memetory/src/main/java/com/example/memetory/domain/memes/exception/NotDeleteMemesException.java +++ b/backend/memetory/src/main/java/com/example/memetory/domain/memes/exception/AccessDinedMemesException.java @@ -4,8 +4,8 @@ import com.example.memetory.global.exception.BusinessException; -public class NotDeleteMemesException extends BusinessException { - public NotDeleteMemesException() { - super(MEMES_NOT_DELETE); +public class AccessDinedMemesException extends BusinessException { + public AccessDinedMemesException() { + super(MEMES_ACCESS_DENY); } } diff --git a/backend/memetory/src/main/java/com/example/memetory/domain/memes/repository/MemesQDtoFactory.java b/backend/memetory/src/main/java/com/example/memetory/domain/memes/repository/MemesQDtoFactory.java index a2b53ac7..29f0c19d 100644 --- a/backend/memetory/src/main/java/com/example/memetory/domain/memes/repository/MemesQDtoFactory.java +++ b/backend/memetory/src/main/java/com/example/memetory/domain/memes/repository/MemesQDtoFactory.java @@ -5,20 +5,20 @@ import org.springframework.context.annotation.Bean; import org.springframework.stereotype.Component; -import com.example.memetory.domain.memes.dto.response.QMemesInfo; +import com.example.memetory.domain.memes.dto.response.QMemesInfoResponse; @Component public class MemesQDtoFactory { @Bean - public QMemesInfo qMemesInfo() { - return new QMemesInfo(memes.id, memes.member.nickname, memes.title, memes.commentCount, memes.likeCount, + public QMemesInfoResponse qMemesInfo() { + return new QMemesInfoResponse(memes.id, memes.member.nickname, memes.title, memes.commentCount, memes.likeCount, memes.createdAt); } @Bean - public QMemesInfo qMemesInfoSetLike() { - return new QMemesInfo(memes.id, memes.member.nickname, memes.title, memes.commentCount, + public QMemesInfoResponse qMemesInfoSetLike() { + return new QMemesInfoResponse(memes.id, memes.member.nickname, memes.title, memes.commentCount, memes.count(), memes.createdAt); } } diff --git a/backend/memetory/src/main/java/com/example/memetory/domain/memes/repository/MemesQueryRepository.java b/backend/memetory/src/main/java/com/example/memetory/domain/memes/repository/MemesQueryRepository.java index 5363c89a..deb464de 100644 --- a/backend/memetory/src/main/java/com/example/memetory/domain/memes/repository/MemesQueryRepository.java +++ b/backend/memetory/src/main/java/com/example/memetory/domain/memes/repository/MemesQueryRepository.java @@ -7,15 +7,15 @@ import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Slice; -import com.example.memetory.domain.memes.dto.response.MemesInfo; +import com.example.memetory.domain.memes.dto.response.MemesInfoResponse; import com.example.memetory.domain.memes.entity.Memes; public interface MemesQueryRepository { Optional findByMemesId(Long memesId); - Slice findAllMemesSlice(Pageable pageable); + Slice findMemesInfoSlice(Pageable pageable); - List findTopMemesOrderByLikeCount(); + List findTopMemesOrderByLikeCount(); - List findTopMemesByLikeCountForPeriod(LocalDateTime time); + List findTopMemesOrderByLikeCountForPeriod(LocalDateTime time); } diff --git a/backend/memetory/src/main/java/com/example/memetory/domain/memes/repository/MemesQueryRepositoryImpl.java b/backend/memetory/src/main/java/com/example/memetory/domain/memes/repository/MemesQueryRepositoryImpl.java index 395d9e2b..4d82cc1d 100644 --- a/backend/memetory/src/main/java/com/example/memetory/domain/memes/repository/MemesQueryRepositoryImpl.java +++ b/backend/memetory/src/main/java/com/example/memetory/domain/memes/repository/MemesQueryRepositoryImpl.java @@ -14,7 +14,7 @@ import org.springframework.data.domain.SliceImpl; import org.springframework.stereotype.Repository; -import com.example.memetory.domain.memes.dto.response.MemesInfo; +import com.example.memetory.domain.memes.dto.response.MemesInfoResponse; import com.example.memetory.domain.memes.entity.Memes; import com.querydsl.jpa.impl.JPAQueryFactory; @@ -38,9 +38,9 @@ public Optional findByMemesId(Long memesId) { } @Override - public Slice findAllMemesSlice(Pageable pageable) { + public Slice findMemesInfoSlice(Pageable pageable) { int pageSize = pageable.getPageSize(); - List memesList = jpaQueryFactory.select(memesQDtoFactory.qMemesInfo()) + List memesList = jpaQueryFactory.select(memesQDtoFactory.qMemesInfo()) .from(memes) .join(memes.member, member) .offset(pageable.getOffset()) @@ -57,7 +57,7 @@ public Slice findAllMemesSlice(Pageable pageable) { } @Override - public List findTopMemesOrderByLikeCount() { + public List findTopMemesOrderByLikeCount() { return jpaQueryFactory.select(memesQDtoFactory.qMemesInfo()) .from(memes) .join(memes.member, member) @@ -67,7 +67,7 @@ public List findTopMemesOrderByLikeCount() { } @Override - public List findTopMemesByLikeCountForPeriod(LocalDateTime time) { + public List findTopMemesOrderByLikeCountForPeriod(LocalDateTime time) { return jpaQueryFactory.select(memesQDtoFactory.qMemesInfoSetLike()) .from(like) .join(like.memes, memes) diff --git a/backend/memetory/src/main/java/com/example/memetory/domain/memes/service/MemesService.java b/backend/memetory/src/main/java/com/example/memetory/domain/memes/service/MemesService.java index fa340015..92244d36 100644 --- a/backend/memetory/src/main/java/com/example/memetory/domain/memes/service/MemesService.java +++ b/backend/memetory/src/main/java/com/example/memetory/domain/memes/service/MemesService.java @@ -14,11 +14,11 @@ import com.example.memetory.domain.meme.entity.Meme; import com.example.memetory.domain.meme.service.MemeService; import com.example.memetory.domain.memes.dto.MemesServiceDto; -import com.example.memetory.domain.memes.dto.response.MemesInfo; +import com.example.memetory.domain.memes.dto.response.MemesInfoResponse; import com.example.memetory.domain.memes.dto.response.MemesInfoSliceResponse; import com.example.memetory.domain.memes.dto.response.MemesResponse; import com.example.memetory.domain.memes.entity.Memes; -import com.example.memetory.domain.memes.exception.NotDeleteMemesException; +import com.example.memetory.domain.memes.exception.AccessDinedMemesException; import com.example.memetory.domain.memes.exception.NotFoundMemesException; import com.example.memetory.domain.memes.repository.MemesRepository; @@ -32,60 +32,77 @@ public class MemesService { private final MemesRepository memesRepository; @Transactional - public void register(MemesServiceDto memesServiceDto) { - Member member = memberService.findMemberFromEmail(memesServiceDto.getEmail()); + public MemesResponse registerMemes(MemesServiceDto memesServiceDto) { Meme meme = memeService.findMemeFromId(memesServiceDto.getMemeId()); - Memes newMemes = memesServiceDto.toEntity(member, meme); - memesRepository.save(newMemes); + Member member = memberService.findMemberFromEmail(memesServiceDto.getEmail()); + Member memeMember = meme.getMember(); + memeService.certifyMemeMember(member, memeMember); + + Memes memes = memesServiceDto.toEntityFromMemberAndMeme(member, meme); + Memes savedMemes = memesRepository.save(memes); + + return MemesResponse.of(savedMemes); } @Transactional - public void delete(MemesServiceDto memesServiceDto) { - Memes memes = findById(memesServiceDto.getMemesId()); - if (memes.getMember() != memberService.findMemberFromEmail(memesServiceDto.getEmail())) { - throw new NotDeleteMemesException(); - } + public void deleteMemes(MemesServiceDto memesServiceDto) { + Memes memes = findMemberMemes(memesServiceDto); + memesRepository.delete(memes); } + private Memes findMemberMemes(MemesServiceDto memesServiceDto) { + Memes memes = findMemesFromMemesId(memesServiceDto.getMemesId()); + + Member loginMember = memberService.findMemberFromEmail(memesServiceDto.getEmail()); + Member memesMember = memes.getMember(); + certifyMemesMember(loginMember, memesMember); + + return memes; + } + + private void certifyMemesMember(Member m1, Member m2) { + if (!m1.equals(m2)) { + throw new AccessDinedMemesException(); + } + } + + @Transactional(readOnly = true) + public MemesResponse findMemesResponse(MemesServiceDto memesServiceDto) { + Memes memes = findMemesFromMemesId(memesServiceDto.getMemesId()); + return MemesResponse.of(memes); + } + @Transactional(readOnly = true) - public MemesResponse getMemesResponse(MemesServiceDto memesServiceDto) { - return MemesResponse.of(findById(memesServiceDto.getMemesId())); + public Memes findMemesFromMemesId(Long memesId) { + return memesRepository.findByMemesId(memesId).orElseThrow(NotFoundMemesException::new); } @Transactional(readOnly = true) - public MemesInfoSliceResponse getMemesInfoSliceResponse(Pageable pageable) { - Slice memesSlice = memesRepository.findAllMemesSlice(pageable); + public MemesInfoSliceResponse findMemesInfoSliceResponse(Pageable pageable) { + Slice memesSlice = memesRepository.findMemesInfoSlice(pageable); return MemesInfoSliceResponse.builder() .currentPage(pageable.getPageNumber()) .hasNext(memesSlice.hasNext()) - .memesInfoList(memesSlice.getContent()) + .memesInfoResponseList(memesSlice.getContent()) .build(); } @Transactional(readOnly = true) - public List getTopMemesByLike() { + public List findTopMemesByLike() { return memesRepository.findTopMemesOrderByLikeCount(); } @Transactional(readOnly = true) - public List getTopMemesByLikeForMonth() { - return memesRepository.findTopMemesByLikeCountForPeriod(now().minusMonths(1)); + public List findTopMemesByLikeForMonth() { + return memesRepository.findTopMemesOrderByLikeCountForPeriod(now().minusMonths(1)); } @Transactional(readOnly = true) - public List getTopMemesByLikeForWeek() { - return memesRepository.findTopMemesByLikeCountForPeriod(now().minusWeeks(1)); + public List findTopMemesByLikeForWeek() { + return memesRepository.findTopMemesOrderByLikeCountForPeriod(now().minusWeeks(1)); } - @Transactional(readOnly = true) - public Memes getMemesBetweenService(Long memesId) { - return findById(memesId); - } - - private Memes findById(Long memesId) { - return memesRepository.findByMemesId(memesId).orElseThrow(NotFoundMemesException::new); - } } diff --git a/backend/memetory/src/main/java/com/example/memetory/global/response/ErrorCode.java b/backend/memetory/src/main/java/com/example/memetory/global/response/ErrorCode.java index 9af3178c..0eb28ca4 100644 --- a/backend/memetory/src/main/java/com/example/memetory/global/response/ErrorCode.java +++ b/backend/memetory/src/main/java/com/example/memetory/global/response/ErrorCode.java @@ -26,11 +26,11 @@ public enum ErrorCode { // Memes MEMES_NOT_FOUND(404, "밈스를 찾기 실패"), - MEMES_NOT_DELETE(403, "밈스 삭제 실패"), + MEMES_ACCESS_DENY(403, "밈스 접근 실패"), // Like LIKE_NOT_FOUND(404, "좋아요 찾기 실패"), - LIKE_NOT_CREATE(400, "중복된 좋아요"), + LIKE_NOT_CREATE(409, "중복된 좋아요"), // Complain COMPLAIN_NOT_FOUND(404, "신고 찾기 실패"), diff --git a/backend/memetory/src/test/java/com/example/memetory/domain/like/repository/LikeRepositoryTest.java b/backend/memetory/src/test/java/com/example/memetory/domain/like/repository/LikeRepositoryTest.java index 9a07ac4f..daf421c5 100644 --- a/backend/memetory/src/test/java/com/example/memetory/domain/like/repository/LikeRepositoryTest.java +++ b/backend/memetory/src/test/java/com/example/memetory/domain/like/repository/LikeRepositoryTest.java @@ -23,7 +23,7 @@ import com.example.memetory.domain.memes.repository.MemesRepository; import com.example.memetory.global.RepositoryTest; -@DisplayName("like 레포지토리 테스트의 ") +@DisplayName("Like 레포지토리 테스트의 ") @RepositoryTest public class LikeRepositoryTest { @Autowired diff --git a/backend/memetory/src/test/java/com/example/memetory/domain/like/service/LikeServiceTest.java b/backend/memetory/src/test/java/com/example/memetory/domain/like/service/LikeServiceTest.java index 891405b7..4c3615f0 100644 --- a/backend/memetory/src/test/java/com/example/memetory/domain/like/service/LikeServiceTest.java +++ b/backend/memetory/src/test/java/com/example/memetory/domain/like/service/LikeServiceTest.java @@ -53,10 +53,10 @@ void setUp() { @Test @DisplayName("likeServiceDto로 인한 Like 등록 성공") - void Given_likeServiceDto_When_registerLike_Execute_likeRepositorySave() { + void Given_likeServiceDto_When_registerLike_Execute_likeRepository_save() { // given given(memberService.findMemberFromEmail(likeServiceDto.getEmail())).willReturn(member); - given(memesService.getMemesBetweenService(likeServiceDto.getMemesId())).willReturn(memes); + given(memesService.findMemesFromMemesId(likeServiceDto.getMemesId())).willReturn(memes); // when likeService.registerLike(likeServiceDto); @@ -71,7 +71,7 @@ void Given_likeServiceDto_When_registerLike_Execute_likeRepositorySave() { void Given_likeServiceDto_When_registerLike_Throw_NotCreateLikeException() { // given given(memberService.findMemberFromEmail(likeServiceDto.getEmail())).willReturn(member); - given(memesService.getMemesBetweenService(likeServiceDto.getMemesId())).willReturn(memes); + given(memesService.findMemesFromMemesId(likeServiceDto.getMemesId())).willReturn(memes); given(likeRepository.save(any())).willThrow(new DataIntegrityViolationException(any())); // then @@ -82,11 +82,11 @@ void Given_likeServiceDto_When_registerLike_Throw_NotCreateLikeException() { @Test @DisplayName("likeServiceDto로 인한 Like 삭제 성공") - void Given_LikeServiceDto_when_cancelLike_Execute_likeRepositoryDelete() { + void Given_LikeServiceDto_when_cancelLike_Execute_likeRepository_delete() { // given Like like = LIKE(member, memes); given(memberService.findMemberFromEmail(likeServiceDto.getEmail())).willReturn(member); - given(memesService.getMemesBetweenService(likeServiceDto.getMemesId())).willReturn(memes); + given(memesService.findMemesFromMemesId(likeServiceDto.getMemesId())).willReturn(memes); given(likeRepository.findLikeByMemberAndMemes(member, memes)).willReturn(Optional.ofNullable(like)); // when diff --git a/backend/memetory/src/test/java/com/example/memetory/domain/meme/controller/MemeControllerTest.java b/backend/memetory/src/test/java/com/example/memetory/domain/meme/controller/MemeControllerTest.java index a6391411..a581ab8f 100644 --- a/backend/memetory/src/test/java/com/example/memetory/domain/meme/controller/MemeControllerTest.java +++ b/backend/memetory/src/test/java/com/example/memetory/domain/meme/controller/MemeControllerTest.java @@ -57,7 +57,7 @@ void Given_emailAndMemeId_When_findMemberMemeResponse_Then_Member_MemeResponse() } @Test - @DisplayName("권한이 없는 멤버 이메일로 인한 AccessDeniedMemeException 반환") + @DisplayName("권한 없는 멤버 이메일로 인한 AccessDeniedMemeException 반환") void Given_NotPermissionMemberId_When_findMemberMemeResponse_Throw_AccessDeniedMemberMemeException() throws Exception { // given given(memeService.findMemberMemeResponse(any(MemeServiceDto.class))).willThrow(new AccessDeniedMemeException()); diff --git a/backend/memetory/src/test/java/com/example/memetory/domain/memes/MemesFixture.java b/backend/memetory/src/test/java/com/example/memetory/domain/memes/MemesFixture.java index d8777f44..911e20a0 100644 --- a/backend/memetory/src/test/java/com/example/memetory/domain/memes/MemesFixture.java +++ b/backend/memetory/src/test/java/com/example/memetory/domain/memes/MemesFixture.java @@ -30,6 +30,7 @@ public static MemesServiceDto MEMES_SERVICE_DTO(){ return MemesServiceDto.builder() .memberId(-1L) .memeId(-1L) + .memesId(-1L) .email("junrain@ourservice.com") .title("new Memes") .build(); diff --git a/backend/memetory/src/test/java/com/example/memetory/domain/memes/controller/MemesControllerTest.java b/backend/memetory/src/test/java/com/example/memetory/domain/memes/controller/MemesControllerTest.java index 573112c6..94f249a5 100644 --- a/backend/memetory/src/test/java/com/example/memetory/domain/memes/controller/MemesControllerTest.java +++ b/backend/memetory/src/test/java/com/example/memetory/domain/memes/controller/MemesControllerTest.java @@ -19,14 +19,16 @@ import org.springframework.test.web.servlet.ResultActions; import com.example.memetory.domain.like.dto.LikeServiceDto; +import com.example.memetory.domain.like.exception.NotCreateLikeException; import com.example.memetory.domain.like.exception.NotFoundLikeException; import com.example.memetory.domain.like.service.LikeService; import com.example.memetory.domain.meme.entity.Meme; +import com.example.memetory.domain.memes.dto.MemesServiceDto; import com.example.memetory.domain.memes.dto.request.GenerateMemesRequest; -import com.example.memetory.domain.memes.dto.response.MemesInfo; +import com.example.memetory.domain.memes.dto.response.MemesInfoResponse; import com.example.memetory.domain.memes.dto.response.MemesInfoSliceResponse; import com.example.memetory.domain.memes.dto.response.MemesResponse; -import com.example.memetory.domain.memes.exception.NotDeleteMemesException; +import com.example.memetory.domain.memes.exception.AccessDinedMemesException; import com.example.memetory.domain.memes.service.MemesService; import com.example.memetory.global.LoginTest; @@ -39,27 +41,35 @@ public class MemesControllerTest extends LoginTest { private LikeService likeService; @Test - @DisplayName("Memes 생성") - public void 밈스_생성() throws Exception { + @DisplayName("GenerateMemesRequest을 통한 Memes 생성 성공") + public void Given_GenerateMemesRequest_When_registerMemes_Then_MemesResponse() throws Exception { // given + final String title = "new Memes"; + Meme meme = MEME(loginMember); + GenerateMemesRequest request = new GenerateMemesRequest(meme.getId(), title); + MemesResponse response = MemesResponse.of(MEMES(loginMember, meme)); + + given(memesService.registerMemes(any(MemesServiceDto.class))).willReturn(response); + // when final ResultActions perform = mockMvc.perform( post("/memes") .contentType(MediaType.APPLICATION_JSON) - .content(toRequestBody(new GenerateMemesRequest(meme.getId(), "new Title"))) + .content(toRequestBody(request)) .header("Authorization", "Bearer " + accessToken) ).andDo(print()); // then perform.andExpect(status().isCreated()) - .andExpect(jsonPath(MESSAGE).value(CREATE_MEMES_SUCCESS.getMessage())); + .andExpect(jsonPath(MESSAGE).value(CREATE_MEMES_SUCCESS.getMessage())) + .andExpect(jsonPath("$.data.title").value(title)); } @Test - @DisplayName("밈스 삭제") - public void 밈스_삭제() throws Exception { + @DisplayName("memesId를 통한 밈스 삭제 성공") + public void Given_memesId_When_deleteMemes_Then_DELETE_MEMES_SUCCESS() throws Exception { // when final ResultActions perform = mockMvc.perform( delete("/memes/-1") @@ -73,10 +83,10 @@ public class MemesControllerTest extends LoginTest { } @Test - @DisplayName("밈스 삭제 실패") - public void 밈스_삭제_실패() throws Exception { + @DisplayName("권한 없는 멤버로 인한 MEMES_ACCESS_DENY 반환") + public void Given_unAuthorizedMember_When_deleteMemes_Then_MEMES_ACCESS_DENY() throws Exception { // given - doThrow(new NotDeleteMemesException()).when(memesService).delete(any()); + doThrow(new AccessDinedMemesException()).when(memesService).deleteMemes(any()); // when final ResultActions perform = mockMvc.perform( @@ -87,12 +97,12 @@ public class MemesControllerTest extends LoginTest { // then perform.andExpect(status().isForbidden()) - .andExpect(jsonPath(ERROR_MESSAGE).value(MEMES_NOT_DELETE.getMessage())); + .andExpect(jsonPath(ERROR_MESSAGE).value(MEMES_ACCESS_DENY.getMessage())); } @Test - @DisplayName("좋아요 Top 10 밈스 조회") - public void TOP10_조회() throws Exception { + @DisplayName("올타임 좋아요 Top 10 밈스 반환 성공") + public void When_findTopMemesByLike_Then_GET_TOP_TEN_MEMES_SUCCESS() throws Exception { // when final ResultActions perform = mockMvc.perform( get("/memes/like/all") @@ -106,8 +116,8 @@ public class MemesControllerTest extends LoginTest { } @Test - @DisplayName("주간 좋아요 Top 10 밈스 조회") - public void 주간_TOP10_조회() throws Exception { + @DisplayName("주간 좋아요 Top 10 밈스 반환 성공") + public void When_findTopMemesByLikeForWeek_Then_GET_WEEK_TOP_TEN_MEMES_SUCCESS() throws Exception { // when final ResultActions perform = mockMvc.perform( get("/memes/like/week") @@ -121,11 +131,11 @@ public class MemesControllerTest extends LoginTest { } @Test - @DisplayName("밈스 단일 조회 성공") - public void 밈스_단일_조회_성공() throws Exception { + @DisplayName("밈스 id를 통한 MemesResponse 반환 성공") + public void Given_memesId_When_findMemesResponse_Thee_MemeResponse() throws Exception { Meme meme = MEME(loginMember); - MemesResponse result = MemesResponse.of(MEMES(loginMember, meme)); - given(memesService.getMemesResponse(any())).willReturn(result); + MemesResponse response = MemesResponse.of(MEMES(loginMember, meme)); + given(memesService.findMemesResponse(any())).willReturn(response); // when final ResultActions perform = mockMvc.perform( @@ -142,21 +152,23 @@ public class MemesControllerTest extends LoginTest { } @Test - @DisplayName("전체 밈스 슬라이스 조회 성공") - public void 전체_밈스_슬라이스_조회_성공() throws Exception { + @DisplayName("Pageable을 통한 MemesInfoSliceResponse 반환 성공") + public void Given_Pageable_When_findMemesInfoSliceResponse_Then_MemesInfoPageResponse() throws Exception { + final String page = "page=0&size=10"; Meme meme = MEME(loginMember); MemesInfoSliceResponse response = MemesInfoSliceResponse.builder() - .memesInfoList(List.of(MemesInfo.of(MEMES(loginMember, meme)))) + .memesInfoResponseList(List.of(MemesInfoResponse.of(MEMES(loginMember, meme)))) .currentPage(0) .hasNext(false) .build(); + given(memesService.findMemesInfoSliceResponse(any())).willReturn(response); - given(memesService.getMemesInfoSliceResponse(any())).willReturn(response); + String expectedTitle = response.getMemesInfoResponseList().get(0).getTitle(); // when final ResultActions perform = mockMvc.perform( - get("/memes?page=0&size=10") + get("/memes?" + page) .contentType(MediaType.APPLICATION_JSON) .header("Authorization", "Bearer " + accessToken) ).andDo(print()); @@ -164,15 +176,17 @@ public class MemesControllerTest extends LoginTest { // then perform.andExpect(status().isOk()).andExpectAll( jsonPath(MESSAGE).value(GET_ALL_MEMES_SUCCESS.getMessage()), - jsonPath("$.data.memesInfoList[0].title").value(response.getMemesInfoList().get(0).getTitle())); + jsonPath("$.data.memesInfoResponseList[0].title").value(expectedTitle)); } - @DisplayName("좋아요 등록 성공") @Test - void 좋아요_등록() throws Exception { + @DisplayName("memesId를 통한 좋아요 등록 성공") + void Given_memesId_When_registerLike_Then_CREATE_LIKE_SUCCESS() throws Exception { // when - final ResultActions perform = mockMvc.perform(post("/memes/-1/like").contentType(MediaType.APPLICATION_JSON) - .header("Authorization", "Bearer " + accessToken)).andDo(print()); + final ResultActions perform = mockMvc.perform(post("/memes/-1/like") + .contentType(MediaType.APPLICATION_JSON) + .header("Authorization", "Bearer " + accessToken)) + .andDo(print()); // then verify(likeService).registerLike(any(LikeServiceDto.class)); @@ -180,12 +194,32 @@ public class MemesControllerTest extends LoginTest { .andExpect(jsonPath(MESSAGE).value(CREATE_LIKE_SUCCESS.getMessage())); } - @DisplayName("좋아요 취소 성공") @Test - void 좋아요_취소() throws Exception { + @DisplayName("DB에 존재하는 좋아요로 인한 LIKE_NOT_CREATE 반환") + void Given_ExistLike_When_registerLike_Then_LIKE_NOT_CREATE() throws Exception { + // given + doThrow(new NotCreateLikeException()).when(likeService).registerLike(any(LikeServiceDto.class)); + // when - final ResultActions perform = mockMvc.perform(delete("/memes/-1/like").contentType(MediaType.APPLICATION_JSON) - .header("Authorization", "Bearer " + accessToken)).andDo(print()); + final ResultActions perform = mockMvc.perform(post("/memes/-1/like") + .contentType(MediaType.APPLICATION_JSON) + .header("Authorization", "Bearer " + accessToken)) + .andDo(print()); + + // then + verify(likeService).registerLike(any(LikeServiceDto.class)); + perform.andExpect(status().isConflict()) + .andExpect(jsonPath(ERROR_MESSAGE).value(LIKE_NOT_CREATE.getMessage())); + } + + @Test + @DisplayName("memesId를 통한 좋아요 삭제 성공") + void Given_memesId_When_cancelLike_Then_DELETE_LIKE_SUCCESS() throws Exception { + // when + final ResultActions perform = mockMvc.perform(delete("/memes/-1/like") + .contentType(MediaType.APPLICATION_JSON) + .header("Authorization", "Bearer " + accessToken)) + .andDo(print()); // then verify(likeService).cancelLike(any(LikeServiceDto.class)); @@ -193,14 +227,17 @@ public class MemesControllerTest extends LoginTest { .andExpect(jsonPath(MESSAGE).value(DELETE_LIKE_SUCCESS.getMessage())); } - @DisplayName("좋아요 취소 실패") @Test - void 좋아요_취소_실패() throws Exception { - // when + @DisplayName("DB에 없는 좋아요로 인한 LIKE_NOT_FOUND 반환") + void Given_NotExistLike_When_cancelLike_Then_LIKE_NOT_FOUND() throws Exception { + // given doThrow(new NotFoundLikeException()).when(likeService).cancelLike(any(LikeServiceDto.class)); - final ResultActions perform = mockMvc.perform(delete("/memes/-1/like").contentType(MediaType.APPLICATION_JSON) - .header("Authorization", "Bearer " + accessToken)).andDo(print()); + // when + final ResultActions perform = mockMvc.perform(delete("/memes/-1/like") + .contentType(MediaType.APPLICATION_JSON) + .header("Authorization", "Bearer " + accessToken)) + .andDo(print()); // then perform.andExpect(status().isNotFound()) diff --git a/backend/memetory/src/test/java/com/example/memetory/domain/memes/repository/MemesRepositoryTest.java b/backend/memetory/src/test/java/com/example/memetory/domain/memes/repository/MemesRepositoryTest.java index 13af6d7d..fb652d96 100644 --- a/backend/memetory/src/test/java/com/example/memetory/domain/memes/repository/MemesRepositoryTest.java +++ b/backend/memetory/src/test/java/com/example/memetory/domain/memes/repository/MemesRepositoryTest.java @@ -1,5 +1,6 @@ package com.example.memetory.domain.memes.repository; +import static com.example.memetory.domain.like.LikeFixture.*; import static com.example.memetory.domain.member.MemberFixture.*; import static com.example.memetory.domain.meme.MemeFixture.*; import static com.example.memetory.domain.memes.MemesFixture.*; @@ -22,11 +23,11 @@ import com.example.memetory.domain.member.repository.MemberRepository; import com.example.memetory.domain.meme.entity.Meme; import com.example.memetory.domain.meme.repository.MemeRepository; -import com.example.memetory.domain.memes.dto.response.MemesInfo; +import com.example.memetory.domain.memes.dto.response.MemesInfoResponse; import com.example.memetory.domain.memes.entity.Memes; import com.example.memetory.global.RepositoryTest; -@DisplayName("memes 레포지토리 테스트의 ") +@DisplayName("Memes 레포지토리 테스트의 ") @RepositoryTest public class MemesRepositoryTest { @Autowired @@ -48,47 +49,48 @@ void setUp() { } @Test - @DisplayName("Memes의 저장 및 단일 조회 테스트") - void MEMES_단일조회() { - // given Memes 세팅 - Memes savedMemes = memesRepository.save(MEMES(member, meme)); + @DisplayName("밈스 id를 통한 단일 밈스 조회 성공") + void Given_Memes_When_findByMemesId_Then_Memes() { + // given + Memes expectedResult = memesRepository.save(MEMES(member, meme)); // when - Memes expect = memesRepository.findByMemesId(savedMemes.getId()).get(); + Memes result = memesRepository.findByMemesId(expectedResult.getId()).get(); // then - assertThat(expect).isEqualTo(savedMemes); - assertThat(expect.getMember()).isEqualTo(member); - assertThat(expect.getMeme()).isEqualTo(meme); + assertThat(result).isEqualTo(expectedResult); } @Test - @DisplayName("Memes의 Slice 테스트") - void MEMES_슬라이스_조회() { - // given Memes 100 세팅 - for (int i = 0; i < 100; i++) { + @DisplayName("Pageable을 통한 Slice 반환") + void Given_Pageable_When_findMemesInfoSlice_Then_Slice_MemesInfo() { + // given + final int pageSize = 10; + + for (int i = 0; i < pageSize + 1; i++) { memesRepository.save(MEMES_SET_LIKE(member, meme, (long)i)); } - PageRequest page1 = PageRequest.of(0, 10); - PageRequest page2 = PageRequest.of(9, 10); - // when 실행 - Slice result1 = memesRepository.findAllMemesSlice(page1); - Slice result2 = memesRepository.findAllMemesSlice(page2); + + PageRequest page = PageRequest.of(0, pageSize); + + // when + Slice result = memesRepository.findMemesInfoSlice(page); // then - assertThat(result1.getContent()).hasSize(10); - assertFalse(result2.hasNext()); + assertThat(result.getContent()).hasSize(pageSize); + assertTrue(result.hasNext()); } @Test - @DisplayName("Memes의 좋아요 Top 10 테스트") - void MEMES_TOP_10() { - // given Memes 100 세팅 + @DisplayName("전체 조회수 Top10 List 반환") + void When_findTopMemesOrderByLikeCount_Then_List_MemesInfo() { + // given for (int i = 0; i < 100; i++) { memesRepository.save(MEMES_SET_LIKE(memberRepository.save(MEMBER()), meme, (long)i)); } - // when 실행 - List result = memesRepository.findTopMemesOrderByLikeCount(); + + // when + List result = memesRepository.findTopMemesOrderByLikeCount(); // then assertThat(result).hasSize(10); @@ -96,23 +98,17 @@ void MEMES_TOP_10() { assertThat(result.get(1).getLikeCount()).isEqualTo(98L); } + // TODO Like의 createdAt의 시간을 바꿔서 테스트를 진행해 봐야함. @Test - @DisplayName("Memes의 주별 좋아요 Top 10 테스트") - void MEMES_TOP_10_WEEK() { - // given Memes 100 세팅 - for (int i = 1; i <= 10; i++) { - Memes savedMemes = memesRepository.save(MEMES(member, meme)); - for (int j = 0; j < i * 2; j++) { - likeRepository.save(Like.builder() - .memes(savedMemes) - .member(memberRepository.save(MEMBER())) - .build()); - } - } - LocalDateTime time = LocalDateTime.now().minusWeeks(1); + @DisplayName("지난주를 통해 일주일동안 좋아요 Top 10 List 반한") + void Given_lastWeek_When_findTopMemesOrderByLikeCountForPeriod_Then_List_MemesInfo() { + // give + saveLikesSetOtherTime(); + + LocalDateTime lastWeek = LocalDateTime.now().minusWeeks(1); // when 실행 - List result = memesRepository.findTopMemesByLikeCountForPeriod(time); + List result = memesRepository.findTopMemesOrderByLikeCountForPeriod(lastWeek); // then assertThat(result).hasSize(10); @@ -120,22 +116,32 @@ void MEMES_TOP_10_WEEK() { assertThat(result.get(0).getMemberNickname()).isEqualTo(member.getNickname()); } + private void saveLikesSetOtherTime() { + for (int i = 1; i <= 10; i++) { + Memes savedMemes = memesRepository.save(MEMES(member, meme)); + + for (int j = 0; j < i * 2; j++) { + Member savedMember = memberRepository.save(MEMBER()); + Like saveLike = likeRepository.save(LIKE(savedMember, savedMemes)); + + saveLike.getCreatedAt().minusDays(i); + } + } + } + @Test - @DisplayName("Memes의 주별 좋아요 Top 10 해당하는 시간 없음") - void MEMES_TOP_10_AFTER_WEEK() { - // given Memes 100 세팅 + @DisplayName("다음주를 통해 좋아요 Top 10 빈 List 반환") + void Given_nextWeek_When_findTopMemesOrderByLikeCountForPeriod_Then_List_MemesInfo() { + // given Memes savedMemes = memesRepository.save(MEMES(member, meme)); - likeRepository.save(Like.builder() - .memes(savedMemes) - .member(member) - .build()); + likeRepository.save(LIKE(member, savedMemes)); - LocalDateTime time = LocalDateTime.now().plusWeeks(1); + LocalDateTime nextWeek = LocalDateTime.now().plusWeeks(1); - // when 실행 - List result = memesRepository.findTopMemesByLikeCountForPeriod(time); + // when + List result = memesRepository.findTopMemesOrderByLikeCountForPeriod(nextWeek); // then - assertThat(result).hasSize(0); + assertThat(result).isEmpty(); } } diff --git a/backend/memetory/src/test/java/com/example/memetory/domain/memes/service/MemesServiceTest.java b/backend/memetory/src/test/java/com/example/memetory/domain/memes/service/MemesServiceTest.java index 4812a4f3..acc5856a 100644 --- a/backend/memetory/src/test/java/com/example/memetory/domain/memes/service/MemesServiceTest.java +++ b/backend/memetory/src/test/java/com/example/memetory/domain/memes/service/MemesServiceTest.java @@ -17,22 +17,21 @@ import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; -import com.example.memetory.domain.member.dto.response.MemberResponse; import com.example.memetory.domain.member.entity.Member; import com.example.memetory.domain.member.service.MemberService; import com.example.memetory.domain.meme.entity.Meme; +import com.example.memetory.domain.meme.exception.AccessDeniedMemeException; import com.example.memetory.domain.meme.service.MemeService; import com.example.memetory.domain.memes.dto.MemesServiceDto; import com.example.memetory.domain.memes.dto.response.MemesResponse; import com.example.memetory.domain.memes.entity.Memes; -import com.example.memetory.domain.memes.exception.NotDeleteMemesException; +import com.example.memetory.domain.memes.exception.AccessDinedMemesException; import com.example.memetory.domain.memes.exception.NotFoundMemesException; import com.example.memetory.domain.memes.repository.MemesRepository; -@DisplayName("memesService 테스트의 ") +@DisplayName("Memes 서비스 테스트의 ") @ExtendWith(MockitoExtension.class) public class MemesServiceTest { - protected MemesServiceDto memesServiceDto; @InjectMocks private MemesService memesService; @Mock @@ -41,6 +40,8 @@ public class MemesServiceTest { private MemesRepository memesRepository; @Mock private MemeService memeService; + + private MemesServiceDto memesServiceDto; private Member member; private Meme meme; private Memes memes; @@ -54,61 +55,84 @@ void setUp() { } @Test - @DisplayName("밈스 저장 성공") - void 밈스_저장() { + @DisplayName("MemesServiceDto를 통한 Memes 저장 성공") + void Given_MemesServiceDto_When_registerMemes_Then_MemesResponse() { + // given + MemesResponse expectedResult = MemesResponse.of(memes); + + given(memberService.findMemberFromEmail(memesServiceDto.getEmail())).willReturn(member); + given(memeService.findMemeFromId(memesServiceDto.getMemeId())).willReturn(meme); + given(memesRepository.save(any(Memes.class))).willReturn(memes); + // when - when(memberService.findMemberFromEmail(memesServiceDto.getEmail())).thenReturn(member); - when(memeService.findMemeFromId(memesServiceDto.getMemeId())).thenReturn(meme); + MemesResponse result = memesService.registerMemes(memesServiceDto); - memesService.register(memesServiceDto); + // then + assertThat(result).usingRecursiveComparison().isEqualTo(expectedResult); + } + + @Test + @DisplayName("권한 없는 멤버로 인한 NotAccessMemeException 반환") + void Given_unAuthorizedMember_When_registerMemes_Throw_NotAccessMemeException() { + // given + Member unAuthorizedMember = OTHER_MEMBER(); + + given(memeService.findMemeFromId(memesServiceDto.getMemeId())).willReturn(meme); + given(memberService.findMemberFromEmail(any(String.class))).willReturn(unAuthorizedMember); // then - verify(memesRepository).save(any()); + assertThrows(AccessDeniedMemeException.class, () -> memesService.registerMemes(memesServiceDto)); } @Test - @DisplayName("밈스 삭제 성공") - void 밈스_삭제() { + @DisplayName("MemesServiceDto를 통한 밈스 제거 성공") + void Given_MemesServiceDto_When_deleteMemes_Execute_MemesRepository_delete() { // given given(memesRepository.findByMemesId(any())).willReturn(Optional.ofNullable(memes)); given(memberService.findMemberFromEmail(any())).willReturn(member); // when - memesService.delete(memesServiceDto); + memesService.deleteMemes(memesServiceDto); // then verify(memesRepository).delete(any()); } @Test - @DisplayName("밈스 삭제 실패") - void 밈스_삭제_실패() { + @DisplayName("권한 없는 멤버로 인한 NotAccessMemesException 반환") + void Given_unAuthorizedMember_When_deleteMemes_Throw_NotAccessMemesException() { // given + Member unAuthorizedMember = OTHER_MEMBER(); + given(memesRepository.findByMemesId(any())).willReturn(Optional.ofNullable(memes)); - given(memberService.findMemberFromEmail(any())).willReturn(any()); + given(memberService.findMemberFromEmail(any())).willReturn(unAuthorizedMember); - // when - assertThrows(NotDeleteMemesException.class, () -> memesService.delete(memesServiceDto)); + // then + assertThrows(AccessDinedMemesException.class, () -> memesService.deleteMemes(memesServiceDto)); } @Test - @DisplayName("밈스 단일조회 성공") - void 밈스_단일_조회() { + @DisplayName("MemesServiceDto를 통한 단일 MemesResponse 반환 성공") + void Given_MemesServiceDto_When_findMemesResponse_Then_MemesResponse() { // given + MemesResponse expectedResult = MemesResponse.of(memes); + given(memesRepository.findByMemesId(any())).willReturn(Optional.ofNullable(memes)); - MemesResponse result = memesService.getMemesResponse(memesServiceDto); + MemesResponse result = memesService.findMemesResponse(memesServiceDto); // when - assertThat(result.getMember().getMemberId()).isEqualTo(MemberResponse.of(member).getMemberId()); + assertThat(result).usingRecursiveComparison().isEqualTo(expectedResult); } @Test - @DisplayName("밈스 단일조회 실패") - void 밈스_단일_조회_실패() { + @DisplayName("존재하지 않는 MemesId로 인한 NotFoundMemesException 반환") + void Given_notExistMemesId_When_findMemesResponse_Throw_MemesResponse() { // given - given(memesRepository.findByMemesId(any())).willReturn(Optional.ofNullable(any())); + Long notExistMemesId = -1L; + + given(memesRepository.findByMemesId(notExistMemesId)).willReturn(Optional.empty()); // then - assertThrows(NotFoundMemesException.class, () -> memesService.getMemesResponse(memesServiceDto)); + assertThrows(NotFoundMemesException.class, () -> memesService.findMemesResponse(memesServiceDto)); } }