注册登录,用户等级,社交,动态,新手任务

Change-Id: I1d3183526517fb3c0dab665e0e7547eefa5c9d76
diff --git a/src/test/java/com/example/myproject/controller/PostControllerTest.java b/src/test/java/com/example/myproject/controller/PostControllerTest.java
new file mode 100644
index 0000000..6586086
--- /dev/null
+++ b/src/test/java/com/example/myproject/controller/PostControllerTest.java
@@ -0,0 +1,279 @@
+package com.example.myproject.controller;
+
+import com.example.myproject.entity.Collections;
+import com.example.myproject.entity.Post;
+import com.example.myproject.entity.Users;
+import com.example.myproject.repository.CollectionsRepository;
+import com.example.myproject.repository.PostRepository;
+import com.example.myproject.repository.UserRepository;
+import com.example.myproject.service.PostService;
+import com.example.myproject.utils.Result;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.mockito.*;
+import org.springframework.web.multipart.MultipartFile;
+
+import java.util.*;
+
+import static org.mockito.Mockito.*;
+import static org.junit.jupiter.api.Assertions.*;
+
+class PostControllerTest {
+
+    @InjectMocks
+    private PostController postController;
+
+    @Mock
+    private PostService postService;
+
+    @Mock
+    private CollectionsRepository collectionsRepository;
+
+    @Mock
+    private PostRepository postRepository;
+
+    @Mock
+    private UserRepository userRepository;
+
+    @InjectMocks
+    private UserController userController;
+
+    @BeforeEach
+    void setup() {
+        MockitoAnnotations.openMocks(this);
+    }
+
+    // 测试创建帖子
+    void createPostTest() {
+        Long userId = 1L;
+        String postContent = "Test Post Content";
+        String title = "Test Post Title";
+        MultipartFile[] imageFiles = new MultipartFile[0];  // Empty array for simplicity
+
+        // 模拟服务层的返回值
+        Map<String, Object> responseMap = new HashMap<>();
+        responseMap.put("postNo", 123L);
+        responseMap.put("message", "帖子创建成功");
+
+        when(postService.createPost(userId, postContent, title, imageFiles)).thenReturn(responseMap);
+
+        // 调用控制器的方法
+        Map<String, Object> resultMap = postController.createPost(userId, postContent, title, imageFiles);
+
+        // 手动将 Map<String, Object> 包装到 Result.success()
+        Result<Map<String, Object>> result = Result.success(resultMap, "帖子创建成功");
+
+        // 验证返回的结果
+        assertEquals("200", result.getCode());
+        assertEquals("帖子创建成功", result.getMsg());
+        assertEquals(123L, result.getData().get("postNo"));
+    }
+
+    // 测试编辑帖子
+    @Test
+    void updatePostTest() {
+        Long postId = 1L;
+        Post post = new Post();
+        post.setPostNo(postId);
+        post.setTitle("Updated Title");
+        post.setPostContent("Updated Content");
+
+        // 模拟服务层的行为
+        doNothing().when(postService).updatePost(postId, post);
+
+        // 调用控制器的方法
+        String result = postController.updatePost(postId, post);
+
+        // 验证返回的结果
+        assertEquals("Post updated successfully!", result);
+    }
+
+    // 测试删除帖子
+    @Test
+    void deletePostTest() {
+        Long postId = 1L;
+
+        // 模拟服务层的行为
+        doNothing().when(postService).deletePost(postId);
+
+        // 调用控制器的方法
+        String result = postController.deletePost(postId);
+
+        // 验证返回的结果
+        assertEquals("Post deleted successfully!", result);
+    }
+
+    // 测试点赞帖子
+    @Test
+    void likePostTest() {
+        Long postId = 1L;
+        Long userId = 1L;
+
+        // 模拟服务层的行为
+        doNothing().when(postService).likePost(postId, userId);
+
+        // 创建请求体
+        Map<String, Long> requestBody = new HashMap<>();
+        requestBody.put("user_id", userId);
+
+        // 调用控制器的方法
+        String result = postController.likePost(postId, requestBody);
+
+        // 验证返回的结果
+        assertEquals("Post liked successfully!", result);
+    }
+
+    // 测试取消点赞帖子
+    @Test
+    void unlikePostTest() {
+        Long postId = 1L;
+        Long userId = 1L;
+
+        // 模拟服务层的行为
+        doNothing().when(postService).unlikePost(postId, userId);
+
+        // 创建请求体
+        Map<String, Long> requestBody = new HashMap<>();
+        requestBody.put("user_id", userId);
+
+        // 调用控制器的方法
+        String result = postController.unlikePost(postId, requestBody);
+
+        // 验证返回的结果
+        assertEquals("Post unliked successfully!", result);
+    }
+
+    // 测试收藏帖子
+    @Test
+    void collectPostTest() {
+        Long postId = 1L;
+        Long userId = 1L;
+
+        // 模拟服务层的行为
+        doNothing().when(postService).collectPost(postId, userId);
+
+        // 创建请求体
+        Map<String, Long> requestBody = new HashMap<>();
+        requestBody.put("user_id", userId);
+
+        // 调用控制器的方法
+        String result = postController.collectPost(postId, requestBody);
+
+        // 验证返回的结果
+        assertEquals("Post collected successfully!", result);
+    }
+
+    // 测试取消收藏帖子
+    @Test
+    void uncollectPostTest() {
+        Long postId = 1L;
+        Long userId = 1L;
+
+        // 模拟服务层的行为
+        doNothing().when(postService).uncollectPost(postId, userId);
+
+        // 创建请求体
+        Map<String, Long> requestBody = new HashMap<>();
+        requestBody.put("user_id", userId);
+
+        // 调用控制器的方法
+        String result = postController.uncollectPost(postId, requestBody);
+
+        // 验证返回的结果
+        assertEquals("Post uncollected successfully!", result);
+    }
+
+    // 测试获取所有帖子
+    @Test
+    void getAllPostsTest() {
+        // 模拟服务层的行为
+        Map<String, Object> responseMap = new HashMap<>();
+        responseMap.put("total", 5);
+        responseMap.put("posts", new ArrayList<>());
+
+        when(postService.getAllPosts()).thenReturn(responseMap);
+
+        // 调用控制器的方法
+        Map<String, Object> result = postController.getAllPosts();
+
+        // 验证返回的结果
+        assertEquals(5, result.get("total"));
+    }
+
+    // 测试根据帖子ID获取帖子
+    @Test
+    void getPostByIdTest() {
+        Long postId = 1L;
+        Map<String, Object> responseMap = new HashMap<>();
+        responseMap.put("postNo", 123L);
+        responseMap.put("message", "Post details");
+
+        // 模拟服务层的行为
+        when(postService.getPostById(postId)).thenReturn(responseMap);
+
+        // 调用控制器的方法
+        Map<String, Object> result = postController.getPostById(postId);
+
+        // 验证返回的结果
+        assertEquals("Post details", result.get("message"));
+        assertEquals(123L, result.get("postNo"));
+    }
+
+    // 测试获取用户收藏的所有帖子接口
+    @Test
+    void testGetAllCollections() {
+        Long userId = 1L;
+
+        // 模拟用户收藏的数据
+        Collections collection = new Collections();
+        collection.setCollectionId(1L);
+        collection.setUserId(userId);
+        collection.setPostNo(101L);
+
+        List<Collections> collections = new ArrayList<>();
+        collections.add(collection);
+
+        // 模拟帖子数据
+        Post post = new Post();
+        post.setPostNo(101L);
+        post.setTitle("Test Post Title");
+        post.setPostContent("Test Post Content");
+        post.setImageUrl("https://example.com/image.jpg");
+        post.setUser_id(2L);  // 作者用户ID
+
+        Users user = new Users();
+        user.setUserId(2L);
+        user.setUsername("testUser");
+        user.setAvatarUrl("https://example.com/avatar.jpg");
+
+        // 设置模拟服务层的返回
+        when(postService.getAllCollections(userId)).thenReturn(Arrays.asList(
+                Map.of(
+                        "postNo", post.getPostNo(),
+                        "title", post.getTitle(),
+                        "postContent", post.getPostContent(),
+                        "imageUrl", post.getImageUrl(),
+                        "userId", post.getUser_id(),
+                        "username", user.getUsername(),
+                        "avatarUrl", user.getAvatarUrl()
+                )
+        ));
+
+        // 调用控制器方法
+        List<Map<String, Object>> result = postController.getAllCollections(userId);
+
+        // 验证返回的结果
+        assertNotNull(result);
+        assertEquals(1, result.size()); // 验证返回数据的数量
+
+        // 验证返回的数据结构
+        Map<String, Object> postInfo = result.get(0);
+        assertEquals(101L, postInfo.get("postNo"));
+        assertEquals("Test Post Title", postInfo.get("title"));
+        assertEquals("Test Post Content", postInfo.get("postContent"));
+        assertEquals("https://example.com/image.jpg", postInfo.get("imageUrl"));
+        assertEquals(2L, postInfo.get("userId"));
+        assertEquals("testUser", postInfo.get("username"));
+        assertEquals("https://example.com/avatar.jpg", postInfo.get("avatarUrl"));
+    }
+}