From eb3e76b45e8965dad35099623231fb32e3ae55a7 Mon Sep 17 00:00:00 2001 From: wxg0103 <727495428@qq.com> Date: Mon, 25 Sep 2023 17:09:08 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E9=A1=B9=E7=9B=AE=E7=AE=A1=E7=90=86):?= =?UTF-8?q?=20=E5=AF=BC=E5=87=BA=E7=8E=AF=E5=A2=83=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E5=85=A8=E9=80=89=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/EnvironmentController.java | 12 ++- .../dto/environment/EnvironmentDTO.java | 20 ++++ .../dto/environment/EnvironmentExportDTO.java | 19 ++++ .../project/mapper/ExtEnvironmentMapper.java | 11 +++ .../project/mapper/ExtEnvironmentMapper.xml | 24 +++++ .../project/service/EnvironmentService.java | 92 +++++++++++-------- .../EnvironmentControllerTests.java | 67 +++++++++----- 7 files changed, 176 insertions(+), 69 deletions(-) create mode 100644 backend/services/project-management/src/main/java/io/metersphere/project/dto/environment/EnvironmentDTO.java create mode 100644 backend/services/project-management/src/main/java/io/metersphere/project/dto/environment/EnvironmentExportDTO.java create mode 100644 backend/services/project-management/src/main/java/io/metersphere/project/mapper/ExtEnvironmentMapper.java create mode 100644 backend/services/project-management/src/main/java/io/metersphere/project/mapper/ExtEnvironmentMapper.xml diff --git a/backend/services/project-management/src/main/java/io/metersphere/project/controller/EnvironmentController.java b/backend/services/project-management/src/main/java/io/metersphere/project/controller/EnvironmentController.java index ebd86ede1f..7c03f60b63 100644 --- a/backend/services/project-management/src/main/java/io/metersphere/project/controller/EnvironmentController.java +++ b/backend/services/project-management/src/main/java/io/metersphere/project/controller/EnvironmentController.java @@ -1,5 +1,7 @@ package io.metersphere.project.controller; +import io.metersphere.project.dto.environment.EnvironmentDTO; +import io.metersphere.project.dto.environment.EnvironmentExportDTO; import io.metersphere.project.dto.environment.EnvironmentRequest; import io.metersphere.project.dto.environment.datasource.DataSource; import io.metersphere.project.dto.environment.ssl.KeyStoreEntry; @@ -36,11 +38,11 @@ public class EnvironmentController { @Resource private CommandService commandService; - @GetMapping("/list/{projectId}") + @PostMapping("/list") @Operation(summary = "项目管理-环境-环境目录-列表") @RequiresPermissions(PermissionConstants.PROJECT_ENVIRONMENT_READ) - public List list(@PathVariable String projectId) { - return environmentService.list(projectId); + public List list(@Validated @RequestBody EnvironmentDTO request) { + return environmentService.list(request); } @GetMapping("/get/{environmentId}") @@ -101,8 +103,8 @@ public class EnvironmentController { @PostMapping("/export") @RequiresPermissions(PermissionConstants.PROJECT_ENVIRONMENT_READ_EXPORT) @Operation(summary = "项目管理-环境-环境目录-导出") - public String export(@RequestBody List environmentIds) { - return environmentService.export(environmentIds); + public String export(@Validated @RequestBody EnvironmentExportDTO request) { + return environmentService.export(request); } @PostMapping(value = "/get/entry") diff --git a/backend/services/project-management/src/main/java/io/metersphere/project/dto/environment/EnvironmentDTO.java b/backend/services/project-management/src/main/java/io/metersphere/project/dto/environment/EnvironmentDTO.java new file mode 100644 index 0000000000..01c62c86c8 --- /dev/null +++ b/backend/services/project-management/src/main/java/io/metersphere/project/dto/environment/EnvironmentDTO.java @@ -0,0 +1,20 @@ +package io.metersphere.project.dto.environment; + +import io.swagger.v3.oas.annotations.media.Schema; +import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.Size; +import lombok.Data; + +@Data +public class EnvironmentDTO { + + @Schema(description = "项目ID", requiredMode = Schema.RequiredMode.REQUIRED) + @NotBlank(message = "{project_application.project_id.not_blank}") + @Size(min = 1, max = 50, message = "{project_parameters.project_id.length_range}") + private String projectId; + @Schema(description = "关键字") + private String keyword; + + + +} diff --git a/backend/services/project-management/src/main/java/io/metersphere/project/dto/environment/EnvironmentExportDTO.java b/backend/services/project-management/src/main/java/io/metersphere/project/dto/environment/EnvironmentExportDTO.java new file mode 100644 index 0000000000..0cf2741d6a --- /dev/null +++ b/backend/services/project-management/src/main/java/io/metersphere/project/dto/environment/EnvironmentExportDTO.java @@ -0,0 +1,19 @@ +package io.metersphere.project.dto.environment; + +import io.metersphere.sdk.dto.TableBatchProcessDTO; +import io.swagger.v3.oas.annotations.media.Schema; +import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.Size; +import lombok.Data; + +@Data +public class EnvironmentExportDTO extends TableBatchProcessDTO { + + @Schema(description = "项目ID", requiredMode = Schema.RequiredMode.REQUIRED) + @NotBlank(message = "{project_application.project_id.not_blank}") + @Size(min = 1, max = 50, message = "{project_parameters.project_id.length_range}") + private String projectId; + + + +} diff --git a/backend/services/project-management/src/main/java/io/metersphere/project/mapper/ExtEnvironmentMapper.java b/backend/services/project-management/src/main/java/io/metersphere/project/mapper/ExtEnvironmentMapper.java new file mode 100644 index 0000000000..fb8421e27f --- /dev/null +++ b/backend/services/project-management/src/main/java/io/metersphere/project/mapper/ExtEnvironmentMapper.java @@ -0,0 +1,11 @@ +package io.metersphere.project.mapper; + +import io.metersphere.sdk.domain.Environment; +import org.apache.ibatis.annotations.Param; + +import java.util.List; + +public interface ExtEnvironmentMapper { + + List selectByKeyword(@Param("keyword") String keyword, @Param("selectId") boolean selectId , @Param("projectId") String projectId); +} diff --git a/backend/services/project-management/src/main/java/io/metersphere/project/mapper/ExtEnvironmentMapper.xml b/backend/services/project-management/src/main/java/io/metersphere/project/mapper/ExtEnvironmentMapper.xml new file mode 100644 index 0000000000..5b6bbcb07e --- /dev/null +++ b/backend/services/project-management/src/main/java/io/metersphere/project/mapper/ExtEnvironmentMapper.xml @@ -0,0 +1,24 @@ + + + + + + \ No newline at end of file diff --git a/backend/services/project-management/src/main/java/io/metersphere/project/service/EnvironmentService.java b/backend/services/project-management/src/main/java/io/metersphere/project/service/EnvironmentService.java index 2884867033..883a7549a0 100644 --- a/backend/services/project-management/src/main/java/io/metersphere/project/service/EnvironmentService.java +++ b/backend/services/project-management/src/main/java/io/metersphere/project/service/EnvironmentService.java @@ -3,8 +3,11 @@ package io.metersphere.project.service; import io.metersphere.project.domain.Project; import io.metersphere.project.dto.environment.EnvironmentConfig; +import io.metersphere.project.dto.environment.EnvironmentDTO; +import io.metersphere.project.dto.environment.EnvironmentExportDTO; import io.metersphere.project.dto.environment.EnvironmentRequest; import io.metersphere.project.dto.environment.datasource.DataSource; +import io.metersphere.project.mapper.ExtEnvironmentMapper; import io.metersphere.project.mapper.ProjectMapper; import io.metersphere.sdk.constants.HttpMethodConstants; import io.metersphere.sdk.domain.Environment; @@ -18,14 +21,14 @@ import io.metersphere.sdk.file.FileRequest; import io.metersphere.sdk.file.MinioRepository; import io.metersphere.sdk.mapper.EnvironmentBlobMapper; import io.metersphere.sdk.mapper.EnvironmentMapper; -import io.metersphere.system.log.constants.OperationLogModule; -import io.metersphere.system.log.constants.OperationLogType; -import io.metersphere.system.log.service.OperationLogService; -import io.metersphere.system.uid.UUID; import io.metersphere.sdk.util.JSON; import io.metersphere.sdk.util.LogUtils; import io.metersphere.sdk.util.Translator; +import io.metersphere.system.log.constants.OperationLogModule; +import io.metersphere.system.log.constants.OperationLogType; +import io.metersphere.system.log.service.OperationLogService; import io.metersphere.system.service.JdbcDriverPluginService; +import io.metersphere.system.uid.UUID; import jakarta.annotation.Resource; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.StringUtils; @@ -36,6 +39,7 @@ import org.springframework.web.multipart.MultipartFile; import java.io.InputStream; import java.sql.Driver; import java.util.*; +import java.util.stream.Collectors; @Service @Transactional @@ -52,10 +56,13 @@ public class EnvironmentService { private ProjectMapper projectMapper; @Resource private OperationLogService operationLogService; + @Resource + private ExtEnvironmentMapper extEnvironmentMapper; private static final String DIR_PATH = "/project-management/environment/"; private static final String USERNAME = "user"; private static final String PASSWORD = "password"; + private static final String PATH = "/project/environment/import"; public List getDriverOptions(String organizationId) { return jdbcDriverPluginService.getJdbcDriverOption(organizationId); @@ -125,11 +132,8 @@ public class EnvironmentService { return request; } - public List list(String projectId) { - EnvironmentExample example = new EnvironmentExample(); - example.createCriteria().andProjectIdEqualTo(projectId); - example.setOrderByClause("update_time desc"); - return environmentMapper.selectByExample(example); + public List list(EnvironmentDTO request) { + return extEnvironmentMapper.selectByKeyword(request.getKeyword(), false, request.getProjectId()); } public EnvironmentRequest get(String environmentId) { @@ -146,37 +150,47 @@ public class EnvironmentService { return environmentRequest; } - public String export(List environmentIds) { - if (CollectionUtils.isNotEmpty(environmentIds)) { - // 查询环境 - EnvironmentExample environmentExample = new EnvironmentExample(); - environmentExample.createCriteria().andIdIn(environmentIds); - List environments = environmentMapper.selectByExample(environmentExample); - Map environmentMap = new HashMap<>(); - environments.forEach(environment -> environmentMap.put(environment.getId(), environment)); - // 查询环境配置 - EnvironmentBlobExample environmentBlobExample = new EnvironmentBlobExample(); - environmentBlobExample.createCriteria().andIdIn(environmentIds); - List environmentBlobs = environmentBlobMapper.selectByExampleWithBLOBs(environmentBlobExample); - Map environmentBlobMap = new HashMap<>(); - environmentBlobs.forEach(environmentBlob -> environmentBlobMap.put(environmentBlob.getId(), environmentBlob)); + public String export(EnvironmentExportDTO environmentExportDTO) { + List environmentIds = this.getEnvironmentIds(environmentExportDTO); + // 查询环境 + EnvironmentExample environmentExample = new EnvironmentExample(); + environmentExample.createCriteria().andIdIn(environmentIds); + List environments = environmentMapper.selectByExample(environmentExample); + Map environmentMap = new HashMap<>(); + environments.forEach(environment -> environmentMap.put(environment.getId(), environment)); + // 查询环境配置 + EnvironmentBlobExample environmentBlobExample = new EnvironmentBlobExample(); + environmentBlobExample.createCriteria().andIdIn(environmentIds); + List environmentBlobs = environmentBlobMapper.selectByExampleWithBLOBs(environmentBlobExample); + Map environmentBlobMap = new HashMap<>(); + environmentBlobs.forEach(environmentBlob -> environmentBlobMap.put(environmentBlob.getId(), environmentBlob)); - List environmentRequests = new ArrayList<>(); - environmentIds.forEach(environmentId -> { - EnvironmentRequest environmentRequest = new EnvironmentRequest(); - Environment environment = environmentMap.get(environmentId); - EnvironmentBlob environmentBlob = environmentBlobMap.get(environmentId); - environmentRequest.setProjectId(environment.getProjectId()); - environmentRequest.setName(environment.getName()); - environmentRequest.setId(environment.getId()); - if (environmentBlob != null) { - environmentRequest.setConfig(JSON.parseObject(new String(environmentBlob.getConfig()), EnvironmentConfig.class)); - } - environmentRequests.add(environmentRequest); - }); - return JSON.toJSONString(environmentRequests); + List environmentRequests = new ArrayList<>(); + environmentIds.forEach(environmentId -> { + EnvironmentRequest environmentRequest = new EnvironmentRequest(); + Environment environment = environmentMap.get(environmentId); + EnvironmentBlob environmentBlob = environmentBlobMap.get(environmentId); + environmentRequest.setProjectId(environment.getProjectId()); + environmentRequest.setName(environment.getName()); + environmentRequest.setId(environment.getId()); + if (environmentBlob != null) { + environmentRequest.setConfig(JSON.parseObject(new String(environmentBlob.getConfig()), EnvironmentConfig.class)); + } + environmentRequests.add(environmentRequest); + }); + return JSON.toJSONString(environmentRequests); + } + + private List getEnvironmentIds(EnvironmentExportDTO request) { + if (request.isSelectAll()) { + List environments = extEnvironmentMapper.selectByKeyword(request.getCondition().getKeyword(), true, request.getProjectId()); + List environmentIds = environments.stream().map(Environment::getId).collect(Collectors.toList()); + if (org.apache.commons.collections4.CollectionUtils.isNotEmpty(request.getExcludeIds())) { + environmentIds.removeAll(request.getExcludeIds()); + } + return environmentIds; } else { - return null; + return request.getSelectIds(); } } @@ -230,7 +244,7 @@ public class EnvironmentService { environment.getName()); logDTO.setMethod(HttpMethodConstants.POST.name()); logDTO.setOriginalValue(JSON.toJSONBytes(environmentRequest.getConfig())); - logDTO.setPath("/project/environment/import"); + logDTO.setPath(PATH); logDTOS.add(logDTO); }); environmentMapper.batchInsert(environments); diff --git a/backend/services/project-management/src/test/java/io/metersphere/project/controller/EnvironmentControllerTests.java b/backend/services/project-management/src/test/java/io/metersphere/project/controller/EnvironmentControllerTests.java index d0872be0d3..f23e69cdc2 100644 --- a/backend/services/project-management/src/test/java/io/metersphere/project/controller/EnvironmentControllerTests.java +++ b/backend/services/project-management/src/test/java/io/metersphere/project/controller/EnvironmentControllerTests.java @@ -1,9 +1,7 @@ package io.metersphere.project.controller; -import io.metersphere.project.dto.environment.EnvironmentConfig; -import io.metersphere.project.dto.environment.EnvironmentRequest; -import io.metersphere.project.dto.environment.KeyValue; +import io.metersphere.project.dto.environment.*; import io.metersphere.project.dto.environment.assertions.*; import io.metersphere.project.dto.environment.auth.AuthConfig; import io.metersphere.project.dto.environment.common.CommonParams; @@ -79,7 +77,7 @@ public class EnvironmentControllerTests extends BaseTest { private static final String get = prefix + "/get/"; private static final String update = prefix + "/update"; private static final String delete = prefix + "/delete/"; - private static final String list = prefix + "/list/"; + private static final String list = prefix + "/list"; private static final String getEnTry = prefix + "/get/entry"; private static final String importEnv = prefix + "/import"; private static final String exportEnv = prefix + "/export"; @@ -144,13 +142,13 @@ public class EnvironmentControllerTests extends BaseTest { .andExpect(content().contentType(MediaType.APPLICATION_JSON)); } - protected MvcResult requestMultipartWithOk(String url, MultiValueMap paramMap) throws Exception { + protected MvcResult requestMultipartWithOk(String url, MultiValueMap paramMap, String projectId) throws Exception { MockMultipartHttpServletRequestBuilder requestBuilder = getMultipartRequestBuilderWithParam(url, paramMap); MockHttpServletRequestBuilder header = requestBuilder .header(SessionConstants.HEADER_TOKEN, sessionId) .header(SessionConstants.CSRF_TOKEN, csrfToken) .header(HttpHeaders.ACCEPT_LANGUAGE, "zh-CN") - .header("PROJECT", "projectId"); + .header("PROJECT", projectId); return mockMvc.perform(header) .andExpect(content().contentType(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()).andReturn(); @@ -694,7 +692,7 @@ public class EnvironmentControllerTests extends BaseTest { MockMultipartFile file11 = new MockMultipartFile("file", "测试一下a", MediaType.APPLICATION_OCTET_STREAM_VALUE, "Test content".getBytes()); paramMap.add("file", List.of(file, file11)); paramMap.set("request", JSON.toJSONString(request)); - mvcResult = requestMultipartWithOk(add, paramMap); + mvcResult = requestMultipartWithOk(add, paramMap, "projectId"); response = parseObjectFromMvcResult(mvcResult, EnvironmentRequest.class); Assertions.assertNotNull(response); environment = environmentMapper.selectByPrimaryKey(response.getId()); @@ -882,7 +880,7 @@ public class EnvironmentControllerTests extends BaseTest { MockMultipartFile file11 = new MockMultipartFile("file", "测试一下a", MediaType.APPLICATION_OCTET_STREAM_VALUE, "Test content".getBytes()); paramMap.add("file", List.of(file, file11)); paramMap.set("request", JSON.toJSONString(request)); - mvcResult = requestMultipartWithOk(update, paramMap); + mvcResult = requestMultipartWithOk(update, paramMap, "projectId"); response = parseObjectFromMvcResult(mvcResult, EnvironmentRequest.class); Assertions.assertNotNull(response); environment = environmentMapper.selectByPrimaryKey(response.getId()); @@ -984,15 +982,25 @@ public class EnvironmentControllerTests extends BaseTest { @Test @Order(11) public void testList() throws Exception { - MvcResult mvcResult = this.responseGet(list + "projectId"); + EnvironmentDTO environmentDTO = new EnvironmentDTO(); + environmentDTO.setProjectId("projectId"); + MvcResult mvcResult = this.responsePost(list, environmentDTO); List response = parseObjectFromMvcResult(mvcResult, List.class); Assertions.assertNotNull(response); - + //输入搜索值 + environmentDTO.setKeyword("commonParams"); + mvcResult = this.responsePost(list, environmentDTO); + response = parseObjectFromMvcResult(mvcResult, List.class); + Assertions.assertNotNull(response); + //校验拿到的数据包含搜索值 + Assertions.assertEquals(1, response.size()); + environmentDTO.setProjectId(DEFAULT_PROJECT_ID); //校验权限 - requestGetPermissionTest(PermissionConstants.PROJECT_ENVIRONMENT_READ, list + DEFAULT_PROJECT_ID); + requestPostPermissionTest(PermissionConstants.PROJECT_ENVIRONMENT_READ, list , environmentDTO); //项目不存在 返回内容为[] - mvcResult = this.responseGet(list + "ceshi"); + environmentDTO.setProjectId("ceshi"); + mvcResult = this.responsePost(list ,environmentDTO); response = parseObjectFromMvcResult(mvcResult, List.class); Assertions.assertEquals(0, response.size()); } @@ -1009,7 +1017,7 @@ public class EnvironmentControllerTests extends BaseTest { MultiValueMap paramMap = new LinkedMultiValueMap<>(); paramMap.add("file", List.of(file)); paramMap.set("request", password); - MvcResult mvcResult = requestMultipartWithOk(getEnTry, paramMap); + MvcResult mvcResult = requestMultipartWithOk(getEnTry, paramMap, "projectId"); List response = parseObjectFromMvcResult(mvcResult, List.class); Assertions.assertNotNull(response); @@ -1039,21 +1047,28 @@ public class EnvironmentControllerTests extends BaseTest { @Test @Order(13) public void testExport() throws Exception { - //校验参数 - EnvironmentExample example = new EnvironmentExample(); - example.createCriteria().andProjectIdEqualTo("projectId"); - List environments = environmentMapper.selectByExample(example); - //把环境的id整理为一个集合 - List ids = environments.stream().map(Environment::getId).toList(); + //指定id + EnvironmentExportDTO environmentExportDTO = new EnvironmentExportDTO(); + environmentExportDTO.setProjectId("projectId"); + environmentExportDTO.setSelectIds(List.of("environmentId1")); - MvcResult mvcResult = this.responsePost(exportEnv, ids); + MvcResult mvcResult = this.responsePost(exportEnv, environmentExportDTO); String response = parseObjectFromMvcResult(mvcResult, String.class); + //判断response只有一条数据 + Assertions.assertNotNull(response); + List environments = JSON.parseArray(response, EnvironmentRequest.class); + Assertions.assertEquals(1, environments.size()); + //全选 + environmentExportDTO.setSelectIds(List.of("environmentId1")); + environmentExportDTO.setSelectAll(true); + environmentExportDTO.setExcludeIds(List.of("environmentId1")); + mvcResult = this.responsePost(exportEnv, environmentExportDTO); + response = parseObjectFromMvcResult(mvcResult, String.class); Assertions.assertNotNull(response); - //传id为空 - mvcResult = this.responsePost(exportEnv, new ArrayList<>()); - response = parseObjectFromMvcResult(mvcResult, String.class); - Assertions.assertNull(response); + environmentExportDTO.setProjectId(DEFAULT_PROJECT_ID); + //校验权限 + requestPostPermissionTest(PermissionConstants.PROJECT_ENVIRONMENT_READ_EXPORT, exportEnv, environmentExportDTO); } @Test @@ -1066,7 +1081,9 @@ public class EnvironmentControllerTests extends BaseTest { MockMultipartFile file = new MockMultipartFile("file", "huanj.json", MediaType.APPLICATION_OCTET_STREAM_VALUE, inputStream); MultiValueMap paramMap = new LinkedMultiValueMap<>(); paramMap.add("file", List.of(file)); - requestMultipartWithOk(importEnv, paramMap); + requestMultipartWithOk(importEnv, paramMap, DEFAULT_PROJECT_ID); + //校验权限 + requestMultipartPermissionTest(PermissionConstants.PROJECT_ENVIRONMENT_READ_IMPORT, importEnv, paramMap); } }