Skip to content

Commit

Permalink
test: Notification 관련 테스트 코드 작성
Browse files Browse the repository at this point in the history
  • Loading branch information
peeerr committed Nov 7, 2024
1 parent 352ceb7 commit 8191df6
Show file tree
Hide file tree
Showing 4 changed files with 462 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package com.potatocake.everymoment.controller;

import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.then;
import static org.mockito.BDDMockito.willDoNothing;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import com.potatocake.everymoment.dto.response.NotificationListResponse;
import com.potatocake.everymoment.entity.Member;
import com.potatocake.everymoment.security.MemberDetails;
import com.potatocake.everymoment.service.NotificationService;
import java.time.LocalDateTime;
import java.util.List;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;

@WebMvcTest(NotificationController.class)
class NotificationControllerTest {

@Autowired
private MockMvc mockMvc;

@MockBean
private NotificationService notificationService;

@Test
@DisplayName("알림 목록이 성공적으로 조회된다.")
void should_GetNotifications_When_ValidRequest() throws Exception {
// given
Long memberId = 1L;
Member member = Member.builder()
.id(memberId)
.number(1234L)
.nickname("testUser")
.build();
MemberDetails memberDetails = new MemberDetails(member);

List<NotificationListResponse> responses = List.of(
NotificationListResponse.builder()
.id(1L)
.content("Notification 1")
.type("TEST1")
.targetId(1L)
.isRead(false)
.createdAt(LocalDateTime.now())
.build(),
NotificationListResponse.builder()
.id(2L)
.content("Notification 2")
.type("TEST2")
.targetId(2L)
.isRead(true)
.createdAt(LocalDateTime.now())
.build()
);

given(notificationService.getNotifications(memberId)).willReturn(responses);

// when
ResultActions result = mockMvc.perform(get("/api/notifications")
.with(user(memberDetails)));

// then
result.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value(200))
.andExpect(jsonPath("$.message").value("success"))
.andExpect(jsonPath("$.info").isArray())
.andExpect(jsonPath("$.info[0].content").value("Notification 1"))
.andExpect(jsonPath("$.info[0].read").value(false))
.andExpect(jsonPath("$.info[1].content").value("Notification 2"))
.andExpect(jsonPath("$.info[1].read").value(true));

then(notificationService).should().getNotifications(memberId);
}

@Test
@DisplayName("알림이 성공적으로 읽음 처리된다.")
void should_UpdateNotification_When_ValidId() throws Exception {
// given
Long memberId = 1L;
Long notificationId = 1L;
Member member = Member.builder()
.id(memberId)
.number(1234L)
.nickname("testUser")
.build();
MemberDetails memberDetails = new MemberDetails(member);

willDoNothing().given(notificationService).updateNotification(memberId, notificationId);

// when
ResultActions result = mockMvc.perform(patch("/api/notifications/{notificationId}", notificationId)
.with(user(memberDetails))
.with(csrf()));

// then
result.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value(200))
.andExpect(jsonPath("$.message").value("success"));

then(notificationService).should().updateNotification(eq(memberId), eq(notificationId));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.potatocake.everymoment.entity;

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

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

class NotificationTest {

@Test
@DisplayName("알림이 성공적으로 생성된다.")
void should_CreateNotification_When_ValidInput() {
// given
Member member = Member.builder()
.id(1L)
.build();

// when
Notification notification = Notification.builder()
.member(member)
.content("Test notification")
.type("TEST")
.targetId(1L)
.isRead(false)
.build();

// then
assertThat(notification.getMember()).isEqualTo(member);
assertThat(notification.getContent()).isEqualTo("Test notification");
assertThat(notification.getType()).isEqualTo("TEST");
assertThat(notification.getTargetId()).isEqualTo(1L);
assertThat(notification.isRead()).isFalse();
}

@Test
@DisplayName("알림이 성공적으로 읽음 처리된다.")
void should_MarkAsRead_When_UpdateIsRead() {
// given
Notification notification = Notification.builder()
.content("Test notification")
.isRead(false)
.build();

// when
notification.updateIsRead();

// then
assertThat(notification.isRead()).isTrue();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package com.potatocake.everymoment.repository;

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

import com.potatocake.everymoment.entity.Member;
import com.potatocake.everymoment.entity.Notification;
import java.util.List;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.test.context.TestPropertySource;

@TestPropertySource(properties = "spring.jpa.hibernate.ddl-auto=create-drop")
@DataJpaTest
class NotificationRepositoryTest {

@Autowired
private NotificationRepository notificationRepository;

@Autowired
private MemberRepository memberRepository;

@Test
@DisplayName("알림이 성공적으로 저장된다.")
void should_SaveNotification_When_ValidEntity() {
// given
Member member = Member.builder()
.number(1234L)
.nickname("testUser")
.profileImageUrl("https://example.com/profile.jpg")
.build();
Member savedMember = memberRepository.save(member);

Notification notification = Notification.builder()
.member(savedMember)
.content("Test notification")
.type("TEST")
.targetId(1L)
.isRead(false)
.build();

// when
Notification savedNotification = notificationRepository.save(notification);

// then
assertThat(savedNotification.getId()).isNotNull();
assertThat(savedNotification.getContent()).isEqualTo("Test notification");
assertThat(savedNotification.getMember()).isEqualTo(savedMember);
}

@Test
@DisplayName("회원의 알림 목록이 성공적으로 조회된다.")
void should_FindNotifications_When_FilteringByMemberId() {
// given
Member member = Member.builder()
.number(1234L)
.nickname("testUser")
.profileImageUrl("https://example.com/profile.jpg")
.build();
Member savedMember = memberRepository.save(member);

List<Notification> notifications = List.of(
Notification.builder()
.member(savedMember)
.content("Notification 1")
.type("TEST1")
.targetId(1L)
.build(),
Notification.builder()
.member(savedMember)
.content("Notification 2")
.type("TEST2")
.targetId(2L)
.build()
);

notificationRepository.saveAll(notifications);

// when
List<Notification> foundNotifications = notificationRepository
.findAllByMemberId(savedMember.getId());

// then
assertThat(foundNotifications).hasSize(2);
assertThat(foundNotifications)
.extracting("content")
.containsExactlyInAnyOrder("Notification 1", "Notification 2");
}

@Test
@DisplayName("알림이 성공적으로 삭제된다.")
void should_DeleteNotification_When_ValidEntity() {
// given
Member member = Member.builder()
.number(1234L)
.nickname("testUser")
.profileImageUrl("https://example.com/profile.jpg")
.build();
Member savedMember = memberRepository.save(member);

Notification notification = Notification.builder()
.member(savedMember)
.content("Test notification")
.type("TEST")
.targetId(1L)
.build();

Notification savedNotification = notificationRepository.save(notification);

// when
notificationRepository.delete(savedNotification);

// then
List<Notification> remainingNotifications = notificationRepository
.findAllByMemberId(savedMember.getId());
assertThat(remainingNotifications).isEmpty();
}

}
Loading

0 comments on commit 8191df6

Please sign in to comment.