Skip to content

Commit

Permalink
implements routenode moved without connecting segments
Browse files Browse the repository at this point in the history
  • Loading branch information
runeanielsen committed Aug 4, 2020
1 parent 8482b89 commit 7e45205
Show file tree
Hide file tree
Showing 6 changed files with 209 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;

namespace OpenFTTH.GDBIntegrator.Integrator.EventMessages
{
public class RouteNodeGeometryModified
{
public readonly string EventType = nameof(RouteSegmentGeometryModified);
public readonly string EventTs = DateTime.UtcNow.ToString("o");
public readonly Guid EventId = Guid.NewGuid();
public Guid CmdId { get; }
public string CmdType { get; }
public Guid NodeId { get; }
public string Geometry { get; }

public RouteNodeGeometryModified(Guid cmdId, Guid nodeId, string cmdType, string geometry)
{
CmdId = cmdId;
NodeId = nodeId;
CmdType = cmdType;
Geometry = geometry;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,22 @@ public async Task<INotification> CreateUpdatedEvent(RouteNode before, RouteNode
if (AlreadyUpdated(after, shadowTableNode))
return new DoNothing($"{nameof(RouteNode)} with id: '{after.Mrid}' was already updated therefore do nothing.");

var shadowTableNodeIntersectingSegments = await _geoDatabase.GetIntersectingRouteSegments(shadowTableNode);
if (shadowTableNodeIntersectingSegments.Count > 0)
return new RollbackInvalidRouteNodeOperation(before);

await _geoDatabase.UpdateRouteNodeShadowTable(after);

var intersectingRouteSegments = await _geoDatabase.GetIntersectingRouteSegments(after);

if (intersectingRouteSegments.Count > 0)
var intersectingRouteNodes = await _geoDatabase.GetIntersectingRouteNodes(after);
if (intersectingRouteSegments.Count > 0 || intersectingRouteNodes.Count > 0)
return new RollbackInvalidRouteNodeOperation(before);

var cmdId = Guid.NewGuid();
if (after.MarkAsDeleted)
return new RouteNodeDeleted { CmdId = cmdId, RouteNode = after };

return new DoNothing($"{nameof(RouteNode)} with id: '{after.Mrid}' found not suitable action for {nameof(CreateUpdatedEvent)}.");
return new RouteNodeLocationChanged { CmdId = cmdId, RouteNode = after };
}

public async Task<INotification> CreateDigitizedEvent(RouteNode routeNode)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using OpenFTTH.GDBIntegrator.RouteNetwork;
using OpenFTTH.GDBIntegrator.Config;
using OpenFTTH.GDBIntegrator.Producer;
using MediatR;
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

namespace OpenFTTH.GDBIntegrator.Integrator.Notifications
{
public class RouteNodeLocationChanged : INotification
{
public RouteNode RouteNode { get; set; }
public Guid CmdId { get; set; }
}

public class RouteNodeLocationChangedHandler : INotificationHandler<RouteNodeLocationChanged>
{
private readonly ILogger<RouteNodeAddedHandler> _logger;
private readonly KafkaSetting _kafkaSettings;
private readonly IProducer _producer;

public RouteNodeLocationChangedHandler(
ILogger<RouteNodeAddedHandler> logger,
IOptions<KafkaSetting> kafkaSettings,
IProducer producer)
{
_logger = logger;
_kafkaSettings = kafkaSettings.Value;
_producer = producer;
}

public async Task Handle(RouteNodeLocationChanged request, CancellationToken token)
{
_logger.LogInformation($"Sending {nameof(RouteNodeLocationChanged)} with mrid '{request.RouteNode.Mrid}' to producer");

await _producer.Produce(_kafkaSettings.EventRouteNetworkTopicName,
new EventMessages.RouteNodeGeometryModified
(
request.CmdId,
request.RouteNode.Mrid,
nameof(RouteNodeLocationChanged),
request.RouteNode.GetGeoJsonCoordinate()
));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
using OpenFTTH.GDBIntegrator.RouteNetwork;
using OpenFTTH.GDBIntegrator.Config;
using OpenFTTH.GDBIntegrator.Producer;
using OpenFTTH.GDBIntegrator.Integrator;
using MediatR;
using System;
using System.Threading;
using System.Threading.Tasks;
using System.Collections.Generic;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;
using Xunit;
using FluentAssertions;
using FluentAssertions.Execution;
using OpenFTTH.GDBIntegrator.Integrator.EventMessages;

namespace OpenFTTH.GDBIntegrator.Integrator.Tests.EventMessages
{
public class RouteNodeGeometryModifiedTest
{
[Fact]
public void RouteNodeGeometryModified_ShouldSetInitialValues_OnConstruction()
{
var cmdId = Guid.NewGuid();
var nodeId = Guid.NewGuid();
var geometry = "[565931.444690517626197297.75114815]";
var cmdType = "RouteSegmentLocationChanged";

var routeNodeGeometryModified = new RouteNodeGeometryModified
(
cmdId,
nodeId,
cmdType,
geometry
);

using (new AssertionScope())
{
routeNodeGeometryModified.Geometry.Should().Be(geometry);
routeNodeGeometryModified.EventId.Should().NotBeEmpty();
routeNodeGeometryModified.NodeId.Should().Be(nodeId);
routeNodeGeometryModified.CmdId.Should().Be(cmdId);
routeNodeGeometryModified.EventType.Should().Be(nameof(RouteSegmentGeometryModified));
routeNodeGeometryModified.EventTs.Should().NotBeEmpty();
routeNodeGeometryModified.CmdType.Should().Be(cmdType);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,30 @@ public async Task CreateUpdatedEvent_ShouldReturnRollbackInvalidRouteNodeOperati
result.Should().BeEquivalentTo(expected);
}

[Fact]
public async Task CreateUpdatedEvent_ShouldReturnRollbackInvalidRouteNodeOperation_OnRouteNodeIntersectingWithOtherRouteNodes()
{
var applicationSetting = A.Fake<IOptions<ApplicationSetting>>();
var geoDatabase = A.Fake<IGeoDatabase>();
var beforeNode = A.Fake<RouteNode>();
var afterNode = A.Fake<RouteNode>();

A.CallTo(() => afterNode.MarkAsDeleted).Returns(true);

A.CallTo(() => geoDatabase.GetIntersectingRouteSegments(afterNode))
.Returns(new List<RouteSegment>());

A.CallTo(() => geoDatabase.GetIntersectingRouteNodes(afterNode))
.Returns(new List<RouteNode> { A.Fake<RouteNode>() });

var factory = new RouteNodeEventFactory(applicationSetting, geoDatabase);
var result = await factory.CreateUpdatedEvent(beforeNode, afterNode);

var expected = new RollbackInvalidRouteNodeOperation(beforeNode);

result.Should().BeEquivalentTo(expected);
}

[Fact]
public async Task CreateUpdatedEvent_ShouldReturnDoNothing_OnRouteNodeMarkedAsDeletedAndCoordBeingTheSame()
{
Expand Down Expand Up @@ -264,5 +288,72 @@ public async Task CreateUpdatedEvent_ShouldReturnDoNothing_OnShadowTableRouteNod

result.Should().BeOfType(typeof(DoNothing));
}

[Fact]
public async Task CreateUpdatedEvent_ShouldReturnRollbackRouteNode_OnShadowTableRouteNodeIntersectingWithSegment()
{
var applicationSetting = A.Fake<IOptions<ApplicationSetting>>();
var geoDatabase = A.Fake<IGeoDatabase>();
var beforeNode = A.Fake<RouteNode>();
var afterNode = A.Fake<RouteNode>();
var shadowTableRouteNode = A.Fake<RouteNode>();

A.CallTo(() => afterNode.Mrid).Returns(Guid.NewGuid());
A.CallTo(() => geoDatabase.GetRouteNodeShadowTable(afterNode.Mrid)).Returns(shadowTableRouteNode);
A.CallTo(() => geoDatabase.GetIntersectingRouteSegments(shadowTableRouteNode))
.Returns(new List<RouteSegment> { A.Fake<RouteSegment>() });

A.CallTo(() => afterNode.GetGeoJsonCoordinate())
.Returns("[665931.4446905176,7197297.75114815]");
A.CallTo(() => afterNode.MarkAsDeleted).Returns(false);

A.CallTo(() => shadowTableRouteNode.GetGeoJsonCoordinate())
.Returns("[565931.4446905176,6197297.75114815]");
A.CallTo(() => shadowTableRouteNode.MarkAsDeleted).Returns(false);

var factory = new RouteNodeEventFactory(applicationSetting, geoDatabase);

var result = (RollbackInvalidRouteNodeOperation)(await factory.CreateUpdatedEvent(beforeNode, afterNode));

using (var scope = new AssertionScope())
{
result.Should().BeOfType(typeof(RollbackInvalidRouteNodeOperation));
result.RollbackToNode.Should().Be(beforeNode);
}
}

[Fact]
public async Task CreateUpdatedEvent_ShouldReturnRouteNodeLocationChanged_OnRouteNodeChangedWithNoChecksFailing()
{
var applicationSetting = A.Fake<IOptions<ApplicationSetting>>();
var geoDatabase = A.Fake<IGeoDatabase>();
var beforeNode = A.Fake<RouteNode>();
var afterNode = A.Fake<RouteNode>();
var shadowTableRouteNode = A.Fake<RouteNode>();

A.CallTo(() => afterNode.Mrid).Returns(Guid.NewGuid());
A.CallTo(() => geoDatabase.GetRouteNodeShadowTable(afterNode.Mrid)).Returns(shadowTableRouteNode);
A.CallTo(() => geoDatabase.GetIntersectingRouteSegments(shadowTableRouteNode))
.Returns(new List<RouteSegment>());

A.CallTo(() => afterNode.GetGeoJsonCoordinate())
.Returns("[665931.4446905176,7197297.75114815]");
A.CallTo(() => afterNode.MarkAsDeleted).Returns(false);

A.CallTo(() => shadowTableRouteNode.GetGeoJsonCoordinate())
.Returns("[565931.4446905176,6197297.75114815]");
A.CallTo(() => shadowTableRouteNode.MarkAsDeleted).Returns(false);

var factory = new RouteNodeEventFactory(applicationSetting, geoDatabase);

var result = (RouteNodeLocationChanged)(await factory.CreateUpdatedEvent(beforeNode, afterNode));

using (var scope = new AssertionScope())
{
result.Should().BeOfType(typeof(RouteNodeLocationChanged));
result.RouteNode.Should().Be(afterNode);
result.CmdId.Should().NotBeEmpty();
}
}
}
}

0 comments on commit 7e45205

Please sign in to comment.