feat(测试计划): 接口用例批量更新执行人

This commit is contained in:
WangXu10 2024-06-05 20:04:58 +08:00 committed by 刘瑞斌
parent 4e4266972a
commit dc8d772658
9 changed files with 163 additions and 12 deletions

View File

@ -4,15 +4,19 @@ import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import io.metersphere.plan.dto.request.BasePlanCaseBatchRequest;
import io.metersphere.plan.dto.request.TestPlanApiCaseRequest;
import io.metersphere.plan.dto.request.TestPlanApiCaseUpdateRequest;
import io.metersphere.plan.dto.request.TestPlanDisassociationRequest;
import io.metersphere.plan.dto.response.TestPlanApiCasePageResponse;
import io.metersphere.plan.dto.response.TestPlanAssociationResponse;
import io.metersphere.plan.service.TestPlanApiCaseLogService;
import io.metersphere.plan.service.TestPlanApiCaseService;
import io.metersphere.plan.service.TestPlanService;
import io.metersphere.sdk.constants.HttpMethodConstants;
import io.metersphere.sdk.constants.PermissionConstants;
import io.metersphere.system.dto.LogInsertModule;
import io.metersphere.system.dto.sdk.BaseTreeNode;
import io.metersphere.system.log.annotation.Log;
import io.metersphere.system.log.constants.OperationLogType;
import io.metersphere.system.security.CheckOwner;
import io.metersphere.system.utils.PageUtils;
import io.metersphere.system.utils.Pager;
@ -79,6 +83,15 @@ public class TestPlanApiCaseController {
return response;
}
@PostMapping("/batch/update/executor")
@Operation(summary = "测试计划-计划详情-功能用例-批量更新执行人")
@RequiresPermissions(PermissionConstants.TEST_PLAN_READ_UPDATE)
@CheckOwner(resourceId = "#request.getTestPlanId()", resourceType = "test_plan")
@Log(type = OperationLogType.DISASSOCIATE, expression = "#msClass.batchUpdateExecutor(#request)", msClass = TestPlanApiCaseLogService.class)
public void batchUpdateExecutor(@Validated @RequestBody TestPlanApiCaseUpdateRequest request) {
testPlanApiCaseService.batchUpdateExecutor(request);
}
//TODO 计划集 type
}

View File

@ -163,7 +163,7 @@ public class TestPlanFunctionalCaseController {
@Operation(summary = "测试计划-计划详情-功能用例-批量更新执行人")
@RequiresPermissions(PermissionConstants.TEST_PLAN_READ_UPDATE)
@CheckOwner(resourceId = "#request.getTestPlanId()", resourceType = "test_plan")
@Log(type = OperationLogType.DISASSOCIATE, expression = "#msClass.batchUpdateExecutor(#request)", msClass = TestPlanCaseLogService.class)
@Log(type = OperationLogType.UPDATE, expression = "#msClass.batchUpdateExecutor(#request)", msClass = TestPlanCaseLogService.class)
public void batchUpdateExecutor(@Validated @RequestBody TestPlanCaseUpdateRequest request) {
testPlanFunctionalCaseService.batchUpdateExecutor(request);
}

View File

@ -0,0 +1,24 @@
package io.metersphere.plan.dto.request;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotEmpty;
import lombok.Data;
import java.util.List;
/**
* @author wx
*/
@Data
public class TestPlanApiCaseUpdateRequest extends BasePlanCaseBatchRequest {
@Schema(description = "执行人id", requiredMode = Schema.RequiredMode.REQUIRED)
@NotBlank(message = "{test_plan.user_id.not_blank}")
private String userId;
@Schema(description = "接口协议", requiredMode = Schema.RequiredMode.REQUIRED)
@NotEmpty(message = "{api_definition.protocol.not_blank}")
private List<String> protocols;
}

View File

@ -49,5 +49,7 @@ public interface ExtTestPlanApiCaseMapper {
List<ApiCaseModuleDTO> selectBaseByProjectIdAndTestPlanId(@Param("testPlanId") String testPlanId);
List<String> getIds(@Param("request") BasePlanCaseBatchRequest request, @Param("deleted") boolean deleted);
List<String> getIds(@Param("request") BasePlanCaseBatchRequest request, @Param("deleted") boolean deleted, @Param("protocols") List<String> protocols);
void batchUpdateExecutor(@Param("ids") List<String> ids, @Param("userId") String userId);
}

View File

@ -599,11 +599,11 @@
</select>
<sql id="queryWhereConditionByBatchQueryRequest">
<if test="request.protocol != null and request.protocol!=''">
and a.protocol = #{request.protocol}
</if>
<if test="request.apiDefinitionId != null and request.apiDefinitionId!=''">
and atc.api_definition_id = #{request.apiDefinitionId}
<if test="protocols != null and protocols.size() > 0">
and a.protocol in
<foreach collection="protocols" item="protocol" separator="," open="(" close=")">
#{protocol}
</foreach>
</if>
<if test="request.condition.keyword != null and request.condition.keyword !=''">
and (
@ -630,4 +630,14 @@
</include>
</sql>
<update id="batchUpdateExecutor">
update test_plan_api_case
set execute_user = #{userId}
where id in
<foreach collection="ids" item="id" open="(" separator="," close=")">
#{id}
</foreach>
</update>
</mapper>

View File

@ -0,0 +1,71 @@
package io.metersphere.plan.service;
import io.metersphere.api.domain.ApiTestCase;
import io.metersphere.api.domain.ApiTestCaseExample;
import io.metersphere.api.mapper.ApiTestCaseMapper;
import io.metersphere.plan.domain.TestPlanApiCase;
import io.metersphere.plan.domain.TestPlanApiCaseExample;
import io.metersphere.plan.dto.request.TestPlanApiCaseUpdateRequest;
import io.metersphere.plan.mapper.TestPlanApiCaseMapper;
import io.metersphere.sdk.constants.HttpMethodConstants;
import io.metersphere.sdk.util.JSON;
import io.metersphere.system.log.constants.OperationLogModule;
import io.metersphere.system.log.constants.OperationLogType;
import io.metersphere.system.log.dto.LogDTO;
import jakarta.annotation.Resource;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@Service
@Transactional(rollbackFor = Exception.class)
public class TestPlanApiCaseLogService {
@Resource
private TestPlanApiCaseService testPlanApiCaseService;
@Resource
private TestPlanApiCaseMapper testPlanApiCaseMapper;
@Resource
private ApiTestCaseMapper apiTestCaseMapper;
public void batchUpdateExecutor(TestPlanApiCaseUpdateRequest request) {
List<String> ids = testPlanApiCaseService.doSelectIds(request, request.getProtocols());
if (CollectionUtils.isNotEmpty(ids)) {
TestPlanApiCaseExample example = new TestPlanApiCaseExample();
example.createCriteria().andIdIn(ids);
List<TestPlanApiCase> planCaseList = testPlanApiCaseMapper.selectByExample(example);
Map<String, String> userMap = planCaseList.stream().collect(Collectors.toMap(TestPlanApiCase::getId, TestPlanApiCase::getExecuteUser));
Map<String, String> idsMap = planCaseList.stream().collect(Collectors.toMap(TestPlanApiCase::getId, TestPlanApiCase::getApiCaseId));
List<String> caseIds = planCaseList.stream().map(TestPlanApiCase::getApiCaseId).collect(Collectors.toList());
ApiTestCaseExample caseExample = new ApiTestCaseExample();
caseExample.createCriteria().andIdIn(caseIds);
List<ApiTestCase> functionalCases = apiTestCaseMapper.selectByExample(caseExample);
Map<String, String> caseMap = functionalCases.stream().collect(Collectors.toMap(ApiTestCase::getId, ApiTestCase::getName));
List<LogDTO> dtoList = new ArrayList<>();
idsMap.forEach((k, v) -> {
LogDTO dto = new LogDTO(
null,
null,
k,
null,
OperationLogType.UPDATE.name(),
OperationLogModule.TEST_PLAN,
caseMap.get(v));
dto.setPath("/test-plan/api/case/batch/update/executor");
dto.setMethod(HttpMethodConstants.POST.name());
dto.setOriginalValue(JSON.toJSONBytes(userMap.get(k)));
dto.setModifiedValue(JSON.toJSONBytes(request.getUserId()));
dtoList.add(dto);
});
}
}
}

View File

@ -14,6 +14,7 @@ import io.metersphere.plan.dto.TestPlanCaseRunResultCount;
import io.metersphere.plan.dto.TestPlanResourceAssociationParam;
import io.metersphere.plan.dto.request.BasePlanCaseBatchRequest;
import io.metersphere.plan.dto.request.TestPlanApiCaseRequest;
import io.metersphere.plan.dto.request.TestPlanApiCaseUpdateRequest;
import io.metersphere.plan.dto.request.TestPlanApiRequest;
import io.metersphere.plan.dto.response.TestPlanApiCasePageResponse;
import io.metersphere.plan.dto.response.TestPlanAssociationResponse;
@ -324,12 +325,11 @@ public class TestPlanApiCaseService extends TestPlanResourceService {
* @return
*/
public TestPlanAssociationResponse disassociate(BasePlanCaseBatchRequest request, LogInsertModule logInsertModule) {
List<String> selectIds = doSelectIds(request);
return super.disassociate(
TestPlanResourceConstants.RESOURCE_API_CASE,
request,
logInsertModule,
selectIds,
request.getSelectIds(),
this::deleteTestPlanResource);
}
@ -341,9 +341,9 @@ public class TestPlanApiCaseService extends TestPlanResourceService {
}
public List<String> doSelectIds(BasePlanCaseBatchRequest request) {
public List<String> doSelectIds(BasePlanCaseBatchRequest request, List<String> protocols) {
if (request.isSelectAll()) {
List<String> ids = extTestPlanApiCaseMapper.getIds(request, false);
List<String> ids = extTestPlanApiCaseMapper.getIds(request, false, protocols);
if (CollectionUtils.isNotEmpty(request.getExcludeIds())) {
ids.removeAll(request.getExcludeIds());
}
@ -352,4 +352,17 @@ public class TestPlanApiCaseService extends TestPlanResourceService {
return request.getSelectIds();
}
}
/**
* 批量更新执行人
*
* @param request
*/
public void batchUpdateExecutor(TestPlanApiCaseUpdateRequest request) {
List<String> ids = doSelectIds(request, request.getProtocols());
if (CollectionUtils.isNotEmpty(ids)) {
extTestPlanApiCaseMapper.batchUpdateExecutor(ids, request.getUserId());
}
}
}

View File

@ -56,7 +56,7 @@ public class TestPlanCaseLogService {
OperationLogType.UPDATE.name(),
OperationLogModule.TEST_PLAN,
caseMap.get(v));
dto.setPath("/test-plan/functional/case/batch/update/executer");
dto.setPath("/test-plan/functional/case/batch/update/executor");
dto.setMethod(HttpMethodConstants.POST.name());
dto.setOriginalValue(JSON.toJSONBytes(userMap.get(k)));
dto.setModifiedValue(JSON.toJSONBytes(request.getUserId()));

View File

@ -1,6 +1,7 @@
package io.metersphere.plan.controller;
import io.metersphere.plan.dto.request.TestPlanApiCaseRequest;
import io.metersphere.plan.dto.request.TestPlanApiCaseUpdateRequest;
import io.metersphere.plan.dto.request.TestPlanDisassociationRequest;
import io.metersphere.sdk.util.JSON;
import io.metersphere.system.base.BaseTest;
@ -14,6 +15,7 @@ import org.springframework.test.web.servlet.MvcResult;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureMockMvc
@ -24,6 +26,7 @@ public class TestPlanApiCaseControllerTests extends BaseTest {
public static final String API_CASE_TREE_COUNT = "/test-plan/api/case/module/count";
public static final String API_CASE_TREE_MODULE_TREE = "/test-plan/api/case/tree/";
public static final String API_CASE_DISASSOCIATE = "/test-plan/api/case/disassociate";
public static final String API_CASE_BATCH_UPDATE_EXECUTOR_URL = "/test-plan/api/case/batch/update/executor";
@Test
@Order(1)
@ -86,4 +89,19 @@ public class TestPlanApiCaseControllerTests extends BaseTest {
Assertions.assertNotNull(resultHolder);
}
@Test
@Order(5)
public void testBatchUpdateExecutor() throws Exception {
TestPlanApiCaseUpdateRequest request = new TestPlanApiCaseUpdateRequest();
request.setUserId("test_user");
request.setTestPlanId("wxxx_2");
request.setSelectAll(true);
request.setProtocols(List.of("HTTP"));
this.requestPostWithOk(API_CASE_BATCH_UPDATE_EXECUTOR_URL, request);
request.setTestPlanId("wxxx_1");
request.setSelectAll(false);
request.setSelectIds(List.of("wxxx_1"));
this.requestPostWithOk(API_CASE_BATCH_UPDATE_EXECUTOR_URL, request);
}
}