refactor(功能用例): 创建功能用例增加消息通知

This commit is contained in:
guoyuqi 2023-10-31 14:04:07 +08:00 committed by f2c-ci-robot[bot]
parent 8d95beec8b
commit 6a375e5e9f
3 changed files with 82 additions and 1 deletions

View File

@ -6,6 +6,7 @@ import io.metersphere.functional.request.FunctionalCaseAddRequest;
import io.metersphere.functional.request.FunctionalCaseEditRequest; import io.metersphere.functional.request.FunctionalCaseEditRequest;
import io.metersphere.functional.request.FunctionalCaseFollowerRequest; import io.metersphere.functional.request.FunctionalCaseFollowerRequest;
import io.metersphere.functional.service.FunctionalCaseLogService; import io.metersphere.functional.service.FunctionalCaseLogService;
import io.metersphere.functional.service.FunctionalCaseNoticeService;
import io.metersphere.functional.service.FunctionalCaseService; import io.metersphere.functional.service.FunctionalCaseService;
import io.metersphere.project.service.ProjectTemplateService; import io.metersphere.project.service.ProjectTemplateService;
import io.metersphere.sdk.constants.PermissionConstants; 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.dto.sdk.TemplateDTO;
import io.metersphere.system.log.annotation.Log; import io.metersphere.system.log.annotation.Log;
import io.metersphere.system.log.constants.OperationLogType; 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.metersphere.system.utils.SessionUtils;
import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag; import io.swagger.v3.oas.annotations.tags.Tag;
@ -54,6 +57,7 @@ public class FunctionalCaseController {
@Operation(summary = "功能用例-新增用例") @Operation(summary = "功能用例-新增用例")
@RequiresPermissions(PermissionConstants.FUNCTIONAL_CASE_READ_ADD) @RequiresPermissions(PermissionConstants.FUNCTIONAL_CASE_READ_ADD)
@Log(type = OperationLogType.ADD, expression = "#msClass.addFunctionalCaseLog(#request, #files)", msClass = FunctionalCaseLogService.class) @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<MultipartFile> files) { public FunctionalCase addFunctionalCase(@Validated @RequestPart("request") FunctionalCaseAddRequest request, @RequestPart(value = "files", required = false) List<MultipartFile> files) {
String userId = SessionUtils.getUserId(); String userId = SessionUtils.getUserId();
return functionalCaseService.addFunctionalCase(request, files, userId); return functionalCaseService.addFunctionalCase(request, files, userId);

View File

@ -3,9 +3,11 @@ package io.metersphere.functional.service;
import io.metersphere.functional.domain.FunctionalCase; import io.metersphere.functional.domain.FunctionalCase;
import io.metersphere.functional.domain.FunctionalCaseCustomField; import io.metersphere.functional.domain.FunctionalCaseCustomField;
import io.metersphere.functional.domain.FunctionalCaseCustomFieldExample; import io.metersphere.functional.domain.FunctionalCaseCustomFieldExample;
import io.metersphere.functional.dto.CaseCustomsFieldDTO;
import io.metersphere.functional.dto.FunctionalCaseDTO; import io.metersphere.functional.dto.FunctionalCaseDTO;
import io.metersphere.functional.mapper.FunctionalCaseCustomFieldMapper; import io.metersphere.functional.mapper.FunctionalCaseCustomFieldMapper;
import io.metersphere.functional.mapper.FunctionalCaseMapper; import io.metersphere.functional.mapper.FunctionalCaseMapper;
import io.metersphere.functional.request.FunctionalCaseAddRequest;
import io.metersphere.functional.request.FunctionalCaseCommentRequest; import io.metersphere.functional.request.FunctionalCaseCommentRequest;
import io.metersphere.sdk.util.BeanUtils; import io.metersphere.sdk.util.BeanUtils;
import io.metersphere.system.domain.CustomField; 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.dto.sdk.OptionDTO;
import io.metersphere.system.mapper.CustomFieldMapper; import io.metersphere.system.mapper.CustomFieldMapper;
import io.metersphere.system.notice.constants.NoticeConstants; import io.metersphere.system.notice.constants.NoticeConstants;
import io.metersphere.system.utils.SessionUtils;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
@ -115,5 +118,29 @@ public class FunctionalCaseNoticeService {
return optionDTOList; 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);
List<OptionDTO>fields = 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;
}
} }

View File

@ -1,14 +1,25 @@
package io.metersphere.functional.controller; package io.metersphere.functional.controller;
import io.metersphere.functional.domain.FunctionalCase;
import io.metersphere.functional.dto.CaseCustomsFieldDTO; import io.metersphere.functional.dto.CaseCustomsFieldDTO;
import io.metersphere.functional.request.FunctionalCaseAddRequest; import io.metersphere.functional.request.FunctionalCaseAddRequest;
import io.metersphere.functional.request.FunctionalCaseEditRequest; import io.metersphere.functional.request.FunctionalCaseEditRequest;
import io.metersphere.functional.request.FunctionalCaseFollowerRequest; import io.metersphere.functional.request.FunctionalCaseFollowerRequest;
import io.metersphere.functional.result.FunctionalCaseResultCode; import io.metersphere.functional.result.FunctionalCaseResultCode;
import io.metersphere.functional.utils.FileBaseUtils; 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.sdk.util.JSON;
import io.metersphere.system.base.BaseTest; import io.metersphere.system.base.BaseTest;
import io.metersphere.system.controller.handler.ResultHolder; 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.junit.jupiter.api.*;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest; 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_EDIT_FOLLOWER_URL = "/functional/case/edit/follower";
public static final String FUNCTIONAL_CASE_FOLLOWER_URL = "/functional/case/follower/"; public static final String FUNCTIONAL_CASE_FOLLOWER_URL = "/functional/case/follower/";
@Resource
private NotificationMapper notificationMapper;
@Resource
private CustomFieldMapper customFieldMapper;
@Test @Test
@Order(1) @Order(1)
@Sql(scripts = {"/dml/init_file_metadata_test.sql"}, config = @SqlConfig(encoding = "utf-8", transactionMode = SqlConfig.TransactionMode.ISOLATED)) @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("request", JSON.toJSONString(request));
paramMap.add("files", files); 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<Notification> notifications = notificationMapper.selectByExampleWithBLOBs(notificationExample);
String jsonString = JSON.toJSONString(notifications);
System.out.println(jsonString);
} }
@ -98,14 +125,37 @@ public class FunctionalCaseControllerTests extends BaseTest {
} }
private List<CaseCustomsFieldDTO> creatCustomsFields() { private List<CaseCustomsFieldDTO> creatCustomsFields() {
insertCustomField();
List<CaseCustomsFieldDTO> list = new ArrayList<>(); List<CaseCustomsFieldDTO> list = new ArrayList<>();
CaseCustomsFieldDTO customsFieldDTO = new CaseCustomsFieldDTO(); CaseCustomsFieldDTO customsFieldDTO = new CaseCustomsFieldDTO();
customsFieldDTO.setFieldId("customs_field_id_1"); customsFieldDTO.setFieldId("customs_field_id_1");
customsFieldDTO.setValue("customs_field_value_1"); customsFieldDTO.setValue("customs_field_value_1");
list.add(customsFieldDTO); list.add(customsFieldDTO);
CaseCustomsFieldDTO customsFieldDTO2 = new CaseCustomsFieldDTO();
customsFieldDTO2.setFieldId("customs_field_id_2");
customsFieldDTO2.setValue("customs_field_value_2");
list.add(customsFieldDTO2);
return list; 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() { private FunctionalCaseAddRequest creatFunctionalCase() {
FunctionalCaseAddRequest functionalCaseAddRequest = new FunctionalCaseAddRequest(); FunctionalCaseAddRequest functionalCaseAddRequest = new FunctionalCaseAddRequest();
functionalCaseAddRequest.setProjectId(DEFAULT_PROJECT_ID); functionalCaseAddRequest.setProjectId(DEFAULT_PROJECT_ID);