test(iot): 重构产品控制器测试用例

- 重构了 ProductControllerTest 中的用户权限相关代码
- 优化了不同数据权限范围下的产品列表测试用例
- 修复了部分测试用例中的断言错误
- 删除了 UserNoticeController 中的冗余注解
This commit is contained in:
zhuangtianxiang 2025-01-22 13:45:01 +08:00
parent 0c7301069e
commit efa6698318
2 changed files with 22 additions and 49 deletions

View File

@ -48,7 +48,6 @@ public class UserNoticeController {
* @param query 查询参数 * @param query 查询参数
* @return 分页数据 * @return 分页数据
*/ */
@DataPermission(tableAlias = "su")
@GetMapping("/self") @GetMapping("/self")
public IPage<UserNoticeVo> selfPage(Page<UserNoticeVo> pageParam, @AuthenticationPrincipal UserDetailsImpl userDetails, UserNoticeQuery query) { public IPage<UserNoticeVo> selfPage(Page<UserNoticeVo> pageParam, @AuthenticationPrincipal UserDetailsImpl userDetails, UserNoticeQuery query) {
query.userId = userDetails.id; query.userId = userDetails.id;

View File

@ -55,12 +55,25 @@ public class ProductControllerTest extends MockMvcConfigBase {
product1.setId(1L); product1.setId(1L);
product2 = ProductBuilder.bProduct().name("Product 2").productType("Description 2").deptId(2L).createId(1L).build(); product2 = ProductBuilder.bProduct().name("Product 2").productType("Description 2").deptId(2L).createId(1L).build();
product2.setId(2L); product2.setId(2L);
product3 = ProductBuilder.bProduct().name("Product 3").productType("Description 3").deptId(2L).createId(1L).build(); product3 = ProductBuilder.bProduct().name("Product 3").productType("Description 3").deptId(2L).createId(2L).build();
product3.setId(3L); product3.setId(3L);
product4 = ProductBuilder.bProduct().name("Product 4").productType("Description 4").deptId(3L).createId(2L).build(); product4 = ProductBuilder.bProduct().name("Product 4").productType("Description 4").deptId(3L).createId(2L).build();
product4.setId(4L); product4.setId(4L);
} }
private UserDetailsImpl createUserDetails(DataScopeType dataScopeType) {
Dept dept = new Dept();
dept.setId(2L);
Role role = new Role();
role.setId(50L);
role.setDataScope(dataScopeType);
userDetails.setId(1L);
userDetails.setDept(dept);
userDetails.setRole(role);
userDetails.setDataScopeDeptIds(Set.of(2L));
return userDetails;
}
@Test @Test
void create() throws Exception { void create() throws Exception {
ProductDto dto = new ProductDto(); ProductDto dto = new ProductDto();
@ -81,15 +94,6 @@ public class ProductControllerTest extends MockMvcConfigBase {
@Test @Test
void listWithDataScopeALL() throws Exception { void listWithDataScopeALL() throws Exception {
Dept dept = new Dept();
dept.setId(2L);
Role role = new Role();
role.setId(50L);
role.setDataScope(DataScopeType.DATA_SCOPE_ALL);
userDetails.setId(1L);
userDetails.setDept(dept);
userDetails.setRole(role);
userDetails.setDataScopeDeptIds(Set.of(2L));
// 创建测试数据 // 创建测试数据
List<Product> products = Lists.newArrayList(product1, product2, product3, product4); List<Product> products = Lists.newArrayList(product1, product2, product3, product4);
Page<Product> pageResult = new Page<>(); Page<Product> pageResult = new Page<>();
@ -99,7 +103,7 @@ public class ProductControllerTest extends MockMvcConfigBase {
when(service.page(any(Page.class), any())).thenReturn(pageResult); when(service.page(any(Page.class), any())).thenReturn(pageResult);
// 执行 GET 请求并验证响应 // 执行 GET 请求并验证响应
mockMvc.perform(get("/api/rest/product") mockMvc.perform(get("/api/rest/product")
.with(user(userDetails))) .with(user(createUserDetails(DataScopeType.DATA_SCOPE_ALL))))
.andExpect(status().isOk()) .andExpect(status().isOk())
.andExpect(jsonPath("$.records.length()").value(products.size())) .andExpect(jsonPath("$.records.length()").value(products.size()))
.andExpect(jsonPath("$.total").value(products.size())) .andExpect(jsonPath("$.total").value(products.size()))
@ -113,7 +117,7 @@ public class ProductControllerTest extends MockMvcConfigBase {
.andExpect(jsonPath("$.records[3].deptId").value(3L)) .andExpect(jsonPath("$.records[3].deptId").value(3L))
.andExpect(jsonPath("$.records[0].createId").value(1L)) .andExpect(jsonPath("$.records[0].createId").value(1L))
.andExpect(jsonPath("$.records[1].createId").value(1L)) .andExpect(jsonPath("$.records[1].createId").value(1L))
.andExpect(jsonPath("$.records[2].createId").value(1L)) .andExpect(jsonPath("$.records[2].createId").value(2L))
.andExpect(jsonPath("$.records[3].createId").value(2L)) .andExpect(jsonPath("$.records[3].createId").value(2L))
.andDo(print()); .andDo(print());
// 验证 service.page 方法确实被调用了一次 // 验证 service.page 方法确实被调用了一次
@ -122,15 +126,6 @@ public class ProductControllerTest extends MockMvcConfigBase {
@Test @Test
void listWithDataScopeDept() throws Exception { void listWithDataScopeDept() throws Exception {
Dept dept = new Dept();
dept.setId(2L);
Role role = new Role();
role.setId(50L);
role.setDataScope(DataScopeType.DATA_SCOPE_DEPT);
userDetails.setId(1L);
userDetails.setDept(dept);
userDetails.setRole(role);
userDetails.setDataScopeDeptIds(Set.of(2L));
// 创建测试数据 // 创建测试数据
List<Product> products = Lists.newArrayList(product1, product2, product3); List<Product> products = Lists.newArrayList(product1, product2, product3);
Page<Product> pageResult = new Page<>(); Page<Product> pageResult = new Page<>();
@ -140,7 +135,7 @@ public class ProductControllerTest extends MockMvcConfigBase {
when(service.page(any(Page.class), any())).thenReturn(pageResult); when(service.page(any(Page.class), any())).thenReturn(pageResult);
// 执行 GET 请求并验证响应 // 执行 GET 请求并验证响应
mockMvc.perform(get("/api/rest/product") mockMvc.perform(get("/api/rest/product")
.with(user(userDetails))) .with(user(createUserDetails(DataScopeType.DATA_SCOPE_DEPT))))
.andExpect(status().isOk()) .andExpect(status().isOk())
.andExpect(jsonPath("$.records.length()").value(products.size())) .andExpect(jsonPath("$.records.length()").value(products.size()))
.andExpect(jsonPath("$.total").value(products.size())) .andExpect(jsonPath("$.total").value(products.size()))
@ -152,7 +147,7 @@ public class ProductControllerTest extends MockMvcConfigBase {
.andExpect(jsonPath("$.records[2].deptId").value(2L)) .andExpect(jsonPath("$.records[2].deptId").value(2L))
.andExpect(jsonPath("$.records[0].createId").value(1L)) .andExpect(jsonPath("$.records[0].createId").value(1L))
.andExpect(jsonPath("$.records[1].createId").value(1L)) .andExpect(jsonPath("$.records[1].createId").value(1L))
.andExpect(jsonPath("$.records[2].createId").value(1L)) .andExpect(jsonPath("$.records[2].createId").value(2L))
.andDo(print()); .andDo(print());
// 验证 service.page 方法确实被调用了一次 // 验证 service.page 方法确实被调用了一次
verify(service).page(any(Page.class), any()); verify(service).page(any(Page.class), any());
@ -160,15 +155,6 @@ public class ProductControllerTest extends MockMvcConfigBase {
@Test @Test
void listWithDataScopeDeptAndChild() throws Exception { void listWithDataScopeDeptAndChild() throws Exception {
Dept dept = new Dept();
dept.setId(2L);
Role role = new Role();
role.setId(50L);
role.setDataScope(DataScopeType.DATA_SCOPE_DEPT_AND_CHILD);
userDetails.setId(1L);
userDetails.setDept(dept);
userDetails.setRole(role);
userDetails.setDataScopeDeptIds(Set.of(2L));
// 创建测试数据 // 创建测试数据
List<Product> products = Lists.newArrayList(product1, product2, product3, product4); List<Product> products = Lists.newArrayList(product1, product2, product3, product4);
Page<Product> pageResult = new Page<>(); Page<Product> pageResult = new Page<>();
@ -178,7 +164,7 @@ public class ProductControllerTest extends MockMvcConfigBase {
when(service.page(any(Page.class), any())).thenReturn(pageResult); when(service.page(any(Page.class), any())).thenReturn(pageResult);
// 执行 GET 请求并验证响应 // 执行 GET 请求并验证响应
mockMvc.perform(get("/api/rest/product") mockMvc.perform(get("/api/rest/product")
.with(user(userDetails))) .with(user(createUserDetails(DataScopeType.DATA_SCOPE_DEPT_AND_CHILD))))
.andExpect(status().isOk()) .andExpect(status().isOk())
.andExpect(jsonPath("$.records.length()").value(products.size())) .andExpect(jsonPath("$.records.length()").value(products.size()))
.andExpect(jsonPath("$.total").value(products.size())) .andExpect(jsonPath("$.total").value(products.size()))
@ -192,7 +178,7 @@ public class ProductControllerTest extends MockMvcConfigBase {
.andExpect(jsonPath("$.records[3].deptId").value(3L)) .andExpect(jsonPath("$.records[3].deptId").value(3L))
.andExpect(jsonPath("$.records[0].createId").value(1L)) .andExpect(jsonPath("$.records[0].createId").value(1L))
.andExpect(jsonPath("$.records[1].createId").value(1L)) .andExpect(jsonPath("$.records[1].createId").value(1L))
.andExpect(jsonPath("$.records[2].createId").value(1L)) .andExpect(jsonPath("$.records[2].createId").value(2L))
.andExpect(jsonPath("$.records[3].createId").value(2L)) .andExpect(jsonPath("$.records[3].createId").value(2L))
.andDo(print()); .andDo(print());
// 验证 service.page 方法确实被调用了一次 // 验证 service.page 方法确实被调用了一次
@ -201,17 +187,8 @@ public class ProductControllerTest extends MockMvcConfigBase {
@Test @Test
void listWithDataScopeSelf() throws Exception { void listWithDataScopeSelf() throws Exception {
Dept dept = new Dept();
dept.setId(2L);
Role role = new Role();
role.setId(50L);
role.setDataScope(DataScopeType.DATA_SCOPE_SELF);
userDetails.setId(1L);
userDetails.setDept(dept);
userDetails.setRole(role);
userDetails.setDataScopeDeptIds(Set.of(2L));
// 创建测试数据 // 创建测试数据
List<Product> products = Lists.newArrayList(product1, product2, product3); List<Product> products = Lists.newArrayList(product1, product2);
Page<Product> pageResult = new Page<>(); Page<Product> pageResult = new Page<>();
pageResult.setRecords(products); pageResult.setRecords(products);
pageResult.setTotal(products.size()); pageResult.setTotal(products.size());
@ -219,19 +196,16 @@ public class ProductControllerTest extends MockMvcConfigBase {
when(service.page(any(Page.class), any())).thenReturn(pageResult); when(service.page(any(Page.class), any())).thenReturn(pageResult);
// 执行 GET 请求并验证响应 // 执行 GET 请求并验证响应
mockMvc.perform(get("/api/rest/product") mockMvc.perform(get("/api/rest/product")
.with(user(userDetails))) .with(user(createUserDetails(DataScopeType.DATA_SCOPE_SELF))))
.andExpect(status().isOk()) .andExpect(status().isOk())
.andExpect(jsonPath("$.records.length()").value(products.size())) .andExpect(jsonPath("$.records.length()").value(products.size()))
.andExpect(jsonPath("$.total").value(products.size())) .andExpect(jsonPath("$.total").value(products.size()))
.andExpect(jsonPath("$.records[0].id").value(product1.getId())) .andExpect(jsonPath("$.records[0].id").value(product1.getId()))
.andExpect(jsonPath("$.records[1].id").value(product2.getId())) .andExpect(jsonPath("$.records[1].id").value(product2.getId()))
.andExpect(jsonPath("$.records[2].id").value(product3.getId()))
.andExpect(jsonPath("$.records[0].deptId").value(2L)) .andExpect(jsonPath("$.records[0].deptId").value(2L))
.andExpect(jsonPath("$.records[1].deptId").value(2L)) .andExpect(jsonPath("$.records[1].deptId").value(2L))
.andExpect(jsonPath("$.records[2].deptId").value(2L))
.andExpect(jsonPath("$.records[0].createId").value(1L)) .andExpect(jsonPath("$.records[0].createId").value(1L))
.andExpect(jsonPath("$.records[1].createId").value(1L)) .andExpect(jsonPath("$.records[1].createId").value(1L))
.andExpect(jsonPath("$.records[2].createId").value(1L))
.andDo(print()); .andDo(print());
// 验证 service.page 方法确实被调用了一次 // 验证 service.page 方法确实被调用了一次
verify(service).page(any(Page.class), any()); verify(service).page(any(Page.class), any());