From 7de1e4368112c69c9763bd9d3cac2b6dbd81dd3d Mon Sep 17 00:00:00 2001 From: guoyuqi Date: Thu, 13 Jul 2023 17:40:01 +0800 Subject: [PATCH] feat: add system version check --- .../controller/SystemVersionController.java | 20 ++++++++++ .../system/service/SystemVersionService.java | 13 +++++++ .../SystemVersionControllerTest.java | 39 +++++++++++++++++++ 3 files changed, 72 insertions(+) create mode 100644 backend/services/system-setting/src/main/java/io/metersphere/system/controller/SystemVersionController.java create mode 100644 backend/services/system-setting/src/main/java/io/metersphere/system/service/SystemVersionService.java create mode 100644 backend/services/system-setting/src/test/java/io/metersphere/system/controller/SystemVersionControllerTest.java diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/controller/SystemVersionController.java b/backend/services/system-setting/src/main/java/io/metersphere/system/controller/SystemVersionController.java new file mode 100644 index 0000000000..99bc5295d5 --- /dev/null +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/controller/SystemVersionController.java @@ -0,0 +1,20 @@ +package io.metersphere.system.controller; + +import io.metersphere.system.service.SystemVersionService; +import jakarta.annotation.Resource; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RequestMapping("/system/version") +@RestController +public class SystemVersionController { + + @Resource + private SystemVersionService systemVersionService; + + @GetMapping("/current") + public String getVersion() { + return systemVersionService.getVersion(); + } +} diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/service/SystemVersionService.java b/backend/services/system-setting/src/main/java/io/metersphere/system/service/SystemVersionService.java new file mode 100644 index 0000000000..cf45ad7d93 --- /dev/null +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/service/SystemVersionService.java @@ -0,0 +1,13 @@ +package io.metersphere.system.service; + +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +@Transactional(rollbackFor = Exception.class) +public class SystemVersionService { + + public String getVersion() { + return System.getenv("MS_VERSION"); + } +} diff --git a/backend/services/system-setting/src/test/java/io/metersphere/system/controller/SystemVersionControllerTest.java b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/SystemVersionControllerTest.java new file mode 100644 index 0000000000..8378c65753 --- /dev/null +++ b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/SystemVersionControllerTest.java @@ -0,0 +1,39 @@ +package io.metersphere.system.controller; + +import base.BaseTest; +import io.metersphere.sdk.constants.SessionConstants; +import io.metersphere.sdk.dto.QueryResourcePoolRequest; +import io.metersphere.sdk.util.JSON; +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.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; + +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +@SpringBootTest +@AutoConfigureMockMvc +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) +public class SystemVersionControllerTest extends BaseTest { + + @Resource + private MockMvc mockMvc; + @Test + @Order(10) + void getVersion() throws Exception { + mockMvc.perform(MockMvcRequestBuilders.get("/system/version/current") + .header(SessionConstants.HEADER_TOKEN, sessionId) + .header(SessionConstants.CSRF_TOKEN, csrfToken) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()).andDo(print()); + } + +}