Skip to content

Commit

Permalink
Merge pull request #454 from depromeet/test/453-notification
Browse files Browse the repository at this point in the history
test: ์•Œ๋ฆผ ๋„๋ฉ”์ธ ์ „์ฒด ํ…Œ์ŠคํŠธ์ฝ”๋“œ ์ž‘์„ฑ
  • Loading branch information
its-sky authored Nov 4, 2024
2 parents 5a139da + 43b72c0 commit 5e12a75
Show file tree
Hide file tree
Showing 8 changed files with 441 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,8 @@ public FollowLog(
this.createdAt = createdAt;
this.hasRead = hasRead;
}

public void read() {
this.hasRead = true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,8 @@ public ReactionLog(
this.createdAt = createdAt;
this.hasRead = hasRead;
}

public void read() {
this.hasRead = true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package com.depromeet.mock.notification;

import com.depromeet.friend.domain.Friend;
import com.depromeet.member.domain.Member;
import com.depromeet.notification.domain.FollowLog;
import com.depromeet.notification.port.out.FollowLogPersistencePort;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.junit.jupiter.api.BeforeEach;

public class FakeFollowLogRepository implements FollowLogPersistencePort {
private Long followLogAutoGeneratedId = 0L;
private Map<Long, FollowLog> followLogDatabase = new HashMap<>();
private Map<Long, Friend> friendDatabase = new HashMap<>();

@BeforeEach
void setUp() {
Member member1 = Member.builder().id(100L).build();
Member member2 = Member.builder().id(101L).build();
Friend friend =
Friend.builder()
.id(1L)
.member(member1)
.following(member2)
.createdAt(LocalDateTime.now())
.build();

friendDatabase.put(friend.getId(), friend);
}

@Override
public FollowLog save(FollowLog followLog) {
if (followLog.getId() == null) {
FollowLog newFollowLog =
FollowLog.builder()
.id(++followLogAutoGeneratedId)
.follower(followLog.getFollower())
.receiver(followLog.getReceiver())
.type(followLog.getType())
.hasRead(followLog.isHasRead())
.createdAt(LocalDateTime.now())
.build();
followLogDatabase.put(followLogAutoGeneratedId, newFollowLog);
return newFollowLog;
}
followLogDatabase.replace(followLog.getId(), followLog);
return followLog;
}

@Override
public List<FollowLog> findByMemberIdAndCursorCreatedAt(
Long memberId, LocalDateTime cursorCreatedAt) {
return followLogDatabase.values().stream()
.filter(followLog -> followLog.getReceiver().getId().equals(memberId))
.filter(
followLog ->
cursorCreatedAt == null
|| followLog.getCreatedAt().isAfter(cursorCreatedAt))
.collect(Collectors.toList());
}

@Override
public void updateAllAsRead(Long memberId) {
followLogDatabase.values().stream()
.filter(followLog -> followLog.getReceiver().getId().equals(memberId))
.forEach(FollowLog::read);
}

@Override
public Long countUnread(Long memberId) {
return followLogDatabase.values().stream()
.filter(followLog -> followLog.getReceiver().getId().equals(memberId))
.filter(followLog -> !followLog.isHasRead())
.count();
}

@Override
public void deleteAllByMemberId(Long memberId) {
followLogDatabase
.values()
.removeIf(followLog -> followLog.getReceiver().getId().equals(memberId));
}

@Override
public boolean existsByReceiverIdAndFollowerId(Long receiverId, Long followerId) {
return followLogDatabase.values().stream()
.anyMatch(
followLog ->
followLog.getReceiver().getId().equals(receiverId)
&& followLog.getFollower().getId().equals(followerId));
}

@Override
public List<Long> getFriendList(Long memberId, List<Long> followerIds) {
return friendDatabase.values().stream()
.filter(friend -> friend.getMember().getId().equals(memberId))
.filter(friend -> followerIds.contains(friend.getFollowing().getId()))
.map(friend -> friend.getFollowing().getId())
.toList();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.depromeet.mock.notification;

import com.depromeet.notification.domain.ReactionLog;
import com.depromeet.notification.port.out.ReactionLogPersistencePort;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class FakeReactionLogRepository implements ReactionLogPersistencePort {
private Long reactionLogAutoGeneratedId = 0L;
private Map<Long, ReactionLog> reactionLogDatabase = new HashMap<>();

@Override
public ReactionLog save(ReactionLog reactionLog) {
if (reactionLog.getId() == null) {
ReactionLog newReactionLog =
ReactionLog.builder()
.id(++reactionLogAutoGeneratedId)
.receiver(reactionLog.getReceiver())
.reaction(reactionLog.getReaction())
.hasRead(reactionLog.isHasRead())
.createdAt(LocalDateTime.now())
.build();
reactionLogDatabase.put(reactionLogAutoGeneratedId, newReactionLog);
return newReactionLog;
}
reactionLogDatabase.replace(reactionLog.getId(), reactionLog);
return reactionLog;
}

@Override
public List<ReactionLog> findByMemberIdAndCursorCreatedAt(
Long memberId, LocalDateTime cursorCreatedAt) {
return reactionLogDatabase.values().stream()
.filter(reactionLog -> reactionLog.getReceiver().getId().equals(memberId))
.filter(
reactionLog ->
cursorCreatedAt == null
|| reactionLog.getCreatedAt().isBefore(cursorCreatedAt))
.limit(11)
.sorted((a, b) -> b.getCreatedAt().compareTo(a.getCreatedAt()))
.collect(Collectors.toList());
}

@Override
public void updateAllAsRead(Long memberId) {
reactionLogDatabase.values().stream()
.filter(reactionLog -> reactionLog.getReceiver().getId().equals(memberId))
.forEach(ReactionLog::read);
}

@Override
public Long countUnread(Long memberId) {
return reactionLogDatabase.values().stream()
.filter(reactionLog -> reactionLog.getReceiver().getId().equals(memberId))
.filter(reactionLog -> !reactionLog.isHasRead())
.count();
}

@Override
public void deleteAllByReactionId(List<Long> reactionIds) {
reactionIds.forEach(reactionLogDatabase.keySet()::remove);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package com.depromeet.service.notification;

import static org.assertj.core.api.Assertions.*;

import com.depromeet.fixture.domain.member.MemberFixture;
import com.depromeet.friend.port.out.persistence.FriendPersistencePort;
import com.depromeet.member.domain.Member;
import com.depromeet.mock.friend.FakeFriendRepository;
import com.depromeet.mock.notification.FakeFollowLogRepository;
import com.depromeet.notification.domain.FollowLog;
import com.depromeet.notification.domain.FollowType;
import com.depromeet.notification.event.FollowLogEvent;
import com.depromeet.notification.port.out.FollowLogPersistencePort;
import com.depromeet.notification.service.FollowLogService;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class FollowLogServiceTest {
private FollowLogPersistencePort followLogPersistencePort;
private FriendPersistencePort friendPersistencePort;
private FollowLogService followLogService;
private Member member1;
private Member member2;

@BeforeEach
void init() {
followLogPersistencePort = new FakeFollowLogRepository();
friendPersistencePort = new FakeFriendRepository();

followLogService = new FollowLogService(followLogPersistencePort, friendPersistencePort);

member1 = MemberFixture.make(1L, "USER");
member2 = MemberFixture.make(2L, "USER");

FollowLogEvent event = FollowLogEvent.of(member1, member2);
followLogService.save(event);
}

@Test
void ํŒ”๋กœ์šฐ_๋กœ๊ทธ๋ฅผ_์ €์žฅํ•ฉ๋‹ˆ๋‹ค() throws Exception {
// given
Member member3 = MemberFixture.make(3L, "USER");
FollowLogEvent event = FollowLogEvent.of(member1, member3);

// when
followLogService.save(event);

// then
List<FollowLog> followLogs = followLogService.getFollowLogs(member1.getId(), null);
assertThat(followLogs.size()).isEqualTo(2);
assertThat(followLogs.getLast().getReceiver().getId()).isEqualTo(1L);
assertThat(followLogs.getLast().getFollower().getId()).isEqualTo(3L);
}

@Test
public void ํŒ”๋กœ์šฐ_๋กœ๊ทธ๋ฅผ_์กฐํšŒํ•ฉ๋‹ˆ๋‹ค() throws Exception {
// when
List<FollowLog> followLogs = followLogService.getFollowLogs(member1.getId(), null);

// then
assertThat(followLogs.size()).isEqualTo(1);
assertThat(followLogs.getFirst().getReceiver().getId()).isEqualTo(1L);
assertThat(followLogs.getFirst().getFollower().getId()).isEqualTo(2L);
assertThat(followLogs.getFirst().getType()).isEqualTo(FollowType.FOLLOW);
}

@Test
public void ๋ฏธํ™•์ธ_ํŒ”๋กœ์šฐ_๋กœ๊ทธ์นด์šดํŠธ๋ฅผ_์กฐํšŒํ•ฉ๋‹ˆ๋‹ค() throws Exception {
// when
Long unreadFollowLogCount = followLogService.getUnreadFollowLogCount(1L);

// then
assertThat(unreadFollowLogCount).isEqualTo(1L);
}

@Test
public void ํŒ”๋กœ์šฐ_ํ™•์ธ_๋ฐ_๋กœ๊ทธ์นด์šดํŠธ_๋ณ€๊ฒฝ์„_ํ™•์ธํ•ฉ๋‹ˆ๋‹ค() throws Exception {
// given
followLogService.markAsReadFollowLogs(1L);

// when
Long unreadFollowLogCount = followLogService.getUnreadFollowLogCount(1L);

// then
assertThat(unreadFollowLogCount).isEqualTo(0L);
}

@Test
public void ํŒ”๋กœ์šฐ_๋กœ๊ทธ_์‚ญ์ œ๋ฅผ_์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค() throws Exception {
// given
followLogService.deleteAllByMemberId(1L);

// when
List<FollowLog> followLogs = followLogService.getFollowLogs(1L, null);

// then
assertThat(followLogs.size()).isEqualTo(0);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package com.depromeet.service.notification;

import static org.assertj.core.api.Assertions.*;

import com.depromeet.fixture.domain.member.MemberFixture;
import com.depromeet.fixture.domain.memory.MemoryFixture;
import com.depromeet.fixture.domain.reaction.ReactionFixture;
import com.depromeet.member.domain.Member;
import com.depromeet.memory.domain.Memory;
import com.depromeet.mock.notification.FakeReactionLogRepository;
import com.depromeet.notification.domain.ReactionLog;
import com.depromeet.notification.event.ReactionLogEvent;
import com.depromeet.notification.port.out.ReactionLogPersistencePort;
import com.depromeet.notification.service.ReactionLogService;
import com.depromeet.reaction.domain.Reaction;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class ReactionLogServiceTest {
private ReactionLogPersistencePort reactionLogPersistencePort;
private ReactionLogService reactionLogService;
private Reaction reaction;
private Member member1;
private Member member2;

@BeforeEach
void init() {
reactionLogPersistencePort = new FakeReactionLogRepository();
reactionLogService = new ReactionLogService(reactionLogPersistencePort);
member1 = MemberFixture.make(1L, "USER");
member2 = MemberFixture.make(2L, "USER");
Memory memory = MemoryFixture.make(member2, null, null, null);
reaction = ReactionFixture.make(member1, memory);

var event = ReactionLogEvent.of(member2, reaction);
reactionLogService.save(event);
}

@Test
public void ์‘์›_๋กœ๊ทธ๋ฅผ_์ €์žฅํ•ฉ๋‹ˆ๋‹ค() throws Exception {
// given
var reactionLogEvent = ReactionLogEvent.of(member2, reaction);

// when
reactionLogService.save(reactionLogEvent);

// then
List<ReactionLog> reactionLogs = reactionLogService.getReactionsLogs(member2.getId(), null);
assertThat(reactionLogs.size()).isEqualTo(2);
assertThat(reactionLogs.getFirst().getReceiver().getId()).isEqualTo(2L);
assertThat(reactionLogs.getFirst().getReaction().getMember().getId()).isEqualTo(1L);
}

@Test
public void ์‘์›_๋กœ๊ทธ๋ฅผ_์กฐํšŒํ•ฉ๋‹ˆ๋‹ค() throws Exception {
// when
List<ReactionLog> reactionLogs = reactionLogService.getReactionsLogs(member2.getId(), null);

// then
assertThat(reactionLogs.size()).isEqualTo(1);
assertThat(reactionLogs.getFirst().getReceiver().getId()).isEqualTo(2L);
assertThat(reactionLogs.getFirst().getReaction().getMember().getId()).isEqualTo(1L);
}

@Test
public void ๋ฏธํ™•์ธ_๋กœ๊ทธ_์ˆ˜๋ฅผ_ํ™•์ธํ•ฉ๋‹ˆ๋‹ค() throws Exception {
// when
Long unreadReactionLogCount = reactionLogService.getUnreadReactionLogCount(2L);

// then
assertThat(unreadReactionLogCount).isEqualTo(1L);
}

@Test
public void ๋กœ๊ทธ_์กฐํšŒ_๋ฐ_๋ฏธํ™•์ธ_๋กœ๊ทธ์นด์šดํŠธ_๋ณ€๊ฒฝ์„_ํ™•์ธํ•ฉ๋‹ˆ๋‹ค() throws Exception {
// given
reactionLogService.markAsReadReactionLogs(2L);

// when
Long unreadReactionLogCount = reactionLogService.getUnreadReactionLogCount(2L);

// then
assertThat(unreadReactionLogCount).isEqualTo(0L);
}

@Test
public void ์‘์›_๋กœ๊ทธ_์‚ญ์ œ๋ฅผ_์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค() throws Exception {
// given
reactionLogService.deleteAllByReactionId(List.of(1L));

// when
List<ReactionLog> reactionLogs = reactionLogService.getReactionsLogs(member2.getId(), null);

// then
assertThat(reactionLogs.size()).isEqualTo(0);
}
}
Loading

0 comments on commit 5e12a75

Please sign in to comment.