Index: Ringtoets/Common/test/Ringtoets.Common.IO.Test/SoilProfile/StochasticSoilModelImporterTest.cs =================================================================== diff -u -ra5f97617cc03e3535d369676ae09179c82947b17 -rd06750a4979861471df64a5f49dca99c33d34ce6 --- Ringtoets/Common/test/Ringtoets.Common.IO.Test/SoilProfile/StochasticSoilModelImporterTest.cs (.../StochasticSoilModelImporterTest.cs) (revision a5f97617cc03e3535d369676ae09179c82947b17) +++ Ringtoets/Common/test/Ringtoets.Common.IO.Test/SoilProfile/StochasticSoilModelImporterTest.cs (.../StochasticSoilModelImporterTest.cs) (revision d06750a4979861471df64a5f49dca99c33d34ce6) @@ -20,19 +20,26 @@ // All rights reserved. using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; using Core.Common.Base; using Core.Common.Base.IO; +using Core.Common.TestUtil; +using log4net.Core; using NUnit.Framework; using Rhino.Mocks; using Ringtoets.Common.Data; using Ringtoets.Common.IO.FileImporters.MessageProviders; using Ringtoets.Common.IO.SoilProfile; +using Ringtoets.Common.IO.SoilProfile.Schema; namespace Ringtoets.Common.IO.Test.SoilProfile { [TestFixture] public class StochasticSoilModelImporterTest { + private readonly string testDataPath = TestHelper.GetTestDataPath(TestDataPath.Ringtoets.Common.IO, "StochasticSoilModelReader"); private MockRepository mocks; private IStochasticSoilModelTransformer transformer; @@ -167,6 +174,202 @@ Assert.IsInstanceOf>>(importer); } + [Test] + public void Import_NonExistingFile_LogErrorReturnFalse() + { + // Setup + var messageProvider = mocks.Stub(); + var filter = mocks.Stub(); + var updateStrategy = mocks.Stub>(); + mocks.ReplayAll(); + + const string file = "nonexisting.soil"; + var collection = new TestStochasticSoilModelCollection(); + string validFilePath = Path.Combine(testDataPath, file); + var configuration = new StochasticSoilModelImporterConfiguration(transformer, filter, updateStrategy); + + var importer = new StochasticSoilModelImporter( + collection, + validFilePath, + messageProvider, + configuration); + + var progress = 0; + importer.SetProgressChanged((description, step, steps) => { progress++; }); + + var importResult = true; + + // Call + Action call = () => importResult = importer.Import(); + + // Assert + TestHelper.AssertLogMessagesWithLevelAndLoggedExceptions(call, messages => + { + Assert.AreEqual(1, messages.Count()); + Tuple expectedLog = messages.ElementAt(0); + + StringAssert.EndsWith($"{string.Empty} {Environment.NewLine}Het bestand wordt overgeslagen.", + expectedLog.Item1); + Assert.AreEqual(Level.Error, expectedLog.Item2); + Assert.IsInstanceOf(expectedLog.Item3); + }); + + Assert.AreEqual(1, progress); + Assert.IsFalse(importResult); + } + + [Test] + [TestCaseSource(typeof(InvalidPathHelper), nameof(InvalidPathHelper.InvalidPaths))] + public void Import_InvalidPath_LogErrorReturnFalse(string fileName) + { + // Setup + var messageProvider = mocks.Stub(); + var filter = mocks.Stub(); + var updateStrategy = mocks.Stub>(); + mocks.ReplayAll(); + + var collection = new TestStochasticSoilModelCollection(); + var configuration = new StochasticSoilModelImporterConfiguration(transformer, filter, updateStrategy); + + var importer = new StochasticSoilModelImporter( + collection, + fileName, + messageProvider, + configuration); + + var progress = 0; + importer.SetProgressChanged((description, step, steps) => { progress++; }); + + var importResult = true; + + // Call + Action call = () => importResult = importer.Import(); + + // Assert + TestHelper.AssertLogMessagesWithLevelAndLoggedExceptions(call, messages => + { + Assert.AreEqual(1, messages.Count()); + Tuple expectedLog = messages.ElementAt(0); + + StringAssert.EndsWith($"{string.Empty} {Environment.NewLine}Het bestand wordt overgeslagen.", + expectedLog.Item1); + Assert.AreEqual(Level.Error, expectedLog.Item2); + Assert.IsInstanceOf(expectedLog.Item3); + }); + + Assert.AreEqual(1, progress); + Assert.IsFalse(importResult); + } + + [Test] + [TestCase(FailureMechanismType.Piping, 3)] + [TestCase(FailureMechanismType.Stability, 3)] + [TestCase(FailureMechanismType.None, 0)] + public void Import_ImportingToValidTargetWithValidFile_ImportSoilModelToCollection( + FailureMechanismType failureMechanismType, + int nrOfFailureMechanismSpecificModelsInDatabase) + { + // Setup + string validFilePath = Path.Combine(testDataPath, "complete.soil"); + const int totalNrOfStochasticSoilModelInDatabase = 6; + + const string expectedAddDataText = "Adding Data"; + + var filter = mocks.StrictMock(); + filter.Expect(f => f.IsValidForFailureMechanism(null)) + .IgnoreArguments() + .Return(false) + .WhenCalled(invocation => { FilterFailureMechanismSpecificModel(invocation, failureMechanismType); }) + .Repeat + .Times(totalNrOfStochasticSoilModelInDatabase); + var messageProvider = mocks.StrictMock(); + var updateStrategy = mocks.StrictMock>(); + + if (nrOfFailureMechanismSpecificModelsInDatabase > 0) + { + messageProvider.Expect(mp => mp.GetAddDataToModelProgressText()) + .Return(expectedAddDataText); + + updateStrategy.Expect(u => u.UpdateModelWithImportedData(null, null)).IgnoreArguments().WhenCalled(invocation => + { + var soilModels = (IEnumerable) invocation.Arguments[0]; + var filePath = (string) invocation.Arguments[1]; + + Assert.AreEqual(nrOfFailureMechanismSpecificModelsInDatabase, soilModels.Count()); + Assert.AreEqual(validFilePath, filePath); + }); + } + + mocks.ReplayAll(); + + var progressChangeNotifications = new List(); + var importer = new StochasticSoilModelImporter( + new TestStochasticSoilModelCollection(), + validFilePath, + messageProvider, + new StochasticSoilModelImporterConfiguration( + transformer, + filter, + updateStrategy)); + + importer.SetProgressChanged((description, step, steps) => + progressChangeNotifications.Add(new ProgressNotification(description, step, steps))); + + // Call + bool importResult = importer.Import(); + + // Assert + Assert.IsTrue(importResult); + + var expectedProgressMessages = new List + { + new ProgressNotification("Inlezen van de D-Soil Model database.", 1, 1) + }; + for (var i = 1; i <= totalNrOfStochasticSoilModelInDatabase; i++) + { + expectedProgressMessages.Add(new ProgressNotification( + "Inlezen van de stochastische ondergrondmodellen.", i, totalNrOfStochasticSoilModelInDatabase)); + } + for (var i = 1; i <= nrOfFailureMechanismSpecificModelsInDatabase; i++) + { + expectedProgressMessages.Add(new ProgressNotification( + "Valideren van ingelezen data.", i, nrOfFailureMechanismSpecificModelsInDatabase)); + } + + if (nrOfFailureMechanismSpecificModelsInDatabase > 0) + { + expectedProgressMessages.Add(new ProgressNotification(expectedAddDataText, 1, 1)); + } + Assert.AreEqual(expectedProgressMessages.Count, progressChangeNotifications.Count); + for (var i = 0; i < expectedProgressMessages.Count; i++) + { + ProgressNotification notification = expectedProgressMessages[i]; + ProgressNotification actualNotification = progressChangeNotifications[i]; + Assert.AreEqual(notification.Text, actualNotification.Text); + Assert.AreEqual(notification.CurrentStep, actualNotification.CurrentStep); + Assert.AreEqual(notification.TotalSteps, actualNotification.TotalSteps); + } + } + + private static void FilterFailureMechanismSpecificModel(MethodInvocation invocation, FailureMechanismType failureMechanismType) + { + invocation.ReturnValue = failureMechanismType == ((StochasticSoilModel) invocation.Arguments[0]).FailureMechanismType; + } + + private class ProgressNotification + { + public ProgressNotification(string description, int currentStep, int totalSteps) + { + Text = description; + CurrentStep = currentStep; + TotalSteps = totalSteps; + } + + public string Text { get; } + public int CurrentStep { get; } + public int TotalSteps { get; } + } + private class TestStochasticSoilModelCollection : ObservableUniqueItemCollectionWithSourcePath { public TestStochasticSoilModelCollection()