Skip to content

Commit

Permalink
test : 테스트 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
seokho-1116 committed Jun 2, 2024
1 parent ee49032 commit 0207f90
Show file tree
Hide file tree
Showing 6 changed files with 274 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package site.timecapsulearchive.core.common.fixture.dto;

import java.util.List;
import java.util.Optional;
import site.timecapsulearchive.core.domain.friend.data.dto.FriendInviteMemberIdsDto;

public class FriendInviteMemberIdsDtoFixture {

public static List<FriendInviteMemberIdsDto> duplicates(
final Long memberId,
final List<Long> friendIds
) {
return friendIds.stream()
.map(friendId -> new FriendInviteMemberIdsDto(memberId, friendId))
.toList();
}

public static List<FriendInviteMemberIdsDto> twoWays(
final Long memberId,
final List<Long> friendIds
) {
return friendIds.stream()
.map(friendId -> new FriendInviteMemberIdsDto(friendId, memberId))
.toList();
}

public static Optional<FriendInviteMemberIdsDto> duplicate(
final Long memberId,
final Long friendId
) {
return Optional.of(
new FriendInviteMemberIdsDto(memberId, friendId)
);
}

public static Optional<FriendInviteMemberIdsDto> twoWay(Long memberId, Long friendId) {
return Optional.of(
new FriendInviteMemberIdsDto(friendId, memberId)
);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package site.timecapsulearchive.core.domain.friend.repository;
package site.timecapsulearchive.core.domain.friend.repository.friend_invite;

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

import com.querydsl.jpa.impl.JPAQueryFactory;
import jakarta.persistence.EntityManager;
import jakarta.persistence.Query;
import java.util.ArrayList;
Expand All @@ -12,10 +13,10 @@
import org.springframework.test.context.TestConstructor;
import org.springframework.test.context.TestConstructor.AutowireMode;
import site.timecapsulearchive.core.common.RepositoryTest;
import site.timecapsulearchive.core.common.fixture.domain.FriendInviteFixture;
import site.timecapsulearchive.core.common.fixture.domain.MemberFixture;
import site.timecapsulearchive.core.domain.friend.data.dto.FriendInviteMemberIdsDto;
import site.timecapsulearchive.core.domain.friend.entity.FriendInvite;
import site.timecapsulearchive.core.domain.friend.repository.friend_invite.FriendInviteQueryRepository;
import site.timecapsulearchive.core.domain.friend.repository.friend_invite.FriendInviteQueryRepositoryImpl;
import site.timecapsulearchive.core.domain.member.entity.Member;

@TestConstructor(autowireMode = AutowireMode.ALL)
Expand All @@ -27,9 +28,11 @@ class FriendInviteQueryRepositoryTest extends RepositoryTest {

private Member owner;

FriendInviteQueryRepositoryTest(EntityManager entityManager, JdbcTemplate jdbcTemplate) {
FriendInviteQueryRepositoryTest(EntityManager entityManager, JdbcTemplate jdbcTemplate,
JPAQueryFactory jpaQueryFactory) {
this.entityManager = entityManager;
this.friendInviteQueryRepository = new FriendInviteQueryRepositoryImpl(jdbcTemplate);
this.friendInviteQueryRepository = new FriendInviteQueryRepositoryImpl(jdbcTemplate,
jpaQueryFactory);
}

@BeforeEach
Expand All @@ -39,6 +42,11 @@ void setup() {

friends.addAll(MemberFixture.members(1, 11));
friends.forEach(entityManager::persist);

for (Member friend : friends) {
FriendInvite friendInvite = FriendInviteFixture.friendInvite(friend, owner);
entityManager.persist(friendInvite);
}
}

@Test
Expand All @@ -63,4 +71,21 @@ private List<FriendInvite> getFriendInvites(EntityManager entityManager, Long ow
query.setParameter("ownerId", ownerId);
return query.getResultList();
}

//Friend -> Owner
@Test
void 친구가_사용자에게_요청을_보낸_경우_사용자_아이디와_친구_아이디_목록으로_모든_요청_방향의_친구_초대를_조회하면_존재하는_단방향_친구_초대가_나온다() {
//given
List<Long> friendIds = friends.stream()
.map(Member::getId)
.toList();

//when
List<FriendInviteMemberIdsDto> friendInviteMemberIdsDtos = friendInviteQueryRepository.findFriendInviteMemberIdsDtoByMemberIdsAndFriendId(
friendIds,
owner.getId()
);

assertThat(friendInviteMemberIdsDtos).hasSize(friendIds.size());
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package site.timecapsulearchive.core.domain.friend.repository;
package site.timecapsulearchive.core.domain.friend.repository.member_friend;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
Expand Down Expand Up @@ -33,8 +33,6 @@
import site.timecapsulearchive.core.domain.friend.data.dto.SearchFriendSummaryDtoByTag;
import site.timecapsulearchive.core.domain.friend.entity.FriendInvite;
import site.timecapsulearchive.core.domain.friend.entity.MemberFriend;
import site.timecapsulearchive.core.domain.friend.repository.member_friend.MemberFriendQueryRepository;
import site.timecapsulearchive.core.domain.friend.repository.member_friend.MemberFriendQueryRepositoryImpl;
import site.timecapsulearchive.core.domain.member.entity.Member;

@TestConstructor(autowireMode = AutowireMode.ALL)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package site.timecapsulearchive.core.domain.friend.repository;
package site.timecapsulearchive.core.domain.friend.repository.member_friend;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.SoftAssertions.assertSoftly;
Expand All @@ -15,7 +15,6 @@
import site.timecapsulearchive.core.common.fixture.domain.MemberFixture;
import site.timecapsulearchive.core.common.fixture.domain.MemberFriendFixture;
import site.timecapsulearchive.core.domain.friend.entity.MemberFriend;
import site.timecapsulearchive.core.domain.friend.repository.member_friend.MemberFriendRepository;
import site.timecapsulearchive.core.domain.member.entity.Member;

@TestConstructor(autowireMode = AutowireMode.ALL)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
package site.timecapsulearchive.core.domain.friend.service.command;

import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;

import java.util.Collections;
import java.util.List;
import java.util.Optional;
import org.junit.jupiter.api.Test;
import org.springframework.transaction.support.TransactionTemplate;
import site.timecapsulearchive.core.common.dependency.TestTransactionTemplate;
import site.timecapsulearchive.core.common.fixture.dto.FriendInviteMemberIdsDtoFixture;
import site.timecapsulearchive.core.domain.friend.exception.FriendInviteDuplicateException;
import site.timecapsulearchive.core.domain.friend.exception.FriendInviteNotFoundException;
import site.timecapsulearchive.core.domain.friend.exception.FriendTwoWayInviteException;
import site.timecapsulearchive.core.domain.friend.exception.SelfFriendOperationException;
import site.timecapsulearchive.core.domain.friend.repository.friend_invite.FriendInviteRepository;
import site.timecapsulearchive.core.domain.friend.repository.member_friend.MemberFriendRepository;
import site.timecapsulearchive.core.domain.member.repository.MemberRepository;
import site.timecapsulearchive.core.global.error.ErrorCode;
import site.timecapsulearchive.core.infra.queue.manager.SocialNotificationManager;

class FriendCommandServiceTest {

private final MemberFriendRepository memberFriendRepository = mock(
MemberFriendRepository.class);
private final MemberRepository memberRepository = mock(MemberRepository.class);
private final FriendInviteRepository friendInviteRepository = mock(
FriendInviteRepository.class);
private final SocialNotificationManager socialNotificationManager = mock(
SocialNotificationManager.class);
private final TransactionTemplate transactionTemplate = TestTransactionTemplate.spied();

private final FriendCommandService friendCommandService = new FriendCommandService(
memberFriendRepository,
memberRepository,
friendInviteRepository,
socialNotificationManager,
transactionTemplate
);

@Test
void 사용자_본인한테_다건_친구_요청을_보낸_경우_어떠한_동작도_수행하지_않는다() {
//given
Long memberId = 1L;
List<Long> friendIds = List.of(1L, 1L, 1L, 1L);
given(friendInviteRepository.findFriendInviteMemberIdsDtoByMemberIdsAndFriendId(anyList(),
any()))
.willReturn(Collections.emptyList());

//when
friendCommandService.requestFriends(memberId, friendIds);

//then
verify(transactionTemplate, never()).execute(any());
}

@Test
void 이미_친구요청된_친구에게_다건_친구_요청을_보낸_경우_어떠한_동작도_수행하지_않는다() {
//given
Long memberId = 1L;
List<Long> friendIds = List.of(2L, 3L, 4L, 5L, 6L);
given(friendInviteRepository.findFriendInviteMemberIdsDtoByMemberIdsAndFriendId(anyList(),
any()))
.willReturn(FriendInviteMemberIdsDtoFixture.duplicates(memberId, friendIds));

//when
friendCommandService.requestFriends(memberId, friendIds);

//then
verify(transactionTemplate, never()).execute(any());
}

@Test
void 양방향_다건_친구_요청을_보낸_경우_어떠한_동작도_수행하지_않는다() {
//given
Long memberId = 1L;
List<Long> friendIds = List.of(2L, 3L, 4L, 5L, 6L);
given(friendInviteRepository.findFriendInviteMemberIdsDtoByMemberIdsAndFriendId(anyList(),
any()))
.willReturn(FriendInviteMemberIdsDtoFixture.twoWays(memberId, friendIds));

//when
friendCommandService.requestFriends(memberId, friendIds);

//then
verify(transactionTemplate, never()).execute(any());
}

@Test
void 사용자_본인한테_단건_친구_요청을_보낸_경우_예외가_발생한다() {
//given
Long memberId = 1L;

//when
//then
assertThatThrownBy(() -> friendCommandService.requestFriend(memberId, memberId))
.isInstanceOf(SelfFriendOperationException.class)
.hasMessageContaining(ErrorCode.SELF_FRIEND_OPERATION_ERROR.getMessage());
}

@Test
void 이미_친구요청된_친구에게_단건_친구_요청을_보낸_경우_예외가_발생한다() {
//given
Long memberId = 1L;
Long friendId = 2L;
given(friendInviteRepository.findFriendInviteMemberIdsDtoByMemberIdAndFriendId(anyLong(), anyLong()))
.willReturn(FriendInviteMemberIdsDtoFixture.duplicate(memberId, friendId));

//when
//then
assertThatThrownBy(() -> friendCommandService.requestFriend(memberId, friendId))
.isInstanceOf(FriendInviteDuplicateException.class)
.hasMessageContaining(ErrorCode.FRIEND_INVITE_DUPLICATE_ERROR.getMessage());
}

@Test
void 양방향_단건_친구_요청을_보낸_경우_예외가_발생한다() {
//given
Long memberId = 1L;
Long friendId = 2L;
given(friendInviteRepository.findFriendInviteMemberIdsDtoByMemberIdAndFriendId(anyLong(), anyLong()))
.willReturn(FriendInviteMemberIdsDtoFixture.twoWay(memberId, friendId));

//when
//then
assertThatThrownBy(() -> friendCommandService.requestFriend(memberId, friendId))
.isInstanceOf(FriendTwoWayInviteException.class)
.hasMessageContaining(ErrorCode.FRIEND_TWO_WAY_INVITE_ERROR.getMessage());
}

@Test
void 사용자_본인한테_친구_요청을_수락한_경우_예외가_발생한다() {
//given
Long memberId = 1L;

//when
//then
assertThatThrownBy(() -> friendCommandService.acceptFriend(memberId, memberId))
.isInstanceOf(SelfFriendOperationException.class)
.hasMessageContaining(ErrorCode.SELF_FRIEND_OPERATION_ERROR.getMessage());
}

@Test
void 사용자_본인한테_친구_요청을_거부한_경우_예외가_발생한다() {
//given
Long memberId = 1L;

//when
//then
assertThatThrownBy(() -> friendCommandService.denyRequestFriend(memberId, memberId))
.isInstanceOf(SelfFriendOperationException.class)
.hasMessageContaining(ErrorCode.SELF_FRIEND_OPERATION_ERROR.getMessage());
}

@Test
void 사용자_본인한테_친구_삭제를_요청한_경우_예외가_발생한다() {
//given
Long memberId = 1L;

//when
//then
assertThatThrownBy(() -> friendCommandService.deleteFriend(memberId, memberId))
.isInstanceOf(SelfFriendOperationException.class)
.hasMessageContaining(ErrorCode.SELF_FRIEND_OPERATION_ERROR.getMessage());
}

@Test
void 친구_요청을_보낸_사용자가_본인의_아이디로_친구_요청을_삭제하면_예외가_발생한다() {
//given
Long memberId = 1L;

//when
//then
assertThatThrownBy(() -> friendCommandService.deleteSendingFriendInvite(memberId, memberId))
.isInstanceOf(SelfFriendOperationException.class)
.hasMessageContaining(ErrorCode.SELF_FRIEND_OPERATION_ERROR.getMessage());
}

@Test
void 친구_요청을_보내지_않은_사용자가_친구_요청을_삭제하면_예외가_발생한다() {
//given
Long memberId = 1L;
Long friendId = 2L;
given(friendInviteRepository.findFriendSendingInviteForUpdateByOwnerIdAndFriendId(anyLong(),
anyLong())).willReturn(Optional.empty());

//when
//then
assertThatThrownBy(() -> friendCommandService.deleteSendingFriendInvite(memberId, friendId))
.isInstanceOf(FriendInviteNotFoundException.class)
.hasMessageContaining(ErrorCode.FRIEND_INVITE_NOT_FOUND_ERROR.getMessage());
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package site.timecapsulearchive.core.domain.friend.service;
package site.timecapsulearchive.core.domain.friend.service.query;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
Expand All @@ -18,10 +18,8 @@
import site.timecapsulearchive.core.common.fixture.dto.FriendDtoFixture;
import site.timecapsulearchive.core.domain.friend.data.dto.SearchFriendSummaryDto;
import site.timecapsulearchive.core.domain.friend.data.dto.SearchFriendSummaryDtoByTag;
import site.timecapsulearchive.core.domain.friend.data.response.SearchTagFriendSummaryResponse;
import site.timecapsulearchive.core.domain.friend.exception.FriendNotFoundException;
import site.timecapsulearchive.core.domain.friend.repository.member_friend.MemberFriendRepository;
import site.timecapsulearchive.core.domain.friend.service.query.FriendQueryService;
import site.timecapsulearchive.core.global.common.wrapper.ByteArrayWrapper;

class FriendQueryServiceTest {
Expand Down

0 comments on commit 0207f90

Please sign in to comment.