// Copyright (C) Stichting Deltares 2020. 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.StiImporter; using NUnit.Framework; namespace Deltares.Dam.Tests.StiImporter { [TestFixture] public class StiFileValidatorTest { private const string testDataFolder = @"TestData\StiImporter\"; private static IEnumerable GetUnsupportedFileVersions() { yield return new TestCaseData("InvalidSoilVersion.sti", "Soil"); yield return new TestCaseData("InvalidGeometryVersion.sti", "Geometry"); yield return new TestCaseData("InvalidYieldStressVersionWithYieldStresses.sti", "Preconsolidation stress"); } [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_ThrowsStiFileImporterException() { // Setup var 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 var validationMessages = fileValidator.Validate(); // Assert CollectionAssert.IsEmpty(validationMessages); } [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 var messages = fileValidator.Validate(); // Assert CollectionAssert.IsEmpty(messages); } [Test] [TestCaseSource(nameof(GetUnsupportedFileVersions))] public void GivenValidatorWithUnsupportedFileVersions_WhenValidateFileVersionCalled_ThenStiFileImporterExceptionThrown( string fileName, string propertyName) { // Given string filePath = Path.Combine(testDataFolder, fileName); var fileValidator = new StiFileValidator(filePath); // When var messages = fileValidator.Validate(); // Then string expectedErrorMessage = $"{propertyName} data in '{filePath}' is of a version of D-Geo Stability that is not supported and cannot be read"; CollectionAssert.AreEqual(new[] { expectedErrorMessage }, messages); } [Test] [TestCase("GeoFile.geo")] [TestCase("SeepFile.SEI")] public void GivenValidatorWithUnsupportedFormat_WhenValidateFileFormatCalled_ThenStiFileImporterExceptionThrown(string fileName) { // Given string filePath = Path.Combine(testDataFolder, fileName); var fileValidator = new StiFileValidator(filePath); // When var messages = fileValidator.Validate(); // Then CollectionAssert.AreEqual(new[] { $"{filePath} is not a D-Geo Stability file (*.sti)." }, messages); } } }