// 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;
using System.IO;
using Deltares.DamEngine.Interface;
using Deltares.DamEngine.Io.XmlInput;
using Deltares.DamEngine.Io.XmlOutput;
using KellermanSoftware.CompareNetObjects;
using NUnit.Framework;
namespace Deltares.DamEngine.Io.Tests;
///
/// This class test just if the (generated) Input an Output class can be used for serializing to and from XML
/// A check, whether the dat itself is correctly written and read is done in
/// - FillDamFromXmlInputTests.CanWriteAndReadDamProjectDataToXml
/// - FillXmlOutputFromDamTests.CanWriteAndReadDamProjectDataToXml
///
[TestFixture]
public class DamXmlSerializationTests
{
[Test]
public void CanReadWriteInputObject()
{
const string filename = "Input.xml";
Input sourceInput = CreatePopulatedInput();
DamXmlSerialization.SaveInputAsXmlFile(filename, sourceInput);
Input destinationInput = DamXmlSerialization.LoadInputFromXmlFile(filename);
CompareInput(sourceInput, destinationInput);
}
[Test]
public void CanReadWriteOutputObject()
{
const string filename = "Output.xml";
Output sourceOutput = CreatePopulatedOutput();
DamXmlSerialization.SaveOutputAsXmlFile(filename, sourceOutput);
Output destinationOutput = DamXmlSerialization.LoadOutputFromXmlFile(filename);
CompareOutput(sourceOutput, destinationOutput);
}
[TestCase(1)]
[TestCase(2)]
public void Run_UsingTestFiles_ButWithUnsupportedPipingModelFails(int oldPipingModel)
{
// testcase 1 = old model PipingModelType.Sellmeijer4Forces
// testcase 2 = old model PipingModelType.SellmeijerVnk
const string calcDir = "TestOperationalGrebbedijk";
const string localWorkingDir = @".\";
const string baseTestDirectory = @".\";
if (Directory.Exists(calcDir))
{
Directory.Delete(calcDir, true); // delete previous results
}
Directory.CreateDirectory(calcDir);
// Switch to TestFiles directory to check if DamLive can also run from another directory
string oldLocalWorkingDir = Directory.GetCurrentDirectory();
try
{
Directory.SetCurrentDirectory(localWorkingDir);
// Based on "DamLive\trunk\src\Deltares.DamLive.Tests\TestData\DamLive\Set2\\output.damx", assume 4Forces
string inputFileName = baseTestDirectory + @"\TestFiles\GrebbedijkStability4Forces.xml";
if (oldPipingModel == 2)
{
inputFileName = baseTestDirectory + @"\TestFiles\GrebbedijkStabilityVNK.xml";
}
string inputString = File.ReadAllText(inputFileName);
Assert.That(() => new EngineInterface(inputString),
Throws.TypeOf(typeof(NotImplementedException)).With.Message.EqualTo("The method or operation is not implemented."));
}
finally
{
Directory.SetCurrentDirectory(oldLocalWorkingDir);
}
}
private Input CreatePopulatedInput()
{
var input = new Input();
input.DamProjectType = InputDamProjectType.Design;
return input;
}
private void CompareInput(Input expected, Input actual)
{
var compare = new CompareLogic
{
Config =
{
MaxDifferences = 100
}
};
ComparisonResult result = compare.Compare(expected, actual);
Assert.AreEqual(0, result.Differences.Count, "Differences found read/write Input object");
}
private Output CreatePopulatedOutput()
{
var output = new Output();
output.Results = new OutputResults();
output.Results.CalculationMessages = new Message[3];
output.Results.CalculationMessages[0] = new Message
{
MessageType = MessageMessageType.Error,
Message1 = "test1"
};
output.Results.CalculationMessages[1] = new Message
{
MessageType = MessageMessageType.Warning,
Message1 = "test2"
};
output.Results.CalculationMessages[2] = new Message
{
MessageType = MessageMessageType.Info,
Message1 = "test3"
};
return output;
}
private void CompareOutput(Output expected, Output actual)
{
var compare = new CompareLogic
{
Config =
{
MaxDifferences = 100
}
};
ComparisonResult result = compare.Compare(expected, actual);
Assert.AreEqual(0, result.Differences.Count, "Differences found read/write Output object");
}
}