feat: add JoeAppEngine OPC UA nodes, fix DCL auto-reconnect and quality push

- Add JoeAppEngine folder to OPC UA nodes.json (BTCS, AlarmCntsBySeverity, Scheduler/ScanTime)
- Fix DataConnectionActor: capture Self in PreStart for use from non-actor threads,
  preventing Self.Tell failure in Disconnected event handler
- Implement InstanceActor.HandleConnectionQualityChanged to mark attributes Bad on disconnect
- Fix LmxFakeProxy TagMapper to serialize arrays as JSON instead of "System.Int32[]"
- Allow DataType and DataSourceReference updates in TemplateService.UpdateAttributeAsync
- Update test_infra_opcua.md with JoeAppEngine documentation
This commit is contained in:
Joseph Doherty
2026-03-19 13:27:54 -04:00
parent ffdda51990
commit 7740a3bcf9
70 changed files with 2684 additions and 541 deletions

View File

@@ -3,6 +3,7 @@ using NSubstitute;
using NSubstitute.ExceptionExtensions;
using ScadaLink.Commons.Entities.Notifications;
using ScadaLink.Commons.Interfaces.Repositories;
using ScadaLink.StoreAndForward;
namespace ScadaLink.NotificationService.Tests;
@@ -145,4 +146,50 @@ public class NotificationDeliveryServiceTests
Assert.False(result.Success);
Assert.Contains("store-and-forward not available", result.ErrorMessage);
}
[Fact]
public async Task Send_UsesBccDelivery_AllRecipientsInBcc()
{
SetupHappyPath();
IEnumerable<string>? capturedBcc = null;
_smtpClient.SendAsync(
Arg.Any<string>(),
Arg.Do<IEnumerable<string>>(bcc => capturedBcc = bcc),
Arg.Any<string>(),
Arg.Any<string>(),
Arg.Any<CancellationToken>())
.Returns(Task.CompletedTask);
var service = CreateService();
await service.SendAsync("ops-team", "Alert", "Body");
Assert.NotNull(capturedBcc);
var bccList = capturedBcc!.ToList();
Assert.Equal(2, bccList.Count);
Assert.Contains("alice@example.com", bccList);
Assert.Contains("bob@example.com", bccList);
}
[Fact]
public async Task Send_TransientError_WithStoreAndForward_BuffersMessage()
{
SetupHappyPath();
_smtpClient.SendAsync(Arg.Any<string>(), Arg.Any<IEnumerable<string>>(), Arg.Any<string>(), Arg.Any<string>(), Arg.Any<CancellationToken>())
.Throws(new TimeoutException("Connection timed out"));
var dbName = $"file:sf_test_{Guid.NewGuid():N}?mode=memory&cache=shared";
var storage = new StoreAndForward.StoreAndForwardStorage(
$"Data Source={dbName}", NullLogger<StoreAndForward.StoreAndForwardStorage>.Instance);
await storage.InitializeAsync();
var sfOptions = new StoreAndForward.StoreAndForwardOptions();
var sfService = new StoreAndForward.StoreAndForwardService(
storage, sfOptions, NullLogger<StoreAndForward.StoreAndForwardService>.Instance);
var service = CreateService(sf: sfService);
var result = await service.SendAsync("ops-team", "Alert", "Body");
Assert.True(result.Success);
Assert.True(result.WasBuffered);
}
}

View File

@@ -0,0 +1,139 @@
using System.Net;
using System.Text.Json;
using Microsoft.Extensions.Logging.Abstractions;
using NSubstitute;
namespace ScadaLink.NotificationService.Tests;
/// <summary>
/// Tests for OAuth2 token flow — token acquisition, caching, and credential parsing.
/// </summary>
public class OAuth2TokenServiceTests
{
private static HttpClient CreateMockHttpClient(HttpStatusCode statusCode, string responseJson)
{
var handler = new MockHttpMessageHandler(statusCode, responseJson);
return new HttpClient(handler);
}
private static IHttpClientFactory CreateMockFactory(HttpClient client)
{
var factory = Substitute.For<IHttpClientFactory>();
factory.CreateClient(Arg.Any<string>()).Returns(client);
return factory;
}
[Fact]
public async Task GetTokenAsync_ReturnsAccessToken_FromTokenEndpoint()
{
var tokenResponse = JsonSerializer.Serialize(new
{
access_token = "mock-access-token-12345",
expires_in = 3600,
token_type = "Bearer"
});
var client = CreateMockHttpClient(HttpStatusCode.OK, tokenResponse);
var factory = CreateMockFactory(client);
var service = new OAuth2TokenService(factory, NullLogger<OAuth2TokenService>.Instance);
var token = await service.GetTokenAsync("tenant123:client456:secret789");
Assert.Equal("mock-access-token-12345", token);
}
[Fact]
public async Task GetTokenAsync_CachesToken_OnSubsequentCalls()
{
var tokenResponse = JsonSerializer.Serialize(new
{
access_token = "cached-token",
expires_in = 3600,
token_type = "Bearer"
});
var handler = new CountingHttpMessageHandler(HttpStatusCode.OK, tokenResponse);
var client = new HttpClient(handler);
var factory = CreateMockFactory(client);
var service = new OAuth2TokenService(factory, NullLogger<OAuth2TokenService>.Instance);
var token1 = await service.GetTokenAsync("tenant:client:secret");
var token2 = await service.GetTokenAsync("tenant:client:secret");
Assert.Equal("cached-token", token1);
Assert.Equal("cached-token", token2);
Assert.Equal(1, handler.CallCount); // Only one HTTP call should be made
}
[Fact]
public async Task GetTokenAsync_InvalidCredentialFormat_ThrowsInvalidOperationException()
{
var client = CreateMockHttpClient(HttpStatusCode.OK, "{}");
var factory = CreateMockFactory(client);
var service = new OAuth2TokenService(factory, NullLogger<OAuth2TokenService>.Instance);
await Assert.ThrowsAsync<InvalidOperationException>(
() => service.GetTokenAsync("invalid-no-colons"));
}
[Fact]
public async Task GetTokenAsync_HttpFailure_ThrowsHttpRequestException()
{
var client = CreateMockHttpClient(HttpStatusCode.Unauthorized, "Unauthorized");
var factory = CreateMockFactory(client);
var service = new OAuth2TokenService(factory, NullLogger<OAuth2TokenService>.Instance);
await Assert.ThrowsAsync<HttpRequestException>(
() => service.GetTokenAsync("tenant:client:secret"));
}
/// <summary>
/// Simple mock HTTP handler that returns a fixed response.
/// </summary>
private class MockHttpMessageHandler : HttpMessageHandler
{
private readonly HttpStatusCode _statusCode;
private readonly string _responseContent;
public MockHttpMessageHandler(HttpStatusCode statusCode, string responseContent)
{
_statusCode = statusCode;
_responseContent = responseContent;
}
protected override Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request, CancellationToken cancellationToken)
{
return Task.FromResult(new HttpResponseMessage(_statusCode)
{
Content = new StringContent(_responseContent)
});
}
}
/// <summary>
/// Mock HTTP handler that counts invocations.
/// </summary>
private class CountingHttpMessageHandler : HttpMessageHandler
{
private readonly HttpStatusCode _statusCode;
private readonly string _responseContent;
public int CallCount { get; private set; }
public CountingHttpMessageHandler(HttpStatusCode statusCode, string responseContent)
{
_statusCode = statusCode;
_responseContent = responseContent;
}
protected override Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request, CancellationToken cancellationToken)
{
CallCount++;
return Task.FromResult(new HttpResponseMessage(_statusCode)
{
Content = new StringContent(_responseContent)
});
}
}
}

View File

@@ -23,6 +23,7 @@
<ItemGroup>
<ProjectReference Include="../../src/ScadaLink.NotificationService/ScadaLink.NotificationService.csproj" />
<ProjectReference Include="../../src/ScadaLink.Commons/ScadaLink.Commons.csproj" />
<ProjectReference Include="../../src/ScadaLink.StoreAndForward/ScadaLink.StoreAndForward.csproj" />
</ItemGroup>
</Project>