Skip to content

Commit

Permalink
test: 기존 테스트 트랜잭션 분리해도 문제없도록 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
melonturtle committed Mar 13, 2024
1 parent 9efb24a commit a37b8c8
Showing 1 changed file with 66 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package life.offonoff.ab.application.notification;

import jakarta.persistence.EntityManager;
import life.offonoff.ab.domain.TestEntityUtil.TestMember;
import life.offonoff.ab.domain.comment.Comment;
import life.offonoff.ab.domain.comment.LikedComment;
import life.offonoff.ab.domain.member.Member;
Expand All @@ -17,18 +16,21 @@
import life.offonoff.ab.web.response.notification.NotificationResponse;
import life.offonoff.ab.web.response.notification.message.CommentOnTopicNotificationMessage;
import life.offonoff.ab.web.response.notification.message.VoteCountOnTopicNotificationMessage;
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.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.transaction.TestTransaction;
import org.springframework.test.jdbc.JdbcTestUtils;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import static life.offonoff.ab.domain.TestEntityUtil.*;
import static life.offonoff.ab.domain.TestEntityUtil.createRandomMember;
import static life.offonoff.ab.domain.TestEntityUtil.createRandomTopic;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

Expand All @@ -48,6 +50,23 @@ public class NotificationServiceIntegrationTest {
@Autowired
EntityManager em;

@Autowired
private JdbcTemplate jdbcTemplate;

private String[] touchedTables = {"notification", "comment", "topic", "member"};

@BeforeEach
void setUp() {
JdbcTestUtils.deleteFromTables(
jdbcTemplate, touchedTables);
}

@AfterEach
void tearDown() {
JdbcTestUtils.deleteFromTables(
jdbcTemplate, touchedTables);
}

@Test
@DisplayName("VoteCountOnTopic 알림 생성한다.")
void create_VoteCountOnTopicNotification() {
Expand All @@ -61,6 +80,8 @@ void create_VoteCountOnTopicNotification() {
.build().buildTopic();
em.persist(topic);

commitTestTransactionAndRestart();

notificationService.notifyVoteCountOnTopic(topic);

// when
Expand All @@ -84,12 +105,15 @@ void delete_VoteResultNotification_when_topic_deleted() {
Topic topic = createRandomTopic();
em.persist(topic);

commitTestTransactionAndRestart();

// notification
VoteResultNotification voteResultNotification = VoteResultNotification.createForAuthor(topic);
em.persist(voteResultNotification);

// when
topicRepository.delete(topic);
commitTestTransactionAndRestart();

// then
assertThat(notificationService.findAllByReceiverId(author.getId())).isEmpty();
Expand All @@ -110,13 +134,17 @@ void delete_VoteCountOnTopicNotification_when_topic_deleted() {
Topic topic = createRandomTopic();
em.persist(topic);

commitTestTransactionAndRestart();

// notification
VoteCountOnTopicNotification notification = new VoteCountOnTopicNotification(topic);
em.persist(notification);

// when
topicRepository.delete(topic);

commitTestTransactionAndRestart();

// then
assertThat(notificationService.findAllByReceiverId(author.getId())).isEmpty();
}
Expand Down Expand Up @@ -144,6 +172,8 @@ void create_CommentOnTopicNotification_when_commented_by_voter() {
Comment comment = new Comment(commenter, topic, ChoiceOption.CHOICE_A, "content");
em.persist(comment);

commitTestTransactionAndRestart();

notificationService.notifyCommentOnTopic(comment);

// when
Expand Down Expand Up @@ -171,6 +201,8 @@ void does_not_create_CommentOnTopicNotification_when_commented_by_author() {
Comment comment = Comment.createAuthorsComment(author, topic, "content");
em.persist(comment);

commitTestTransactionAndRestart();

notificationService.notifyCommentOnTopic(comment);

// when
Expand Down Expand Up @@ -204,11 +236,14 @@ void delete_CommentOnTopicNotification_when_comment_deleted() {
Comment comment = new Comment(commenter, topic, ChoiceOption.CHOICE_A, "content");
em.persist(comment);

commitTestTransactionAndRestart();

notificationService.notifyCommentOnTopic(comment);

// when
comment.remove();
em.remove(comment);
em.remove(em.merge(comment));
commitTestTransactionAndRestart();

// then
assertThat(notificationService.findAllByReceiverId(author.getId())).isEmpty();
Expand Down Expand Up @@ -240,10 +275,13 @@ void delete_CommentOnTopicNotification_when_topic_deleted(){
Comment comment = new Comment(commenter, topic, ChoiceOption.CHOICE_A, "content");
em.persist(comment);

commitTestTransactionAndRestart();

notificationService.notifyCommentOnTopic(comment);

// when
em.remove(topic);
em.remove(em.merge(topic));

commitTestTransactionAndRestart();

// then
List<NotificationResponse> responses = notificationService.findAllByReceiverId(author.getId());
Expand Down Expand Up @@ -286,6 +324,8 @@ void create_LikeInCommentNotification_when_comment_liked_by_liker() {

LikedComment likedComment = new LikedComment(liker, comment);

commitTestTransactionAndRestart();

notificationService.notifyLikeInComment(likedComment);

// when
Expand Down Expand Up @@ -325,6 +365,8 @@ void does_not_create_LikeInCommentNotification_when_liker_is_writer() {

LikedComment likedComment = new LikedComment(commenter, comment);

commitTestTransactionAndRestart();

notificationService.notifyLikeInComment(likedComment);

// when
Expand Down Expand Up @@ -370,11 +412,14 @@ void delete_LikeInCommentNotification_when_comment_deleted() {

LikedComment likedComment = new LikedComment(commenter, comment);

commitTestTransactionAndRestart();

notificationService.notifyLikeInComment(likedComment);

// when
comment.remove();
em.remove(comment);
em.remove(em.merge(comment));
commitTestTransactionAndRestart();

// then
List<NotificationResponse> responses = notificationService.findAllByReceiverId(commenter.getId());
Expand Down Expand Up @@ -416,10 +461,13 @@ void delete_LikeInCommentNotification_when_topic_deleted() {

LikedComment likedComment = new LikedComment(commenter, comment);

commitTestTransactionAndRestart();

notificationService.notifyLikeInComment(likedComment);

// when
em.remove(topic);
em.remove(em.merge(topic));
commitTestTransactionAndRestart();

// then
List<NotificationResponse> responses = notificationService.findAllByReceiverId(commenter.getId());
Expand Down Expand Up @@ -466,4 +514,14 @@ void read_notification_exception_read_by_non_receiver() {
assertThatThrownBy(() -> notificationService.readNotification(nonReceiver.getId(), notification.getId()))
.isInstanceOf(IllegalReceiverException.class);
}

private void commitTestTransactionAndRestart() {
// 현재 영속성 컨텍스트의 엔티티 상태 정보 db에 반영.
// 실제 운영 중 notificationService는 엔티티의 상태가 이미 반영된 상태에서만 알림을 보낼 것이므로
// 운영 상황과 같다.
// ! 주의할 점은 실제로 데이터베이스에 커밋되므로 수동으로 롤백해줘야한다.
TestTransaction.flagForCommit();
TestTransaction.end();
TestTransaction.start();
}
}

0 comments on commit a37b8c8

Please sign in to comment.