refactor(接口测试): 导入的接口构造默认数据

This commit is contained in:
wxg0103 2024-03-13 17:26:56 +08:00 committed by Craftsman
parent 11741c834b
commit ff2062d94d
5 changed files with 26 additions and 32 deletions

View File

@ -33,6 +33,7 @@ import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -79,10 +80,14 @@ public class ApiTestCaseController {
@GetMapping("recover/{id}") @GetMapping("recover/{id}")
@Operation(summary = "接口测试-接口管理-接口用例-恢复") @Operation(summary = "接口测试-接口管理-接口用例-恢复")
@RequiresPermissions(PermissionConstants.PROJECT_API_DEFINITION_CASE_DELETE) @RequiresPermissions(PermissionConstants.PROJECT_API_DEFINITION_CASE_DELETE)
@Log(type = OperationLogType.RECOVER, expression = "#msClass.recoverLog(#id)", msClass = ApiTestCaseLogService.class)
@CheckOwner(resourceId = "#id", resourceType = "api_test_case") @CheckOwner(resourceId = "#id", resourceType = "api_test_case")
public void recover(@PathVariable String id) { public void recover(@PathVariable String id) {
apiTestCaseService.recover(id, SessionUtils.getUserId(), SessionUtils.getCurrentProjectId()); ApiTestCaseBatchRequest request = new ApiTestCaseBatchRequest();
List<String> ids = new ArrayList<>();
ids.add(id);
request.setSelectIds(ids);
request.setProjectId(SessionUtils.getCurrentProjectId());
apiTestCaseRecoverService.batchRecover(request, SessionUtils.getUserId());
} }
@GetMapping("follow/{id}") @GetMapping("follow/{id}")

View File

@ -20,6 +20,7 @@ import io.metersphere.sdk.exception.MSException;
import io.metersphere.sdk.util.JSON; import io.metersphere.sdk.util.JSON;
import io.metersphere.sdk.util.LogUtils; import io.metersphere.sdk.util.LogUtils;
import io.metersphere.sdk.util.Translator; import io.metersphere.sdk.util.Translator;
import io.metersphere.system.uid.IDGenerator;
import io.swagger.parser.OpenAPIParser; import io.swagger.parser.OpenAPIParser;
import io.swagger.v3.oas.models.*; import io.swagger.v3.oas.models.*;
import io.swagger.v3.oas.models.media.*; import io.swagger.v3.oas.models.media.*;
@ -147,6 +148,7 @@ public class Swagger3Parser<T> implements ImportParser<ApiDefinitionImport> {
request.setAuthConfig(new NoAuth()); request.setAuthConfig(new NoAuth());
apiDefinitionDTO.setRequest(request); apiDefinitionDTO.setRequest(request);
//解析请求内容 //解析请求内容
parseResponse(operation.getResponses(), apiDefinitionDTO.getResponse()); parseResponse(operation.getResponses(), apiDefinitionDTO.getResponse());
results.add(apiDefinitionDTO); results.add(apiDefinitionDTO);
@ -158,13 +160,6 @@ public class Swagger3Parser<T> implements ImportParser<ApiDefinitionImport> {
} }
private void parseRequestBody(RequestBody requestBody, Body body) { private void parseRequestBody(RequestBody requestBody, Body body) {
body.setBinaryBody(new BinaryBody());
body.setFormDataBody(new FormDataBody());
body.setXmlBody(new XmlBody());
body.setRawBody(new RawBody());
body.setNoneBody(new NoneBody());
body.setJsonBody(new JsonBody());
body.setWwwFormBody(new WWWFormBody());
if (requestBody != null) { if (requestBody != null) {
Content content = requestBody.getContent(); Content content = requestBody.getContent();
if (content != null) { if (content != null) {
@ -365,6 +360,16 @@ public class Swagger3Parser<T> implements ImportParser<ApiDefinitionImport> {
httpConfig.setResponseTimeout(60000L); httpConfig.setResponseTimeout(60000L);
request.setOtherConfig(httpConfig); request.setOtherConfig(httpConfig);
request.setAuthConfig(new NoAuth()); request.setAuthConfig(new NoAuth());
Body body = new Body();
body.setBinaryBody(new BinaryBody());
body.setFormDataBody(new FormDataBody());
body.setXmlBody(new XmlBody());
body.setRawBody(new RawBody());
body.setNoneBody(new NoneBody());
body.setJsonBody(new JsonBody());
body.setWwwFormBody(new WWWFormBody());
body.setNoneBody(new NoneBody());
request.setBody(body);
return request; return request;
} }
@ -532,6 +537,7 @@ public class Swagger3Parser<T> implements ImportParser<ApiDefinitionImport> {
JsonSchemaItem jsonSchemaItem = new JsonSchemaItem(); JsonSchemaItem jsonSchemaItem = new JsonSchemaItem();
jsonSchemaItem.setType(PropertyConstant.OBJECT); jsonSchemaItem.setType(PropertyConstant.OBJECT);
jsonSchemaItem.setRequired(objectSchema.getRequired()); jsonSchemaItem.setRequired(objectSchema.getRequired());
jsonSchemaItem.setId(IDGenerator.nextStr());
jsonSchemaItem.setDescription(objectSchema.getDescription()); jsonSchemaItem.setDescription(objectSchema.getDescription());
Map<String, Schema> properties = objectSchema.getProperties(); Map<String, Schema> properties = objectSchema.getProperties();
Map<String, JsonSchemaItem> jsonSchemaProperties = new LinkedHashMap<>(); Map<String, JsonSchemaItem> jsonSchemaProperties = new LinkedHashMap<>();
@ -617,6 +623,7 @@ public class Swagger3Parser<T> implements ImportParser<ApiDefinitionImport> {
private JsonSchemaItem parseString(StringSchema stringSchema) { private JsonSchemaItem parseString(StringSchema stringSchema) {
JsonSchemaItem jsonSchemaString = new JsonSchemaItem(); JsonSchemaItem jsonSchemaString = new JsonSchemaItem();
jsonSchemaString.setType(PropertyConstant.STRING); jsonSchemaString.setType(PropertyConstant.STRING);
jsonSchemaString.setId(IDGenerator.nextStr());
jsonSchemaString.setFormat(StringUtils.isNotBlank(stringSchema.getFormat()) ? stringSchema.getFormat() : StringUtils.EMPTY); jsonSchemaString.setFormat(StringUtils.isNotBlank(stringSchema.getFormat()) ? stringSchema.getFormat() : StringUtils.EMPTY);
jsonSchemaString.setDescription(getDefaultStringValue(stringSchema.getDescription())); jsonSchemaString.setDescription(getDefaultStringValue(stringSchema.getDescription()));
jsonSchemaString.setExample(stringSchema.getExample()); jsonSchemaString.setExample(stringSchema.getExample());
@ -637,6 +644,7 @@ public class Swagger3Parser<T> implements ImportParser<ApiDefinitionImport> {
private JsonSchemaItem parseInteger(IntegerSchema integerSchema) { private JsonSchemaItem parseInteger(IntegerSchema integerSchema) {
JsonSchemaItem jsonSchemaInteger = new JsonSchemaItem(); JsonSchemaItem jsonSchemaInteger = new JsonSchemaItem();
jsonSchemaInteger.setType(PropertyConstant.INTEGER); jsonSchemaInteger.setType(PropertyConstant.INTEGER);
jsonSchemaInteger.setId(IDGenerator.nextStr());
jsonSchemaInteger.setFormat(StringUtils.isNotBlank(integerSchema.getFormat()) ? integerSchema.getFormat() : StringUtils.EMPTY); jsonSchemaInteger.setFormat(StringUtils.isNotBlank(integerSchema.getFormat()) ? integerSchema.getFormat() : StringUtils.EMPTY);
jsonSchemaInteger.setDescription(StringUtils.isNotBlank(integerSchema.getDescription()) ? integerSchema.getDescription() : StringUtils.EMPTY); jsonSchemaInteger.setDescription(StringUtils.isNotBlank(integerSchema.getDescription()) ? integerSchema.getDescription() : StringUtils.EMPTY);
jsonSchemaInteger.setExample(integerSchema.getExample()); jsonSchemaInteger.setExample(integerSchema.getExample());
@ -649,6 +657,7 @@ public class Swagger3Parser<T> implements ImportParser<ApiDefinitionImport> {
private JsonSchemaItem parseNumber(NumberSchema numberSchema) { private JsonSchemaItem parseNumber(NumberSchema numberSchema) {
JsonSchemaItem jsonSchemaNumber = new JsonSchemaItem(); JsonSchemaItem jsonSchemaNumber = new JsonSchemaItem();
jsonSchemaNumber.setType(PropertyConstant.NUMBER); jsonSchemaNumber.setType(PropertyConstant.NUMBER);
jsonSchemaNumber.setId(IDGenerator.nextStr());
jsonSchemaNumber.setDescription(StringUtils.isNotBlank(numberSchema.getDescription()) ? numberSchema.getDescription() : StringUtils.EMPTY); jsonSchemaNumber.setDescription(StringUtils.isNotBlank(numberSchema.getDescription()) ? numberSchema.getDescription() : StringUtils.EMPTY);
jsonSchemaNumber.setExample(numberSchema.getExample()); jsonSchemaNumber.setExample(numberSchema.getExample());
jsonSchemaNumber.setEnumNumber(numberSchema.getEnum()); jsonSchemaNumber.setEnumNumber(numberSchema.getEnum());
@ -658,6 +667,7 @@ public class Swagger3Parser<T> implements ImportParser<ApiDefinitionImport> {
private JsonSchemaItem parseBoolean(BooleanSchema booleanSchema) { private JsonSchemaItem parseBoolean(BooleanSchema booleanSchema) {
JsonSchemaItem jsonSchemaBoolean = new JsonSchemaItem(); JsonSchemaItem jsonSchemaBoolean = new JsonSchemaItem();
jsonSchemaBoolean.setType(PropertyConstant.BOOLEAN); jsonSchemaBoolean.setType(PropertyConstant.BOOLEAN);
jsonSchemaBoolean.setId(IDGenerator.nextStr());
jsonSchemaBoolean.setDescription(getDefaultStringValue(booleanSchema.getDescription())); jsonSchemaBoolean.setDescription(getDefaultStringValue(booleanSchema.getDescription()));
jsonSchemaBoolean.setExample(booleanSchema.getExample()); jsonSchemaBoolean.setExample(booleanSchema.getExample());
return jsonSchemaBoolean; return jsonSchemaBoolean;
@ -665,6 +675,7 @@ public class Swagger3Parser<T> implements ImportParser<ApiDefinitionImport> {
private JsonSchemaItem parseNull() { private JsonSchemaItem parseNull() {
JsonSchemaItem jsonSchemaNull = new JsonSchemaItem(); JsonSchemaItem jsonSchemaNull = new JsonSchemaItem();
jsonSchemaNull.setId(IDGenerator.nextStr());
jsonSchemaNull.setType(PropertyConstant.NULL); jsonSchemaNull.setType(PropertyConstant.NULL);
return jsonSchemaNull; return jsonSchemaNull;
} }
@ -672,6 +683,7 @@ public class Swagger3Parser<T> implements ImportParser<ApiDefinitionImport> {
private JsonSchemaItem parseArraySchema(Schema<?> items) { private JsonSchemaItem parseArraySchema(Schema<?> items) {
JsonSchemaItem jsonSchemaArray = new JsonSchemaItem(); JsonSchemaItem jsonSchemaArray = new JsonSchemaItem();
jsonSchemaArray.setType(PropertyConstant.ARRAY); jsonSchemaArray.setType(PropertyConstant.ARRAY);
jsonSchemaArray.setId(IDGenerator.nextStr());
Schema itemsSchema = null; Schema itemsSchema = null;
if (StringUtils.isNotBlank(items.get$ref())) { if (StringUtils.isNotBlank(items.get$ref())) {
itemsSchema = getModelByRef(items.get$ref()); itemsSchema = getModelByRef(items.get$ref());

View File

@ -97,23 +97,6 @@ public class ApiTestCaseLogService {
return dto; return dto;
} }
public LogDTO recoverLog(String id) {
ApiTestCase apiTestCase = apiTestCaseMapper.selectByPrimaryKey(id);
Project project = projectMapper.selectByPrimaryKey(apiTestCase.getProjectId());
LogDTO dto = new LogDTO(
apiTestCase.getProjectId(),
project.getOrganizationId(),
id,
null,
OperationLogType.RECOVER.name(),
OperationLogModule.API_TEST_MANAGEMENT_CASE,
apiTestCase.getName());
dto.setHistory(false);
dto.setMethod(HttpMethodConstants.GET.name());
dto.setOriginalValue(JSON.toJSONBytes(apiTestCase));
return dto;
}
public LogDTO followLog(String id) { public LogDTO followLog(String id) {
ApiTestCase apiTestCase = apiTestCaseMapper.selectByPrimaryKey(id); ApiTestCase apiTestCase = apiTestCaseMapper.selectByPrimaryKey(id);
Project project = projectMapper.selectByPrimaryKey(apiTestCase.getProjectId()); Project project = projectMapper.selectByPrimaryKey(apiTestCase.getProjectId());

View File

@ -336,11 +336,6 @@ public class ApiTestCaseService extends MoveNodeService {
} }
} }
public void recover(String id, String userId, String projectId) {
ApiTestCase apiCase = checkResourceExist(id);
batchRecover(List.of(apiCase), userId, projectId);
}
public void batchRecover(List<ApiTestCase> apiTestCases, String userId, String projectId) { public void batchRecover(List<ApiTestCase> apiTestCases, String userId, String projectId) {
if (CollectionUtils.isNotEmpty(apiTestCases)) { if (CollectionUtils.isNotEmpty(apiTestCases)) {
apiTestCases.forEach(apiTestCase -> { apiTestCases.forEach(apiTestCase -> {

View File

@ -535,7 +535,6 @@ public class ApiTestCaseControllerTests extends BaseTest {
Assertions.assertFalse(apiCase.getDeleted()); Assertions.assertFalse(apiCase.getDeleted());
// @@校验日志 // @@校验日志
checkLog(apiTestCase.getId(), OperationLogType.RECOVER); checkLog(apiTestCase.getId(), OperationLogType.RECOVER);
this.requestGet(RECOVER + "111").andExpect(ERROR_REQUEST_MATCHER);
// @@校验权限 // @@校验权限
requestGetPermissionTest(PermissionConstants.PROJECT_API_DEFINITION_CASE_DELETE, RECOVER + apiTestCase.getId()); requestGetPermissionTest(PermissionConstants.PROJECT_API_DEFINITION_CASE_DELETE, RECOVER + apiTestCase.getId());