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 _systemService; private readonly Mock _templateService; private readonly Mock _rulesService; private readonly Mock _statisticsService; private readonly ConfigController _controller; public ConfigControllerTests() { _systemService = new Mock(); _templateService = new Mock(); _rulesService = new Mock(); _statisticsService = new Mock(); _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(); } [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().Which.StatusCode.Should().Be(500); } [Fact] public async Task UpdateSystemConfiguration_WhenSuccess_ReturnsOk() { _systemService.Setup(s => s.UpdateSystemConfigurationAsync(It.IsAny())).ReturnsAsync(true); var result = await _controller.UpdateSystemConfiguration(new SystemConfiguration()); result.Result.Should().BeOfType(); } [Fact] public async Task UpdateSystemConfiguration_WhenException_Returns500() { _systemService.Setup(s => s.UpdateSystemConfigurationAsync(It.IsAny())).ThrowsAsync(new Exception("t")); var result = await _controller.UpdateSystemConfiguration(new SystemConfiguration()); result.Result.Should().BeOfType().Which.StatusCode.Should().Be(500); } [Fact] public async Task GetProductionTargets_WhenSuccess_ReturnsOk() { _systemService.Setup(s => s.GetProductionTargetsAsync()).ReturnsAsync(new List()); var result = await _controller.GetProductionTargets(); result.Result.Should().BeOfType(); } [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().Which.StatusCode.Should().Be(500); } [Fact] public async Task UpdateProductionTargets_WhenSuccess_ReturnsOk() { _systemService.Setup(s => s.UpdateProductionTargetsAsync(It.IsAny>())).ReturnsAsync(true); var result = await _controller.UpdateProductionTargets(new List()); result.Result.Should().BeOfType(); } [Fact] public async Task UpdateProductionTargets_WhenException_Returns500() { _systemService.Setup(s => s.UpdateProductionTargetsAsync(It.IsAny>())).ThrowsAsync(new Exception("t")); var result = await _controller.UpdateProductionTargets(new List()); result.Result.Should().BeOfType().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(); } [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().Which.StatusCode.Should().Be(500); } [Fact] public async Task UpdateWorkingHoursConfig_WhenSuccess_ReturnsOk() { _systemService.Setup(s => s.UpdateWorkingHoursConfigAsync(It.IsAny())).ReturnsAsync(true); var result = await _controller.UpdateWorkingHoursConfig(new WorkingHoursConfig()); result.Result.Should().BeOfType(); } [Fact] public async Task UpdateWorkingHoursConfig_WhenException_Returns500() { _systemService.Setup(s => s.UpdateWorkingHoursConfigAsync(It.IsAny())).ThrowsAsync(new Exception("t")); var result = await _controller.UpdateWorkingHoursConfig(new WorkingHoursConfig()); result.Result.Should().BeOfType().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(); } [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().Which.StatusCode.Should().Be(500); } [Fact] public async Task UpdateAlertConfiguration_WhenSuccess_ReturnsOk() { _systemService.Setup(s => s.UpdateAlertConfigurationAsync(It.IsAny())).ReturnsAsync(true); var result = await _controller.UpdateAlertConfiguration(new AlertConfiguration()); result.Result.Should().BeOfType(); } [Fact] public async Task UpdateAlertConfiguration_WhenException_Returns500() { _systemService.Setup(s => s.UpdateAlertConfigurationAsync(It.IsAny())).ThrowsAsync(new Exception("t")); var result = await _controller.UpdateAlertConfiguration(new AlertConfiguration()); result.Result.Should().BeOfType().Which.StatusCode.Should().Be(500); } [Fact] public async Task GetBusinessRules_WhenSuccess_ReturnsOk() { _rulesService.Setup(s => s.GetAllRulesAsync()).ReturnsAsync(new List()); var result = await _controller.GetBusinessRules(); result.Result.Should().BeOfType(); } [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().Which.StatusCode.Should().Be(500); } [Fact] public async Task CreateOrUpdateBusinessRule_WhenSuccess_ReturnsOk() { _rulesService.Setup(s => s.CreateOrUpdateRuleAsync(It.IsAny())).ReturnsAsync(new BusinessRule()); var result = await _controller.CreateOrUpdateBusinessRule(new Haoliang.Models.System.BusinessRuleConfig()); result.Result.Should().BeOfType(); } [Fact] public async Task CreateOrUpdateBusinessRule_WhenException_Returns500() { _rulesService.Setup(s => s.CreateOrUpdateRuleAsync(It.IsAny())).ThrowsAsync(new Exception("t")); var result = await _controller.CreateOrUpdateBusinessRule(new Haoliang.Models.System.BusinessRuleConfig()); result.Result.Should().BeOfType().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(); } [Fact] public async Task DeleteBusinessRule_WhenException_Returns500() { _rulesService.Setup(s => s.DeleteRuleAsync(It.IsAny())).ThrowsAsync(new Exception("t")); var result = await _controller.DeleteBusinessRule(1); result.Result.Should().BeOfType().Which.StatusCode.Should().Be(500); } [Fact] public async Task GetStatisticsRules_WhenSuccess_ReturnsOk() { _rulesService.Setup(s => s.GetStatisticsRulesAsync()).ReturnsAsync(new List()); var result = await _controller.GetStatisticsRules(); result.Result.Should().BeOfType(); } [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().Which.StatusCode.Should().Be(500); } [Fact] public async Task UpdateStatisticsRules_WhenSuccess_ReturnsOk() { _rulesService.Setup(s => s.UpdateStatisticsRulesAsync(It.IsAny>())).ReturnsAsync(true); var result = await _controller.UpdateStatisticsRules(new List()); result.Result.Should().BeOfType(); } [Fact] public async Task UpdateStatisticsRules_WhenException_Returns500() { _rulesService.Setup(s => s.UpdateStatisticsRulesAsync(It.IsAny>())).ThrowsAsync(new Exception("t")); var result = await _controller.UpdateStatisticsRules(new List()); result.Result.Should().BeOfType().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(); } [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().Which.StatusCode.Should().Be(500); } [Fact] public async Task UpdateDataRetentionConfig_WhenSuccess_ReturnsOk() { _systemService.Setup(s => s.UpdateDataRetentionConfigAsync(It.IsAny())).ReturnsAsync(true); var result = await _controller.UpdateDataRetentionConfig(new DataRetentionConfig()); result.Result.Should().BeOfType(); } [Fact] public async Task UpdateDataRetentionConfig_WhenException_Returns500() { _systemService.Setup(s => s.UpdateDataRetentionConfigAsync(It.IsAny())).ThrowsAsync(new Exception("t")); var result = await _controller.UpdateDataRetentionConfig(new DataRetentionConfig()); result.Result.Should().BeOfType().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(); } [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().Which.StatusCode.Should().Be(500); } [Fact] public async Task UpdateDashboardConfig_WhenSuccess_ReturnsOk() { _systemService.Setup(s => s.UpdateDashboardConfigAsync(It.IsAny())).ReturnsAsync(true); var result = await _controller.UpdateDashboardConfig(new DashboardConfig()); result.Result.Should().BeOfType(); } [Fact] public async Task UpdateDashboardConfig_WhenException_Returns500() { _systemService.Setup(s => s.UpdateDashboardConfigAsync(It.IsAny())).ThrowsAsync(new Exception("t")); var result = await _controller.UpdateDashboardConfig(new DashboardConfig()); result.Result.Should().BeOfType().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(); } [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().Which.StatusCode.Should().Be(500); } [Fact] public async Task UpdateExportConfig_WhenSuccess_ReturnsOk() { _systemService.Setup(s => s.UpdateExportConfigAsync(It.IsAny())).ReturnsAsync(true); var result = await _controller.UpdateExportConfig(new ExportConfig()); result.Result.Should().BeOfType(); } [Fact] public async Task UpdateExportConfig_WhenException_Returns500() { _systemService.Setup(s => s.UpdateExportConfigAsync(It.IsAny())).ThrowsAsync(new Exception("t")); var result = await _controller.UpdateExportConfig(new ExportConfig()); result.Result.Should().BeOfType().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(); } [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().Which.StatusCode.Should().Be(500); } [Fact] public async Task UpdateCollectionConfig_WhenSuccess_ReturnsOk() { _systemService.Setup(s => s.UpdateCollectionConfigAsync(It.IsAny())).ReturnsAsync(true); var result = await _controller.UpdateCollectionConfig(new CollectionConfig()); result.Result.Should().BeOfType(); } [Fact] public async Task UpdateCollectionConfig_WhenException_Returns500() { _systemService.Setup(s => s.UpdateCollectionConfigAsync(It.IsAny())).ThrowsAsync(new Exception("t")); var result = await _controller.UpdateCollectionConfig(new CollectionConfig()); result.Result.Should().BeOfType().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(); } [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().Which.StatusCode.Should().Be(500); } [Fact] public async Task UpdateNotificationConfig_WhenSuccess_ReturnsOk() { _systemService.Setup(s => s.UpdateNotificationConfigAsync(It.IsAny())).ReturnsAsync(true); var result = await _controller.UpdateNotificationConfig(new NotificationConfig()); result.Result.Should().BeOfType(); } [Fact] public async Task UpdateNotificationConfig_WhenException_Returns500() { _systemService.Setup(s => s.UpdateNotificationConfigAsync(It.IsAny())).ThrowsAsync(new Exception("t")); var result = await _controller.UpdateNotificationConfig(new NotificationConfig()); result.Result.Should().BeOfType().Which.StatusCode.Should().Be(500); } [Fact] public async Task ValidateConfiguration_WhenSuccess_ReturnsOk() { _systemService.Setup(s => s.ValidateConfigurationAsync(It.IsAny())).ReturnsAsync(true); var result = await _controller.ValidateConfiguration(new {}); result.Result.Should().BeOfType(); } [Fact] public async Task ValidateConfiguration_WhenException_Returns500() { _systemService.Setup(s => s.ValidateConfigurationAsync(It.IsAny())).ThrowsAsync(new Exception("t")); var result = await _controller.ValidateConfiguration(new {}); result.Result.Should().BeOfType().Which.StatusCode.Should().Be(500); } // ExportConfiguration - ActionResult has type parameter issue in controller, skip direct test // The controller method signature itself is invalid for ActionResult, tested indirectly via GetSystemConfiguration [Fact] public async Task ImportConfiguration_WhenSuccess_ReturnsOk() { _systemService.Setup(s => s.ImportConfigurationAsync(It.IsAny())).ReturnsAsync(true); var result = await _controller.ImportConfiguration(new SystemConfiguration()); result.Result.Should().BeOfType(); } [Fact] public async Task ImportConfiguration_WhenException_Returns500() { _systemService.Setup(s => s.ImportConfigurationAsync(It.IsAny())).ThrowsAsync(new Exception("t")); var result = await _controller.ImportConfiguration(new SystemConfiguration()); result.Result.Should().BeOfType().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(); } [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().Which.StatusCode.Should().Be(500); } [Fact] public async Task GetConfigurationHistory_WhenSuccess_ReturnsOk() { _systemService.Setup(s => s.GetConfigurationChangeHistoryAsync(null, null)).ReturnsAsync(new List()); var result = await _controller.GetConfigurationHistory(); result.Result.Should().BeOfType(); } [Fact] public async Task GetConfigurationHistory_WhenException_Returns500() { _systemService.Setup(s => s.GetConfigurationChangeHistoryAsync(It.IsAny(), It.IsAny())).ThrowsAsync(new Exception("t")); var result = await _controller.GetConfigurationHistory(); result.Result.Should().BeOfType().Which.StatusCode.Should().Be(500); } } }