From 094a5949866072fe80baa0f336f0cfd3774f46f8 Mon Sep 17 00:00:00 2001 From: fit2-zhao Date: Thu, 23 Nov 2023 12:44:51 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):=20?= =?UTF-8?q?=E5=AE=9A=E4=B9=89=E5=AF=BC=E5=85=A5=E5=A4=84=E7=90=86=E7=BB=93?= =?UTF-8?q?=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/dto/request/ExportRequest.java | 10 ++++++ .../api/dto/request/ImportRequest.java | 31 ++++++++++++++++ .../api/enums/ApiImportPlatform.java | 5 +++ .../metersphere/api/parser/ExportParser.java | 7 ++++ .../metersphere/api/parser/ImportParser.java | 10 ++++++ .../api/parser/ImportParserFactory.java | 17 +++++++++ .../api/parser/api/PostmanParser.java | 17 +++++++++ .../api/parser/api/Swagger3Parser.java | 23 ++++++++++++ .../metersphere/api/parser/ParserTests.java | 36 +++++++++++++++++++ 9 files changed, 156 insertions(+) create mode 100644 backend/services/api-test/src/main/java/io/metersphere/api/dto/request/ExportRequest.java create mode 100644 backend/services/api-test/src/main/java/io/metersphere/api/dto/request/ImportRequest.java create mode 100644 backend/services/api-test/src/main/java/io/metersphere/api/enums/ApiImportPlatform.java create mode 100644 backend/services/api-test/src/main/java/io/metersphere/api/parser/ExportParser.java create mode 100644 backend/services/api-test/src/main/java/io/metersphere/api/parser/ImportParser.java create mode 100644 backend/services/api-test/src/main/java/io/metersphere/api/parser/ImportParserFactory.java create mode 100644 backend/services/api-test/src/main/java/io/metersphere/api/parser/api/PostmanParser.java create mode 100644 backend/services/api-test/src/main/java/io/metersphere/api/parser/api/Swagger3Parser.java create mode 100644 backend/services/api-test/src/test/java/io/metersphere/api/parser/ParserTests.java diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/dto/request/ExportRequest.java b/backend/services/api-test/src/main/java/io/metersphere/api/dto/request/ExportRequest.java new file mode 100644 index 0000000000..d311e0120e --- /dev/null +++ b/backend/services/api-test/src/main/java/io/metersphere/api/dto/request/ExportRequest.java @@ -0,0 +1,10 @@ +package io.metersphere.api.dto.request; + +import lombok.Data; + +@Data +public class ExportRequest { + private String id; + + // TODO: Add more fields +} diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/dto/request/ImportRequest.java b/backend/services/api-test/src/main/java/io/metersphere/api/dto/request/ImportRequest.java new file mode 100644 index 0000000000..ccacc55921 --- /dev/null +++ b/backend/services/api-test/src/main/java/io/metersphere/api/dto/request/ImportRequest.java @@ -0,0 +1,31 @@ +package io.metersphere.api.dto.request; + +import lombok.Data; + +@Data +public class ImportRequest { + private String id; + private String name; + private String moduleId; + private String environmentId; + private String projectId; + private String platform; + private String swaggerUrl; + + //导入策略 + private String modeId; + private String userId; + private String versionId; // 新导入选择的版本 + private String updateVersionId; // 覆盖导入已存在的接口选择的版本 + private String defaultVersion; + //调用类型 + private String type; + // 是否开启自定义ID + private Boolean openCustomNum = false; + // 是否覆盖模块 + private Boolean coverModule; + // 当前协议 + private String protocol; + //上传文件来源,目前用于辨别是否是idea插件 + private String origin; +} diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/enums/ApiImportPlatform.java b/backend/services/api-test/src/main/java/io/metersphere/api/enums/ApiImportPlatform.java new file mode 100644 index 0000000000..84295166d9 --- /dev/null +++ b/backend/services/api-test/src/main/java/io/metersphere/api/enums/ApiImportPlatform.java @@ -0,0 +1,5 @@ +package io.metersphere.api.enums; + +public enum ApiImportPlatform { + MeterSphere, Postman, Swagger3, Plugin, Jmeter, Har +} diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/parser/ExportParser.java b/backend/services/api-test/src/main/java/io/metersphere/api/parser/ExportParser.java new file mode 100644 index 0000000000..0556936bda --- /dev/null +++ b/backend/services/api-test/src/main/java/io/metersphere/api/parser/ExportParser.java @@ -0,0 +1,7 @@ +package io.metersphere.api.parser; + +import io.metersphere.api.dto.request.ExportRequest; + +public interface ExportParser { + T parse(ExportRequest request) throws Exception; +} diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/parser/ImportParser.java b/backend/services/api-test/src/main/java/io/metersphere/api/parser/ImportParser.java new file mode 100644 index 0000000000..bab9a6d4cc --- /dev/null +++ b/backend/services/api-test/src/main/java/io/metersphere/api/parser/ImportParser.java @@ -0,0 +1,10 @@ +package io.metersphere.api.parser; + + +import io.metersphere.api.dto.request.ImportRequest; + +import java.io.InputStream; + +public interface ImportParser { + T parse(InputStream source, ImportRequest request) throws Exception; +} diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/parser/ImportParserFactory.java b/backend/services/api-test/src/main/java/io/metersphere/api/parser/ImportParserFactory.java new file mode 100644 index 0000000000..62e007b1d6 --- /dev/null +++ b/backend/services/api-test/src/main/java/io/metersphere/api/parser/ImportParserFactory.java @@ -0,0 +1,17 @@ +package io.metersphere.api.parser; + +import io.metersphere.api.enums.ApiImportPlatform; +import io.metersphere.api.parser.api.PostmanParser; +import io.metersphere.api.parser.api.Swagger3Parser; +import org.apache.commons.lang3.StringUtils; + +public class ImportParserFactory { + public static ImportParser getImportParser(String platform) { + if (StringUtils.equals(ApiImportPlatform.Swagger3.name(), platform)) { + return new Swagger3Parser<>(); + } else if (StringUtils.equals(ApiImportPlatform.Postman.name(), platform)) { + return new PostmanParser<>(); + } + return null; + } +} diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/parser/api/PostmanParser.java b/backend/services/api-test/src/main/java/io/metersphere/api/parser/api/PostmanParser.java new file mode 100644 index 0000000000..a9d1741cba --- /dev/null +++ b/backend/services/api-test/src/main/java/io/metersphere/api/parser/api/PostmanParser.java @@ -0,0 +1,17 @@ +package io.metersphere.api.parser.api; + + +import io.metersphere.api.dto.request.ImportRequest; +import io.metersphere.api.parser.ImportParser; +import io.metersphere.sdk.util.LogUtils; + +import java.io.InputStream; + +public class PostmanParser implements ImportParser { + + @Override + public T parse(InputStream source, ImportRequest request) { + LogUtils.info("PostmanParser parse"); + return null; + } +} diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/parser/api/Swagger3Parser.java b/backend/services/api-test/src/main/java/io/metersphere/api/parser/api/Swagger3Parser.java new file mode 100644 index 0000000000..bc1f9f4fc3 --- /dev/null +++ b/backend/services/api-test/src/main/java/io/metersphere/api/parser/api/Swagger3Parser.java @@ -0,0 +1,23 @@ +package io.metersphere.api.parser.api; + +import io.metersphere.api.dto.request.ImportRequest; +import io.metersphere.api.parser.ImportParser; +import io.metersphere.sdk.util.LogUtils; + +import java.io.InputStream; + + +public class Swagger3Parser implements ImportParser { + + @Override + public T parse(InputStream source, ImportRequest request) throws Exception { + LogUtils.info("Swagger3Parser parse"); + + // todo: 检查swagger文件版本 + + // todo:检查文件的合规性 + + // todo:解析文件pojo + return null; + } +} diff --git a/backend/services/api-test/src/test/java/io/metersphere/api/parser/ParserTests.java b/backend/services/api-test/src/test/java/io/metersphere/api/parser/ParserTests.java new file mode 100644 index 0000000000..d63687d9c9 --- /dev/null +++ b/backend/services/api-test/src/test/java/io/metersphere/api/parser/ParserTests.java @@ -0,0 +1,36 @@ +package io.metersphere.api.parser; + +import io.metersphere.api.enums.ApiImportPlatform; +import org.junit.jupiter.api.MethodOrderer; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; + +import java.util.Objects; + + +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) +@AutoConfigureMockMvc +public class ParserTests { + + @Test + @Order(1) + public void testImportParserSwagger() throws Exception { + Objects.requireNonNull(ImportParserFactory.getImportParser(ApiImportPlatform.Swagger3.name())).parse(null, null); + } + + @Test + @Order(2) + public void testImportParserPostman() throws Exception { + Objects.requireNonNull(ImportParserFactory.getImportParser(ApiImportPlatform.Postman.name())).parse(null, null); + } + + @Test + @Order(3) + public void testImportParserMs() throws Exception { + ImportParserFactory.getImportParser(ApiImportPlatform.MeterSphere.name()); + } +}