-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #454 from depromeet/test/453-notification
test: ์๋ฆผ ๋๋ฉ์ธ ์ ์ฒด ํ ์คํธ์ฝ๋ ์์ฑ
- Loading branch information
Showing
8 changed files
with
441 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
module-domain/src/test/java/com/depromeet/mock/notification/FakeFollowLogRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
module-domain/src/test/java/com/depromeet/mock/notification/FakeReactionLogRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
100 changes: 100 additions & 0 deletions
100
module-domain/src/test/java/com/depromeet/service/notification/FollowLogServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
98 changes: 98 additions & 0 deletions
98
module-domain/src/test/java/com/depromeet/service/notification/ReactionLogServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.