mirror of
https://gitee.com/fit2cloud-feizhiyun/MeterSphere.git
synced 2024-12-02 12:09:13 +08:00
feat(系统设置): license增加单元测试
This commit is contained in:
parent
387460f4e5
commit
9722337e15
@ -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> T parseObjectFromMvcResult(MvcResult mvcResult, Class<T> 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());
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user