diff --git a/src/main/java/com/zsc/edu/gateway/modules/iot/device/entity/Device.java b/src/main/java/com/zsc/edu/gateway/modules/iot/device/entity/Device.java index 9a48215..2476082 100644 --- a/src/main/java/com/zsc/edu/gateway/modules/iot/device/entity/Device.java +++ b/src/main/java/com/zsc/edu/gateway/modules/iot/device/entity/Device.java @@ -79,10 +79,6 @@ public class Device extends BaseEntity { */ public Long productId; - /** - * 部门ID(权限) - */ - public Long deptId; /** * 所属产品 diff --git a/src/main/java/com/zsc/edu/gateway/modules/iot/tsl/entity/BaseParam.java b/src/main/java/com/zsc/edu/gateway/modules/iot/tsl/entity/BaseParam.java index 10d7c4d..4b57744 100644 --- a/src/main/java/com/zsc/edu/gateway/modules/iot/tsl/entity/BaseParam.java +++ b/src/main/java/com/zsc/edu/gateway/modules/iot/tsl/entity/BaseParam.java @@ -40,4 +40,9 @@ public class BaseParam implements Serializable { */ private String remark; + /** + * 部门ID(权限) + */ + public Long deptId; + } diff --git a/src/test/java/com/zsc/edu/gateway/domain/iot/DeviceBuilder.java b/src/test/java/com/zsc/edu/gateway/domain/iot/DeviceBuilder.java index 5124c50..4491538 100644 --- a/src/test/java/com/zsc/edu/gateway/domain/iot/DeviceBuilder.java +++ b/src/test/java/com/zsc/edu/gateway/domain/iot/DeviceBuilder.java @@ -1,9 +1,10 @@ package com.zsc.edu.gateway.domain.iot; +import com.zsc.edu.gateway.domain.system.BaseEntityBuilder; import com.zsc.edu.gateway.modules.iot.device.entity.Device; import com.zsc.edu.gateway.modules.system.entity.BaseEntity; -public class DeviceBuilder extends BaseEntity { +public class DeviceBuilder extends BaseEntityBuilder { private String name; private Device.Status state = Device.Status.UNACTIVATED; diff --git a/src/test/java/com/zsc/edu/gateway/domain/iot/ProductBuilder.java b/src/test/java/com/zsc/edu/gateway/domain/iot/ProductBuilder.java index cf85f3f..621e2ee 100644 --- a/src/test/java/com/zsc/edu/gateway/domain/iot/ProductBuilder.java +++ b/src/test/java/com/zsc/edu/gateway/domain/iot/ProductBuilder.java @@ -1,11 +1,13 @@ package com.zsc.edu.gateway.domain.iot; +import com.zsc.edu.gateway.domain.system.BaseEntityBuilder; import com.zsc.edu.gateway.modules.iot.product.entity.Product; -public class ProductBuilder extends BaseParamBuilder { +public class ProductBuilder extends BaseEntityBuilder { public String name; public String productType; public String model; + public Long deptId; public Product.LinkType link; public static ProductBuilder bProduct() { @@ -17,6 +19,16 @@ public class ProductBuilder extends BaseParamBuilder { return this; } + public ProductBuilder deptId(Long deptId) { + this.deptId = deptId; + return this; + } + + public ProductBuilder createId(Long createId) { + this.createId = createId; + return this; + } + public ProductBuilder productType(String productType) { this.productType = productType; return this; @@ -38,6 +50,8 @@ public class ProductBuilder extends BaseParamBuilder { product.setProductType(productType); product.setModel(model); product.setLink(link); + product.setDeptId(deptId); + product.setCreateId(createId); product.setParams(null); return product; } diff --git a/src/test/java/com/zsc/edu/gateway/domain/system/BaseEntityBuilder.java b/src/test/java/com/zsc/edu/gateway/domain/system/BaseEntityBuilder.java index 2a7cb5c..b7d4aff 100644 --- a/src/test/java/com/zsc/edu/gateway/domain/system/BaseEntityBuilder.java +++ b/src/test/java/com/zsc/edu/gateway/domain/system/BaseEntityBuilder.java @@ -12,11 +12,13 @@ public class BaseEntityBuilder { public String remark; public LocalDateTime createAt; public LocalDateTime updateAt; + public Long createId; public BaseEntityBuilder() { remark = randomAlphabetic(5); createAt = LocalDateTime.now(); updateAt = LocalDateTime.now(); + createId = 1L; } } diff --git a/src/test/java/com/zsc/edu/gateway/rest/iot/ProductControllerTest.java b/src/test/java/com/zsc/edu/gateway/rest/iot/ProductControllerTest.java index 8bb520f..7e89efa 100644 --- a/src/test/java/com/zsc/edu/gateway/rest/iot/ProductControllerTest.java +++ b/src/test/java/com/zsc/edu/gateway/rest/iot/ProductControllerTest.java @@ -5,10 +5,14 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.fasterxml.jackson.databind.ObjectMapper; import com.zsc.edu.gateway.MockMvcConfigBase; import com.zsc.edu.gateway.domain.iot.ProductBuilder; +import com.zsc.edu.gateway.framework.mybatisplus.DataScopeType; +import com.zsc.edu.gateway.framework.security.UserDetailsImpl; import com.zsc.edu.gateway.modules.iot.product.controller.ProductController; import com.zsc.edu.gateway.modules.iot.product.dto.ProductDto; import com.zsc.edu.gateway.modules.iot.product.entity.Product; import com.zsc.edu.gateway.modules.iot.product.service.ProductService; +import com.zsc.edu.gateway.modules.system.entity.Dept; +import com.zsc.edu.gateway.modules.system.entity.Role; import org.assertj.core.util.Lists; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; @@ -19,6 +23,7 @@ import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.http.MediaType; import java.util.List; +import java.util.Set; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyLong; @@ -28,6 +33,7 @@ import static org.springframework.security.test.web.servlet.request.SecurityMock import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @WebMvcTest(ProductController.class) @@ -35,6 +41,8 @@ public class ProductControllerTest extends MockMvcConfigBase { @Spy private static Product product1; private static Product product2; + private static Product product3; + private static Product product4; @MockBean private ProductService service; @@ -43,10 +51,14 @@ public class ProductControllerTest extends MockMvcConfigBase { @BeforeAll static void beforeAll() { - product1 = ProductBuilder.bProduct().name("Product 1").productType("Description 1").build(); + product1 = ProductBuilder.bProduct().name("Product 1").productType("Description 1").deptId(2L).createId(1L).build(); product1.setId(1L); - product2 = ProductBuilder.bProduct().name("Product 2").productType("Description 2").build(); + product2 = ProductBuilder.bProduct().name("Product 2").productType("Description 2").deptId(2L).createId(1L).build(); product2.setId(2L); + product3 = ProductBuilder.bProduct().name("Product 3").productType("Description 3").deptId(2L).createId(1L).build(); + product3.setId(3L); + product4 = ProductBuilder.bProduct().name("Product 4").productType("Description 4").deptId(3L).createId(2L).build(); + product4.setId(4L); } @Test @@ -68,13 +80,161 @@ public class ProductControllerTest extends MockMvcConfigBase { } @Test - void list() throws Exception { - List products = Lists.newArrayList(product1, product2); - when(service.page(any(Page.class), any(LambdaQueryWrapper.class))).thenReturn(new Page().setRecords(products).setTotal((long) products.size())); - mockMvc.perform(get("/api/rest/product").with(user(userDetails))) + 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 products = Lists.newArrayList(product1, product2, product3, product4); + Page pageResult = new Page<>(); + pageResult.setRecords(products); + pageResult.setTotal(products.size()); + // 模拟 service.page 方法的返回值 + when(service.page(any(Page.class), any())).thenReturn(pageResult); + // 执行 GET 请求并验证响应 + mockMvc.perform(get("/api/rest/product") + .with(user(userDetails))) .andExpect(status().isOk()) + .andExpect(jsonPath("$.records.length()").value(products.size())) + .andExpect(jsonPath("$.total").value(products.size())) + .andExpect(jsonPath("$.records[0].id").value(product1.getId())) + .andExpect(jsonPath("$.records[1].id").value(product2.getId())) + .andExpect(jsonPath("$.records[2].id").value(product3.getId())) + .andExpect(jsonPath("$.records[3].id").value(product4.getId())) + .andExpect(jsonPath("$.records[0].deptId").value(2L)) + .andExpect(jsonPath("$.records[1].deptId").value(2L)) + .andExpect(jsonPath("$.records[2].deptId").value(2L)) + .andExpect(jsonPath("$.records[3].deptId").value(3L)) + .andExpect(jsonPath("$.records[0].createId").value(1L)) + .andExpect(jsonPath("$.records[1].createId").value(1L)) + .andExpect(jsonPath("$.records[2].createId").value(1L)) + .andExpect(jsonPath("$.records[3].createId").value(2L)) .andDo(print()); - verify(service).page(any(Page.class), any(LambdaQueryWrapper.class)); + // 验证 service.page 方法确实被调用了一次 + verify(service).page(any(Page.class), any()); + } + + @Test + 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 products = Lists.newArrayList(product1, product2, product3); + Page pageResult = new Page<>(); + pageResult.setRecords(products); + pageResult.setTotal(products.size()); + // 模拟 service.page 方法的返回值 + when(service.page(any(Page.class), any())).thenReturn(pageResult); + // 执行 GET 请求并验证响应 + mockMvc.perform(get("/api/rest/product") + .with(user(userDetails))) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.records.length()").value(products.size())) + .andExpect(jsonPath("$.total").value(products.size())) + .andExpect(jsonPath("$.records[0].id").value(product1.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[1].deptId").value(2L)) + .andExpect(jsonPath("$.records[2].deptId").value(2L)) + .andExpect(jsonPath("$.records[0].createId").value(1L)) + .andExpect(jsonPath("$.records[1].createId").value(1L)) + .andExpect(jsonPath("$.records[2].createId").value(1L)) + .andDo(print()); + // 验证 service.page 方法确实被调用了一次 + verify(service).page(any(Page.class), any()); + } + + @Test + 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 products = Lists.newArrayList(product1, product2, product3, product4); + Page pageResult = new Page<>(); + pageResult.setRecords(products); + pageResult.setTotal(products.size()); + // 模拟 service.page 方法的返回值 + when(service.page(any(Page.class), any())).thenReturn(pageResult); + // 执行 GET 请求并验证响应 + mockMvc.perform(get("/api/rest/product") + .with(user(userDetails))) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.records.length()").value(products.size())) + .andExpect(jsonPath("$.total").value(products.size())) + .andExpect(jsonPath("$.records[0].id").value(product1.getId())) + .andExpect(jsonPath("$.records[1].id").value(product2.getId())) + .andExpect(jsonPath("$.records[2].id").value(product3.getId())) + .andExpect(jsonPath("$.records[3].id").value(product4.getId())) + .andExpect(jsonPath("$.records[0].deptId").value(2L)) + .andExpect(jsonPath("$.records[1].deptId").value(2L)) + .andExpect(jsonPath("$.records[2].deptId").value(2L)) + .andExpect(jsonPath("$.records[3].deptId").value(3L)) + .andExpect(jsonPath("$.records[0].createId").value(1L)) + .andExpect(jsonPath("$.records[1].createId").value(1L)) + .andExpect(jsonPath("$.records[2].createId").value(1L)) + .andExpect(jsonPath("$.records[3].createId").value(2L)) + .andDo(print()); + // 验证 service.page 方法确实被调用了一次 + verify(service).page(any(Page.class), any()); + } + + @Test + 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 products = Lists.newArrayList(product1, product2, product3); + Page pageResult = new Page<>(); + pageResult.setRecords(products); + pageResult.setTotal(products.size()); + // 模拟 service.page 方法的返回值 + when(service.page(any(Page.class), any())).thenReturn(pageResult); + // 执行 GET 请求并验证响应 + mockMvc.perform(get("/api/rest/product") + .with(user(userDetails))) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.records.length()").value(products.size())) + .andExpect(jsonPath("$.total").value(products.size())) + .andExpect(jsonPath("$.records[0].id").value(product1.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[1].deptId").value(2L)) + .andExpect(jsonPath("$.records[2].deptId").value(2L)) + .andExpect(jsonPath("$.records[0].createId").value(1L)) + .andExpect(jsonPath("$.records[1].createId").value(1L)) + .andExpect(jsonPath("$.records[2].createId").value(1L)) + .andDo(print()); + // 验证 service.page 方法确实被调用了一次 + verify(service).page(any(Page.class), any()); } @Test