// Copyright (C) Stichting Deltares 2019. All rights reserved. // // This file is part of the application DAM - Live. // // DAM - UI is free software: you can redistribute it and/or modify // it under the terms of the GNU 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 General Public License for more details. // // You should have received a copy of the GNU 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.Application.Live; using Deltares.Dam.Data; using NUnit.Framework; namespace Deltares.DamLive.Tests { [TestFixture] public class ModelRunnerTest { #region Test Setup const string TestWorkingFolder = "LiveDikeTestWorkingFolder"; private const string TestDataFolder = @"TestData\DamLive\Set1"; private string inputFile; private string outputFile; private string projectFile; private string paramFile; private ModelRunner runner; private DamProjectData damProjectData; [TestFixtureSetUp] public void SetupFixture() { paramFile = Path.Combine(TestDataFolder, "Deltares.Dam.Application.Live.Tests.ParametersFile.xml"); inputFile = Path.Combine(TestDataFolder, "Deltares.Dam.Application.Live.Tests.InputFile.xml"); outputFile = Path.Combine(TestDataFolder, "Deltares.Dam.Application.Live.Tests.OutputFile.xml"); projectFile = Path.Combine(TestDataFolder, "Deltares.Dam.Application.Live.Tests.ProjectFile.damx"); RemoveTestWorkingDirectory(); // to be sure no test directory exist from previous tests Directory.CreateDirectory(TestWorkingFolder); } [TestFixtureTearDown] 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 var actualTestPath = Guid.NewGuid().ToString().Replace("-", ""); actualTestPath = Path.Combine(TestWorkingFolder, actualTestPath); damProjectData = new DamProjectData(); DamProject.SetTestProgramVersion("19.1"); runner = new ModelRunner { WorkingPath = actualTestPath, StabilityWorkingPath = "stability", PipingWorkingPath = "piping", WaterLevelOffset = 0, CalculationParameters = new CalculationParameters { CalculationModules = new CalculationModules(), MStabParameters = new MStabParameters() }, ProjectData = 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 /// /// 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.ProjectData = damProjectData; runner.InputTimeSeriesCollection = new TimeSerieCollection(); runner.OutputTimeSeriesCollection = new TimeSerieCollection(); runner.Process(); } /// /// 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.Process(); } /// /// 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] public void Run_UsingDefaultTestFiles_ShouldRunWithoutException() { runner = new ModelRunner { ParametersFile = new FileInfo(paramFile), DamXFile = new FileInfo(projectFile), FewsOutputFile = new FileInfo(outputFile), FewsInputFile = new FileInfo(inputFile) }; runner.Run(); AssertNoErrors(); } [Test] [ExpectedException(typeof(InvalidOperationException), ExpectedMessage = ModelRunner.NoDamxFile)] public void Run_NoDamXFile_Throws() { (new ModelRunner()).Run(); } [Test] [ExpectedException(typeof(InvalidOperationException), ExpectedMessage = ModelRunner.NoFewsOutputFileAvailable)] public void Run_NoOutputFile_Throws() { (new ModelRunner() { DamXFile = new FileInfo(projectFile), FewsInputFile = new FileInfo(inputFile) }).Run(); } [Test] [ExpectedException(typeof(InvalidOperationException), ExpectedMessage = ModelRunner.NoFewsInputFileAvailable)] public void Run_NoInputFile_Throws() { (new ModelRunner() { DamXFile = new FileInfo(projectFile), FewsOutputFile = new FileInfo(outputFile) }).Run(); } [Test, Ignore("Under construction")] public void Process_UsingDefaultTestData_OutputCollectionShouldContainResults() { damProjectData.Dispose(); DamProject.SetTestProgramVersion("19.1"); damProjectData = DamProject.LoadData(projectFile); runner.CalculationParameters = CalculationParameters.LoadFromFile(paramFile); runner.ProjectData = damProjectData; runner.InputTimeSeriesCollection = TimeSerieCollection.LoadFromFile(inputFile); runner.OutputTimeSeriesCollection = new TimeSerieCollection(); // runner.RunnerDelegate = new LegacyModelRunner(); runner.Process(); Assert.IsNotEmpty(runner.OutputTimeSeriesCollection.Series); } } }