|
@@ -0,0 +1,90 @@
|
|
|
|
|
+package com.payment.platform.module.system.role.service;
|
|
|
|
|
+
|
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.Wrapper;
|
|
|
|
|
+import com.fasterxml.jackson.databind.JsonNode;
|
|
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
|
+import com.payment.platform.common.config.JacksonConfig;
|
|
|
|
|
+import com.payment.platform.module.system.menu.dto.MenuVO;
|
|
|
|
|
+import com.payment.platform.module.system.role.dto.RoleVO;
|
|
|
|
|
+import com.payment.platform.module.system.role.entity.RoleEntity;
|
|
|
|
|
+import com.payment.platform.module.system.role.mapper.RoleMapper;
|
|
|
|
|
+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 java.util.List;
|
|
|
|
|
+import java.util.Map;
|
|
|
|
|
+
|
|
|
|
|
+import static org.junit.jupiter.api.Assertions.*;
|
|
|
|
|
+import static org.mockito.ArgumentMatchers.any;
|
|
|
|
|
+import static org.mockito.Mockito.*;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 角色详情要带上「这个角色已经授了哪些菜单/部门」。
|
|
|
|
|
+ *
|
|
|
|
|
+ * 消费方是权限分配抽屉(frontend PermissonDrawer.vue 的 init):它读
|
|
|
|
|
+ * `detail.menus[].id` 与 `detail.depts[].id`,再 setCheckedKeys 回勾。
|
|
|
|
|
+ * 详情不返回这两个数组时,树**全未勾选**,而保存走的是「先删光再插入」
|
|
|
|
|
+ * (RoleMapper.deleteRoleMenusByRoleIds → insertRoleMenus)——
|
|
|
|
|
+ * 于是「打开抽屉、什么都没改、点确定」会把该角色的授权**清空**。
|
|
|
|
|
+ * 所以这两个字段空了不是显示问题,是数据丢失。
|
|
|
|
|
+ *
|
|
|
|
|
+ * 断言一律走**线上那支 ObjectMapper**(JacksonConfig,SNAKE_CASE)序列化后的 JSON:
|
|
|
|
|
+ * 前端读的是键名(parent_id / data_scope),不是 Java 字段名。
|
|
|
|
|
+ */
|
|
|
|
|
+@ExtendWith(MockitoExtension.class)
|
|
|
|
|
+class RoleServiceTest {
|
|
|
|
|
+
|
|
|
|
|
+ @Mock
|
|
|
|
|
+ private RoleMapper roleMapper;
|
|
|
|
|
+
|
|
|
|
|
+ @InjectMocks
|
|
|
|
|
+ private RoleService roleService;
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void getDetail_carriesTheRolesOwnMenusAndDepts() throws Exception {
|
|
|
|
|
+ RoleEntity role = new RoleEntity();
|
|
|
|
|
+ role.setId(2L);
|
|
|
|
|
+ role.setName("租户管理员");
|
|
|
|
|
+ when(roleMapper.selectById(2L)).thenReturn(role);
|
|
|
|
|
+
|
|
|
|
|
+ MenuVO shopPage = new MenuVO();
|
|
|
|
|
+ shopPage.setId(31L);
|
|
|
|
|
+ shopPage.setName("门店管理");
|
|
|
|
|
+ shopPage.setParentId(9L);
|
|
|
|
|
+ when(roleMapper.selectMenusByRoleId(2L)).thenReturn(List.of(shopPage));
|
|
|
|
|
+ when(roleMapper.selectDeptsByRoleId(2L))
|
|
|
|
|
+ .thenReturn(List.of(Map.of("id", 7L, "name", "总部", "parent_id", 3L)));
|
|
|
|
|
+
|
|
|
|
|
+ JsonNode json = readJson(roleService.getDetail(2L));
|
|
|
|
|
+
|
|
|
|
|
+ JsonNode menus = json.get("menus");
|
|
|
|
|
+ assertNotNull(menus, "detail 少了 menus,抽屉只能全不勾。实测: " + json);
|
|
|
|
|
+ assertEquals(1, menus.size(), "menus 应当是**已授权**的那一条,实测: " + json);
|
|
|
|
|
+ assertEquals(31L, menus.get(0).get("id").asLong(), "抽屉按 menus[].id 回勾,实测: " + json);
|
|
|
|
|
+
|
|
|
|
|
+ JsonNode depts = json.get("depts");
|
|
|
|
|
+ assertNotNull(depts, "detail 少了 depts,实测: " + json);
|
|
|
|
|
+ assertEquals(7L, depts.get(0).get("id").asLong(), "抽屉按 depts[].id 回勾,实测: " + json);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /** 列表不带授权明细:那是每行两次查询的 N+1,抽屉只用 detail。 */
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void getList_doesNotLoadMenusPerRole() {
|
|
|
|
|
+ when(roleMapper.selectList(any(Wrapper.class))).thenReturn(List.of(new RoleEntity()));
|
|
|
|
|
+
|
|
|
|
|
+ List<RoleVO> list = roleService.getList(null);
|
|
|
|
|
+
|
|
|
|
|
+ assertEquals(1, list.size());
|
|
|
|
|
+ assertTrue(list.get(0).getMenus().isEmpty(), "列表不该带授权明细");
|
|
|
|
|
+ verify(roleMapper, never()).selectMenusByRoleId(any());
|
|
|
|
|
+ verify(roleMapper, never()).selectDeptsByRoleId(any());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private JsonNode readJson(Object value) throws Exception {
|
|
|
|
|
+ ObjectMapper mapper = new JacksonConfig().objectMapper();
|
|
|
|
|
+ return mapper.readTree(mapper.writeValueAsString(value));
|
|
|
|
|
+ }
|
|
|
|
|
+}
|