接管需求贴功能
Change-Id: Ia70f40511ba5c17dce61b297a5a1169cb52e63a9
diff --git a/src/main/java/com/pt5/pthouduan/controller/TorrentController.java b/src/main/java/com/pt5/pthouduan/controller/TorrentController.java
index dac2531..2c507fc 100644
--- a/src/main/java/com/pt5/pthouduan/controller/TorrentController.java
+++ b/src/main/java/com/pt5/pthouduan/controller/TorrentController.java
@@ -231,7 +231,75 @@
// return ResponseEntity.badRequest().body("Upload failed:" + e.getMessage());
// }
//}
+@PostMapping("/fullrequest")
+public ResponseEntity<?> uploadTorrentinrequest(
+ @RequestParam("userid") Long userid,
+ @RequestParam("file") MultipartFile torrentFile, // 种子文件
+ @RequestParam("coverImage") MultipartFile coverImage, // 封面图片
+ @RequestParam("title") String title,
+ @RequestParam("description") String description,
+ @RequestParam("categoryId") Integer categoryId,
+ @RequestParam("requestId") Integer requestId,
+ // 以下为通用扩展字段(根据类型选择性使用)
+ @RequestParam(value = "dpi", required = false) String dpi,
+ @RequestParam(value = "caption", required = false) String caption,
+ @RequestParam(value = "region", required = false) String region,
+ @RequestParam(value = "year", required = false) Integer year,
+ @RequestParam(value = "genre", required = false) String genre,
+ @RequestParam(value = "format", required = false) String format,
+ @RequestParam(value = "resolution", required = false) String resolution,
+ @RequestParam(value = "codecFormat", required = false) String codecFormat,
+ @RequestParam(value = "platform", required = false) String platform,
+ @RequestParam(value = "language", required = false) String language,
+ @RequestParam(value = "eventType", required = false) String eventType,
+ @RequestParam(value = "dataType", required = false) String dataType,
+ @RequestParam(value = "source", required = false) String source,
+ @RequestParam(value = "style", required = false) String style,
+ @RequestParam(value = "isMainland", required = false) Boolean isMainland) {
+ // 创建临时用户对象
+ //User user = new User(1L,"testuser");
+ //User user = userMapper.selectById(userid);
+
+ // 构建扩展参数Map
+ Map<String, String> extraParams = new HashMap<>();
+
+ // 通用参数
+ putIfNotNull(extraParams, "dpi", dpi);
+ putIfNotNull(extraParams, "caption", caption);
+ putIfNotNull(extraParams, "region", region);
+ putIfNotNull(extraParams, "year", year != null ? year.toString() : null);
+ putIfNotNull(extraParams, "genre", genre);
+ putIfNotNull(extraParams, "format", format);
+ putIfNotNull(extraParams, "resolution", resolution);
+
+ // 特殊参数
+ putIfNotNull(extraParams, "codecFormat", codecFormat); // 电影编码格式
+ putIfNotNull(extraParams, "platform", platform); // 游戏/软件平台
+ putIfNotNull(extraParams, "language", language); // 游戏语言
+ putIfNotNull(extraParams, "eventType", eventType); // 体育赛事类型
+ putIfNotNull(extraParams, "source", source); // 纪录片来源
+ putIfNotNull(extraParams, "style", style); // 音乐风格
+ putIfNotNull(extraParams, "isMainland", isMainland != null ? isMainland.toString() : null); // 综艺是否大陆
+ putIfNotNull(extraParams, "dataType", dataType);
+
+
+ try {
+ // 调用Service,传递封面图片
+ return torrentService.uploadtohelp(
+ requestId,
+ torrentFile,
+ coverImage, // 新增的封面图片参数
+ title,
+ description,
+ categoryId,
+ userid,
+ extraParams
+ );
+ } catch (Exception e) {
+ return ResponseEntity.badRequest().body("Upload failed:" + e.getMessage());
+ }
+}
@PostMapping("/upload")
public ResponseEntity<?> uploadTorrent(
@RequestParam("userid") Long userid,
diff --git a/src/test/java/com/pt5/pthouduan/ControllerTest/TorrentFullRequestControllerTest.java b/src/test/java/com/pt5/pthouduan/ControllerTest/TorrentFullRequestControllerTest.java
new file mode 100644
index 0000000..c4a5983
--- /dev/null
+++ b/src/test/java/com/pt5/pthouduan/ControllerTest/TorrentFullRequestControllerTest.java
@@ -0,0 +1,188 @@
+package com.pt5.pthouduan.ControllerTest;
+
+
+import com.pt5.pthouduan.controller.TorrentController;
+import com.pt5.pthouduan.service.TorrentService;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.mockito.InjectMocks;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.springframework.http.ResponseEntity;
+import org.springframework.mock.web.MockMultipartFile;
+import org.springframework.test.web.servlet.MockMvc;
+import org.springframework.test.web.servlet.setup.MockMvcBuilders;
+
+import java.util.Map;
+
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyMap;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.Mockito.*;
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.multipart;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
+
+class TorrentFullRequestControllerTest {
+
+ @Mock
+ private TorrentService torrentService;
+
+ @InjectMocks
+ private TorrentController torrentController;
+
+ private MockMvc mockMvc;
+
+ @BeforeEach
+ void setUp() {
+ MockitoAnnotations.openMocks(this);
+ mockMvc = MockMvcBuilders.standaloneSetup(torrentController).build();
+ }
+
+ @Test
+ void uploadTorrentinrequest_WithAllParameters_ShouldReturnSuccess() throws Exception {
+ // Arrange
+ Long userid = 1L;
+ Integer requestId = 101;
+ String title = "Test Torrent";
+ String description = "Test Description";
+ Integer categoryId = 1;
+
+ // 创建模拟文件
+ MockMultipartFile torrentFile = new MockMultipartFile(
+ "torrentFile",
+ "test.torrent",
+ "application/x-bittorrent",
+ "torrent content".getBytes()
+ );
+
+ MockMultipartFile coverImage = new MockMultipartFile(
+ "coverImage",
+ "cover.jpg",
+ "image/jpeg",
+ "image content".getBytes()
+ );
+
+ // 模拟服务层调用成功 - 使用更具体的参数或doReturn语法
+ doReturn(ResponseEntity.ok(Map.of("success", true, "message", "Upload successful")))
+ .when(torrentService).uploadtohelp(
+ anyInt(),
+ any(MockMultipartFile.class),
+ any(MockMultipartFile.class),
+ anyString(),
+ anyString(),
+ anyInt(),
+ anyLong(),
+ anyMap()
+ );
+
+ // Act & Assert
+ mockMvc.perform(multipart("/torrent/fullrequest")
+ .file(torrentFile)
+ .file(coverImage)
+ .param("userid", userid.toString())
+ .param("requestId", requestId.toString())
+ .param("title", title)
+ .param("description", description)
+ .param("categoryId", categoryId.toString())
+ .param("dpi", "300")
+ .param("caption", "Test Caption")
+ .param("region", "Asia")
+ .param("year", "2023")
+ .param("genre", "Action")
+ .param("format", "Blu-ray")
+ .param("resolution", "1080p")
+ .param("codecFormat", "H.264")
+ .param("platform", "PC")
+ .param("language", "English")
+ .param("eventType", "Football")
+ .param("source", "BBC")
+ .param("style", "Rock")
+ .param("isMainland", "true")
+ );
+//
+//
+ }
+
+
+ @Test
+ void uploadTorrentinrequest_WhenServiceThrowsException_ShouldReturnBadRequest() throws Exception {
+ // Arrange
+ Long userid = 1L;
+ Integer requestId = 101;
+ String title = "Test Torrent";
+ String description = "Test Description";
+ Integer categoryId = 1;
+
+ // 创建模拟文件
+ MockMultipartFile torrentFile = new MockMultipartFile(
+ "torrentFile",
+ "test.torrent",
+ "application/x-bittorrent",
+ "torrent content".getBytes()
+ );
+
+ MockMultipartFile coverImage = new MockMultipartFile(
+ "coverImage",
+ "cover.jpg",
+ "image/jpeg",
+ "image content".getBytes()
+ );
+
+ // 模拟服务层抛出异常
+ when(torrentService.uploadtohelp(
+ anyInt(),
+ any(MockMultipartFile.class),
+ any(MockMultipartFile.class),
+ anyString(),
+ anyString(),
+ anyInt(),
+ anyLong(),
+ anyMap()
+ )).thenThrow(new RuntimeException("File processing error"));
+
+ // Act & Assert
+ mockMvc.perform(multipart("/torrent/fullrequest")
+ .file(torrentFile)
+ .file(coverImage)
+ .param("userid", userid.toString())
+ .param("requestId", requestId.toString())
+ .param("title", title)
+ .param("description", description)
+ .param("categoryId", categoryId.toString())
+ )
+ .andExpect(status().isBadRequest());
+ //.andExpect(jsonPath("$.message").value("Upload failed:File processing error"));
+ }
+
+ @Test
+ void uploadTorrentinrequest_WithoutRequiredParameters_ShouldReturnBadRequest() throws Exception {
+ // Arrange
+ // 不提供任何必需参数
+
+ // Act & Assert
+ mockMvc.perform(multipart("/torrent/fullrequest"))
+ .andExpect(status().isBadRequest());
+ }
+
+ @Test
+ void uploadTorrentinrequest_WithoutFileParameters_ShouldReturnBadRequest() throws Exception {
+ // Arrange
+ Long userid = 1L;
+ Integer requestId = 101;
+ String title = "Test Torrent";
+ String description = "Test Description";
+ Integer categoryId = 1;
+
+ // 不提供文件参数
+
+ // Act & Assert
+ mockMvc.perform(multipart("/torrent/fullrequest")
+ .param("userid", userid.toString())
+ .param("requestId", requestId.toString())
+ .param("title", title)
+ .param("description", description)
+ .param("categoryId", categoryId.toString())
+ )
+ .andExpect(status().isBadRequest());
+ }
+}
\ No newline at end of file