// Copyright (C) Stichting Deltares 2024. All rights reserved. // // This file is part of the Dam Engine. // // The Dam Engine is free software: you can redistribute it and/or modify // it under the terms of the GNU Affero 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 Affero General Public License for more details. // // You should have received a copy of the GNU Affero 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.Collections.Generic; using Deltares.DamEngine.Data.Design; using Deltares.DamEngine.Data.General; using Deltares.DamEngine.Data.Geotechnics; using Deltares.DamEngine.Io; using Deltares.DamEngine.Io.XmlInput; using Deltares.DamEngine.TestHelpers.Factories; using KellermanSoftware.CompareNetObjects; using NUnit.Framework; using Location = Deltares.DamEngine.Data.General.Location; using SoilProfile2D = Deltares.DamEngine.Io.XmlInput.SoilProfile2D; namespace Deltares.DamEngine.Interface.Tests; [TestFixture] public class FillDamFromXmlInputTests { [Test] public void CanWriteAndReadDamProjectDataToXml() { const string inputFilename = "InputFile.xml"; DamProjectData expectedDamProjectData = FactoryForDamProjectData.CreateExampleDamProjectData(); // Write input file Input input = FillXmlInputFromDam.CreateInput(expectedDamProjectData); DamXmlSerialization.SaveInputAsXmlFile(inputFilename, input); // Init static that is to be loaded with not expected value DamProjectCalculationSpecification.SelectedAnalysisType = FactoryForDamProjectData.NotExpectedAnalysisType; // Load input file input = DamXmlSerialization.LoadInputFromXmlFile(inputFilename); DamProjectData actualDamProjectData = FillDamFromXmlInput.CreateDamProjectData(input); CompareDamProjectData(actualDamProjectData, expectedDamProjectData); } [Test] public void CanWriteAndReadDamProjectDataToXmlString() { DamProjectData expectedDamProjectData = FactoryForDamProjectData.CreateExampleDamProjectData(); // Write input string string xmlString; Input input = FillXmlInputFromDam.CreateInput(expectedDamProjectData); xmlString = DamXmlSerialization.SaveInputAsXmlString(input); // Init static that is to be loaded with not expected value DamProjectCalculationSpecification.SelectedAnalysisType = FactoryForDamProjectData.NotExpectedAnalysisType; // Load input string input = DamXmlSerialization.LoadInputFromXmlString(xmlString); DamProjectData actualDamProjectData = FillDamFromXmlInput.CreateDamProjectData(input); CompareDamProjectData(actualDamProjectData, expectedDamProjectData); } [Test] [TestCase("location_12_2_1D1")] // Between "location_12" and "_2_1D1" there are 2 illegal characters (1F hex) public void GivenDataSetContainingIllegalCharactersWhenWritingXmlThenRaiseExceptionWithClearMessage(string id) { // Given DataSet Containing Illegal Characters DamProjectData expectedDamProjectData = FactoryForDamProjectData.CreateExampleDamProjectData(); Location location = expectedDamProjectData.Dike.Locations[0]; location.Name = id; // When Writing Xml // Then Raise Exception With Clear Message() Assert.That(() => FillXmlInputFromDam.CreateInput(expectedDamProjectData), Throws.InstanceOf().With.Message.EqualTo("Location has an invalid name location_12_2_1D1")); } [Test] [TestCase("ABCDEFGHIJLMNOPQRSTUVWXYZ")] [TestCase("A")] [TestCase("Z")] [TestCase("K")] [TestCase("JUSTATEST")] [TestCase("abcdefghijklmnopqrstuvwxyz")] [TestCase("a")] [TestCase("z")] [TestCase("k")] [TestCase("justatest")] [TestCase("01234567879")] [TestCase("0")] [TestCase("9")] [TestCase("5")] [TestCase("!#$%&()*+,-./")] [TestCase(":;<=>?@")] [TestCase(@"[\]^_`")] [TestCase("{|}~")] [TestCase("!")] public void GivenDataSetContainingIdWithLegalCharactersWhenWritingXmlThenSucceeds(string id) { // Given DataSet Containing Illegal Characters DamProjectData expectedDamProjectData = FactoryForDamProjectData.CreateExampleDamProjectData(); Location location = expectedDamProjectData.Dike.Locations[0]; location.Name = id; location.CurrentScenario.LocationName = id; foreach (DesignScenario scenario in location.Scenarios) { scenario.LocationName = id; } // When Writing Xml string xmlString; Input input = FillXmlInputFromDam.CreateInput(expectedDamProjectData); xmlString = DamXmlSerialization.SaveInputAsXmlString(input); // Then Raise Exception With Clear Message() input = DamXmlSerialization.LoadInputFromXmlString(xmlString); DamProjectData actualDamProjectData = FillDamFromXmlInput.CreateDamProjectData(input); CompareDamProjectData(actualDamProjectData, expectedDamProjectData); } [Test] public void GivenSoilLayer2DFromXml_WhenTransferredToDamObject_ThenOuterLoopIsClosed() { // Setup var inputSoilProfile2D = new SoilProfile2D { Layers2D = new SoilProfile2DLayer2D[1] }; inputSoilProfile2D.Layers2D[0] = new SoilProfile2DLayer2D { Surface = new SoilProfile2DLayer2DSurface { OuterLoop = new SoilProfile2DLayer2DSurfaceOuterPoint[4] } }; inputSoilProfile2D.Layers2D[0].Surface.OuterLoop[0] = new SoilProfile2DLayer2DSurfaceOuterPoint { X = 0, Z = 0 }; inputSoilProfile2D.Layers2D[0].Surface.OuterLoop[1] = new SoilProfile2DLayer2DSurfaceOuterPoint { X = 10, Z = 0 }; inputSoilProfile2D.Layers2D[0].Surface.OuterLoop[2] = new SoilProfile2DLayer2DSurfaceOuterPoint { X = 10, Z = -5 }; inputSoilProfile2D.Layers2D[0].Surface.OuterLoop[3] = new SoilProfile2DLayer2DSurfaceOuterPoint { X = 0, Z = -5 }; // Call var soilProfile2D = new Data.Geotechnics.SoilProfile2D(); FillDamFromXmlInput.AddLayers2D(inputSoilProfile2D, soilProfile2D, new SoilList()); // Assert Assert.Multiple(() => { Assert.That(soilProfile2D.Geometry.Surfaces, Has.Count.EqualTo(1)); Assert.That(soilProfile2D.Geometry.Surfaces[0].OuterLoop.CurveList, Has.Count.EqualTo(4)); Assert.That(soilProfile2D.Geometry.Surfaces[0].OuterLoop.CurveList[0].HeadPoint.X, Is.EqualTo(0)); Assert.That(soilProfile2D.Geometry.Surfaces[0].OuterLoop.CurveList[0].HeadPoint.Z, Is.EqualTo(0)); Assert.That(soilProfile2D.Geometry.Surfaces[0].OuterLoop.CurveList[0].EndPoint.X, Is.EqualTo(10)); Assert.That(soilProfile2D.Geometry.Surfaces[0].OuterLoop.CurveList[0].EndPoint.Z, Is.EqualTo(0)); Assert.That(soilProfile2D.Geometry.Surfaces[0].OuterLoop.CurveList[1].HeadPoint.X, Is.EqualTo(10)); Assert.That(soilProfile2D.Geometry.Surfaces[0].OuterLoop.CurveList[1].HeadPoint.Z, Is.EqualTo(0)); Assert.That(soilProfile2D.Geometry.Surfaces[0].OuterLoop.CurveList[1].EndPoint.X, Is.EqualTo(10)); Assert.That(soilProfile2D.Geometry.Surfaces[0].OuterLoop.CurveList[1].EndPoint.Z, Is.EqualTo(-5)); Assert.That(soilProfile2D.Geometry.Surfaces[0].OuterLoop.CurveList[2].HeadPoint.X, Is.EqualTo(10)); Assert.That(soilProfile2D.Geometry.Surfaces[0].OuterLoop.CurveList[2].HeadPoint.Z, Is.EqualTo(-5)); Assert.That(soilProfile2D.Geometry.Surfaces[0].OuterLoop.CurveList[2].EndPoint.X, Is.EqualTo(0)); Assert.That(soilProfile2D.Geometry.Surfaces[0].OuterLoop.CurveList[2].EndPoint.Z, Is.EqualTo(-5)); Assert.That(soilProfile2D.Geometry.Surfaces[0].OuterLoop.CurveList[3].HeadPoint.X, Is.EqualTo(0)); Assert.That(soilProfile2D.Geometry.Surfaces[0].OuterLoop.CurveList[3].HeadPoint.Z, Is.EqualTo(-5)); Assert.That(soilProfile2D.Geometry.Surfaces[0].OuterLoop.CurveList[3].EndPoint.X, Is.EqualTo(0)); Assert.That(soilProfile2D.Geometry.Surfaces[0].OuterLoop.CurveList[3].EndPoint.Z, Is.EqualTo(0)); }); } private void CompareDamProjectData(DamProjectData actual, DamProjectData expected) { Assert.That(DamProjectCalculationSpecification.SelectedAnalysisType, Is.EqualTo(FactoryForDamProjectData.ExpectedAnalysisType)); var compare = new CompareLogic { Config = { MaxDifferences = 100 } }; compare.Config.MembersToIgnore = new List { "Points", "MinGeometryPointsX", "MinGeometryPointsZ", "MaxGeometryPointsX", "MaxGeometryPointsZ", "CurveList", "Curves", "Surfaces", "SurfaceLine", "Loops", "Right" }; // TODO i.m.o the serializing of the geometry should be improved, so these ignores are not needed anymore ComparisonResult result = compare.Compare(expected, actual); Assert.That(result.Differences.Count, Is.EqualTo(0), "Differences found read/write Input object:" + result.DifferencesString); } }