// Copyright (C) Stichting Deltares 2019. 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.General;
using Deltares.DamEngine.Io;
using Deltares.DamEngine.Io.XmlInput;
using Deltares.DamEngine.TestHelpers.Factories;
using KellermanSoftware.CompareNetObjects;
using NUnit.Framework;
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";
var input = DamXmlSerialization.LoadInputFromXmlFile(inputFileName);
Assert.AreEqual(28, input.AquiferSoils.Length);
DamProjectData actualDamProjectData = FillDamFromXmlInput.CreateDamProjectData(input);
Assert.AreEqual(11, actualDamProjectData.Dike.SoilList.AquiferDictionary.Count);
Assert.AreEqual(7, actualDamProjectData.CalculationMessages.Count);
}
[Test]
[TestCase("location_12_2_1D1")] // Between "location_12" and "_2_1D1" there are 2 illegal characters (1F hex)
[ExpectedException(typeof(IdValidatorException), ExpectedMessage = "Location has an invalid name location_12_2_1D1")]
public void GivenDataSetContainingIllegalCharactersWhenWritingXmlThenRaiseExceptionWithClearMessage(string id)
{
// Given DataSet Containing Illegal Characters
DamProjectData expectedDamProjectData = FactoryForDamProjectData.CreateExampleDamProjectData();
var location = expectedDamProjectData.Dike.Locations[0];
location.Name = 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]
[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();
var location = expectedDamProjectData.Dike.Locations[0];
location.Name = 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.AreEqual(FactoryForDamProjectData.ExpectedAnalysisType, DamProjectCalculationSpecification.SelectedAnalysisType);
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
var result = compare.Compare(expected, actual);
Assert.AreEqual(0, result.Differences.Count, "Differences found read/write Input object");
}
}
}