Seamher | 5ff35f8 | 2025-06-09 01:23:27 +0800 | [diff] [blame^] | 1 | package com.g9.g9backend.controller; |
| 2 | |
| 3 | import com.g9.g9backend.pojo.TorrentRecord; |
| 4 | import com.g9.g9backend.service.TorrentRecordService; |
| 5 | import org.junit.jupiter.api.BeforeEach; |
| 6 | import org.junit.jupiter.api.Test; |
| 7 | import org.mockito.InjectMocks; |
| 8 | import org.mockito.Mock; |
| 9 | import org.mockito.MockitoAnnotations; |
| 10 | import org.springframework.mock.web.MockMultipartFile; |
| 11 | import org.springframework.test.web.servlet.MockMvc; |
| 12 | import org.springframework.test.web.servlet.setup.MockMvcBuilders; |
| 13 | |
| 14 | import java.nio.charset.StandardCharsets; |
| 15 | import java.util.Date; |
| 16 | |
| 17 | import static org.mockito.Mockito.verify; |
| 18 | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.multipart; |
| 19 | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; |
| 20 | import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
| 21 | import static org.hamcrest.Matchers.containsString; |
| 22 | import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; |
| 23 | |
| 24 | public class FileControllerTest { |
| 25 | |
| 26 | private MockMvc mockMvc; |
| 27 | |
| 28 | @InjectMocks |
| 29 | private FileController fileController; |
| 30 | |
| 31 | @Mock |
| 32 | private TorrentRecordService torrentRecordService; |
| 33 | |
| 34 | @BeforeEach |
| 35 | public void setup() { |
| 36 | MockitoAnnotations.openMocks(this); |
| 37 | mockMvc = MockMvcBuilders.standaloneSetup(fileController).build(); |
| 38 | } |
| 39 | |
| 40 | // 测试上传文件接口(模拟 multipart/form-data) |
| 41 | @Test |
| 42 | public void testUploadFile_success() throws Exception { |
| 43 | MockMultipartFile mockFile = new MockMultipartFile( |
| 44 | "file", // 参数名,必须为 file |
| 45 | "test.txt", // 文件名 |
| 46 | "text/plain", // MIME 类型 |
| 47 | "test content".getBytes(StandardCharsets.UTF_8) // 内容 |
| 48 | ); |
| 49 | |
| 50 | mockMvc.perform(multipart("/file").file(mockFile)) |
| 51 | .andExpect(status().isOk()) |
| 52 | .andExpect(content().string(containsString("http://localhost:65/"))); |
| 53 | } |
| 54 | |
| 55 | // 测试上传 BT 文件接口 |
| 56 | @Test |
| 57 | public void testUploadBTFile_success() throws Exception { |
| 58 | TorrentRecord record = new TorrentRecord(); |
| 59 | record.setTorrentRecordId(1); |
| 60 | record.setTorrentUrl("test.torrent"); |
| 61 | record.setInfoHash("abc123"); |
| 62 | record.setUploaderUserId(1); |
| 63 | record.setUploadTime(new Date()); |
| 64 | |
| 65 | // 模拟 post 请求,传 json 数据 |
| 66 | mockMvc.perform(post("/file/bt") |
| 67 | .contentType("application/json") |
| 68 | .content(""" |
| 69 | { |
| 70 | "torrentRecordId": 1, |
| 71 | "torrentUrl": "test.torrent", |
| 72 | "infoHash": "abc123", |
| 73 | "uploaderUserId": 1, |
| 74 | "uploadTime": "2025-06-09T10:00:00" |
| 75 | } |
| 76 | """)) |
| 77 | .andExpect(status().isOk()); |
| 78 | |
| 79 | // 验证 service 是否调用 |
| 80 | verify(torrentRecordService).save(org.mockito.ArgumentMatchers.any(TorrentRecord.class)); |
| 81 | } |
| 82 | } |