// Copyright (C) Stichting Deltares 2021. All rights reserved. // // This file is part of the application DAM - UI. // // DAM - UI 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 Deltares.Dam.Data; using Deltares.Dam.TestHelper.TestUtils; using Deltares.Geometry; using Deltares.Geotechnics.GeotechnicalGeometry; using Deltares.Geotechnics.Soils; using Deltares.Geotechnics.SurfaceLines; using NUnit.Framework; namespace Deltares.Dam.Tests { [TestFixture] public class EntityFactoryTest { private EntityFactory factory; private SoilProfile1D soilProfile; [TestFixtureSetUp] public void FixtureSetup() { soilProfile = FactoryForSoilProfileTests.CreateSimpleProfile(); soilProfile.Name = "1"; } [SetUp] public void TestSetup() { factory = new EntityFactory(); } [Test] [ExpectedException(typeof(ArgumentException))] public void ThrowsExceptionWhenSegmentIdIsNull() { factory.CreateSegment(null, "D1", 1, FailureMechanismSystemType.StabilityInside, null); } [Test] [ExpectedException(typeof(ArgumentException))] public void ThrowsExceptionWhenSegmentIdIsEmpty() { factory.CreateSegment("", "D1", 1, FailureMechanismSystemType.StabilityInside, null); } [Test] [ExpectedException(typeof(EntityFactoryException))] public void ThrowsExceptionWhenSoilProfileIdIsNull() { factory.CreateSegment("1", null, 1, FailureMechanismSystemType.StabilityInside, null); } [Test] [ExpectedException(typeof(EntityFactoryException))] public void ThrowsExceptionWhenSoilProfileIdIsEmpty() { factory.CreateSegment("1", "", 1, FailureMechanismSystemType.StabilityInside, null); } [Test] public void DoesntThrowExceptionWhenCollectionsAreEmpty() { factory = new EntityFactory(new List(), new List { soilProfile }, new List()); var segment = factory.CreateSegment("1", "1", 0, FailureMechanismSystemType.StabilityInside, null); Assert.IsNotNull(segment); } [Test] public void DoesntThrowExceptionWhenSegmentCollectionIsNull() { factory = new EntityFactory(new List(), new List { soilProfile }, null); var segment = factory.CreateSegment("1", "1", 0, FailureMechanismSystemType.StabilityInside, null); Assert.IsNotNull(segment); } [Test] public void DoesntThrowExceptionWhenSegmentCollectionsContainsNullItems() { factory = new EntityFactory(new List(), new List { soilProfile }, new List { null }); var segment = factory.CreateSegment("1", "1", 0, FailureMechanismSystemType.StabilityInside, null); Assert.IsNotNull(segment); } [Test] public void DoesntThrowExceptionWhenLocationCollectionIsNull() { factory = new EntityFactory(null, new List { soilProfile }, new List()); var segment = factory.CreateSegment("1", "1", 0, FailureMechanismSystemType.StabilityInside, null); Assert.IsNotNull(segment); } [Test] public void DoesntThrowExceptionWhenLocationCollectionContainsNullItems() { factory = new EntityFactory(new List { null }, new List { soilProfile }, new List()); var segment = factory.CreateSegment("1", "1", 0, FailureMechanismSystemType.StabilityInside, null); Assert.IsNotNull(segment); } [Test] public void CanCreateSegment() { factory = new EntityFactory(new List(), new List { soilProfile }, new List()); var segment = factory.CreateSegment("1", "1", 20, FailureMechanismSystemType.StabilityInside, null); Assert.AreEqual("1", segment.Name); Assert.AreEqual(20, segment.GetSoilProfileProbability(soilProfile, FailureMechanismSystemType.StabilityInside)); Assert.AreEqual("1", segment.GetMostProbableProfile(FailureMechanismSystemType.StabilityInside).Name); Assert.AreEqual(1, factory.SoilProfiles.Count()); } [Test] [ExpectedException(typeof(RequiredEntityNotExistException))] public void ThrowsWhenTheRequiredSurfaceLineDoesNotExistInLookupOnCreatingASurfaceLinePoint() { const string surfaceLineId = "D1"; factory.CreateSurfaceLinePoint(surfaceLineId, CharacteristicPointType.TrafficLoadOutside, 0, 0); } [Test] [ExpectedException(typeof(PointNotExistsException))] public void ThrowsExceptionWhenPointToAddDoesNotExistInSurfaceLinePoinList() { const string surfaceLineId = "D1"; using (var surfaceLine2 = new SurfaceLine2 { Name = surfaceLineId, Geometry = new LocalizedGeometryPointString(), CharacteristicPoints = { GeometryMustContainPoint = true } }) { var surfaceLines = new List { surfaceLine2 }; factory = new EntityFactory(surfaceLines); factory.CreateSurfaceLinePoint(surfaceLineId, CharacteristicPointType.TrafficLoadOutside, 0, 0); } } [Test] public void CanCreateSurfaceLinePoint() { const string surfaceLineId = "D1"; using (var surfaceLine2 = new SurfaceLine2 { Name = surfaceLineId, Geometry = new LocalizedGeometryPointString(), CharacteristicPoints = { GeometryMustContainPoint = true } }) { var surfaceLines = new List { surfaceLine2 }; surfaceLines[0].AddCharacteristicPoint(new GeometryPoint()); factory = new EntityFactory(surfaceLines); var point = factory.CreateSurfaceLinePoint(surfaceLineId, CharacteristicPointType.TrafficLoadOutside, 0, 0); Assert.IsNotNull(point); Assert.AreEqual(1, surfaceLines[0].Geometry.Points.Count); } } [Test] public void DoesntAdCharacteristicPointToSurfaceLineWhenPointIsInvalid() { const string surfaceLineId = "D1"; using (var surfaceLine2 = new SurfaceLine2 { Name = surfaceLineId, Geometry = new LocalizedGeometryPointString(), CharacteristicPoints = { GeometryMustContainPoint = true } }) { var surfaceLines = new List { surfaceLine2 }; factory = new EntityFactory(surfaceLines); var invalidPoint = new { X = -1, Z = -1 }; var point = factory.CreateSurfaceLinePoint(surfaceLineId, CharacteristicPointType.TrafficLoadOutside, invalidPoint.X, invalidPoint.Z); Assert.IsNull(point); Assert.AreEqual(0, surfaceLines[0].Geometry.Points.Count); } } [Test] public void DoesntThrowExceptionWhenSegmentAlreadyExist() { const string segmentId = "1"; var segment = new Segment { Name = segmentId }; factory = new EntityFactory( null, new List { soilProfile }, new List { segment }); factory.CreateSegment(segmentId, "1", 0, FailureMechanismSystemType.StabilityInside, null); } [Test] public void WhenCreatingASegmentExistingLocationsAreAdded() { const string exptectedLocationName = "SomeLocation"; using (var location = new Location { Segment = new Segment { Name = "1" }, Name = exptectedLocationName }) { factory = new EntityFactory(new List{location}, new List { soilProfile }, null); var segment = factory.CreateSegment("1", "1", 20, FailureMechanismSystemType.StabilityInside, null); Assert.AreEqual("1", segment.Name); Assert.AreEqual(1, factory.Locations.Count()); Assert.AreEqual(exptectedLocationName, factory.Locations[0].Name); Assert.AreEqual(20, segment.GetSoilProfileProbability(soilProfile, FailureMechanismSystemType.StabilityInside)); Assert.AreEqual("1", segment.GetMostProbableProfile(FailureMechanismSystemType.StabilityInside).Name); } } [Test] public void WhenCreatingASegmentExistingProfilesAreAdded() { const string exptectedName = "D1"; factory = new EntityFactory( null, new List { new SoilProfile1D { Name = exptectedName } }, null); var segment = factory.CreateSegment("1", "D1", 20, FailureMechanismSystemType.StabilityInside, null); Assert.AreEqual("1", segment.Name); Assert.AreEqual(1, factory.SoilProfiles.Count()); Assert.AreEqual(exptectedName, segment.GetMostProbableProfile(FailureMechanismSystemType.StabilityInside).Name); Assert.AreSame(factory.SoilProfiles.ElementAt(0), segment.GetMostProbableProfile(FailureMechanismSystemType.StabilityInside)); } [TestFixtureTearDown] public void FixtureTearDown() { } } }