Skip to content

Commit

Permalink
now checks shadow table if route element was created by gdb-integrator (
Browse files Browse the repository at this point in the history
  • Loading branch information
runeanielsen authored May 11, 2023
1 parent be21689 commit 38edb47
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 16 deletions.
2 changes: 2 additions & 0 deletions src/OpenFTTH.GDBIntegrator.GeoDatabase/IGeoDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ namespace OpenFTTH.GDBIntegrator.GeoDatabase
{
public interface IGeoDatabase
{
Task<bool> RouteNodeInShadowTableExists(Guid mrid);
Task<RouteNode> GetRouteNodeShadowTable(Guid mrid, bool includeDeleted = false);
Task<bool> RouteSegmentInShadowTableExists(Guid mrid);
Task<RouteSegment> GetRouteSegmentShadowTable(Guid mrid, bool includeDeleted = false);
Task<List<RouteNode>> GetIntersectingStartRouteNodes(RouteSegment routeSegment);
Task<List<RouteNode>> GetIntersectingStartRouteNodes(byte[] coord);
Expand Down
14 changes: 14 additions & 0 deletions src/OpenFTTH.GDBIntegrator.GeoDatabase/Postgres/Postgis.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ public async Task RollbackTransaction()
await DisposeConnection();
}

public async Task<bool> RouteNodeInShadowTableExists(Guid mrid)
{
var connection = GetNpgsqlConnection();
var query = @"SELECT exists(SELECT 1 FROM route_network_integrator.route_node WHERE mrid = @mrid)";
return await connection.QueryFirstAsync<bool>(query, new { mrid });
}

public async Task<RouteNode> GetRouteNodeShadowTable(Guid mrid, bool includeDeleted = false)
{
var connection = GetNpgsqlConnection();
Expand Down Expand Up @@ -146,6 +153,13 @@ naming_description AS namingDescription
return routeNodes.FirstOrDefault();
}

public async Task<bool> RouteSegmentInShadowTableExists(Guid mrid)
{
var connection = GetNpgsqlConnection();
var query = @"SELECT exists(SELECT 1 FROM route_network_integrator.route_segment WHERE mrid = @mrid)";
return await connection.QueryFirstAsync<bool>(query, new { mrid });
}

public async Task<RouteSegment> GetRouteSegmentShadowTable(Guid mrid, bool includeDeleted = false)
{
var connection = GetNpgsqlConnection();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,11 @@ public async Task<List<INotification>> CreateDigitizedEvent(RouteNode routeNode)
if (routeNode is null)
throw new ArgumentNullException($"Parameter {nameof(routeNode)} cannot be null");

if (IsCreatedByApplication(routeNode))
// If we find the route node is in the shadow table, it means that it was created by the application and we therefore do nothing.
if (await _geoDatabase.RouteNodeInShadowTableExists(routeNode.Mrid))
{
return new List<INotification> { new DoNothing($"{nameof(RouteNode)} with id: '{routeNode.Mrid}' was created by {routeNode.ApplicationName} therefore do nothing.") };
}

await _geoDatabase.InsertRouteNodeShadowTable(routeNode);

Expand Down Expand Up @@ -121,9 +124,6 @@ private async Task<bool> IsValidNodeUpdate(RouteNode shadowTableNode, RouteNode
private bool AlreadyUpdated(RouteNode routeNode, RouteNode routeNodeShadowTable)
=> routeNode.MarkAsDeleted == routeNodeShadowTable.MarkAsDeleted && routeNode.GetGeoJsonCoordinate() == routeNodeShadowTable.GetGeoJsonCoordinate();

private bool IsCreatedByApplication(RouteNode routeNode)
=> routeNode.ApplicationName == _applicationSettings.ApplicationName;

private bool IsModifiedDistanceLessThanTolerance(RouteNode shadowTableNode, RouteNode after)
{
var distance = after.GetPoint().Distance(shadowTableNode.GetPoint());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,11 @@ public async Task<IEnumerable<INotification>> CreateDigitizedEvent(RouteSegment
if (routeSegment is null)
throw new ArgumentNullException($"Parameter {nameof(routeSegment)} must not be null");

if (IsCreatedByApplication(routeSegment))
// If we find the route segment is in the shadow table, it means that it was created by the application and we therefore do nothing.
if (await _geoDatabase.RouteSegmentInShadowTableExists(routeSegment.Mrid))
{
return new List<INotification>();
}

// Update integrator "shadow table" with the used digitized segment
await _geoDatabase.InsertRouteSegmentShadowTable(routeSegment);
Expand Down Expand Up @@ -164,11 +167,6 @@ private bool AlreadyUpdated(RouteSegment routeSegment, RouteSegment shadowTableR
return routeSegment.MarkAsDeleted == shadowTableRouteSegment.MarkAsDeleted && routeSegment.GetGeoJsonCoordinate() == shadowTableRouteSegment.GetGeoJsonCoordinate();
}

private bool IsCreatedByApplication(RouteSegment routeSegment)
{
return routeSegment.ApplicationName == _applicationSettings.ApplicationName;
}

private RouteSegmentDeleted CreateRouteSegmentDeleted(RouteSegment routeSegment)
{
return new RouteSegmentDeleted
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,14 @@ public async Task CreateDigitizedEvent_ShouldReturnDoNothing_OnRouteNodeApplicat
var applicationSetting = A.Fake<IOptions<ApplicationSetting>>();
var geoDatabase = A.Fake<IGeoDatabase>();
var routeNodeValidator = A.Fake<IRouteNodeValidator>();
var routeNodeMrid = Guid.NewGuid();

A.CallTo(() => applicationSetting.Value).Returns(
new ApplicationSetting { ApplicationName = "GDB_INTEGRATOR" });
A.CallTo(() => geoDatabase.RouteNodeInShadowTableExists(routeNodeMrid))
.Returns(true);

var factory = new RouteNodeCommandFactory(applicationSetting, geoDatabase, routeNodeValidator);

var routeNode = new RouteNode(Guid.Empty, null, Guid.Empty, String.Empty, "GDB_INTEGRATOR");
var routeNode = new RouteNode(routeNodeMrid, null, Guid.Empty, String.Empty, "GDB_INTEGRATOR");
var result = (DoNothing)((await factory.CreateDigitizedEvent(routeNode)).First());

result.Should().BeOfType<DoNothing>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ public async Task CreateDigitizedEvent_ShouldReturnNull_OnRouteSegmentApplicatio
var routeSegmentValidator = A.Fake<IRouteSegmentValidator>();
var geoDatabase = A.Fake<IGeoDatabase>();
var routeNodeFactory = A.Fake<IRouteNodeFactory>();
var routeSegment = new RouteSegment { ApplicationName = "GDB_INTEGRATOR" };
var routeSegment = new RouteSegment { Mrid = Guid.NewGuid(), ApplicationName = "GDB_INTEGRATOR" };

A.CallTo(() => applicationSettings.Value)
.Returns(new ApplicationSetting { ApplicationName = "GDB_INTEGRATOR" });
A.CallTo(() => geoDatabase.RouteSegmentInShadowTableExists(routeSegment.Mrid))
.Returns(true);

var factory = new RouteSegmentCommandFactory(applicationSettings, routeSegmentValidator, geoDatabase, routeNodeFactory);

Expand Down

0 comments on commit 38edb47

Please sign in to comment.