Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prerelease 2023-12-18 - fix Rest API #12

Merged
merged 2 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Core/SmartIOT.Connector.Core/SmartIotConnector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,13 +154,16 @@ public async Task<bool> RemoveConnectorAsync(IConnector connector)

public async Task<bool> 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;
}
Expand Down
4 changes: 4 additions & 0 deletions Tests/SmartIOT.Connector.Mocks/FakeConnector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public class FakeConnector : AbstractConnector
public IList<DeviceStatusEvent> DeviceStatusEvents { get; } = new List<DeviceStatusEvent>();
public IList<ExceptionEventArgs> ExceptionEvents { get; } = new List<ExceptionEventArgs>();
public IServiceProvider? ServiceProvider { get; }
public ManualResetEventSlim StartEvent { get; } = new();
public ManualResetEventSlim StopEvent { get; } = new();

public FakeConnector() : base("fake://")
{
Expand Down Expand Up @@ -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;
}

Expand Down
66 changes: 52 additions & 14 deletions Tests/SmartIOT.Connector.RestApi.Tests/ConnectorControllerTests.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
using Microsoft.AspNetCore.Mvc;
using SmartIOT.Connector.Core;
using SmartIOT.Connector.Mocks;
using SmartIOT.Connector.RestApi.Controllers.V1;
using SmartIOT.Connector.RestApi.Services;

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);
Expand All @@ -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();

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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://");

Expand All @@ -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://");

Expand All @@ -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<OkResult>(r);
Assert.Empty(controller.GetConnectors());

if (startConnector)
Assert.True(connector.StopEvent.IsSet);
else
Assert.False(connector.StopEvent.IsSet);
}
}
Loading