Skip to content

Commit

Permalink
feat(测试计划): 计划关联用例
Browse files Browse the repository at this point in the history
  • Loading branch information
WangXu10 authored and fit2-zhao committed May 12, 2024
1 parent 1b00d8c commit 1dae94a
Show file tree
Hide file tree
Showing 15 changed files with 139 additions and 821 deletions.
@@ -1,55 +1,12 @@
package io.metersphere.plan.controller;

import io.metersphere.plan.constants.TestPlanResourceConfig;
import io.metersphere.plan.dto.request.ResourceSortRequest;
import io.metersphere.plan.dto.request.TestPlanAssociationRequest;
import io.metersphere.plan.dto.response.TestPlanAssociationResponse;
import io.metersphere.plan.dto.response.TestPlanResourceSortResponse;
import io.metersphere.plan.service.TestPlanApiCaseService;
import io.metersphere.plan.service.TestPlanManagementService;
import io.metersphere.sdk.constants.HttpMethodConstants;
import io.metersphere.sdk.constants.PermissionConstants;
import io.metersphere.system.dto.LogInsertModule;
import io.metersphere.system.security.CheckOwner;
import io.metersphere.system.utils.SessionUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;

@Tag(name = "测试计划接口用例")
@RestController
@RequestMapping("/test-plan/api/case")
public class TestPlanApiCaseController {

@Resource
private TestPlanManagementService testPlanManagementService;
@Resource
private TestPlanApiCaseService testPlanApiCaseService;

@PostMapping(value = "/association")
@Operation(summary = "测试计划接口用例-关联接口用例")
@RequiresPermissions(PermissionConstants.TEST_PLAN_READ_ASSOCIATION)
@CheckOwner(resourceId = "#request.getTestPlanId()", resourceType = "test_plan")
public TestPlanAssociationResponse association(@Validated @RequestBody TestPlanAssociationRequest request) {
testPlanManagementService.checkModuleIsOpen(request.getTestPlanId(), TestPlanResourceConfig.CHECK_TYPE_TEST_PLAN, Collections.singletonList(TestPlanResourceConfig.CONFIG_TEST_PLAN_API_CASE));
return testPlanApiCaseService.association(request, new LogInsertModule(SessionUtils.getUserId(), "/test-plan/api/case/association", HttpMethodConstants.POST.name()));
}

@PostMapping(value = "/sort")
@Operation(summary = "测试计划接口用例-关联功能用例")
@RequiresPermissions(PermissionConstants.TEST_PLAN_READ_UPDATE)
@CheckOwner(resourceId = "#request.getTestPlanId()", resourceType = "test_plan")
public TestPlanResourceSortResponse sortNode(@Validated @RequestBody ResourceSortRequest request) {
testPlanManagementService.checkModuleIsOpen(request.getTestPlanId(), TestPlanResourceConfig.CHECK_TYPE_TEST_PLAN, Collections.singletonList(TestPlanResourceConfig.CONFIG_TEST_PLAN_API_CASE));
return testPlanApiCaseService.sortNode(request, new LogInsertModule(SessionUtils.getUserId(), "/test-plan/api/case/sort", HttpMethodConstants.POST.name()));
}

}
@@ -1,55 +1,13 @@
package io.metersphere.plan.controller;

import io.metersphere.plan.constants.TestPlanResourceConfig;
import io.metersphere.plan.dto.request.ResourceSortRequest;
import io.metersphere.plan.dto.request.TestPlanAssociationRequest;
import io.metersphere.plan.dto.response.TestPlanAssociationResponse;
import io.metersphere.plan.dto.response.TestPlanResourceSortResponse;
import io.metersphere.plan.service.TestPlanApiScenarioService;
import io.metersphere.plan.service.TestPlanManagementService;
import io.metersphere.sdk.constants.HttpMethodConstants;
import io.metersphere.sdk.constants.PermissionConstants;
import io.metersphere.system.dto.LogInsertModule;
import io.metersphere.system.security.CheckOwner;
import io.metersphere.system.utils.SessionUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;

@Tag(name = "测试计划场景用例")
@RestController
@RequestMapping("/test-plan/api/scenario")
public class TestPlanApiScenarioController {

@Resource
private TestPlanManagementService testPlanManagementService;
@Resource
private TestPlanApiScenarioService testPlanApiScenarioService;

@PostMapping(value = "/association")
@Operation(summary = "测试计划场景用例-关联接口用例")
@RequiresPermissions(PermissionConstants.TEST_PLAN_READ_ASSOCIATION)
@CheckOwner(resourceId = "#request.getTestPlanId()", resourceType = "test_plan")
public TestPlanAssociationResponse association(@Validated @RequestBody TestPlanAssociationRequest request) {
testPlanManagementService.checkModuleIsOpen(request.getTestPlanId(), TestPlanResourceConfig.CHECK_TYPE_TEST_PLAN, Collections.singletonList(TestPlanResourceConfig.CONFIG_TEST_PLAN_API_SCENARIO));
return testPlanApiScenarioService.association(request, new LogInsertModule(SessionUtils.getUserId(), "/test-plan/api/scenario/association", HttpMethodConstants.POST.name()));
}

@PostMapping(value = "/sort")
@Operation(summary = "测试计划场景用例-关联功能用例")
@RequiresPermissions(PermissionConstants.TEST_PLAN_READ_UPDATE)
@CheckOwner(resourceId = "#request.getTestPlanId()", resourceType = "test_plan")
public TestPlanResourceSortResponse sortNode(@Validated @RequestBody ResourceSortRequest request) {
testPlanManagementService.checkModuleIsOpen(request.getTestPlanId(), TestPlanResourceConfig.CHECK_TYPE_TEST_PLAN, Collections.singletonList(TestPlanResourceConfig.CONFIG_TEST_PLAN_API_SCENARIO));
return testPlanApiScenarioService.sortNode(request, new LogInsertModule(SessionUtils.getUserId(), "/test-plan/api/scenario/sort", HttpMethodConstants.POST.name()));
}

}
Expand Up @@ -158,4 +158,13 @@ public void batchArchived(@Validated @RequestBody TestPlanBatchRequest request)
testPlanManagementService.checkModuleIsOpen(request.getProjectId(), TestPlanResourceConfig.CHECK_TYPE_PROJECT, Collections.singletonList(TestPlanResourceConfig.CONFIG_TEST_PLAN));
testPlanService.batchArchived(request, SessionUtils.getUserId());
}

@PostMapping(value = "/association")
@Operation(summary = "测试计划功能用例-关联功能用例")
@RequiresPermissions(PermissionConstants.TEST_PLAN_READ_ASSOCIATION)
@CheckOwner(resourceId = "#request.getTestPlanId()", resourceType = "test_plan")
public void association(@Validated @RequestBody TestPlanAssociationRequest request) {
testPlanManagementService.checkModuleIsOpen(request.getTestPlanId(), TestPlanResourceConfig.CHECK_TYPE_TEST_PLAN, Collections.singletonList(TestPlanResourceConfig.CONFIG_TEST_PLAN_FUNCTIONAL_CASE));
testPlanService.association(request);
}
}
Expand Up @@ -5,7 +5,6 @@
import io.metersphere.plan.constants.TestPlanResourceConfig;
import io.metersphere.plan.dto.request.BasePlanCaseBatchRequest;
import io.metersphere.plan.dto.request.ResourceSortRequest;
import io.metersphere.plan.dto.request.TestPlanAssociationRequest;
import io.metersphere.plan.dto.request.TestPlanCaseRequest;
import io.metersphere.plan.dto.response.TestPlanAssociationResponse;
import io.metersphere.plan.dto.response.TestPlanCasePageResponse;
Expand Down Expand Up @@ -41,14 +40,6 @@ public class TestPlanFunctionalCaseController {
@Resource
private TestPlanFunctionalCaseService testPlanFunctionalCaseService;

@PostMapping(value = "/association")
@Operation(summary = "测试计划功能用例-关联功能用例")
@RequiresPermissions(PermissionConstants.TEST_PLAN_READ_ASSOCIATION)
@CheckOwner(resourceId = "#request.getTestPlanId()", resourceType = "test_plan")
public TestPlanAssociationResponse association(@Validated @RequestBody TestPlanAssociationRequest request) {
testPlanManagementService.checkModuleIsOpen(request.getTestPlanId(), TestPlanResourceConfig.CHECK_TYPE_TEST_PLAN, Collections.singletonList(TestPlanResourceConfig.CONFIG_TEST_PLAN_FUNCTIONAL_CASE));
return testPlanFunctionalCaseService.association(request, new LogInsertModule(SessionUtils.getUserId(), "/test-plan/functional/case/association", HttpMethodConstants.POST.name()));
}

@PostMapping(value = "/sort")
@Operation(summary = "测试计划功能用例-关联功能用例")
Expand Down
@@ -1,6 +1,5 @@
package io.metersphere.plan.dto.request;

import io.metersphere.system.dto.table.TableBatchProcessDTO;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

Expand All @@ -12,14 +11,11 @@
* @author wx
*/
@Data
public class BaseAssociateCaseRequest extends TableBatchProcessDTO implements Serializable {
public class BaseAssociateCaseRequest implements Serializable {

@Serial
private static final long serialVersionUID = 1L;

@Schema(description = "模块id")
private List<String> moduleIds;

@Schema(description = "功能用例选中的ids")
private List<String> functionalSelectIds;

Expand Down
Expand Up @@ -3,43 +3,16 @@
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import lombok.Data;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.io.Serial;

@Data
public class TestPlanAssociationRequest {
public class TestPlanAssociationRequest extends BaseAssociateCaseRequest {

@Serial
private static final long serialVersionUID = 1L;

@NotBlank(message = "{test_plan.id.not_blank}")
@Schema(description = "测试计划ID")
private String testPlanId;

@Schema(description = "选择的id")
private List<String> selectIds = new ArrayList<>();
@Schema(description = "不处理的ID")
private List<String> excludeIds = new ArrayList<>();
@Schema(description = "选择的模块ID")
private List<String> selectModuleIds = new ArrayList<>();

@Schema(description = "排序字段")
private String orderColumn;
@Schema(description = "是否是正序")
private boolean orderByAsc;

public boolean isEmpty() {
return CollectionUtils.isEmpty(selectIds) && CollectionUtils.isEmpty(selectModuleIds);
}

public String getOrderString() {
if (StringUtils.isNotBlank(orderColumn)) {
if (orderByAsc) {
return orderColumn + " ASC";
} else {
return orderColumn + " DESC";
}
} else {
return null;
}
}
}
@@ -1,34 +1,13 @@
package io.metersphere.plan.service;

import io.metersphere.plan.domain.TestPlan;
import io.metersphere.plan.domain.TestPlanApiCase;
import io.metersphere.plan.domain.TestPlanApiCaseExample;
import io.metersphere.plan.dto.AssociationNodeSortDTO;
import io.metersphere.plan.dto.ResourceLogInsertModule;
import io.metersphere.plan.dto.TestPlanResourceAssociationParam;
import io.metersphere.plan.dto.request.ResourceSortRequest;
import io.metersphere.plan.dto.request.TestPlanAssociationRequest;
import io.metersphere.plan.dto.response.TestPlanAssociationResponse;
import io.metersphere.plan.dto.response.TestPlanResourceSortResponse;
import io.metersphere.plan.mapper.ExtTestPlanApiCaseMapper;
import io.metersphere.plan.mapper.TestPlanApiCaseMapper;
import io.metersphere.plan.mapper.TestPlanMapper;
import io.metersphere.sdk.constants.ApplicationNumScope;
import io.metersphere.sdk.constants.TestPlanResourceConstants;
import io.metersphere.sdk.exception.MSException;
import io.metersphere.system.dto.LogInsertModule;
import io.metersphere.system.uid.IDGenerator;
import io.metersphere.system.uid.NumGenerator;
import jakarta.annotation.Resource;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import java.util.ArrayList;
import java.util.List;

@Service
Expand All @@ -40,10 +19,6 @@ public class TestPlanApiCaseService extends TestPlanResourceService {
private TestPlanApiCaseMapper testPlanApiCaseMapper;
@Resource
private ExtTestPlanApiCaseMapper extTestPlanApiCaseMapper;
@Resource
private TestPlanResourceLogService testPlanResourceLogService;
@Resource
private TestPlanMapper testPlanMapper;

@Override
public int deleteBatchByTestPlanId(List<String> testPlanIdList) {
Expand All @@ -52,78 +27,24 @@ public int deleteBatchByTestPlanId(List<String> testPlanIdList) {
return testPlanApiCaseMapper.deleteByExample(example);
}

@Override
public long getNextOrder(String testPlanId) {
Long maxPos = extTestPlanApiCaseMapper.getMaxPosByTestPlanId(testPlanId);
if (maxPos == null) {
return 0;
} else {
return maxPos + DEFAULT_NODE_INTERVAL_POS;
}
}

@Override
public void updatePos(String id, long pos) {
extTestPlanApiCaseMapper.updatePos(id, pos);
// TODO
//extTestPlanApiCaseMapper.updatePos(id, pos);
}

@Override
public void refreshPos(String testPlanId) {
List<String> caseIdList = extTestPlanApiCaseMapper.selectIdByTestPlanIdOrderByPos(testPlanId);
// TODO
/*List<String> caseIdList = extTestPlanApiCaseMapper.selectIdByTestPlanIdOrderByPos(testPlanId);
SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
ExtTestPlanApiCaseMapper batchUpdateMapper = sqlSession.getMapper(ExtTestPlanApiCaseMapper.class);
for (int i = 0; i < caseIdList.size(); i++) {
batchUpdateMapper.updatePos(caseIdList.get(i), i * DEFAULT_NODE_INTERVAL_POS);
}
sqlSession.flushStatements();
SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory);
SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory);*/
}

public TestPlanAssociationResponse association(TestPlanAssociationRequest request, LogInsertModule logInsertModule) {
return super.association(
TestPlanResourceConstants.RESOURCE_API_CASE,
request,
logInsertModule,
extTestPlanApiCaseMapper::getIdByParam,
this::saveTestPlanResource);
}

private void saveTestPlanResource(@Validated TestPlanResourceAssociationParam associationParam) {
long pox = this.getNextOrder(associationParam.getTestPlanId());
long now = System.currentTimeMillis();
List<TestPlanApiCase> testPlanResourceList = new ArrayList<>();
List<String> associationIdList = associationParam.getResourceIdList();
for (int i = 0; i < associationIdList.size(); i++) {
TestPlanApiCase testPlanResourceCase = new TestPlanApiCase();
testPlanResourceCase.setId(IDGenerator.nextStr());
testPlanResourceCase.setNum(NumGenerator.nextNum(associationParam.getTestPlanNum() + "_" + associationParam.getProjectId(), ApplicationNumScope.TEST_PLAN_API_CASE));
testPlanResourceCase.setTestPlanId(associationParam.getTestPlanId());
testPlanResourceCase.setApiCaseId(associationIdList.get(i));
testPlanResourceCase.setPos(pox + (i + 1) * DEFAULT_NODE_INTERVAL_POS);
testPlanResourceCase.setCreateTime(now);
testPlanResourceCase.setCreateUser(associationParam.getOperator());
testPlanResourceCase.setExecuteUser(associationParam.getOperator());
testPlanResourceList.add(testPlanResourceCase);
}
testPlanApiCaseMapper.batchInsert(testPlanResourceList);
}

public TestPlanResourceSortResponse sortNode(ResourceSortRequest request, LogInsertModule logInsertModule) {
TestPlanResourceSortResponse response = new TestPlanResourceSortResponse();
TestPlan testPlan = testPlanMapper.selectByPrimaryKey(request.getTestPlanId());
TestPlanApiCase dragNode = testPlanApiCaseMapper.selectByPrimaryKey(request.getDragNodeId());
if (dragNode == null) {
throw new MSException("test_plan.drag.node.error");
}
AssociationNodeSortDTO sortDTO = super.getNodeSortDTO(
request,
extTestPlanApiCaseMapper::selectDragInfoById,
extTestPlanApiCaseMapper::selectNodeByPosOperator
);
this.sort(sortDTO);
response.setSortNodeNum(1);
testPlanResourceLogService.saveSortLog(testPlan, request.getDragNodeId(), new ResourceLogInsertModule(TestPlanResourceConstants.RESOURCE_API_CASE, logInsertModule));
return response;

}
}

0 comments on commit 1dae94a

Please sign in to comment.