// Copyright (C) Stichting Deltares 2020. All rights reserved. // // This file is part of the Layer On Slope Tool. // // The Layer On Slope Tool 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.Collections.Generic; using System.IO; using Deltares.LayerOnSlopeTool.Data; using NUnit.Framework; namespace Deltares.LayerOnSlopeTool.StiFileCreator.Tests { [TestFixture] public class StiFileCreatorTests { private const string TestFileFolder = @"TestFiles\"; private static IEnumerable GetInvalidStiFileCreatorInput() { SurfaceLine validSurfaceLine = new SurfaceLine(); const string testFilesFolder = @"TestFiles\"; string nonExistingFilename = Path.Combine(testFilesFolder, "NonExistingFile.sti"); string existingFilename = Path.Combine(testFilesFolder, "Example.sti"); yield return new TestCaseData(null, null, null, "No input filename specified in StiFileCreatorInput"); yield return new TestCaseData("", null, null, "No input filename specified in StiFileCreatorInput"); yield return new TestCaseData(nonExistingFilename, null, null, @"File 'TestFiles\NonExistingFile.sti' not found"); yield return new TestCaseData(existingFilename, null, null, "Invalid surfaceline in StiFileCreatorInput"); yield return new TestCaseData(existingFilename, validSurfaceLine, null, "No dike material name specified in StiFileCreatorInput"); yield return new TestCaseData(existingFilename, validSurfaceLine, "", "No dike material name specified in StiFileCreatorInput"); } [Test] public void GivenNullInputWhenProcessingThenExceptionIsThrown() { Assert.That( // Given Null Input // When Processing () => StiFileCreator.ProcessFile(null), // Then Exception Is Thrown Throws.TypeOf().With.Message.EqualTo( "Value cannot be null." + Environment.NewLine + "Parameter name: stiFileCreatorInput")); } [Test] [TestCaseSource(nameof(GetInvalidStiFileCreatorInput))] public void GivenInvalidInputWhenProcessingThenExceptionIsThrown(string inputFilename, SurfaceLine surfaceLine, string dikeMaterialName, string errorMessage) { // Given Invalid Input var stiFileCreatorInput = new StiFileCreatorInput() { InputFilename = inputFilename, SurfaceLine = surfaceLine, DikeMaterialName = dikeMaterialName, XOffset = 0.0 }; Assert.That( // When Processing () => StiFileCreator.ProcessFile(stiFileCreatorInput), // Then Exception Is Thrown Throws.ArgumentException.With.Message.EqualTo(errorMessage)); } [Test] public void GivenValidInputWhenProcessingThenValidStiFileIsGenerated() { // Given Valid Input var surfaceLine = new SurfaceLine(); surfaceLine.SurfaceLinePoints.Add(new SurfaceLinePoint() { XCoordinate = -5.0, ZCoordinate = 0.4, PointType = CharacteristicPointType.SurfaceLevelOutside, }); surfaceLine.SurfaceLinePoints.Add(new SurfaceLinePoint() { XCoordinate = 2.0, ZCoordinate = 0.4, PointType = CharacteristicPointType.None }); surfaceLine.SurfaceLinePoints.Add(new SurfaceLinePoint() { XCoordinate = 3.0, ZCoordinate = 0.4, PointType = CharacteristicPointType.DikeToeAtRiver }); surfaceLine.SurfaceLinePoints.Add(new SurfaceLinePoint() { XCoordinate = 5.0, ZCoordinate = 2.5, PointType = CharacteristicPointType.DikeTopAtRiver }); surfaceLine.SurfaceLinePoints.Add(new SurfaceLinePoint() { XCoordinate = 7.0, ZCoordinate = 2.5, PointType = CharacteristicPointType.DikeTopAtPolder }); surfaceLine.SurfaceLinePoints.Add(new SurfaceLinePoint() { XCoordinate = 10.0, ZCoordinate = 0.5, PointType = CharacteristicPointType.DikeToeAtPolder }); surfaceLine.SurfaceLinePoints.Add(new SurfaceLinePoint() { XCoordinate = 12.0, ZCoordinate = 0.5, PointType = CharacteristicPointType.None }); surfaceLine.SurfaceLinePoints.Add(new SurfaceLinePoint() { XCoordinate = 16, ZCoordinate = 0.5, PointType = CharacteristicPointType.SurfaceLevelInside }); string outputFilename = Path.Combine(TestFileFolder, "ExampleOutput.sti"); var stiFileCreatorInput = new StiFileCreatorInput() { InputFilename = Path.Combine(TestFileFolder, "Example.sti"), OutputFilename = outputFilename, SurfaceLine = surfaceLine, DikeMaterialName = "Clay", XOffset = 0.0 }; // When Processing if (File.Exists(outputFilename)) { File.Delete(outputFilename); } StiFileCreator.ProcessFile(stiFileCreatorInput); // Then Valid Sti File Is Generated Assert.IsTrue(File.Exists(outputFilename), String.Format("File '{0}' has not been created", outputFilename)); } } }