Index: Ringtoets/Common/src/Ringtoets.Common.Data/Exceptions/IllustrationPointConversionException.cs =================================================================== diff -u --- Ringtoets/Common/src/Ringtoets.Common.Data/Exceptions/IllustrationPointConversionException.cs (revision 0) +++ Ringtoets/Common/src/Ringtoets.Common.Data/Exceptions/IllustrationPointConversionException.cs (revision be44404797296f09605f5fc861c87f5766c14478) @@ -0,0 +1,68 @@ +// Copyright (C) Stichting Deltares 2017. All rights reserved. +// +// This file is part of Ringtoets. +// +// Ringtoets is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +// +// All names, logos, and references to "Deltares" are registered trademarks of +// Stichting Deltares and remain full property of Stichting Deltares at all times. +// All rights reserved. + +using System; +using System.Runtime.Serialization; + +namespace Ringtoets.Common.Data.Exceptions +{ + /// + /// Exception that is thrown when converting illustration points from the HydraRing data was unsuccesful. + /// + [Serializable] + public class IllustrationPointConversionException : Exception + { + /// + /// Initializes a new instance of the class. + /// + public IllustrationPointConversionException() {} + + /// + /// Initializes a new instance of the class + /// with a specified error message. + /// + /// The error message that explains the reason for the exception. + public IllustrationPointConversionException(string message) : base(message) {} + + /// + /// Initializes a new instance of the class + /// with a specified error message and a reference to the inner exception that is + /// the cause of this exception. + /// + /// The error message that explains the reason for the exception. + /// The exception that is the cause of the current exception, + /// or null if no inner exception is specified. + public IllustrationPointConversionException(string message, Exception inner) : base(message, inner) {} + + /// + /// Initializes a new instance of with + /// serialized data. + /// The that holds the serialized + /// object data about the exception being thrown. + /// The that contains contextual + /// information about the source or destination. + /// The parameter is + /// null. + /// The class name is null or + /// is zero (0). + protected IllustrationPointConversionException(SerializationInfo info, StreamingContext context) : base(info, context) {} + } +} \ No newline at end of file Index: Ringtoets/Common/src/Ringtoets.Common.Data/Ringtoets.Common.Data.csproj =================================================================== diff -u -r2e5f2b59e81cda48f3d7ee03afa4db88abc7b1a8 -rbe44404797296f09605f5fc861c87f5766c14478 --- Ringtoets/Common/src/Ringtoets.Common.Data/Ringtoets.Common.Data.csproj (.../Ringtoets.Common.Data.csproj) (revision 2e5f2b59e81cda48f3d7ee03afa4db88abc7b1a8) +++ Ringtoets/Common/src/Ringtoets.Common.Data/Ringtoets.Common.Data.csproj (.../Ringtoets.Common.Data.csproj) (revision be44404797296f09605f5fc861c87f5766c14478) @@ -59,6 +59,7 @@ + Index: Ringtoets/Common/src/Ringtoets.Common.Service/IllustrationPoints/GeneralResultConverter.cs =================================================================== diff -u -r2e5f2b59e81cda48f3d7ee03afa4db88abc7b1a8 -rbe44404797296f09605f5fc861c87f5766c14478 --- Ringtoets/Common/src/Ringtoets.Common.Service/IllustrationPoints/GeneralResultConverter.cs (.../GeneralResultConverter.cs) (revision 2e5f2b59e81cda48f3d7ee03afa4db88abc7b1a8) +++ Ringtoets/Common/src/Ringtoets.Common.Service/IllustrationPoints/GeneralResultConverter.cs (.../GeneralResultConverter.cs) (revision be44404797296f09605f5fc861c87f5766c14478) @@ -22,12 +22,14 @@ using System; using System.Collections.Generic; using System.Linq; +using Ringtoets.Common.Data.Exceptions; using Ringtoets.Common.Data.IllustrationPoints; using HydraRingWindDirectionClosingSituation = Ringtoets.HydraRing.Calculation.Data.Output.IllustrationPoints.WindDirectionClosingSituation; using HydraRingIllustrationPointTreeNode = Ringtoets.HydraRing.Calculation.Data.Output.IllustrationPoints.IllustrationPointTreeNode; using HydraRingGeneralResult = Ringtoets.HydraRing.Calculation.Data.Output.IllustrationPoints.GeneralResult; using IHydraRingIllustrationPoint = Ringtoets.HydraRing.Calculation.Data.Output.IllustrationPoints.IIllustrationPoint; using HydraRingSubMechanismIllustrationPoint = Ringtoets.HydraRing.Calculation.Data.Output.IllustrationPoints.SubMechanismIllustrationPoint; +using HydraRingFaultTreeIllustrationPoint = Ringtoets.HydraRing.Calculation.Data.Output.IllustrationPoints.FaultTreeIllustrationPoint; namespace Ringtoets.Common.Service.IllustrationPoints { @@ -36,6 +38,13 @@ /// public static class GeneralResultConverter { + private static IEnumerable GetStochasts(HydraRingGeneralResult hydraGeneralResult) + { + return hydraGeneralResult.Stochasts.Select(StochastConverter.CreateStochast); + } + + #region SubMechanismIllustrationPoint + /// /// Creates a new instance of for top level sub /// mechanism illustration points based on the information of . @@ -44,6 +53,8 @@ /// to create on. /// The newly created . /// Thrown when is null. + /// Thrown when the + /// cannot be converted to a with top level sub mechanism illustration points. public static GeneralResult CreateGeneralResultTopLevelSubMechanismIllustrationPoint( HydraRingGeneralResult hydraRingGeneralResult) { @@ -52,37 +63,128 @@ throw new ArgumentNullException(nameof(hydraRingGeneralResult)); } - WindDirection windDirection = WindDirectionConverter.Create(hydraRingGeneralResult.GoverningWindDirection); + WindDirection governingWindDirection = WindDirectionConverter.Create(hydraRingGeneralResult.GoverningWindDirection); IEnumerable stochasts = GetStochasts(hydraRingGeneralResult); IEnumerable windDirectionClosingScenarioIllustrationPoints = - GetWindDirectionClosingSituationIllustrationPoint(hydraRingGeneralResult); + GetTopLevelSubMechanismIllustrationPoints(hydraRingGeneralResult.IllustrationPoints); - return new GeneralResult(windDirection, stochasts, windDirectionClosingScenarioIllustrationPoints); + return new GeneralResult(governingWindDirection, + stochasts, + windDirectionClosingScenarioIllustrationPoints); } - private static IEnumerable GetStochasts(HydraRingGeneralResult hydraGeneralResult) + /// + /// Creates all the top level fault tree illustration points based on the + /// combinations of and + /// . + /// + /// The collection of + /// and + /// combinations to base the on. + /// An of . + /// Thrown when the combination of + /// and + /// cannot be converted to . + private static IEnumerable GetTopLevelSubMechanismIllustrationPoints( + IDictionary hydraRingTopLevelIllustrationPoints) { - return hydraGeneralResult.Stochasts.Select(StochastConverter.CreateStochast); + var topLevelIlustrationPoints = new List(); + foreach (KeyValuePair topLevelIllustrationPointTreeNode in hydraRingTopLevelIllustrationPoints) + { + IHydraRingIllustrationPoint hydraIllustrationPointData = topLevelIllustrationPointTreeNode.Value.Data; + HydraRingWindDirectionClosingSituation hydraWindDirectionClosingSituation = topLevelIllustrationPointTreeNode.Key; + + var subMechanismIllustrationPoint = hydraIllustrationPointData as HydraRingSubMechanismIllustrationPoint; + if (subMechanismIllustrationPoint != null) + { + topLevelIlustrationPoints.Add(TopLevelSubMechanismIllustrationPointConverter.Create( + hydraWindDirectionClosingSituation, subMechanismIllustrationPoint)); + } + else + { + string exceptionMessage = $"Expected a fault tree node with data of type {typeof(HydraRingSubMechanismIllustrationPoint)} as root, " + + $"but got {hydraIllustrationPointData.GetType()}"; + throw new IllustrationPointConversionException(exceptionMessage); + } + } + + return topLevelIlustrationPoints; } - private static IEnumerable GetWindDirectionClosingSituationIllustrationPoint( - HydraRingGeneralResult hydraGeneralResult) + #endregion + + #region FaultTreeIllustrationPoint + + /// + /// Creates a new instance of for fault tree illustration points + /// based on the information of . + /// + /// The + /// to base the to create on. + /// The newly created . + /// Thrown when + /// is null. + /// Thrown when the + /// cannot be converted to a with top level fault tree illustration points. + public static GeneralResult CreateGeneralResultTopLevelFaultTreeIllustrationPoint( + HydraRingGeneralResult hydraRingGeneralResult) { - var combinations = new List(); - foreach (KeyValuePair illustrationPointTreeNode in hydraGeneralResult.IllustrationPoints) + if (hydraRingGeneralResult == null) { - IHydraRingIllustrationPoint hydraIllustrationPoint = illustrationPointTreeNode.Value.Data; + throw new ArgumentNullException(nameof(hydraRingGeneralResult)); + } + + WindDirection governingWindDirection = WindDirectionConverter.Create(hydraRingGeneralResult.GoverningWindDirection); + IEnumerable stochasts = GetStochasts(hydraRingGeneralResult); + IEnumerable topLevelIllustrationPoints = + GetTopLevelFaultTreeIllustrationPoints(hydraRingGeneralResult.IllustrationPoints); + + return new GeneralResult(governingWindDirection, + stochasts, + topLevelIllustrationPoints); + } + + /// + /// Creates all the top level fault tree illustration points based on the + /// combinations of and + /// . + /// + /// The collection of + /// and + /// combinations to base the on. + /// An of . + /// Thrown when the combination of + /// and + /// cannot be converted to . + private static IEnumerable GetTopLevelFaultTreeIllustrationPoints( + IDictionary hydraRingTopLevelIllustrationPoints) + { + var topLevelIllustrationPoints = new List(); + foreach (KeyValuePair illustrationPointTreeNode in hydraRingTopLevelIllustrationPoints) + { + HydraRingIllustrationPointTreeNode hydraRingIllustrationPointTreeNode = illustrationPointTreeNode.Value; + IHydraRingIllustrationPoint hydraIllustrationPointData = hydraRingIllustrationPointTreeNode.Data; HydraRingWindDirectionClosingSituation hydraWindDirectionClosingSituation = illustrationPointTreeNode.Key; - var subMechanismIllustrationPoint = hydraIllustrationPoint as HydraRingSubMechanismIllustrationPoint; - if (subMechanismIllustrationPoint != null) + var faultTreeIllustrationPoint = hydraIllustrationPointData as HydraRingFaultTreeIllustrationPoint; + if (faultTreeIllustrationPoint != null) { - combinations.Add(TopLevelSubMechanismIllustrationPointConverter.Create( - hydraWindDirectionClosingSituation, subMechanismIllustrationPoint)); + topLevelIllustrationPoints.Add(TopLevelFaultTreeIllustrationPointConverter.Create( + hydraWindDirectionClosingSituation, hydraRingIllustrationPointTreeNode)); } + else + { + string exceptionMessage = $"Expected a fault tree node with data of type {typeof(HydraRingFaultTreeIllustrationPoint)} as root, " + + $"but got {hydraIllustrationPointData.GetType()}"; + throw new IllustrationPointConversionException(exceptionMessage); + } } - return combinations; + return topLevelIllustrationPoints; } + + #endregion } } \ No newline at end of file Index: Ringtoets/Common/test/Ringtoets.Common.Data.Test/Exceptions/IllustrationPointConversionExceptionTest.cs =================================================================== diff -u --- Ringtoets/Common/test/Ringtoets.Common.Data.Test/Exceptions/IllustrationPointConversionExceptionTest.cs (revision 0) +++ Ringtoets/Common/test/Ringtoets.Common.Data.Test/Exceptions/IllustrationPointConversionExceptionTest.cs (revision be44404797296f09605f5fc861c87f5766c14478) @@ -0,0 +1,32 @@ +// Copyright (C) Stichting Deltares 2017. All rights reserved. +// +// This file is part of Ringtoets. +// +// Ringtoets is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +// +// All names, logos, and references to "Deltares" are registered trademarks of +// Stichting Deltares and remain full property of Stichting Deltares at all times. +// All rights reserved. + +using System; +using Core.Common.TestUtil; +using NUnit.Framework; +using Ringtoets.Common.Data.Exceptions; + +namespace Ringtoets.Common.Data.Test.Exceptions +{ + [TestFixture] + public class IllustrationPointConversionExceptionTest : + CustomExceptionDesignGuidelinesTestFixture {} +} \ No newline at end of file Index: Ringtoets/Common/test/Ringtoets.Common.Data.Test/Ringtoets.Common.Data.Test.csproj =================================================================== diff -u -r2e5f2b59e81cda48f3d7ee03afa4db88abc7b1a8 -rbe44404797296f09605f5fc861c87f5766c14478 --- Ringtoets/Common/test/Ringtoets.Common.Data.Test/Ringtoets.Common.Data.Test.csproj (.../Ringtoets.Common.Data.Test.csproj) (revision 2e5f2b59e81cda48f3d7ee03afa4db88abc7b1a8) +++ Ringtoets/Common/test/Ringtoets.Common.Data.Test/Ringtoets.Common.Data.Test.csproj (.../Ringtoets.Common.Data.Test.csproj) (revision be44404797296f09605f5fc861c87f5766c14478) @@ -70,6 +70,7 @@ + Index: Ringtoets/Common/test/Ringtoets.Common.Service.Test/IllustrationPoints/GeneralResultConverterTest.cs =================================================================== diff -u -r2c00fea093af45947307620c4203188ceb872951 -rbe44404797296f09605f5fc861c87f5766c14478 --- Ringtoets/Common/test/Ringtoets.Common.Service.Test/IllustrationPoints/GeneralResultConverterTest.cs (.../GeneralResultConverterTest.cs) (revision 2c00fea093af45947307620c4203188ceb872951) +++ Ringtoets/Common/test/Ringtoets.Common.Service.Test/IllustrationPoints/GeneralResultConverterTest.cs (.../GeneralResultConverterTest.cs) (revision be44404797296f09605f5fc861c87f5766c14478) @@ -24,6 +24,7 @@ using System.Linq; using Core.Common.TestUtil; using NUnit.Framework; +using Ringtoets.Common.Data.Exceptions; using Ringtoets.Common.Data.IllustrationPoints; using Ringtoets.Common.Data.TestUtil; using Ringtoets.Common.Service.IllustrationPoints; @@ -55,7 +56,7 @@ } [Test] - public void Create_HydraGeneralResultWithoutIllustrationPoints_ExpectedProperties() + public void CreateGeneralResultTopLevelSubMechanismIllustrationPoint_HydraGeneralResultWithoutIllustrationPoints_ExpectedProperties() { // Setup var random = new Random(21); @@ -80,7 +81,7 @@ } [Test] - public void Create_HydraGeneralResultWithSubMechanismIllustrationPointsOnly_ExpectedProperties() + public void CreateGeneralResultTopLevelSubMechanismIllustrationPoint_HydraGeneralResultWithSubMechanismIllustrationPointsOnly_ExpectedProperties() { // Setup const string closingSituation = "Closing situation"; @@ -132,7 +133,7 @@ } [Test] - public void Create_HydraRingGeneralResultWithFaultTreeIllustrationPointsOnly_ExpectedProperties() + public void CreateGeneralResultTopLevelSubMechanismIllustrationPoint_HydraRingGeneralResultWithFaultTreeIllustrationPointsOnly_ThrowsIllustrationPointConversionException() { // Setup var random = new Random(21); @@ -166,14 +167,149 @@ }); // Call - GeneralResult generalResultSubMechanismIllustrationPoint = - GeneralResultConverter.CreateGeneralResultTopLevelSubMechanismIllustrationPoint(hydraRingGeneralResult); + TestDelegate call = () => GeneralResultConverter.CreateGeneralResultTopLevelSubMechanismIllustrationPoint(hydraRingGeneralResult); // Assert - WindDirection generalResultGoverningWindDirection = generalResultSubMechanismIllustrationPoint.GoverningWindDirection; + var exception = Assert.Throws(call); + string expectedMessage = $"Expected a fault tree node with data of type {typeof(HydraRingSubMechanismIllustrationPoint)} as root, " + + $"but got {hydraRingIllustrationPoint.GetType()}"; + Assert.AreEqual(expectedMessage, exception.Message); + } + + [Test] + public void CreateGeneralResultTopLevelFaultTreeIllustrationPoint_HydraRingGeneralResultNull_ThrowsArgumentNullException() + { + // Call + TestDelegate call = () => GeneralResultConverter.CreateGeneralResultTopLevelFaultTreeIllustrationPoint(null); + + // Assert + string paramName = Assert.Throws(call).ParamName; + Assert.AreEqual("hydraRingGeneralResult", paramName); + } + + [Test] + public void CreateGeneralResultTopLevelFaultTreeIllustrationPoint_HydraGeneralResultWithoutIllustrationPoints_ExpectedProperties() + { + // Setup + var random = new Random(21); + var hydraGoverningWindDirection = new HydraRingWindDirection("Name", random.NextDouble()); + + var hydraRingGeneralResult = new HydraRingGeneralResult( + random.NextDouble(), + hydraGoverningWindDirection, + Enumerable.Empty(), + new Dictionary< + HydraRingWindDirectionClosingSituation, + HydraRingIllustrationPointTreeNode>()); + + // Call + GeneralResult generalResult = + GeneralResultConverter.CreateGeneralResultTopLevelFaultTreeIllustrationPoint(hydraRingGeneralResult); + + // Assert + AssertWindDirection(hydraGoverningWindDirection, generalResult.GoverningWindDirection); + CollectionAssert.IsEmpty(generalResult.TopLevelIllustrationPoints); + CollectionAssert.IsEmpty(generalResult.Stochasts); + } + + [Test] + public void CreateGeneralResultTopLevelFaultTreeIllustrationPoint_HydraGeneralResultWithSubMechanismIllustrationPointsOnly_ThrowsIllustrationPointConversionException() + { + // Setup + const string closingSituation = "Closing situation"; + + var random = new Random(21); + var hydraRingWindDirection = new HydraRingWindDirection("SSE", random.NextDouble()); + var hydraRingWindDirectionClosingSituation = new HydraRingWindDirectionClosingSituation( + hydraRingWindDirection, closingSituation); + + var hydraRingIllustrationPoint = new HydraRingSubMechanismIllustrationPoint( + "Illustration Point", + Enumerable.Empty(), + Enumerable.Empty(), + random.NextDouble()); + var hydraRingIllustrationTreeNode = new HydraRingIllustrationPointTreeNode(hydraRingIllustrationPoint); + + double governingWindDirectionAngle = random.NextDouble(); + var governingHydraWindDirection = new HydraRingWindDirection("Name", governingWindDirectionAngle); + var hydraGeneralResult = new HydraRingGeneralResult( + random.NextDouble(), + governingHydraWindDirection, + Enumerable.Empty(), + new Dictionary + { + { + hydraRingWindDirectionClosingSituation, hydraRingIllustrationTreeNode + } + }); + + // Call + TestDelegate call = () => GeneralResultConverter.CreateGeneralResultTopLevelFaultTreeIllustrationPoint(hydraGeneralResult); + + // Assert + var exception = Assert.Throws(call); + string expectedMessage = $"Expected a fault tree node with data of type {typeof(HydraRingFaultTreeIllustrationPoint)} as root, but got {hydraRingIllustrationPoint.GetType()}"; + Assert.AreEqual(expectedMessage, exception.Message); + } + + [Test] + public void CreateGeneralResultTopLevelFaultTreeIllustrationPoint_HydraRingGeneralResultWithFaultTreeIllustrationPointsOnly_ExpectedProperties() + { + // Setup + var random = new Random(21); + + const string closingSituation = "Closing situation"; + var hydraRingWindDirection = new HydraRingWindDirection("SSE", random.NextDouble()); + var hydraRingWindDirectionClosingSituation = new HydraRingWindDirectionClosingSituation( + hydraRingWindDirection, + closingSituation); + + var hydraRingIllustrationPoint = new HydraRingFaultTreeIllustrationPoint( + "IllustrationPoint", + random.NextDouble(), + Enumerable.Empty(), + random.NextEnumValue()); + var hydraRingIllustrationTreeNode = new HydraRingIllustrationPointTreeNode(hydraRingIllustrationPoint); + + double governingWindDirectionAngle = random.NextDouble(); + var governingHydraRingWindDirection = new HydraRingWindDirection( + "Name", + governingWindDirectionAngle); + var hydraRingGeneralResult = new HydraRingGeneralResult( + random.NextDouble(), + governingHydraRingWindDirection, + Enumerable.Empty(), + new Dictionary + { + { + hydraRingWindDirectionClosingSituation, hydraRingIllustrationTreeNode + } + }); + + // Call + GeneralResult generalResultSubMechanismIllustrationPoint = + GeneralResultConverter.CreateGeneralResultTopLevelFaultTreeIllustrationPoint(hydraRingGeneralResult); + + // Assert + WindDirection generalResultGoverningWindDirection = + generalResultSubMechanismIllustrationPoint.GoverningWindDirection; AssertWindDirection(governingHydraRingWindDirection, generalResultGoverningWindDirection); - CollectionAssert.IsEmpty(generalResultSubMechanismIllustrationPoint.Stochasts); - CollectionAssert.IsEmpty(generalResultSubMechanismIllustrationPoint.TopLevelIllustrationPoints); + + TopLevelFaultTreeIllustrationPoint topLevelFaultTreeIllustrationPoint = + generalResultSubMechanismIllustrationPoint.TopLevelIllustrationPoints.Single(); + AssertWindDirection(hydraRingWindDirection, topLevelFaultTreeIllustrationPoint.WindDirection); + Assert.AreEqual(closingSituation, topLevelFaultTreeIllustrationPoint.ClosingSituation); + + IllustrationPointNode faultTreeIllustrationPoint = + topLevelFaultTreeIllustrationPoint.FaultTreeNodeRoot; + CollectionAssert.IsEmpty(faultTreeIllustrationPoint.Children); + + var faultTreeIllustrationPointData = (FaultTreeIllustrationPoint) faultTreeIllustrationPoint.Data; + CollectionAssert.IsEmpty(faultTreeIllustrationPointData.Stochasts); + Assert.AreEqual(hydraRingIllustrationPoint.Name, faultTreeIllustrationPointData.Name); + Assert.AreEqual(hydraRingIllustrationPoint.Beta, faultTreeIllustrationPointData.Beta, + faultTreeIllustrationPointData.Beta.GetAccuracy()); + Assert.AreEqual((CombinationType) hydraRingIllustrationPoint.CombinationType, faultTreeIllustrationPointData.CombinationType); } private static void AssertWindDirection(HydraRingWindDirection hydraRingWindDirection, WindDirection windDirection)