Skip to content

Commit

Permalink
test: 테스트 작성
Browse files Browse the repository at this point in the history
  • Loading branch information
ashsty committed Nov 15, 2024
1 parent 99bd529 commit fb014df
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ public void match(long roomId) {
});
} catch (CoreaException e) {
recordMatchingFailure(roomId, e);
createMatchingFailedAlarm(roomId);
}
}

Expand Down Expand Up @@ -77,6 +76,7 @@ private void recordMatchingFailure(long roomId, CoreaException e) {
template.execute(status -> {
updateRoomStatusToFail(roomId);
saveFailedMatching(roomId, e);
createMatchingFailedAlarm(roomId);
return null;
});
}
Expand Down
83 changes: 77 additions & 6 deletions backend/src/test/java/corea/alarm/service/AlarmServiceTest.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
package corea.alarm.service;

import config.ServiceTest;
import corea.alarm.domain.AlarmActionType;
import corea.alarm.domain.ServerToUserAlarm;
import corea.alarm.domain.UserToUserAlarm;
import corea.alarm.dto.AlarmCheckRequest;
import corea.alarm.dto.AlarmCountResponse;
import corea.alarm.dto.AlarmResponse;
import corea.alarm.dto.AlarmResponses;
import corea.alarm.repository.ServerToUserAlarmRepository;
import corea.alarm.repository.UserToUserAlarmRepository;
import corea.exception.CoreaException;
import corea.fixture.AlarmFixture;
import corea.fixture.MemberFixture;
import corea.fixture.RoomFixture;
import corea.member.domain.Member;
import corea.member.domain.MemberRole;
import corea.member.repository.MemberRepository;
import corea.participation.domain.Participation;
import corea.participation.domain.ParticipationStatus;
import corea.participation.repository.ParticipationRepository;
import corea.room.domain.Room;
import corea.room.repository.RoomRepository;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
Expand All @@ -25,19 +33,27 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;

@ServiceTest
class AlarmServiceTest {

@Autowired
AlarmService alarmService;

@Autowired
ServerToUserAlarmRepository serverToUserAlarmRepository;

@Autowired
UserToUserAlarmRepository userToUserAlarmRepository;

@Autowired
MemberRepository memberRepository;

@Autowired
ParticipationRepository participationRepository;

private Member actor;
private Member receiver;
private Member notReceiver;
Expand All @@ -56,35 +72,54 @@ void setUp() {
interactionId = interaction.getId();
}

@AfterEach
void tearDown() {
serverToUserAlarmRepository.deleteAll();
}

@Test
@DisplayName("자신에게 작성된 알람들을 가져온다.")
void get_alarm_count() {
userToUserAlarmRepository.save(AlarmFixture.REVIEW_COMPLETE(actor.getId(), receiver.getId(), interactionId));
userToUserAlarmRepository.save(AlarmFixture.REVIEW_COMPLETE(actor.getId(), notReceiver.getId(), interactionId));

serverToUserAlarmRepository.save(AlarmFixture.MATCH_COMPLETE(receiver.getId(), interactionId));
serverToUserAlarmRepository.save(AlarmFixture.MATCH_FAIL(notReceiver.getId(), interactionId));

AlarmCountResponse response = alarmService.getUnReadAlarmCount(receiver.getId());
assertThat(response.count()).isEqualTo(1);
assertThat(response.count()).isEqualTo(2);
}

@Test
@DisplayName("읽지 않은 알람만 가져온다.")
void get_only_not_read_alarm_count() {
userToUserAlarmRepository.save(AlarmFixture.REVIEW_COMPLETE(actor.getId(), receiver.getId(), interactionId));
userToUserAlarmRepository.save(AlarmFixture.READ_REVIEW_COMPLETE(actor.getId(), receiver.getId(), interactionId));

serverToUserAlarmRepository.save(AlarmFixture.MATCH_COMPLETE(receiver.getId(), interactionId));
serverToUserAlarmRepository.save(AlarmFixture.READ_MATCH_COMPLETE(receiver.getId(), interactionId));

AlarmCountResponse response = alarmService.getUnReadAlarmCount(receiver.getId());
assertThat(response.count()).isEqualTo(1);
assertThat(response.count()).isEqualTo(2);
}

@Test
@DisplayName("사용자의 모든 알람을 최신순으로 가져온다.")
void get_alarm() {
UserToUserAlarm alarm1 = userToUserAlarmRepository.save(AlarmFixture.REVIEW_COMPLETE(actor.getId(), receiver.getId(), interactionId));
UserToUserAlarm alarm2 = userToUserAlarmRepository.save(AlarmFixture.READ_REVIEW_COMPLETE(actor.getId(), receiver.getId(), interactionId));

ServerToUserAlarm alarm3 = serverToUserAlarmRepository.save(AlarmFixture.MATCH_COMPLETE(receiver.getId(), interactionId));
ServerToUserAlarm alarm4 = serverToUserAlarmRepository.save(AlarmFixture.MATCH_FAIL(receiver.getId(), interactionId));

AlarmResponses responses = alarmService.getAlarm(receiver.getId());
assertThat(responses.data()).hasSize(2)
assertThat(responses.data()).hasSize(4)
.usingElementComparatorIgnoringFields("createAt")
.containsExactly(
AlarmResponse.from(alarm2, actor, interaction),
AlarmResponse.from(alarm1, actor, interaction)
AlarmResponse.of(alarm4, interaction),
AlarmResponse.of(alarm3, interaction),
AlarmResponse.of(alarm2, actor, interaction),
AlarmResponse.of(alarm1, actor, interaction)
);
}

Expand All @@ -98,7 +133,7 @@ void throw_exception_when_not_receive_alarm() {
}

@Test
@DisplayName("자신에게 해당된 알람이 아니면 예외를 발생한다.")
@DisplayName("존재하는 알람이 아니면 예외를 발생한다.")
void throw_exception_when_not_exist_alarm() {
userToUserAlarmRepository.save(AlarmFixture.REVIEW_COMPLETE(actor.getId(), receiver.getId(), interactionId));

Expand Down Expand Up @@ -134,4 +169,40 @@ void does_not_create_urge_alarm_when_unread_urge_alarm_exist() {
assertThatThrownBy(() -> alarmService.createUrgeAlarm(actor.getId(), receiver.getId(), interactionId))
.isInstanceOf(CoreaException.class);
}

@Test
@DisplayName("매칭 완료 알람을 생성한다.")
void matching_complete_alarm() {
participationRepository.save(new Participation(interaction, receiver, MemberRole.BOTH, ParticipationStatus.PARTICIPATED, 2));
participationRepository.save(new Participation(interaction, actor, MemberRole.BOTH, ParticipationStatus.PARTICIPATED, 2));

alarmService.createMatchingCompletedAlarm(interactionId);

List<ServerToUserAlarm> matchingAlarmsToReceiver = serverToUserAlarmRepository.findAllByReceiverId(receiver.getId());
List<ServerToUserAlarm> totalAlarms = serverToUserAlarmRepository.findAll();

assertAll(
() -> assertEquals(totalAlarms.size(), 2),
() -> assertEquals(matchingAlarmsToReceiver.size(), 1),
() -> assertEquals(matchingAlarmsToReceiver.get(0).getAlarmActionType(), AlarmActionType.MATCH_COMPLETE)
);
}

@Test
@DisplayName("매칭 실패 알람을 생성한다.")
void matching_fail_alarm() {
participationRepository.save(new Participation(interaction, receiver, MemberRole.BOTH, ParticipationStatus.PARTICIPATED, 2));
participationRepository.save(new Participation(interaction, actor, MemberRole.BOTH, ParticipationStatus.PARTICIPATED, 2));

alarmService.createMatchingFailedAlarm(interactionId);

List<ServerToUserAlarm> matchingAlarmsToReceiver = serverToUserAlarmRepository.findAllByReceiverId(receiver.getId());
List<ServerToUserAlarm> totalAlarms = serverToUserAlarmRepository.findAll();

assertAll(
() -> assertEquals(totalAlarms.size(), 2),
() -> assertEquals(matchingAlarmsToReceiver.size(), 1),
() -> assertEquals(matchingAlarmsToReceiver.get(0).getAlarmActionType(), AlarmActionType.MATCH_FAIL)
);
}
}
15 changes: 14 additions & 1 deletion backend/src/test/java/corea/fixture/AlarmFixture.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package corea.fixture;

import corea.alarm.domain.UserToUserAlarm;
import corea.alarm.domain.AlarmActionType;
import corea.alarm.domain.ServerToUserAlarm;
import corea.alarm.domain.UserToUserAlarm;

public class AlarmFixture {
public static UserToUserAlarm REVIEW_COMPLETE(long actorId, long receiverId, long interactionId) {
Expand All @@ -19,4 +20,16 @@ public static UserToUserAlarm URGE_REVIEW(long actorId, long receiverId, long in
public static UserToUserAlarm READ_URGE_REVIEW(long actorId, long receiverId, long interactionId) {
return new UserToUserAlarm(AlarmActionType.REVIEW_URGE, actorId, receiverId, interactionId, true);
}

public static ServerToUserAlarm MATCH_COMPLETE(long receiverId, long interactionId) {
return new ServerToUserAlarm(AlarmActionType.MATCH_COMPLETE, receiverId, interactionId, false);
}

public static ServerToUserAlarm READ_MATCH_COMPLETE(long receiverId, long interactionId) {
return new ServerToUserAlarm(AlarmActionType.MATCH_COMPLETE, receiverId, interactionId, true);
}

public static ServerToUserAlarm MATCH_FAIL(long receiverId, long interactionId) {
return new ServerToUserAlarm(AlarmActionType.MATCH_FAIL, receiverId, interactionId, false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import config.ServiceTest;
import config.TestAsyncConfig;
import corea.alarm.domain.AlarmActionType;
import corea.alarm.domain.ServerToUserAlarm;
import corea.alarm.repository.ServerToUserAlarmRepository;
import corea.fixture.MemberFixture;
import corea.fixture.RoomFixture;
import corea.matching.domain.PullRequestInfo;
Expand All @@ -22,9 +25,7 @@
import corea.room.repository.RoomRepository;
import corea.scheduler.domain.AutomaticMatching;
import corea.scheduler.repository.AutomaticMatchingRepository;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Import;
Expand All @@ -35,6 +36,8 @@
import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;

Expand All @@ -60,6 +63,9 @@ class MatchingExecutorTest {
@Autowired
private ParticipationRepository participationRepository;

@Autowired
private ServerToUserAlarmRepository serverToUserAlarmRepository;

@MockBean
private PullRequestProvider pullRequestProvider;

Expand All @@ -71,6 +77,7 @@ class MatchingExecutorTest {
private Member movin;
private Member ten;
private Member cho;

@Autowired
private RoomMatchInfoRepository roomMatchInfoRepository;

Expand All @@ -84,7 +91,7 @@ void setUp() {
cho = memberRepository.save(MemberFixture.MEMBER_CHOCO());

room = roomRepository.save(RoomFixture.ROOM_DOMAIN(pororo, LocalDateTime.now().plusSeconds(3)));
roomMatchInfoRepository.save(new RoomMatchInfo(room.getId(),true));
roomMatchInfoRepository.save(new RoomMatchInfo(room.getId(), true));
emptyParticipantRoom = roomRepository.save(RoomFixture.ROOM_DOMAIN(ash, LocalDateTime.now().plusSeconds(3)));

participationRepository.save(new Participation(room, pororo, MemberRole.BOTH, room.getMatchingSize()));
Expand Down Expand Up @@ -122,6 +129,11 @@ private PullRequestInfo getPullRequestInfo(Member pororo, Member ash, Member joy
));
}

@AfterEach
void tearDown() {
serverToUserAlarmRepository.deleteAll();
}

@Test
@DisplayName("매칭을 진행한다.")
void match() {
Expand All @@ -133,6 +145,21 @@ void match() {
assertThat(matchResults).isNotEmpty();
}

@Test
@DisplayName("매칭이 완료되면 매칭 완료 알람이 생성된다.")
void matchCompleteAlarm() {
AutomaticMatching automaticMatching = automaticMatchingRepository.save(new AutomaticMatching(room.getId(), room.getRecruitmentDeadline()));

matchingExecutor.match(automaticMatching.getRoomId());

List<ServerToUserAlarm> serverToUserAlarms = serverToUserAlarmRepository.findAll();

assertAll(
() -> assertThat(serverToUserAlarms).isNotEmpty(),
() -> assertEquals(serverToUserAlarms.get(0).getAlarmActionType(), AlarmActionType.MATCH_COMPLETE)
);
}

@Transactional
@Test
@DisplayName("매칭 시도 중 예외가 발생했다면 방 상태를 FAIL로 변경한다.")
Expand All @@ -143,4 +170,22 @@ void matchFail() {

assertThat(emptyParticipantRoom.getStatus()).isEqualTo(RoomStatus.FAIL);
}

// 이 부분 트랜잭션 문제 같은데 정확히 모르겠어서 같이 봐주셨으면 합니당...
@Disabled
@Transactional
@Test
@DisplayName("매칭이 실패하면 매칭 실패 알람이 생성된다.")
void matchFailAlarm() {
AutomaticMatching automaticMatching = automaticMatchingRepository.save(new AutomaticMatching(emptyParticipantRoom.getId(), emptyParticipantRoom.getRecruitmentDeadline()));

matchingExecutor.match(automaticMatching.getRoomId());
List<ServerToUserAlarm> serverToUserAlarms = serverToUserAlarmRepository.findAll();

assertAll(
() -> assertThat(emptyParticipantRoom.getStatus()).isEqualTo(RoomStatus.FAIL),
() -> assertThat(serverToUserAlarms).isNotEmpty(),
() -> assertEquals(serverToUserAlarms.get(0).getAlarmActionType(), AlarmActionType.MATCH_FAIL)
);
}
}

0 comments on commit fb014df

Please sign in to comment.