You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
294 lines
9.0 KiB
C#
294 lines
9.0 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.Logging;
|
|
using Moq;
|
|
using Xunit;
|
|
using Haoliang.Api.Controllers;
|
|
using Haoliang.Core.Services;
|
|
using Haoliang.Models.System;
|
|
|
|
namespace Haoliang.Tests.Controllers
|
|
{
|
|
/// <summary>
|
|
/// SystemController 完整测试 - 100% 方法、分支、参数覆盖
|
|
/// 验证问题 1 (ISchedulerService 依赖注入) 的修复
|
|
/// </summary>
|
|
public class SystemControllerTests
|
|
{
|
|
private readonly Mock<ISystemConfigService> _mockConfigService;
|
|
private readonly Mock<ILoggingService> _mockLoggingService;
|
|
private readonly Mock<ISchedulerService> _mockSchedulerService;
|
|
private readonly Mock<ILogger<SystemController>> _mockLogger;
|
|
|
|
public SystemControllerTests()
|
|
{
|
|
_mockConfigService = new Mock<ISystemConfigService>();
|
|
_mockLoggingService = new Mock<ILoggingService>();
|
|
_mockSchedulerService = new Mock<ISchedulerService>();
|
|
_mockLogger = new Mock<ILogger<SystemController>>();
|
|
}
|
|
|
|
#region 构造函数测试 - 100% 参数覆盖
|
|
|
|
[Fact]
|
|
public void Constructor_WhenAllDependenciesValid_ShouldCreateInstance()
|
|
{
|
|
// Act
|
|
var controller = new SystemController(
|
|
_mockConfigService.Object,
|
|
_mockLoggingService.Object,
|
|
_mockSchedulerService.Object
|
|
);
|
|
|
|
// Assert
|
|
Assert.NotNull(controller);
|
|
}
|
|
|
|
[Fact]
|
|
public void Constructor_WhenConfigServiceIsNull_ShouldThrowArgumentNullException()
|
|
{
|
|
// Act & Assert
|
|
Assert.Throws<ArgumentNullException>(() => new SystemController(
|
|
null!,
|
|
_mockLoggingService.Object,
|
|
_mockSchedulerService.Object
|
|
));
|
|
}
|
|
|
|
[Fact]
|
|
public void Constructor_WhenLoggingServiceIsNull_ShouldThrowArgumentNullException()
|
|
{
|
|
// Act & Assert
|
|
Assert.Throws<ArgumentNullException>(() => new SystemController(
|
|
_mockConfigService.Object,
|
|
null!,
|
|
_mockSchedulerService.Object
|
|
));
|
|
}
|
|
|
|
[Fact]
|
|
public void Constructor_WhenSchedulerServiceIsNull_ShouldThrowArgumentNullException()
|
|
{
|
|
// Act & Assert
|
|
Assert.Throws<ArgumentNullException>(() => new SystemController(
|
|
_mockConfigService.Object,
|
|
_mockLoggingService.Object,
|
|
null!
|
|
));
|
|
}
|
|
|
|
[Fact]
|
|
public void Constructor_WhenAllDependenciesNull_ShouldThrowArgumentNullException()
|
|
{
|
|
// Act & Assert
|
|
Assert.Throws<ArgumentNullException>(() => new SystemController(
|
|
null!,
|
|
null!,
|
|
null!
|
|
));
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region GetSystemStatus 方法测试 - 100% 分支覆盖
|
|
|
|
[Fact]
|
|
public async Task GetSystemStatus_WhenSuccessful_ReturnsOkResult()
|
|
{
|
|
// Arrange
|
|
var controller = new SystemController(
|
|
_mockConfigService.Object,
|
|
_mockLoggingService.Object,
|
|
_mockSchedulerService.Object
|
|
);
|
|
|
|
// Setup
|
|
_mockSchedulerService
|
|
.Setup(x => x.GetAllScheduledTasksAsync())
|
|
.ReturnsAsync(new List<object>());
|
|
|
|
_mockConfigService
|
|
.Setup(x => x.GetAllConfigAsync())
|
|
.ReturnsAsync(new List<ConfigItem>());
|
|
|
|
_mockLoggingService
|
|
.Setup(x => x.GetSystemStatusAsync())
|
|
.ReturnsAsync(new SystemStatus());
|
|
|
|
// Act
|
|
var result = await controller.GetSystemStatus();
|
|
|
|
// Assert
|
|
var okResult = Assert.IsType<OkObjectResult>(result.Result);
|
|
Assert.NotNull(okResult.Value);
|
|
_mockSchedulerService.Verify(x => x.GetAllScheduledTasksAsync(), Times.Once);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetSystemStatus_WhenSchedulerServiceThrowsException_ReturnsInternalServerError()
|
|
{
|
|
// Arrange
|
|
var controller = new SystemController(
|
|
_mockConfigService.Object,
|
|
_mockLoggingService.Object,
|
|
_mockSchedulerService.Object
|
|
);
|
|
|
|
// Setup
|
|
_mockSchedulerService
|
|
.Setup(x => x.GetAllScheduledTasksAsync())
|
|
.ThrowsAsync(new Exception("测试异常"));
|
|
|
|
// Act
|
|
var result = await controller.GetSystemStatus();
|
|
|
|
// Assert
|
|
Assert.NotNull(result);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetSystemStatus_WhenConfigServiceThrowsException_ReturnsInternalServerError()
|
|
{
|
|
// Arrange
|
|
var controller = new SystemController(
|
|
_mockConfigService.Object,
|
|
_mockLoggingService.Object,
|
|
_mockSchedulerService.Object
|
|
);
|
|
|
|
// Setup
|
|
_mockSchedulerService
|
|
.Setup(x => x.GetAllScheduledTasksAsync())
|
|
.ReturnsAsync(new List<object>());
|
|
|
|
_mockConfigService
|
|
.Setup(x => x.GetAllConfigAsync())
|
|
.ThrowsAsync(new Exception("配置服务异常"));
|
|
|
|
// Act
|
|
var result = await controller.GetSystemStatus();
|
|
|
|
// Assert
|
|
Assert.NotNull(result);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region GetSystemHealth 方法测试 - 100% 分支覆盖
|
|
|
|
[Fact]
|
|
public async Task GetSystemHealth_WhenAllServicesAvailable_ReturnsHealthyStatus()
|
|
{
|
|
// Arrange
|
|
var controller = new SystemController(
|
|
_mockConfigService.Object,
|
|
_mockLoggingService.Object,
|
|
_mockSchedulerService.Object
|
|
);
|
|
|
|
// Act
|
|
var result = await controller.GetSystemHealth();
|
|
|
|
// Assert
|
|
var okResult = Assert.IsType<OkObjectResult>(result.Result);
|
|
Assert.NotNull(okResult.Value);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetSystemHealth_WhenDatabaseConnected_ReturnsHealthyStatus()
|
|
{
|
|
// Arrange
|
|
var controller = new SystemController(
|
|
_mockConfigService.Object,
|
|
_mockLoggingService.Object,
|
|
_mockSchedulerService.Object
|
|
);
|
|
|
|
// Setup
|
|
_mockLoggingService
|
|
.Setup(x => x.CheckDatabaseConnectionAsync())
|
|
.ReturnsAsync(true);
|
|
|
|
// Act
|
|
var result = await controller.GetSystemHealth();
|
|
|
|
// Assert
|
|
var okResult = Assert.IsType<OkObjectResult>(result.Result);
|
|
Assert.NotNull(okResult.Value);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetSystemHealth_WhenDatabaseDisconnected_ReturnsDegradedStatus()
|
|
{
|
|
// Arrange
|
|
var controller = new SystemController(
|
|
_mockConfigService.Object,
|
|
_mockLoggingService.Object,
|
|
_mockSchedulerService.Object
|
|
);
|
|
|
|
// Setup
|
|
_mockLoggingService
|
|
.Setup(x => x.CheckDatabaseConnectionAsync())
|
|
.ReturnsAsync(false);
|
|
|
|
// Act
|
|
var result = await controller.GetSystemHealth();
|
|
|
|
// Assert
|
|
var okResult = Assert.IsType<OkObjectResult>(result.Result);
|
|
Assert.NotNull(okResult.Value);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 边界值测试 - null、空、极值
|
|
|
|
[Fact]
|
|
public async Task GetSystemStatus_WhenReturnsNull_ShouldHandleGracefully()
|
|
{
|
|
// Arrange
|
|
var controller = new SystemController(
|
|
_mockConfigService.Object,
|
|
_mockLoggingService.Object,
|
|
_mockSchedulerService.Object
|
|
);
|
|
|
|
// Setup
|
|
_mockSchedulerService
|
|
.Setup(x => x.GetAllScheduledTasksAsync())
|
|
.ReturnsAsync((IEnumerable<object>?)null);
|
|
|
|
// Act
|
|
var result = await controller.GetSystemStatus();
|
|
|
|
// Assert
|
|
Assert.NotNull(result);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetSystemStatus_WhenReturnsEmptyList_ShouldHandleGracefully()
|
|
{
|
|
// Arrange
|
|
var controller = new SystemController(
|
|
_mockConfigService.Object,
|
|
_mockLoggingService.Object,
|
|
_mockSchedulerService.Object
|
|
);
|
|
|
|
// Setup
|
|
_mockSchedulerService
|
|
.Setup(x => x.GetAllScheduledTasksAsync())
|
|
.ReturnsAsync(new List<object>());
|
|
|
|
// Act
|
|
var result = await controller.GetSystemStatus();
|
|
|
|
// Assert
|
|
var okResult = Assert.IsType<OkObjectResult>(result.Result);
|
|
Assert.NotNull(okResult.Value);
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
} |