// 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.Io;
using Deltares.DamEngine.Io.XmlInput;
using Deltares.DamEngine.TestHelpers.Factories;
using KellermanSoftware.CompareNetObjects;
using NUnit.Framework;
using Location = Deltares.DamEngine.Data.General.Location;
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]
public void CanAquiferListBeHandledOk()
{
const string inputFileName = @"TestFiles\AquiferListInputFile.xml";
Input input = DamXmlSerialization.LoadInputFromXmlFile(inputFileName);
Assert.That(input.AquiferSoils.Length, Is.EqualTo(28));
DamProjectData actualDamProjectData = FillDamFromXmlInput.CreateDamProjectData(input);
Assert.That(actualDamProjectData.Dike.SoilList.AquiferDictionary.Count, Is.EqualTo(11));
Assert.That(actualDamProjectData.CalculationMessages.Count, Is.EqualTo(7));
}
[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);
}
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"
}; // 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);
}
}