优化目录结构

This commit is contained in:
WilliamColton
2025-08-03 01:27:28 +08:00
parent d63081955e
commit c08723574d
222 changed files with 2 additions and 25 deletions

View File

@@ -0,0 +1,83 @@
package com.openisle.controller;
import org.junit.jupiter.api.Test;
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.context.annotation.Import;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.boot.test.mock.mockito.MockBean;
import com.openisle.config.CustomAccessDeniedHandler;
import com.openisle.config.SecurityConfig;
import com.openisle.service.JwtService;
import com.openisle.repository.UserRepository;
import com.openisle.service.UserVisitService;
import com.openisle.model.Role;
import com.openisle.model.User;
import java.util.Optional;
import org.mockito.Mockito;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@WebMvcTest(AdminController.class)
@AutoConfigureMockMvc
@Import({SecurityConfig.class, CustomAccessDeniedHandler.class})
class AdminControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private JwtService jwtService;
@MockBean
private UserRepository userRepository;
@MockBean
private UserVisitService userVisitService;
@Test
void adminHelloReturnsMessage() throws Exception {
Mockito.when(jwtService.validateAndGetSubject("adminToken")).thenReturn("admin");
User admin = new User();
admin.setUsername("admin");
admin.setPassword("p");
admin.setEmail("a@b.com");
admin.setRole(Role.ADMIN);
Mockito.when(userRepository.findByUsername("admin")).thenReturn(Optional.of(admin));
mockMvc.perform(get("/api/admin/hello").header("Authorization", "Bearer adminToken"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.message").value("Hello, Admin User"));
}
@Test
void adminHelloMissingToken() throws Exception {
mockMvc.perform(get("/api/admin/hello"))
.andExpect(status().isUnauthorized())
.andExpect(jsonPath("$.error").value("Missing token"));
}
@Test
void adminHelloInvalidToken() throws Exception {
Mockito.when(jwtService.validateAndGetSubject("bad")).thenThrow(new RuntimeException());
mockMvc.perform(get("/api/admin/hello").header("Authorization", "Bearer bad"))
.andExpect(status().isUnauthorized())
.andExpect(jsonPath("$.error").value("Invalid or expired token"));
}
@Test
void adminHelloNotAdmin() throws Exception {
Mockito.when(jwtService.validateAndGetSubject("userToken")).thenReturn("user");
User user = new User();
user.setUsername("user");
user.setPassword("p");
user.setEmail("u@example.com");
user.setRole(Role.USER);
Mockito.when(userRepository.findByUsername("user")).thenReturn(Optional.of(user));
mockMvc.perform(get("/api/admin/hello").header("Authorization", "Bearer userToken"))
.andExpect(status().isUnauthorized())
.andExpect(jsonPath("$.error").value("Unauthorized"));
}
}

View File

@@ -0,0 +1,97 @@
package com.openisle.controller;
import com.openisle.model.User;
import com.openisle.service.*;
import com.openisle.model.RegisterMode;
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.test.web.servlet.MockMvc;
import java.util.Map;
import java.util.Optional;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@WebMvcTest(AuthController.class)
@AutoConfigureMockMvc(addFilters = false)
class AuthControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private UserService userService;
@MockBean
private JwtService jwtService;
@MockBean
private EmailSender emailService;
@MockBean
private CaptchaService captchaService;
@MockBean
private GoogleAuthService googleAuthService;
@MockBean
private RegisterModeService registerModeService;
@Test
void registerSendsEmail() throws Exception {
User user = new User();
user.setEmail("a@b.com");
user.setUsername("u");
user.setVerificationCode("123456");
Mockito.when(registerModeService.getRegisterMode()).thenReturn(RegisterMode.DIRECT);
Mockito.when(userService.register(eq("u"), eq("a@b.com"), eq("p"), any(), eq(RegisterMode.DIRECT))).thenReturn(user);
mockMvc.perform(post("/api/auth/register")
.contentType(MediaType.APPLICATION_JSON)
.content("{\"username\":\"u\",\"email\":\"a@b.com\",\"password\":\"p\",\"reason\":\"test reason more than twenty\"}"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.message").exists());
Mockito.verify(emailService).sendEmail(eq("a@b.com"), any(), any());
}
@Test
void verifyCodeEndpoint() throws Exception {
Mockito.when(userService.verifyCode("u", "123")).thenReturn(true);
mockMvc.perform(post("/api/auth/verify")
.contentType(MediaType.APPLICATION_JSON)
.content("{\"username\":\"u\",\"code\":\"123\"}"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.message").value("Verified"));
}
@Test
void loginReturnsToken() throws Exception {
User user = new User();
user.setUsername("u");
Mockito.when(userService.findByUsername("u")).thenReturn(Optional.of(user));
Mockito.when(userService.matchesPassword(user, "p")).thenReturn(true);
Mockito.when(jwtService.generateToken("u")).thenReturn("token");
mockMvc.perform(post("/api/auth/login")
.contentType(MediaType.APPLICATION_JSON)
.content("{\"username\":\"u\",\"password\":\"p\"}"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.token").value("token"));
}
@Test
void loginFails() throws Exception {
Mockito.when(userService.findByUsername("u")).thenReturn(Optional.empty());
mockMvc.perform(post("/api/auth/login")
.contentType(MediaType.APPLICATION_JSON)
.content("{\"username\":\"u\",\"password\":\"bad\"}"))
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.reason_code").value("INVALID_CREDENTIALS"));
}
}

View File

@@ -0,0 +1,96 @@
package com.openisle.controller;
import com.openisle.model.Category;
import com.openisle.service.CategoryService;
import com.openisle.service.PostService;
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.test.web.servlet.MockMvc;
import java.util.List;
import static org.mockito.ArgumentMatchers.eq;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
@WebMvcTest(CategoryController.class)
@AutoConfigureMockMvc(addFilters = false)
class CategoryControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private CategoryService categoryService;
@MockBean
private PostService postService;
@Test
void createAndGetCategory() throws Exception {
Category c = new Category();
c.setId(1L);
c.setName("tech");
c.setDescription("d");
c.setIcon("i");
c.setSmallIcon("s1");
Mockito.when(categoryService.createCategory(eq("tech"), eq("d"), eq("i"), eq("s1"))).thenReturn(c);
Mockito.when(categoryService.getCategory(1L)).thenReturn(c);
mockMvc.perform(post("/api/categories")
.contentType(MediaType.APPLICATION_JSON)
.content("{\"name\":\"tech\",\"description\":\"d\",\"icon\":\"i\",\"smallIcon\":\"s1\"}"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.name").value("tech"))
.andExpect(jsonPath("$.description").value("d"))
.andExpect(jsonPath("$.icon").value("i"))
.andExpect(jsonPath("$.smallIcon").value("s1"));
mockMvc.perform(get("/api/categories/1"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.id").value(1));
}
@Test
void listCategories() throws Exception {
Category c = new Category();
c.setId(2L);
c.setName("life");
c.setDescription("d2");
c.setIcon("i2");
c.setSmallIcon("s2");
Mockito.when(categoryService.listCategories()).thenReturn(List.of(c));
mockMvc.perform(get("/api/categories"))
.andExpect(status().isOk())
.andExpect(jsonPath("$[0].name").value("life"))
.andExpect(jsonPath("$[0].description").value("d2"))
.andExpect(jsonPath("$[0].icon").value("i2"))
.andExpect(jsonPath("$[0].smallIcon").value("s2"));
}
@Test
void updateCategory() throws Exception {
Category c = new Category();
c.setId(3L);
c.setName("tech");
c.setDescription("d3");
c.setIcon("i3");
c.setSmallIcon("s3");
Mockito.when(categoryService.updateCategory(eq(3L), eq("tech"), eq("d3"), eq("i3"), eq("s3"))).thenReturn(c);
mockMvc.perform(put("/api/categories/3")
.contentType(MediaType.APPLICATION_JSON)
.content("{\"name\":\"tech\",\"description\":\"d3\",\"icon\":\"i3\",\"smallIcon\":\"s3\"}"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.id").value(3))
.andExpect(jsonPath("$.name").value("tech"))
.andExpect(jsonPath("$.description").value("d3"))
.andExpect(jsonPath("$.icon").value("i3"))
.andExpect(jsonPath("$.smallIcon").value("s3"));
}
}

View File

@@ -0,0 +1,88 @@
package com.openisle.controller;
import com.openisle.model.Comment;
import com.openisle.model.Post;
import com.openisle.model.User;
import com.openisle.service.CommentService;
import com.openisle.service.CaptchaService;
import com.openisle.service.LevelService;
import com.openisle.service.ReactionService;
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.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.test.web.servlet.MockMvc;
import java.time.LocalDateTime;
import java.util.List;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.any;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@WebMvcTest(CommentController.class)
@AutoConfigureMockMvc(addFilters = false)
class CommentControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private CommentService commentService;
@MockBean
private CaptchaService captchaService;
@MockBean
private LevelService levelService;
@MockBean
private ReactionService reactionService;
private Comment createComment(Long id, String content, String authorName) {
User user = new User();
user.setUsername(authorName);
Comment c = new Comment();
c.setId(id);
c.setContent(content);
c.setCreatedAt(LocalDateTime.now());
c.setAuthor(user);
c.setPost(new Post());
return c;
}
@Test
void createAndListComments() throws Exception {
Comment comment = createComment(1L, "hi", "bob");
Mockito.when(commentService.addComment(eq("bob"), eq(1L), eq("hi"))).thenReturn(comment);
Mockito.when(commentService.getCommentsForPost(eq(1L), any())).thenReturn(List.of(comment));
Mockito.when(commentService.getReplies(1L)).thenReturn(List.of());
Mockito.when(reactionService.getReactionsForComment(1L)).thenReturn(List.of());
mockMvc.perform(post("/api/posts/1/comments")
.contentType("application/json")
.content("{\"content\":\"hi\"}")
.principal(new UsernamePasswordAuthenticationToken("bob", "p")))
.andExpect(status().isOk())
.andExpect(jsonPath("$.content").value("hi"));
mockMvc.perform(get("/api/posts/1/comments"))
.andExpect(status().isOk())
.andExpect(jsonPath("$[0].id").value(1));
}
@Test
void replyComment() throws Exception {
Comment reply = createComment(2L, "re", "alice");
Mockito.when(commentService.addReply(eq("alice"), eq(1L), eq("re"))).thenReturn(reply);
mockMvc.perform(post("/api/comments/1/replies")
.contentType("application/json")
.content("{\"content\":\"re\"}")
.principal(new UsernamePasswordAuthenticationToken("alice", "p")))
.andExpect(status().isOk())
.andExpect(jsonPath("$.id").value(2));
}
}

View File

@@ -0,0 +1,68 @@
package com.openisle.controller;
import org.junit.jupiter.api.Test;
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.context.annotation.Import;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.boot.test.mock.mockito.MockBean;
import com.openisle.config.CustomAccessDeniedHandler;
import com.openisle.config.SecurityConfig;
import com.openisle.service.JwtService;
import com.openisle.repository.UserRepository;
import com.openisle.service.UserVisitService;
import com.openisle.model.Role;
import com.openisle.model.User;
import java.util.Optional;
import org.mockito.Mockito;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@WebMvcTest(HelloController.class)
@AutoConfigureMockMvc
@Import({SecurityConfig.class, CustomAccessDeniedHandler.class})
class HelloControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private JwtService jwtService;
@MockBean
private UserRepository userRepository;
@MockBean
private UserVisitService userVisitService;
@Test
void helloReturnsMessage() throws Exception {
Mockito.when(jwtService.validateAndGetSubject("token")).thenReturn("user");
User user = new User();
user.setUsername("user");
user.setPassword("p");
user.setEmail("u@example.com");
user.setRole(Role.USER);
Mockito.when(userRepository.findByUsername("user")).thenReturn(Optional.of(user));
mockMvc.perform(get("/api/hello").header("Authorization", "Bearer token"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.message").value("Hello, Authenticated User"));
}
@Test
void helloMissingToken() throws Exception {
mockMvc.perform(get("/api/hello"))
.andExpect(status().isUnauthorized())
.andExpect(jsonPath("$.error").value("Missing token"));
}
@Test
void helloInvalidToken() throws Exception {
Mockito.when(jwtService.validateAndGetSubject("bad")).thenThrow(new RuntimeException());
mockMvc.perform(get("/api/hello").header("Authorization", "Bearer bad"))
.andExpect(status().isUnauthorized())
.andExpect(jsonPath("$.error").value("Invalid or expired token"));
}
}

View File

@@ -0,0 +1,73 @@
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].post.id").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));
}
@Test
void unreadCountEndpoint() throws Exception {
Mockito.when(notificationService.countUnread("alice")).thenReturn(3L);
mockMvc.perform(get("/api/notifications/unread-count")
.principal(new UsernamePasswordAuthenticationToken("alice","p")))
.andExpect(status().isOk())
.andExpect(jsonPath("$.count").value(3));
}
}

View File

@@ -0,0 +1,266 @@
package com.openisle.controller;
import com.openisle.model.Post;
import com.openisle.model.User;
import com.openisle.model.Category;
import com.openisle.model.Tag;
import com.openisle.service.PostService;
import com.openisle.service.CommentService;
import com.openisle.service.ReactionService;
import com.openisle.service.CaptchaService;
import com.openisle.service.DraftService;
import com.openisle.service.LevelService;
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.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.test.web.servlet.MockMvc;
import java.time.LocalDateTime;
import java.util.List;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import org.springframework.test.util.ReflectionTestUtils;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.never;
@WebMvcTest(PostController.class)
@AutoConfigureMockMvc(addFilters = false)
class PostControllerTest {
@Autowired
private MockMvc mockMvc;
@Autowired
private PostController postController;
@MockBean
private PostService postService;
@MockBean
private CommentService commentService;
@MockBean
private ReactionService reactionService;
@MockBean
private CaptchaService captchaService;
@MockBean
private DraftService draftService;
@MockBean
private LevelService levelService;
@Test
void createAndGetPost() throws Exception {
User user = new User();
user.setUsername("alice");
Category cat = new Category();
cat.setId(1L);
cat.setName("tech");
cat.setDescription("d");
cat.setIcon("i");
Tag tag = new Tag();
tag.setId(1L);
tag.setName("java");
tag.setDescription("td");
tag.setIcon("ti");
Post post = new Post();
post.setId(1L);
post.setTitle("t");
post.setContent("c");
post.setCreatedAt(LocalDateTime.now());
post.setAuthor(user);
post.setCategory(cat);
post.setTags(java.util.Set.of(tag));
Mockito.when(commentService.getParticipants(Mockito.anyLong(), Mockito.anyInt())).thenReturn(java.util.List.of());
Mockito.when(postService.createPost(eq("alice"), eq(1L), eq("t"), eq("c"), eq(java.util.List.of(1L)))).thenReturn(post);
Mockito.when(postService.viewPost(eq(1L), Mockito.isNull())).thenReturn(post);
mockMvc.perform(post("/api/posts")
.contentType("application/json")
.content("{\"title\":\"t\",\"content\":\"c\",\"categoryId\":1,\"tagIds\":[1]}")
.principal(new UsernamePasswordAuthenticationToken("alice", "p")))
.andExpect(status().isOk())
.andExpect(jsonPath("$.title").value("t"));
mockMvc.perform(get("/api/posts/1"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.id").value(1));
}
@Test
void listPosts() throws Exception {
User user = new User();
user.setUsername("bob");
Category cat = new Category();
cat.setId(1L);
cat.setName("tech");
cat.setDescription("d");
cat.setIcon("i");
Tag tag = new Tag();
tag.setId(1L);
tag.setName("java");
tag.setDescription("td");
tag.setIcon("ti");
Post post = new Post();
post.setId(2L);
post.setTitle("hello");
post.setContent("world");
post.setCreatedAt(LocalDateTime.now());
post.setAuthor(user);
post.setCategory(cat);
post.setTags(java.util.Set.of(tag));
Mockito.when(commentService.getParticipants(Mockito.anyLong(), Mockito.anyInt())).thenReturn(java.util.List.of());
Mockito.when(postService.listPostsByCategories(Mockito.isNull(), Mockito.isNull(), Mockito.isNull()))
.thenReturn(List.of(post));
mockMvc.perform(get("/api/posts"))
.andExpect(status().isOk())
.andExpect(jsonPath("$[0].title").value("hello"));
}
@Test
void createPostRejectsInvalidCaptcha() throws Exception {
org.springframework.test.util.ReflectionTestUtils.setField(postController, "captchaEnabled", true);
org.springframework.test.util.ReflectionTestUtils.setField(postController, "postCaptchaEnabled", true);
Mockito.when(captchaService.verify("bad")).thenReturn(false);
mockMvc.perform(post("/api/posts")
.contentType("application/json")
.content("{\"title\":\"t\",\"content\":\"c\",\"categoryId\":1,\"tagIds\":[1],\"captcha\":\"bad\"}")
.principal(new UsernamePasswordAuthenticationToken("alice", "p")))
.andExpect(status().isBadRequest());
verify(postService, never()).createPost(any(), any(), any(), any(), any());
}
@Test
void getPostWithNestedData() throws Exception {
User user = new User();
user.setUsername("alice");
Category cat = new Category();
cat.setId(1L);
cat.setName("tech");
Tag tag = new Tag();
tag.setId(1L);
tag.setName("java");
Post post = new Post();
post.setId(1L);
post.setTitle("t");
post.setContent("c");
post.setCreatedAt(LocalDateTime.now());
post.setAuthor(user);
post.setCategory(cat);
post.setTags(java.util.Set.of(tag));
com.openisle.model.Comment comment = new com.openisle.model.Comment();
comment.setId(2L);
comment.setContent("hi");
comment.setCreatedAt(LocalDateTime.now());
comment.setAuthor(user);
comment.setPost(post);
com.openisle.model.Comment reply = new com.openisle.model.Comment();
reply.setId(3L);
reply.setContent("reply");
reply.setCreatedAt(LocalDateTime.now());
reply.setAuthor(user);
reply.setPost(post);
com.openisle.model.Reaction pr = new com.openisle.model.Reaction();
pr.setId(10L);
pr.setUser(user);
pr.setPost(post);
pr.setType(com.openisle.model.ReactionType.LIKE);
com.openisle.model.Reaction cr = new com.openisle.model.Reaction();
cr.setId(11L);
cr.setUser(user);
cr.setComment(comment);
cr.setType(com.openisle.model.ReactionType.LIKE);
Mockito.when(postService.viewPost(eq(1L), Mockito.isNull())).thenReturn(post);
Mockito.when(commentService.getCommentsForPost(eq(1L), any())).thenReturn(List.of(comment));
Mockito.when(commentService.getReplies(2L)).thenReturn(List.of(reply));
Mockito.when(commentService.getReplies(3L)).thenReturn(List.of());
Mockito.when(commentService.getParticipants(Mockito.anyLong(), Mockito.anyInt())).thenReturn(java.util.List.of());
Mockito.when(reactionService.getReactionsForPost(1L)).thenReturn(List.of(pr));
Mockito.when(reactionService.getReactionsForComment(2L)).thenReturn(List.of(cr));
Mockito.when(reactionService.getReactionsForComment(3L)).thenReturn(List.of());
mockMvc.perform(get("/api/posts/1"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.reactions[0].id").value(10))
.andExpect(jsonPath("$.comments[0].replies[0].id").value(3))
.andExpect(jsonPath("$.comments[0].reactions[0].id").value(11));
}
@Test
void listPostsByCategoriesAndTags() throws Exception {
User user = new User();
user.setUsername("alice");
Category cat = new Category();
cat.setName("tech");
Tag tag = new Tag();
tag.setId(1L);
tag.setName("java");
Post post = new Post();
post.setId(2L);
post.setTitle("hello");
post.setCreatedAt(LocalDateTime.now());
post.setAuthor(user);
post.setCategory(cat);
post.setTags(java.util.Set.of(tag));
Mockito.when(commentService.getParticipants(Mockito.anyLong(), Mockito.anyInt())).thenReturn(java.util.List.of());
Mockito.when(postService.listPostsByCategoriesAndTags(eq(java.util.List.of(1L)), eq(java.util.List.of(1L, 2L)), eq(0), eq(5)))
.thenReturn(List.of(post));
mockMvc.perform(get("/api/posts")
.param("tagIds", "1,2")
.param("page", "0")
.param("pageSize", "5")
.param("categoryId", "1"))
.andExpect(status().isOk())
.andExpect(jsonPath("$[0].id").value(2));
verify(postService).listPostsByCategoriesAndTags(eq(java.util.List.of(1L)), eq(java.util.List.of(1L, 2L)), eq(0), eq(5));
verify(postService, never()).listPostsByCategories(any(), any(), any());
}
@Test
void rankingPostsFiltered() throws Exception {
User user = new User();
user.setUsername("alice");
Category cat = new Category();
cat.setName("tech");
Tag tag = new Tag();
tag.setId(1L);
tag.setName("java");
Post post = new Post();
post.setId(3L);
post.setTitle("rank");
post.setCreatedAt(LocalDateTime.now());
post.setAuthor(user);
post.setCategory(cat);
post.setTags(java.util.Set.of(tag));
Mockito.when(commentService.getParticipants(Mockito.anyLong(), Mockito.anyInt())).thenReturn(java.util.List.of());
Mockito.when(postService.listPostsByViews(eq(java.util.List.of(1L)), eq(java.util.List.of(1L, 2L)), eq(0), eq(5)))
.thenReturn(List.of(post));
mockMvc.perform(get("/api/posts/ranking")
.param("tagIds", "1,2")
.param("page", "0")
.param("pageSize", "5")
.param("categoryId", "1"))
.andExpect(status().isOk())
.andExpect(jsonPath("$[0].id").value(3));
verify(postService).listPostsByViews(eq(java.util.List.of(1L)), eq(java.util.List.of(1L, 2L)), eq(0), eq(5));
}
}

View File

@@ -0,0 +1,36 @@
package com.openisle.controller;
import com.openisle.service.PushSubscriptionService;
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 static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@WebMvcTest(PushSubscriptionController.class)
@AutoConfigureMockMvc(addFilters = false)
class PushSubscriptionControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private PushSubscriptionService pushSubscriptionService;
@Test
void subscribeEndpoint() throws Exception {
mockMvc.perform(post("/api/push/subscribe")
.contentType(MediaType.APPLICATION_JSON)
.content("{\"endpoint\":\"e\",\"p256dh\":\"p\",\"auth\":\"a\"}")
.principal(new UsernamePasswordAuthenticationToken("u","p")))
.andExpect(status().isOk());
Mockito.verify(pushSubscriptionService).saveSubscription("u","e","p","a");
}
}

View File

@@ -0,0 +1,84 @@
package com.openisle.controller;
import com.openisle.model.Comment;
import com.openisle.model.Post;
import com.openisle.model.Reaction;
import com.openisle.model.ReactionType;
import com.openisle.model.User;
import com.openisle.service.ReactionService;
import com.openisle.service.LevelService;
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.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.test.web.servlet.MockMvc;
import static org.mockito.ArgumentMatchers.eq;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@WebMvcTest(ReactionController.class)
@AutoConfigureMockMvc(addFilters = false)
class ReactionControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private ReactionService reactionService;
@MockBean
private LevelService levelService;
@Test
void reactToPost() throws Exception {
User user = new User();
user.setUsername("u1");
Post post = new Post();
post.setId(1L);
Reaction reaction = new Reaction();
reaction.setId(1L);
reaction.setUser(user);
reaction.setPost(post);
reaction.setType(ReactionType.LIKE);
Mockito.when(reactionService.reactToPost(eq("u1"), eq(1L), eq(ReactionType.LIKE))).thenReturn(reaction);
mockMvc.perform(post("/api/posts/1/reactions")
.contentType("application/json")
.content("{\"type\":\"LIKE\"}")
.principal(new UsernamePasswordAuthenticationToken("u1", "p")))
.andExpect(status().isOk())
.andExpect(jsonPath("$.postId").value(1));
}
@Test
void reactToComment() throws Exception {
User user = new User();
user.setUsername("u2");
Comment comment = new Comment();
comment.setId(2L);
Reaction reaction = new Reaction();
reaction.setId(2L);
reaction.setUser(user);
reaction.setComment(comment);
reaction.setType(ReactionType.RECOMMEND);
Mockito.when(reactionService.reactToComment(eq("u2"), eq(2L), eq(ReactionType.RECOMMEND))).thenReturn(reaction);
mockMvc.perform(post("/api/comments/2/reactions")
.contentType("application/json")
.content("{\"type\":\"RECOMMEND\"}")
.principal(new UsernamePasswordAuthenticationToken("u2", "p")))
.andExpect(status().isOk())
.andExpect(jsonPath("$.commentId").value(2));
}
@Test
void listReactionTypes() throws Exception {
mockMvc.perform(get("/api/reaction-types"))
.andExpect(status().isOk())
.andExpect(jsonPath("$[0]").value("LIKE"));
}
}

View File

@@ -0,0 +1,90 @@
package com.openisle.controller;
import com.openisle.model.Comment;
import com.openisle.model.Post;
import com.openisle.model.User;
import com.openisle.model.PostStatus;
import com.openisle.service.SearchService;
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.test.web.servlet.MockMvc;
import java.util.List;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
@WebMvcTest(SearchController.class)
@AutoConfigureMockMvc(addFilters = false)
class SearchControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private SearchService searchService;
@Test
void userSearchEndpoint() throws Exception {
User user = new User();
user.setId(1L);
user.setUsername("alice");
Mockito.when(searchService.searchUsers("ali")).thenReturn(List.of(user));
mockMvc.perform(get("/api/search/users").param("keyword", "ali"))
.andExpect(status().isOk())
.andExpect(jsonPath("$[0].username").value("alice"));
}
@Test
void globalSearchAggregatesTypes() throws Exception {
User u = new User();
u.setId(1L);
u.setUsername("bob");
Post p = new Post();
p.setId(2L);
p.setTitle("hello");
p.setStatus(PostStatus.PUBLISHED);
Comment c = new Comment();
c.setId(3L);
c.setContent("nice");
Mockito.when(searchService.globalSearch("n")).thenReturn(List.of(
new SearchService.SearchResult("user", 1L, "bob", null, null, null),
new SearchService.SearchResult("post", 2L, "hello", null, null, null),
new SearchService.SearchResult("comment", 3L, "nice", null, null, null)
));
mockMvc.perform(get("/api/search/global").param("keyword", "n"))
.andExpect(status().isOk())
.andExpect(jsonPath("$[0].type").value("user"))
.andExpect(jsonPath("$[1].type").value("post"))
.andExpect(jsonPath("$[2].type").value("comment"));
}
@Test
void searchPostsByTitle() throws Exception {
Post p = new Post();
p.setId(2L);
p.setTitle("spring");
Mockito.when(searchService.searchPostsByTitle("spr")).thenReturn(List.of(p));
mockMvc.perform(get("/api/search/posts/title").param("keyword", "spr"))
.andExpect(status().isOk())
.andExpect(jsonPath("$[0].title").value("spring"));
}
@Test
void searchPosts() throws Exception {
Post p = new Post();
p.setId(5L);
p.setTitle("hello");
Mockito.when(searchService.searchPosts("he")).thenReturn(List.of(p));
mockMvc.perform(get("/api/search/posts").param("keyword", "he"))
.andExpect(status().isOk())
.andExpect(jsonPath("$[0].id").value(5));
}
}

View File

@@ -0,0 +1,74 @@
package com.openisle.controller;
import com.openisle.config.CustomAccessDeniedHandler;
import com.openisle.config.SecurityConfig;
import com.openisle.service.JwtService;
import com.openisle.repository.UserRepository;
import com.openisle.service.UserVisitService;
import com.openisle.model.Role;
import com.openisle.model.User;
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.context.annotation.Import;
import org.springframework.test.web.servlet.MockMvc;
import java.util.Optional;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@WebMvcTest(StatController.class)
@AutoConfigureMockMvc
@Import({SecurityConfig.class, CustomAccessDeniedHandler.class})
class StatControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private JwtService jwtService;
@MockBean
private UserRepository userRepository;
@MockBean
private UserVisitService userVisitService;
@Test
void dauReturnsCount() throws Exception {
Mockito.when(jwtService.validateAndGetSubject("token")).thenReturn("user");
User user = new User();
user.setUsername("user");
user.setPassword("p");
user.setEmail("u@example.com");
user.setRole(Role.USER);
Mockito.when(userRepository.findByUsername("user")).thenReturn(Optional.of(user));
Mockito.when(userVisitService.countDau(Mockito.any())).thenReturn(3L);
mockMvc.perform(get("/api/stats/dau").header("Authorization", "Bearer token"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.dau").value(3));
}
@Test
void dauRangeReturnsSeries() throws Exception {
Mockito.when(jwtService.validateAndGetSubject("token")).thenReturn("user");
User user = new User();
user.setUsername("user");
user.setPassword("p");
user.setEmail("u@example.com");
user.setRole(Role.USER);
Mockito.when(userRepository.findByUsername("user")).thenReturn(Optional.of(user));
java.util.Map<java.time.LocalDate, Long> map = new java.util.LinkedHashMap<>();
map.put(java.time.LocalDate.now().minusDays(1), 1L);
map.put(java.time.LocalDate.now(), 2L);
Mockito.when(userVisitService.countDauRange(Mockito.any(), Mockito.any())).thenReturn(map);
mockMvc.perform(get("/api/stats/dau-range").param("days", "2").header("Authorization", "Bearer token"))
.andExpect(status().isOk())
.andExpect(jsonPath("$[0].value").value(1))
.andExpect(jsonPath("$[1].value").value(2));
}
}

View File

@@ -0,0 +1,98 @@
package com.openisle.controller;
import com.openisle.model.Tag;
import com.openisle.service.TagService;
import com.openisle.service.PostService;
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.test.web.servlet.MockMvc;
import java.util.List;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.isNull;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
@WebMvcTest(TagController.class)
@AutoConfigureMockMvc(addFilters = false)
class TagControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private TagService tagService;
@MockBean
private PostService postService;
@Test
void createAndGetTag() throws Exception {
Tag t = new Tag();
t.setId(1L);
t.setName("java");
t.setDescription("d");
t.setIcon("i");
t.setSmallIcon("s1");
Mockito.when(tagService.createTag(eq("java"), eq("d"), eq("i"), eq("s1"), eq(true), isNull())).thenReturn(t);
Mockito.when(tagService.getTag(1L)).thenReturn(t);
mockMvc.perform(post("/api/tags")
.contentType(MediaType.APPLICATION_JSON)
.content("{\"name\":\"java\",\"description\":\"d\",\"icon\":\"i\",\"smallIcon\":\"s1\"}"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.name").value("java"))
.andExpect(jsonPath("$.description").value("d"))
.andExpect(jsonPath("$.icon").value("i"))
.andExpect(jsonPath("$.smallIcon").value("s1"));
mockMvc.perform(get("/api/tags/1"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.id").value(1));
}
@Test
void listTags() throws Exception {
Tag t = new Tag();
t.setId(2L);
t.setName("spring");
t.setDescription("d2");
t.setIcon("i2");
t.setSmallIcon("s2");
Mockito.when(tagService.listTags()).thenReturn(List.of(t));
mockMvc.perform(get("/api/tags"))
.andExpect(status().isOk())
.andExpect(status().isOk())
.andExpect(jsonPath("$[0].name").value("spring"))
.andExpect(jsonPath("$[0].description").value("d2"))
.andExpect(jsonPath("$[0].icon").value("i2"))
.andExpect(jsonPath("$[0].smallIcon").value("s2"));
}
@Test
void updateTag() throws Exception {
Tag t = new Tag();
t.setId(3L);
t.setName("java");
t.setDescription("d3");
t.setIcon("i3");
t.setSmallIcon("s3");
Mockito.when(tagService.updateTag(eq(3L), eq("java"), eq("d3"), eq("i3"), eq("s3"))).thenReturn(t);
mockMvc.perform(put("/api/tags/3")
.contentType(MediaType.APPLICATION_JSON)
.content("{\"name\":\"java\",\"description\":\"d3\",\"icon\":\"i3\",\"smallIcon\":\"s3\"}"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.id").value(3))
.andExpect(jsonPath("$.name").value("java"))
.andExpect(jsonPath("$.description").value("d3"))
.andExpect(jsonPath("$.icon").value("i3"))
.andExpect(jsonPath("$.smallIcon").value("s3"));
}
}

View File

@@ -0,0 +1,168 @@
package com.openisle.controller;
import com.openisle.model.User;
import com.openisle.service.*;
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.mock.web.MockMultipartFile;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.test.web.servlet.MockMvc;
import java.util.Optional;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
@WebMvcTest(UserController.class)
@AutoConfigureMockMvc(addFilters = false)
class UserControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private ReactionService reactionService;
@MockBean
private SubscriptionService subscriptionService;
@MockBean
private UserService userService;
@MockBean
private ImageUploader imageUploader;
@MockBean
private PostService postService;
@MockBean
private CommentService commentService;
@MockBean
private LevelService levelService;
@Test
void getCurrentUser() throws Exception {
User u = new User();
u.setId(1L);
u.setUsername("alice");
u.setEmail("a@b.com");
u.setAvatar("http://x/avatar.png");
Mockito.when(userService.findByUsername("alice")).thenReturn(Optional.of(u));
mockMvc.perform(get("/api/users/me").principal(new UsernamePasswordAuthenticationToken("alice","p")))
.andExpect(status().isOk())
.andExpect(jsonPath("$.avatar").value("http://x/avatar.png"));
}
@Test
void uploadAvatar() throws Exception {
MockMultipartFile file = new MockMultipartFile("file", "a.png", MediaType.IMAGE_PNG_VALUE, "img".getBytes());
Mockito.when(imageUploader.upload(any(), eq("a.png"))).thenReturn(java.util.concurrent.CompletableFuture.completedFuture("http://img/a.png"));
mockMvc.perform(multipart("/api/users/me/avatar").file(file).principal(new UsernamePasswordAuthenticationToken("alice","p")))
.andExpect(status().isOk())
.andExpect(jsonPath("$.url").value("http://img/a.png"));
Mockito.verify(userService).updateAvatar("alice", "http://img/a.png");
}
@Test
void uploadAvatarRejectsNonImage() throws Exception {
MockMultipartFile file = new MockMultipartFile("file", "a.txt", MediaType.TEXT_PLAIN_VALUE, "text".getBytes());
mockMvc.perform(multipart("/api/users/me/avatar").file(file).principal(new UsernamePasswordAuthenticationToken("alice","p")))
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.error").value("File is not an image"));
Mockito.verify(imageUploader, Mockito.never()).upload(any(), any());
}
@Test
void getUserByName() throws Exception {
User u = new User();
u.setId(2L);
u.setUsername("bob");
Mockito.when(userService.findByIdentifier("bob")).thenReturn(Optional.of(u));
mockMvc.perform(get("/api/users/bob"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.id").value(2));
}
@Test
void listUserPosts() throws Exception {
User user = new User();
user.setUsername("bob");
com.openisle.model.Category cat = new com.openisle.model.Category();
cat.setName("tech");
com.openisle.model.Post post = new com.openisle.model.Post();
post.setId(3L);
post.setTitle("hello");
post.setCreatedAt(java.time.LocalDateTime.now());
post.setCategory(cat);
post.setAuthor(user);
Mockito.when(userService.findByIdentifier("bob")).thenReturn(Optional.of(user));
Mockito.when(postService.getRecentPostsByUser("bob", 10)).thenReturn(java.util.List.of(post));
mockMvc.perform(get("/api/users/bob/posts"))
.andExpect(status().isOk())
.andExpect(jsonPath("$[0].title").value("hello"));
}
@Test
void listUserReplies() throws Exception {
User user = new User();
user.setUsername("bob");
com.openisle.model.Post post = new com.openisle.model.Post();
post.setId(5L);
com.openisle.model.Category cat = new com.openisle.model.Category();
cat.setName("tech");
post.setCategory(cat);
com.openisle.model.Comment comment = new com.openisle.model.Comment();
comment.setId(4L);
comment.setContent("hi");
comment.setCreatedAt(java.time.LocalDateTime.now());
comment.setAuthor(user);
comment.setPost(post);
Mockito.when(userService.findByIdentifier("bob")).thenReturn(Optional.of(user));
Mockito.when(commentService.getRecentCommentsByUser("bob", 50)).thenReturn(java.util.List.of(comment));
mockMvc.perform(get("/api/users/bob/replies"))
.andExpect(status().isOk())
.andExpect(jsonPath("$[0].id").value(4));
}
@Test
void aggregateUserData() throws Exception {
User user = new User();
user.setId(2L);
user.setUsername("bob");
user.setEmail("b@e.com");
com.openisle.model.Category cat = new com.openisle.model.Category();
cat.setName("tech");
com.openisle.model.Post post = new com.openisle.model.Post();
post.setId(3L);
post.setTitle("hello");
post.setCreatedAt(java.time.LocalDateTime.now());
post.setCategory(cat);
post.setAuthor(user);
com.openisle.model.Comment comment = new com.openisle.model.Comment();
comment.setId(4L);
comment.setContent("hi");
comment.setCreatedAt(java.time.LocalDateTime.now());
comment.setAuthor(user);
comment.setPost(post);
Mockito.when(userService.findByIdentifier("bob")).thenReturn(Optional.of(user));
Mockito.when(postService.getRecentPostsByUser("bob", 10)).thenReturn(java.util.List.of(post));
Mockito.when(commentService.getRecentCommentsByUser("bob", 50)).thenReturn(java.util.List.of(comment));
mockMvc.perform(get("/api/users/bob/all"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.user.id").value(2))
.andExpect(jsonPath("$.posts[0].id").value(3))
.andExpect(jsonPath("$.replies[0].id").value(4));
}
}