OCT REM: 1. 重构中间件管理接口单元测试实现
This commit is contained in:
parent
a0078e2222
commit
925b11bede
|
@ -104,7 +104,7 @@ public class MiddlewareManagerApi {
|
|||
if (ret.getFirstParam() == ErrorCode.ERR_OK) {
|
||||
return ProtocolResp.result(DeploymentMiddlewareResp.builder().deploymentId(ret.getSecondParam()).build());
|
||||
} else {
|
||||
return ProtocolResp.result(ret.getFirstParam(),
|
||||
return ProtocolResp.result(ErrorCode.ERR_CALLDEVICE,
|
||||
ret.getFirstParam().getHttpCode(),
|
||||
HelperUtils.formatErrorMessage(ret.getFirstParam(),
|
||||
Collections.singletonList(ret.getSecondParam())));
|
||||
|
|
|
@ -2,23 +2,30 @@ package com.cmcc.magent.controller;
|
|||
|
||||
import com.cmcc.magent.common.ErrorCode;
|
||||
import com.cmcc.magent.common.MulReturnType;
|
||||
import com.cmcc.magent.misc.ApiContextUtils;
|
||||
import com.cmcc.magent.misc.HelperUtils;
|
||||
import com.cmcc.magent.misc.MessageUtil;
|
||||
import com.cmcc.magent.service.MiddlewareManagerService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockedStatic;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
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.setup.MockMvcBuilders;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.mockStatic;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
||||
|
@ -31,10 +38,9 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
|
|||
* @version 1.0.0
|
||||
* @since 2025-02-24
|
||||
*/
|
||||
@SpringBootTest
|
||||
@DisplayName("中间件管理接口测试")
|
||||
@Slf4j
|
||||
public class MiddlewareManagerApiTest {
|
||||
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@Mock
|
||||
|
@ -60,13 +66,29 @@ public class MiddlewareManagerApiTest {
|
|||
"deploymentCmd":"echo deploymentCmd","configCmd":"echo configCmd","startCmd":"echo startCmd",\
|
||||
"stopCmd":"echo stopCmd","restartCmd":"echo restartCmd","uninstallCmd":"echo uninstallCmd"}}""";
|
||||
|
||||
private MockedStatic<MessageUtil> mockMessageUtils;
|
||||
|
||||
private MockedStatic<HelperUtils> mockHelperUtils;
|
||||
|
||||
private MockedStatic<ApiContextUtils> mockApiContextUtils;
|
||||
|
||||
/**
|
||||
* 在每个测试方法执行前进行初始化操作。
|
||||
*/
|
||||
@BeforeEach
|
||||
public void setup() {
|
||||
MockitoAnnotations.openMocks(this);
|
||||
mockMvc = MockMvcBuilders.standaloneSetup(middlewareManagerApi).build();
|
||||
mockMvc = MockMvcBuilders.standaloneSetup(middlewareManagerApi).build();
|
||||
mockMessageUtils = mockStatic(MessageUtil.class);
|
||||
mockHelperUtils = mockStatic(HelperUtils.class);
|
||||
mockApiContextUtils = mockStatic(ApiContextUtils.class);
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
public void tearDown() {
|
||||
mockMessageUtils.close();
|
||||
mockHelperUtils.close();
|
||||
mockApiContextUtils.close();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -86,9 +108,13 @@ public class MiddlewareManagerApiTest {
|
|||
any(List.class))).thenReturn(new MulReturnType<>(ErrorCode.ERR_OK,
|
||||
"deploymentId"));
|
||||
|
||||
mockMvc.perform(post("/api/middleware/deployment").contentType(MediaType.APPLICATION_JSON).content(reqString))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.msgContent.deploymentId").value("deploymentId"));
|
||||
mockHelperUtils.when(() -> HelperUtils.stringNotEmptyOrNull(anyString())).thenReturn(true);
|
||||
mockHelperUtils.when(() -> HelperUtils.validate(any(), any())).thenReturn(new ArrayList<>());
|
||||
mockMessageUtils.when(() -> MessageUtil.get(anyString(), anyString())).thenReturn("");
|
||||
|
||||
log.info("Result: {}", mockMvc.perform(post("/api/middleware/deployment").contentType(MediaType.APPLICATION_JSON)
|
||||
.content(reqString)).andExpect(status().isOk()).andExpect(
|
||||
jsonPath("$.msgContent.deploymentId").value("deploymentId")).andReturn().getResponse().getContentAsString());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -100,19 +126,31 @@ public class MiddlewareManagerApiTest {
|
|||
@DisplayName("合法请求服务错误")
|
||||
@SuppressWarnings("unchecked")
|
||||
public void deploymentMiddleware_ValidRequest_ServiceError() throws Exception {
|
||||
String reqString = String.format(fmt, System.currentTimeMillis());
|
||||
ErrorCode err = ErrorCode.ERR_SYSTEMEXCEPTION;
|
||||
ErrorCode errReturn = ErrorCode.ERR_CALLDEVICE;
|
||||
String reqString = String.format(fmt, System.currentTimeMillis());
|
||||
|
||||
when(middlewareManagerService.executeCommandTask(anyString(),
|
||||
anyString(),
|
||||
any(HashMap.class),
|
||||
any(List.class))).thenReturn(new MulReturnType<>(ErrorCode.ERR_SYSTEMEXCEPTION,
|
||||
"error"));
|
||||
any(List.class))).thenReturn(new MulReturnType<>(err, err.getEnumString()));
|
||||
|
||||
mockMvc.perform(post("/api/middleware/deployment").contentType(MediaType.APPLICATION_JSON).content(reqString))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.msgContent.status").value(14))
|
||||
.andExpect(jsonPath("$.msgContent.message[0]").value("0: 系统异常"))
|
||||
.andExpect(jsonPath("$.msgContent.message[1]").value("1: error"));
|
||||
mockMessageUtils.when(() -> MessageUtil.get(anyString(), anyString())).thenReturn(err.getEnumString());
|
||||
mockHelperUtils.when(() -> HelperUtils.validate(any(), any(Class[].class))).thenReturn(new ArrayList<>());
|
||||
mockHelperUtils.when(() -> HelperUtils.formatErrorMessage(any(ErrorCode.class), any(List.class))).thenReturn(new String[] {
|
||||
"0: " + ErrorCode.ERR_CALLDEVICE.getEnumString(), "1: " + err.getEnumString()});
|
||||
|
||||
|
||||
log.info("Result: {}", mockMvc.perform(post("/api/middleware/deployment").contentType(MediaType.APPLICATION_JSON)
|
||||
.content(reqString))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.msgContent.status").value(errReturn.getCode()))
|
||||
.andExpect(jsonPath("$.msgContent.message[0]").value("0: " +
|
||||
errReturn.getEnumString()))
|
||||
.andExpect(jsonPath("$.msgContent.message[1]").value("1: " + err.getEnumString()))
|
||||
.andReturn()
|
||||
.getResponse()
|
||||
.getContentAsString());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -122,13 +160,30 @@ public class MiddlewareManagerApiTest {
|
|||
*/
|
||||
@Test
|
||||
@DisplayName("非法请求验证错误")
|
||||
@SuppressWarnings("unchecked")
|
||||
public void deploymentMiddleware_InvalidRequest_ValidationErrors() throws Exception {
|
||||
String reqString = String.format(errFmt, System.currentTimeMillis());
|
||||
String validErr = "middleware Not Empty";
|
||||
ErrorCode err = ErrorCode.ERR_PARAMEXCEPTION;
|
||||
String reqString = String.format(errFmt, System.currentTimeMillis());
|
||||
|
||||
mockMvc.perform(post("/api/middleware/deployment").contentType(MediaType.APPLICATION_JSON).content(reqString))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.msgContent.status").value(23))
|
||||
.andExpect(jsonPath("$.msgContent.message[0]").value("0: 参数异常"))
|
||||
.andExpect(jsonPath("$.msgContent.message[1]").value("1: middleware Not Empty"));
|
||||
mockHelperUtils.when(() -> HelperUtils.validate(any(), any(Class[].class))).thenReturn(new ArrayList<>() {{
|
||||
add(validErr);
|
||||
}});
|
||||
|
||||
mockMessageUtils.when(() -> MessageUtil.get(anyString(), anyString())).thenReturn(err.getEnumString());
|
||||
mockHelperUtils.when(() -> HelperUtils.validate(any(), any())).thenReturn(new ArrayList<>());
|
||||
mockHelperUtils.when(() -> HelperUtils.formatErrorMessage(any(ErrorCode.class), any(List.class))).thenReturn(new String[] {
|
||||
"0: " + err.getEnumString(), "1: " + validErr});
|
||||
|
||||
log.info("Result: {}", mockMvc.perform(post("/api/middleware/deployment").contentType(MediaType.APPLICATION_JSON)
|
||||
.content(reqString))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.msgContent.status").value(err.getCode()))
|
||||
.andExpect(jsonPath("$.msgContent.message[0]").value("0: " +
|
||||
err.getEnumString()))
|
||||
.andExpect(jsonPath("$.msgContent.message[1]").value("1: " + validErr))
|
||||
.andReturn()
|
||||
.getResponse()
|
||||
.getContentAsString());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue