From c64f283ef7683d1a8b0893454574b689649564e4 Mon Sep 17 00:00:00 2001 From: fit2-zhao Date: Fri, 26 May 2023 19:02:18 +0800 Subject: [PATCH] build: add api test template --- .../controller/ApiDefinitionController.java | 27 ++++++ .../metersphere/api/dto/ApiDefinitionDTO.java | 7 ++ .../api/service/ApiDefinitionService.java | 15 ++++ .../java/io/metersphere/api/Application.java | 23 +++++ .../ApiDefinitionControllerTests.java | 48 +++++++++++ .../test/resources/application.properties | 83 +++++++++++++++++++ .../main/test/resources/bootstrap.properties | 13 +++ 7 files changed, 216 insertions(+) create mode 100644 backend/services/api-test/src/main/java/io/metersphere/api/controller/ApiDefinitionController.java create mode 100644 backend/services/api-test/src/main/java/io/metersphere/api/dto/ApiDefinitionDTO.java create mode 100644 backend/services/api-test/src/main/java/io/metersphere/api/service/ApiDefinitionService.java create mode 100644 backend/services/api-test/src/main/test/java/io/metersphere/api/Application.java create mode 100644 backend/services/api-test/src/main/test/java/io/metersphere/api/controller/ApiDefinitionControllerTests.java create mode 100644 backend/services/api-test/src/main/test/resources/application.properties create mode 100644 backend/services/api-test/src/main/test/resources/bootstrap.properties diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/controller/ApiDefinitionController.java b/backend/services/api-test/src/main/java/io/metersphere/api/controller/ApiDefinitionController.java new file mode 100644 index 0000000000..8a9c73ec86 --- /dev/null +++ b/backend/services/api-test/src/main/java/io/metersphere/api/controller/ApiDefinitionController.java @@ -0,0 +1,27 @@ +package io.metersphere.api.controller; + +import io.metersphere.api.dto.ApiDefinitionDTO; +import io.metersphere.api.service.ApiDefinitionService; +import jakarta.annotation.Resource; +import org.springframework.http.MediaType; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestPart; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.multipart.MultipartFile; + +import java.util.List; + + +@RestController +@RequestMapping(value = "/api/definition") +public class ApiDefinitionController { + @Resource + private ApiDefinitionService apiDefinitionService; + + @PostMapping(value = "/create", consumes = {MediaType.MULTIPART_FORM_DATA_VALUE}) + public ApiDefinitionDTO create(@RequestPart("request") ApiDefinitionDTO request, + @RequestPart(value = "files", required = false) List bodyFiles) { + return apiDefinitionService.create(request, bodyFiles); + } +} diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/dto/ApiDefinitionDTO.java b/backend/services/api-test/src/main/java/io/metersphere/api/dto/ApiDefinitionDTO.java new file mode 100644 index 0000000000..3f0e5c679b --- /dev/null +++ b/backend/services/api-test/src/main/java/io/metersphere/api/dto/ApiDefinitionDTO.java @@ -0,0 +1,7 @@ +package io.metersphere.api.dto; + +import io.metersphere.api.domain.ApiDefinition; + +public class ApiDefinitionDTO extends ApiDefinition { + +} diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiDefinitionService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiDefinitionService.java new file mode 100644 index 0000000000..1aa2806567 --- /dev/null +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiDefinitionService.java @@ -0,0 +1,15 @@ +package io.metersphere.api.service; + +import io.metersphere.api.dto.ApiDefinitionDTO; +import org.springframework.stereotype.Service; +import org.springframework.web.multipart.MultipartFile; + +import java.util.List; + +@Service +public class ApiDefinitionService { + + public ApiDefinitionDTO create(ApiDefinitionDTO request, List bodyFiles) { + return new ApiDefinitionDTO(); + } +} diff --git a/backend/services/api-test/src/main/test/java/io/metersphere/api/Application.java b/backend/services/api-test/src/main/test/java/io/metersphere/api/Application.java new file mode 100644 index 0000000000..6163ab4c88 --- /dev/null +++ b/backend/services/api-test/src/main/test/java/io/metersphere/api/Application.java @@ -0,0 +1,23 @@ +package io.metersphere.api; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.autoconfigure.ldap.LdapAutoConfiguration; +import org.springframework.boot.autoconfigure.neo4j.Neo4jAutoConfiguration; +import org.springframework.boot.autoconfigure.quartz.QuartzAutoConfiguration; +import org.springframework.boot.web.servlet.ServletComponentScan; +import org.springframework.context.annotation.ComponentScan; + +@SpringBootApplication(exclude = { + QuartzAutoConfiguration.class, + LdapAutoConfiguration.class, + Neo4jAutoConfiguration.class +}) +@ServletComponentScan +@ComponentScan(basePackages = {"io.metersphere.sdk", "io.metersphere.api"}) +public class Application { + + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } +} diff --git a/backend/services/api-test/src/main/test/java/io/metersphere/api/controller/ApiDefinitionControllerTests.java b/backend/services/api-test/src/main/test/java/io/metersphere/api/controller/ApiDefinitionControllerTests.java new file mode 100644 index 0000000000..63e8ff27a0 --- /dev/null +++ b/backend/services/api-test/src/main/test/java/io/metersphere/api/controller/ApiDefinitionControllerTests.java @@ -0,0 +1,48 @@ +package io.metersphere.api.controller; + +import com.fasterxml.jackson.databind.ObjectMapper; +import io.metersphere.api.dto.ApiDefinitionDTO; +import io.metersphere.sdk.util.LogUtils; +import jakarta.annotation.Resource; +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 org.springframework.http.MediaType; +import org.springframework.mock.web.MockMultipartFile; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; +import org.springframework.test.web.servlet.result.MockMvcResultMatchers; + + +@SpringBootTest +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) +@AutoConfigureMockMvc +public class ApiDefinitionControllerTests { + @Resource + private MockMvc mockMvc; + private final static String prefix = "/api/definition"; + + @Test + @Order(1) + public void testCreate() throws Exception { + LogUtils.info("create api test"); + // 创建一个 MockMultipartFile 对象,用于模拟文件上传 + MockMultipartFile file = new MockMultipartFile("files", "test.txt", "application/octet-stream", "Test content".getBytes()); + + // 创建一个 ApiDefinitionDTO 对象,用于模拟请求的一部分 + ApiDefinitionDTO request = new ApiDefinitionDTO(); + // 补充属性内容 + request.setId("test-api-id"); + + mockMvc.perform(MockMvcRequestBuilders.multipart(prefix + "/create") + .file(file) + .contentType(MediaType.MULTIPART_FORM_DATA) + .param("request", new ObjectMapper().writeValueAsString(request))) + .andExpect(MockMvcResultMatchers.status().is(400)); + //.andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON)) + //.andExpect(MockMvcResultMatchers.jsonPath("$.id").value("test-api-id")); + } +} diff --git a/backend/services/api-test/src/main/test/resources/application.properties b/backend/services/api-test/src/main/test/resources/application.properties new file mode 100644 index 0000000000..dbb73ca597 --- /dev/null +++ b/backend/services/api-test/src/main/test/resources/application.properties @@ -0,0 +1,83 @@ +spring.application.name=metersphere +api.server.port=10001 +server.port=8081 +# gzip +server.compression.enabled=true +server.compression.mime-types=application/json,application/xml,text/html,text/xml,text/plain,application/javascript,text/css,text/javascript,image/jpeg +server.compression.min-response-size=2048 +# +quartz.enabled=false +quartz.scheduler-name=msScheduler +quartz.thread-count=10 +quartz.properties.org.quartz.jobStore.acquireTriggersWithinLock=true +# +logging.file.path=/opt/metersphere/logs/metersphere/api +# Hikari +spring.datasource.url=jdbc:mysql://${embedded.mysql.host}:${embedded.mysql.port}/test?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8 +spring.datasource.username=${embedded.mysql.user} +spring.datasource.password=${embedded.mysql.password} +spring.datasource.type=com.zaxxer.hikari.HikariDataSource +spring.datasource.hikari.maximum-pool-size=100 +spring.datasource.hikari.minimum-idle=10 +spring.datasource.hikari.idle-timeout=300000 +spring.datasource.hikari.auto-commit=true +spring.datasource.hikari.pool-name=DatebookHikariCP +spring.datasource.hikari.max-lifetime=1800000 +spring.datasource.hikari.connection-timeout=30000 +spring.datasource.hikari.connection-test-query=SELECT 1 + +# +# spring.kafka +spring.kafka.bootstrap-servers=${embedded.kafka.brokerList} +spring.kafka.consumer.group-id=metersphere_group_id +spring.kafka.consumer.debug.group-id=metersphere_group_id_${random.uuid} +spring.kafka.listener.missing-topics-fatal=false +spring.kafka.producer.properties.max.request.size=32428800 +spring.kafka.producer.batch-size=16384 +spring.kafka.consumer.properties.max.partition.fetch.bytes=52428800 +# mybatis +mybatis.configuration.cache-enabled=true +mybatis.configuration.lazy-loading-enabled=false +mybatis.configuration.aggressive-lazy-loading=true +mybatis.configuration.multiple-result-sets-enabled=true +mybatis.configuration.use-column-label=true +mybatis.configuration.auto-mapping-behavior=full +mybatis.configuration.default-statement-timeout=25000 +mybatis.configuration.map-underscore-to-camel-case=true +# view +spring.mvc.throw-exception-if-no-handler-found=true +# flyway enable +spring.flyway.enabled=true +spring.flyway.baseline-on-migrate=true +spring.flyway.locations=filesystem:../../app/src/main/resources/migration +spring.flyway.table=metersphere_version +spring.flyway.baseline-version=0 +spring.flyway.encoding=UTF-8 +spring.flyway.validate-on-migrate=false +# jmeter +jmeter.home=/opt/jmeter +# file upload +spring.servlet.multipart.max-file-size=500MB +spring.servlet.multipart.max-request-size=500MB +# i18n +spring.messages.basename=i18n/commons,i18n/api,i18n/issue,i18n/load,i18n/project,i18n/system,i18n/plan,i18n/functional,i18n/ui,i18n/workstation +# actuator +management.endpoints.web.exposure.include=* +management.endpoints.enabled-by-default=false +# redis +spring.session.timeout=43200s +spring.data.redis.host=${embedded.redis.host} +spring.data.redis.password=${embedded.redis.password} +spring.data.redis.port=${embedded.redis.port} +spring.session.redis.repository-type=indexed +# +spring.freemarker.check-template-location=false +spring.groovy.template.check-template-location=false +# +minio.endpoint=http://${embedded.minio.host}:${embedded.minio.port} +minio.access-key=${embedded.minio.accessKey} +minio.secret-key=${embedded.minio.secretKey} + +logging.level.org.springframework.jdbc.core=trace +logging.level.io.metersphere.sdk.mapper=debug +logging.level.io.metersphere.project.mapper=debug \ No newline at end of file diff --git a/backend/services/api-test/src/main/test/resources/bootstrap.properties b/backend/services/api-test/src/main/test/resources/bootstrap.properties new file mode 100644 index 0000000000..4517589df2 --- /dev/null +++ b/backend/services/api-test/src/main/test/resources/bootstrap.properties @@ -0,0 +1,13 @@ +# embedded config +embedded.containers.enabled=true +embedded.containers.forceShutdown=true +# mysql +embedded.mysql.enabled=true +embedded.mysql.encoding=utf8mb4 +embedded.mysql.collation=utf8mb4_general_ci +# redis +embedded.redis.enabled=true +# kafka +embedded.kafka.enabled=false +# minio +embedded.minio.enabled=false \ No newline at end of file