513fa1aa16
Comment/XML-doc changes only: - add summaries to ~10k test methods, fixtures, fakes and helpers (+ <param> on theories, <returns> on async Task methods, <inheritdoc/> on interface implementations the checker can resolve) - strip ~2.6k project-tracking references (WP*, P*-*, row-NN, Task N, #NN, Component-0NN) from comments, keeping the explanatory text; reword false positives in comments only - real docs on fakes implementing BCL/library interfaces (IDisposable, ILogger, IOptionsMonitor, IAsyncLifetime) - reword the last 5 src/ false positives (CWE-1236, failures-1, year-0001, M365) Only non-comment edits: four one-line nested classes split across lines so members can carry docs. Build 0 errors / 0 warnings.
133 lines
5.4 KiB
C#
133 lines
5.4 KiB
C#
using Moq;
|
|
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Instances;
|
|
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Sites;
|
|
using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Repositories;
|
|
using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Services;
|
|
using ZB.MOM.WW.ScadaBridge.TemplateEngine.Services;
|
|
|
|
namespace ZB.MOM.WW.ScadaBridge.TemplateEngine.Tests.Services;
|
|
|
|
public class SiteServiceTests
|
|
{
|
|
private readonly Mock<ISiteRepository> _repoMock = new();
|
|
private readonly Mock<IAuditService> _auditMock = new();
|
|
private readonly SiteService _sut;
|
|
|
|
/// <summary>Initializes the test class with a mocked repository and the service under test.</summary>
|
|
public SiteServiceTests()
|
|
{
|
|
_sut = new SiteService(_repoMock.Object, _auditMock.Object);
|
|
}
|
|
|
|
/// <summary>Verifies that creating a site with valid input succeeds.</summary>
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
[Fact]
|
|
public async Task CreateSite_ValidInput_ReturnsSuccess()
|
|
{
|
|
_repoMock.Setup(r => r.GetSiteByIdentifierAsync("SITE-001", It.IsAny<CancellationToken>()))
|
|
.ReturnsAsync((Site?)null);
|
|
_repoMock.Setup(r => r.SaveChangesAsync(It.IsAny<CancellationToken>()))
|
|
.ReturnsAsync(1);
|
|
|
|
var result = await _sut.CreateSiteAsync("Plant Alpha", "SITE-001", "Main plant", "admin");
|
|
|
|
Assert.True(result.IsSuccess);
|
|
Assert.Equal("Plant Alpha", result.Value.Name);
|
|
Assert.Equal("SITE-001", result.Value.SiteIdentifier);
|
|
}
|
|
|
|
/// <summary>Verifies that creating a site with a duplicate identifier fails.</summary>
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
[Fact]
|
|
public async Task CreateSite_DuplicateIdentifier_ReturnsFailure()
|
|
{
|
|
_repoMock.Setup(r => r.GetSiteByIdentifierAsync("SITE-001", It.IsAny<CancellationToken>()))
|
|
.ReturnsAsync(new Site("Existing", "SITE-001") { Id = 1 });
|
|
|
|
var result = await _sut.CreateSiteAsync("New", "SITE-001", null, "admin");
|
|
|
|
Assert.True(result.IsFailure);
|
|
Assert.Contains("already exists", result.Error);
|
|
}
|
|
|
|
/// <summary>Verifies that creating a site with an empty name fails.</summary>
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
[Fact]
|
|
public async Task CreateSite_EmptyName_ReturnsFailure()
|
|
{
|
|
var result = await _sut.CreateSiteAsync("", "SITE-001", null, "admin");
|
|
|
|
Assert.True(result.IsFailure);
|
|
Assert.Contains("required", result.Error);
|
|
}
|
|
|
|
/// <summary>Verifies that deleting a site that has instances fails.</summary>
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
[Fact]
|
|
public async Task DeleteSite_WithInstances_ReturnsFailure()
|
|
{
|
|
_repoMock.Setup(r => r.GetSiteByIdAsync(1, It.IsAny<CancellationToken>()))
|
|
.ReturnsAsync(new Site("Plant", "SITE-001") { Id = 1 });
|
|
_repoMock.Setup(r => r.GetInstancesBySiteIdAsync(1, It.IsAny<CancellationToken>()))
|
|
.ReturnsAsync(new List<Instance>
|
|
{
|
|
new("Inst1") { Id = 1, SiteId = 1 },
|
|
new("Inst2") { Id = 2, SiteId = 1 }
|
|
});
|
|
|
|
var result = await _sut.DeleteSiteAsync(1, "admin");
|
|
|
|
Assert.True(result.IsFailure);
|
|
Assert.Contains("2 instance(s)", result.Error);
|
|
}
|
|
|
|
/// <summary>Verifies that deleting a site without instances succeeds.</summary>
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
[Fact]
|
|
public async Task DeleteSite_NoInstances_Success()
|
|
{
|
|
_repoMock.Setup(r => r.GetSiteByIdAsync(1, It.IsAny<CancellationToken>()))
|
|
.ReturnsAsync(new Site("Plant", "SITE-001") { Id = 1 });
|
|
_repoMock.Setup(r => r.GetInstancesBySiteIdAsync(1, It.IsAny<CancellationToken>()))
|
|
.ReturnsAsync(new List<Instance>());
|
|
_repoMock.Setup(r => r.SaveChangesAsync(It.IsAny<CancellationToken>()))
|
|
.ReturnsAsync(1);
|
|
|
|
var result = await _sut.DeleteSiteAsync(1, "admin");
|
|
|
|
Assert.True(result.IsSuccess);
|
|
}
|
|
|
|
/// <summary>Verifies that creating a data connection with valid input succeeds.</summary>
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
[Fact]
|
|
public async Task CreateDataConnection_ValidInput_Success()
|
|
{
|
|
_repoMock.Setup(r => r.SaveChangesAsync(It.IsAny<CancellationToken>()))
|
|
.ReturnsAsync(1);
|
|
|
|
var result = await _sut.CreateDataConnectionAsync(1, "OPC-Server1", "OpcUa", "{\"url\":\"opc.tcp://localhost\"}", null, 3, "admin");
|
|
|
|
Assert.True(result.IsSuccess);
|
|
Assert.Equal("OPC-Server1", result.Value.Name);
|
|
Assert.Equal("OpcUa", result.Value.Protocol);
|
|
Assert.Equal(1, result.Value.SiteId);
|
|
}
|
|
|
|
/// <summary>Verifies that updating a site with valid input succeeds.</summary>
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
[Fact]
|
|
public async Task UpdateSite_ValidInput_Success()
|
|
{
|
|
_repoMock.Setup(r => r.GetSiteByIdAsync(1, It.IsAny<CancellationToken>()))
|
|
.ReturnsAsync(new Site("Old", "S1") { Id = 1 });
|
|
_repoMock.Setup(r => r.SaveChangesAsync(It.IsAny<CancellationToken>()))
|
|
.ReturnsAsync(1);
|
|
|
|
var result = await _sut.UpdateSiteAsync(1, "New Name", "New desc", "admin");
|
|
|
|
Assert.True(result.IsSuccess);
|
|
Assert.Equal("New Name", result.Value.Name);
|
|
}
|
|
}
|