OCT REM: 1. 增加获取系统相关接口

2. 增加获取系统信息相关测试用例
This commit is contained in:
HuangXin 2025-01-14 10:16:34 +08:00
parent 64d88e366c
commit 970bc7818a
12 changed files with 546 additions and 182 deletions

View File

@ -86,7 +86,6 @@ import java.lang.annotation.Target;
*/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@JsonSerialize(using = AutoExternStringJsonProcess.class)
public @interface AutoExternString {
/**

View File

@ -73,6 +73,8 @@ public class AutoExternStringJsonProcess extends JsonSerializer<Number> implemen
*/
private Class<? extends Enum<?>> enumClass = null;
private boolean enableFormat = false;
/**
* 默认构造方法
*/
@ -92,12 +94,14 @@ public class AutoExternStringJsonProcess extends JsonSerializer<Number> implemen
String suffix,
UtilsFormatType formatType,
String units,
Class<? extends Enum<?>> classType) {
Class<? extends Enum<?>> classType,
boolean enableFormat) {
this.prefix = prefix;
this.suffix = suffix;
this.formatType = formatType;
this.enumClass = classType;
this.units = units;
this.enableFormat = enableFormat;
}
/**
@ -115,6 +119,7 @@ public class AutoExternStringJsonProcess extends JsonSerializer<Number> implemen
public void serialize(Number value, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
Long numericValue = null;
// 将字段值转换为 Long 类型
if (value instanceof Long) {
numericValue = (Long) value;
@ -126,6 +131,7 @@ public class AutoExternStringJsonProcess extends JsonSerializer<Number> implemen
// 写入原始字段值
jsonGenerator.writeNumber(numericValue);
if (enableFormat) {
// 生成额外字段名
String filedName = prefix + jsonGenerator.getOutputContext().getCurrentName() + suffix;
jsonGenerator.writeFieldName(filedName);
@ -158,6 +164,7 @@ public class AutoExternStringJsonProcess extends JsonSerializer<Number> implemen
} else {
jsonGenerator.writeString(numericValue.toString());
}
}
} else {
// 如果值为空或不支持则使用默认序列化方式
serializerProvider.defaultSerializeValue(value, jsonGenerator);
@ -183,7 +190,7 @@ public class AutoExternStringJsonProcess extends JsonSerializer<Number> implemen
if (annotation != null) {
// 根据注解配置创建新的序列化器
return new AutoExternStringJsonProcess(annotation.prefix(), annotation.suffix(), annotation.format(), annotation.units(),
annotation.enumClass());
annotation.enumClass(), true);
}
}
// 如果字段不存在注解返回默认实例

View File

@ -2,7 +2,10 @@ package com.cmhi.magent.controller;
import com.cmhi.magent.annotation.EncryptionProtocol;
import com.cmhi.magent.annotation.OperationLogAnnotation;
import com.cmhi.magent.pojo.vo.GetFileStoreInfoResp;
import com.cmhi.magent.pojo.vo.GetHwInfoResp;
import com.cmhi.magent.pojo.vo.GetMemoryInfoResp;
import com.cmhi.magent.pojo.vo.GetNetworkInfoResp;
import com.cmhi.magent.pojo.vo.GetOsInfoResp;
import com.cmhi.magent.pojo.vo.GetProcessorInfoResp;
import com.cmhi.magent.pojo.vo.ProtocolResp;
@ -100,4 +103,52 @@ public class SystemInfoApi {
public ProtocolResp<GetHwInfoResp> getHwInfo() {
return ProtocolResp.result(GetHwInfoResp.builder().hwInfo(systemInfoService.getHardwareInfo()).build());
}
/**
* 获取网络信息
*
* <p>该接口返回系统网络相关信息返回数据通过加密协议进行保护并记录操作日志</p>
*
* @return 封装了网络信息的响应对象
*/
@GetMapping("/networkInfo")
@EncryptionProtocol
@OperationLogAnnotation(OperationModule = "SYSLOG_MOD_SYSINFO",
OperationType = "SYSLOG_TYPE_READ",
OperationDesc = "SYSLOG_DESC_GET_HW_INFO")
public ProtocolResp<GetNetworkInfoResp> getNetworkInfo() {
return ProtocolResp.result(GetNetworkInfoResp.builder().networkInfo(systemInfoService.getNetworkInfo()).build());
}
/**
* 获取内存信息
*
* <p>该接口返回系统内存相关信息返回数据通过加密协议进行保护并记录操作日志</p>
*
* @return 封装了网络信息的响应对象
*/
@GetMapping("/memoryInfo")
@EncryptionProtocol
@OperationLogAnnotation(OperationModule = "SYSLOG_MOD_SYSINFO",
OperationType = "SYSLOG_TYPE_READ",
OperationDesc = "SYSLOG_DESC_GET_HW_INFO")
public ProtocolResp<GetMemoryInfoResp> geMemoryInfo() {
return ProtocolResp.result(GetMemoryInfoResp.builder().memoryInfo(systemInfoService.getMemoryInfo()).build());
}
/**
* 获取文件存储信息
*
* <p>该接口返回系统文件系统以及磁盘相关信息返回数据通过加密协议进行保护并记录操作日志</p>
*
* @return 封装了文件存储信息的响应对象
*/
@GetMapping("/fileStoreInfo")
@EncryptionProtocol
@OperationLogAnnotation(OperationModule = "SYSLOG_MOD_SYSINFO",
OperationType = "SYSLOG_TYPE_READ",
OperationDesc = "SYSLOG_DESC_GET_HW_INFO")
public ProtocolResp<GetFileStoreInfoResp> geFileStoreInfo() {
return ProtocolResp.result(GetFileStoreInfoResp.builder().fileStoreInfo(systemInfoService.getFileStoreInfo()).build());
}
}

View File

@ -5,145 +5,55 @@ import com.cmhi.magent.common.UtilsFormatType;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import java.util.List;
/**
* 网络信息类
* NetworkInfo 类表示系统的网络信息包括网络接口主机名域名DNS 服务器和网关信息
* <p>
* 此类用于封装网络的详细信息包括网络名称网络类型网络状态网络速度等
* 主要用于监控系统网络的状态例如网络连接的速度和稳定性
* 该类通过 Lombok {@code @Data} 注解自动生成 getter/setter 方法
* 同时集成了 Swagger 3 的注释以支持接口文档的生成
* </p>
*
* <p>
* 依赖于 Lombok {@code @Data} 注解自动生成以下方法
* </p>
* <ul>
* <li>Getter Setter 方法</li>
* <li>{@code toString()} 方法</li>
* <li>{@code equals()} {@code hashCode()} 方法</li>
* </ul>
*
* <p>
* 此类还集成了自定义注解 {@link AutoExternString}用于在 JSON 序列化时对字段进行额外处理
* 例如自动将网络速度值格式化为更易识别的单位 MbpsKbps
* </p>
*
* <p><b>使用示例</b></p>
* <pre>
* NetworkInfo networkInfo = new NetworkInfo();
* networkInfo.setNetworkName("Wi-Fi");
* networkInfo.setNetworkType("Wireless");
* networkInfo.setNetworkSpeed(1024L); // 设置网络速度为 1 Mbps
* </pre>
*
* <p><b>JSON 输出示例</b></p>
* <pre>
* {
* "networkName": "Wi-Fi",
* "networkType": "Wireless",
* "networkSpeed": 1024,
* "networkSpeedStr": "1 Mbps"
* }
* </pre>
*
* <p>注意{@code @AutoExternString} 注解会自动根据字段值转换为适合的单位并生成额外的格式化字符串</p>
*
* <p>例如 {@code 1024L}字节/ JSON 中将自动格式化为 {@code "1 Mbps"}</p>
*
* @author huangxin@cmhi.chinamobile.com
* @version 1.0.0
* @since 2025-01-13
* * @version 1.0.0
* * @since 2025-01-13
*/
@Data
public class NetworkInfo {
/**
* 网络名称
* <p>
* 该字段记录网络的名称通常用于标识不同的网络连接 Wi-Fi以太网等
* JSON 序列化时该字段直接输出原始值
* </p>
*
* <p><b>示例值</b></p>
* <ul>
* <li>{@code "Wi-Fi"}</li>
* </ul>
*
* <p><b>JSON 输出示例</b></p>
* <pre>
* {
* "networkName": "Wi-Fi"
* }
* </pre>
* 系统中所有的网络接口信息
*/
@Schema(description = "网络名称。", example = "Wi-Fi")
private String networkName;
@Schema(description = "系统中所有的网络接口信息")
private List<IfInterfaceInfo> ifNetwork;
/**
* 网络类型
* <p>
* 该字段记录网络的类型如无线网络Wireless有线网络Wired
* JSON 序列化时该字段直接输出原始值
* </p>
*
* <p><b>示例值</b></p>
* <ul>
* <li>{@code "Wireless"}</li>
* </ul>
*
* <p><b>JSON 输出示例</b></p>
* <pre>
* {
* "networkType": "Wireless"
* }
* </pre>
* 系统的主机名
*/
@Schema(description = "网络类型。", example = "Wireless")
private String networkType;
@Schema(description = "系统的主机名")
private String hostName;
/**
* 网络状态
* <p>
* 该字段记录网络的当前状态如已连接Connected断开连接Disconnected
* JSON 序列化时该字段直接输出原始值
* </p>
*
* <p><b>示例值</b></p>
* <ul>
* <li>{@code "Connected"}</li>
* </ul>
*
* <p><b>JSON 输出示例</b></p>
* <pre>
* {
* "networkStatus": "Connected"
* }
* </pre>
* 系统所在的域名
*/
@Schema(description = "网络状态。", example = "Connected")
private String networkStatus;
@Schema(description = "系统所在的域名")
private String domainName;
/**
* 网络速度单位字节/
* <p>
* 该字段记录网络的当前速度通常用于表示网络连接的带宽或数据传输速率
* JSON 序列化时通过 {@link AutoExternString} 注解生成一个格式化后的额外字段
* </p>
*
* <p><b>示例值</b></p>
* <ul>
* <li>{@code 1024L}表示 1 Mbps 网络速度</li>
* </ul>
*
* <p><b>JSON 输出示例</b></p>
* <pre>
* {
* "networkSpeed": 1024,
* "networkSpeedStr": "1 Mbps"
* }
* </pre>
*
* @see AutoExternString
* 系统配置的 DNS 服务器地址列表
*/
@Schema(description = "网络速度(单位:字节/秒)。", example = "1024")
@AutoExternString(format = UtilsFormatType.FORMAT_BYTES)
private Long networkSpeed;
@Schema(description = "系统配置的 DNS 服务器地址列表")
private String[] dnsServers;
/**
* 系统的 IPv4 默认网关地址
*/
@Schema(description = "系统的 IPv4 默认网关地址")
private String ipv4DefaultGateway;
/**
* 系统的 IPv6 默认网关地址
*/
@Schema(description = "系统的 IPv6 默认网关地址")
private String ipv6DefaultGateway;
}

View File

@ -1,5 +1,7 @@
package com.cmhi.magent.pojo.po;
import com.cmhi.magent.annotation.AutoExternString;
import com.cmhi.magent.common.UtilsFormatType;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
@ -116,6 +118,7 @@ public class ProcessorInfo {
* </p>
*/
@Schema(description = "处理器的标称主频单位Hz", example = "3800000000")
@AutoExternString(format = UtilsFormatType.FORMAT_UNITS, units = "Hz")
private Long cpuVendorFreq;
/**

View File

@ -0,0 +1,56 @@
package com.cmhi.magent.pojo.vo;
import com.cmhi.magent.pojo.po.BaseRespStatus;
import com.cmhi.magent.pojo.po.FileStoreInfo;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
/**
* 文件存储信息响应类 {@code GetFileStoreInfoResp}
* <p>
* 用于封装文件存储信息的响应结果继承自 {@link BaseRespStatus}包含状态码消息以及文件存储的详细信息
* </p>
*
* <p>
* 核心功能
* <ul>
* <li>继承基础响应状态类统一包含状态码和消息</li>
* <li>封装文件存储相关的详细信息通过 {@code fileStoreInfo} 字段返回</li>
* </ul>
* </p>
*
* <p>
* 使用场景
* <ul>
* <li>用于 RESTful API 的文件存储信息查询接口</li>
* <li>适用于系统监控模块获取文件存储的详细配置信息例如总存储空间可用存储空间文件系统类型等</li>
* </ul>
* </p>
*
* @author huangxin@cmhi.chinamobile.com
* @version 1.0.0
* @since 2025-01-07
*/
@Schema(name = "文件存储信息", description = "包含文件存储的详细信息,如总存储空间、可用存储空间、文件系统类型等。")
@EqualsAndHashCode(callSuper = true)
@Data
@Builder
@JsonPropertyOrder({"fileStoreInfo", "status", "message"})
@AllArgsConstructor
@NoArgsConstructor
public class GetFileStoreInfoResp extends BaseRespStatus {
/**
* 文件存储信息内容
* <p>
* 包含文件存储的详细信息例如总存储空间可用存储空间文件系统类型等
* 类型为 {@link FileStoreInfo}
* </p>
*/
private FileStoreInfo fileStoreInfo;
}

View File

@ -0,0 +1,58 @@
package com.cmhi.magent.pojo.vo;
import com.cmhi.magent.pojo.po.BaseRespStatus;
import com.cmhi.magent.pojo.po.MemoryInfo;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
/**
* 内存信息响应类 {@code GetMemoryInfoResp}
* <p>
* 用于封装内存信息的响应结果继承自 {@link BaseRespStatus}包含状态码消息以及内存的详细信息
* </p>
*
* <p>
* 核心功能
* <ul>
* <li>继承基础响应状态类统一包含状态码和消息</li>
* <li>封装内存相关的详细信息通过 {@code memoryInfo} 字段返回</li>
* </ul>
* </p>
*
* <p>
* 使用场景
* <ul>
* <li>用于 RESTful API 的内存信息查询接口</li>
* <li>适用于系统监控模块获取内存的详细配置信息例如总内存可用内存内存使用率等</li>
* </ul>
* </p>
*
* </p>
*
* @author huangxin@cmhi.chinamobile.com
* @version 1.0.0
* @since 2025-01-07
*/
@Schema(name = "内存信息", description = "包含内存的详细信息,如总内存、可用内存、内存使用率等。")
@EqualsAndHashCode(callSuper = true)
@Data
@Builder
@JsonPropertyOrder({"memoryInfo", "status", "message"})
@AllArgsConstructor
@NoArgsConstructor
public class GetMemoryInfoResp extends BaseRespStatus {
/**
* 内存信息内容
* <p>
* 包含内存的详细信息例如总内存可用内存内存使用率等
* 类型为 {@link MemoryInfo}
* </p>
*/
private MemoryInfo memoryInfo;
}

View File

@ -0,0 +1,74 @@
package com.cmhi.magent.pojo.vo;
import com.cmhi.magent.pojo.po.BaseRespStatus;
import com.cmhi.magent.pojo.po.NetworkInfo;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
/**
* 网络信息响应类 {@code GetNetworkInfoResp}
* <p>
* 用于封装网络信息的响应结果继承自 {@link BaseRespStatus}包含状态码消息以及网络的详细信息
* </p>
*
* <p>
* 核心功能
* <ul>
* <li>继承基础响应状态类统一包含状态码和消息</li>
* <li>封装网络相关的详细信息通过 {@code networkInfo} 字段返回</li>
* </ul>
* </p>
*
* <p>
* 使用场景
* <ul>
* <li>用于 RESTful API 的网络信息查询接口</li>
* <li>适用于系统监控模块获取网络的详细配置信息例如IP地址MAC地址网络接口等</li>
* </ul>
* </p>
*
* <p>
* JSON 输出示例
* <pre>
* {
* "status": "SUCCESS",
* "message": "Network information retrieved successfully",
* "networkInfo": {
* "ipAddress": "192.168.1.100",
* "macAddress": "00:11:22:33:44:55",
* "networkInterfaces": [
* "eth0",
* "wlan0"
* ]
* }
* }
* </pre>
* </p>
*
* @author huangxin@cmhi.chinamobile.com
* @version 1.0.0
* @since 2025-01-07
*/
@Schema(name = "网络信息", description = "包含网络的详细信息如IP地址、MAC地址、网络接口等。")
@EqualsAndHashCode(callSuper = true)
@Data
@Builder
@JsonPropertyOrder({"networkInfo", "status", "message"})
@AllArgsConstructor
@NoArgsConstructor
public class GetNetworkInfoResp extends BaseRespStatus {
/**
* 网络信息内容
* <p>
* 包含网络的详细信息例如IP地址MAC地址网络接口等
* 类型为 {@link NetworkInfo}
* </p>
*/
private NetworkInfo networkInfo;
}

View File

@ -1,6 +1,9 @@
package com.cmhi.magent.service;
import com.cmhi.magent.pojo.po.FileStoreInfo;
import com.cmhi.magent.pojo.po.HwInfo;
import com.cmhi.magent.pojo.po.MemoryInfo;
import com.cmhi.magent.pojo.po.NetworkInfo;
import com.cmhi.magent.pojo.po.ProcessorInfo;
/**
@ -55,4 +58,32 @@ public interface SystemInfoService {
* @return 硬件信息对象
*/
HwInfo getHardwareInfo();
/**
* 获取网络信息
* <p>
* 封装网络的相关信息如网络名称网络类型网络状态网络速度等结果以 {@link NetworkInfo} 对象形式返回
* </p>
*
* @return 网络信息对象
*/
NetworkInfo getNetworkInfo();
/**
* 获取内存信息
* <p>
* 封装内存的相关信息如物理内存大小可用内存当前内存占用情况等结果以 {@link MemoryInfo} 对象形式返回
* </p>
* @return 返回一个 MemoryInfo 对象包含内存的详细信息
*/
MemoryInfo getMemoryInfo();
/**
* 获取文件存储信息
* <p>
* 封装操作系统文件系统相关信息如磁盘大小分区大小等结果以 {@link FileStoreInfo} 对象形式返回
* </p>
* @return 返回一个 MemoryInfo 对象包含内存的详细信息
*/
FileStoreInfo getFileStoreInfo();
}

View File

@ -1,19 +1,15 @@
package com.cmhi.magent.service.impl;
import com.cmhi.magent.pojo.mapper.IObjectConvert;
import com.cmhi.magent.pojo.po.FileStoreDetails;
import com.cmhi.magent.pojo.po.FileStoreInfo;
import com.cmhi.magent.pojo.po.HwInfo;
import com.cmhi.magent.pojo.po.MemoryInfo;
import com.cmhi.magent.pojo.po.NetworkInfo;
import com.cmhi.magent.pojo.po.ProcessorInfo;
import com.cmhi.magent.service.SystemInfoService;
import org.springframework.stereotype.Service;
import oshi.SystemInfo;
import oshi.hardware.CentralProcessor;
import oshi.hardware.ComputerSystem;
import oshi.hardware.GlobalMemory;
import oshi.software.os.OSFileStore;
import java.util.ArrayList;
/**
* 系统信息服务实现类
@ -98,26 +94,46 @@ public class SystemInfoServiceImpl implements SystemInfoService {
*/
@Override
public HwInfo getHardwareInfo() {
ComputerSystem cs = si.getHardware().getComputerSystem();
GlobalMemory gm = si.getHardware().getMemory();
HwInfo hw = IObjectConvert.INSTANCE.toHwInfo(si);
return IObjectConvert.INSTANCE.toHwInfo(si);
}
// FileStoreInfo fi = new FileStoreInfo();
// fi.setFileStore(new ArrayList<>());
//
// for (OSFileStore os : si.getOperatingSystem().getFileSystem().getFileStores()) {
// fi.getFileStore().add(FileStoreDetails.builder()
// .name(os.getName())
// .volume(os.getVolume())
// .mountPoint(os.getMount())
// .uuid(os.getUUID())
// .fsType(os.getType())
// .description(os.getDescription())
// .freeSpace(os.getFreeSpace())
// .totalSpace(os.getTotalSpace())
// .usableSpace(os.getUsableSpace())
// .build());
// }
return hw;
/**
* 获取网络信息
* <p>
* 通过 OSHI 库获取网络接口的详细信息包括网络名称网络类型网络状态网络速度等
* 并通过 {@link IObjectConvert} 工具类将其转换为自定义的 {@link NetworkInfo} 对象
* </p>
*
* @return 包含网络详细信息的 {@link NetworkInfo} 对象
*/
@Override
public NetworkInfo getNetworkInfo() {
return IObjectConvert.INSTANCE.toNetworkInfo(si, si.getOperatingSystem().getNetworkParams());
}
/**
* 获取内存信息
* <p>
* 封装内存的相关信息如物理内存大小可用内存当前内存占用情况等结果以 {@link MemoryInfo} 对象形式返回
* </p>
*
* @return 返回一个 MemoryInfo 对象包含内存的详细信息
*/
@Override
public MemoryInfo getMemoryInfo() {
return IObjectConvert.INSTANCE.toMemoryInfo(si.getHardware().getMemory());
}
/**
* 获取文件存储信息
* <p>
* 封装操作系统文件系统相关信息如磁盘大小分区大小等结果以 {@link FileStoreInfo} 对象形式返回
* </p>
*
* @return 返回一个 MemoryInfo 对象包含内存的详细信息
*/
@Override
public FileStoreInfo getFileStoreInfo() {
return IObjectConvert.INSTANCE.toFileStoreInfo(si.getOperatingSystem().getFileSystem(), si.getHardware().getDiskStores());
}
}

View File

@ -1,7 +1,10 @@
package com.cmhi.magent.controller;
import com.cmhi.magent.common.TestBaseAuthentication;
import com.cmhi.magent.pojo.vo.GetFileStoreInfoResp;
import com.cmhi.magent.pojo.vo.GetHwInfoResp;
import com.cmhi.magent.pojo.vo.GetMemoryInfoResp;
import com.cmhi.magent.pojo.vo.GetNetworkInfoResp;
import com.cmhi.magent.pojo.vo.GetOsInfoResp;
import com.cmhi.magent.pojo.vo.GetProcessorInfoResp;
import org.junit.jupiter.api.Assertions;
@ -57,4 +60,46 @@ public class SystemInfoApiTest extends TestBaseAuthentication {
// 验证硬件信息数据的合法性
Assertions.assertNotNull(resp.getHwInfo(), "硬件信息不应为空");
}
@Test
@DisplayName("获取内存信息")
void testGetMemoryInfo() throws Exception {
// 发起 GET 请求调用 /api/systemInfo/memoryInfo 接口
GetMemoryInfoResp resp = (GetMemoryInfoResp) performanceRestful(RequestMethod.GET, null, "/api/systemInfo/memoryInfo",
new Class[] {GetMemoryInfoResp.class});
// 验证通用响应数据的合法性
AssertValidCommonResp(resp);
// 验证硬件信息数据的合法性
Assertions.assertNotNull(resp.getMemoryInfo(), "内存信息不应为空");
}
@Test
@DisplayName("获取网络信息")
void testGetNetworkInfo() throws Exception {
// 发起 GET 请求调用 /api/systemInfo/networkInfo 接口
GetNetworkInfoResp resp = (GetNetworkInfoResp) performanceRestful(RequestMethod.GET, null, "/api/systemInfo/networkInfo",
new Class[] {GetNetworkInfoResp.class});
// 验证通用响应数据的合法性
AssertValidCommonResp(resp);
// 验证硬件信息数据的合法性
Assertions.assertNotNull(resp.getNetworkInfo(), "网络信息不应为空");
}
@Test
@DisplayName("获取文件存储信息")
void testGetFileStoreInfo() throws Exception {
// 发起 GET 请求调用 /api/systemInfo/fileStoreInfo 接口
GetFileStoreInfoResp resp = (GetFileStoreInfoResp) performanceRestful(RequestMethod.GET, null, "/api/systemInfo/fileStoreInfo",
new Class[] {GetFileStoreInfoResp.class});
// 验证通用响应数据的合法性
AssertValidCommonResp(resp);
// 验证硬件信息数据的合法性
Assertions.assertNotNull(resp.getFileStoreInfo(), "文件存储信息不应为空");
}
}

View File

@ -0,0 +1,114 @@
package com.cmhi.magent.service;
import com.cmhi.magent.misc.HelperUtils;
import com.cmhi.magent.pojo.mapper.IObjectConvert;
import com.cmhi.magent.pojo.po.HwInfo;
import com.cmhi.magent.pojo.po.MemoryDetails;
import com.cmhi.magent.pojo.po.ProcessorInfo;
import com.cmhi.magent.service.impl.SystemInfoServiceImpl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.annotation.Resource;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import oshi.SystemInfo;
import oshi.hardware.GlobalMemory;
import oshi.hardware.HWDiskStore;
import oshi.hardware.NetworkIF;
import oshi.hardware.PhysicalMemory;
import oshi.software.os.FileSystem;
import oshi.software.os.NetworkParams;
import oshi.software.os.OSFileStore;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
@SpringBootTest
class SystemInfoServiceTest {
private SystemInfoServiceImpl systemInfoService; // 实现类对象
@Resource
private ObjectMapper objectMapper;
@BeforeEach
void setUp() {
// 实例化实现类
systemInfoService = new SystemInfoServiceImpl();
}
@Test
void testGetOsName() {
// 调用接口方法
String osName = systemInfoService.getOsName();
// 检查返回值是否符合预期这里只能验证非空因为操作系统名称依赖具体环境
assertNotNull(osName, "操作系统名称不能为空!");
System.out.println("操作系统名称: " + osName);
}
@Test
void testGetOsBootTimeStamp() {
// 调用接口方法
long bootTimeStamp = systemInfoService.getOsBootTimeStamp();
// 验证时间戳一般会大于当前时间减去一个合理的范围
assertTrue(bootTimeStamp > 0, "启动时间戳应该是正值!");
System.out.println("操作系统启动时间戳: " + bootTimeStamp);
}
@Test
void testGetProcessInfo() {
// 调用接口方法获取处理器信息
ProcessorInfo processorInfo = systemInfoService.getProcessInfo();
// 检查返回的处理器信息是否合理
assertNotNull(processorInfo, "处理器信息不能为空!");
assertTrue(processorInfo.getPhysicalCores() > 0, "处理器核心数量应该大于0");
assertTrue(processorInfo.getLogicalCpu() >= processorInfo.getPhysicalCores(), "线程数量应该不小于核心数量!");
assertTrue(processorInfo.getCpuVendorFreq() > 0, "CPU频率应该大于0");
System.out.println(
"处理器信息: 核心数量 = " + processorInfo.getPhysicalCores() + ", 线程数量 = " + processorInfo.getLogicalCpu() + ", 主频 = " + processorInfo.getCpuVendorFreq() + " GHz");
}
@Test
void testGetHardwareInfo() {
// // 调用接口方法获取硬件信息
// HwInfo hardwareInfo = systemInfoService.getHardwareInfo();
//
// // 检查返回的硬件信息是否合理
// assertNotNull(hardwareInfo, "硬件信息不能为空!");
// assertTrue(hardwareInfo.get() > 0, "内存大小应该大于0");
// assertTrue(hardwareInfo.getDiskCapacity() > 0, "磁盘容量应该大于0");
// System.out.println("硬件信息: 内存大小 = " + hardwareInfo.getMemorySize() +
// " GB, 磁盘容量 = " + hardwareInfo.getDiskCapacity() + " GB");
}
@Test
void testGetMemoryInfo() throws JsonProcessingException {
SystemInfo si = new SystemInfo();
GlobalMemory memory = si.getHardware().getMemory();
List<PhysicalMemory> ph = memory.getPhysicalMemory();
List<HWDiskStore> d = si.getHardware().getDiskStores();
FileSystem fs = si.getOperatingSystem().getFileSystem();
List<OSFileStore> os = fs.getFileStores(true);
List<NetworkIF> nf = si.getHardware().getNetworkIFs(true);
NetworkParams np = si.getOperatingSystem().getNetworkParams();
MemoryDetails md = new MemoryDetails();
md.setFreeMemory(1000L);
md.setTotalMemory(3000L);
HwInfo hw = IObjectConvert.INSTANCE.toHwInfo(si);
System.out.println(HelperUtils.getJson(hw));
// System.out.println(HelperUtils.getJson(d));
// System.out.println(HelperUtils.getJson(fs));
//System.out.println(objectMapper.writeValueAsString(md));
// System.out.println(HelperUtils.getJson(memory));
// System.out.println(HelperUtils.getJson(ph));
}
}