-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Will not write tests for error handling, because there is no nice way to make them run fast (delay 1s).
- Loading branch information
Showing
2 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
56
Specifications/for_Connector/when_running/and_data_is_read.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |