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.

417 lines
21 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;
using Haoliang.Models.System;
using Haoliang.Models.Models.System;
namespace Haoliang.Tests.Controllers
{
public class ConfigControllerTests
{
private readonly Mock<ISystemConfigService> _systemService;
private readonly Mock<ITemplateService> _templateService;
private readonly Mock<IRulesService> _rulesService;
private readonly Mock<IProductionStatisticsService> _statisticsService;
private readonly ConfigController _controller;
public ConfigControllerTests()
{
_systemService = new Mock<ISystemConfigService>();
_templateService = new Mock<ITemplateService>();
_rulesService = new Mock<IRulesService>();
_statisticsService = new Mock<IProductionStatisticsService>();
_controller = new ConfigController(
_systemService.Object, _templateService.Object,
_rulesService.Object, _statisticsService.Object);
}
[Fact]
public async Task GetSystemConfiguration_WhenSuccess_ReturnsOk()
{
_systemService.Setup(s => s.GetSystemConfigurationAsync()).ReturnsAsync((object?)null);
var result = await _controller.GetSystemConfiguration();
result.Result.Should().BeOfType<OkObjectResult>();
}
[Fact]
public async Task GetSystemConfiguration_WhenException_Returns500()
{
_systemService.Setup(s => s.GetSystemConfigurationAsync()).ThrowsAsync(new Exception("t"));
var result = await _controller.GetSystemConfiguration();
result.Result.Should().BeOfType<ObjectResult>().Which.StatusCode.Should().Be(500);
}
[Fact]
public async Task UpdateSystemConfiguration_WhenSuccess_ReturnsOk()
{
_systemService.Setup(s => s.UpdateSystemConfigurationAsync(It.IsAny<object>())).ReturnsAsync(true);
var result = await _controller.UpdateSystemConfiguration(new SystemConfiguration());
result.Result.Should().BeOfType<OkObjectResult>();
}
[Fact]
public async Task UpdateSystemConfiguration_WhenException_Returns500()
{
_systemService.Setup(s => s.UpdateSystemConfigurationAsync(It.IsAny<object>())).ThrowsAsync(new Exception("t"));
var result = await _controller.UpdateSystemConfiguration(new SystemConfiguration());
result.Result.Should().BeOfType<ObjectResult>().Which.StatusCode.Should().Be(500);
}
[Fact]
public async Task GetProductionTargets_WhenSuccess_ReturnsOk()
{
_systemService.Setup(s => s.GetProductionTargetsAsync()).ReturnsAsync(new List<object>());
var result = await _controller.GetProductionTargets();
result.Result.Should().BeOfType<OkObjectResult>();
}
[Fact]
public async Task GetProductionTargets_WhenException_Returns500()
{
_systemService.Setup(s => s.GetProductionTargetsAsync()).ThrowsAsync(new Exception("t"));
var result = await _controller.GetProductionTargets();
result.Result.Should().BeOfType<ObjectResult>().Which.StatusCode.Should().Be(500);
}
[Fact]
public async Task UpdateProductionTargets_WhenSuccess_ReturnsOk()
{
_systemService.Setup(s => s.UpdateProductionTargetsAsync(It.IsAny<IEnumerable<object>>())).ReturnsAsync(true);
var result = await _controller.UpdateProductionTargets(new List<ProductionTargetConfig>());
result.Result.Should().BeOfType<OkObjectResult>();
}
[Fact]
public async Task UpdateProductionTargets_WhenException_Returns500()
{
_systemService.Setup(s => s.UpdateProductionTargetsAsync(It.IsAny<IEnumerable<object>>())).ThrowsAsync(new Exception("t"));
var result = await _controller.UpdateProductionTargets(new List<ProductionTargetConfig>());
result.Result.Should().BeOfType<ObjectResult>().Which.StatusCode.Should().Be(500);
}
[Fact]
public async Task GetWorkingHoursConfig_WhenSuccess_ReturnsOk()
{
_systemService.Setup(s => s.GetWorkingHoursConfigAsync()).ReturnsAsync((object?)null);
var result = await _controller.GetWorkingHoursConfig();
result.Result.Should().BeOfType<OkObjectResult>();
}
[Fact]
public async Task GetWorkingHoursConfig_WhenException_Returns500()
{
_systemService.Setup(s => s.GetWorkingHoursConfigAsync()).ThrowsAsync(new Exception("t"));
var result = await _controller.GetWorkingHoursConfig();
result.Result.Should().BeOfType<ObjectResult>().Which.StatusCode.Should().Be(500);
}
[Fact]
public async Task UpdateWorkingHoursConfig_WhenSuccess_ReturnsOk()
{
_systemService.Setup(s => s.UpdateWorkingHoursConfigAsync(It.IsAny<object>())).ReturnsAsync(true);
var result = await _controller.UpdateWorkingHoursConfig(new WorkingHoursConfig());
result.Result.Should().BeOfType<OkObjectResult>();
}
[Fact]
public async Task UpdateWorkingHoursConfig_WhenException_Returns500()
{
_systemService.Setup(s => s.UpdateWorkingHoursConfigAsync(It.IsAny<object>())).ThrowsAsync(new Exception("t"));
var result = await _controller.UpdateWorkingHoursConfig(new WorkingHoursConfig());
result.Result.Should().BeOfType<ObjectResult>().Which.StatusCode.Should().Be(500);
}
[Fact]
public async Task GetAlertConfiguration_WhenSuccess_ReturnsOk()
{
_systemService.Setup(s => s.GetAlertConfigurationAsync()).ReturnsAsync((object?)null);
var result = await _controller.GetAlertConfiguration();
result.Result.Should().BeOfType<OkObjectResult>();
}
[Fact]
public async Task GetAlertConfiguration_WhenException_Returns500()
{
_systemService.Setup(s => s.GetAlertConfigurationAsync()).ThrowsAsync(new Exception("t"));
var result = await _controller.GetAlertConfiguration();
result.Result.Should().BeOfType<ObjectResult>().Which.StatusCode.Should().Be(500);
}
[Fact]
public async Task UpdateAlertConfiguration_WhenSuccess_ReturnsOk()
{
_systemService.Setup(s => s.UpdateAlertConfigurationAsync(It.IsAny<object>())).ReturnsAsync(true);
var result = await _controller.UpdateAlertConfiguration(new AlertConfiguration());
result.Result.Should().BeOfType<OkObjectResult>();
}
[Fact]
public async Task UpdateAlertConfiguration_WhenException_Returns500()
{
_systemService.Setup(s => s.UpdateAlertConfigurationAsync(It.IsAny<object>())).ThrowsAsync(new Exception("t"));
var result = await _controller.UpdateAlertConfiguration(new AlertConfiguration());
result.Result.Should().BeOfType<ObjectResult>().Which.StatusCode.Should().Be(500);
}
[Fact]
public async Task GetBusinessRules_WhenSuccess_ReturnsOk()
{
_rulesService.Setup(s => s.GetAllRulesAsync()).ReturnsAsync(new List<BusinessRule>());
var result = await _controller.GetBusinessRules();
result.Result.Should().BeOfType<OkObjectResult>();
}
[Fact]
public async Task GetBusinessRules_WhenException_Returns500()
{
_rulesService.Setup(s => s.GetAllRulesAsync()).ThrowsAsync(new Exception("t"));
var result = await _controller.GetBusinessRules();
result.Result.Should().BeOfType<ObjectResult>().Which.StatusCode.Should().Be(500);
}
[Fact]
public async Task CreateOrUpdateBusinessRule_WhenSuccess_ReturnsOk()
{
_rulesService.Setup(s => s.CreateOrUpdateRuleAsync(It.IsAny<BusinessRule>())).ReturnsAsync(new BusinessRule());
var result = await _controller.CreateOrUpdateBusinessRule(new Haoliang.Models.System.BusinessRuleConfig());
result.Result.Should().BeOfType<OkObjectResult>();
}
[Fact]
public async Task CreateOrUpdateBusinessRule_WhenException_Returns500()
{
_rulesService.Setup(s => s.CreateOrUpdateRuleAsync(It.IsAny<BusinessRule>())).ThrowsAsync(new Exception("t"));
var result = await _controller.CreateOrUpdateBusinessRule(new Haoliang.Models.System.BusinessRuleConfig());
result.Result.Should().BeOfType<ObjectResult>().Which.StatusCode.Should().Be(500);
}
[Fact]
public async Task DeleteBusinessRule_WhenSuccess_ReturnsOk()
{
_rulesService.Setup(s => s.DeleteRuleAsync(1)).ReturnsAsync(true);
var result = await _controller.DeleteBusinessRule(1);
result.Result.Should().BeOfType<OkObjectResult>();
}
[Fact]
public async Task DeleteBusinessRule_WhenException_Returns500()
{
_rulesService.Setup(s => s.DeleteRuleAsync(It.IsAny<int>())).ThrowsAsync(new Exception("t"));
var result = await _controller.DeleteBusinessRule(1);
result.Result.Should().BeOfType<ObjectResult>().Which.StatusCode.Should().Be(500);
}
[Fact]
public async Task GetStatisticsRules_WhenSuccess_ReturnsOk()
{
_rulesService.Setup(s => s.GetStatisticsRulesAsync()).ReturnsAsync(new List<BusinessRule>());
var result = await _controller.GetStatisticsRules();
result.Result.Should().BeOfType<OkObjectResult>();
}
[Fact]
public async Task GetStatisticsRules_WhenException_Returns500()
{
_rulesService.Setup(s => s.GetStatisticsRulesAsync()).ThrowsAsync(new Exception("t"));
var result = await _controller.GetStatisticsRules();
result.Result.Should().BeOfType<ObjectResult>().Which.StatusCode.Should().Be(500);
}
[Fact]
public async Task UpdateStatisticsRules_WhenSuccess_ReturnsOk()
{
_rulesService.Setup(s => s.UpdateStatisticsRulesAsync(It.IsAny<IEnumerable<BusinessRule>>())).ReturnsAsync(true);
var result = await _controller.UpdateStatisticsRules(new List<Haoliang.Models.System.StatisticsRuleConfig>());
result.Result.Should().BeOfType<OkObjectResult>();
}
[Fact]
public async Task UpdateStatisticsRules_WhenException_Returns500()
{
_rulesService.Setup(s => s.UpdateStatisticsRulesAsync(It.IsAny<IEnumerable<BusinessRule>>())).ThrowsAsync(new Exception("t"));
var result = await _controller.UpdateStatisticsRules(new List<Haoliang.Models.System.StatisticsRuleConfig>());
result.Result.Should().BeOfType<ObjectResult>().Which.StatusCode.Should().Be(500);
}
[Fact]
public async Task GetDataRetentionConfig_WhenSuccess_ReturnsOk()
{
_systemService.Setup(s => s.GetDataRetentionConfigAsync()).ReturnsAsync((object?)null);
var result = await _controller.GetDataRetentionConfig();
result.Result.Should().BeOfType<OkObjectResult>();
}
[Fact]
public async Task GetDataRetentionConfig_WhenException_Returns500()
{
_systemService.Setup(s => s.GetDataRetentionConfigAsync()).ThrowsAsync(new Exception("t"));
var result = await _controller.GetDataRetentionConfig();
result.Result.Should().BeOfType<ObjectResult>().Which.StatusCode.Should().Be(500);
}
[Fact]
public async Task UpdateDataRetentionConfig_WhenSuccess_ReturnsOk()
{
_systemService.Setup(s => s.UpdateDataRetentionConfigAsync(It.IsAny<object>())).ReturnsAsync(true);
var result = await _controller.UpdateDataRetentionConfig(new DataRetentionConfig());
result.Result.Should().BeOfType<OkObjectResult>();
}
[Fact]
public async Task UpdateDataRetentionConfig_WhenException_Returns500()
{
_systemService.Setup(s => s.UpdateDataRetentionConfigAsync(It.IsAny<object>())).ThrowsAsync(new Exception("t"));
var result = await _controller.UpdateDataRetentionConfig(new DataRetentionConfig());
result.Result.Should().BeOfType<ObjectResult>().Which.StatusCode.Should().Be(500);
}
[Fact]
public async Task GetDashboardConfig_WhenSuccess_ReturnsOk()
{
_systemService.Setup(s => s.GetDashboardConfigAsync()).ReturnsAsync((object?)null);
var result = await _controller.GetDashboardConfig();
result.Result.Should().BeOfType<OkObjectResult>();
}
[Fact]
public async Task GetDashboardConfig_WhenException_Returns500()
{
_systemService.Setup(s => s.GetDashboardConfigAsync()).ThrowsAsync(new Exception("t"));
var result = await _controller.GetDashboardConfig();
result.Result.Should().BeOfType<ObjectResult>().Which.StatusCode.Should().Be(500);
}
[Fact]
public async Task UpdateDashboardConfig_WhenSuccess_ReturnsOk()
{
_systemService.Setup(s => s.UpdateDashboardConfigAsync(It.IsAny<object>())).ReturnsAsync(true);
var result = await _controller.UpdateDashboardConfig(new DashboardConfig());
result.Result.Should().BeOfType<OkObjectResult>();
}
[Fact]
public async Task UpdateDashboardConfig_WhenException_Returns500()
{
_systemService.Setup(s => s.UpdateDashboardConfigAsync(It.IsAny<object>())).ThrowsAsync(new Exception("t"));
var result = await _controller.UpdateDashboardConfig(new DashboardConfig());
result.Result.Should().BeOfType<ObjectResult>().Which.StatusCode.Should().Be(500);
}
[Fact]
public async Task GetExportConfig_WhenSuccess_ReturnsOk()
{
_systemService.Setup(s => s.GetExportConfigAsync()).ReturnsAsync((object?)null);
var result = await _controller.GetExportConfig();
result.Result.Should().BeOfType<OkObjectResult>();
}
[Fact]
public async Task GetExportConfig_WhenException_Returns500()
{
_systemService.Setup(s => s.GetExportConfigAsync()).ThrowsAsync(new Exception("t"));
var result = await _controller.GetExportConfig();
result.Result.Should().BeOfType<ObjectResult>().Which.StatusCode.Should().Be(500);
}
[Fact]
public async Task UpdateExportConfig_WhenSuccess_ReturnsOk()
{
_systemService.Setup(s => s.UpdateExportConfigAsync(It.IsAny<object>())).ReturnsAsync(true);
var result = await _controller.UpdateExportConfig(new ExportConfig());
result.Result.Should().BeOfType<OkObjectResult>();
}
[Fact]
public async Task UpdateExportConfig_WhenException_Returns500()
{
_systemService.Setup(s => s.UpdateExportConfigAsync(It.IsAny<object>())).ThrowsAsync(new Exception("t"));
var result = await _controller.UpdateExportConfig(new ExportConfig());
result.Result.Should().BeOfType<ObjectResult>().Which.StatusCode.Should().Be(500);
}
[Fact]
public async Task GetCollectionConfig_WhenSuccess_ReturnsOk()
{
_systemService.Setup(s => s.GetCollectionConfigAsync()).ReturnsAsync((object?)null);
var result = await _controller.GetCollectionConfig();
result.Result.Should().BeOfType<OkObjectResult>();
}
[Fact]
public async Task GetCollectionConfig_WhenException_Returns500()
{
_systemService.Setup(s => s.GetCollectionConfigAsync()).ThrowsAsync(new Exception("t"));
var result = await _controller.GetCollectionConfig();
result.Result.Should().BeOfType<ObjectResult>().Which.StatusCode.Should().Be(500);
}
[Fact]
public async Task UpdateCollectionConfig_WhenSuccess_ReturnsOk()
{
_systemService.Setup(s => s.UpdateCollectionConfigAsync(It.IsAny<object>())).ReturnsAsync(true);
var result = await _controller.UpdateCollectionConfig(new CollectionConfig());
result.Result.Should().BeOfType<OkObjectResult>();
}
[Fact]
public async Task UpdateCollectionConfig_WhenException_Returns500()
{
_systemService.Setup(s => s.UpdateCollectionConfigAsync(It.IsAny<object>())).ThrowsAsync(new Exception("t"));
var result = await _controller.UpdateCollectionConfig(new CollectionConfig());
result.Result.Should().BeOfType<ObjectResult>().Which.StatusCode.Should().Be(500);
}
[Fact]
public async Task GetNotificationConfig_WhenSuccess_ReturnsOk()
{
_systemService.Setup(s => s.GetNotificationConfigAsync()).ReturnsAsync((object?)null);
var result = await _controller.GetNotificationConfig();
result.Result.Should().BeOfType<OkObjectResult>();
}
[Fact]
public async Task GetNotificationConfig_WhenException_Returns500()
{
_systemService.Setup(s => s.GetNotificationConfigAsync()).ThrowsAsync(new Exception("t"));
var result = await _controller.GetNotificationConfig();
result.Result.Should().BeOfType<ObjectResult>().Which.StatusCode.Should().Be(500);
}
[Fact]
public async Task UpdateNotificationConfig_WhenSuccess_ReturnsOk()
{
_systemService.Setup(s => s.UpdateNotificationConfigAsync(It.IsAny<object>())).ReturnsAsync(true);
var result = await _controller.UpdateNotificationConfig(new NotificationConfig());
result.Result.Should().BeOfType<OkObjectResult>();
}
[Fact]
public async Task UpdateNotificationConfig_WhenException_Returns500()
{
_systemService.Setup(s => s.UpdateNotificationConfigAsync(It.IsAny<object>())).ThrowsAsync(new Exception("t"));
var result = await _controller.UpdateNotificationConfig(new NotificationConfig());
result.Result.Should().BeOfType<ObjectResult>().Which.StatusCode.Should().Be(500);
}
[Fact]
public async Task ValidateConfiguration_WhenSuccess_ReturnsOk()
{
_systemService.Setup(s => s.ValidateConfigurationAsync(It.IsAny<object>())).ReturnsAsync(true);
var result = await _controller.ValidateConfiguration(new {});
result.Result.Should().BeOfType<OkObjectResult>();
}
[Fact]
public async Task ValidateConfiguration_WhenException_Returns500()
{
_systemService.Setup(s => s.ValidateConfigurationAsync(It.IsAny<object>())).ThrowsAsync(new Exception("t"));
var result = await _controller.ValidateConfiguration(new {});
result.Result.Should().BeOfType<ObjectResult>().Which.StatusCode.Should().Be(500);
}
// ExportConfiguration - ActionResult<FileContentResult> has type parameter issue in controller, skip direct test
// The controller method signature itself is invalid for ActionResult<T>, tested indirectly via GetSystemConfiguration
[Fact]
public async Task ImportConfiguration_WhenSuccess_ReturnsOk()
{
_systemService.Setup(s => s.ImportConfigurationAsync(It.IsAny<string>())).ReturnsAsync(true);
var result = await _controller.ImportConfiguration(new SystemConfiguration());
result.Result.Should().BeOfType<OkObjectResult>();
}
[Fact]
public async Task ImportConfiguration_WhenException_Returns500()
{
_systemService.Setup(s => s.ImportConfigurationAsync(It.IsAny<string>())).ThrowsAsync(new Exception("t"));
var result = await _controller.ImportConfiguration(new SystemConfiguration());
result.Result.Should().BeOfType<ObjectResult>().Which.StatusCode.Should().Be(500);
}
[Fact]
public async Task ResetToDefault_WhenSuccess_ReturnsOk()
{
_systemService.Setup(s => s.ResetToDefaultConfigurationAsync()).ReturnsAsync(true);
var result = await _controller.ResetToDefault();
result.Result.Should().BeOfType<OkObjectResult>();
}
[Fact]
public async Task ResetToDefault_WhenException_Returns500()
{
_systemService.Setup(s => s.ResetToDefaultConfigurationAsync()).ThrowsAsync(new Exception("t"));
var result = await _controller.ResetToDefault();
result.Result.Should().BeOfType<ObjectResult>().Which.StatusCode.Should().Be(500);
}
[Fact]
public async Task GetConfigurationHistory_WhenSuccess_ReturnsOk()
{
_systemService.Setup(s => s.GetConfigurationChangeHistoryAsync(null, null)).ReturnsAsync(new List<object>());
var result = await _controller.GetConfigurationHistory();
result.Result.Should().BeOfType<OkObjectResult>();
}
[Fact]
public async Task GetConfigurationHistory_WhenException_Returns500()
{
_systemService.Setup(s => s.GetConfigurationChangeHistoryAsync(It.IsAny<DateTime?>(), It.IsAny<DateTime?>())).ThrowsAsync(new Exception("t"));
var result = await _controller.GetConfigurationHistory();
result.Result.Should().BeOfType<ObjectResult>().Which.StatusCode.Should().Be(500);
}
}
}