Phase 2 WP-1–13+23: Template Engine CRUD, composition, overrides, locking, collision detection, acyclicity
- WP-23: ITemplateEngineRepository full EF Core implementation - WP-1: Template CRUD with deletion constraints (instances, children, compositions) - WP-2–4: Attribute, alarm, script definitions with lock flags and override granularity - WP-5: Shared script CRUD with syntax validation - WP-6–7: Composition with recursive nesting and canonical naming - WP-8–11: Override granularity, locking rules, inheritance/composition scope - WP-12: Naming collision detection on canonical names (recursive) - WP-13: Graph acyclicity (inheritance + composition cycles) Core services: TemplateService, SharedScriptService, TemplateResolver, LockEnforcer, CollisionDetector, CycleDetector. 358 tests pass.
This commit is contained in:
@@ -0,0 +1,144 @@
|
||||
using Moq;
|
||||
using ScadaLink.Commons.Entities.Instances;
|
||||
using ScadaLink.Commons.Interfaces.Repositories;
|
||||
using ScadaLink.Commons.Interfaces.Services;
|
||||
using ScadaLink.TemplateEngine.Services;
|
||||
|
||||
namespace ScadaLink.TemplateEngine.Tests.Services;
|
||||
|
||||
public class AreaServiceTests
|
||||
{
|
||||
private readonly Mock<ITemplateEngineRepository> _repoMock = new();
|
||||
private readonly Mock<IAuditService> _auditMock = new();
|
||||
private readonly AreaService _sut;
|
||||
|
||||
public AreaServiceTests()
|
||||
{
|
||||
_sut = new AreaService(_repoMock.Object, _auditMock.Object);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CreateArea_ValidInput_ReturnsSuccess()
|
||||
{
|
||||
_repoMock.Setup(r => r.GetAreasBySiteIdAsync(1, It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(new List<Area>());
|
||||
_repoMock.Setup(r => r.SaveChangesAsync(It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(1);
|
||||
|
||||
var result = await _sut.CreateAreaAsync("Building A", 1, null, "admin");
|
||||
|
||||
Assert.True(result.IsSuccess);
|
||||
Assert.Equal("Building A", result.Value.Name);
|
||||
Assert.Equal(1, result.Value.SiteId);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CreateArea_DuplicateName_ReturnsFailure()
|
||||
{
|
||||
_repoMock.Setup(r => r.GetAreasBySiteIdAsync(1, It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(new List<Area>
|
||||
{
|
||||
new("Building A") { Id = 1, SiteId = 1, ParentAreaId = null }
|
||||
});
|
||||
|
||||
var result = await _sut.CreateAreaAsync("Building A", 1, null, "admin");
|
||||
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Contains("already exists", result.Error);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CreateArea_WithParent_ValidatesParentBelongsToSite()
|
||||
{
|
||||
_repoMock.Setup(r => r.GetAreaByIdAsync(5, It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(new Area("Parent") { Id = 5, SiteId = 99 }); // Different site!
|
||||
|
||||
var result = await _sut.CreateAreaAsync("Child", 1, 5, "admin");
|
||||
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Contains("does not belong", result.Error);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task DeleteArea_WithAssignedInstances_ReturnsFailure()
|
||||
{
|
||||
_repoMock.Setup(r => r.GetAreaByIdAsync(1, It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(new Area("Building A") { Id = 1, SiteId = 1 });
|
||||
_repoMock.Setup(r => r.GetInstancesBySiteIdAsync(1, It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(new List<Instance>
|
||||
{
|
||||
new("Inst1") { Id = 1, AreaId = 1, SiteId = 1 }
|
||||
});
|
||||
_repoMock.Setup(r => r.GetAreasBySiteIdAsync(1, It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(new List<Area> { new("Building A") { Id = 1, SiteId = 1 } });
|
||||
|
||||
var result = await _sut.DeleteAreaAsync(1, "admin");
|
||||
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Contains("instance(s) are assigned", result.Error);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task DeleteArea_WithChildAreas_ReturnsFailure()
|
||||
{
|
||||
_repoMock.Setup(r => r.GetAreaByIdAsync(1, It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(new Area("Parent") { Id = 1, SiteId = 1 });
|
||||
_repoMock.Setup(r => r.GetInstancesBySiteIdAsync(1, It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(new List<Instance>());
|
||||
_repoMock.Setup(r => r.GetAreasBySiteIdAsync(1, It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(new List<Area>
|
||||
{
|
||||
new("Parent") { Id = 1, SiteId = 1 },
|
||||
new("Child") { Id = 2, SiteId = 1, ParentAreaId = 1 }
|
||||
});
|
||||
|
||||
var result = await _sut.DeleteAreaAsync(1, "admin");
|
||||
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Contains("child areas", result.Error);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task DeleteArea_NoConstraints_Success()
|
||||
{
|
||||
_repoMock.Setup(r => r.GetAreaByIdAsync(1, It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(new Area("Empty") { Id = 1, SiteId = 1 });
|
||||
_repoMock.Setup(r => r.GetInstancesBySiteIdAsync(1, It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(new List<Instance>());
|
||||
_repoMock.Setup(r => r.GetAreasBySiteIdAsync(1, It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(new List<Area> { new("Empty") { Id = 1, SiteId = 1 } });
|
||||
_repoMock.Setup(r => r.SaveChangesAsync(It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(1);
|
||||
|
||||
var result = await _sut.DeleteAreaAsync(1, "admin");
|
||||
|
||||
Assert.True(result.IsSuccess);
|
||||
_repoMock.Verify(r => r.DeleteAreaAsync(1, It.IsAny<CancellationToken>()), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task DeleteArea_InstancesInDescendants_Blocked()
|
||||
{
|
||||
// Area hierarchy: Area1 -> Area2 -> Area3
|
||||
// Instance assigned to Area3, trying to delete Area1 should be blocked
|
||||
_repoMock.Setup(r => r.GetAreaByIdAsync(1, It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(new Area("Root") { Id = 1, SiteId = 1 });
|
||||
_repoMock.Setup(r => r.GetAreasBySiteIdAsync(1, It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(new List<Area>
|
||||
{
|
||||
new("Root") { Id = 1, SiteId = 1 },
|
||||
new("Mid") { Id = 2, SiteId = 1, ParentAreaId = 1 },
|
||||
new("Leaf") { Id = 3, SiteId = 1, ParentAreaId = 2 }
|
||||
});
|
||||
_repoMock.Setup(r => r.GetInstancesBySiteIdAsync(1, It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(new List<Instance>
|
||||
{
|
||||
new("DeepInstance") { Id = 10, AreaId = 3, SiteId = 1 }
|
||||
});
|
||||
|
||||
var result = await _sut.DeleteAreaAsync(1, "admin");
|
||||
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Contains("instance(s) are assigned", result.Error);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,185 @@
|
||||
using Moq;
|
||||
using ScadaLink.Commons.Entities.Instances;
|
||||
using ScadaLink.Commons.Entities.Templates;
|
||||
using ScadaLink.Commons.Interfaces.Repositories;
|
||||
using ScadaLink.Commons.Interfaces.Services;
|
||||
using ScadaLink.Commons.Types.Enums;
|
||||
using ScadaLink.TemplateEngine.Services;
|
||||
|
||||
namespace ScadaLink.TemplateEngine.Tests.Services;
|
||||
|
||||
public class InstanceServiceTests
|
||||
{
|
||||
private readonly Mock<ITemplateEngineRepository> _repoMock = new();
|
||||
private readonly Mock<IAuditService> _auditMock = new();
|
||||
private readonly InstanceService _sut;
|
||||
|
||||
public InstanceServiceTests()
|
||||
{
|
||||
_sut = new InstanceService(_repoMock.Object, _auditMock.Object);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CreateInstance_ValidInput_ReturnsSuccess()
|
||||
{
|
||||
_repoMock.Setup(r => r.GetTemplateByIdAsync(1, It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(new Template("TestTemplate") { Id = 1 });
|
||||
_repoMock.Setup(r => r.GetInstanceByUniqueNameAsync("Inst1", It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync((Instance?)null);
|
||||
_repoMock.Setup(r => r.SaveChangesAsync(It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(1);
|
||||
|
||||
var result = await _sut.CreateInstanceAsync("Inst1", 1, 1, null, "admin");
|
||||
|
||||
Assert.True(result.IsSuccess);
|
||||
Assert.Equal("Inst1", result.Value.UniqueName);
|
||||
Assert.Equal(InstanceState.Disabled, result.Value.State); // Starts disabled
|
||||
_repoMock.Verify(r => r.AddInstanceAsync(It.IsAny<Instance>(), It.IsAny<CancellationToken>()), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CreateInstance_DuplicateName_ReturnsFailure()
|
||||
{
|
||||
_repoMock.Setup(r => r.GetTemplateByIdAsync(1, It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(new Template("T") { Id = 1 });
|
||||
_repoMock.Setup(r => r.GetInstanceByUniqueNameAsync("Inst1", It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(new Instance("Inst1") { Id = 99 });
|
||||
|
||||
var result = await _sut.CreateInstanceAsync("Inst1", 1, 1, null, "admin");
|
||||
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Contains("already exists", result.Error);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CreateInstance_MissingTemplate_ReturnsFailure()
|
||||
{
|
||||
_repoMock.Setup(r => r.GetTemplateByIdAsync(999, It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync((Template?)null);
|
||||
|
||||
var result = await _sut.CreateInstanceAsync("Inst1", 999, 1, null, "admin");
|
||||
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Contains("not found", result.Error);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task SetAttributeOverride_LockedAttribute_ReturnsFailure()
|
||||
{
|
||||
var instance = new Instance("Inst1") { Id = 1, TemplateId = 1 };
|
||||
_repoMock.Setup(r => r.GetInstanceByIdAsync(1, It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(instance);
|
||||
_repoMock.Setup(r => r.GetAttributesByTemplateIdAsync(1, It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(new List<TemplateAttribute>
|
||||
{
|
||||
new("LockedAttr") { IsLocked = true }
|
||||
});
|
||||
|
||||
var result = await _sut.SetAttributeOverrideAsync(1, "LockedAttr", "new", "admin");
|
||||
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Contains("locked", result.Error, StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task SetAttributeOverride_NonExistentAttribute_ReturnsFailure()
|
||||
{
|
||||
var instance = new Instance("Inst1") { Id = 1, TemplateId = 1 };
|
||||
_repoMock.Setup(r => r.GetInstanceByIdAsync(1, It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(instance);
|
||||
_repoMock.Setup(r => r.GetAttributesByTemplateIdAsync(1, It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(new List<TemplateAttribute>());
|
||||
|
||||
var result = await _sut.SetAttributeOverrideAsync(1, "Missing", "value", "admin");
|
||||
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Contains("does not exist", result.Error);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task SetAttributeOverride_UnlockedAttribute_ReturnsSuccess()
|
||||
{
|
||||
var instance = new Instance("Inst1") { Id = 1, TemplateId = 1 };
|
||||
_repoMock.Setup(r => r.GetInstanceByIdAsync(1, It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(instance);
|
||||
_repoMock.Setup(r => r.GetAttributesByTemplateIdAsync(1, It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(new List<TemplateAttribute>
|
||||
{
|
||||
new("Threshold") { IsLocked = false }
|
||||
});
|
||||
_repoMock.Setup(r => r.GetOverridesByInstanceIdAsync(1, It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(new List<InstanceAttributeOverride>());
|
||||
_repoMock.Setup(r => r.SaveChangesAsync(It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(1);
|
||||
|
||||
var result = await _sut.SetAttributeOverrideAsync(1, "Threshold", "99", "admin");
|
||||
|
||||
Assert.True(result.IsSuccess);
|
||||
Assert.Equal("Threshold", result.Value.AttributeName);
|
||||
Assert.Equal("99", result.Value.OverrideValue);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Enable_ExistingInstance_SetsEnabled()
|
||||
{
|
||||
var instance = new Instance("Inst1") { Id = 1, State = InstanceState.Disabled };
|
||||
_repoMock.Setup(r => r.GetInstanceByIdAsync(1, It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(instance);
|
||||
_repoMock.Setup(r => r.SaveChangesAsync(It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(1);
|
||||
|
||||
var result = await _sut.EnableAsync(1, "admin");
|
||||
|
||||
Assert.True(result.IsSuccess);
|
||||
Assert.Equal(InstanceState.Enabled, result.Value.State);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Disable_ExistingInstance_SetsDisabled()
|
||||
{
|
||||
var instance = new Instance("Inst1") { Id = 1, State = InstanceState.Enabled };
|
||||
_repoMock.Setup(r => r.GetInstanceByIdAsync(1, It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(instance);
|
||||
_repoMock.Setup(r => r.SaveChangesAsync(It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(1);
|
||||
|
||||
var result = await _sut.DisableAsync(1, "admin");
|
||||
|
||||
Assert.True(result.IsSuccess);
|
||||
Assert.Equal(InstanceState.Disabled, result.Value.State);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task SetConnectionBindings_BulkAssignment_Success()
|
||||
{
|
||||
var instance = new Instance("Inst1") { Id = 1 };
|
||||
_repoMock.Setup(r => r.GetInstanceByIdAsync(1, It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(instance);
|
||||
_repoMock.Setup(r => r.GetBindingsByInstanceIdAsync(1, It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(new List<InstanceConnectionBinding>());
|
||||
_repoMock.Setup(r => r.SaveChangesAsync(It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(1);
|
||||
|
||||
var bindings = new List<(string, int)> { ("Temp", 100), ("Pressure", 200) };
|
||||
var result = await _sut.SetConnectionBindingsAsync(1, bindings, "admin");
|
||||
|
||||
Assert.True(result.IsSuccess);
|
||||
Assert.Equal(2, result.Value.Count);
|
||||
_repoMock.Verify(r => r.AddInstanceConnectionBindingAsync(It.IsAny<InstanceConnectionBinding>(), It.IsAny<CancellationToken>()), Times.Exactly(2));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task AssignToArea_AreaInDifferentSite_ReturnsFailure()
|
||||
{
|
||||
var instance = new Instance("Inst1") { Id = 1, SiteId = 1 };
|
||||
_repoMock.Setup(r => r.GetInstanceByIdAsync(1, It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(instance);
|
||||
_repoMock.Setup(r => r.GetAreaByIdAsync(5, It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(new Area("WrongSiteArea") { Id = 5, SiteId = 99 });
|
||||
|
||||
var result = await _sut.AssignToAreaAsync(1, 5, "admin");
|
||||
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Contains("does not belong", result.Error);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,149 @@
|
||||
using Moq;
|
||||
using ScadaLink.Commons.Entities.Instances;
|
||||
using ScadaLink.Commons.Entities.Sites;
|
||||
using ScadaLink.Commons.Interfaces.Repositories;
|
||||
using ScadaLink.Commons.Interfaces.Services;
|
||||
using ScadaLink.TemplateEngine.Services;
|
||||
|
||||
namespace ScadaLink.TemplateEngine.Tests.Services;
|
||||
|
||||
public class SiteServiceTests
|
||||
{
|
||||
private readonly Mock<ISiteRepository> _repoMock = new();
|
||||
private readonly Mock<IAuditService> _auditMock = new();
|
||||
private readonly SiteService _sut;
|
||||
|
||||
public SiteServiceTests()
|
||||
{
|
||||
_sut = new SiteService(_repoMock.Object, _auditMock.Object);
|
||||
}
|
||||
|
||||
[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);
|
||||
}
|
||||
|
||||
[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);
|
||||
}
|
||||
|
||||
[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);
|
||||
}
|
||||
|
||||
[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);
|
||||
}
|
||||
|
||||
[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);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CreateDataConnection_ValidInput_Success()
|
||||
{
|
||||
_repoMock.Setup(r => r.SaveChangesAsync(It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(1);
|
||||
|
||||
var result = await _sut.CreateDataConnectionAsync("OPC-Server1", "OpcUa", "{\"url\":\"opc.tcp://localhost\"}", "admin");
|
||||
|
||||
Assert.True(result.IsSuccess);
|
||||
Assert.Equal("OPC-Server1", result.Value.Name);
|
||||
Assert.Equal("OpcUa", result.Value.Protocol);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task AssignConnectionToSite_AlreadyAssigned_ReturnsFailure()
|
||||
{
|
||||
_repoMock.Setup(r => r.GetSiteByIdAsync(1, It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(new Site("S", "S1") { Id = 1 });
|
||||
_repoMock.Setup(r => r.GetDataConnectionByIdAsync(100, It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(new DataConnection("Conn", "OpcUa") { Id = 100 });
|
||||
_repoMock.Setup(r => r.GetSiteDataConnectionAssignmentAsync(1, 100, It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(new SiteDataConnectionAssignment { Id = 1, SiteId = 1, DataConnectionId = 100 });
|
||||
|
||||
var result = await _sut.AssignConnectionToSiteAsync(1, 100, "admin");
|
||||
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Contains("already assigned", result.Error);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task AssignConnectionToSite_Valid_Success()
|
||||
{
|
||||
_repoMock.Setup(r => r.GetSiteByIdAsync(1, It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(new Site("S", "S1") { Id = 1 });
|
||||
_repoMock.Setup(r => r.GetDataConnectionByIdAsync(100, It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(new DataConnection("Conn", "OpcUa") { Id = 100 });
|
||||
_repoMock.Setup(r => r.GetSiteDataConnectionAssignmentAsync(1, 100, It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync((SiteDataConnectionAssignment?)null);
|
||||
_repoMock.Setup(r => r.SaveChangesAsync(It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(1);
|
||||
|
||||
var result = await _sut.AssignConnectionToSiteAsync(1, 100, "admin");
|
||||
|
||||
Assert.True(result.IsSuccess);
|
||||
}
|
||||
|
||||
[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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,174 @@
|
||||
using Moq;
|
||||
using ScadaLink.Commons.Entities.Instances;
|
||||
using ScadaLink.Commons.Entities.Templates;
|
||||
using ScadaLink.Commons.Interfaces.Repositories;
|
||||
using ScadaLink.TemplateEngine.Services;
|
||||
|
||||
namespace ScadaLink.TemplateEngine.Tests.Services;
|
||||
|
||||
public class TemplateDeletionServiceTests
|
||||
{
|
||||
private readonly Mock<ITemplateEngineRepository> _repoMock = new();
|
||||
private readonly TemplateDeletionService _sut;
|
||||
|
||||
public TemplateDeletionServiceTests()
|
||||
{
|
||||
_sut = new TemplateDeletionService(_repoMock.Object);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CanDeleteTemplate_NoReferences_ReturnsSuccess()
|
||||
{
|
||||
_repoMock.Setup(r => r.GetTemplateByIdAsync(1, It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(new Template("Orphan") { Id = 1 });
|
||||
_repoMock.Setup(r => r.GetInstancesByTemplateIdAsync(1, It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(new List<Instance>());
|
||||
_repoMock.Setup(r => r.GetAllTemplatesAsync(It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(new List<Template> { new("Orphan") { Id = 1 } });
|
||||
_repoMock.Setup(r => r.GetCompositionsByTemplateIdAsync(It.IsAny<int>(), It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(new List<TemplateComposition>());
|
||||
|
||||
var result = await _sut.CanDeleteTemplateAsync(1);
|
||||
|
||||
Assert.True(result.IsSuccess);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CanDeleteTemplate_WithInstances_ReturnsFailure()
|
||||
{
|
||||
_repoMock.Setup(r => r.GetTemplateByIdAsync(1, It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(new Template("Used") { Id = 1 });
|
||||
_repoMock.Setup(r => r.GetInstancesByTemplateIdAsync(1, It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(new List<Instance>
|
||||
{
|
||||
new("Inst1") { Id = 1 },
|
||||
new("Inst2") { Id = 2 }
|
||||
});
|
||||
_repoMock.Setup(r => r.GetAllTemplatesAsync(It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(new List<Template> { new("Used") { Id = 1 } });
|
||||
_repoMock.Setup(r => r.GetCompositionsByTemplateIdAsync(It.IsAny<int>(), It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(new List<TemplateComposition>());
|
||||
|
||||
var result = await _sut.CanDeleteTemplateAsync(1);
|
||||
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Contains("2 instance(s)", result.Error);
|
||||
Assert.Contains("Inst1", result.Error);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CanDeleteTemplate_WithChildTemplates_ReturnsFailure()
|
||||
{
|
||||
_repoMock.Setup(r => r.GetTemplateByIdAsync(1, It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(new Template("Base") { Id = 1 });
|
||||
_repoMock.Setup(r => r.GetInstancesByTemplateIdAsync(1, It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(new List<Instance>());
|
||||
_repoMock.Setup(r => r.GetAllTemplatesAsync(It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(new List<Template>
|
||||
{
|
||||
new("Base") { Id = 1 },
|
||||
new("Child") { Id = 2, ParentTemplateId = 1 }
|
||||
});
|
||||
_repoMock.Setup(r => r.GetCompositionsByTemplateIdAsync(It.IsAny<int>(), It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(new List<TemplateComposition>());
|
||||
|
||||
var result = await _sut.CanDeleteTemplateAsync(1);
|
||||
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Contains("child template(s)", result.Error);
|
||||
Assert.Contains("Child", result.Error);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CanDeleteTemplate_ComposedByOthers_ReturnsFailure()
|
||||
{
|
||||
_repoMock.Setup(r => r.GetTemplateByIdAsync(1, It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(new Template("Module") { Id = 1 });
|
||||
_repoMock.Setup(r => r.GetInstancesByTemplateIdAsync(1, It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(new List<Instance>());
|
||||
_repoMock.Setup(r => r.GetAllTemplatesAsync(It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(new List<Template>
|
||||
{
|
||||
new("Module") { Id = 1 },
|
||||
new("Composer") { Id = 2 }
|
||||
});
|
||||
_repoMock.Setup(r => r.GetCompositionsByTemplateIdAsync(2, It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(new List<TemplateComposition>
|
||||
{
|
||||
new("PumpModule") { ComposedTemplateId = 1 }
|
||||
});
|
||||
_repoMock.Setup(r => r.GetCompositionsByTemplateIdAsync(1, It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(new List<TemplateComposition>());
|
||||
|
||||
var result = await _sut.CanDeleteTemplateAsync(1);
|
||||
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Contains("compose it", result.Error);
|
||||
Assert.Contains("Composer", result.Error);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CanDeleteTemplate_NotFound_ReturnsFailure()
|
||||
{
|
||||
_repoMock.Setup(r => r.GetTemplateByIdAsync(999, It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync((Template?)null);
|
||||
|
||||
var result = await _sut.CanDeleteTemplateAsync(999);
|
||||
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Contains("not found", result.Error);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task DeleteTemplate_AllConstraintsMet_Deletes()
|
||||
{
|
||||
_repoMock.Setup(r => r.GetTemplateByIdAsync(1, It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(new Template("Safe") { Id = 1 });
|
||||
_repoMock.Setup(r => r.GetInstancesByTemplateIdAsync(1, It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(new List<Instance>());
|
||||
_repoMock.Setup(r => r.GetAllTemplatesAsync(It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(new List<Template> { new("Safe") { Id = 1 } });
|
||||
_repoMock.Setup(r => r.GetCompositionsByTemplateIdAsync(It.IsAny<int>(), It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(new List<TemplateComposition>());
|
||||
_repoMock.Setup(r => r.SaveChangesAsync(It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(1);
|
||||
|
||||
var result = await _sut.DeleteTemplateAsync(1);
|
||||
|
||||
Assert.True(result.IsSuccess);
|
||||
_repoMock.Verify(r => r.DeleteTemplateAsync(1, It.IsAny<CancellationToken>()), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CanDeleteTemplate_MultipleConstraints_AllErrorsReported()
|
||||
{
|
||||
_repoMock.Setup(r => r.GetTemplateByIdAsync(1, It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(new Template("Busy") { Id = 1 });
|
||||
_repoMock.Setup(r => r.GetInstancesByTemplateIdAsync(1, It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(new List<Instance> { new("Inst1") { Id = 1 } });
|
||||
_repoMock.Setup(r => r.GetAllTemplatesAsync(It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(new List<Template>
|
||||
{
|
||||
new("Busy") { Id = 1 },
|
||||
new("Child") { Id = 2, ParentTemplateId = 1 },
|
||||
new("Composer") { Id = 3 }
|
||||
});
|
||||
_repoMock.Setup(r => r.GetCompositionsByTemplateIdAsync(3, It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(new List<TemplateComposition>
|
||||
{
|
||||
new("Module") { ComposedTemplateId = 1 }
|
||||
});
|
||||
_repoMock.Setup(r => r.GetCompositionsByTemplateIdAsync(1, It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(new List<TemplateComposition>());
|
||||
_repoMock.Setup(r => r.GetCompositionsByTemplateIdAsync(2, It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(new List<TemplateComposition>());
|
||||
|
||||
var result = await _sut.CanDeleteTemplateAsync(1);
|
||||
|
||||
Assert.True(result.IsFailure);
|
||||
// All three constraint types should be mentioned
|
||||
Assert.Contains("instance(s)", result.Error);
|
||||
Assert.Contains("child template(s)", result.Error);
|
||||
Assert.Contains("compose it", result.Error);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user