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.
508 lines
17 KiB
C#
508 lines
17 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Moq;
|
|
using FluentAssertions;
|
|
using Xunit;
|
|
using Haoliang.Api.Controllers;
|
|
using Haoliang.Core.Services;
|
|
using Haoliang.Models.Common;
|
|
|
|
namespace Haoliang.Tests.Controllers
|
|
{
|
|
public class SystemControllerTests
|
|
{
|
|
private readonly Mock<ISystemConfigService> _configService;
|
|
private readonly Mock<ILoggingService> _loggingService;
|
|
private readonly Mock<ISchedulerService> _schedulerService;
|
|
private readonly SystemController _controller;
|
|
|
|
public SystemControllerTests()
|
|
{
|
|
_configService = new Mock<ISystemConfigService>();
|
|
_loggingService = new Mock<ILoggingService>();
|
|
_schedulerService = new Mock<ISchedulerService>();
|
|
_controller = new SystemController(_configService.Object, _loggingService.Object, _schedulerService.Object);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetAllConfigs_WhenSuccess_ReturnsOk()
|
|
{
|
|
_configService.Setup(s => s.GetAllConfigsAsync())
|
|
.ReturnsAsync(new List<Haoliang.Core.Services.SystemConfig>());
|
|
|
|
var result = await _controller.GetAllConfigs();
|
|
|
|
result.Should().BeOfType<OkObjectResult>();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetAllConfigs_WhenException_Returns500()
|
|
{
|
|
_configService.Setup(s => s.GetAllConfigsAsync())
|
|
.ThrowsAsync(new Exception("test"));
|
|
|
|
var result = await _controller.GetAllConfigs();
|
|
|
|
var objResult = result.Should().BeOfType<ObjectResult>().Subject;
|
|
objResult.StatusCode.Should().Be(500);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetConfig_WhenFound_ReturnsOk()
|
|
{
|
|
_configService.Setup(s => s.GetConfigAsync("key1"))
|
|
.ReturnsAsync(new Haoliang.Core.Services.SystemConfig { Key = "key1" });
|
|
|
|
var result = await _controller.GetConfig("key1");
|
|
|
|
result.Should().BeOfType<OkObjectResult>();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetConfig_WhenNotFound_ReturnsNotFound()
|
|
{
|
|
_configService.Setup(s => s.GetConfigAsync("missing"))
|
|
.ReturnsAsync((Haoliang.Core.Services.SystemConfig?)null);
|
|
|
|
var result = await _controller.GetConfig("missing");
|
|
|
|
result.Should().BeOfType<NotFoundObjectResult>();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetConfig_WhenException_Returns500()
|
|
{
|
|
_configService.Setup(s => s.GetConfigAsync("key"))
|
|
.ThrowsAsync(new Exception("test"));
|
|
|
|
var result = await _controller.GetConfig("key");
|
|
|
|
var objResult = result.Should().BeOfType<ObjectResult>().Subject;
|
|
objResult.StatusCode.Should().Be(500);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task SetConfig_WhenSuccess_ReturnsOk()
|
|
{
|
|
_configService.Setup(s => s.SetConfigAsync("key1", "value1"))
|
|
.ReturnsAsync(new Haoliang.Core.Services.SystemConfig { Key = "key1", Value = "value1" });
|
|
|
|
var result = await _controller.SetConfig("key1", new SetConfigRequest { Value = "value1" });
|
|
|
|
result.Should().BeOfType<OkObjectResult>();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task SetConfig_WhenException_Returns500()
|
|
{
|
|
_configService.Setup(s => s.SetConfigAsync(It.IsAny<string>(), It.IsAny<string>()))
|
|
.ThrowsAsync(new Exception("test"));
|
|
|
|
var result = await _controller.SetConfig("key", new SetConfigRequest { Value = "val" });
|
|
|
|
var objResult = result.Should().BeOfType<ObjectResult>().Subject;
|
|
objResult.StatusCode.Should().Be(500);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task DeleteConfig_WhenFound_ReturnsOk()
|
|
{
|
|
_configService.Setup(s => s.DeleteConfigAsync("key1"))
|
|
.ReturnsAsync(true);
|
|
|
|
var result = await _controller.DeleteConfig("key1");
|
|
|
|
result.Should().BeOfType<OkObjectResult>();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task DeleteConfig_WhenNotFound_ReturnsNotFound()
|
|
{
|
|
_configService.Setup(s => s.DeleteConfigAsync("missing"))
|
|
.ReturnsAsync(false);
|
|
|
|
var result = await _controller.DeleteConfig("missing");
|
|
|
|
result.Should().BeOfType<NotFoundObjectResult>();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task DeleteConfig_WhenException_Returns500()
|
|
{
|
|
_configService.Setup(s => s.DeleteConfigAsync(It.IsAny<string>()))
|
|
.ThrowsAsync(new Exception("test"));
|
|
|
|
var result = await _controller.DeleteConfig("key");
|
|
|
|
var objResult = result.Should().BeOfType<ObjectResult>().Subject;
|
|
objResult.StatusCode.Should().Be(500);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ConfigExists_WhenSuccess_ReturnsOk()
|
|
{
|
|
_configService.Setup(s => s.ConfigExistsAsync("key1"))
|
|
.ReturnsAsync(true);
|
|
|
|
var result = await _controller.ConfigExists("key1");
|
|
|
|
result.Should().BeOfType<OkObjectResult>();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ConfigExists_WhenException_Returns500()
|
|
{
|
|
_configService.Setup(s => s.ConfigExistsAsync(It.IsAny<string>()))
|
|
.ThrowsAsync(new Exception("test"));
|
|
|
|
var result = await _controller.ConfigExists("key");
|
|
|
|
var objResult = result.Should().BeOfType<ObjectResult>().Subject;
|
|
objResult.StatusCode.Should().Be(500);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetConfigsByCategory_WhenSuccess_ReturnsOk()
|
|
{
|
|
_configService.Setup(s => s.GetConfigsByCategoryAsync("cat1"))
|
|
.ReturnsAsync(new List<Haoliang.Core.Services.SystemConfig>());
|
|
|
|
var result = await _controller.GetConfigsByCategory("cat1");
|
|
|
|
result.Should().BeOfType<OkObjectResult>();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetConfigsByCategory_WhenException_Returns500()
|
|
{
|
|
_configService.Setup(s => s.GetConfigsByCategoryAsync(It.IsAny<string>()))
|
|
.ThrowsAsync(new Exception("test"));
|
|
|
|
var result = await _controller.GetConfigsByCategory("cat");
|
|
|
|
var objResult = result.Should().BeOfType<ObjectResult>().Subject;
|
|
objResult.StatusCode.Should().Be(500);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task RefreshConfigCache_WhenSuccess_ReturnsOk()
|
|
{
|
|
var result = await _controller.RefreshConfigCache();
|
|
|
|
result.Should().BeOfType<OkObjectResult>();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task RefreshConfigCache_WhenException_Returns500()
|
|
{
|
|
_configService.Setup(s => s.RefreshConfigCacheAsync())
|
|
.ThrowsAsync(new Exception("test"));
|
|
|
|
var result = await _controller.RefreshConfigCache();
|
|
|
|
var objResult = result.Should().BeOfType<ObjectResult>().Subject;
|
|
objResult.StatusCode.Should().Be(500);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetLogs_WhenSuccess_ReturnsOk()
|
|
{
|
|
_loggingService.Setup(s => s.GetLogsAsync(null, null, null, null))
|
|
.ReturnsAsync(new List<Haoliang.Models.System.LogEntry>());
|
|
|
|
var result = await _controller.GetLogs();
|
|
|
|
result.Should().BeOfType<OkObjectResult>();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetLogs_WhenException_Returns500()
|
|
{
|
|
_loggingService.Setup(s => s.GetLogsAsync(It.IsAny<Haoliang.Models.System.LogLevel?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<string>()))
|
|
.ThrowsAsync(new Exception("test"));
|
|
|
|
var result = await _controller.GetLogs();
|
|
|
|
var objResult = result.Should().BeOfType<ObjectResult>().Subject;
|
|
objResult.StatusCode.Should().Be(500);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetErrorLogs_WhenSuccess_ReturnsOk()
|
|
{
|
|
_loggingService.Setup(s => s.GetErrorLogsAsync(null, null))
|
|
.ReturnsAsync(new List<Haoliang.Models.System.LogEntry>());
|
|
|
|
var result = await _controller.GetErrorLogs();
|
|
|
|
result.Should().BeOfType<OkObjectResult>();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetErrorLogs_WhenException_Returns500()
|
|
{
|
|
_loggingService.Setup(s => s.GetErrorLogsAsync(It.IsAny<DateTime?>(), It.IsAny<DateTime?>()))
|
|
.ThrowsAsync(new Exception("test"));
|
|
|
|
var result = await _controller.GetErrorLogs();
|
|
|
|
var objResult = result.Should().BeOfType<ObjectResult>().Subject;
|
|
objResult.StatusCode.Should().Be(500);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetLogCount_WhenSuccess_ReturnsOk()
|
|
{
|
|
_loggingService.Setup(s => s.GetLogCountAsync(null, null, null))
|
|
.ReturnsAsync(5);
|
|
|
|
var result = await _controller.GetLogCount();
|
|
|
|
result.Should().BeOfType<OkObjectResult>();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetLogCount_WhenException_Returns500()
|
|
{
|
|
_loggingService.Setup(s => s.GetLogCountAsync(It.IsAny<Haoliang.Models.System.LogLevel?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()))
|
|
.ThrowsAsync(new Exception("test"));
|
|
|
|
var result = await _controller.GetLogCount();
|
|
|
|
var objResult = result.Should().BeOfType<ObjectResult>().Subject;
|
|
objResult.StatusCode.Should().Be(500);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ArchiveLogs_WhenSuccess_ReturnsOk()
|
|
{
|
|
var result = await _controller.ArchiveLogs(30);
|
|
|
|
result.Should().BeOfType<OkObjectResult>();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ArchiveLogs_WhenException_Returns500()
|
|
{
|
|
_loggingService.Setup(s => s.ArchiveLogsAsync(It.IsAny<int>()))
|
|
.ThrowsAsync(new Exception("test"));
|
|
|
|
var result = await _controller.ArchiveLogs(30);
|
|
|
|
var objResult = result.Should().BeOfType<ObjectResult>().Subject;
|
|
objResult.StatusCode.Should().Be(500);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ClearLogs_WhenSuccess_ReturnsOk()
|
|
{
|
|
var result = await _controller.ClearLogs();
|
|
|
|
result.Should().BeOfType<OkObjectResult>();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ClearLogs_WhenException_Returns500()
|
|
{
|
|
_loggingService.Setup(s => s.ClearLogsAsync())
|
|
.ThrowsAsync(new Exception("test"));
|
|
|
|
var result = await _controller.ClearLogs();
|
|
|
|
var objResult = result.Should().BeOfType<ObjectResult>().Subject;
|
|
objResult.StatusCode.Should().Be(500);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetScheduledTasks_WhenSuccess_ReturnsOk()
|
|
{
|
|
_schedulerService.Setup(s => s.GetAllScheduledTasksAsync())
|
|
.ReturnsAsync(new List<ScheduledTask>());
|
|
|
|
var result = await _controller.GetScheduledTasks();
|
|
|
|
result.Should().BeOfType<OkObjectResult>();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetScheduledTasks_WhenException_Returns500()
|
|
{
|
|
_schedulerService.Setup(s => s.GetAllScheduledTasksAsync())
|
|
.ThrowsAsync(new Exception("test"));
|
|
|
|
var result = await _controller.GetScheduledTasks();
|
|
|
|
var objResult = result.Should().BeOfType<ObjectResult>().Subject;
|
|
objResult.StatusCode.Should().Be(500);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetScheduledTask_WhenFound_ReturnsOk()
|
|
{
|
|
_schedulerService.Setup(s => s.GetTaskByIdAsync("task1"))
|
|
.ReturnsAsync(new ScheduledTask { TaskId = "task1" });
|
|
|
|
var result = await _controller.GetScheduledTask("task1");
|
|
|
|
result.Should().BeOfType<OkObjectResult>();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetScheduledTask_WhenNotFound_ReturnsNotFound()
|
|
{
|
|
_schedulerService.Setup(s => s.GetTaskByIdAsync("missing"))
|
|
.ReturnsAsync((ScheduledTask?)null);
|
|
|
|
var result = await _controller.GetScheduledTask("missing");
|
|
|
|
result.Should().BeOfType<NotFoundObjectResult>();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetScheduledTask_WhenException_Returns500()
|
|
{
|
|
_schedulerService.Setup(s => s.GetTaskByIdAsync(It.IsAny<string>()))
|
|
.ThrowsAsync(new Exception("test"));
|
|
|
|
var result = await _controller.GetScheduledTask("task1");
|
|
|
|
var objResult = result.Should().BeOfType<ObjectResult>().Subject;
|
|
objResult.StatusCode.Should().Be(500);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task CreateScheduledTask_WhenSuccess_ReturnsOk()
|
|
{
|
|
var task = new ScheduledTask { TaskId = "task1", Name = "Test" };
|
|
|
|
var result = await _controller.CreateScheduledTask(task);
|
|
|
|
result.Should().BeOfType<OkObjectResult>();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task CreateScheduledTask_WhenException_Returns500()
|
|
{
|
|
_schedulerService.Setup(s => s.ScheduleTaskAsync(It.IsAny<ScheduledTask>()))
|
|
.ThrowsAsync(new Exception("test"));
|
|
|
|
var result = await _controller.CreateScheduledTask(new ScheduledTask());
|
|
|
|
var objResult = result.Should().BeOfType<ObjectResult>().Subject;
|
|
objResult.StatusCode.Should().Be(500);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ExecuteTask_WhenSuccess_ReturnsOk()
|
|
{
|
|
var result = await _controller.ExecuteTask("task1");
|
|
|
|
result.Should().BeOfType<OkObjectResult>();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ExecuteTask_WhenException_Returns500()
|
|
{
|
|
_schedulerService.Setup(s => s.ExecuteTaskAsync(It.IsAny<string>()))
|
|
.ThrowsAsync(new Exception("test"));
|
|
|
|
var result = await _controller.ExecuteTask("task1");
|
|
|
|
var objResult = result.Should().BeOfType<ObjectResult>().Subject;
|
|
objResult.StatusCode.Should().Be(500);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task RemoveTask_WhenFound_ReturnsOk()
|
|
{
|
|
_schedulerService.Setup(s => s.RemoveTaskAsync("task1"))
|
|
.ReturnsAsync(true);
|
|
|
|
var result = await _controller.RemoveTask("task1");
|
|
|
|
result.Should().BeOfType<OkObjectResult>();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task RemoveTask_WhenNotFound_ReturnsNotFound()
|
|
{
|
|
_schedulerService.Setup(s => s.RemoveTaskAsync("missing"))
|
|
.ReturnsAsync(false);
|
|
|
|
var result = await _controller.RemoveTask("missing");
|
|
|
|
result.Should().BeOfType<NotFoundObjectResult>();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task RemoveTask_WhenException_Returns500()
|
|
{
|
|
_schedulerService.Setup(s => s.RemoveTaskAsync(It.IsAny<string>()))
|
|
.ThrowsAsync(new Exception("test"));
|
|
|
|
var result = await _controller.RemoveTask("task1");
|
|
|
|
var objResult = result.Should().BeOfType<ObjectResult>().Subject;
|
|
objResult.StatusCode.Should().Be(500);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task StartScheduler_WhenSuccess_ReturnsOk()
|
|
{
|
|
var result = await _controller.StartScheduler();
|
|
|
|
result.Should().BeOfType<OkObjectResult>();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task StartScheduler_WhenException_Returns500()
|
|
{
|
|
_schedulerService.Setup(s => s.StartSchedulerAsync())
|
|
.ThrowsAsync(new Exception("test"));
|
|
|
|
var result = await _controller.StartScheduler();
|
|
|
|
var objResult = result.Should().BeOfType<ObjectResult>().Subject;
|
|
objResult.StatusCode.Should().Be(500);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task StopScheduler_WhenSuccess_ReturnsOk()
|
|
{
|
|
var result = await _controller.StopScheduler();
|
|
|
|
result.Should().BeOfType<OkObjectResult>();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task StopScheduler_WhenException_Returns500()
|
|
{
|
|
_schedulerService.Setup(s => s.StopSchedulerAsync())
|
|
.ThrowsAsync(new Exception("test"));
|
|
|
|
var result = await _controller.StopScheduler();
|
|
|
|
var objResult = result.Should().BeOfType<ObjectResult>().Subject;
|
|
objResult.StatusCode.Should().Be(500);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetSystemStatus_WhenSuccess_ReturnsOk()
|
|
{
|
|
var result = await _controller.GetSystemStatus();
|
|
|
|
result.Should().BeOfType<OkObjectResult>();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetSystemHealth_WhenSuccess_ReturnsOk()
|
|
{
|
|
var result = await _controller.GetSystemHealth();
|
|
|
|
result.Should().BeOfType<OkObjectResult>();
|
|
}
|
|
}
|
|
}
|