liujie
2026-08-11 323764c813a09cd56cdab780155ef659dbe0b364
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package com.ruoyi.system.service.impl;
 
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.system.domain.SysUserRole;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.mapper.SysUserRoleMapper;
import org.springframework.test.util.ReflectionTestUtils;
 
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
 
/**
 * 中台用户本地映射回归测试。
 */
public class SysUserServiceImplMiddlePlatformTest
{
    public static void main(String[] args)
    {
        shouldReuseExistingLocalUser();
        shouldCreateMissingLocalUserWithAdminRole();
        shouldTreatAdminRoleAsAdministrator();
    }
 
    private static void shouldReuseExistingLocalUser()
    {
        SysUser existingUser = new SysUser();
        existingUser.setUserId(20L);
        existingUser.setUserName("middle-user");
        UserMapperStub userMapperStub = new UserMapperStub(existingUser);
        UserRoleMapperStub roleMapperStub = new UserRoleMapperStub();
 
        SysUserServiceImpl service = createService(userMapperStub, roleMapperStub);
 
        require(existingUser == service.getOrCreateMiddlePlatformAdmin("middle-user"),
                "已有本地用户未被复用");
        require(userMapperStub.insertedUsers.isEmpty(), "已有本地用户被重复创建");
        require(roleMapperStub.insertedRoles.isEmpty(), "已有本地用户的角色被覆盖");
    }
 
    private static void shouldCreateMissingLocalUserWithAdminRole()
    {
        UserMapperStub userMapperStub = new UserMapperStub(null);
        UserRoleMapperStub roleMapperStub = new UserRoleMapperStub();
        SysUserServiceImpl service = createService(userMapperStub, roleMapperStub);
 
        SysUser createdUser = service.getOrCreateMiddlePlatformAdmin("middle-user");
 
        require(createdUser != null && Long.valueOf(21L).equals(createdUser.getUserId()),
                "未返回自动创建的本地用户");
        require(userMapperStub.insertedUsers.size() == 1, "本地用户创建次数不正确");
        SysUser insertedUser = userMapperStub.insertedUsers.get(0);
        require("middle-user".equals(insertedUser.getUserName()), "本地用户名不正确");
        require("middle-user".equals(insertedUser.getNickName()), "本地昵称不正确");
        require("0".equals(insertedUser.getStatus()), "本地用户状态不正确");
        require(insertedUser.getPassword() != null, "未生成随机本地密码");
        require(roleMapperStub.insertedRoles.size() == 1, "管理员角色绑定次数不正确");
        SysUserRole role = roleMapperStub.insertedRoles.get(0);
        require(Long.valueOf(21L).equals(role.getUserId()), "角色绑定的用户不正确");
        require(Long.valueOf(1L).equals(role.getRoleId()), "未绑定管理员角色");
    }
 
    private static void shouldTreatAdminRoleAsAdministrator()
    {
        SysRole adminRole = new SysRole();
        adminRole.setRoleId(1L);
        SysUser user = new SysUser();
        user.setUserId(21L);
        user.setRoles(Arrays.asList(adminRole));
 
        require(user.isAdmin(), "管理员角色未获得管理员权限");
    }
 
    private static SysUserServiceImpl createService(UserMapperStub userMapperStub,
                                                    UserRoleMapperStub roleMapperStub)
    {
        SysUserServiceImpl service = new SysUserServiceImpl();
        ReflectionTestUtils.setField(service, "userMapper", proxy(SysUserMapper.class, userMapperStub));
        ReflectionTestUtils.setField(service, "userRoleMapper", proxy(SysUserRoleMapper.class, roleMapperStub));
        return service;
    }
 
    @SuppressWarnings("unchecked")
    private static <T> T proxy(Class<T> type, MapperStub stub)
    {
        return (T) Proxy.newProxyInstance(type.getClassLoader(), new Class<?>[] { type },
                (proxy, method, args) -> stub.invoke(method.getName(), args));
    }
 
    private static void require(boolean condition, String message)
    {
        if (!condition)
        {
            throw new AssertionError(message);
        }
    }
 
    private interface MapperStub
    {
        Object invoke(String methodName, Object[] args);
    }
 
    private static final class UserMapperStub implements MapperStub
    {
        private SysUser storedUser;
 
        private final List<SysUser> insertedUsers = new ArrayList<SysUser>();
 
        private UserMapperStub(SysUser storedUser)
        {
            this.storedUser = storedUser;
        }
 
        @Override
        public Object invoke(String methodName, Object[] args)
        {
            if ("selectUserByUserName".equals(methodName))
            {
                return storedUser;
            }
            if ("insertUser".equals(methodName))
            {
                SysUser user = (SysUser) args[0];
                user.setUserId(21L);
                insertedUsers.add(user);
                storedUser = user;
                return 1;
            }
            return null;
        }
    }
 
    private static final class UserRoleMapperStub implements MapperStub
    {
        private final List<SysUserRole> insertedRoles = new ArrayList<SysUserRole>();
 
        @Override
        public Object invoke(String methodName, Object[] args)
        {
            if ("insertUserRole".equals(methodName))
            {
                insertedRoles.add((SysUserRole) args[0]);
                return 1;
            }
            return null;
        }
    }
}