夜雨声烦 | 451d71c | 2025-05-20 00:58:36 +0800 | [diff] [blame] | 1 | package com.example.g8backend.service; |
| 2 | |
夜雨声烦 | 7affa47 | 2025-05-20 19:27:16 +0800 | [diff] [blame^] | 3 | import com.example.g8backend.entity.Post; |
夜雨声烦 | 451d71c | 2025-05-20 00:58:36 +0800 | [diff] [blame] | 4 | import com.example.g8backend.entity.User; |
| 5 | import com.example.g8backend.mapper.UserMapper; |
| 6 | import com.example.g8backend.service.impl.AdminServiceImpl; |
| 7 | import org.junit.jupiter.api.Test; |
| 8 | import org.junit.jupiter.api.extension.ExtendWith; |
| 9 | import org.mockito.InjectMocks; |
| 10 | import org.mockito.Mock; |
| 11 | import org.mockito.junit.jupiter.MockitoExtension; |
夜雨声烦 | 7affa47 | 2025-05-20 19:27:16 +0800 | [diff] [blame^] | 12 | import com.example.g8backend.mapper.PostMapper; |
| 13 | |
| 14 | import java.time.LocalDateTime; |
| 15 | |
夜雨声烦 | 451d71c | 2025-05-20 00:58:36 +0800 | [diff] [blame] | 16 | import static org.junit.jupiter.api.Assertions.*; |
| 17 | import static org.mockito.Mockito.*; |
| 18 | |
| 19 | @ExtendWith(MockitoExtension.class) |
| 20 | public class AdminServiceImplTest { |
| 21 | @Mock |
| 22 | private UserMapper userMapper; |
| 23 | @InjectMocks |
| 24 | private AdminServiceImpl adminService; |
夜雨声烦 | 7affa47 | 2025-05-20 19:27:16 +0800 | [diff] [blame^] | 25 | @Mock |
| 26 | private PostMapper postMapper; |
夜雨声烦 | 451d71c | 2025-05-20 00:58:36 +0800 | [diff] [blame] | 27 | |
| 28 | @Test |
| 29 | public void testGrantVip_Success() { |
| 30 | User user = new User(); |
| 31 | user.setUserId(1L); |
| 32 | when(userMapper.selectById(1L)).thenReturn(user); |
| 33 | |
| 34 | boolean result = adminService.grantVip(1L); |
| 35 | assertTrue(result); |
| 36 | assertEquals("vip", user.getUserLevel()); |
| 37 | } |
| 38 | |
| 39 | @Test |
| 40 | public void testGrantVip_UserNotFound() { |
| 41 | when(userMapper.selectById(1L)).thenReturn(null); |
| 42 | boolean result = adminService.grantVip(1L); |
| 43 | assertFalse(result); |
| 44 | } |
夜雨声烦 | 7affa47 | 2025-05-20 19:27:16 +0800 | [diff] [blame^] | 45 | |
| 46 | void testBanUser_Success() { |
| 47 | User user = new User().setUserId(1L); |
| 48 | when(userMapper.selectById(1L)).thenReturn(user); |
| 49 | when(userMapper.updateById(any(User.class))).thenReturn(1); |
| 50 | |
| 51 | boolean result = adminService.banUser(1L, "违规操作", 1001L); |
| 52 | assertTrue(result); |
| 53 | |
| 54 | assertTrue(user.getIsBanned()); |
| 55 | assertEquals("违规操作", user.getBannedReason()); |
| 56 | assertNotNull(user.getBannedAt()); |
| 57 | assertEquals(1001L, user.getBannedBy()); |
| 58 | verify(userMapper).updateById(any(User.class)); |
| 59 | } |
| 60 | |
| 61 | @Test |
| 62 | void testBanUser_UserNotFound() { |
| 63 | when(userMapper.selectById(1L)).thenReturn(null); |
| 64 | boolean result = adminService.banUser(1L, "原因", 1001L); |
| 65 | assertFalse(result); |
| 66 | verify(userMapper, never()).updateById(any(User.class)); |
| 67 | } |
| 68 | |
| 69 | @Test |
| 70 | void testUnbanUser_Success() { |
| 71 | User user = new User().setUserId(1L) |
| 72 | .setIsBanned(true) |
| 73 | .setBannedReason("原因") |
| 74 | .setBannedAt(LocalDateTime.now()) |
| 75 | .setBannedBy(1001L); |
| 76 | when(userMapper.selectById(1L)).thenReturn(user); |
| 77 | when(userMapper.updateById(any(User.class))).thenReturn(1); |
| 78 | |
| 79 | boolean result = adminService.unbanUser(1L, 1001L); |
| 80 | assertTrue(result); |
| 81 | |
| 82 | assertFalse(user.getIsBanned()); |
| 83 | assertNull(user.getBannedReason()); |
| 84 | assertNull(user.getBannedAt()); |
| 85 | assertNull(user.getBannedBy()); |
| 86 | verify(userMapper).updateById(any(User.class)); |
| 87 | } |
| 88 | |
| 89 | @Test |
| 90 | void testUnbanUser_UserNotFound() { |
| 91 | when(userMapper.selectById(1L)).thenReturn(null); |
| 92 | boolean result = adminService.unbanUser(1L, 1001L); |
| 93 | assertFalse(result); |
| 94 | verify(userMapper, never()).updateById(any(User.class)); |
| 95 | } |
| 96 | |
| 97 | @Test |
| 98 | void testLockPost_Success() { |
| 99 | Post post = new Post().setPostId(1L); |
| 100 | when(postMapper.selectById(1L)).thenReturn(post); |
| 101 | when(postMapper.updateById(any(Post.class))).thenReturn(1); |
| 102 | |
| 103 | boolean result = adminService.lockPost(1L, "违规内容", 1001L); |
| 104 | assertTrue(result); |
| 105 | |
| 106 | assertTrue(post.getIsLocked()); |
| 107 | assertEquals("违规内容", post.getLockedReason()); |
| 108 | assertNotNull(post.getLockedAt()); |
| 109 | assertEquals(1001L, post.getLockedBy()); |
| 110 | verify(postMapper).updateById(any(Post.class)); |
| 111 | } |
| 112 | |
| 113 | @Test |
| 114 | void testLockPost_PostNotFound() { |
| 115 | when(postMapper.selectById(1L)).thenReturn(null); |
| 116 | boolean result = adminService.lockPost(1L, "原因", 1001L); |
| 117 | assertFalse(result); |
| 118 | verify(postMapper, never()).updateById(any(Post.class)); |
| 119 | } |
| 120 | |
| 121 | @Test |
| 122 | void testUnlockPost_Success() { |
| 123 | Post post = new Post().setPostId(1L) |
| 124 | .setIsLocked(true) |
| 125 | .setLockedReason("原因") |
| 126 | .setLockedAt(LocalDateTime.now()) |
| 127 | .setLockedBy(1001L); |
| 128 | when(postMapper.selectById(1L)).thenReturn(post); |
| 129 | when(postMapper.updateById(any(Post.class))).thenReturn(1); |
| 130 | |
| 131 | boolean result = adminService.unlockPost(1L, 1001L); |
| 132 | assertTrue(result); |
| 133 | |
| 134 | assertFalse(post.getIsLocked()); |
| 135 | assertNull(post.getLockedReason()); |
| 136 | assertNull(post.getLockedAt()); |
| 137 | assertNull(post.getLockedBy()); |
| 138 | verify(postMapper).updateById(any(Post.class)); |
| 139 | } |
| 140 | @Test |
| 141 | void testUnlockPost_PostNotFound() { |
| 142 | when(postMapper.selectById(1L)).thenReturn(null); |
| 143 | boolean result = adminService.unlockPost(1L, 1001L); |
| 144 | assertFalse(result); |
| 145 | verify(postMapper, never()).updateById(any(Post.class)); |
| 146 | } |
夜雨声烦 | 451d71c | 2025-05-20 00:58:36 +0800 | [diff] [blame] | 147 | } |