Skip to content

Commit

Permalink
test : 차단된 유저와 매칭되지 않는 기능 테스트
Browse files Browse the repository at this point in the history
  • Loading branch information
suhyun0918 committed Feb 15, 2024
1 parent 3e118ec commit 903aa98
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package com.aliens.backend.matching.unit.business;

import com.aliens.backend.auth.domain.Member;
import com.aliens.backend.auth.domain.repository.MemberRepository;
import com.aliens.backend.block.domain.Block;
import com.aliens.backend.block.domain.repository.BlockRepository;
import com.aliens.backend.global.BaseServiceTest;
import com.aliens.backend.global.DummyGenerator;
import com.aliens.backend.global.response.error.MatchingError;
import com.aliens.backend.global.exception.RestApiException;
import com.aliens.backend.global.property.MatchingTimeProperties;
import com.aliens.backend.global.response.error.MemberError;
import com.aliens.backend.matching.util.time.MockClock;
import com.aliens.backend.matching.util.time.MockTime;
import com.aliens.backend.mathcing.business.MatchingBusiness;
import com.aliens.backend.mathcing.business.model.Partner;
import com.aliens.backend.mathcing.controller.dto.request.MatchingOperateRequest;
import com.aliens.backend.mathcing.domain.MatchingApplication;
import com.aliens.backend.mathcing.domain.MatchingResult;
Expand All @@ -18,7 +22,6 @@
import com.aliens.backend.mathcing.domain.repository.MatchingRoundRepository;
import com.aliens.backend.mathcing.service.MatchingProcessService;
import com.aliens.backend.mathcing.business.model.Participant;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
Expand All @@ -29,7 +32,6 @@
import java.util.List;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.junit.jupiter.api.Assertions.*;

@SpringBootTest
class MatchingBusinessTest extends BaseServiceTest {
Expand All @@ -40,6 +42,8 @@ class MatchingBusinessTest extends BaseServiceTest {
@Autowired MatchingBusiness matchingBusiness;
@Autowired DummyGenerator dummyGenerator;
@Autowired MatchingTimeProperties matchingTimeProperties;
@Autowired BlockRepository blockRepository;
@Autowired MemberRepository memberRepository;
@Autowired MockClock mockClock;

MatchingOperateRequest matchingOperateRequest;
Expand Down Expand Up @@ -81,10 +85,30 @@ private List<MatchingResult> getPreviousMatchingResult(MatchingRound matchingRou
return matchingResultRepository.findAllByMatchingRound(previousMatchingRound);
}

private List<Block> getBlockListByMatchingApplications(MatchingRound matchingRound) {
List<MatchingApplication> matchingApplications = getMatchingApplications(matchingRound);
List<Block> blockHistory = matchingApplications.stream()
.map(MatchingApplication::getMemberId)
.map(this::getMemberById)
.flatMap(member -> getBlockListByBlockingMember(member).stream())
.toList();
return blockHistory;
}

private List<Block> getBlockListByBlockingMember(Member blockingMember) {
return blockRepository.findAllByBlockingMember(blockingMember);
}

private Member getMemberById(Long memberId) {
return memberRepository.findById(memberId)
.orElseThrow(() -> new RestApiException(MemberError.NULL_MEMBER));
}

private MatchingOperateRequest createOperateRequest(MatchingRound matchingRound) {
List<MatchingApplication> matchingApplications = getMatchingApplications(matchingRound);
List<MatchingResult> previousMatchingResult = getPreviousMatchingResult(matchingRound);
return MatchingOperateRequest.of(matchingApplications, previousMatchingResult);
List<Block> participantBlockHistory = getBlockListByMatchingApplications(matchingRound);
return MatchingOperateRequest.of(matchingApplications, previousMatchingResult, participantBlockHistory);
}

private void saveMatchRound(MockTime mockTime) {
Expand All @@ -95,6 +119,7 @@ private void saveMatchRound(MockTime mockTime) {

private void operateMatching(MockTime mockTime) {
mockClock.mockTime(mockTime);
dummyGenerator.generateMultiMember(21);
dummyGenerator.generateAppliersToMatch(20L);
matchingOperateRequest = createOperateRequest(getCurrentRound());
matchingBusiness.operateMatching(matchingOperateRequest);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.aliens.backend.matching.unit.service;

import com.aliens.backend.auth.controller.dto.LoginMember;
import com.aliens.backend.auth.domain.MemberRole;
import com.aliens.backend.auth.domain.Member;
import com.aliens.backend.block.controller.dto.BlockRequest;
import com.aliens.backend.block.domain.Block;
import com.aliens.backend.block.domain.repository.BlockRepository;
import com.aliens.backend.global.BaseServiceTest;
import com.aliens.backend.global.DummyGenerator;
import com.aliens.backend.global.response.error.MatchingError;
Expand All @@ -25,35 +28,30 @@
import java.util.stream.Collectors;

import static org.assertj.core.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

@SpringBootTest
class MatchingProcessServiceTest extends BaseServiceTest {
@Autowired MatchingProcessService matchingProcessService;
@Autowired MatchingRoundRepository matchingRoundRepository;
@Autowired MatchingTimeProperties matchingTimeProperties;
@Autowired MatchingResultRepository matchingResultRepository;
@Autowired BlockRepository blockRepository;
@Autowired MockClock mockClock;
@Autowired DummyGenerator dummyGenerator;

LoginMember loginMember;
List<Member> members;

@BeforeEach
void setUp() {
loginMember = new LoginMember(1L, MemberRole.MEMBER);
members = dummyGenerator.generateMultiMember(20);
saveMatchRound(MockTime.MONDAY);
}

@Test
@DisplayName("매칭 결과 조회")
void operateMatchingTest() {
// given
mockClock.mockTime(MockTime.VALID_RECEPTION_TIME_ON_MONDAY);
dummyGenerator.generateAppliersToMatch(20L);

// when
matchingProcessService.operateMatching();
// given & when
operateMatching(MockTime.VALID_RECEPTION_TIME_ON_MONDAY);

// then
List<MatchingResult> result = matchingResultRepository.findAllByMatchingRound(getCurrentRound());
Expand Down Expand Up @@ -94,12 +92,26 @@ void isDuplicateMatchingTest() {
@Test
@DisplayName("차단된 유저와 매칭이 되지 않는지 테스트")
void isBlockedMemberTest() {
// given
Member blockingMember = members.get(0);
makeThisMemberBlockAllPartner(blockingMember);

// when
operateMatching(MockTime.VALID_RECEPTION_TIME_ON_MONDAY);

// then
List<MatchingResult> matchingResults = getMatchingResultByMatchingRound(getCurrentRound());
List<MatchingResult> matchingResultsOfBlockingMember = matchingResults.stream()
.filter(matchingResult -> matchingResult.getMatchingMemberId().equals(blockingMember.getId())).toList();
assertThat(matchingResultsOfBlockingMember).isEmpty();
}

@Test
@DisplayName("매칭을 신청한 적이 없는 회원이 매칭 조회")
void getMatchingResultTest() {
Member member = members.get(0);
LoginMember loginMember = member.getLoginMember();

assertThatThrownBy(() -> matchingProcessService.findMatchingResult(loginMember))
.hasMessage(MatchingError.NOT_FOUND_MATCHING_APPLICATION_INFO.getDevelopCode());
}
Expand All @@ -124,4 +136,12 @@ private void operateMatching(MockTime mockTime) {
private List<MatchingResult> getMatchingResultByMatchingRound(MatchingRound matchingRound) {
return matchingResultRepository.findAllByMatchingRound(matchingRound);
}

private void makeThisMemberBlockAllPartner(Member blockingMember) {
for (int i = 1; i < members.size(); i++) {
Member blockedMember = members.get(i);
Block blockRequest = Block.of(blockingMember, blockedMember);
blockRepository.save(blockRequest);
}
}
}

0 comments on commit 903aa98

Please sign in to comment.