blob: da9bf5717bf5fe8d9d5126ca25056bc64c454f16 [file] [log] [blame]
Seamher97b11562025-06-07 13:34:31 +08001package com.g9.g9backend.controller;
2
3import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
4import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
5import com.fasterxml.jackson.databind.ObjectMapper;
6import com.g9.g9backend.pojo.Community;
7import com.g9.g9backend.pojo.Subscription;
8import com.g9.g9backend.pojo.Thread;
9import com.g9.g9backend.service.CommunityService;
10import com.g9.g9backend.service.SubscriptionService;
11import com.g9.g9backend.service.ThreadLikeService;
12import com.g9.g9backend.service.ThreadService;
13import org.jetbrains.annotations.NotNull;
14import org.junit.jupiter.api.BeforeEach;
15import org.junit.jupiter.api.Test;
16import org.junit.jupiter.api.extension.ExtendWith;
17import org.mockito.InjectMocks;
18import org.mockito.Mock;
19import org.mockito.junit.jupiter.MockitoExtension;
20import org.springframework.http.MediaType;
21import org.springframework.test.web.servlet.MockMvc;
22import org.springframework.test.web.servlet.setup.MockMvcBuilders;
23
24import java.util.ArrayList;
25import java.util.Date;
26import java.util.List;
27
28import static org.mockito.ArgumentMatchers.any;
29import static org.mockito.Mockito.when;
30import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
31import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
32
33@ExtendWith(MockitoExtension.class)
34public class CommunityControllerTest {
35
36 private MockMvc mockMvc;
37
38 @InjectMocks
39 private CommunityController communityController;
40
41 @Mock
42 private CommunityService communityService;
43
44 @Mock
45 private ThreadService threadService;
46
47 @Mock
48 private SubscriptionService subscriptionService;
49
50 @Mock
51 private ThreadLikeService threadLikeService;
52
53 private final ObjectMapper objectMapper = new ObjectMapper();
54
55 @BeforeEach
56 public void setup() {
57 mockMvc = MockMvcBuilders.standaloneSetup(communityController).build();
58 }
59
60 @Test
61 public void shouldReturnHotCommunities_whenCallingHotEndpoint() throws Exception {
62 Community c1 = new Community(1, "社区A", "pic", "desc", 9.0f, "type", 5, 1);
63 Page<Community> page = createCommunityPage(List.of(c1));
64 when(communityService.page(any(), any())).thenReturn(page);
65
66 mockMvc.perform(get("/community/hot"))
67 .andExpect(status().isOk())
68 .andExpect(jsonPath("$.communityList[0].communityId").value(1));
69 }
70
71 @Test
72 public void shouldReturnCommonCommunities_whenCallingCommonEndpoint() throws Exception {
73 Community c1 = new Community(2, "社区B", "pic", "desc", 7.0f, "type", 3, 1);
74 Page<Community> page = createCommunityPage(List.of(c1));
75 when(communityService.page(any(), any())).thenReturn(page);
76
77 mockMvc.perform(get("/community/common"))
78 .andExpect(status().isOk())
79 .andExpect(jsonPath("$.communityList[0].communityId").value(2));
80 }
81
82 @Test
83 public void shouldReturnCommunityInfo_whenIdIsValid() throws Exception {
84 Community community = new Community(3, "社区C", "pic", "desc", 6.0f, "type", 4, 2);
85 when(communityService.getById(3)).thenReturn(community);
86
87 mockMvc.perform(get("/community/info").param("communityId", "3"))
88 .andExpect(status().isOk())
89 .andExpect(jsonPath("$.communityId").value(3));
90 }
91
92 @Test
93 public void shouldReturnThreadDetails_whenThreadIdAndUserIdAreValid() throws Exception {
94 Thread thread = mockThread(1, 10);
95 when(threadService.getById(1)).thenReturn(thread);
96 when(threadLikeService.getOne(any())).thenReturn(null);
97
98 mockMvc.perform(get("/thread")
99 .param("threadId", "1")
100 .param("userId", "10"))
101 .andExpect(status().isOk())
102 .andExpect(jsonPath("$.threadId").value(1))
103 .andExpect(jsonPath("$.userId").value(10));
104 }
105
106 @Test
107 public void shouldPostThread_whenValidRequest() throws Exception {
108 Thread thread = new Thread();
109 thread.setThreadId(100);
110 thread.setCommunityId(1);
111
112 Community community = new Community();
113 community.setCommunityId(1);
114 community.setThreadNumber(5);
115
116 when(communityService.getById(1)).thenReturn(community);
117
118 mockMvc.perform(post("/thread")
119 .contentType(MediaType.APPLICATION_JSON)
120 .content(objectMapper.writeValueAsString(thread)))
121 .andExpect(status().isOk());
122 }
123
124 @Test
125 public void shouldLikeThread_whenValidInput() throws Exception {
126 Thread thread = new Thread();
127 thread.setThreadId(100);
128 thread.setLikes(5);
129
130 when(threadService.getById(100)).thenReturn(thread);
131
132 mockMvc.perform(post("/thread/like")
133 .contentType(MediaType.APPLICATION_JSON)
134 .content("{\"userId\":1, \"threadId\":100}"))
135 .andExpect(status().isOk());
136 }
137
138 @Test
139 public void shouldDeleteThread_whenThreadExists() throws Exception {
140 Thread thread = new Thread();
141 thread.setThreadId(100);
142 thread.setCommunityId(1);
143
144 Community community = new Community();
145 community.setCommunityId(1);
146 community.setThreadNumber(5);
147
148 when(threadService.getById(100)).thenReturn(thread);
149 when(communityService.getById(1)).thenReturn(community);
150
151 mockMvc.perform(delete("/thread").param("threadId", "100"))
152 .andExpect(status().isNoContent());
153 }
154
155 @Test
156 public void shouldCancelLike_whenValidRequest() throws Exception {
157 Thread thread = new Thread();
158 thread.setThreadId(100);
159 thread.setLikes(5);
160
161 when(threadService.getById(100)).thenReturn(thread);
162
163 mockMvc.perform(delete("/thread/like")
164 .param("userId", "1")
165 .param("threadId", "100"))
166 .andExpect(status().isNoContent());
167 }
168
169 @Test
170 public void shouldReturnCommunities_whenSearchingByType() throws Exception {
171 Community c = new Community(1, "搜索社区", "pic", "desc", 9.5f, "test", 8, 5);
172 Page<Community> page = createCommunityPage(List.of(c));
173
174 when(communityService.page(any(), any())).thenReturn(page);
175
176 mockMvc.perform(get("/community")
177 .param("searchValue", "搜索")
178 .param("type", "test")
179 .param("pageNumber", "1")
180 .param("rows", "10"))
181 .andExpect(status().isOk())
182 .andExpect(jsonPath("$.records[0].communityId").value(1));
183 }
184
185 @Test
186 public void shouldReturnHotThreads_whenOptionIsHot() throws Exception {
187 Thread t = new Thread();
188 t.setThreadId(1);
189 t.setUserId(2);
190 t.setTitle("热帖");
191 t.setLikes(10);
192 Page<Thread> page = new Page<>(1, 10);
193 page.setRecords(List.of(t));
194 page.setTotal(1L);
195
196 when(threadService.page(any(), any())).thenReturn(page);
197
198 mockMvc.perform(get("/community/threads")
199 .param("communityId", "1")
200 .param("pageNumber", "1")
201 .param("rows", "10")
202 .param("option", "最高热度")
203 .param("searchValue", "")
204 .param("userId", "2"))
205 .andExpect(status().isOk())
206 .andExpect(jsonPath("$.records[0].threadId").value(1));
207 }
208
209 @Test
210 public void shouldReturnFollowedThreads_whenOptionIsFollowed() throws Exception {
211 Subscription s = new Subscription();
212 s.setUserId(1);
213 s.setFollowerId(2);
214
215 List<Subscription> subscriptions = new ArrayList<>();
216 subscriptions.add(s);
217
218 @SuppressWarnings("unchecked")
219 LambdaQueryWrapper<Subscription> wrapper = any(LambdaQueryWrapper.class);
220 when(subscriptionService.list(wrapper)).thenReturn(subscriptions);
221
222 Thread t = new Thread();
223 t.setThreadId(2);
224 t.setUserId(2);
225 t.setTitle("关注者帖子");
226 t.setLikes(8);
227
228 Page<Thread> page = new Page<>(1, 10);
229 page.setRecords(List.of(t));
230 page.setTotal(1L);
231
232 when(threadService.page(any(), any())).thenReturn(page);
233
234 mockMvc.perform(get("/community/threads")
235 .param("communityId", "1")
236 .param("pageNumber", "1")
237 .param("rows", "10")
238 .param("option", "关注者")
239 .param("searchValue", "")
240 .param("userId", "1"))
241 .andExpect(status().isOk())
242 .andExpect(jsonPath("$.records[0].threadId").value(2));
243 }
244
245 @NotNull
246 private Page<Community> createCommunityPage(List<Community> communities) {
247 Page<Community> page = new Page<>(1, 3);
248 page.setRecords(communities);
249 page.setTotal(communities.size());
250 return page;
251 }
252
253 @NotNull
254 @SuppressWarnings("SameParameterValue")
255 private Thread mockThread(int threadId, int userId) {
256 Thread thread = new Thread();
257 thread.setThreadId(threadId);
258 thread.setUserId(userId);
259 thread.setThreadPicture("pic");
260 thread.setTitle("title");
261 thread.setContent("content");
262 thread.setLikes(5);
263 thread.setCreateAt(new Date());
264 thread.setCommentNumber(2);
265 thread.setCommunityId(99);
266 return thread;
267 }
268}