Skip to content

Commit

Permalink
[Weekly/11/Test/Like] LikeControllerUnitTest 생성 (#141)
Browse files Browse the repository at this point in the history
* test: Event Unit Test 리팩터링

* feat: Like 리팩터링

* feat: LikeData 추가

* test: LikeControllerUnitTest 생성
  • Loading branch information
ariimo authored Nov 14, 2024
1 parent e27b377 commit 0a2cbee
Show file tree
Hide file tree
Showing 10 changed files with 699 additions and 282 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,28 @@ public class LikeController {

@GetMapping
public ResponseEntity<ApiResponseBody<LikeSliceResponse>> getLikes(
@Authorize(MemberType.normal) MemberIdentifier identifier,
@Authorize({MemberType.normal, MemberType.curator}) MemberIdentifier identifier,
@RequestParam("type") MemberType memberType,
@RequestParam(defaultValue = ParamDefaults.PAGE) Integer page,
@RequestParam(defaultValue = ParamDefaults.PAGE_SIZE) Integer size,
@RequestParam(defaultValue = ParamDefaults.LAST_ID) Long lastId
) {
return ApiResponse.ok(
likeServiceFactory.getLikeServiceFrom(memberType)
.getLikes(identifier.id(), PageRequest.of(page, size), lastId));
likeServiceFactory
.getLikeServiceFrom(memberType)
.getLikes(identifier, PageRequest.of(page, size), lastId));
}

@PostMapping("/{targetMemberId}")
public ResponseEntity<ApiResponseBody<LikeToggleResponse>> createOrDeleteLike(
@Authorize(MemberType.normal) MemberIdentifier identifier,
@Authorize({MemberType.normal, MemberType.curator}) MemberIdentifier identifier,
@RequestParam("type") MemberType memberType,
@PathVariable("targetMemberId") Long targetId) {
return ApiResponse.created(
likeServiceFactory.getLikeServiceFrom(targetId).toggleLike(identifier.id(), targetId));
likeServiceFactory.getLikeServiceFrom(memberType)
.toggleLike(identifier, targetId));
}
}

// TODO: Like service에 Identifier 넘겨주기
// TODO: Review service에 Identifier 넘겨주기
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.List;
import lombok.RequiredArgsConstructor;
import org.ktc2.cokaen.wouldyouin.auth.MemberIdentifier;
import org.ktc2.cokaen.wouldyouin.like.api.dto.LikeResponse;
import org.ktc2.cokaen.wouldyouin.like.api.dto.LikeSliceResponse;
import org.ktc2.cokaen.wouldyouin.like.api.dto.LikeToggleResponse;
Expand Down Expand Up @@ -31,18 +32,18 @@ public abstract class LikeService<LikeType extends Like<? extends LikeableMember
public abstract MemberType getTargetLikeableMemberType();

@Transactional(readOnly = true)
public LikeSliceResponse getLikes(Long memberId, Pageable pageable, Long beforeLastId) {
public LikeSliceResponse getLikes(MemberIdentifier identifier, Pageable pageable, Long beforeLastId) {
Slice<LikeType> likes = getLikeRepository().findAllByMember(
memberService.getByIdOrThrow(memberId), beforeLastId, pageable);
memberService.getByIdOrThrow(identifier.id()), beforeLastId, pageable);
Long newLastId = getLastId(likes, beforeLastId);
List<LikeResponse> responses = likes.stream()
.map(like -> LikeResponse.from(like.getLikeableMember())).toList();
return LikeSliceResponse.from(responses, likes.getSize(), newLastId);
}

@Transactional
public LikeToggleResponse toggleLike(Long memberId, Long targetMemberId) {
Member member = memberService.getByIdOrThrow(memberId);
public LikeToggleResponse toggleLike(MemberIdentifier identifier, Long targetMemberId) {
Member member = memberService.getByIdOrThrow(identifier.id());
LikeableMember targetLikeableMember = getLikeableMemberByIdOrThrow(targetMemberId);
return getLikeRepository().findByMemberAndLikeableMember(member, targetLikeableMember)
.map(like -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

package org.ktc2.cokaen.wouldyouin.like.api.dto;

import java.util.List;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.ktc2.cokaen.wouldyouin.member.persist.LikeableMember;

@Getter
@RequiredArgsConstructor(access = AccessLevel.PROTECTED)
@Builder
public class LikeResponse {

private final Long memberId;
private final String nickname;
private final String intro;
private final List<String> hashtags;
private final String profileImageUrl;

public static LikeResponse from(LikeableMember member) {
return LikeResponse.builder()
.memberId(member.getId())
.nickname(member.getNickname())
.intro(member.getIntro())
.hashtags(member.getHashtags())
.profileImageUrl(member.getProfileImageUrl())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.ktc2.cokaen.wouldyouin.like.application.dto;

import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.ktc2.cokaen.wouldyouin.member.persist.LikeableMember;

@Getter
@RequiredArgsConstructor(access = AccessLevel.PROTECTED)
@Builder
public class LikeToggleResponse {
private final boolean isLiked;

public static LikeToggleResponse from(boolean state) {
return LikeToggleResponse.builder()
.isLiked(state)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
@Retention(RetentionPolicy.RUNTIME)
@WithMockCustomUser(memberId = curator1.id, memberType = MemberType.curator)
public @interface WithMockCurator1 {

// TODO: mockMember 패키지명 변경
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import org.ktc2.cokaen.wouldyouin._common.api.SliceInfo;
import org.ktc2.cokaen.wouldyouin._global.testdata.CurationData.R.curation1;
import org.ktc2.cokaen.wouldyouin._global.testdata.MemberData.R;
import org.ktc2.cokaen.wouldyouin._global.testdata.MemberData.R.curator1;
import org.ktc2.cokaen.wouldyouin._global.testdata.MemberData.R.host1;
import org.ktc2.cokaen.wouldyouin._global.testdata.ReservationData.R.reservation1;
import org.ktc2.cokaen.wouldyouin._global.testdata.ReviewData.R.review1;

Expand All @@ -26,13 +28,36 @@ public static SliceInfo get() {
}

public static class like {

public static SliceInfo get() {
return SliceInfo.builder()
.sliceSize(2)
.lastId(curator1.id)
.build();
public static class normal1 {
public static class hostLikes {
public static SliceInfo get() {
return SliceInfo.builder()
.sliceSize(1)
.lastId(host1.id)
.build();
}
}
public static class curatorLikes {
public static SliceInfo get() {
return SliceInfo.builder()
.sliceSize(1)
.lastId(R.curator1.id)
.build();
}
}
}
public static class curator1 {
public static class hostLikes {
public static SliceInfo get() {
return SliceInfo.builder()
.sliceSize(1)
.lastId(host1.id)
.build();
}
}
}


}

public static class reservation {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package org.ktc2.cokaen.wouldyouin._global.testdata;

import java.util.List;
import org.ktc2.cokaen.wouldyouin._global.testdata.LikeData.R.likeHost1.byNormal1;
import org.ktc2.cokaen.wouldyouin._global.testdata.LikeData.R.likeHost1.byNormal1.create;
import org.ktc2.cokaen.wouldyouin.like.api.dto.LikeResponse;
import org.ktc2.cokaen.wouldyouin.like.api.dto.LikeSliceResponse;
import org.ktc2.cokaen.wouldyouin.like.api.dto.LikeToggleResponse;
import org.ktc2.cokaen.wouldyouin.like.persist.CuratorLike;
import org.ktc2.cokaen.wouldyouin.like.persist.HostLike;
import org.ktc2.cokaen.wouldyouin.member.persist.Curator;
Expand All @@ -29,6 +32,10 @@ public static class response {
public static final List<String> hashtags = MemberData.R.host1.hashtags;
public static final String profileImageUrl = MemberData.R.host1.profileImageUrl;
}
public static class create{
public static final Boolean created = true;
public static final Boolean deleted = false;
}
}
public static class byCurator1 {
public static class _Relation {
Expand All @@ -46,6 +53,10 @@ public static class response {
public static final List<String> hashtags = MemberData.R.host1.hashtags;
public static final String profileImageUrl = MemberData.R.host1.profileImageUrl;
}
public static class create{
public static final Boolean created = true;
public static final Boolean deleted = false;
}
}
}
public static class likeCurator1 {
Expand Down Expand Up @@ -137,28 +148,71 @@ public static LikeResponse get() {
}
}
}
public static class toggleResponse{
public static class hostLikes {
public static class create {

public static class normal1LikeSliceResponse {
public static LikeSliceResponse get() {
return LikeSliceResponse.builder()
.likes(List.of(
LikeResponse.builder()
.memberId(R.likeHost1.byNormal1.response.id)
.nickname(R.likeHost1.byNormal1.response.nickname)
.intro(R.likeHost1.byNormal1.response.intro)
.hashtags(R.likeHost1.byNormal1.response.hashtags)
.profileImageUrl(R.likeHost1.byNormal1.response.profileImageUrl)
.build(),
LikeResponse.builder()
.memberId(R.likeCurator1.byNormal1.response.id)
.nickname(R.likeCurator1.byNormal1.response.nickname)
.intro(R.likeCurator1.byNormal1.response.intro)
.hashtags(R.likeCurator1.byNormal1.response.hashtags)
.profileImageUrl(R.likeCurator1.byNormal1.response.profileImageUrl)
.build()
))
.sliceInfo(CommonData.sliceInfo.like.get())
.build();
public static LikeToggleResponse get() {
return LikeToggleResponse.builder().isLiked(byNormal1.create.created).build();
}
}
public static class delete {

public static LikeToggleResponse get() {
return LikeToggleResponse.builder().isLiked(byNormal1.create.deleted).build();
}
}
}
}
public static class sliceResponse {
public static class normal1 {
public static class hostLikes {
public static LikeSliceResponse get() {
return LikeSliceResponse.builder()
.likes(List.of(
LikeResponse.builder()
.memberId(R.likeHost1.byNormal1.response.id)
.nickname(R.likeHost1.byNormal1.response.nickname)
.intro(R.likeHost1.byNormal1.response.intro)
.hashtags(R.likeHost1.byNormal1.response.hashtags)
.profileImageUrl(R.likeHost1.byNormal1.response.profileImageUrl)
.build()))
.sliceInfo(CommonData.sliceInfo.like.normal1.hostLikes.get())
.build();
}
}
public static class curatorLikes {
public static LikeSliceResponse get() {
return LikeSliceResponse.builder()
.likes(List.of(
LikeResponse.builder()
.memberId(R.likeCurator1.byNormal1.response.id)
.nickname(R.likeCurator1.byNormal1.response.nickname)
.intro(R.likeCurator1.byNormal1.response.intro)
.hashtags(R.likeCurator1.byNormal1.response.hashtags)
.profileImageUrl(R.likeCurator1.byNormal1.response.profileImageUrl)
.build()))
.sliceInfo(CommonData.sliceInfo.like.normal1.curatorLikes.get())
.build();
}
}
}
public static class curator1 {
public static class hostLikes {
public static LikeSliceResponse get() {
return LikeSliceResponse.builder()
.likes(List.of(
LikeResponse.builder()
.memberId(R.likeHost1.byCurator1.response.id)
.nickname(R.likeHost1.byCurator1.response.nickname)
.intro(R.likeHost1.byCurator1.response.intro)
.hashtags(R.likeHost1.byCurator1.response.hashtags)
.profileImageUrl(R.likeHost1.byCurator1.response.profileImageUrl)
.build()))
.sliceInfo(CommonData.sliceInfo.like.curator1.hostLikes.get())
.build();
}
}
}
}
}
Loading

0 comments on commit 0a2cbee

Please sign in to comment.