// Copyright (C) Stichting Deltares 2023. All rights reserved. // // This file is part of the application DAM - Live. // // DAM - Live 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.Dam.Data; using Deltares.DamLive.Application; using Deltares.DamLive.Data; using Deltares.DamLive.Io; using NUnit.Framework; namespace Deltares.DamLive.Tests; [TestFixture] public class DamEngineRunnerTest { /// /// Just calls the internal process method to test the core / inner workings /// of the ModelRunner, without the need to read in files and write them back /// to output files /// /// Scenario: Empty input data is used to see if the runner can run without /// an exception /// [Test] public void Process_EmptyInputData_ShouldRunWithoutException() { damProjectData.Dispose(); damProjectData = new DamProjectData(); runner.CalculationParameters = null; runner.DamProjectData = damProjectData; runner.InputTimeSeriesCollection = new TimeSerieCollection(); runner.OutputTimeSeriesCollection = new TimeSerieCollection(); runner.Run(); AssertNoErrors(); } /// /// Just calls the internal process method to test the core / inner workings /// of the ModelRunner, without the need to read in files and write them back /// to output files /// /// Scenario: One dike has been used to test the calculation, see /// [Test] public void Process_DefaultTestData_ShouldRunWithoutException() { runner.Run(); AssertNoErrors(); } /// /// Just calls the runner's Run method. /// This is an example to run the model from a FEWS/console client. /// This test is an integration test. /// /// Required files are: /// - the DAMX file which contains the dike models /// - FEWS input file containing the water level time series for the locations /// or monitoring points /// - FEWS output file containing the time series results for each node /// (location/monitoring point) /// [Test] [Category("Integration")] public void Run_UsingDefaultTestFiles_ShouldRunWithoutExceptionAndContainResults() { runner = new DamEngineRunner { ParametersFile = new FileInfo(paramFile), DamXFile = new FileInfo(projectFile), FewsOutputFile = new FileInfo(outputFile), FewsInputFile = new FileInfo(inputFile) }; runner.Run(); AssertNoErrors(); Assert.That(runner.OutputTimeSeriesCollection.Series, Is.Not.Empty); } [Test] public void Run_NoDamXFile_Throws() { Assert.That(() => new DamEngineRunner().Run(), Throws.InvalidOperationException.With.Message.EqualTo(DamEngineRunner.NoDamxFile)); } [Test] public void Run_NoOutputFile_Throws() { Assert.That(() => new DamEngineRunner { DamXFile = new FileInfo(projectFile), FewsInputFile = new FileInfo(inputFile) }.Run(), Throws.InvalidOperationException.With.Message.EqualTo(DamEngineRunner.NoFewsOutputFileAvailable)); } [Test] public void Run_NoInputFile_Throws() { Assert.That(() => new DamEngineRunner { DamXFile = new FileInfo(projectFile), FewsOutputFile = new FileInfo(outputFile) }.Run(), Throws.InvalidOperationException.With.Message.EqualTo(DamEngineRunner.NoFewsInputFileAvailable)); } #region Test Setup private const string testWorkingFolder = "LiveDikeTestWorkingFolder"; private const string testDataFolder = @"TestData\IntegrationTests\StabilityInsideBishopGrid"; private string inputFile; private string outputFile; private string projectFile; private string paramFile; private DamEngineRunner runner; private DamProjectData damProjectData; [SetUp] public void SetupFixture() { paramFile = Path.Combine(testDataFolder, "live.ParametersFile.xml"); inputFile = Path.Combine(testDataFolder, "live.InputTimeSeriesMHW.xml"); outputFile = Path.Combine(testDataFolder, "live.OutputTimeSeriesMHW.xml"); projectFile = Path.Combine(testDataFolder, "DAMLive.damx"); RemoveTestWorkingDirectory(); // to be sure no test directory exist from previous tests Directory.CreateDirectory(testWorkingFolder); } [TearDown] public void TearDownFixture() { RemoveTestWorkingDirectory(); } [TearDown] public void TearDownTest() { damProjectData.Dispose(); AssertNoErrors(); } [SetUp] public void SetupTest() { // create a random unique test directory name in the // test working folder string actualTestPath = Guid.NewGuid().ToString().Replace("-", ""); actualTestPath = Path.Combine(testWorkingFolder, actualTestPath); damProjectData = new DamProjectData(); DamProject.SetTestProgramVersion("19.1"); runner = new DamEngineRunner { WorkingPath = actualTestPath, StabilityWorkingPath = "stability", PipingWorkingPath = "piping", WaterLevelOffset = 0, CalculationParameters = new CalculationParameters { CalculationModules = new CalculationModules(), StabilityParameters = new MStabParameters() }, FewsInputFile = new FileInfo(inputFile), FewsOutputFile = new FileInfo(outputFile), DamProjectData = damProjectData, InputTimeSeriesCollection = new TimeSerieCollection(), OutputTimeSeriesCollection = new TimeSerieCollection() }; // prepare the working folders //runner.CreateAndSetWorkingDirectories(); //runner.DeleteFormerProjectFiles(); } #endregion #region Helper methods /// /// Removes the test working directory. The working directory grows! /// private static void RemoveTestWorkingDirectory() { if (Directory.Exists(testWorkingFolder)) { const bool recursive = true; Directory.Delete(testWorkingFolder, recursive); } } /// /// Asserts that there are no errors in the runner /// private void AssertNoErrors() { if (runner.HasErrors) { Assert.Fail("The test failed. See the error log in the test output console for more info"); } } #endregion }