Skip to content

Commit

Permalink
✅ 이벤트를 적용한 알림 테스트 코드 작성
Browse files Browse the repository at this point in the history
  • Loading branch information
weonest committed Dec 27, 2023
1 parent 950a02d commit b1686b2
Show file tree
Hide file tree
Showing 4 changed files with 161 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ void getNotificationsTest() throws Exception {
),
responseFields(
fieldWithPath("notifications[].id").type(NUMBER).description("알림 식별자"),
fieldWithPath("notifications[].name").type(STRING).description("알림 대상 스테디 이름"),
fieldWithPath("notifications[].type").type(STRING).description("알림 타입"),
fieldWithPath("notifications[].content").type(STRING).description("알림 내용"),
fieldWithPath("notifications[].redirectUri").type(STRING).description("리다이렉트 Uri"),
fieldWithPath("notifications[].result").type(STRING).description("알림 결과"),
fieldWithPath("notifications[].isRead").type(BOOLEAN).description("읽음 여부"),
fieldWithPath("freshCount").type(NUMBER).description("읽지 않은 알림 개수")
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package dev.steady.notification.event;

import dev.steady.application.domain.repository.ApplicationRepository;
import dev.steady.application.domain.repository.SurveyResultRepository;
import dev.steady.application.dto.request.ApplicationStatusUpdateRequest;
import dev.steady.application.service.ApplicationService;
import dev.steady.notification.domain.Notification;
import dev.steady.notification.domain.repository.NotificationRepository;
import dev.steady.steady.domain.repository.SteadyRepository;
import dev.steady.user.domain.Position;
import dev.steady.user.domain.Stack;
import dev.steady.user.domain.User;
import dev.steady.user.domain.repository.PositionRepository;
import dev.steady.user.domain.repository.StackRepository;
import dev.steady.user.domain.repository.UserRepository;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

import static dev.steady.application.domain.ApplicationStatus.ACCEPTED;
import static dev.steady.application.fixture.ApplicationFixture.createApplication;
import static dev.steady.application.fixture.SurveyResultFixture.createSurveyResultRequests;
import static dev.steady.global.auth.AuthFixture.createUserInfo;
import static dev.steady.steady.fixture.SteadyFixturesV2.createSteady;
import static dev.steady.user.fixture.UserFixturesV2.generatePosition;
import static dev.steady.user.fixture.UserFixturesV2.generateStack;
import static dev.steady.user.fixture.UserFixturesV2.generateUser;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.*;

@SpringBootTest
class NotificationEventListenerTest {

@Autowired
private UserRepository userRepository;

@Autowired
private StackRepository stackRepository;

@Autowired
private SteadyRepository steadyRepository;

@Autowired
private PositionRepository positionRepository;

@Autowired
private SurveyResultRepository surveyResultRepository;

@Autowired
private NotificationRepository notificationRepository;

@Autowired
private ApplicationRepository applicationRepository;

@Autowired
private ApplicationService applicationService;

private Position position;
private Stack stack;
private User leader;

@BeforeEach
void setUp() {
this.position = positionRepository.save(generatePosition());
this.stack = stackRepository.save(generateStack());
this.leader = userRepository.save(generateUser(position));
}

@AfterEach
void tearDown() {
surveyResultRepository.deleteAll();
applicationRepository.deleteAll();
steadyRepository.deleteAll();
notificationRepository.deleteAll();
userRepository.deleteAll();
stackRepository.deleteAll();
positionRepository.deleteAll();
}

@Test
@DisplayName("새로운 신청서가 등록되면 리더에게 새로운 신청 알림이 생성된다.")
void createNotificationWhenCreateApplicationTest() throws InterruptedException {
//given
var steady = steadyRepository.save(createSteady(leader, List.of(stack)));
var user = userRepository.save(generateUser(position));
var surveyResultRequests = createSurveyResultRequests();
var userInfo = createUserInfo(user.getId());

//when
applicationService.createApplication(steady.getId(), surveyResultRequests, userInfo);
Thread.sleep(500);

//then
Notification notification = notificationRepository.findByReceiverId(leader.getId()).get(0);
assertAll(
() -> assertThat(notification).isNotNull(),
() -> assertThat(notification.getReceiver().getId()).isEqualTo(leader.getId())
);
}

@Test
@DisplayName("신청서가 거절 혹은 수락되면 유저에게 새로운 신청서 결과 알림이 생성된다.")
void createNotificationWhenApplicationGotResultTest() throws InterruptedException {
//given
var steady = steadyRepository.save(createSteady(leader, List.of(stack)));
var user = userRepository.save(generateUser(position));
var application = applicationRepository.save(createApplication(user, steady));
var userInfo = createUserInfo(leader.getId());
var request = new ApplicationStatusUpdateRequest(ACCEPTED);

//when
applicationService.updateStatusOfApplication(application.getId(), request, userInfo);
Thread.sleep(500);

//then
Notification notification = notificationRepository.findByReceiverId(user.getId()).get(0);
assertAll(
() -> assertThat(notification).isNotNull(),
() -> assertThat(notification.getReceiver().getId()).isEqualTo(user.getId())
);
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.steady.notification.fixture;

import dev.steady.notification.domain.Notification;
import dev.steady.notification.domain.NotificationResult;
import dev.steady.notification.domain.NotificationType;
import dev.steady.notification.dto.NotificationResponse;
import dev.steady.notification.dto.NotificationsResponse;
Expand All @@ -10,28 +11,30 @@

public class NotificationFixture {

public static Notification createFreshApplicationNoti(User receiver) {
public static Notification createFreshApplicationEvent(User receiver) {
return Notification.builder()
.name("테스트 스테디")
.type(NotificationType.FRESH_APPLICATION)
.content("알림 내용")
.redirectUri("리다이렉트 uri")
.result(NotificationResult.ARRIVED)
.receiver(receiver)
.build();
}

public static Notification createApplicationResultNoti(User receiver) {
public static Notification createApplicationResultEvent(User receiver) {
return Notification.builder()
.name("테스트 스테디")
.type(NotificationType.APPLICATION_RESULT)
.content("알림 내용")
.redirectUri("리다이렉트 uri")
.result(NotificationResult.ACCEPTED)
.receiver(receiver)
.build();
}

public static NotificationsResponse createNotificationsResponse() {
return new NotificationsResponse(List.of(
new NotificationResponse(1L, NotificationType.FRESH_APPLICATION, "내용", "리다이렉트 uri", false),
new NotificationResponse(1L, NotificationType.FRESH_APPLICATION, "내용", "리다이렉트 uri", true)
new NotificationResponse(1L, "테스트 스테디", NotificationType.FRESH_APPLICATION,
NotificationResult.ARRIVED,false),
new NotificationResponse(1L, "테스트 스테디", NotificationType.FRESH_APPLICATION,
NotificationResult.ARRIVED, true)
), 1L);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
import dev.steady.application.dto.request.ApplicationStatusUpdateRequest;
import dev.steady.application.service.ApplicationService;
import dev.steady.global.exception.NotFoundException;
import dev.steady.notification.domain.ApplicationResultNotificationStrategy;
import dev.steady.notification.domain.FreshApplicationNotificationStrategy;
import dev.steady.notification.domain.Notification;
import dev.steady.notification.domain.repository.NotificationRepository;
import dev.steady.notification.dto.NotificationsResponse;
import dev.steady.notification.event.ApplicationResultNotificationEvent;
import dev.steady.notification.event.FreshApplicationNotificationEvent;
import dev.steady.steady.domain.repository.SteadyRepository;
import dev.steady.user.domain.Position;
import dev.steady.user.domain.Stack;
Expand All @@ -31,10 +31,8 @@
import static dev.steady.application.fixture.ApplicationFixture.createApplication;
import static dev.steady.application.fixture.SurveyResultFixture.createSurveyResultRequests;
import static dev.steady.global.auth.AuthFixture.createUserInfo;
import static dev.steady.notification.domain.NotificationMessage.getApplicationResultMessage;
import static dev.steady.notification.domain.NotificationMessage.getFreshApplicationMessage;
import static dev.steady.notification.fixture.NotificationFixture.createApplicationResultNoti;
import static dev.steady.notification.fixture.NotificationFixture.createFreshApplicationNoti;
import static dev.steady.notification.fixture.NotificationFixture.createApplicationResultEvent;
import static dev.steady.notification.fixture.NotificationFixture.createFreshApplicationEvent;
import static dev.steady.steady.fixture.SteadyFixturesV2.createSteady;
import static dev.steady.user.fixture.UserFixturesV2.generatePosition;
import static dev.steady.user.fixture.UserFixturesV2.generateStack;
Expand Down Expand Up @@ -100,16 +98,16 @@ void tearDown() {
void createNewApplicationNotificationTest() {
// given
var steady = steadyRepository.save(createSteady(leader, List.of(stack)));
var freshApplicationNoti = new FreshApplicationNotificationStrategy(steady);
var notificationEvent = new FreshApplicationNotificationEvent(steady);

// when
notificationService.create(freshApplicationNoti);
notificationService.create(notificationEvent);

// then
Notification notification = notificationRepository.findByReceiverId(leader.getId()).get(0);
assertAll(
() -> assertThat(notification.getContent()).isEqualTo(getFreshApplicationMessage(steady.getName())),
() -> assertThat(notification.getRedirectUri()).isEqualTo(freshApplicationNoti.getRedirectUri())
() -> assertThat(notification.getName()).isEqualTo(steady.getName()),
() -> assertThat(notification.getResult()).isEqualTo(notificationEvent.getResult())
);
}

Expand All @@ -121,57 +119,16 @@ void createAcceptedApplicationNotificationTest() {
var user = userRepository.save(generateUser(position));
var application = createApplication(user, steady);
application.updateStatus(ApplicationStatus.ACCEPTED, leader);
var applicationResultNoti = new ApplicationResultNotificationStrategy(application);
var notificationEvent = new ApplicationResultNotificationEvent(application);

// when
notificationService.create(applicationResultNoti);
notificationService.create(notificationEvent);

// then
Notification notification = notificationRepository.findByReceiverId(user.getId()).get(0);
assertAll(
() -> assertThat(notification.getContent()).isEqualTo(getApplicationResultMessage(steady.getName(), application.getStatus())),
() -> assertThat(notification.getRedirectUri()).isEqualTo(applicationResultNoti.getRedirectUri())
);
}

@Test
@DisplayName("새로운 신청서가 등록되면 리더에게 새로운 신청 알림이 생성된다.")
void createNotificationWhenCreateApplicationTest() {
//given
var steady = steadyRepository.save(createSteady(leader, List.of(stack)));
var user = userRepository.save(generateUser(position));
var surveyResultRequests = createSurveyResultRequests();
var userInfo = createUserInfo(user.getId());

//when
applicationService.createApplication(steady.getId(), surveyResultRequests, userInfo);

//then
Notification notification = notificationRepository.findByReceiverId(leader.getId()).get(0);
assertAll(
() -> assertThat(notification).isNotNull(),
() -> assertThat(notification.getReceiver().getId()).isEqualTo(leader.getId())
);
}

@Test
@DisplayName("신청서가 거절 혹은 수락되면 유저에게 새로운 신청서 결과 알림이 생성된다.")
void createNotificationWhenApplicationGotResultTest() {
//given
var steady = steadyRepository.save(createSteady(leader, List.of(stack)));
var user = userRepository.save(generateUser(position));
var application = applicationRepository.save(createApplication(user, steady));
var userInfo = createUserInfo(leader.getId());
var request = new ApplicationStatusUpdateRequest(ACCEPTED);

//when
applicationService.updateStatusOfApplication(application.getId(), request, userInfo);

//then
Notification notification = notificationRepository.findByReceiverId(user.getId()).get(0);
assertAll(
() -> assertThat(notification).isNotNull(),
() -> assertThat(notification.getReceiver().getId()).isEqualTo(user.getId())
() -> assertThat(notification.getName()).isEqualTo(steady.getName()),
() -> assertThat(notification.getResult()).isEqualTo(notificationEvent.getResult())
);
}

Expand All @@ -181,8 +138,8 @@ void getNotificationsTest() {
// given
var userInfo = createUserInfo(leader.getId());
steadyRepository.save(createSteady(leader, List.of(stack)));
notificationRepository.save(createFreshApplicationNoti(leader));
notificationRepository.save(createApplicationResultNoti(leader));
notificationRepository.save(createFreshApplicationEvent(leader));
notificationRepository.save(createApplicationResultEvent(leader));

// when
NotificationsResponse notifications = notificationService.getNotifications(userInfo);
Expand All @@ -197,7 +154,7 @@ void getNotificationsTest() {
void readNotificaitonTest() {
// given
var userInfo = createUserInfo(leader.getId());
var notification = notificationRepository.save(createFreshApplicationNoti(leader));
var notification = notificationRepository.save(createFreshApplicationEvent(leader));

// when
notificationService.readNotification(notification.getId(), userInfo);
Expand All @@ -213,8 +170,8 @@ void readNotificaitonsTest() {
// given
var user = userRepository.save(generateUser(position));
var userInfo = createUserInfo(user.getId());
notificationRepository.save(createFreshApplicationNoti(user));
notificationRepository.save(createFreshApplicationNoti(user));
notificationRepository.save(createFreshApplicationEvent(user));
notificationRepository.save(createFreshApplicationEvent(user));

// when
notificationService.readNotifications(userInfo);
Expand All @@ -229,7 +186,7 @@ void readNotificaitonsTest() {
void deleteNotificaitonTest() {
// given
var userInfo = createUserInfo(leader.getId());
Notification notification = notificationRepository.save(createFreshApplicationNoti(leader));
Notification notification = notificationRepository.save(createFreshApplicationEvent(leader));

// when
notificationService.deleteNotification(notification.getId(), userInfo);
Expand All @@ -244,8 +201,8 @@ void deleteNotificaitonTest() {
void deleteNotificaitonsTest() {
// given
var userInfo = createUserInfo(leader.getId());
notificationRepository.save(createFreshApplicationNoti(leader));
notificationRepository.save(createFreshApplicationNoti(leader));
notificationRepository.save(createFreshApplicationEvent(leader));
notificationRepository.save(createFreshApplicationEvent(leader));

// when
notificationService.deleteAll(userInfo);
Expand Down

0 comments on commit b1686b2

Please sign in to comment.