-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #119 from AndyTWF/active-runway-validation
Active runway validation
- Loading branch information
Showing
9 changed files
with
272 additions
and
3 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
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,36 @@ | ||
using System.Linq; | ||
using Compiler.Argument; | ||
using Compiler.Error; | ||
using Compiler.Event; | ||
using Compiler.Model; | ||
|
||
namespace Compiler.Validate | ||
{ | ||
public class AllActiveRunwaysMustBeUnique: IValidationRule | ||
{ | ||
public void Validate(SectorElementCollection sectorElements, CompilerArguments args, IEventLogger events) | ||
{ | ||
var duplicates = sectorElements.ActiveRunways.GroupBy( | ||
runway => runway | ||
) | ||
.Where(g => g.Count() > 1) | ||
.ToList(); | ||
|
||
if (!duplicates.Any()) | ||
{ | ||
return; | ||
} | ||
|
||
foreach (var duplicate in duplicates) | ||
{ | ||
ActiveRunway runway = duplicate.First(); | ||
events.AddEvent( | ||
new ValidationRuleFailure( | ||
$"Duplicate ACTIVE_RUNWAY {runway.Airfield}:{runway.Identifier}:{runway.Mode}", | ||
runway | ||
) | ||
); | ||
} | ||
} | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/Compiler/Validate/AllActiveRunwaysMustReferenceARunway.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,42 @@ | ||
using System.Linq; | ||
using Compiler.Argument; | ||
using Compiler.Error; | ||
using Compiler.Event; | ||
using Compiler.Model; | ||
|
||
namespace Compiler.Validate | ||
{ | ||
public class AllActiveRunwaysMustReferenceARunway: IValidationRule | ||
{ | ||
public void Validate(SectorElementCollection sectorElements, CompilerArguments args, IEventLogger events) | ||
{ | ||
var missingRunways = sectorElements.ActiveRunways | ||
.Where( | ||
activeRunway => !sectorElements.Runways.Exists(runway => IsSameRunway(activeRunway, runway)) | ||
) | ||
.ToList(); | ||
|
||
if (!missingRunways.Any()) | ||
{ | ||
return; | ||
} | ||
|
||
foreach (ActiveRunway runway in missingRunways) | ||
{ | ||
events.AddEvent( | ||
new ValidationRuleFailure( | ||
$"Ruunway {runway.Airfield}/{runway.Identifier} specified in active runway does not exist", | ||
runway | ||
) | ||
); | ||
} | ||
} | ||
|
||
private bool IsSameRunway(ActiveRunway activeRunway, Runway runway) | ||
{ | ||
return runway.AirfieldIcao == activeRunway.Airfield && | ||
(runway.FirstIdentifier == activeRunway.Identifier || | ||
runway.ReverseIdentifier == activeRunway.Identifier); | ||
} | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/Compiler/Validate/AllActiveRunwaysMustReferenceAnAirport.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,34 @@ | ||
using System.Linq; | ||
using Compiler.Argument; | ||
using Compiler.Error; | ||
using Compiler.Event; | ||
using Compiler.Model; | ||
|
||
namespace Compiler.Validate | ||
{ | ||
public class AllActiveRunwaysMustReferenceAnAirport: IValidationRule | ||
{ | ||
public void Validate(SectorElementCollection sectorElements, CompilerArguments args, IEventLogger events) | ||
{ | ||
var airports = sectorElements.Airports.Select(airport => airport.Icao).ToList(); | ||
var missingAirports = sectorElements.ActiveRunways | ||
.Where(activeRunway => !airports.Contains(activeRunway.Airfield)) | ||
.ToList(); | ||
|
||
if (!missingAirports.Any()) | ||
{ | ||
return; | ||
} | ||
|
||
foreach (ActiveRunway runway in missingAirports) | ||
{ | ||
events.AddEvent( | ||
new ValidationRuleFailure( | ||
$"Airport {runway.Airfield} specified in active runway does not exist", | ||
runway | ||
) | ||
); | ||
} | ||
} | ||
} | ||
} |
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
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
37 changes: 37 additions & 0 deletions
37
tests/CompilerTest/Validate/AllActiveRunwaysMustBeUniqueTest.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,37 @@ | ||
using Compiler.Validate; | ||
using CompilerTest.Bogus.Factory; | ||
using Xunit; | ||
|
||
namespace CompilerTest.Validate | ||
{ | ||
public class AllActiveRunwaysMustBeUniqueTest: AbstractValidatorTestCase | ||
{ | ||
[Fact] | ||
public void TestItFailsIfDuplicatesExist() | ||
{ | ||
sectorElements.Add(ActiveRunwayFactory.Make("EGLL", "27R", 1)); | ||
sectorElements.Add(ActiveRunwayFactory.Make("EGLL", "27R", 1)); | ||
sectorElements.Add(ActiveRunwayFactory.Make("EGLL", "27R", 0)); | ||
sectorElements.Add(ActiveRunwayFactory.Make("EGLL", "27L", 1)); | ||
sectorElements.Add(ActiveRunwayFactory.Make("EGLL", "27L", 1)); | ||
sectorElements.Add(ActiveRunwayFactory.Make("EGLL", "27L", 0)); | ||
sectorElements.Add(ActiveRunwayFactory.Make("EGLL", "27L", 0)); | ||
AssertValidationErrors(3); | ||
} | ||
|
||
[Fact] | ||
public void TestItPassesOnNoDuplicates() | ||
{ | ||
sectorElements.Add(ActiveRunwayFactory.Make("EGLL", "27R", 1)); | ||
sectorElements.Add(ActiveRunwayFactory.Make("EGLL", "27R", 0)); | ||
sectorElements.Add(ActiveRunwayFactory.Make("EGLL", "27L", 0)); | ||
sectorElements.Add(ActiveRunwayFactory.Make("EGXY", "27L", 0)); | ||
AssertNoValidationErrors(); | ||
} | ||
|
||
protected override IValidationRule GetValidationRule() | ||
{ | ||
return new AllActiveRunwaysMustBeUnique(); | ||
} | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
tests/CompilerTest/Validate/AllActiveRunwaysMustReferenceARunwayTest.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,42 @@ | ||
using Compiler.Validate; | ||
using CompilerTest.Bogus.Factory; | ||
using Xunit; | ||
|
||
namespace CompilerTest.Validate | ||
{ | ||
public class AllActiveRunwaysMustReferenceARunwayTest: AbstractValidatorTestCase | ||
{ | ||
public AllActiveRunwaysMustReferenceARunwayTest() | ||
{ | ||
sectorElements.Add(RunwayFactory.Make("EGLL", "09R", "27L")); | ||
sectorElements.Add(RunwayFactory.Make("EGKK", "26L", "08R")); | ||
} | ||
|
||
[Theory] | ||
[InlineData("EGLL", "27R")] | ||
[InlineData("EGKK", "26R")] | ||
[InlineData("EGLL", "abc")] | ||
[InlineData("EGGD", "09")] | ||
public void TestItFailsIfRunwayDoesntExist(string icao, string identifier) | ||
{ | ||
sectorElements.Add(ActiveRunwayFactory.Make(icao, identifier)); | ||
AssertValidationErrors(); | ||
} | ||
|
||
[Theory] | ||
[InlineData("EGLL", "09R")] // First identifier match | ||
[InlineData("EGKK", "26L")] // First identifier match | ||
[InlineData("EGLL", "27L")] // Second identifier match | ||
[InlineData("EGKK", "08R")] // Second identifier match | ||
public void TestItPassesIfRunwayExists(string icao, string identifier) | ||
{ | ||
sectorElements.Add(ActiveRunwayFactory.Make(icao, identifier)); | ||
AssertNoValidationErrors(); | ||
} | ||
|
||
protected override IValidationRule GetValidationRule() | ||
{ | ||
return new AllActiveRunwaysMustReferenceARunway(); | ||
} | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
tests/CompilerTest/Validate/AllActiveRunwaysMustReferenceAnAirportTest.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,39 @@ | ||
using Compiler.Validate; | ||
using CompilerTest.Bogus.Factory; | ||
using Xunit; | ||
|
||
namespace CompilerTest.Validate | ||
{ | ||
public class AllActiveRunwaysMustReferenceAnAirportTest: AbstractValidatorTestCase | ||
{ | ||
public AllActiveRunwaysMustReferenceAnAirportTest() | ||
{ | ||
sectorElements.Add(AirportFactory.Make("EGLL")); | ||
sectorElements.Add(AirportFactory.Make("EGKK")); | ||
sectorElements.Add(AirportFactory.Make("EGCC")); | ||
} | ||
|
||
[Fact] | ||
public void TestItFailsIfAirportDoesntExist() | ||
{ | ||
sectorElements.Add(ActiveRunwayFactory.Make("EGKB")); | ||
sectorElements.Add(ActiveRunwayFactory.Make("EGLC")); | ||
sectorElements.Add(ActiveRunwayFactory.Make("EGGD")); | ||
AssertValidationErrors(3); | ||
} | ||
|
||
[Fact] | ||
public void TestItPassesIfAirportExists() | ||
{ | ||
sectorElements.Add(ActiveRunwayFactory.Make("EGLL")); | ||
sectorElements.Add(ActiveRunwayFactory.Make("EGKK")); | ||
sectorElements.Add(ActiveRunwayFactory.Make("EGCC")); | ||
AssertNoValidationErrors(); | ||
} | ||
|
||
protected override IValidationRule GetValidationRule() | ||
{ | ||
return new AllActiveRunwaysMustReferenceAnAirport(); | ||
} | ||
} | ||
} |