// Copyright (C) Stichting Deltares 2024. All rights reserved. // // This file is part of the application DAM - UI. // // 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.Collections.Generic; using System.IO; using Deltares.Dam.Data.Geometry2DImporter; using NUnit.Framework; namespace Deltares.Dam.Tests.Geometry2DImporter { [TestFixture] public class StiFileValidatorTest { private const string TestDataFolder = @"TestData\StiImporter\"; [Test] [TestCase(null)] [TestCase("")] [TestCase(" ")] public void Constructor_FilePathNullOrEmpty_ThrowsArgumentException(string filePath) { // Call TestDelegate call = () => new StiFileValidator(filePath); // Assert Assert.That(call, Throws.ArgumentException .With.Message.EqualTo("'filePath' cannot be null or consist of whitespaces only.")); } [Test] public void Constructor_FileResultsInIOException_ThrowsStiFileReadException() { // Setup const string nonExistingFile = "DoesNotExist"; // Call TestDelegate call = () => new StiFileValidator(nonExistingFile); // Assert var exception = Assert.Throws(call); Exception innerException = exception.InnerException; Assert.That(innerException, Is.InstanceOf()); Assert.That(exception.Message, Is.EqualTo(innerException.Message)); } [Test] [TestCase("Tutorial-1a 10.1.4.3.sti")] [TestCase("Tutorial-1a 14.1.2.1.sti")] [TestCase("Tutorial-1a 16.1.2.1.sti")] public void GivenValidatorWithSupportedFileVersions_WhenValidateCalled_ThenNoValidationMessages(string fileName) { // Setup string filePath = Path.Combine(TestDataFolder, fileName); var fileValidator = new StiFileValidator(filePath); // Call IEnumerable validationMessages = fileValidator.Validate(); // Assert Assert.That(validationMessages, Is.Empty); } [Test] public void GivenValidatorWithSupportedFormat_WhenValidateFileFormatCalled_ThenNoValidationMessages() { // Setup string filePath = Path.Combine(TestDataFolder, "Tutorial-1a 10.1.4.3.sti"); var fileValidator = new StiFileValidator(filePath); // Call IEnumerable messages = fileValidator.Validate(); // Assert Assert.That(messages, Is.Empty); } [Test] [TestCaseSource(nameof(GetUnsupportedFileVersions))] public void GivenValidatorWithUnsupportedFileVersions_WhenValidateFileVersionCalled_ThenLogMessagesGenerated( string fileName, string propertyName) { // Given string filePath = Path.Combine(TestDataFolder, fileName); var fileValidator = new StiFileValidator(filePath); // When IEnumerable messages = fileValidator.Validate(); // Then var expectedErrorMessage = $"{propertyName} data in '{filePath}' is of a version of D-Geo Stability that is not supported and cannot be read."; Assert.That(messages, Is.EqualTo(new[] { expectedErrorMessage }).AsCollection); } [Test] [TestCase("GeoFile.geo")] [TestCase("SeepFile.SEI")] public void GivenValidatorWithUnsupportedFormat_WhenValidateFileFormatCalled_ThenLogMessagesGenerated(string fileName) { // Given string filePath = Path.Combine(TestDataFolder, fileName); var fileValidator = new StiFileValidator(filePath); // When IEnumerable messages = fileValidator.Validate(); // Then Assert.That(messages, Is.EqualTo(new[] { $"{filePath} is not a D-Geo Stability file (*.sti)." }).AsCollection); } private static IEnumerable GetUnsupportedFileVersions() { yield return new TestCaseData("InvalidSoilVersion.sti", "Soil"); yield return new TestCaseData("InvalidGeometryVersion.sti", "Geometry"); } } }