user module API & resource module API
GET /user/thread
POST /resource/version
GET /resource/search
GET /resource/info
GET /resource/hot
GET /resource/hot/slide
GET /resource/hot-trend
PUT /resource/info
Change-Id: I39c94b06a1d69967b8e235a67a0133c8095abb38
diff --git a/src/test/java/com/g9/g9backend/controller/ResourceControllerTest.java b/src/test/java/com/g9/g9backend/controller/ResourceControllerTest.java
index 9d7c908..61b73d8 100644
--- a/src/test/java/com/g9/g9backend/controller/ResourceControllerTest.java
+++ b/src/test/java/com/g9/g9backend/controller/ResourceControllerTest.java
@@ -1,5 +1,7 @@
package com.g9.g9backend.controller;
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.g9.g9backend.pojo.*;
import com.g9.g9backend.pojo.DTO.*;
@@ -13,7 +15,9 @@
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
+import java.util.ArrayList;
import java.util.Date;
+import java.util.List;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.when;
@@ -66,6 +70,21 @@
@Mock
private NotificationService notificationService;
+ @Mock
+ private ResourceVersionService resourceVersionService;
+
+ @Mock
+ private SearchHistoryService searchHistoryService;
+
+ @Mock
+ private GameVersionService gameVersionService;
+
+ @Mock
+ private TorrentRecordService torrentRecordService;
+
+ @Mock
+ private HotTrendService hotTrendService;
+
// ObjectMapper对象用于将 Java 对象和 JSON 字符串互相转换 ,因为在测试中,需要把请求参数或返回响应转换成 JSON 字符串形式来传输
private final ObjectMapper objectMapper = new ObjectMapper();
@@ -115,6 +134,23 @@
.andExpect(status().isOk()); // 期望接口返回 HTTP 状态码 200(成功)
}
+ // 上传版本
+ @Test
+ public void testUploadResourceVersion() throws Exception {
+
+ ResourceVersion resourceVersion = new ResourceVersion();
+ resourceVersion.setResourceId(1);
+ resourceVersion.setResourceVersionName("Test ResourceVersion");
+
+ when(resourceVersionService.save(any())).thenReturn(true);
+
+ // 模拟 HTTP 请求
+ mockMvc.perform(post("/resource/version")
+ .contentType(MediaType.APPLICATION_JSON)
+ .content(objectMapper.writeValueAsString(resourceVersion)))
+ .andExpect(status().isOk());
+ }
+
// 购买资源
@Test
public void testPurchaseResource_success() throws Exception {
@@ -253,4 +289,136 @@
.param("resourceId", "1"))
.andExpect(status().isNoContent());
}
+
+ // 搜索资源
+ @Test
+ public void testGetResourceSearch() throws Exception {
+
+ when(searchHistoryService.save(any())).thenReturn(true);
+ when(resourceService.page(any(), any())).thenReturn(new Page<>(1, 1));
+
+ List<Gameplay> gameplayList = new ArrayList<>();
+ QueryWrapper<Gameplay> gameplayQuery = new QueryWrapper<>();
+ gameplayQuery.eq("resource_id", 1);
+ when(gameplayService.list(gameplayQuery)).thenReturn(gameplayList);
+
+ mockMvc.perform(get("/resource/search")
+ .param("userId", "1")
+ .param("searchValue", "")
+ .param("classify", "")
+ .param("gameplayList", "")
+ .param("gameVersionList", "")
+ .param("pageNumber", "1")
+ .param("rows", "1"))
+ .andExpect(status().isOk());
+ }
+
+ // 获取资源信息
+ @Test
+ public void testGetResourceInfo() throws Exception {
+
+ when(resourceService.getById(any())).thenReturn(new Resource());
+ when(gameplayService.list(new QueryWrapper<Gameplay>().eq("resource_id", 1))).thenReturn(new ArrayList<>());
+ when(resourceVersionService.list(new QueryWrapper<ResourceVersion>().eq("resource_id", 1))).thenReturn(new ArrayList<>());
+ when(gameVersionService.list(new QueryWrapper<GameVersion>().eq("resource_version_id", 1))).thenReturn(new ArrayList<>());
+ when(torrentRecordService.list(new QueryWrapper<TorrentRecord>().eq("resource_version_id", 1))).thenReturn(new ArrayList<>());
+ when(userCollectionService.getOne(any())).thenReturn(new UserCollection());
+ when(userLikeService.getOne(any())).thenReturn(new UserLike());
+ when(userPurchaseService.getOne(any())).thenReturn(new UserPurchase());
+ when(userUploadService.getOne(any())).thenReturn(new UserUpload());
+
+ mockMvc.perform(get("/resource/info")
+ .param("resourceId", "1")
+ .param("userId", "1"))
+ .andExpect(status().isOk());
+ }
+
+ // 获取热门资源
+ @Test
+ public void testGetHotResource() throws Exception {
+
+ when(resourceService.page(any(), any())).thenReturn(new Page<>(1, 1));
+
+ List<Gameplay> gameplayList = new ArrayList<>();
+ QueryWrapper<Gameplay> gameplayQuery = new QueryWrapper<>();
+ gameplayQuery.eq("resource_id", 1);
+ when(gameplayService.list(gameplayQuery)).thenReturn(gameplayList);
+
+ mockMvc.perform(get("/resource/hot")
+ .param("classify", "map")
+ .param("pageNumber", "1")
+ .param("rows", "1"))
+ .andExpect(status().isOk());
+ }
+
+ // 获取热门资源幻灯片数据
+ @Test
+ public void testGetHotSlideResource() throws Exception {
+
+ List<Resource> resourceList = new ArrayList<>();
+ QueryWrapper<Resource> resourceQuery = new QueryWrapper<>();
+ resourceQuery.orderByDesc("(downloads * 0.25 + likes * 0.25 + collections * 0.25 + comments * 0.25)");
+ resourceQuery.last("LIMIT 3");
+ when(resourceService.list(resourceQuery)).thenReturn(resourceList);
+
+ mockMvc.perform(get("/resource/hot/slide"))
+ .andExpect(status().isOk());
+ }
+
+ // 获取热门资源趋势图
+ @Test
+ public void testGetResourceHotTrend() throws Exception {
+
+ List<HotTrend> hotTrendList = new ArrayList<>();
+ when(hotTrendService.list()).thenReturn(hotTrendList);
+
+ mockMvc.perform(get("/resource/hot-trend"))
+ .andExpect(status().isOk());
+ }
+
+ // 修改资源信息
+ @Test
+ public void testPutResourceInfo_success() throws Exception {
+
+ PutResourceInfoDTO putResourceInfoDTO = new PutResourceInfoDTO();
+ putResourceInfoDTO.setResourceId(1);
+ putResourceInfoDTO.setResourceName("test");
+ putResourceInfoDTO.setResourcePicture("test");
+ putResourceInfoDTO.setResourceSummary("test");
+ putResourceInfoDTO.setResourceDetail("test");
+ putResourceInfoDTO.setPrice(100);
+ String[] gameplayList = {"test1", "test2"};
+ putResourceInfoDTO.setGameplayList(gameplayList);
+
+ when(resourceService.getOne(any())).thenReturn(null);
+ when(resourceService.update(any())).thenReturn(true);
+ when(gameplayService.remove(any())).thenReturn(true);
+ when(gameplayService.save(any())).thenReturn(true);
+
+ mockMvc.perform(put("/resource/info")
+ .contentType(MediaType.APPLICATION_JSON)
+ .content(objectMapper.writeValueAsString(putResourceInfoDTO)))
+ .andExpect(status().isOk());
+ }
+
+ @Test
+ public void testPutResourceInfo__resourceNameExists() throws Exception {
+
+ PutResourceInfoDTO putResourceInfoDTO = new PutResourceInfoDTO();
+ putResourceInfoDTO.setResourceId(1);
+ putResourceInfoDTO.setResourceName("test");
+ putResourceInfoDTO.setResourcePicture("test");
+ putResourceInfoDTO.setResourceSummary("test");
+ putResourceInfoDTO.setResourceDetail("test");
+ putResourceInfoDTO.setPrice(100);
+ String[] gameplayList = {"test1", "test2"};
+ putResourceInfoDTO.setGameplayList(gameplayList);
+
+ when(resourceService.getOne(any())).thenReturn(new Resource());
+
+ mockMvc.perform(put("/resource/info")
+ .contentType(MediaType.APPLICATION_JSON)
+ .content(objectMapper.writeValueAsString(putResourceInfoDTO)))
+ .andExpect(status().is(411));
+ }
}
\ No newline at end of file
diff --git a/src/test/java/com/g9/g9backend/controller/UserControllerTest.java b/src/test/java/com/g9/g9backend/controller/UserControllerTest.java
index d390dea..76db7ee 100644
--- a/src/test/java/com/g9/g9backend/controller/UserControllerTest.java
+++ b/src/test/java/com/g9/g9backend/controller/UserControllerTest.java
@@ -58,6 +58,9 @@
@Mock
private ResourceService resourceService;
+ @Mock
+ private ThreadService threadService;
+
private final ObjectMapper objectMapper = new ObjectMapper();
@BeforeEach
@@ -303,7 +306,7 @@
public void testGetUserCollection() throws Exception {
when(userCollectionService.page(any(), any())).thenReturn(new Page<>(1, 2));
- when(resourceService.getById(anyInt())).thenReturn(new Resource(1, "a", null, null, null, new Date(), new Date(), 100, 0, 0, 0, 0, 0, "mod"));
+ when(resourceService.getById(anyInt())).thenReturn(new Resource(1, "a", null, null, null, new Date(), new Date(), 100, 0, 0, 0, 0, "mod"));
mockMvc.perform(get("/user/collection")
.param("userId", "1")
@@ -317,7 +320,7 @@
public void testGetUserUpload() throws Exception {
when(userUploadService.page(any(), any())).thenReturn(new Page<>(1, 2));
- when(resourceService.getById(anyInt())).thenReturn(new Resource(1, "a", null, null, null, new Date(), new Date(), 100, 0, 0, 0, 0, 0, "mod"));
+ when(resourceService.getById(anyInt())).thenReturn(new Resource(1, "a", null, null, null, new Date(), new Date(), 100, 0, 0, 0, 0, "mod"));
mockMvc.perform(get("/user/upload")
.param("userId", "1")
@@ -331,7 +334,7 @@
public void testGetUserPurchase() throws Exception {
when(userPurchaseService.page(any(), any())).thenReturn(new Page<>(1, 2));
- when(resourceService.getById(anyInt())).thenReturn(new Resource(1, "a", null, null, null, new Date(), new Date(), 100, 0, 0, 0, 0, 0, "mod"));
+ when(resourceService.getById(anyInt())).thenReturn(new Resource(1, "a", null, null, null, new Date(), new Date(), 100, 0, 0, 0, 0, "mod"));
mockMvc.perform(get("/user/purchase")
.param("userId", "1")
@@ -356,6 +359,19 @@
.andExpect(status().isOk());
}
+ // 获取发布过的贴子
+ @Test
+ public void testGetUserThread() throws Exception {
+
+ when(threadService.page(any(), any())).thenReturn(new Page<>(1, 2));
+
+ mockMvc.perform(get("/user/thread")
+ .param("userId", "1")
+ .param("pageNumber", "1")
+ .param("rows", "2"))
+ .andExpect(status().isOk());
+ }
+
// 获取用户数据
@Test
public void testGetUserData() throws Exception {