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.
473 lines
16 KiB
C#
473 lines
16 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;
|
|
|
|
namespace Haoliang.Tests.Controllers
|
|
{
|
|
public class AlarmControllerTests
|
|
{
|
|
private readonly Mock<IAlarmService> _alarmService;
|
|
private readonly Mock<IAlarmRuleService> _alarmRuleService;
|
|
private readonly Mock<IAlarmNotificationService> _notificationService;
|
|
private readonly Mock<ILoggingService> _loggingService;
|
|
private readonly AlarmController _controller;
|
|
|
|
public AlarmControllerTests()
|
|
{
|
|
_alarmService = new Mock<IAlarmService>();
|
|
_alarmRuleService = new Mock<IAlarmRuleService>();
|
|
_notificationService = new Mock<IAlarmNotificationService>();
|
|
_loggingService = new Mock<ILoggingService>();
|
|
_controller = new AlarmController(
|
|
_alarmService.Object,
|
|
_alarmRuleService.Object,
|
|
_notificationService.Object,
|
|
_loggingService.Object);
|
|
}
|
|
|
|
// GetAllAlarms - 3 branches
|
|
[Fact]
|
|
public async Task GetAllAlarms_WithType_ReturnsOk()
|
|
{
|
|
_alarmService.Setup(s => s.GetAlarmsByTypeAsync(It.IsAny<AlarmType>()))
|
|
.ReturnsAsync(new List<Alarm>());
|
|
|
|
var result = await _controller.GetAllAlarms(AlarmType.DeviceError);
|
|
|
|
result.Should().BeOfType<OkObjectResult>();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetAllAlarms_WithStatus_ReturnsOk()
|
|
{
|
|
_alarmService.Setup(s => s.GetActiveAlarmsAsync())
|
|
.ReturnsAsync(new List<Alarm>());
|
|
|
|
var result = await _controller.GetAllAlarms(null, AlarmStatus.Active);
|
|
|
|
result.Should().BeOfType<OkObjectResult>();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetAllAlarms_WithoutFilters_ReturnsOk()
|
|
{
|
|
_alarmService.Setup(s => s.GetAllAlarmsAsync())
|
|
.ReturnsAsync(new List<Alarm>());
|
|
|
|
var result = await _controller.GetAllAlarms();
|
|
|
|
result.Should().BeOfType<OkObjectResult>();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetAllAlarms_WhenException_Returns500()
|
|
{
|
|
_alarmService.Setup(s => s.GetAllAlarmsAsync())
|
|
.ThrowsAsync(new Exception("test"));
|
|
|
|
var result = await _controller.GetAllAlarms();
|
|
|
|
var objResult = result.Should().BeOfType<ObjectResult>().Subject;
|
|
objResult.StatusCode.Should().Be(500);
|
|
}
|
|
|
|
// GetAlarm
|
|
[Fact]
|
|
public async Task GetAlarm_WhenFound_ReturnsOk()
|
|
{
|
|
_alarmService.Setup(s => s.GetAlarmByIdAsync(1))
|
|
.ReturnsAsync(new Alarm());
|
|
|
|
var result = await _controller.GetAlarm(1);
|
|
|
|
result.Should().BeOfType<OkObjectResult>();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetAlarm_WhenNotFound_ReturnsNotFound()
|
|
{
|
|
_alarmService.Setup(s => s.GetAlarmByIdAsync(999))
|
|
.ReturnsAsync((Alarm?)null);
|
|
|
|
var result = await _controller.GetAlarm(999);
|
|
|
|
result.Should().BeOfType<NotFoundObjectResult>();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetAlarm_WhenException_Returns500()
|
|
{
|
|
_alarmService.Setup(s => s.GetAlarmByIdAsync(It.IsAny<int>()))
|
|
.ThrowsAsync(new Exception("test"));
|
|
|
|
var result = await _controller.GetAlarm(1);
|
|
|
|
var objResult = result.Should().BeOfType<ObjectResult>().Subject;
|
|
objResult.StatusCode.Should().Be(500);
|
|
}
|
|
|
|
// CreateAlarm
|
|
[Fact]
|
|
public async Task CreateAlarm_WhenSuccess_ReturnsOk()
|
|
{
|
|
_alarmService.Setup(s => s.CreateAlarmAsync(It.IsAny<Alarm>()))
|
|
.ReturnsAsync(new Alarm());
|
|
|
|
var result = await _controller.CreateAlarm(new Alarm());
|
|
|
|
result.Should().BeOfType<OkObjectResult>();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task CreateAlarm_WhenException_Returns500()
|
|
{
|
|
_alarmService.Setup(s => s.CreateAlarmAsync(It.IsAny<Alarm>()))
|
|
.ThrowsAsync(new Exception("test"));
|
|
|
|
var result = await _controller.CreateAlarm(new Alarm());
|
|
|
|
var objResult = result.Should().BeOfType<ObjectResult>().Subject;
|
|
objResult.StatusCode.Should().Be(500);
|
|
}
|
|
|
|
// UpdateAlarm
|
|
[Fact]
|
|
public async Task UpdateAlarm_WhenFound_ReturnsOk()
|
|
{
|
|
_alarmService.Setup(s => s.UpdateAlarmAsync(It.IsAny<int>(), It.IsAny<Alarm>()))
|
|
.ReturnsAsync(new Alarm());
|
|
|
|
var result = await _controller.UpdateAlarm(1, new Alarm());
|
|
|
|
result.Should().BeOfType<OkObjectResult>();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task UpdateAlarm_WhenNotFound_ReturnsNotFound()
|
|
{
|
|
_alarmService.Setup(s => s.UpdateAlarmAsync(It.IsAny<int>(), It.IsAny<Alarm>()))
|
|
.ReturnsAsync((Alarm?)null);
|
|
|
|
var result = await _controller.UpdateAlarm(999, new Alarm());
|
|
|
|
result.Should().BeOfType<NotFoundObjectResult>();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task UpdateAlarm_WhenException_Returns500()
|
|
{
|
|
_alarmService.Setup(s => s.UpdateAlarmAsync(It.IsAny<int>(), It.IsAny<Alarm>()))
|
|
.ThrowsAsync(new Exception("test"));
|
|
|
|
var result = await _controller.UpdateAlarm(1, new Alarm());
|
|
|
|
var objResult = result.Should().BeOfType<ObjectResult>().Subject;
|
|
objResult.StatusCode.Should().Be(500);
|
|
}
|
|
|
|
// DeleteAlarm
|
|
[Fact]
|
|
public async Task DeleteAlarm_WhenFound_ReturnsOk()
|
|
{
|
|
_alarmService.Setup(s => s.DeleteAlarmAsync(1))
|
|
.ReturnsAsync(true);
|
|
|
|
var result = await _controller.DeleteAlarm(1);
|
|
|
|
result.Should().BeOfType<OkObjectResult>();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task DeleteAlarm_WhenNotFound_ReturnsNotFound()
|
|
{
|
|
_alarmService.Setup(s => s.DeleteAlarmAsync(999))
|
|
.ReturnsAsync(false);
|
|
|
|
var result = await _controller.DeleteAlarm(999);
|
|
|
|
result.Should().BeOfType<NotFoundObjectResult>();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task DeleteAlarm_WhenException_Returns500()
|
|
{
|
|
_alarmService.Setup(s => s.DeleteAlarmAsync(It.IsAny<int>()))
|
|
.ThrowsAsync(new Exception("test"));
|
|
|
|
var result = await _controller.DeleteAlarm(1);
|
|
|
|
var objResult = result.Should().BeOfType<ObjectResult>().Subject;
|
|
objResult.StatusCode.Should().Be(500);
|
|
}
|
|
|
|
// ResolveAlarm
|
|
[Fact]
|
|
public async Task ResolveAlarm_WhenFound_ReturnsOk()
|
|
{
|
|
_alarmService.Setup(s => s.ResolveAlarmAsync(1, It.IsAny<string?>()))
|
|
.ReturnsAsync(true);
|
|
|
|
var result = await _controller.ResolveAlarm(1, new ResolveAlarmRequest { ResolutionNote = "fixed" });
|
|
|
|
result.Should().BeOfType<OkObjectResult>();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ResolveAlarm_WhenNotFound_ReturnsNotFound()
|
|
{
|
|
_alarmService.Setup(s => s.ResolveAlarmAsync(It.IsAny<int>(), It.IsAny<string?>()))
|
|
.ReturnsAsync(false);
|
|
|
|
var result = await _controller.ResolveAlarm(999, new ResolveAlarmRequest());
|
|
|
|
result.Should().BeOfType<NotFoundObjectResult>();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ResolveAlarm_WhenException_Returns500()
|
|
{
|
|
_alarmService.Setup(s => s.ResolveAlarmAsync(It.IsAny<int>(), It.IsAny<string?>()))
|
|
.ThrowsAsync(new Exception("test"));
|
|
|
|
var result = await _controller.ResolveAlarm(1, new ResolveAlarmRequest());
|
|
|
|
var objResult = result.Should().BeOfType<ObjectResult>().Subject;
|
|
objResult.StatusCode.Should().Be(500);
|
|
}
|
|
|
|
// AcknowledgeAlarm
|
|
[Fact]
|
|
public async Task AcknowledgeAlarm_WhenFound_ReturnsOk()
|
|
{
|
|
_alarmService.Setup(s => s.AcknowledgeAlarmAsync(1, It.IsAny<string?>()))
|
|
.ReturnsAsync(true);
|
|
|
|
var result = await _controller.AcknowledgeAlarm(1, new AcknowledgeAlarmRequest { AcknowledgeNote = "ok" });
|
|
|
|
result.Should().BeOfType<OkObjectResult>();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task AcknowledgeAlarm_WhenNotFound_ReturnsNotFound()
|
|
{
|
|
_alarmService.Setup(s => s.AcknowledgeAlarmAsync(It.IsAny<int>(), It.IsAny<string?>()))
|
|
.ReturnsAsync(false);
|
|
|
|
var result = await _controller.AcknowledgeAlarm(999, new AcknowledgeAlarmRequest());
|
|
|
|
result.Should().BeOfType<NotFoundObjectResult>();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task AcknowledgeAlarm_WhenException_Returns500()
|
|
{
|
|
_alarmService.Setup(s => s.AcknowledgeAlarmAsync(It.IsAny<int>(), It.IsAny<string?>()))
|
|
.ThrowsAsync(new Exception("test"));
|
|
|
|
var result = await _controller.AcknowledgeAlarm(1, new AcknowledgeAlarmRequest());
|
|
|
|
var objResult = result.Should().BeOfType<ObjectResult>().Subject;
|
|
objResult.StatusCode.Should().Be(500);
|
|
}
|
|
|
|
// GetDeviceAlarms
|
|
[Fact]
|
|
public async Task GetDeviceAlarms_WhenSuccess_ReturnsOk()
|
|
{
|
|
_alarmService.Setup(s => s.GetDeviceAlarmsAsync(1, 7))
|
|
.ReturnsAsync(new List<Alarm>());
|
|
|
|
var result = await _controller.GetDeviceAlarms(1);
|
|
|
|
result.Should().BeOfType<OkObjectResult>();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetDeviceAlarms_WhenException_Returns500()
|
|
{
|
|
_alarmService.Setup(s => s.GetDeviceAlarmsAsync(It.IsAny<int>(), It.IsAny<int>()))
|
|
.ThrowsAsync(new Exception("test"));
|
|
|
|
var result = await _controller.GetDeviceAlarms(1);
|
|
|
|
var objResult = result.Should().BeOfType<ObjectResult>().Subject;
|
|
objResult.StatusCode.Should().Be(500);
|
|
}
|
|
|
|
// GetCriticalAlarms
|
|
[Fact]
|
|
public async Task GetCriticalAlarms_WhenSuccess_ReturnsOk()
|
|
{
|
|
_alarmService.Setup(s => s.GetCriticalAlarmsAsync())
|
|
.ReturnsAsync(new List<Alarm>());
|
|
|
|
var result = await _controller.GetCriticalAlarms();
|
|
|
|
result.Should().BeOfType<OkObjectResult>();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetCriticalAlarms_WhenException_Returns500()
|
|
{
|
|
_alarmService.Setup(s => s.GetCriticalAlarmsAsync())
|
|
.ThrowsAsync(new Exception("test"));
|
|
|
|
var result = await _controller.GetCriticalAlarms();
|
|
|
|
var objResult = result.Should().BeOfType<ObjectResult>().Subject;
|
|
objResult.StatusCode.Should().Be(500);
|
|
}
|
|
|
|
// GetAlarmStatistics
|
|
[Fact]
|
|
public async Task GetAlarmStatistics_WhenSuccess_ReturnsOk()
|
|
{
|
|
_alarmService.Setup(s => s.GetAlarmStatisticsAsync(It.IsAny<DateTime>()))
|
|
.ReturnsAsync(new AlarmStatistics());
|
|
|
|
var result = await _controller.GetAlarmStatistics(DateTime.Today);
|
|
|
|
result.Should().BeOfType<OkObjectResult>();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetAlarmStatistics_WhenException_Returns500()
|
|
{
|
|
_alarmService.Setup(s => s.GetAlarmStatisticsAsync(It.IsAny<DateTime>()))
|
|
.ThrowsAsync(new Exception("test"));
|
|
|
|
var result = await _controller.GetAlarmStatistics(DateTime.Today);
|
|
|
|
var objResult = result.Should().BeOfType<ObjectResult>().Subject;
|
|
objResult.StatusCode.Should().Be(500);
|
|
}
|
|
|
|
// GetAlarmsByDateRange
|
|
[Fact]
|
|
public async Task GetAlarmsByDateRange_WhenSuccess_ReturnsOk()
|
|
{
|
|
_alarmService.Setup(s => s.GetAlarmsByDateRangeAsync(It.IsAny<DateTime>(), It.IsAny<DateTime>()))
|
|
.ReturnsAsync(new List<Alarm>());
|
|
|
|
var result = await _controller.GetAlarmsByDateRange(DateTime.Today, DateTime.Today);
|
|
|
|
result.Should().BeOfType<OkObjectResult>();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetAlarmsByDateRange_WhenException_Returns500()
|
|
{
|
|
_alarmService.Setup(s => s.GetAlarmsByDateRangeAsync(It.IsAny<DateTime>(), It.IsAny<DateTime>()))
|
|
.ThrowsAsync(new Exception("test"));
|
|
|
|
var result = await _controller.GetAlarmsByDateRange(DateTime.Today, DateTime.Today);
|
|
|
|
var objResult = result.Should().BeOfType<ObjectResult>().Subject;
|
|
objResult.StatusCode.Should().Be(500);
|
|
}
|
|
|
|
// TestNotification
|
|
[Fact]
|
|
public async Task TestNotification_WhenSuccess_ReturnsOk()
|
|
{
|
|
var result = await _controller.TestNotification(new TestNotificationRequest
|
|
{
|
|
DeviceId = 1,
|
|
NotificationType = NotificationType.Email,
|
|
Recipient = "test@test.com"
|
|
});
|
|
|
|
result.Should().BeOfType<OkObjectResult>();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task TestNotification_WhenException_Returns500()
|
|
{
|
|
_notificationService.Setup(s => s.SendAlarmNotificationAsync(It.IsAny<AlarmNotification>()))
|
|
.ThrowsAsync(new Exception("test"));
|
|
|
|
var result = await _controller.TestNotification(new TestNotificationRequest());
|
|
|
|
var objResult = result.Should().BeOfType<ObjectResult>().Subject;
|
|
objResult.StatusCode.Should().Be(500);
|
|
}
|
|
|
|
// GetAlarmRules
|
|
[Fact]
|
|
public async Task GetAlarmRules_WhenSuccess_ReturnsOk()
|
|
{
|
|
_alarmRuleService.Setup(s => s.GetAllAlarmRulesAsync())
|
|
.ReturnsAsync(new List<AlarmRule>());
|
|
|
|
var result = await _controller.GetAlarmRules();
|
|
|
|
result.Should().BeOfType<OkObjectResult>();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetAlarmRules_WhenException_Returns500()
|
|
{
|
|
_alarmRuleService.Setup(s => s.GetAllAlarmRulesAsync())
|
|
.ThrowsAsync(new Exception("test"));
|
|
|
|
var result = await _controller.GetAlarmRules();
|
|
|
|
var objResult = result.Should().BeOfType<ObjectResult>().Subject;
|
|
objResult.StatusCode.Should().Be(500);
|
|
}
|
|
|
|
// CreateAlarmRule
|
|
[Fact]
|
|
public async Task CreateAlarmRule_WhenSuccess_ReturnsOk()
|
|
{
|
|
_alarmRuleService.Setup(s => s.CreateAlarmRuleAsync(It.IsAny<AlarmRule>()))
|
|
.ReturnsAsync(new AlarmRule());
|
|
|
|
var result = await _controller.CreateAlarmRule(new AlarmRule());
|
|
|
|
result.Should().BeOfType<OkObjectResult>();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task CreateAlarmRule_WhenException_Returns500()
|
|
{
|
|
_alarmRuleService.Setup(s => s.CreateAlarmRuleAsync(It.IsAny<AlarmRule>()))
|
|
.ThrowsAsync(new Exception("test"));
|
|
|
|
var result = await _controller.CreateAlarmRule(new AlarmRule());
|
|
|
|
var objResult = result.Should().BeOfType<ObjectResult>().Subject;
|
|
objResult.StatusCode.Should().Be(500);
|
|
}
|
|
|
|
// TestAlarmRule
|
|
[Fact]
|
|
public async Task TestAlarmRule_WhenSuccess_ReturnsOk()
|
|
{
|
|
var result = await _controller.TestAlarmRule(1);
|
|
|
|
result.Should().BeOfType<OkObjectResult>();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task TestAlarmRule_WhenException_Returns500()
|
|
{
|
|
_alarmRuleService.Setup(s => s.TestAlarmRuleAsync(It.IsAny<int>()))
|
|
.ThrowsAsync(new Exception("test"));
|
|
|
|
var result = await _controller.TestAlarmRule(1);
|
|
|
|
var objResult = result.Should().BeOfType<ObjectResult>().Subject;
|
|
objResult.StatusCode.Should().Be(500);
|
|
}
|
|
}
|
|
}
|