Skip to content

Commit

Permalink
Add tests for Connector
Browse files Browse the repository at this point in the history
Will not write tests for error handling, because there is no nice way to make them run fast (delay 1s).
  • Loading branch information
jakhog committed Jun 17, 2024
1 parent d772900 commit 591263e
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
37 changes: 37 additions & 0 deletions Specifications/for_Connector/given/a_connector.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Machine.Specifications;
using Moq;
using RaaLabs.Edge.Connectors.OPCUA.Events;
using Serilog;

namespace RaaLabs.Edge.Connectors.OPCUA.for_Connector.given;

public class a_connector
{
protected static Mock<ICreateSessions> sessions;
protected static Mock<IRetrieveData> retriever;
protected static Mock<ICreateDatapointsFromDataValues> datapoints;

protected static Connector connector;

Establish context = () =>
{
sessions = new();

retriever = new();

datapoints = new();

connector = new(sessions.Object, retriever.Object, datapoints.Object, Mock.Of<ILogger>());

var collected = sent_datapoints = [];
connector.SendDatapoint += _ =>
{
collected.Add(_);
return Task.CompletedTask;
};
};

protected static List<OpcuaDatapointOutput> sent_datapoints;
}
56 changes: 56 additions & 0 deletions Specifications/for_Connector/when_running/and_data_is_read.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Machine.Specifications;
using Moq;
using Opc.Ua.Client;
using RaaLabs.Edge.Connectors.OPCUA.Events;
using It = Machine.Specifications.It;

namespace RaaLabs.Edge.Connectors.OPCUA.for_Connector.when_running;

public class and_data_is_read : given.a_connector
{
static NodeValue value_to_recieve;
static OpcuaDatapointOutput output_to_produce;
static ISession created_session;
static Func<NodeValue, Task> send_events_callback;

Establish context = () =>
{
value_to_recieve = new(new(15), new("Hello"));
output_to_produce = new()
{
Source = "testing",
Tag = "moar tests",
Value = "yeah"
};
created_session = Mock.Of<ISession>();

sessions
.Setup(_ => _.ConnectToServer(Moq.It.IsAny<CancellationToken>()))
.ReturnsAsync(created_session);

var reading = new TaskCompletionSource();
retriever
.Setup(_ => _.ReadDataForever(created_session, Moq.It.IsAny<Func<NodeValue, Task>>(), Moq.It.IsAny<CancellationToken>()))
.Callback<ISession, Func<NodeValue, Task>, CancellationToken>((_, callback, _) => send_events_callback = callback)
.Returns(reading.Task);

datapoints
.Setup(_ => _.CreateDatapointFrom(value_to_recieve))
.Returns(output_to_produce);
};

Because of = async () =>
{
_ = connector.Run();
await Task.Delay(20);
await send_events_callback(value_to_recieve);
};

It should_connect_to_the_server = () => sessions.Verify(_ => _.ConnectToServer(CancellationToken.None), Times.Once);
It should_read_data_from_the_connection = () => retriever.Verify(_ => _.ReadDataForever(created_session, Moq.It.IsAny<Func<NodeValue, Task>>(), CancellationToken.None), Times.Once);
It should_convert_data_using_datapoints = () => datapoints.Verify(_ => _.CreateDatapointFrom(value_to_recieve), Times.Once);
It should_emit_the_converted_data = () => sent_datapoints.ShouldContainOnly(output_to_produce);
}

0 comments on commit 591263e

Please sign in to comment.