完善了服务器配置,实现自动解析oss-url,自定义tracker响应
Change-Id: I2bf0848547427095bf898f2252c5020641764b21
diff --git a/src/test/java/edu/bjtu/groupone/backend/DatabaseConnectionTest.java b/src/test/java/edu/bjtu/groupone/backend/DatabaseConnectionTest.java
new file mode 100644
index 0000000..e2e565f
--- /dev/null
+++ b/src/test/java/edu/bjtu/groupone/backend/DatabaseConnectionTest.java
@@ -0,0 +1,28 @@
+package edu.bjtu.groupone.backend;
+
+import org.junit.jupiter.api.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+
+import javax.sql.DataSource;
+import java.sql.Connection;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+@SpringBootTest
+public class DatabaseConnectionTest {
+
+ @Autowired
+ private DataSource dataSource;
+
+ @Test
+ public void testDatabaseConnection() {
+ try (Connection connection = dataSource.getConnection()) {
+ assertNotNull(connection);
+ System.out.println("✅ 数据库连接成功: " + connection.getMetaData().getURL());
+ System.out.println("📌 数据库用户: " + connection.getMetaData().getUserName());
+ } catch (Exception e) {
+ fail("❌ 数据库连接失败: " + e.getMessage());
+ }
+ }
+}
diff --git a/src/test/java/edu/bjtu/groupone/backend/TorrentControllerTest.java b/src/test/java/edu/bjtu/groupone/backend/TorrentControllerTest.java
deleted file mode 100644
index 86573d8..0000000
--- a/src/test/java/edu/bjtu/groupone/backend/TorrentControllerTest.java
+++ /dev/null
@@ -1,125 +0,0 @@
-package edu.bjtu.groupone.backend;
-
-import edu.bjtu.groupone.backend.api.TorrentController;
-import edu.bjtu.groupone.backend.domain.entity.Torrent;
-import edu.bjtu.groupone.backend.service.TorrentService;
-import edu.bjtu.groupone.backend.utils.GetTokenUserId;
-import jakarta.servlet.http.HttpServletRequest;
-import org.assertj.core.api.Assertions;
-import org.junit.jupiter.api.Test;
-import org.junit.jupiter.api.extension.ExtendWith;
-import org.mockito.InjectMocks;
-import org.mockito.Mock;
-import org.mockito.MockedStatic;
-import org.mockito.Mockito;
-import org.mockito.junit.jupiter.MockitoExtension;
-import org.springframework.core.io.ByteArrayResource;
-import org.springframework.core.io.Resource;
-import org.springframework.http.ResponseEntity;
-import org.springframework.mock.web.MockMultipartFile;
-import org.springframework.web.multipart.MultipartFile;
-
-@ExtendWith({MockitoExtension.class})
-public class TorrentControllerTest {
- @Mock
- private TorrentService torrentService;
- @InjectMocks
- private TorrentController torrentController;
- @Mock
- private HttpServletRequest request;
-
- public TorrentControllerTest() {
- }
-
- @Test
- public void uploadTorrent_shouldReturnTorrent_whenUserIdValid() throws Exception {
- MultipartFile file = new MockMultipartFile("file", "test.torrent", "application/x-bittorrent", "dummy data".getBytes());
- MockedStatic<GetTokenUserId> utilities = Mockito.mockStatic(GetTokenUserId.class);
-
- try {
- utilities.when(() -> {
- GetTokenUserId.getUserId(this.request);
- }).thenReturn("123");
- Torrent expectedTorrent = new Torrent();
- expectedTorrent.setId(1L);
- expectedTorrent.setName("testfile");
- expectedTorrent.setInfoHash("fakehash");
- expectedTorrent.setSize(12345L);
- expectedTorrent.setUploaderId(123L);
- Mockito.when(this.torrentService.uploadTorrent(file, 123L)).thenReturn(expectedTorrent);
- ResponseEntity<?> response = this.torrentController.uploadTorrent(file, this.request);
- Assertions.assertThat(response.getStatusCodeValue()).isEqualTo(200);
- Assertions.assertThat(response.getBody()).isEqualTo(expectedTorrent);
- ((TorrentService)Mockito.verify(this.torrentService, Mockito.times(1))).uploadTorrent(file, 123L);
- } catch (Throwable var6) {
- if (utilities != null) {
- try {
- utilities.close();
- } catch (Throwable var5) {
- var6.addSuppressed(var5);
- }
- }
-
- throw var6;
- }
-
- if (utilities != null) {
- utilities.close();
- }
-
- }
-
- @Test
- public void uploadTorrent_shouldReturnUnauthorized_whenUserIdNull() throws Exception {
- MockedStatic<GetTokenUserId> utilities = Mockito.mockStatic(GetTokenUserId.class);
-
- try {
- utilities.when(() -> {
- GetTokenUserId.getUserId(this.request);
- }).thenReturn((Object)null);
- MultipartFile file = new MockMultipartFile("file", "test.torrent", "application/x-bittorrent", "dummy data".getBytes());
- ResponseEntity<?> response = this.torrentController.uploadTorrent(file, this.request);
- Assertions.assertThat(response.getStatusCodeValue()).isEqualTo(401);
- Assertions.assertThat(response.getBody()).isEqualTo("Token无效或缺失");
- Mockito.verifyNoInteractions(new Object[]{this.torrentService});
- } catch (Throwable var5) {
- if (utilities != null) {
- try {
- utilities.close();
- } catch (Throwable var4) {
- var5.addSuppressed(var4);
- }
- }
-
- throw var5;
- }
-
- if (utilities != null) {
- utilities.close();
- }
-
- }
-
- @Test
- public void downloadTorrent_shouldReturnResource_whenFound() throws Exception {
- String infoHash = "fakehash";
- byte[] data = "torrent data".getBytes();
- Resource resource = new ByteArrayResource(data);
- Mockito.when(this.torrentService.downloadTorrent(infoHash)).thenReturn(resource);
- ResponseEntity<Resource> response = this.torrentController.downloadTorrent(infoHash);
- Assertions.assertThat(response.getStatusCodeValue()).isEqualTo(200);
- Assertions.assertThat(response.getHeaders().getFirst("Content-Disposition")).isEqualTo("attachment; filename=\"" + infoHash + ".torrent\"");
- Assertions.assertThat((Resource)response.getBody()).isEqualTo(resource);
- ((TorrentService)Mockito.verify(this.torrentService, Mockito.times(1))).downloadTorrent(infoHash);
- }
-
- @Test
- public void downloadTorrent_shouldReturnNotFound_whenException() throws Exception {
- String infoHash = "notexist";
- Mockito.when(this.torrentService.downloadTorrent(infoHash)).thenThrow(new Throwable[]{new RuntimeException("Not found")});
- ResponseEntity<Resource> response = this.torrentController.downloadTorrent(infoHash);
- Assertions.assertThat(response.getStatusCodeValue()).isEqualTo(404);
- Assertions.assertThat((Resource)response.getBody()).isNull();
- ((TorrentService)Mockito.verify(this.torrentService, Mockito.times(1))).downloadTorrent(infoHash);
- }
-}
\ No newline at end of file
diff --git a/src/test/java/edu/bjtu/groupone/backend/TorrentServiceImplTest.java b/src/test/java/edu/bjtu/groupone/backend/TorrentServiceImplTest.java
deleted file mode 100644
index f25f7cb..0000000
--- a/src/test/java/edu/bjtu/groupone/backend/TorrentServiceImplTest.java
+++ /dev/null
@@ -1,68 +0,0 @@
-package edu.bjtu.groupone.backend;
-
-import edu.bjtu.groupone.backend.mapper.TorrentMapper;
-import edu.bjtu.groupone.backend.mapper.UserMapper;
-import edu.bjtu.groupone.backend.domain.entity.Torrent;
-import edu.bjtu.groupone.backend.domain.entity.User;
-import edu.bjtu.groupone.backend.service.impl.TorrentServiceImpl;
-import java.io.File;
-import java.io.FileInputStream;
-import org.assertj.core.api.Assertions;
-import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.Test;
-import org.junit.jupiter.api.extension.ExtendWith;
-import org.mockito.InjectMocks;
-import org.mockito.Mock;
-import org.mockito.Mockito;
-import org.mockito.junit.jupiter.MockitoExtension;
-import org.springframework.core.io.Resource;
-import org.springframework.mock.web.MockMultipartFile;
-
-@ExtendWith({MockitoExtension.class})
-public class TorrentServiceImplTest {
- @Mock
- private TorrentMapper torrentMapper;
- @Mock
- private UserMapper userMapper;
- @InjectMocks
- private TorrentServiceImpl torrentService;
-
- public TorrentServiceImplTest() {
- }
-
- @BeforeEach
- void setup() {
- }
-
- @Test
- public void uploadTorrent_shouldSaveAndReturnTorrent() throws Exception {
- File file = new File("torrents/22301024-王玉涛.doc.torrent");
- Assertions.assertThat(file.exists()).isTrue();
- FileInputStream inputStream = new FileInputStream(file);
- MockMultipartFile mockFile = new MockMultipartFile("file", "22301024-王玉涛.doc.torrent", "application/x-bittorrent", inputStream);
- ((TorrentMapper)Mockito.doNothing().when(this.torrentMapper)).insertTorrent((Torrent)Mockito.any(Torrent.class));
- User fakeUser = new User();
- fakeUser.setUserId(100);
- Mockito.when(this.userMapper.selectById(100L)).thenReturn(fakeUser);
- Torrent result = this.torrentService.uploadTorrent(mockFile, 100L);
- Assertions.assertThat(result).isNotNull();
- Assertions.assertThat(result.getInfoHash()).isNotBlank();
- Assertions.assertThat(result.getName()).isNotBlank();
- Assertions.assertThat(result.getUploaderId()).isEqualTo(100L);
- ((TorrentMapper)Mockito.verify(this.torrentMapper, Mockito.times(1))).insertTorrent((Torrent)Mockito.any(Torrent.class));
- }
-
- @Test
- public void downloadTorrent_shouldReturnResource_whenTorrentExists() throws Exception {
- File tempFile = File.createTempFile("test-torrent-", ".torrent");
- tempFile.deleteOnExit();
- Torrent fakeTorrent = new Torrent();
- fakeTorrent.setInfoHash("fakeinfohash123");
- fakeTorrent.setFilePath(tempFile.getAbsolutePath());
- Mockito.when(this.torrentMapper.selectByInfoHash("fakeinfohash123")).thenReturn(fakeTorrent);
- Resource resource = this.torrentService.downloadTorrent("fakeinfohash123");
- Assertions.assertThat(resource).isNotNull();
- Assertions.assertThat(resource.exists()).isTrue();
- Assertions.assertThat(resource.getFile().getAbsolutePath()).isEqualTo(tempFile.getAbsolutePath());
- }
-}
diff --git a/src/test/java/edu/bjtu/groupone/backend/WorkServiceTest.java b/src/test/java/edu/bjtu/groupone/backend/WorkServiceTest.java
index b3871da..44a05b6 100644
--- a/src/test/java/edu/bjtu/groupone/backend/WorkServiceTest.java
+++ b/src/test/java/edu/bjtu/groupone/backend/WorkServiceTest.java
@@ -1,10 +1,13 @@
package edu.bjtu.groupone.backend;
+import com.turn.ttorrent.tracker.Tracker;
+import edu.bjtu.groupone.backend.config.TrafficAwareTracker;
import edu.bjtu.groupone.backend.domain.dto.WorkResponse;
import edu.bjtu.groupone.backend.domain.entity.Category;
import edu.bjtu.groupone.backend.domain.entity.Work;
import edu.bjtu.groupone.backend.mapper.WorkMybatisMapper;
import edu.bjtu.groupone.backend.service.CategoryService;
+import edu.bjtu.groupone.backend.service.PeerTrafficService;
import edu.bjtu.groupone.backend.service.WorkService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@@ -12,10 +15,14 @@
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
+import java.io.IOException;
+import java.net.InetSocketAddress;
import java.util.Arrays;
import java.util.List;
@@ -23,6 +30,7 @@
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
+@SpringBootTest
@ExtendWith(MockitoExtension.class)
class WorkServiceTest {
@@ -117,4 +125,14 @@
List<WorkResponse> result = workService.getWorksByAuthor("李四");
assertTrue(result.isEmpty());
}
+
+ @Autowired
+ PeerTrafficService peerTrafficService;
+ @Test
+ public void test() throws IOException {
+ Tracker tracker;
+ InetSocketAddress trackerAddress = new InetSocketAddress("0.0.0.0", 6969);
+ tracker = new Tracker(trackerAddress);
+ tracker.start();
+ }
}
\ No newline at end of file