mirror of
https://github.com/nagisa77/OpenIsle.git
synced 2026-02-22 22:21:09 +08:00
Add tests for notification module
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
package com.openisle.controller;
|
||||
|
||||
import com.openisle.model.Notification;
|
||||
import com.openisle.model.NotificationType;
|
||||
import com.openisle.model.Post;
|
||||
import com.openisle.service.NotificationService;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mockito;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@WebMvcTest(NotificationController.class)
|
||||
@AutoConfigureMockMvc(addFilters = false)
|
||||
class NotificationControllerTest {
|
||||
|
||||
@Autowired
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@MockBean
|
||||
private NotificationService notificationService;
|
||||
|
||||
@Test
|
||||
void listNotifications() throws Exception {
|
||||
Notification n = new Notification();
|
||||
n.setId(1L);
|
||||
n.setType(NotificationType.POST_VIEWED);
|
||||
Post p = new Post();
|
||||
p.setId(2L);
|
||||
n.setPost(p);
|
||||
n.setCreatedAt(LocalDateTime.now());
|
||||
Mockito.when(notificationService.listNotifications("alice", null))
|
||||
.thenReturn(List.of(n));
|
||||
|
||||
mockMvc.perform(get("/api/notifications")
|
||||
.principal(new UsernamePasswordAuthenticationToken("alice","p")))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$[0].id").value(1))
|
||||
.andExpect(jsonPath("$[0].postId").value(2));
|
||||
}
|
||||
|
||||
@Test
|
||||
void markReadEndpoint() throws Exception {
|
||||
mockMvc.perform(post("/api/notifications/read")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content("{\"ids\":[1,2]}" )
|
||||
.principal(new UsernamePasswordAuthenticationToken("alice","p")))
|
||||
.andExpect(status().isOk());
|
||||
|
||||
verify(notificationService).markRead("alice", List.of(1L,2L));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package com.openisle.service;
|
||||
|
||||
import com.openisle.model.Notification;
|
||||
import com.openisle.model.User;
|
||||
import com.openisle.repository.NotificationRepository;
|
||||
import com.openisle.repository.UserRepository;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
class NotificationServiceTest {
|
||||
|
||||
@Test
|
||||
void markReadUpdatesOnlyOwnedNotifications() {
|
||||
NotificationRepository nRepo = mock(NotificationRepository.class);
|
||||
UserRepository uRepo = mock(UserRepository.class);
|
||||
NotificationService service = new NotificationService(nRepo, uRepo);
|
||||
|
||||
User user = new User();
|
||||
user.setId(1L);
|
||||
user.setUsername("alice");
|
||||
when(uRepo.findByUsername("alice")).thenReturn(Optional.of(user));
|
||||
|
||||
Notification n1 = new Notification();
|
||||
n1.setId(10L);
|
||||
n1.setUser(user);
|
||||
Notification n2 = new Notification();
|
||||
n2.setId(11L);
|
||||
n2.setUser(user);
|
||||
when(nRepo.findAllById(List.of(10L, 11L))).thenReturn(List.of(n1, n2));
|
||||
|
||||
service.markRead("alice", List.of(10L, 11L));
|
||||
|
||||
assertTrue(n1.isRead());
|
||||
assertTrue(n2.isRead());
|
||||
verify(nRepo).saveAll(List.of(n1, n2));
|
||||
}
|
||||
|
||||
@Test
|
||||
void listNotificationsWithoutFilter() {
|
||||
NotificationRepository nRepo = mock(NotificationRepository.class);
|
||||
UserRepository uRepo = mock(UserRepository.class);
|
||||
NotificationService service = new NotificationService(nRepo, uRepo);
|
||||
|
||||
User user = new User();
|
||||
user.setId(2L);
|
||||
user.setUsername("bob");
|
||||
when(uRepo.findByUsername("bob")).thenReturn(Optional.of(user));
|
||||
|
||||
Notification n = new Notification();
|
||||
when(nRepo.findByUserOrderByCreatedAtDesc(user)).thenReturn(List.of(n));
|
||||
|
||||
List<Notification> list = service.listNotifications("bob", null);
|
||||
|
||||
assertEquals(1, list.size());
|
||||
verify(nRepo).findByUserOrderByCreatedAtDesc(user);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user