// 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.Collections.Generic; using System.Linq; using Core.Common.Base.Geometry; using Core.Common.TestUtil; using Core.Common.Utils; using NUnit.Framework; using Ringtoets.Common.Data; namespace Ringtoets.Piping.Primitives.Test { [TestFixture] public class PipingSurfaceLineTest { [Test] public void DefaultConstructor_ExpectedValues() { // Call var surfaceLine = new PipingSurfaceLine(); // Assert Assert.IsInstanceOf(surfaceLine); } [Test] public void CopyProperties_WithSurfaceLineNull_ThrowsArgumentNullException() { // Setup var surfaceLine = new PipingSurfaceLine(); // Call TestDelegate call = () => surfaceLine.CopyProperties(null); // Assert string paramName = Assert.Throws(call).ParamName; Assert.AreEqual("fromSurfaceLine", paramName); } [Test] public void CopyProperties_LineWithUpdatedGeometricPoints_PropertiesUpdated() { // Setup PipingSurfaceLine surfaceLine = CreateSurfaceLineWithCharacteristicPoints(); PipingSurfaceLine surfaceLineToUpdateFrom = CreateSurfaceLineWithCharacteristicPoints(); var expectedGeometry = new List { new Point3D(0, 1, 2), new Point3D(3, 4, 5), new Point3D(6, 7, 8) }; expectedGeometry.AddRange(surfaceLine.Points); surfaceLineToUpdateFrom.SetGeometry(expectedGeometry); // Call surfaceLine.CopyProperties(surfaceLineToUpdateFrom); // Assert AssertPropertiesUpdated(surfaceLineToUpdateFrom, surfaceLine); } [Test] public void CopyProperties_LineUpdatedWithRemovedCharacteristicPoints_PropertiesUpdated() { // Setup PipingSurfaceLine surfaceLine = CreateSurfaceLineWithCharacteristicPoints(); var surfaceLineToUpdateFrom = new PipingSurfaceLine { Name = surfaceLine.Name, ReferenceLineIntersectionWorldPoint = surfaceLine.ReferenceLineIntersectionWorldPoint }; surfaceLineToUpdateFrom.SetGeometry(surfaceLine.Points); // Call surfaceLine.CopyProperties(surfaceLineToUpdateFrom); // Assert AssertPropertiesUpdated(surfaceLineToUpdateFrom, surfaceLine); } [Test] public void CopyProperties_LineWithUpdatedReferenceLineWorldPoint_PropertiesUpdated() { // Setup PipingSurfaceLine surfaceLine = CreateSurfaceLineWithCharacteristicPoints(); var expectedIntersectionPoint = new Point2D(123, 456); PipingSurfaceLine surfaceLineToUpdateFrom = CreateSurfaceLineWithCharacteristicPoints(); surfaceLineToUpdateFrom.ReferenceLineIntersectionWorldPoint = expectedIntersectionPoint; // Call surfaceLine.CopyProperties(surfaceLineToUpdateFrom); // Assert AssertPropertiesUpdated(surfaceLineToUpdateFrom, surfaceLine); } [Test] public void CopyProperties_LineWithUpdatedGeometryAndReferenceLineIntersectionAndCharacteristicPoints_PropertiesUpdated() { // Setup var surfaceLine = new PipingSurfaceLine(); PipingSurfaceLine surfaceLineToUpdateFrom = CreateSurfaceLineWithCharacteristicPoints(); // Call surfaceLine.CopyProperties(surfaceLineToUpdateFrom); // Assert AssertPropertiesUpdated(surfaceLineToUpdateFrom, surfaceLine); } [Test] public void Equals_DerivedClassWithEqualProperties_ReturnsTrue() { // Setup PipingSurfaceLine profile = CreateSurfaceLineWithCharacteristicPoints(); var derivedLayer = new TestSurfaceLine(profile); // Call bool areEqual = profile.Equals(derivedLayer); // Assert Assert.IsTrue(areEqual); } [Test] public void Equals_ToItself_ReturnsTrue() { // Setup var surfaceLineOne = new PipingSurfaceLine(); // Call bool isLineOneEqualToLineOne = surfaceLineOne.Equals(surfaceLineOne); // Assert Assert.IsTrue(isLineOneEqualToLineOne); } [Test] public void Equals_SameReference_ReturnsTrue() { // Setup var surfaceLineOne = new PipingSurfaceLine(); PipingSurfaceLine surfaceLineTwo = surfaceLineOne; // Call bool isLineOneEqualToLineTwo = surfaceLineOne.Equals(surfaceLineTwo); bool isLineTwoEqualToLineOne = surfaceLineTwo.Equals(surfaceLineOne); // Assert Assert.IsTrue(isLineOneEqualToLineTwo); Assert.IsTrue(isLineTwoEqualToLineOne); } [Test] public void Equals_ToNull_ReturnsFalse() { // Setup var surfaceLineOne = new PipingSurfaceLine { Name = "Name A" }; // Call bool isLineOneEqualToNull = surfaceLineOne.Equals(null); // Assert Assert.IsFalse(isLineOneEqualToNull); } [Test] public void Equals_ToDifferentType_ReturnsFalse() { // Setup var surfaceLineOne = new PipingSurfaceLine { Name = "Name A" }; var differentType = new object(); // Call bool isSurfaceLineEqualToDifferentType = surfaceLineOne.Equals(differentType); bool isDifferentTypeEqualToSurfaceLine = differentType.Equals(surfaceLineOne); // Assert Assert.IsFalse(isSurfaceLineEqualToDifferentType); Assert.IsFalse(isDifferentTypeEqualToSurfaceLine); } [Test] public void Equals_DifferentNames_ReturnsFalse() { // Setup PipingSurfaceLine surfaceLineOne = CreateSurfaceLineWithCharacteristicPoints(); surfaceLineOne.Name = "Name A"; PipingSurfaceLine surfaceLineTwo = CreateSurfaceLineWithCharacteristicPoints(); surfaceLineTwo.Name = "Name B"; // Call bool isLineOneEqualToLineTwo = surfaceLineOne.Equals(surfaceLineTwo); bool isLineTwoEqualToLineOne = surfaceLineTwo.Equals(surfaceLineOne); // Assert Assert.IsFalse(isLineOneEqualToLineTwo); Assert.IsFalse(isLineTwoEqualToLineOne); } [Test] public void Equals_DifferentGeometries_ReturnsFalse() { // Setup var surfaceLineOne = new PipingSurfaceLine { Name = "Name A" }; surfaceLineOne.SetGeometry(new[] { new Point3D(1, 2, 3) }); var surfaceLineTwo = new PipingSurfaceLine { Name = "Name A" }; surfaceLineTwo.SetGeometry(new[] { new Point3D(3, 4, 5) }); // Call bool isLineOneEqualToLineTwo = surfaceLineOne.Equals(surfaceLineTwo); bool isLineTwoEqualToLineOne = surfaceLineTwo.Equals(surfaceLineOne); // Assert Assert.IsFalse(isLineOneEqualToLineTwo); Assert.IsFalse(isLineTwoEqualToLineOne); } [Test] public void Equals_DifferentReferenceLineIntersectionWorldPoint_ReturnsFalse() { // Setup PipingSurfaceLine surfaceLineOne = CreateSurfaceLineWithCharacteristicPoints(); surfaceLineOne.ReferenceLineIntersectionWorldPoint = new Point2D(0, 0); PipingSurfaceLine surfaceLineTwo = CreateSurfaceLineWithCharacteristicPoints(); surfaceLineTwo.ReferenceLineIntersectionWorldPoint = new Point2D(1, 1); // Call bool isLineOneEqualToLineTwo = surfaceLineOne.Equals(surfaceLineTwo); bool isLineTwoEqualToLineOne = surfaceLineTwo.Equals(surfaceLineOne); // Assert Assert.IsFalse(isLineOneEqualToLineTwo); Assert.IsFalse(isLineTwoEqualToLineOne); } [Test] public void Equals_DifferentBottomDitchDikeSide_ReturnsFalse() { // Setup PipingSurfaceLine surfaceLineOne = CreateSurfaceLineWithCharacteristicPoints(); PipingSurfaceLine surfaceLineTwo = CreateSurfaceLineWithCharacteristicPoints(); Point3D[] points = surfaceLineTwo.Points.ToArray(); surfaceLineTwo.SetBottomDitchDikeSideAt(points[5]); // Call bool isLineOneEqualToLineTwo = surfaceLineOne.Equals(surfaceLineTwo); bool isLineTwoEqualToLineOne = surfaceLineTwo.Equals(surfaceLineOne); // Assert Assert.IsFalse(isLineOneEqualToLineTwo); Assert.IsFalse(isLineTwoEqualToLineOne); } [Test] public void Equals_DifferentBottomDitchPolderSide_ReturnsFalse() { // Setup PipingSurfaceLine surfaceLineOne = CreateSurfaceLineWithCharacteristicPoints(); PipingSurfaceLine surfaceLineTwo = CreateSurfaceLineWithCharacteristicPoints(); Point3D[] points = surfaceLineTwo.Points.ToArray(); surfaceLineTwo.SetBottomDitchPolderSideAt(points[5]); // Call bool isLineOneEqualToLineTwo = surfaceLineOne.Equals(surfaceLineTwo); bool isLineTwoEqualToLineOne = surfaceLineTwo.Equals(surfaceLineOne); // Assert Assert.IsFalse(isLineOneEqualToLineTwo); Assert.IsFalse(isLineTwoEqualToLineOne); } [Test] public void Equals_DifferentDikeToeAtPolder_ReturnsFalse() { // Setup PipingSurfaceLine surfaceLineOne = CreateSurfaceLineWithCharacteristicPoints(); PipingSurfaceLine surfaceLineTwo = CreateSurfaceLineWithCharacteristicPoints(); Point3D[] points = surfaceLineTwo.Points.ToArray(); surfaceLineTwo.SetDikeToeAtPolderAt(points[5]); // Call bool isLineOneEqualToLineTwo = surfaceLineOne.Equals(surfaceLineTwo); bool isLineTwoEqualToLineOne = surfaceLineTwo.Equals(surfaceLineOne); // Assert Assert.IsFalse(isLineOneEqualToLineTwo); Assert.IsFalse(isLineTwoEqualToLineOne); } [Test] public void Equals_DifferentDikeToeAtRiver_ReturnsFalse() { // Setup PipingSurfaceLine surfaceLineOne = CreateSurfaceLineWithCharacteristicPoints(); PipingSurfaceLine surfaceLineTwo = CreateSurfaceLineWithCharacteristicPoints(); Point3D[] points = surfaceLineTwo.Points.ToArray(); surfaceLineTwo.SetDikeToeAtRiverAt(points[5]); // Call bool isLineOneEqualToLineTwo = surfaceLineOne.Equals(surfaceLineTwo); bool isLineTwoEqualToLineOne = surfaceLineTwo.Equals(surfaceLineOne); // Assert Assert.IsFalse(isLineOneEqualToLineTwo); Assert.IsFalse(isLineTwoEqualToLineOne); } [Test] public void Equals_DifferentDitchDikeSide_ReturnsFalse() { // Setup PipingSurfaceLine surfaceLineOne = CreateSurfaceLineWithCharacteristicPoints(); PipingSurfaceLine surfaceLineTwo = CreateSurfaceLineWithCharacteristicPoints(); Point3D[] points = surfaceLineTwo.Points.ToArray(); surfaceLineTwo.SetDitchDikeSideAt(points[1]); // Call bool isLineOneEqualToLineTwo = surfaceLineOne.Equals(surfaceLineTwo); bool isLineTwoEqualToLineOne = surfaceLineTwo.Equals(surfaceLineOne); // Assert Assert.IsFalse(isLineOneEqualToLineTwo); Assert.IsFalse(isLineTwoEqualToLineOne); } [Test] public void Equals_DifferentDitchPolderSide_ReturnsFalse() { // Setup PipingSurfaceLine surfaceLineOne = CreateSurfaceLineWithCharacteristicPoints(); PipingSurfaceLine surfaceLineTwo = CreateSurfaceLineWithCharacteristicPoints(); Point3D[] points = surfaceLineTwo.Points.ToArray(); surfaceLineTwo.SetDitchPolderSideAt(points[1]); // Call bool isLineOneEqualToLineTwo = surfaceLineOne.Equals(surfaceLineTwo); bool isLineTwoEqualToLineOne = surfaceLineTwo.Equals(surfaceLineOne); // Assert Assert.IsFalse(isLineOneEqualToLineTwo); Assert.IsFalse(isLineTwoEqualToLineOne); } [Test] public void Equals_NamesGeometriesAndReferenceLineIntersectionWorldPointAndCharacteristicPointsEqual_ReturnsTrue() { // Setup PipingSurfaceLine surfaceLineOne = CreateSurfaceLineWithCharacteristicPoints(); PipingSurfaceLine surfaceLineTwo = CreateSurfaceLineWithCharacteristicPoints(); // Call bool isLineOneEqualToLineTwo = surfaceLineOne.Equals(surfaceLineTwo); bool isLineTwoEqualToLineOne = surfaceLineTwo.Equals(surfaceLineOne); // Assert Assert.IsTrue(isLineOneEqualToLineTwo); Assert.IsTrue(isLineTwoEqualToLineOne); } [Test] public void Equals_TransitivePropertyWithSameNamesAndGeometry_ReturnsTrue() { // Setup PipingSurfaceLine surfaceLineOne = CreateSurfaceLineWithCharacteristicPoints(); PipingSurfaceLine surfaceLineTwo = CreateSurfaceLineWithCharacteristicPoints(); PipingSurfaceLine surfaceLineThree = CreateSurfaceLineWithCharacteristicPoints(); // Call bool isLineOneEqualToLineTwo = surfaceLineOne.Equals(surfaceLineTwo); bool isLineTwoEqualToLineThree = surfaceLineTwo.Equals(surfaceLineThree); bool isLineOneEqualToLineThree = surfaceLineOne.Equals(surfaceLineThree); // Assert Assert.IsTrue(isLineOneEqualToLineTwo); Assert.IsTrue(isLineTwoEqualToLineThree); Assert.IsTrue(isLineOneEqualToLineThree); } [Test] public void GetHashCode_EqualSurfaceLines_ReturnSameHashCode() { // Setup var surfaceLineOne = new PipingSurfaceLine(); var surfaceLineTwo = new PipingSurfaceLine(); // Call int hashCodeOne = surfaceLineOne.GetHashCode(); int hashCodeTwo = surfaceLineTwo.GetHashCode(); // Assert Assert.AreEqual(hashCodeOne, hashCodeTwo); } public abstract class SetCharacteristicPointTest { [Test] public void PointInGeometry_PointSetFromGeometry() { // Setup const double testX = 1.0; const double testY = 2.2; const double testZ = 4.4; var testPoint = new Point3D(testX, testY, testZ); var surfaceLine = new PipingSurfaceLine(); CreateTestGeometry(testPoint, surfaceLine); // Call SetCharacteristicPoint(surfaceLine, testPoint); // Assert Assert.AreEqual(testPoint, GetCharacteristicPoint(surfaceLine)); Assert.AreNotSame(testPoint, GetCharacteristicPoint(surfaceLine)); } [Test] public void GeometryEmpty_ThrowsInvalidOperationException() { // Setup var random = new Random(21); var testPoint = new Point3D(random.NextDouble(), random.NextDouble(), random.NextDouble()); var surfaceLine = new PipingSurfaceLine(); // Call TestDelegate test = () => SetCharacteristicPoint(surfaceLine, testPoint); // Assert string expectedMessage = $"De geometrie bevat geen punt op locatie {testPoint} om als '{CharacteristicPointDescription()}' in te stellen."; TestHelper.AssertThrowsArgumentExceptionAndTestMessage(test, expectedMessage); } [Test] public void Null_ThrowsArgumentNullException() { // Setup var surfaceLine = new PipingSurfaceLine(); // Call TestDelegate test = () => SetCharacteristicPoint(surfaceLine, null); // Assert const string expectedMessage = "Cannot find a point in geometry using a null point."; TestHelper.AssertThrowsArgumentExceptionAndTestMessage(test, expectedMessage); } protected abstract void SetCharacteristicPoint(PipingSurfaceLine surfaceLine, Point3D point); protected abstract Point3D GetCharacteristicPoint(PipingSurfaceLine surfaceLine); protected abstract string CharacteristicPointDescription(); } [TestFixture] public class SetDitchPolderSideAtTest : SetCharacteristicPointTest { protected override void SetCharacteristicPoint(PipingSurfaceLine surfaceLine, Point3D point) { surfaceLine.SetDitchPolderSideAt(point); } protected override Point3D GetCharacteristicPoint(PipingSurfaceLine surfaceLine) { return surfaceLine.DitchPolderSide; } protected override string CharacteristicPointDescription() { return "Insteek sloot polderzijde"; } } [TestFixture] public class SetBottomDitchPolderSideAtTest : SetCharacteristicPointTest { protected override void SetCharacteristicPoint(PipingSurfaceLine surfaceLine, Point3D point) { surfaceLine.SetBottomDitchPolderSideAt(point); } protected override Point3D GetCharacteristicPoint(PipingSurfaceLine surfaceLine) { return surfaceLine.BottomDitchPolderSide; } protected override string CharacteristicPointDescription() { return "Slootbodem polderzijde"; } } [TestFixture] public class SetBottomDitchDikeSideAtTest : SetCharacteristicPointTest { protected override void SetCharacteristicPoint(PipingSurfaceLine surfaceLine, Point3D point) { surfaceLine.SetBottomDitchDikeSideAt(point); } protected override Point3D GetCharacteristicPoint(PipingSurfaceLine surfaceLine) { return surfaceLine.BottomDitchDikeSide; } protected override string CharacteristicPointDescription() { return "Slootbodem dijkzijde"; } } [TestFixture] public class SetDitchDikeSideAtTest : SetCharacteristicPointTest { protected override void SetCharacteristicPoint(PipingSurfaceLine surfaceLine, Point3D point) { surfaceLine.SetDitchDikeSideAt(point); } protected override Point3D GetCharacteristicPoint(PipingSurfaceLine surfaceLine) { return surfaceLine.DitchDikeSide; } protected override string CharacteristicPointDescription() { return "Insteek sloot dijkzijde"; } } [TestFixture] public class SetDikeToeAtRiverAtTest : SetCharacteristicPointTest { protected override void SetCharacteristicPoint(PipingSurfaceLine surfaceLine, Point3D point) { surfaceLine.SetDikeToeAtRiverAt(point); } protected override Point3D GetCharacteristicPoint(PipingSurfaceLine surfaceLine) { return surfaceLine.DikeToeAtRiver; } protected override string CharacteristicPointDescription() { return "Teen dijk buitenwaarts"; } } [TestFixture] public class SetDikeToeAtPolderAtTest : SetCharacteristicPointTest { protected override void SetCharacteristicPoint(PipingSurfaceLine surfaceLine, Point3D point) { surfaceLine.SetDikeToeAtPolderAt(point); } protected override Point3D GetCharacteristicPoint(PipingSurfaceLine surfaceLine) { return surfaceLine.DikeToeAtPolder; } protected override string CharacteristicPointDescription() { return "Teen dijk binnenwaarts"; } } private class TestSurfaceLine : PipingSurfaceLine { public TestSurfaceLine(PipingSurfaceLine profile) { CopyProperties(profile); } } private static PipingSurfaceLine CreateSurfaceLineWithCharacteristicPoints() { var surfaceLine = new PipingSurfaceLine { Name = "Name A", ReferenceLineIntersectionWorldPoint = new Point2D(0, 0) }; var geometry = new[] { new Point3D(0, 0, 0), new Point3D(1, 0, 2), new Point3D(2, 0, 3), new Point3D(3, 0, 0), new Point3D(4, 0, 2), new Point3D(5, 0, 3) }; surfaceLine.SetGeometry(geometry); surfaceLine.SetBottomDitchDikeSideAt(geometry[0]); surfaceLine.SetBottomDitchPolderSideAt(geometry[1]); surfaceLine.SetDikeToeAtPolderAt(geometry[2]); surfaceLine.SetDikeToeAtRiverAt(geometry[3]); surfaceLine.SetDitchDikeSideAt(geometry[4]); surfaceLine.SetDitchPolderSideAt(geometry[5]); return surfaceLine; } private static void CreateTestGeometry(Point3D testPoint, PipingSurfaceLine surfaceLine) { var random = new Random(21); var points = new[] { new Point3D(random.NextDouble(), random.NextDouble(), random.NextDouble()), new Point3D(testPoint), new Point3D(2 + random.NextDouble(), random.NextDouble(), random.NextDouble()) }; surfaceLine.SetGeometry(points); } private static void AssertPropertiesUpdated(PipingSurfaceLine expectedSurfaceLine, PipingSurfaceLine actualSurfaceLine) { Assert.AreEqual(expectedSurfaceLine.Name, actualSurfaceLine.Name); TestHelper.AssertAreEqualButNotSame(expectedSurfaceLine.ReferenceLineIntersectionWorldPoint, actualSurfaceLine.ReferenceLineIntersectionWorldPoint); CollectionAssert.AreEqual(expectedSurfaceLine.Points, actualSurfaceLine.Points); TestHelper.AssertCollectionAreNotSame(expectedSurfaceLine.Points, actualSurfaceLine.Points); Point3D[] actualSurfaceLinePoints = actualSurfaceLine.Points; AssertAreEqualAndFromSurfaceLine(actualSurfaceLinePoints, expectedSurfaceLine.BottomDitchDikeSide, actualSurfaceLine.BottomDitchDikeSide); AssertAreEqualAndFromSurfaceLine(actualSurfaceLinePoints, expectedSurfaceLine.BottomDitchPolderSide, actualSurfaceLine.BottomDitchPolderSide); AssertAreEqualAndFromSurfaceLine(actualSurfaceLinePoints, expectedSurfaceLine.DikeToeAtPolder, actualSurfaceLine.DikeToeAtPolder); AssertAreEqualAndFromSurfaceLine(actualSurfaceLinePoints, expectedSurfaceLine.DikeToeAtRiver, actualSurfaceLine.DikeToeAtRiver); AssertAreEqualAndFromSurfaceLine(actualSurfaceLinePoints, expectedSurfaceLine.DitchPolderSide, actualSurfaceLine.DitchPolderSide); AssertAreEqualAndFromSurfaceLine(actualSurfaceLinePoints, expectedSurfaceLine.DitchDikeSide, actualSurfaceLine.DitchDikeSide); } private static void AssertAreEqualAndFromSurfaceLine(Point3D[] actualSurfaceLinePoints, Point3D expectedPoint, Point3D actualPoint) { Assert.AreEqual(expectedPoint, actualPoint); if (actualPoint != null) { Assert.IsTrue(actualSurfaceLinePoints.Contains(actualPoint, new ReferenceEqualityComparer())); } } } }