From 9722337e156a051f271ecc2088774a1905818d4e Mon Sep 17 00:00:00 2001 From: wxg0103 <727495428@qq.com> Date: Mon, 18 Sep 2023 17:36:38 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E7=B3=BB=E7=BB=9F=E8=AE=BE=E7=BD=AE):=20l?= =?UTF-8?q?icense=E5=A2=9E=E5=8A=A0=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/LicenseControllerTests.java | 89 +++++++++++++++++++ .../system/mock/LicenseServiceMockImpl.java | 30 +++++++ 2 files changed, 119 insertions(+) create mode 100644 backend/services/system-setting/src/test/java/io/metersphere/system/controller/LicenseControllerTests.java create mode 100644 backend/services/system-setting/src/test/java/io/metersphere/system/mock/LicenseServiceMockImpl.java diff --git a/backend/services/system-setting/src/test/java/io/metersphere/system/controller/LicenseControllerTests.java b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/LicenseControllerTests.java new file mode 100644 index 0000000000..2965921f22 --- /dev/null +++ b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/LicenseControllerTests.java @@ -0,0 +1,89 @@ +package io.metersphere.system.controller; + +import io.metersphere.sdk.constants.SessionConstants; +import io.metersphere.sdk.dto.LicenseDTO; +import io.metersphere.sdk.util.JSON; +import io.metersphere.system.base.BaseTest; +import io.metersphere.system.controller.handler.ResultHolder; +import jakarta.annotation.Resource; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.MethodOrderer; +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.MvcResult; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; + +import java.nio.charset.StandardCharsets; + +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 LicenseControllerTests extends BaseTest { + + @Resource + private MockMvc mockMvc; + + private final String validate = "/license/validate"; + private final String add = "/license/add"; + + private MvcResult responsePost(String url, Object param) throws Exception { + return mockMvc.perform(MockMvcRequestBuilders.post(url) + .header(SessionConstants.HEADER_TOKEN, sessionId) + .header(SessionConstants.CSRF_TOKEN, csrfToken) + .content(JSON.toJSONString(param)) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(content().contentType(MediaType.APPLICATION_JSON)).andReturn(); + } + + public static T parseObjectFromMvcResult(MvcResult mvcResult, Class parseClass) { + try { + String returnData = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8); + ResultHolder resultHolder = JSON.parseObject(returnData, ResultHolder.class); + //返回请求正常 + Assertions.assertNotNull(resultHolder); + return JSON.parseObject(JSON.toJSONString(resultHolder.getData()), parseClass); + } catch (Exception ignore) { + } + return null; + } + + private MvcResult responseGet(String url) throws Exception { + return mockMvc.perform(MockMvcRequestBuilders.get(url) + .header(SessionConstants.HEADER_TOKEN, sessionId) + .header(SessionConstants.CSRF_TOKEN, csrfToken) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(content().contentType(MediaType.APPLICATION_JSON)).andReturn(); + } + + + @Test + public void testAdd() throws Exception { + + MvcResult mvcResult = responsePost(add, "TgRuVfH7On"); + Assertions.assertEquals(200, mvcResult.getResponse().getStatus()); + LicenseDTO licenseDTO = parseObjectFromMvcResult(mvcResult, LicenseDTO.class); + Assertions.assertNotNull(licenseDTO); + Assertions.assertEquals("OK", licenseDTO.getStatus()); + } + + @Test + public void testValidate() throws Exception { + MvcResult mvcResult = responseGet(validate); + Assertions.assertEquals(200, mvcResult.getResponse().getStatus()); + LicenseDTO licenseDTO = parseObjectFromMvcResult(mvcResult, LicenseDTO.class); + Assertions.assertNotNull(licenseDTO); + Assertions.assertEquals("OK", licenseDTO.getStatus()); + } + + + +} diff --git a/backend/services/system-setting/src/test/java/io/metersphere/system/mock/LicenseServiceMockImpl.java b/backend/services/system-setting/src/test/java/io/metersphere/system/mock/LicenseServiceMockImpl.java new file mode 100644 index 0000000000..3d41955ee6 --- /dev/null +++ b/backend/services/system-setting/src/test/java/io/metersphere/system/mock/LicenseServiceMockImpl.java @@ -0,0 +1,30 @@ +package io.metersphere.system.mock; + +import io.metersphere.sdk.dto.LicenseDTO; +import io.metersphere.system.service.LicenseService; +import org.springframework.stereotype.Service; +@Service +public class LicenseServiceMockImpl implements LicenseService { + + + @Override + public LicenseDTO refreshLicense() { + LicenseDTO licenseDTO = new LicenseDTO(); + licenseDTO.setStatus("OK"); + return licenseDTO; + } + + @Override + public LicenseDTO validate() { + LicenseDTO licenseDTO = new LicenseDTO(); + licenseDTO.setStatus("OK"); + return licenseDTO; + } + + @Override + public LicenseDTO addLicense(String licenseCode, String userId) { + LicenseDTO licenseDTO = new LicenseDTO(); + licenseDTO.setStatus("OK"); + return licenseDTO; + } +}