UserServiceTest.java
4.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package com.example.erp.module.usr;
import com.example.erp.common.exception.BizException;
import com.example.erp.config.UserPrincipal;
import com.example.erp.module.usr.dto.UserCreateReqDTO;
import com.example.erp.module.usr.entity.StaffEntity;
import com.example.erp.module.usr.entity.UsrUserEntity;
import com.example.erp.module.usr.entity.UserPermissionEntity;
import com.example.erp.module.usr.mapper.PermissionGroupMapper;
import com.example.erp.module.usr.mapper.StaffMapper;
import com.example.erp.common.constants.UsrErrorCode;
import com.example.erp.module.usr.mapper.UserPermissionMapper;
import com.example.erp.module.usr.mapper.UsrUserMapper;
import com.example.erp.module.usr.service.impl.UserServiceImpl;
import com.example.erp.module.usr.vo.UserCreateRespVO;
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.junit.jupiter.MockitoExtension;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
@ExtendWith(MockitoExtension.class)
class UserServiceTest {
@Mock private UsrUserMapper userMapper;
@Mock private StaffMapper staffMapper;
@Mock private PermissionGroupMapper permGroupMapper;
@Mock private UserPermissionMapper userPermissionMapper;
@Mock private BCryptPasswordEncoder passwordEncoder;
@InjectMocks
private UserServiceImpl userService;
private UserCreateReqDTO req;
private UserPrincipal superAdmin;
private UserPrincipal normalUser;
@BeforeEach
void setUp() {
req = new UserCreateReqDTO();
req.setUserCode("UC001");
req.setUsername("testuser");
req.setUserType("普通用户");
req.setLanguage("中文");
superAdmin = new UserPrincipal("u1", "admin", "超级管理员", "b1");
normalUser = new UserPrincipal("u2", "user", "普通用户", "b1");
}
@Test
void createUser_normalUser_throws40300() {
BizException ex = assertThrows(BizException.class,
() -> userService.createUser(req, normalUser));
assertEquals(40300, ex.getCode());
}
@Test
void createUser_success_insertsUserAndReturnsVO() {
when(userMapper.selectCount(any())).thenReturn(0L);
when(passwordEncoder.encode(anyString())).thenReturn("$2a$10$hashed");
when(userMapper.insert(any(UsrUserEntity.class))).thenReturn(1);
UserCreateRespVO vo = userService.createUser(req, superAdmin);
assertNotNull(vo.getUserId());
assertEquals("UC001", vo.getUserCode());
assertEquals("testuser", vo.getUsername());
verify(userMapper).insert(any(UsrUserEntity.class));
}
@Test
void createUser_duplicateUserCode_throws40902() {
when(userMapper.selectCount(any())).thenReturn(1L);
BizException ex = assertThrows(BizException.class,
() -> userService.createUser(req, superAdmin));
assertEquals(40902, ex.getCode());
}
@Test
void createUser_duplicateUsername_throws40901() {
when(userMapper.selectCount(any())).thenReturn(0L, 1L);
BizException ex = assertThrows(BizException.class,
() -> userService.createUser(req, superAdmin));
assertEquals(40901, ex.getCode());
}
@Test
@SuppressWarnings("unchecked")
void createUser_withPermGroups_insertsPermissions() {
req.setPermGroupIds(List.of("g1", "g2"));
when(userMapper.selectCount(any())).thenReturn(0L);
when(passwordEncoder.encode(anyString())).thenReturn("$2a$10$hashed");
when(userMapper.insert(any(UsrUserEntity.class))).thenReturn(1);
userService.createUser(req, superAdmin);
verify(userPermissionMapper).insert(argThat((java.util.Collection<UserPermissionEntity> c) -> c.size() == 2));
}
@Test
void createUser_invalidEmployeeId_throws40001() {
req.setEmployeeId("bad-staff-id");
when(userMapper.selectCount(any())).thenReturn(0L);
when(staffMapper.selectOne(any())).thenReturn(null);
BizException ex = assertThrows(BizException.class,
() -> userService.createUser(req, superAdmin));
assertEquals(UsrErrorCode.EMPLOYEE_NOT_FOUND, ex.getCode());
}
}