From 6a375e5e9f774fd481eaa7e667a34994212c68a9 Mon Sep 17 00:00:00 2001 From: guoyuqi Date: Tue, 31 Oct 2023 14:04:07 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E5=8A=9F=E8=83=BD=E7=94=A8=E4=BE=8B):?= =?UTF-8?q?=20=E5=88=9B=E5=BB=BA=E5=8A=9F=E8=83=BD=E7=94=A8=E4=BE=8B?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=B6=88=E6=81=AF=E9=80=9A=E7=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/FunctionalCaseController.java | 4 ++ .../service/FunctionalCaseNoticeService.java | 27 ++++++++++ .../FunctionalCaseControllerTests.java | 52 ++++++++++++++++++- 3 files changed, 82 insertions(+), 1 deletion(-) diff --git a/backend/services/case-management/src/main/java/io/metersphere/functional/controller/FunctionalCaseController.java b/backend/services/case-management/src/main/java/io/metersphere/functional/controller/FunctionalCaseController.java index 1f13d666c1..cc24c124d7 100644 --- a/backend/services/case-management/src/main/java/io/metersphere/functional/controller/FunctionalCaseController.java +++ b/backend/services/case-management/src/main/java/io/metersphere/functional/controller/FunctionalCaseController.java @@ -6,6 +6,7 @@ import io.metersphere.functional.request.FunctionalCaseAddRequest; import io.metersphere.functional.request.FunctionalCaseEditRequest; import io.metersphere.functional.request.FunctionalCaseFollowerRequest; import io.metersphere.functional.service.FunctionalCaseLogService; +import io.metersphere.functional.service.FunctionalCaseNoticeService; import io.metersphere.functional.service.FunctionalCaseService; import io.metersphere.project.service.ProjectTemplateService; import io.metersphere.sdk.constants.PermissionConstants; @@ -13,6 +14,8 @@ import io.metersphere.sdk.constants.TemplateScene; import io.metersphere.system.dto.sdk.TemplateDTO; import io.metersphere.system.log.annotation.Log; import io.metersphere.system.log.constants.OperationLogType; +import io.metersphere.system.notice.annotation.SendNotice; +import io.metersphere.system.notice.constants.NoticeConstants; import io.metersphere.system.utils.SessionUtils; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; @@ -54,6 +57,7 @@ public class FunctionalCaseController { @Operation(summary = "功能用例-新增用例") @RequiresPermissions(PermissionConstants.FUNCTIONAL_CASE_READ_ADD) @Log(type = OperationLogType.ADD, expression = "#msClass.addFunctionalCaseLog(#request, #files)", msClass = FunctionalCaseLogService.class) + @SendNotice(taskType = NoticeConstants.TaskType.FUNCTIONAL_CASE_TASK, event = NoticeConstants.Event.CREATE,target = "#targetClass.getMainFunctionalCaseDTO(#request)", targetClass = FunctionalCaseNoticeService.class) public FunctionalCase addFunctionalCase(@Validated @RequestPart("request") FunctionalCaseAddRequest request, @RequestPart(value = "files", required = false) List files) { String userId = SessionUtils.getUserId(); return functionalCaseService.addFunctionalCase(request, files, userId); diff --git a/backend/services/case-management/src/main/java/io/metersphere/functional/service/FunctionalCaseNoticeService.java b/backend/services/case-management/src/main/java/io/metersphere/functional/service/FunctionalCaseNoticeService.java index d9ad8ec2be..c689fe27c7 100644 --- a/backend/services/case-management/src/main/java/io/metersphere/functional/service/FunctionalCaseNoticeService.java +++ b/backend/services/case-management/src/main/java/io/metersphere/functional/service/FunctionalCaseNoticeService.java @@ -3,9 +3,11 @@ package io.metersphere.functional.service; import io.metersphere.functional.domain.FunctionalCase; import io.metersphere.functional.domain.FunctionalCaseCustomField; import io.metersphere.functional.domain.FunctionalCaseCustomFieldExample; +import io.metersphere.functional.dto.CaseCustomsFieldDTO; import io.metersphere.functional.dto.FunctionalCaseDTO; import io.metersphere.functional.mapper.FunctionalCaseCustomFieldMapper; import io.metersphere.functional.mapper.FunctionalCaseMapper; +import io.metersphere.functional.request.FunctionalCaseAddRequest; import io.metersphere.functional.request.FunctionalCaseCommentRequest; import io.metersphere.sdk.util.BeanUtils; import io.metersphere.system.domain.CustomField; @@ -13,6 +15,7 @@ import io.metersphere.system.domain.CustomFieldExample; import io.metersphere.system.dto.sdk.OptionDTO; import io.metersphere.system.mapper.CustomFieldMapper; import io.metersphere.system.notice.constants.NoticeConstants; +import io.metersphere.system.utils.SessionUtils; import jakarta.annotation.Resource; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.StringUtils; @@ -115,5 +118,29 @@ public class FunctionalCaseNoticeService { return optionDTOList; } + public FunctionalCaseDTO getMainFunctionalCaseDTO(FunctionalCaseAddRequest request) { + String userId = SessionUtils.getUserId(); + FunctionalCaseDTO functionalCaseDTO = new FunctionalCaseDTO(); + functionalCaseDTO.setName(request.getName()); + functionalCaseDTO.setCaseEditType(request.getCaseEditType()); + functionalCaseDTO.setCreateUser(userId); + Listfields = new ArrayList<>(); + if (CollectionUtils.isNotEmpty(request.getCustomsFields())){ + for (CaseCustomsFieldDTO customsFieldDTO : request.getCustomsFields()) { + OptionDTO optionDTO = new OptionDTO(); + CustomField customField = customFieldMapper.selectByPrimaryKey(customsFieldDTO.getFieldId()); + if (customField == null) { + continue; + } + optionDTO.setId(customField.getName()); + optionDTO.setName(customsFieldDTO.getValue()); + fields.add(optionDTO); + } + } + functionalCaseDTO.setFields(fields); + return functionalCaseDTO; + } + + } diff --git a/backend/services/case-management/src/test/java/io/metersphere/functional/controller/FunctionalCaseControllerTests.java b/backend/services/case-management/src/test/java/io/metersphere/functional/controller/FunctionalCaseControllerTests.java index fca7dd4ca8..26c774c756 100644 --- a/backend/services/case-management/src/test/java/io/metersphere/functional/controller/FunctionalCaseControllerTests.java +++ b/backend/services/case-management/src/test/java/io/metersphere/functional/controller/FunctionalCaseControllerTests.java @@ -1,14 +1,25 @@ package io.metersphere.functional.controller; +import io.metersphere.functional.domain.FunctionalCase; import io.metersphere.functional.dto.CaseCustomsFieldDTO; import io.metersphere.functional.request.FunctionalCaseAddRequest; import io.metersphere.functional.request.FunctionalCaseEditRequest; import io.metersphere.functional.request.FunctionalCaseFollowerRequest; import io.metersphere.functional.result.FunctionalCaseResultCode; import io.metersphere.functional.utils.FileBaseUtils; +import io.metersphere.project.domain.Notification; +import io.metersphere.project.domain.NotificationExample; +import io.metersphere.project.mapper.NotificationMapper; +import io.metersphere.sdk.constants.CustomFieldType; +import io.metersphere.sdk.constants.TemplateScene; +import io.metersphere.sdk.constants.TemplateScopeType; import io.metersphere.sdk.util.JSON; import io.metersphere.system.base.BaseTest; import io.metersphere.system.controller.handler.ResultHolder; +import io.metersphere.system.domain.CustomField; +import io.metersphere.system.mapper.CustomFieldMapper; +import io.metersphere.system.notice.constants.NoticeConstants; +import jakarta.annotation.Resource; import org.junit.jupiter.api.*; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; @@ -37,6 +48,12 @@ public class FunctionalCaseControllerTests extends BaseTest { public static final String FUNCTIONAL_CASE_EDIT_FOLLOWER_URL = "/functional/case/edit/follower"; public static final String FUNCTIONAL_CASE_FOLLOWER_URL = "/functional/case/follower/"; + @Resource + private NotificationMapper notificationMapper; + + @Resource + private CustomFieldMapper customFieldMapper; + @Test @Order(1) @Sql(scripts = {"/dml/init_file_metadata_test.sql"}, config = @SqlConfig(encoding = "utf-8", transactionMode = SqlConfig.TransactionMode.ISOLATED)) @@ -69,7 +86,17 @@ public class FunctionalCaseControllerTests extends BaseTest { paramMap.add("request", JSON.toJSONString(request)); paramMap.add("files", files); - this.requestMultipartWithOkAndReturn(FUNCTIONAL_CASE_ADD_URL, paramMap); + MvcResult functionalCaseMvcResult = this.requestMultipartWithOkAndReturn(FUNCTIONAL_CASE_ADD_URL, paramMap); + + String functionalCaseData = functionalCaseMvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8); + ResultHolder functionalCaseResultHolder = JSON.parseObject(functionalCaseData, ResultHolder.class); + FunctionalCase functionalCase = JSON.parseObject(JSON.toJSONString(functionalCaseResultHolder.getData()), FunctionalCase.class); + + NotificationExample notificationExample = new NotificationExample(); + notificationExample.createCriteria().andResourceNameEqualTo(functionalCase.getName()).andResourceTypeEqualTo(NoticeConstants.TaskType.FUNCTIONAL_CASE_TASK); + List notifications = notificationMapper.selectByExampleWithBLOBs(notificationExample); + String jsonString = JSON.toJSONString(notifications); + System.out.println(jsonString); } @@ -98,14 +125,37 @@ public class FunctionalCaseControllerTests extends BaseTest { } private List creatCustomsFields() { + insertCustomField(); List list = new ArrayList<>(); CaseCustomsFieldDTO customsFieldDTO = new CaseCustomsFieldDTO(); customsFieldDTO.setFieldId("customs_field_id_1"); customsFieldDTO.setValue("customs_field_value_1"); list.add(customsFieldDTO); + CaseCustomsFieldDTO customsFieldDTO2 = new CaseCustomsFieldDTO(); + customsFieldDTO2.setFieldId("customs_field_id_2"); + customsFieldDTO2.setValue("customs_field_value_2"); + list.add(customsFieldDTO2); return list; } + private void insertCustomField() { + CustomField customField = new CustomField(); + customField.setId("customs_field_id_1"); + customField.setName("test_customs_field_id_1"); + customField.setType(CustomFieldType.INPUT.toString()); + customField.setScene(TemplateScene.FUNCTIONAL.name()); + customField.setCreateUser("gyq"); + customField.setCreateTime(System.currentTimeMillis()); + customField.setUpdateTime(System.currentTimeMillis()); + customField.setRefId("test_customs_field_id_1"); + customField.setScopeId(DEFAULT_PROJECT_ID); + customField.setScopeType(TemplateScopeType.PROJECT.name()); + customField.setInternal(false); + customField.setEnableOptionKey(false); + customField.setRemark("1"); + customFieldMapper.insertSelective(customField); + } + private FunctionalCaseAddRequest creatFunctionalCase() { FunctionalCaseAddRequest functionalCaseAddRequest = new FunctionalCaseAddRequest(); functionalCaseAddRequest.setProjectId(DEFAULT_PROJECT_ID);