puzhibing
9 小时以前 6dead3d8451b1b231d77b653a7fca84406159371
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
151
152
153
154
package com.ruoyi.system.service;
 
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.ruoyi.chargingPile.api.feignClient.SiteClient;
import com.ruoyi.chargingPile.api.model.Partner;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.system.api.domain.SysRole;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.domain.SysUserRole;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.mockito.ArgumentCaptor;
 
import java.util.Arrays;
import java.util.Collections;
 
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
 
class PartnerTypeRoleServiceTest {
 
    @ParameterizedTest
    @CsvSource({
            "1, independent_partner, 11",
            "2, site_owner, 12",
            "3, service_provider, 13"
    })
    void addsRoleMatchingPartnerType(Integer partnerType, String expectedRoleKey, Long expectedRoleId) {
        Fixture fixture = new Fixture(partnerType);
        fixture.stubBuiltInRoles();
        when(fixture.userRoleService.list(any(Wrapper.class))).thenReturn(Collections.emptyList());
 
        fixture.service.synchronize(fixture.user);
 
        ArgumentCaptor<SysUserRole> captor = ArgumentCaptor.forClass(SysUserRole.class);
        verify(fixture.userRoleService).save(captor.capture());
        assertEquals(expectedRoleId, captor.getValue().getRoleId());
        assertEquals(expectedRoleKey, fixture.roleKey(expectedRoleId));
    }
 
    @Test
    void replacesStaleBuiltInRoleButKeepsUnrelatedCustomRole() {
        Fixture fixture = new Fixture(2);
        fixture.stubBuiltInRoles();
        SysUserRole oldTypeRole = userRole(100L, 11L);
        SysUserRole customRole = userRole(100L, 99L);
        when(fixture.userRoleService.list(any(Wrapper.class)))
                .thenReturn(Arrays.asList(oldTypeRole, customRole));
 
        fixture.service.synchronize(fixture.user);
 
        verify(fixture.userRoleService).remove(any(Wrapper.class));
        ArgumentCaptor<SysUserRole> captor = ArgumentCaptor.forClass(SysUserRole.class);
        verify(fixture.userRoleService).save(captor.capture());
        assertEquals(Long.valueOf(12L), captor.getValue().getRoleId());
    }
 
    @Test
    void doesNothingForPlatformUser() {
        Fixture fixture = new Fixture(1);
        fixture.user.setRoleType(1);
 
        fixture.service.synchronize(fixture.user);
 
        verify(fixture.siteClient, never()).getPartnerR(any());
        verify(fixture.roleService, never()).list(any(Wrapper.class));
        verify(fixture.userRoleService, never()).save(any(SysUserRole.class));
    }
 
    @Test
    void doesNothingForCompanyAccount() {
        // roleType=5 为公司账号,不触发合作商站点级角色同步
        Fixture fixture = new Fixture(2);
        fixture.user.setRoleType(5);
        fixture.user.setObjectId(null);
 
        fixture.service.synchronize(fixture.user);
 
        verify(fixture.siteClient, never()).getPartnerR(any());
        verify(fixture.roleService, never()).list(any(Wrapper.class));
        verify(fixture.userRoleService, never()).save(any(SysUserRole.class));
    }
 
    @Test
    void failsClearlyWhenConfiguredRoleIsMissing() {
        Fixture fixture = new Fixture(3);
        when(fixture.roleService.list(any(Wrapper.class)))
                .thenReturn(Arrays.asList(role(11L, "independent_partner"), role(12L, "site_owner")));
 
        ServiceException exception = assertThrows(ServiceException.class,
                () -> fixture.service.synchronize(fixture.user));
 
        assertEquals("系统角色【service_provider】不存在或已停用,请先配置角色", exception.getMessage());
        verify(fixture.userRoleService, never()).save(any(SysUserRole.class));
    }
 
    private static SysRole role(Long roleId, String roleKey) {
        SysRole role = new SysRole();
        role.setRoleId(roleId);
        role.setRoleKey(roleKey);
        return role;
    }
 
    private static SysUserRole userRole(Long userId, Long roleId) {
        SysUserRole userRole = new SysUserRole();
        userRole.setUserId(userId);
        userRole.setRoleId(roleId);
        return userRole;
    }
 
    private static class Fixture {
        private final SiteClient siteClient = mock(SiteClient.class);
        private final ISysRoleService roleService = mock(ISysRoleService.class);
        private final ISysUserRoleService userRoleService = mock(ISysUserRoleService.class);
        private final PartnerTypeRoleService service =
                new PartnerTypeRoleService(siteClient, roleService, userRoleService);
        private final SysUser user = new SysUser();
 
        private Fixture(Integer partnerType) {
            user.setUserId(100L);
            user.setRoleType(2);
            user.setObjectId(715);
            Partner partner = new Partner();
            partner.setId(715);
            partner.setPartnerType(partnerType);
            when(siteClient.getPartnerR(715)).thenReturn(R.ok(partner));
        }
 
        private void stubBuiltInRoles() {
            when(roleService.list(any(Wrapper.class))).thenReturn(Arrays.asList(
                    role(11L, "independent_partner"),
                    role(12L, "site_owner"),
                    role(13L, "service_provider")
            ));
        }
 
        private String roleKey(Long roleId) {
            if (Long.valueOf(11L).equals(roleId)) {
                return "independent_partner";
            }
            if (Long.valueOf(12L).equals(roleId)) {
                return "site_owner";
            }
            return "service_provider";
        }
    }
}