Skip to content

Commit

Permalink
add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Scretch9 committed Nov 27, 2023
1 parent 3669d46 commit ab8285d
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 27 deletions.
121 changes: 96 additions & 25 deletions src/FieldElementSubsystems.Test/Point/PointTest.cs
Original file line number Diff line number Diff line change
@@ -1,56 +1,127 @@
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;
namespace FieldElementSubsystems.Test;

public class PointTest
{
private IDictionary<string, string?> _testSettings = new Dictionary<string, string?> {
private EulynxLive.Point.Point CreateDefaultPoint(IPointToInterlockingConnection? connection = null) =>
new EulynxLive.Point.Point(_logger, _configuration, connection ?? Mock.Of<IPointToInterlockingConnection>());

private Mock<IPointToInterlockingConnection> CreateDefaultMockConnection() {
var mockConnection = new Mock<IPointToInterlockingConnection>();
mockConnection
.Setup(m => m.SendPointPosition(
It.IsAny<IPointToInterlockingConnection.PointState>()))
.Returns(Task.FromResult(0));
mockConnection
.Setup(m => m.InitializeConnection(
It.IsAny<IPointToInterlockingConnection.PointState>()))
.Returns(Task.FromResult(true));
return mockConnection;
}

private static IDictionary<string, string?> _testSettings = new Dictionary<string, string?> {
{"PointSettings:LocalId", "99W1" },
{"PointSettings:LocalRastaId", "100" },
{"PointSettings:RemoteId", "INTERLOCKING" },
{"PointSettings:RemoteEndpoint", "http://localhost:50051" },
{"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<ILogger<EulynxLive.Point.Point>>();

var point = new EulynxLive.Point.Point(Mock.Of<ILogger<EulynxLive.Point.Point>>(), configuration, Mock.Of<IPointToInterlockingConnection>());
[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<ILogger<EulynxLive.Point.Point>>();
Assert.Equal(IPointToInterlockingConnection.PointPosition.NO_ENDPOSITION, point.PointState.PointPosition);
}

var point = new EulynxLive.Point.Point(logger, configuration, Mock.Of<IPointToInterlockingConnection>());
var mockConnection = new Mock<IPointToInterlockingConnection>();
[Fact]
public async void Test_Turn_Left()
{
// Arrange
var point = CreateDefaultPoint();
var mockConnection = CreateDefaultMockConnection();

var finished = false;
mockConnection
.Setup(m => m.SendPointPosition(
It.IsAny<IPointToInterlockingConnection.PointState>()))
.Returns(Task.FromResult(0));
.SetupSequence(m => m.ReceivePointPosition())
.Returns(Task.FromResult<IPointToInterlockingConnection.PointPosition?>(IPointToInterlockingConnection.PointPosition.LEFT))
.Returns(() =>
{
finished = true;
return Task.FromResult<IPointToInterlockingConnection.PointPosition?>(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<IPointToInterlockingConnection.PointState>()));
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<IPointToInterlockingConnection.PointState>()))
.Returns(Task.FromResult(true));
.SetupSequence(m => m.ReceivePointPosition())
.Returns(Task.FromResult<IPointToInterlockingConnection.PointPosition?>(IPointToInterlockingConnection.PointPosition.RIGHT))
.Returns(() =>
{
finished = true;
return Task.FromResult<IPointToInterlockingConnection.PointPosition?>(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<IPointToInterlockingConnection.PointState>()));
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
Expand All @@ -64,7 +135,7 @@ public async void Test_Turnover()
return Task.FromResult<IPointToInterlockingConnection.PointPosition?>(null);
});

point = new EulynxLive.Point.Point(logger, configuration, mockConnection.Object);
point = CreateDefaultPoint(mockConnection.Object);

// Act
await point.StartAsync(CancellationToken.None);
Expand Down
4 changes: 2 additions & 2 deletions src/Point/Point.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class Point : BackgroundService
{
public bool AllPointMachinesCrucial { get; }

private readonly ILogger<Point> _logger;
private readonly ILogger _logger;
private readonly List<WebSocket> _webSockets;
private IPointToInterlockingConnection _connection;
private readonly Random _random;
Expand All @@ -25,7 +25,7 @@ public class Point : BackgroundService
private readonly PointState _pointState;
public PointState PointState { get { return _pointState; } }

public Point(ILogger<Point> logger, IConfiguration configuration, IPointToInterlockingConnection connection)
public Point(ILogger logger, IConfiguration configuration, IPointToInterlockingConnection connection)
{
_logger = logger;

Expand Down

0 comments on commit ab8285d

Please sign in to comment.