// Copyright (C) Stichting Deltares 2017. 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 System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Xml.Linq;
using Deltares.DamEngine.Calculators.KernelWrappers.Common;
using Deltares.DamEngine.Calculators.KernelWrappers.DamMacroStability;
using Deltares.DamEngine.Calculators.KernelWrappers.DamMacroStability.Assemblers;
using Deltares.DamEngine.Data.General.Results;
using Deltares.DamEngine.Data.Standard.Logging;
using NUnit.Framework;
namespace Deltares.DamEngine.Calculators.Tests
{
[TestFixture]
public class DamMacroStabilityTests
{
[Test]
public void TestFullCalculation()
{
const double diff = 0.0001;
// ToDo zant Fill input
var damKernelInput = new DamKernelInput();
var kernelWrapper = new DamMacroStabilityKernelWrapper();
// Prepare the wrapper. Result is input for the calculation dll
var damStabilityInput = kernelWrapper.Prepare(damKernelInput);
// Validate the input
List messages;
kernelWrapper.Validate(damStabilityInput, out messages);
// Assert.AreEqual(0, messages.Count);
// Run the dll
// DamMacroStabilityOutput output = (DamMacroStabilityOutput)kernelWrapper.Execute(damStabilityInput, out messages);
// Assert.AreEqual(0, messages.Count);
// ToDo zant specify output
// Assert.AreEqual(0.0, output.xxx, diff);
// Fill the design results
// DesignResult result;
// kernelWrapper.PostProcess(output, out result);
// ToDo zant specify output
// Assert.AreEqual(0.0, result.StabilityDesignResults.SafetyFactor, diff);
}
[Test]
public void TestCreateDGeoStabilityInputFile()
{
const string testFolder = @"..\..\Deltares.DamEngine.Calculators.Tests\Files\MacroStability";
var xmlFileName = Path.Combine(testFolder, "test.xml");
var stiFileName = Path.Combine(testFolder, "test.sti");
var geometryFileName = Path.Combine(testFolder, "DWP_1.sti");
var soilDbName = Path.Combine(testFolder, "DAM Tutorial Design0.soilmaterials.mdb");
var expectedStiFileName = Path.Combine(testFolder, "expectedTest.sti");
if (File.Exists(stiFileName))
{
File.Delete(stiFileName);
}
// modify name of output sti file
XDocument xDocument = XDocument.Load(xmlFileName);
XElement inputElement = (from element in xDocument.Root.Descendants()
where element.Name.LocalName == DamMStabAssembler.XmlElementNameInput
select element).Single();
XAttribute mstabFileName = inputElement.Attribute(DamMStabAssembler.XmlAttributeMStabFileName);
Debug.Assert(mstabFileName != null, "mstabFileName != null");
mstabFileName.Value = stiFileName;
// modify name of Soil DB Name
XAttribute dbName = inputElement.Attribute(DamMStabAssembler.XmlAttributeSoilDBName);
Debug.Assert(dbName != null, "dbName != null");
dbName.Value = soilDbName;
// modify name of geometry input file
XElement geometryOptionsElement = (from element in inputElement.Descendants()
where element.Name.LocalName == DamMStabAssembler.XmlElementGeometryCreationOptions
select element).Single();
XAttribute geomFileName = geometryOptionsElement.Attribute(DamMStabAssembler.XmlAttributeSoilGeometry2DFilename);
Debug.Assert(geomFileName != null, "geomFileName != null");
geomFileName.Value = geometryFileName;
var kernelWrapper = new DamMacroStabilityKernelWrapper();
kernelWrapper.CreateStiFile(xDocument);
Assert.IsTrue(File.Exists(stiFileName));
Assert.AreEqual(ContentOfStiFile(expectedStiFileName), ContentOfStiFile(stiFileName));
}
[Test]
[ExpectedException(typeof(MacroStabilityException))]
public void TestThrowsExceptionXmlFileNotValid()
{
var kernelWrapper = new DamMacroStabilityKernelWrapper();
XDocument xDocument = new XDocument();
kernelWrapper.CreateStiFile(xDocument);
}
private string ContentOfStiFile(string stiFileName)
{
try
{
StreamReader stream = File.OpenText(stiFileName);
for (int i = 1; i <= 7; i++)
{
stream.ReadLine(); // skip first 7 lines with date, time and filename
}
string text = stream.ReadToEnd();
stream.Close();
return text;
}
catch
{
return null;
}
}
[Test]
public void TestValidate()
{
var kernelWrapper = new DamMacroStabilityKernelWrapper();
// Validate without setting values. Expected error messages.
var damStabilityInput = new DamMacroStabilityInput();
List messages;
//ToDo zant Validate is not implemented yet
// kernelWrapper.Validate(damStabilityInput, out messages);
// Assert.IsTrue(messages.Count > 0);
// Validate the input when valid input is provided. Expected no messages.
damStabilityInput = new DamMacroStabilityInput
{
// ToDo zant Fill input
};
// messages.Clear();
kernelWrapper.Validate(damStabilityInput, out messages);
Assert.AreEqual(0, messages.Count);
}
[Test]
public void TestPostProcess()
{
var kernelWrapper = new DamMacroStabilityKernelWrapper();
DamMacroStabilityOutput output = new DamMacroStabilityOutput();
// ToDo zant specify output
// output.xxx = 1.1;
// output.yyy = 2.2;
DesignResult result;
kernelWrapper.PostProcess(null, output, out result);
// ToDo zant specify output
// Assert.AreEqual(output.xxx, result.StabilityDesignResults.SafetyFactor);
}
[Test]
[ExpectedException(typeof(NoNullAllowedException), ExpectedMessage = "Geen invoer object gedefinieerd voor Macrostabiliteit")]
[SetUICulture("nl-NL")]
public void TestLanguageNLThrowsExceptionWhenInputIsNull()
{
DamMacroStabilityKernelWrapper.StabilityCalculator(null);
}
[Test]
[ExpectedException(typeof(NoNullAllowedException), ExpectedMessage = "No input object defined for Macro Stability")]
[SetUICulture("en-US")]
public void TestLanguageENThrowsExceptionWhenInputIsNull()
{
DamMacroStabilityKernelWrapper.StabilityCalculator(null);
}
[Test]
[ExpectedException(typeof(NoNullAllowedException), ExpectedMessage = "Geen uitvoer object gedefinieerd voor Macrostabiliteit")]
[SetUICulture("nl-NL")]
public void TestThrowsExceptionWhenOutputIsNull()
{
var kernelWrapper = new DamMacroStabilityKernelWrapper();
DesignResult result;
kernelWrapper.PostProcess(null, null, out result);
}
}
}