diff --git a/Core/SmartIOT.Connector.Core/SmartIotConnector.cs b/Core/SmartIOT.Connector.Core/SmartIotConnector.cs index 6e4cdcd..b6b1b60 100644 --- a/Core/SmartIOT.Connector.Core/SmartIotConnector.cs +++ b/Core/SmartIOT.Connector.Core/SmartIotConnector.cs @@ -154,13 +154,16 @@ public async Task RemoveConnectorAsync(IConnector connector) public async Task ReplaceConnectorAsync(int index, IConnector connector) { - if (!_connectors.TryReplaceAt(index, connector, out _)) + if (!_connectors.TryReplaceAt(index, connector, out var oldConnector)) return false; AddConnectorEvents(connector); if (IsStarted) + { + await oldConnector!.StopAsync(); await connector.StartAsync(this); + } return true; } diff --git a/Tests/SmartIOT.Connector.Mocks/FakeConnector.cs b/Tests/SmartIOT.Connector.Mocks/FakeConnector.cs index 95b7e14..a124728 100644 --- a/Tests/SmartIOT.Connector.Mocks/FakeConnector.cs +++ b/Tests/SmartIOT.Connector.Mocks/FakeConnector.cs @@ -13,6 +13,8 @@ public class FakeConnector : AbstractConnector public IList DeviceStatusEvents { get; } = new List(); public IList ExceptionEvents { get; } = new List(); public IServiceProvider? ServiceProvider { get; } + public ManualResetEventSlim StartEvent { get; } = new(); + public ManualResetEventSlim StopEvent { get; } = new(); public FakeConnector() : base("fake://") { @@ -55,11 +57,13 @@ public void ClearEvents() public override Task StartAsync(ISmartIOTConnectorInterface connectorInterface) { _connectorInterface = connectorInterface; + StartEvent.Set(); return Task.CompletedTask; } public override Task StopAsync() { + StopEvent.Set(); return Task.CompletedTask; } diff --git a/Tests/SmartIOT.Connector.RestApi.Tests/ConnectorControllerTests.cs b/Tests/SmartIOT.Connector.RestApi.Tests/ConnectorControllerTests.cs index c7dfd15..2a40ad2 100644 --- a/Tests/SmartIOT.Connector.RestApi.Tests/ConnectorControllerTests.cs +++ b/Tests/SmartIOT.Connector.RestApi.Tests/ConnectorControllerTests.cs @@ -1,5 +1,6 @@ using Microsoft.AspNetCore.Mvc; using SmartIOT.Connector.Core; +using SmartIOT.Connector.Mocks; using SmartIOT.Connector.RestApi.Controllers.V1; using SmartIOT.Connector.RestApi.Services; @@ -7,15 +8,16 @@ namespace SmartIOT.Connector.RestApi.Tests; public class ConnectorControllerTests { - private ConnectorController SetupController() + private static SmartIotConnectorBuilder CreateBuilder() { - var builder = new SmartIotConnectorBuilder() + return new SmartIotConnectorBuilder() .WithAutoDiscoverConnectorFactories() .WithAutoDiscoverDeviceDriverFactories() .WithConfigurationJsonFilePath("test-config.json"); + } - var sic = builder.Build(); - + private ConnectorController SetupController(SmartIotConnector sic, SmartIotConnectorBuilder builder) + { var service = new ConnectorService(sic, builder.ConnectorFactory); return new ConnectorController(sic, service); @@ -24,7 +26,9 @@ private ConnectorController SetupController() [Fact] public void Test_GetConnectors() { - var controller = SetupController(); + SmartIotConnectorBuilder builder = CreateBuilder(); + SmartIotConnector sic = builder.Build(); + var controller = SetupController(sic, builder); var list = controller.GetConnectors(); @@ -39,7 +43,9 @@ public void Test_GetConnectors() [Fact] public void Test_GetConnector_ok() { - var controller = SetupController(); + SmartIotConnectorBuilder builder = CreateBuilder(); + SmartIotConnector sic = builder.Build(); + var controller = SetupController(sic, builder); var r = controller.GetConnector(0); @@ -54,7 +60,9 @@ public void Test_GetConnector_ok() [Fact] public void Test_GetConnector_notfound() { - var controller = SetupController(); + SmartIotConnectorBuilder builder = CreateBuilder(); + SmartIotConnector sic = builder.Build(); + var controller = SetupController(sic, builder); var r = controller.GetConnector(1); @@ -64,7 +72,9 @@ public void Test_GetConnector_notfound() [Fact] public async Task Test_AddConnector() { - var controller = SetupController(); + SmartIotConnectorBuilder builder = CreateBuilder(); + SmartIotConnector sic = builder.Build(); + var controller = SetupController(sic, builder); var r = await controller.AddConnector("mock://"); @@ -76,10 +86,19 @@ public async Task Test_AddConnector() Assert.Equal("mock://", c.ConnectionString); } - [Fact] - public async Task Test_UpdateConnector() + [Theory] + [InlineData(true)] + [InlineData(false)] + public async Task Test_UpdateConnector(bool startConnector) { - var controller = SetupController(); + SmartIotConnectorBuilder builder = CreateBuilder(); + SmartIotConnector sic = builder.Build(); + var controller = SetupController(sic, builder); + + FakeConnector connector = (FakeConnector)sic.Connectors[0]; + + if (startConnector) + await sic.StartAsync(); var r = await controller.UpdateConnector(0, "mock://"); @@ -89,16 +108,35 @@ public async Task Test_UpdateConnector() Assert.Equal(0, c.Index); Assert.Equal("mock://", c.ConnectionString); + + if (startConnector) + Assert.True(connector.StopEvent.IsSet); + else + Assert.False(connector.StopEvent.IsSet); } - [Fact] - public async Task Test_RemoveConnector() + [Theory] + [InlineData(true)] + [InlineData(false)] + public async Task Test_RemoveConnector(bool startConnector) { - var controller = SetupController(); + SmartIotConnectorBuilder builder = CreateBuilder(); + SmartIotConnector sic = builder.Build(); + var controller = SetupController(sic, builder); + + FakeConnector connector = (FakeConnector)sic.Connectors[0]; + + if (startConnector) + await sic.StartAsync(); var r = await controller.RemoveConnector(0); Assert.IsType(r); Assert.Empty(controller.GetConnectors()); + + if (startConnector) + Assert.True(connector.StopEvent.IsSet); + else + Assert.False(connector.StopEvent.IsSet); } } \ No newline at end of file