OCT REM: 1. 添加操作记录单元测试

This commit is contained in:
HuangXin 2025-02-26 19:24:31 +08:00
parent 44dff747fe
commit 51485e1df4
2 changed files with 151 additions and 2 deletions

View File

@ -32,6 +32,7 @@ import com.cmcc.magent.misc.SpringBootBeanUtilsTest;
import com.cmcc.magent.pojo.mapper.IObjectConvertImplTest;
import com.cmcc.magent.pojo.vo.ProtocolRespTest;
import com.cmcc.magent.service.impl.MiddlewareManagerServiceImplTest;
import com.cmcc.magent.service.impl.OperationLogServiceImplTest;
import com.cmcc.magent.service.impl.PlatformApiServiceImplTest;
import com.cmcc.magent.service.impl.ProtocolSecurityServiceImplTest;
import com.cmcc.magent.service.impl.ResourceUsageServiceImplTest;
@ -54,7 +55,7 @@ import org.springframework.scheduling.annotation.EnableScheduling;
* <p>
* 测试类包括
* <ul>
* <li>{@link CommonFrameworkApiTest}</li>
* <li>{@link CommonFrameworkApiTest}, {@link OperationLogServiceImplTest}</li>
* <li>{@link EnumCoverTest}</li>
* <li>{@link HelperUtilsTest}</li>
* <li>{@link PlatformApiServiceImplTest}</li>
@ -103,7 +104,7 @@ import org.springframework.scheduling.annotation.EnableScheduling;
MessageUtilTest.class, IObjectConvertImplTest.class, ExecutingCommandTest.class, HttpClientUtilsTest.class,
FileDownloaderTest.class, CustomLifecycleTest.class, MiddlewareDataMapperTest.class, MiddlewareAgentApplicationTest.class,
ValidFixValuesImplTest.class, AutoExternStringJsonProcessTest.class, MiddlewareManagerServiceImplTest.class,
MiddlewareManagerApiTest.class, RestfulLogAspectTest.class, RequestBodyFilterTest.class})
MiddlewareManagerApiTest.class, RestfulLogAspectTest.class, RequestBodyFilterTest.class, OperationLogServiceImplTest.class})
@DisplayName("接口集成测试")
@Suite
@EnableScheduling

View File

@ -0,0 +1,148 @@
package com.cmcc.magent.service.impl;
import com.cmcc.magent.annotation.OperationLogAnnotation;
import com.cmcc.magent.common.ErrorCode;
import com.cmcc.magent.config.CommonConfigure;
import com.cmcc.magent.config.ObjectMapperProvider;
import com.cmcc.magent.misc.ApiContextUtils;
import com.cmcc.magent.misc.HelperUtils;
import com.cmcc.magent.misc.HttpUtils;
import com.cmcc.magent.misc.JsonUtils;
import com.cmcc.magent.pojo.po.ApiContext;
import jakarta.servlet.http.HttpServletRequest;
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.autoconfigure.web.ServerProperties;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.when;
/**
* OperationLogServiceImpl 测试类用于测试 OperationLogServiceImpl 日志服务的功能
* 该类通过模拟 HTTP 请求API 上下文工具类等验证日志服务在不同场景下的行为
*
* @author huangxin@cmhi.chinamobile.com
* @version 1.0.0
* @since 2025-02-26
*/
@DisplayName("操作日志测试")
public class OperationLogServiceImplTest {
@InjectMocks
private OperationLogServiceImpl operationLogService;
@Mock
private ServerProperties serverProperties;
private MockedStatic<HelperUtils> mockHelperUtils;
private MockedStatic<HttpUtils> mockHttpUtils;
private MockedStatic<JsonUtils> mockJsonUtils;
private MockedStatic<ObjectMapperProvider> mockObjectMapperProvider;
private MockedStatic<ApiContextUtils> mockApiContextUtils;
/**
* 在每个测试方法执行前初始化测试环境
* 包括初始化 Mock 对象模拟静态方法等
*/
@BeforeEach
public void setUp() {
MockitoAnnotations.openMocks(this);
CommonConfigure.PROJECT_PREFIX_URL = "/api";
mockHelperUtils = mockStatic(HelperUtils.class);
mockHttpUtils = mockStatic(HttpUtils.class);
mockObjectMapperProvider = mockStatic(ObjectMapperProvider.class);
mockJsonUtils = mockStatic(JsonUtils.class);
mockApiContextUtils = mockStatic(ApiContextUtils.class);
}
/**
* 在每个测试方法执行后清理测试环境
* 包括关闭模拟的静态方法
*/
@AfterEach
public void tearDown() {
mockHelperUtils.close();
mockHttpUtils.close();
mockJsonUtils.close();
mockObjectMapperProvider.close();
mockApiContextUtils.close();
}
/**
* 测试日志服务在传入完整参数时是否正常调用
* 验证在传入完整参数的情况下日志服务是否被正确调用
*/
@Test
@DisplayName("完整参数调用")
public void systemOperationLog_fullParamsCall() {
HttpServletRequest request = mock(HttpServletRequest.class);
when(request.getRequestURI()).thenReturn("/api/error/someError");
mockHelperUtils.when(() -> HelperUtils.stringNotEmptyOrNull(anyString())).thenReturn(true);
operationLogService.systemOperationLog("testCall", request, new Object(), "testModule", "testType", "testDesc", ErrorCode.ERR_OK);
}
/**
* 测试日志服务在传入简化参数带注解时是否正常调用
* 验证在传入简化参数带注解的情况下日志服务是否被正确调用
*/
@Test
@DisplayName("简单参数调用")
public void systemOperationLog_simpleParamsCall() {
HttpServletRequest request = mock(HttpServletRequest.class);
OperationLogAnnotation annotation = mock(OperationLogAnnotation.class);
when(annotation.OperationModule()).thenReturn("testModule");
when(annotation.OperationType()).thenReturn("testType");
when(annotation.OperationDesc()).thenReturn("testDesc");
when(request.getRequestURI()).thenReturn("/api/error/someError");
mockHelperUtils.when(() -> HelperUtils.stringNotEmptyOrNull(anyString())).thenReturn(true);
operationLogService.systemOperationLog("testCall", request, new Object(), annotation, ErrorCode.ERR_OK);
}
/**
* 测试日志服务在传入简化参数注解为空时是否正常调用
* 验证在传入简化参数注解为空的情况下日志服务是否被正确调用
*/
@Test
@DisplayName("简单参数调用(注解为空)")
public void systemOperationLog_simpleParamsCall_annotationNull() {
HttpServletRequest request = mock(HttpServletRequest.class);
mockHelperUtils.when(() -> HelperUtils.stringNotEmptyOrNull(anyString())).thenReturn(true);
operationLogService.systemOperationLog("testCall", request, new Object(), null, ErrorCode.ERR_OK);
}
/**
* 测试日志服务在有效请求时是否成功记录日志
* 验证在请求有效的情况下日志是否被成功记录
*
*/
@Test
@DisplayName("有效请求")
public void systemOperationLog_ValidRequest_LogsSuccessfully() {
HttpServletRequest request = mock(HttpServletRequest.class);
when(request.getRequestURI()).thenReturn("/api/somePath");
when(request.getMethod()).thenReturn("GET");
when(request.getHeaderNames()).thenReturn(new java.util.Vector<String>().elements());
when(request.getHeader(anyString())).thenReturn("someHeaderValue");
ServerProperties.Servlet servlet = mock(ServerProperties.Servlet.class);
when(serverProperties.getServlet()).thenReturn(servlet);
when(servlet.getContextPath()).thenReturn("/api");
ApiContext apiContext = new ApiContext();
apiContext.setRequestBody("{\"timeStamp\": 1609459200000}");
apiContext.setRequestTime(System.currentTimeMillis());
mockHttpUtils.when(() -> HttpUtils.getHttpRequestHeaders(any(HttpServletRequest.class))).thenReturn(
"{\"someHeader\": \"someHeaderValue\"}");
mockHttpUtils.when(() -> HttpUtils.getHttpRequestSrcIp(any(HttpServletRequest.class))).thenReturn("127.0.0.1");
mockHelperUtils.when(() -> HelperUtils.truncateString(anyString(), anyInt())).thenReturn("{\"someHeader\": \"someHeaderValue\"}");
mockApiContextUtils.when(ApiContextUtils::get).thenReturn(apiContext);
mockHelperUtils.when(() -> HelperUtils.stringNotEmptyOrNull(anyString())).thenReturn(true);
operationLogService.systemOperationLog("testCall", request, new Object(), "testModule", "testType", "testDesc", ErrorCode.ERR_OK);
}
}