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 _repoMock = new(); private readonly Mock _auditMock = new(); private readonly SiteService _sut; /// Initializes the test class with a mocked repository and the service under test. public SiteServiceTests() { _sut = new SiteService(_repoMock.Object, _auditMock.Object); } /// Verifies that creating a site with valid input succeeds. /// A task that represents the asynchronous operation. [Fact] public async Task CreateSite_ValidInput_ReturnsSuccess() { _repoMock.Setup(r => r.GetSiteByIdentifierAsync("SITE-001", It.IsAny())) .ReturnsAsync((Site?)null); _repoMock.Setup(r => r.SaveChangesAsync(It.IsAny())) .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); } /// Verifies that creating a site with a duplicate identifier fails. /// A task that represents the asynchronous operation. [Fact] public async Task CreateSite_DuplicateIdentifier_ReturnsFailure() { _repoMock.Setup(r => r.GetSiteByIdentifierAsync("SITE-001", It.IsAny())) .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); } /// Verifies that creating a site with an empty name fails. /// A task that represents the asynchronous operation. [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); } /// Verifies that deleting a site that has instances fails. /// A task that represents the asynchronous operation. [Fact] public async Task DeleteSite_WithInstances_ReturnsFailure() { _repoMock.Setup(r => r.GetSiteByIdAsync(1, It.IsAny())) .ReturnsAsync(new Site("Plant", "SITE-001") { Id = 1 }); _repoMock.Setup(r => r.GetInstancesBySiteIdAsync(1, It.IsAny())) .ReturnsAsync(new List { 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); } /// Verifies that deleting a site without instances succeeds. /// A task that represents the asynchronous operation. [Fact] public async Task DeleteSite_NoInstances_Success() { _repoMock.Setup(r => r.GetSiteByIdAsync(1, It.IsAny())) .ReturnsAsync(new Site("Plant", "SITE-001") { Id = 1 }); _repoMock.Setup(r => r.GetInstancesBySiteIdAsync(1, It.IsAny())) .ReturnsAsync(new List()); _repoMock.Setup(r => r.SaveChangesAsync(It.IsAny())) .ReturnsAsync(1); var result = await _sut.DeleteSiteAsync(1, "admin"); Assert.True(result.IsSuccess); } /// /// Review 2026-10-21 08/C1: SiteService must not offer a data-connection create/update /// that audits the raw DataConnection entity — its Primary/BackupConfiguration /// can embed protocol credentials, which would land in plaintext in the indefinitely /// retained config audit. The Management Service path (which audits the scrubbed /// public shape) is the only writer. /// [Theory] [InlineData("CreateDataConnectionAsync")] [InlineData("UpdateDataConnectionAsync")] public void SiteService_HasNoRawDataConnectionWriter(string method) { Assert.Null(typeof(SiteService).GetMethod(method)); } /// Verifies that updating a site with valid input succeeds. /// A task that represents the asynchronous operation. [Fact] public async Task UpdateSite_ValidInput_Success() { _repoMock.Setup(r => r.GetSiteByIdAsync(1, It.IsAny())) .ReturnsAsync(new Site("Old", "S1") { Id = 1 }); _repoMock.Setup(r => r.SaveChangesAsync(It.IsAny())) .ReturnsAsync(1); var result = await _sut.UpdateSiteAsync(1, "New Name", "New desc", "admin"); Assert.True(result.IsSuccess); Assert.Equal("New Name", result.Value.Name); } }