From aec74277735d1ecb69e5492383ee3cf022bece2a Mon Sep 17 00:00:00 2001 From: chenjianxing Date: Sat, 22 Jan 2022 22:14:12 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E9=87=8D=E6=9E=84=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=E7=94=A8=E4=BE=8B=E5=AF=BC=E5=85=A5=E5=90=8E=E7=AB=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../metersphere/service/ProjectService.java | 5 +- .../track/controller/TestCaseController.java | 20 +- .../testcase/TestCaseImportRequest.java | 14 + .../track/service/TestCaseService.java | 343 ++++++++---------- .../import/TestCaseCommonImport.vue | 68 ++-- .../case/components/import/TestCaseImport.vue | 14 +- 6 files changed, 209 insertions(+), 255 deletions(-) create mode 100644 backend/src/main/java/io/metersphere/track/request/testcase/TestCaseImportRequest.java diff --git a/backend/src/main/java/io/metersphere/service/ProjectService.java b/backend/src/main/java/io/metersphere/service/ProjectService.java index b217b83cf0..f266f2210b 100644 --- a/backend/src/main/java/io/metersphere/service/ProjectService.java +++ b/backend/src/main/java/io/metersphere/service/ProjectService.java @@ -524,7 +524,10 @@ public class ProjectService { } public boolean useCustomNum(String projectId) { - Project project = this.getProjectById(projectId); + return useCustomNum(this.getProjectById(projectId)); + } + + public boolean useCustomNum(Project project) { if (project != null) { Boolean customNum = project.getCustomNum(); // 未开启自定义ID diff --git a/backend/src/main/java/io/metersphere/track/controller/TestCaseController.java b/backend/src/main/java/io/metersphere/track/controller/TestCaseController.java index cad3c1a2f2..21ad3259e5 100644 --- a/backend/src/main/java/io/metersphere/track/controller/TestCaseController.java +++ b/backend/src/main/java/io/metersphere/track/controller/TestCaseController.java @@ -27,10 +27,7 @@ import io.metersphere.notice.annotation.SendNotice; import io.metersphere.service.CheckPermissionService; import io.metersphere.service.FileService; import io.metersphere.track.dto.TestCaseDTO; -import io.metersphere.track.request.testcase.EditTestCaseRequest; -import io.metersphere.track.request.testcase.QueryTestCaseRequest; -import io.metersphere.track.request.testcase.TestCaseBatchRequest; -import io.metersphere.track.request.testcase.TestCaseMinderEditRequest; +import io.metersphere.track.request.testcase.*; import io.metersphere.track.request.testplan.FileOperationRequest; import io.metersphere.track.request.testplan.LoadCaseRequest; import io.metersphere.track.service.TestCaseService; @@ -259,18 +256,11 @@ public class TestCaseController { } - @PostMapping("/import/{projectId}/{userId}/{importType}") + @PostMapping("/import") @MsAuditLog(module = "track_test_case", type = OperLogConstants.IMPORT, project = "#projectId") - public ExcelResponse testCaseImport(MultipartFile file, @PathVariable String projectId, @PathVariable String userId, @PathVariable String importType, HttpServletRequest request) { - checkPermissionService.checkProjectOwner(projectId); - return testCaseService.testCaseImport(file, projectId, userId, importType, request); - } - - @PostMapping("/importIgnoreError/{projectId}/{userId}/{importType}") - @MsAuditLog(module = "track_test_case", type = OperLogConstants.IMPORT, project = "#projectId") - public ExcelResponse testCaseImportIgnoreError(MultipartFile file, @PathVariable String projectId, @PathVariable String userId, @PathVariable String importType, HttpServletRequest request) { - checkPermissionService.checkProjectOwner(projectId); - return testCaseService.testCaseImportIgnoreError(file, projectId, userId, importType, request); + public ExcelResponse testCaseImport(@RequestPart("request") TestCaseImportRequest request, @RequestPart("file") MultipartFile file, HttpServletRequest httpRequest) { + checkPermissionService.checkProjectOwner(request.getProjectId()); + return testCaseService.testCaseImport(file, request, httpRequest); } @GetMapping("/export/template/{projectId}/{importType}") diff --git a/backend/src/main/java/io/metersphere/track/request/testcase/TestCaseImportRequest.java b/backend/src/main/java/io/metersphere/track/request/testcase/TestCaseImportRequest.java new file mode 100644 index 0000000000..a37d63a11c --- /dev/null +++ b/backend/src/main/java/io/metersphere/track/request/testcase/TestCaseImportRequest.java @@ -0,0 +1,14 @@ +package io.metersphere.track.request.testcase; + +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class TestCaseImportRequest { + private String projectId; + private String userId; + private String importType; + private String version; + private boolean ignore; +} diff --git a/backend/src/main/java/io/metersphere/track/service/TestCaseService.java b/backend/src/main/java/io/metersphere/track/service/TestCaseService.java index 63e6f3c424..e23858730d 100644 --- a/backend/src/main/java/io/metersphere/track/service/TestCaseService.java +++ b/backend/src/main/java/io/metersphere/track/service/TestCaseService.java @@ -48,10 +48,7 @@ import io.metersphere.performance.service.PerformanceTestService; import io.metersphere.service.*; import io.metersphere.track.dto.TestCaseCommentDTO; import io.metersphere.track.dto.TestCaseDTO; -import io.metersphere.track.request.testcase.EditTestCaseRequest; -import io.metersphere.track.request.testcase.QueryTestCaseRequest; -import io.metersphere.track.request.testcase.TestCaseBatchRequest; -import io.metersphere.track.request.testcase.TestCaseMinderEditRequest; +import io.metersphere.track.request.testcase.*; import io.metersphere.track.request.testplan.LoadCaseRequest; import io.metersphere.xmind.XmindCaseParser; import io.metersphere.xmind.pojo.TestCaseXmindData; @@ -708,47 +705,64 @@ public class TestCaseService { } - public ExcelResponse testCaseImport(MultipartFile multipartFile, String projectId, String userId, String importType, HttpServletRequest request) { - - ExcelResponse excelResponse = new ExcelResponse(); - boolean isUpdated = false; //判断是否更新了用例 - String currentWorkspaceId = SessionUtils.getCurrentWorkspaceId(); - QueryTestCaseRequest queryTestCaseRequest = new QueryTestCaseRequest(); - queryTestCaseRequest.setProjectId(projectId); - boolean useCunstomId = projectService.useCustomNum(projectId); - List testCases = extTestCaseMapper.getTestCaseNames(queryTestCaseRequest); - Set savedIds = new HashSet<>(); - Set testCaseNames = new HashSet<>(); - for (TestCase testCase : testCases) { - if (useCunstomId) { - savedIds.add(testCase.getCustomNum()); - } else { - savedIds.add(String.valueOf(testCase.getNum())); - } - - testCaseNames.add(testCase.getName()); - } - List> errList = null; + public ExcelResponse testCaseImport(MultipartFile multipartFile, TestCaseImportRequest request, HttpServletRequest httpRequest) { if (multipartFile == null) { MSException.throwException(Translator.get("upload_fail")); } if (multipartFile.getOriginalFilename().endsWith(".xmind")) { - try { - XmindCaseParser xmindParser = new XmindCaseParser(this, userId, projectId, testCaseNames, useCunstomId, importType); - errList = xmindParser.parse(multipartFile); - if (CollectionUtils.isEmpty(xmindParser.getNodePaths()) - && CollectionUtils.isEmpty(xmindParser.getTestCase()) - && CollectionUtils.isEmpty(xmindParser.getUpdateTestCase())) { - if (errList == null) { - errList = new ArrayList<>(); - } - ExcelErrData excelErrData = new ExcelErrData(null, 1, Translator.get("upload_fail") + ":" + Translator.get("upload_content_is_null")); - errList.add(excelErrData); - excelResponse.setErrList(errList); + return testCaseXmindImport(multipartFile, request, httpRequest); + } else { + return testCaseExcelImport(multipartFile, request, httpRequest); + } + } + + private List getTestCaseForImport(String projectId) { + QueryTestCaseRequest queryTestCaseRequest = new QueryTestCaseRequest(); + queryTestCaseRequest.setProjectId(projectId); + return extTestCaseMapper.getTestCaseNames(queryTestCaseRequest); + } + + private ExcelResponse getImportResponse(List> errList, boolean isUpdated) { + ExcelResponse excelResponse = new ExcelResponse(); + //如果包含错误信息就导出错误信息 + if (!errList.isEmpty()) { + excelResponse.setSuccess(false); + excelResponse.setErrList(errList); + excelResponse.setIsUpdated(isUpdated); + } else { + excelResponse.setSuccess(true); + } + return excelResponse; + } + + private ExcelResponse testCaseXmindImport(MultipartFile multipartFile, TestCaseImportRequest request, + HttpServletRequest httpRequest) { + String projectId = request.getProjectId(); + List> errList = new ArrayList<>(); + Project project = projectService.getProjectById(projectId); + boolean useCunstomId = projectService.useCustomNum(project); + + Set testCaseNames = getTestCaseForImport(projectId).stream() + .map(TestCase::getName) + .collect(Collectors.toSet()); + + try { + XmindCaseParser xmindParser = new XmindCaseParser(this, request.getUserId(), projectId, testCaseNames, useCunstomId, request.getImportType()); + errList = xmindParser.parse(multipartFile); + if (CollectionUtils.isEmpty(xmindParser.getNodePaths()) + && CollectionUtils.isEmpty(xmindParser.getTestCase()) + && CollectionUtils.isEmpty(xmindParser.getUpdateTestCase())) { + if (errList == null) { + errList = new ArrayList<>(); } + ExcelErrData excelErrData = new ExcelErrData(null, 1, Translator.get("upload_fail") + ":" + Translator.get("upload_content_is_null")); + errList.add(excelErrData); + } + + List names = new LinkedList<>(); + List ids = new LinkedList<>(); + if (!request.isIgnore()) { if (errList.isEmpty()) { - List names = new LinkedList<>(); - List ids = new LinkedList<>(); if (CollectionUtils.isNotEmpty(xmindParser.getNodePaths())) { testCaseNodeService.createNodes(xmindParser.getNodePaths(), projectId); } @@ -763,58 +777,111 @@ public class TestCaseService { names.addAll(xmindParser.getUpdateTestCase().stream().map(TestCase::getName).collect(Collectors.toList())); ids.addAll(xmindParser.getUpdateTestCase().stream().map(TestCase::getId).collect(Collectors.toList())); } - request.setAttribute("ms-req-title", String.join(",", names)); - request.setAttribute("ms-req-source-id", JSON.toJSONString(ids)); + httpRequest.setAttribute("ms-req-title", String.join(",", names)); + httpRequest.setAttribute("ms-req-source-id", JSON.toJSONString(ids)); } - xmindParser.clear(); - } catch (Exception e) { - LogUtil.error(e.getMessage(), e); - MSException.throwException(e.getMessage()); + } else { + List continueCaseList = xmindParser.getContinueValidatedCase(); + if (CollectionUtils.isNotEmpty(continueCaseList) || CollectionUtils.isNotEmpty(xmindParser.getUpdateTestCase())) { + if (CollectionUtils.isNotEmpty(xmindParser.getUpdateTestCase())) { + continueCaseList.removeAll(xmindParser.getUpdateTestCase()); + this.updateImportData(xmindParser.getUpdateTestCase(), projectId); + names = xmindParser.getTestCase().stream().map(TestCase::getName).collect(Collectors.toList()); + ids = xmindParser.getTestCase().stream().map(TestCase::getId).collect(Collectors.toList()); + } + List nodePathList = xmindParser.getValidatedNodePath(); + if (CollectionUtils.isNotEmpty(nodePathList)) { + testCaseNodeService.createNodes(nodePathList, projectId); + } + if (CollectionUtils.isNotEmpty(continueCaseList)) { +// Collections.reverse(continueCaseList); + this.saveImportData(continueCaseList, projectId); + names.addAll(continueCaseList.stream().map(TestCase::getName).collect(Collectors.toList())); + ids.addAll(continueCaseList.stream().map(TestCase::getId).collect(Collectors.toList())); + + } + httpRequest.setAttribute("ms-req-title", String.join(",", names)); + httpRequest.setAttribute("ms-req-source-id", JSON.toJSONString(ids)); + } + + } + xmindParser.clear(); + } catch (Exception e) { + LogUtil.error(e); + MSException.throwException(e.getMessage()); + } + return getImportResponse(errList, false); + } + + private ExcelResponse testCaseExcelImport(MultipartFile multipartFile, TestCaseImportRequest request, + HttpServletRequest httpRequest) { + String projectId = request.getProjectId(); + Set userIds; + Project project = projectService.getProjectById(projectId); + boolean useCunstomId = projectService.useCustomNum(project); + + Set savedIds = new HashSet<>(); + Set testCaseNames = new HashSet<>(); + List> errList = new ArrayList<>(); + boolean isUpdated = false; + + List testCases = getTestCaseForImport(projectId); + for (TestCase testCase : testCases) { + if (useCunstomId) { + savedIds.add(testCase.getCustomNum()); + } else { + savedIds.add(String.valueOf(testCase.getNum())); } - } else { + testCaseNames.add(testCase.getName()); + } + if (!request.isIgnore()) { QueryMemberRequest queryMemberRequest = new QueryMemberRequest(); queryMemberRequest.setProjectId(projectId); - Set userIds = userService.getProjectMemberList(queryMemberRequest) + userIds = userService.getProjectMemberList(queryMemberRequest) .stream() .map(User::getId) .collect(Collectors.toSet()); - - try { - //根据本地语言环境选择用哪种数据对象进行存放读取的数据 - Class clazz = new TestCaseExcelDataFactory().getExcelDataByLocal(); - TestCaseTemplateService testCaseTemplateService = CommonBeanFactory.getBean(TestCaseTemplateService.class); - TestCaseTemplateDao testCaseTemplate = testCaseTemplateService.getTemplate(projectId); - List customFields = null; - if (testCaseTemplate == null) { - customFields = new ArrayList<>(); - } else { - customFields = testCaseTemplate.getCustomFields(); - } - TestCaseNoModelDataListener easyExcelListener = new TestCaseNoModelDataListener(false, clazz, customFields, projectId, testCaseNames, savedIds, userIds, useCunstomId, importType); - //读取excel数据 - EasyExcelFactory.read(multipartFile.getInputStream(), easyExcelListener).sheet().doRead(); - request.setAttribute("ms-req-title", String.join(",", easyExcelListener.getNames())); - request.setAttribute("ms-req-source-id", JSON.toJSONString(easyExcelListener.getIds())); - - errList = easyExcelListener.getErrList(); - isUpdated = easyExcelListener.isUpdated(); - } catch (Exception e) { - LogUtil.error(e.getMessage(), e); - MSException.throwException(e.getMessage()); - } - - } - //如果包含错误信息就导出错误信息 - if (!errList.isEmpty()) { - excelResponse.setSuccess(false); - excelResponse.setErrList(errList); - excelResponse.setIsUpdated(isUpdated); } else { - excelResponse.setSuccess(true); + GroupExample groupExample = new GroupExample(); + groupExample.createCriteria().andTypeIn(Arrays.asList(UserGroupType.WORKSPACE, UserGroupType.PROJECT)); + List groups = groupMapper.selectByExample(groupExample); + List groupIds = groups.stream().map(Group::getId).collect(Collectors.toList()); + + UserGroupExample userGroupExample = new UserGroupExample(); + userGroupExample.createCriteria() + .andGroupIdIn(groupIds) + .andSourceIdEqualTo(project.getWorkspaceId()); + userIds = userGroupMapper.selectByExample(userGroupExample).stream().map(UserGroup::getUserId).collect(Collectors.toSet()); } - return excelResponse; + + try { + //根据本地语言环境选择用哪种数据对象进行存放读取的数据 + Class clazz = new TestCaseExcelDataFactory().getExcelDataByLocal(); + TestCaseTemplateService testCaseTemplateService = CommonBeanFactory.getBean(TestCaseTemplateService.class); + TestCaseTemplateDao testCaseTemplate = testCaseTemplateService.getTemplate(projectId); + List customFields = null; + if (testCaseTemplate == null) { + customFields = new ArrayList<>(); + } else { + customFields = testCaseTemplate.getCustomFields(); + } + TestCaseNoModelDataListener easyExcelListener = new TestCaseNoModelDataListener(request.isIgnore(), clazz, customFields, projectId, testCaseNames, + savedIds, userIds, useCunstomId, request.getImportType()); + + //读取excel数据 + EasyExcelFactory.read(multipartFile.getInputStream(), easyExcelListener).sheet().doRead(); + httpRequest.setAttribute("ms-req-title", String.join(",", easyExcelListener.getNames())); + httpRequest.setAttribute("ms-req-source-id", JSON.toJSONString(easyExcelListener.getIds())); + + errList = easyExcelListener.getErrList(); + isUpdated = easyExcelListener.isUpdated(); + } catch (Exception e) { + LogUtil.error(e.getMessage(), e); + MSException.throwException(e.getMessage()); + } + return getImportResponse(errList, isUpdated); } public void saveImportData(List testCases, String projectId) { @@ -1865,122 +1932,6 @@ public class TestCaseService { extTestCaseMapper.updateTestCaseCustomNumByProjectId(projectId); } - public ExcelResponse testCaseImportIgnoreError(MultipartFile multipartFile, String projectId, String userId, String importType, HttpServletRequest request) { - - ExcelResponse excelResponse = new ExcelResponse(); - boolean isUpdated = false; //判断是否更新了用例 - String currentWorkspaceId = SessionUtils.getCurrentWorkspaceId(); - QueryTestCaseRequest queryTestCaseRequest = new QueryTestCaseRequest(); - queryTestCaseRequest.setProjectId(projectId); - List testCases = extTestCaseMapper.getTestCaseNames(queryTestCaseRequest); - boolean useCunstomId = projectService.useCustomNum(projectId); - Set savedIds = new HashSet<>(); - Set testCaseNames = new HashSet<>(); - for (TestCase testCase : testCases) { - if (useCunstomId) { - savedIds.add(testCase.getCustomNum()); - } else { - savedIds.add(String.valueOf(testCase.getNum())); - } - testCaseNames.add(testCase.getName()); - } - List> errList = null; - if (multipartFile == null) { - MSException.throwException(Translator.get("upload_fail")); - } - if (multipartFile.getOriginalFilename().endsWith(".xmind")) { - try { - XmindCaseParser xmindParser = new XmindCaseParser(this, userId, projectId, testCaseNames, useCunstomId, importType); - errList = xmindParser.parse(multipartFile); - if (CollectionUtils.isEmpty(xmindParser.getNodePaths()) - && CollectionUtils.isEmpty(xmindParser.getTestCase()) - && CollectionUtils.isEmpty(xmindParser.getUpdateTestCase())) { - if (errList == null) { - errList = new ArrayList<>(); - } - ExcelErrData excelErrData = new ExcelErrData(null, 1, Translator.get("upload_fail") + ":" + Translator.get("upload_content_is_null")); - errList.add(excelErrData); - excelResponse.setErrList(errList); - } - List continueCaseList = xmindParser.getContinueValidatedCase(); - if (CollectionUtils.isNotEmpty(continueCaseList) || CollectionUtils.isNotEmpty(xmindParser.getUpdateTestCase())) { - List names = new LinkedList<>(); - List ids = new LinkedList<>(); - - if (CollectionUtils.isNotEmpty(xmindParser.getUpdateTestCase())) { - continueCaseList.removeAll(xmindParser.getUpdateTestCase()); - this.updateImportData(xmindParser.getUpdateTestCase(), projectId); - names = xmindParser.getTestCase().stream().map(TestCase::getName).collect(Collectors.toList()); - ids = xmindParser.getTestCase().stream().map(TestCase::getId).collect(Collectors.toList()); - } - List nodePathList = xmindParser.getValidatedNodePath(); - if (CollectionUtils.isNotEmpty(nodePathList)) { - testCaseNodeService.createNodes(nodePathList, projectId); - } - if (CollectionUtils.isNotEmpty(continueCaseList)) { -// Collections.reverse(continueCaseList); - this.saveImportData(continueCaseList, projectId); - names.addAll(continueCaseList.stream().map(TestCase::getName).collect(Collectors.toList())); - ids.addAll(continueCaseList.stream().map(TestCase::getId).collect(Collectors.toList())); - - } - request.setAttribute("ms-req-title", String.join(",", names)); - request.setAttribute("ms-req-source-id", JSON.toJSONString(ids)); - - } - xmindParser.clear(); - } catch (Exception e) { - LogUtil.error(e.getMessage(), e); - MSException.throwException(e.getMessage()); - } - } else { - GroupExample groupExample = new GroupExample(); - groupExample.createCriteria().andTypeIn(Arrays.asList(UserGroupType.WORKSPACE, UserGroupType.PROJECT)); - List groups = groupMapper.selectByExample(groupExample); - List groupIds = groups.stream().map(Group::getId).collect(Collectors.toList()); - - UserGroupExample userGroupExample = new UserGroupExample(); - userGroupExample.createCriteria() - .andGroupIdIn(groupIds) - .andSourceIdEqualTo(currentWorkspaceId); - Set userIds = userGroupMapper.selectByExample(userGroupExample).stream().map(UserGroup::getUserId).collect(Collectors.toSet()); - - try { - //根据本地语言环境选择用哪种数据对象进行存放读取的数据 - Class clazz = new TestCaseExcelDataFactory().getExcelDataByLocal(); - TestCaseTemplateService testCaseTemplateService = CommonBeanFactory.getBean(TestCaseTemplateService.class); - TestCaseTemplateDao testCaseTemplate = testCaseTemplateService.getTemplate(projectId); - List customFields = null; - if (testCaseTemplate == null) { - customFields = new ArrayList<>(); - } else { - customFields = testCaseTemplate.getCustomFields(); - } - TestCaseNoModelDataListener easyExcelListener = new TestCaseNoModelDataListener(true, clazz, customFields, projectId, testCaseNames, savedIds, userIds, useCunstomId, importType); - //读取excel数据 - EasyExcelFactory.read(multipartFile.getInputStream(), easyExcelListener).sheet().doRead(); - request.setAttribute("ms-req-title", String.join(",", easyExcelListener.getNames())); - request.setAttribute("ms-req-source-id", JSON.toJSONString(easyExcelListener.getIds())); - errList = easyExcelListener.getErrList(); - isUpdated = easyExcelListener.isUpdated(); - } catch (Exception e) { - - LogUtil.error(e); - MSException.throwException(e.getMessage()); - } - } - //如果包含错误信息就导出错误信息 - if (!errList.isEmpty()) { - excelResponse.setSuccess(false); - excelResponse.setErrList(errList); - excelResponse.setIsUpdated(isUpdated); - } else { - excelResponse.setSuccess(true); - } - - return excelResponse; - } - public String getLogDetails(String id) { TestCaseWithBLOBs bloBs = testCaseMapper.selectByPrimaryKey(id); if (bloBs != null) { diff --git a/frontend/src/business/components/track/case/components/import/TestCaseCommonImport.vue b/frontend/src/business/components/track/case/components/import/TestCaseCommonImport.vue index 69013063b8..e5a7e4ab5a 100644 --- a/frontend/src/business/components/track/case/components/import/TestCaseCommonImport.vue +++ b/frontend/src/business/components/track/case/components/import/TestCaseCommonImport.vue @@ -52,7 +52,7 @@ :on-remove="handleRemove" :file-list="fileList">