From ab8285dbe5233f2916156b4cdd6d510ff7f471bb Mon Sep 17 00:00:00 2001 From: Benedikt Schenkel Date: Mon, 27 Nov 2023 09:00:53 +0100 Subject: [PATCH] add more tests --- .../Point/PointTest.cs | 121 ++++++++++++++---- src/Point/Point.cs | 4 +- 2 files changed, 98 insertions(+), 27 deletions(-) diff --git a/src/FieldElementSubsystems.Test/Point/PointTest.cs b/src/FieldElementSubsystems.Test/Point/PointTest.cs index 512ae49..e955da0 100644 --- a/src/FieldElementSubsystems.Test/Point/PointTest.cs +++ b/src/FieldElementSubsystems.Test/Point/PointTest.cs @@ -1,7 +1,6 @@ -using Castle.Core.Logging; using EulynxLive.Messages.Baseline4R1; using EulynxLive.Messages.IPointToInterlockingConnection; -using EulynxLive.Point; +using Point = EulynxLive.Point.Point; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging; using Moq; @@ -9,7 +8,23 @@ namespace FieldElementSubsystems.Test; public class PointTest { - private IDictionary _testSettings = new Dictionary { + private EulynxLive.Point.Point CreateDefaultPoint(IPointToInterlockingConnection? connection = null) => + new EulynxLive.Point.Point(_logger, _configuration, connection ?? Mock.Of()); + + private Mock CreateDefaultMockConnection() { + var mockConnection = new Mock(); + mockConnection + .Setup(m => m.SendPointPosition( + It.IsAny())) + .Returns(Task.FromResult(0)); + mockConnection + .Setup(m => m.InitializeConnection( + It.IsAny())) + .Returns(Task.FromResult(true)); + return mockConnection; + } + + private static IDictionary _testSettings = new Dictionary { {"PointSettings:LocalId", "99W1" }, {"PointSettings:LocalRastaId", "100" }, {"PointSettings:RemoteId", "INTERLOCKING" }, @@ -17,40 +32,96 @@ public class PointTest {"PointSettings:AllPointMachinesCrucial", "true" }, {"PointSettings:SimulateRandomTimeouts", "false" }, }; - - [Fact] - public void PointShouldParseConfiguration() - { - var configuration = new ConfigurationBuilder() + private IConfiguration _configuration = new ConfigurationBuilder() .AddInMemoryCollection(_testSettings) .Build(); + private ILogger _logger = Mock.Of>(); - var point = new EulynxLive.Point.Point(Mock.Of>(), configuration, Mock.Of()); + [Fact] + public void Test_Parse_Configuration() + { + var point = CreateDefaultPoint(); Assert.True(point.AllPointMachinesCrucial); } [Fact] - public async void Test_Turnover() + public async void Test_Default_Position() { - - // Arrange - var configuration = new ConfigurationBuilder() - .AddInMemoryCollection(_testSettings) - .Build(); + var point = CreateDefaultPoint(); + await point.StartAsync(CancellationToken.None); - var logger = Mock.Of>(); + Assert.Equal(IPointToInterlockingConnection.PointPosition.NO_ENDPOSITION, point.PointState.PointPosition); + } - var point = new EulynxLive.Point.Point(logger, configuration, Mock.Of()); - var mockConnection = new Mock(); + [Fact] + public async void Test_Turn_Left() + { + // Arrange + var point = CreateDefaultPoint(); + var mockConnection = CreateDefaultMockConnection(); + + var finished = false; mockConnection - .Setup(m => m.SendPointPosition( - It.IsAny())) - .Returns(Task.FromResult(0)); + .SetupSequence(m => m.ReceivePointPosition()) + .Returns(Task.FromResult(IPointToInterlockingConnection.PointPosition.LEFT)) + .Returns(() => + { + finished = true; + return Task.FromResult(null); + }); + + point = CreateDefaultPoint(mockConnection.Object); + + // Act + await point.StartAsync(CancellationToken.None); + while (!finished) + { + await Task.Delay(1000); + } + + // Assert + mockConnection.Verify(v => v.InitializeConnection(It.IsAny())); + Assert.Equal(IPointToInterlockingConnection.PointPosition.LEFT, point.PointState.PointPosition); + } + + [Fact] + public async void Test_Turn_Right() + { + // Arrange + var point = CreateDefaultPoint(); + var mockConnection = CreateDefaultMockConnection(); + + var finished = false; mockConnection - .Setup(m => m.InitializeConnection( - It.IsAny())) - .Returns(Task.FromResult(true)); + .SetupSequence(m => m.ReceivePointPosition()) + .Returns(Task.FromResult(IPointToInterlockingConnection.PointPosition.RIGHT)) + .Returns(() => + { + finished = true; + return Task.FromResult(null); + }); + + point = CreateDefaultPoint(mockConnection.Object); + + // Act + await point.StartAsync(CancellationToken.None); + while (!finished) + { + await Task.Delay(1000); + } + + // Assert + mockConnection.Verify(v => v.InitializeConnection(It.IsAny())); + Assert.Equal(IPointToInterlockingConnection.PointPosition.RIGHT, point.PointState.PointPosition); + } + + [Fact] + public async void Test_Turnover() + { + // Arrange + var point = CreateDefaultPoint(); + var mockConnection = CreateDefaultMockConnection(); var finished = false; mockConnection @@ -64,7 +135,7 @@ public async void Test_Turnover() return Task.FromResult(null); }); - point = new EulynxLive.Point.Point(logger, configuration, mockConnection.Object); + point = CreateDefaultPoint(mockConnection.Object); // Act await point.StartAsync(CancellationToken.None); diff --git a/src/Point/Point.cs b/src/Point/Point.cs index 0ef1131..bbbc7a5 100644 --- a/src/Point/Point.cs +++ b/src/Point/Point.cs @@ -15,7 +15,7 @@ public class Point : BackgroundService { public bool AllPointMachinesCrucial { get; } - private readonly ILogger _logger; + private readonly ILogger _logger; private readonly List _webSockets; private IPointToInterlockingConnection _connection; private readonly Random _random; @@ -25,7 +25,7 @@ public class Point : BackgroundService private readonly PointState _pointState; public PointState PointState { get { return _pointState; } } - public Point(ILogger logger, IConfiguration configuration, IPointToInterlockingConnection connection) + public Point(ILogger logger, IConfiguration configuration, IPointToInterlockingConnection connection) { _logger = logger;