diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/invoker/ProjectServiceInvoker.java b/backend/framework/sdk/src/main/java/io/metersphere/sdk/invoker/ProjectServiceInvoker.java index b284cad0a4..e5cca59b6f 100644 --- a/backend/framework/sdk/src/main/java/io/metersphere/sdk/invoker/ProjectServiceInvoker.java +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/invoker/ProjectServiceInvoker.java @@ -1,6 +1,7 @@ package io.metersphere.sdk.invoker; import io.metersphere.sdk.service.CleanupProjectResourceService; +import io.metersphere.sdk.service.CreateProjectResourceService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @@ -10,9 +11,13 @@ import java.util.List; public class ProjectServiceInvoker { private final List cleanupProjectResourceServices; + private final List createProjectResourceServices; + + @Autowired - public ProjectServiceInvoker(List services) { + public ProjectServiceInvoker(List services, List createProjectResourceServices) { this.cleanupProjectResourceServices = services; + this.createProjectResourceServices = createProjectResourceServices; } public void invokeServices(String projectId) { @@ -20,4 +25,10 @@ public class ProjectServiceInvoker { service.deleteResources(projectId); } } + + public void invokeCreateServices(String projectId) { + for (CreateProjectResourceService service : createProjectResourceServices) { + service.createResources(projectId); + } + } } diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/service/CreateProjectResourceService.java b/backend/framework/sdk/src/main/java/io/metersphere/sdk/service/CreateProjectResourceService.java new file mode 100644 index 0000000000..da8fdd45e9 --- /dev/null +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/service/CreateProjectResourceService.java @@ -0,0 +1,8 @@ +package io.metersphere.sdk.service; + +/** + * 新增项目资源 + */ +public interface CreateProjectResourceService { + void createResources(String projectId); +} diff --git a/backend/services/project-management/src/main/java/io/metersphere/project/dto/ProjectRobotDTO.java b/backend/services/project-management/src/main/java/io/metersphere/project/dto/ProjectRobotDTO.java index 8ab8546b4f..f776c6d9d0 100644 --- a/backend/services/project-management/src/main/java/io/metersphere/project/dto/ProjectRobotDTO.java +++ b/backend/services/project-management/src/main/java/io/metersphere/project/dto/ProjectRobotDTO.java @@ -1,15 +1,22 @@ package io.metersphere.project.dto; +import io.metersphere.validation.groups.Created; +import io.metersphere.validation.groups.Updated; import io.swagger.v3.oas.annotations.media.Schema; +import jakarta.validation.constraints.NotBlank; import lombok.Data; +import lombok.EqualsAndHashCode; @Data +@EqualsAndHashCode(callSuper = false) public class ProjectRobotDTO { @Schema(description = "id", requiredMode = Schema.RequiredMode.REQUIRED) + @NotBlank(message = "{project_robot.id.not_blank}", groups = {Updated.class}) private String id; @Schema(description = "名称", requiredMode = Schema.RequiredMode.REQUIRED) + @NotBlank(message = "{project_robot.name.not_blank}", groups = {Created.class, Updated.class}) private String name; @Schema(description = "所属平台(飞书,钉钉,企业微信,自定义)", requiredMode = Schema.RequiredMode.REQUIRED) @@ -34,5 +41,6 @@ public class ProjectRobotDTO { private String description; @Schema(description = "项目id", requiredMode = Schema.RequiredMode.REQUIRED) + @NotBlank(message = "{project_robot.project_id.not_blank}", groups = {Created.class, Updated.class}) private String projectId; } diff --git a/backend/services/project-management/src/main/java/io/metersphere/project/request/ProjectRobotRequest.java b/backend/services/project-management/src/main/java/io/metersphere/project/request/ProjectRobotRequest.java index d12301940f..deee76e8b3 100644 --- a/backend/services/project-management/src/main/java/io/metersphere/project/request/ProjectRobotRequest.java +++ b/backend/services/project-management/src/main/java/io/metersphere/project/request/ProjectRobotRequest.java @@ -6,6 +6,10 @@ import lombok.Data; @Data public class ProjectRobotRequest extends BasePageRequest { + + @Schema(description = "是否禁用") + private String projectId; + @Schema(description = "是否禁用") private Boolean enable; } diff --git a/backend/services/project-management/src/main/java/io/metersphere/project/service/CreateRobotResourceService.java b/backend/services/project-management/src/main/java/io/metersphere/project/service/CreateRobotResourceService.java new file mode 100644 index 0000000000..98e195436c --- /dev/null +++ b/backend/services/project-management/src/main/java/io/metersphere/project/service/CreateRobotResourceService.java @@ -0,0 +1,47 @@ +package io.metersphere.project.service; + +import io.metersphere.project.domain.ProjectRobot; +import io.metersphere.project.enums.ProjectRobotPlatform; +import io.metersphere.project.mapper.ProjectRobotMapper; +import io.metersphere.sdk.service.CreateProjectResourceService; +import io.metersphere.sdk.uid.UUID; +import jakarta.annotation.Resource; +import org.springframework.stereotype.Component; + +import java.util.ArrayList; +import java.util.List; + +@Component +public class CreateRobotResourceService implements CreateProjectResourceService { + + @Resource + private ProjectRobotMapper robotMapper; + + @Override + public void createResources(String projectId) { + List list = new ArrayList<>(); + ProjectRobot projectRobot = new ProjectRobot(); + projectRobot.setId(UUID.randomUUID().toString()); + projectRobot.setProjectId(projectId); + projectRobot.setName("站内信"); + projectRobot.setPlatform(ProjectRobotPlatform.IN_SITE.toString()); + projectRobot.setWebhook("NONE"); + projectRobot.setCreateUser("admin"); + projectRobot.setCreateTime(System.currentTimeMillis()); + projectRobot.setUpdateUser("admin"); + projectRobot.setUpdateTime(System.currentTimeMillis()); + list.add(projectRobot); + ProjectRobot projectRobotMail = new ProjectRobot(); + projectRobotMail.setId(UUID.randomUUID().toString()); + projectRobotMail.setProjectId(projectId); + projectRobotMail.setName("邮件"); + projectRobotMail.setPlatform(ProjectRobotPlatform.MAIL.toString()); + projectRobotMail.setWebhook("NONE"); + projectRobotMail.setCreateUser("admin"); + projectRobotMail.setCreateTime(System.currentTimeMillis()); + projectRobotMail.setUpdateUser("admin"); + projectRobotMail.setUpdateTime(System.currentTimeMillis()); + list.add(projectRobotMail); + robotMapper.batchInsert(list); + } +} diff --git a/backend/services/project-management/src/main/java/io/metersphere/project/service/ProjectRobotService.java b/backend/services/project-management/src/main/java/io/metersphere/project/service/ProjectRobotService.java index 67aa336f5f..291ae3b991 100644 --- a/backend/services/project-management/src/main/java/io/metersphere/project/service/ProjectRobotService.java +++ b/backend/services/project-management/src/main/java/io/metersphere/project/service/ProjectRobotService.java @@ -16,6 +16,7 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.List; + import io.metersphere.sdk.uid.UUID; @Service @@ -95,6 +96,10 @@ public class ProjectRobotService { criteria.andEnableEqualTo(request.getEnable()); } + if (request.getProjectId() != null) { + criteria.andProjectIdEqualTo(request.getProjectId()); + } + projectExample.setOrderByClause("create_time desc"); return robotMapper.selectByExample(projectExample); diff --git a/backend/services/project-management/src/test/java/io/metersphere/project/controller/CreateRobotResourceTests.java b/backend/services/project-management/src/test/java/io/metersphere/project/controller/CreateRobotResourceTests.java new file mode 100644 index 0000000000..bd88c1a191 --- /dev/null +++ b/backend/services/project-management/src/test/java/io/metersphere/project/controller/CreateRobotResourceTests.java @@ -0,0 +1,64 @@ +package io.metersphere.project.controller; + +import io.metersphere.project.domain.ProjectRobot; +import io.metersphere.project.request.ProjectRobotRequest; +import io.metersphere.sdk.base.BaseTest; +import io.metersphere.sdk.constants.SessionConstants; +import io.metersphere.sdk.controller.handler.ResultHolder; +import io.metersphere.sdk.invoker.ProjectServiceInvoker; +import io.metersphere.sdk.util.JSON; +import io.metersphere.sdk.util.Pager; +import org.junit.jupiter.api.*; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.http.MediaType; +import org.springframework.test.web.servlet.MvcResult; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; + +import java.nio.charset.StandardCharsets; +import java.util.List; + +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + + +@SpringBootTest +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) +@AutoConfigureMockMvc +public class CreateRobotResourceTests extends BaseTest { + private final ProjectServiceInvoker serviceInvoker; + + public static final String ROBOT_LIST = "/project/robot/list/page"; + + @Autowired + public CreateRobotResourceTests(ProjectServiceInvoker serviceInvoker) { + this.serviceInvoker = serviceInvoker; + } + + @Test + @Order(1) + public void testCleanupResource() throws Exception { + serviceInvoker.invokeCreateServices("test"); + ProjectRobotRequest request = new ProjectRobotRequest(); + request.setCurrent(1); + request.setPageSize(5); + request.setProjectId("test"); + Pager sortPageDataAfter = getPager(request); + List projectRobotAfters = JSON.parseArray(JSON.toJSONString(sortPageDataAfter.getList()), ProjectRobot.class); + Assertions.assertEquals(2, projectRobotAfters.size()); + } + + private Pager getPager(ProjectRobotRequest request) throws Exception { + MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.post(ROBOT_LIST) + .header(SessionConstants.HEADER_TOKEN, sessionId) + .header(SessionConstants.CSRF_TOKEN, csrfToken) + .content(JSON.toJSONString(request)) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(content().contentType(MediaType.APPLICATION_JSON)).andReturn(); + String sortData = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8); + ResultHolder sortHolder = JSON.parseObject(sortData, ResultHolder.class); + return JSON.parseObject(JSON.toJSONString(sortHolder.getData()), Pager.class); + } +} diff --git a/backend/services/project-management/src/test/java/io/metersphere/project/controller/ProjectRobotControllerTests.java b/backend/services/project-management/src/test/java/io/metersphere/project/controller/ProjectRobotControllerTests.java index 8a92b65d6f..b1e7596b9c 100644 --- a/backend/services/project-management/src/test/java/io/metersphere/project/controller/ProjectRobotControllerTests.java +++ b/backend/services/project-management/src/test/java/io/metersphere/project/controller/ProjectRobotControllerTests.java @@ -183,7 +183,7 @@ public class ProjectRobotControllerTests extends BaseTest { setCustomRobot("测试ID空失败"); ProjectRobot projectRobot = getRobot("测试ID空失败"); projectRobot.setId(null); - checkUpdate(projectRobot, "测试ID空失败", status().is5xxServerError()); + checkUpdate(projectRobot, "测试ID空失败", status().isBadRequest()); } @Test @@ -295,6 +295,21 @@ public class ProjectRobotControllerTests extends BaseTest { @Test @Order(19) + void getListSuccessByProject() throws Exception { + setCustomRobot("测试集合"); + ProjectRobotRequest request = new ProjectRobotRequest(); + request.setCurrent(1); + request.setPageSize(5); + request.setProjectId("test_project"); + request.setEnable(true); + Pager sortPageData = getPager(request); + List projectRobots = JSON.parseArray(JSON.toJSONString(sortPageData.getList()), ProjectRobot.class); + ProjectRobot projectRobot = projectRobots.get(0); + Assertions.assertTrue(projectRobot.getEnable()); + } + + @Test + @Order(20) void setEnableSuccess() throws Exception { setCustomRobot("测试Enable"); ProjectRobot projectRobot = getRobot("测试Enable"); @@ -309,7 +324,7 @@ public class ProjectRobotControllerTests extends BaseTest { } @Test - @Order(20) + @Order(21) void setEnableFail() throws Exception { mockMvc.perform(MockMvcRequestBuilders.get(ROBOT_ENABLE+"/no_id") .header(SessionConstants.HEADER_TOKEN, sessionId) diff --git a/backend/services/system-setting/src/test/java/io/metersphere/system/controller/CreateResourceTests.java b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/CreateResourceTests.java new file mode 100644 index 0000000000..3268f3196b --- /dev/null +++ b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/CreateResourceTests.java @@ -0,0 +1,35 @@ +package io.metersphere.system.controller; + +import io.metersphere.sdk.invoker.ProjectServiceInvoker; +import io.metersphere.system.mock.CreateTestResourceService; +import jakarta.annotation.Resource; +import org.junit.jupiter.api.MethodOrderer; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; + + +@SpringBootTest +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) +@AutoConfigureMockMvc +public class CreateResourceTests { + private final ProjectServiceInvoker serviceInvoker; + @Resource + private CreateTestResourceService createTestResourceService; + + @Autowired + public CreateResourceTests(ProjectServiceInvoker serviceInvoker) { + this.serviceInvoker = serviceInvoker; + } + + @Test + @Order(1) + public void testCreateResource() { + serviceInvoker.invokeCreateServices("test"); + createTestResourceService.createResources("test"); + } + +} diff --git a/backend/services/system-setting/src/test/java/io/metersphere/system/mock/CreateTestResourceService.java b/backend/services/system-setting/src/test/java/io/metersphere/system/mock/CreateTestResourceService.java new file mode 100644 index 0000000000..328b515143 --- /dev/null +++ b/backend/services/system-setting/src/test/java/io/metersphere/system/mock/CreateTestResourceService.java @@ -0,0 +1,15 @@ +package io.metersphere.system.mock; + +import io.metersphere.sdk.service.CreateProjectResourceService; +import io.metersphere.sdk.util.LogUtils; +import org.springframework.stereotype.Component; + +@Component +public class CreateTestResourceService implements CreateProjectResourceService { + + @Override + public void createResources(String projectId) { + LogUtils.info("默认增加当前项目[" + projectId + "]TEST资源"); + } + +}