Index: Ringtoets/Common/test/Ringtoets.Common.IO.Test/FileImporters/CalculationConfigurationImporterTest.cs =================================================================== diff -u -rba686f1e57553487d9e0d2ce1e8547b55a162947 -re55a25791932bd0452d1f01e331794a57e4db15d --- Ringtoets/Common/test/Ringtoets.Common.IO.Test/FileImporters/CalculationConfigurationImporterTest.cs (.../CalculationConfigurationImporterTest.cs) (revision ba686f1e57553487d9e0d2ce1e8547b55a162947) +++ Ringtoets/Common/test/Ringtoets.Common.IO.Test/FileImporters/CalculationConfigurationImporterTest.cs (.../CalculationConfigurationImporterTest.cs) (revision e55a25791932bd0452d1f01e331794a57e4db15d) @@ -22,19 +22,24 @@ using System; using System.Collections.Generic; using System.IO; +using System.Linq; using System.Xml.Linq; +using Core.Common.Base; using Core.Common.Base.IO; using Core.Common.TestUtil; using NUnit.Framework; using Ringtoets.Common.Data.Calculation; using Ringtoets.Common.IO.FileImporters; using Ringtoets.Common.IO.Readers; +using Ringtoets.Common.IO.Schema; namespace Ringtoets.Common.IO.Test.FileImporters { [TestFixture] public class CalculationConfigurationImporterTest { + private readonly string readerPath = TestHelper.GetTestDataPath(TestDataPath.Ringtoets.Common.IO, "ConfigurationReader"); + [Test] public void Constructor_ExpectedValues() { @@ -47,50 +52,118 @@ } [Test] - public void Import_CancelingImport_CancelImportAndLog() + public void Import_FilePathIsDirectory_CancelImportWithErrorMessage() { // Setup - var importer = new TestCalculationConfigurationImporter("", + string filePath = TestHelper.GetTestDataPath(TestDataPath.Ringtoets.Common.IO, Path.DirectorySeparatorChar.ToString()); + + var importer = new TestCalculationConfigurationImporter(filePath, new CalculationGroup()); - importer.SetProgressChanged((description, step, steps) => { importer.Cancel(); }); + // Call + var importSuccessful = true; + Action call = () => importSuccessful = importer.Import(); + // Assert + string expectedMessage = $"Fout bij het lezen van bestand '{filePath}': bestandspad mag niet verwijzen naar een lege bestandsnaam. " + Environment.NewLine + + "Er is geen berekeningenconfiguratie geïmporteerd."; + TestHelper.AssertLogMessageIsGenerated(call, expectedMessage, 1); + Assert.IsFalse(importSuccessful); + } + + [Test] + public void Import_FileDoesNotExist_CancelImportWithErrorMessage() + { + // Setup + string filePath = TestHelper.GetTestDataPath(TestDataPath.Ringtoets.Common.IO, "I_dont_exist"); + + var importer = new TestCalculationConfigurationImporter(filePath, + new CalculationGroup()); + // Call - Action call = () => importer.Import(); + var importSuccessful = true; + Action call = () => importSuccessful = importer.Import(); // Assert - TestHelper.AssertLogMessageIsGenerated(call, "Berekeningenconfiguratie importeren afgebroken. Geen data ingelezen.", 1); + string expectedMessage = $"Fout bij het lezen van bestand '{filePath}': het bestand bestaat niet. " + Environment.NewLine + + "Er is geen berekeningenconfiguratie geïmporteerd."; + TestHelper.AssertLogMessageIsGenerated(call, expectedMessage, 1); + Assert.IsFalse(importSuccessful); } - private class TestCalculationConfigurationImporter : CalculationConfigurationImporter + [Test] + public void Import_InvalidFile_CancelImportWithErrorMessage() { - public TestCalculationConfigurationImporter(string filePath, CalculationGroup importTarget) - : base(filePath, importTarget) {} + // Setup + string filePath = Path.Combine(readerPath, "invalidFolderNoName.xml"); + var importer = new TestCalculationConfigurationImporter(filePath, + new CalculationGroup()); - protected override bool OnImport() + // Call + var importSuccessful = true; + Action call = () => importSuccessful = importer.Import(); + + // Assert + TestHelper.AssertLogMessages(call, messages => { - NotifyProgress("Progress", 1, 1); + var msgs = messages.ToArray(); + Assert.AreEqual(1, msgs.Length); + StringAssert.StartsWith($"Fout bij het lezen van bestand '{filePath}': het XML-document dat de configuratie voor de berekeningen beschrijft is niet geldig.", msgs[0]); + }); - if (Canceled) + Assert.IsFalse(importSuccessful); + } + + [Test] + [TestCase("Inlezen")] + [TestCase("Valideren")] + public void Import_CancelingImport_CancelImportAndLog(string expectedProgressMessage) + { + // Setup + var calculationGroup = new CalculationGroup(); + + string filePath = Path.Combine(readerPath, "validConfiguration.xml"); + var importer = new TestCalculationConfigurationImporter(filePath, + calculationGroup); + + importer.SetProgressChanged((description, step, steps) => + { + if (description.Contains(expectedProgressMessage)) { - return false; + importer.Cancel(); } + }); - return true; - } + // Call + var importSuccessful = true; + Action call = () => importSuccessful = importer.Import(); + // Assert + TestHelper.AssertLogMessageIsGenerated(call, "Berekeningenconfiguratie importeren afgebroken. Geen data ingelezen.", 1); + CollectionAssert.IsEmpty(calculationGroup.Children); + Assert.IsFalse(importSuccessful); + } + + private class TestCalculationConfigurationImporter : CalculationConfigurationImporter + { + public TestCalculationConfigurationImporter(string filePath, CalculationGroup importTarget) + : base(filePath, importTarget) {} + protected override TestConfigurationReader CreateConfigurationReader(string xmlFilePath) { return new TestConfigurationReader(xmlFilePath); } - protected override ICalculationBase ParseReadCalculation(TestReadConfigurationItem readCalculation) + protected override ICalculationBase ParseReadCalculation(ReadCalculation readCalculation) { - throw new NotImplementedException(); + return new TestCalculation + { + Name = readCalculation.Name + }; } } - private class TestConfigurationReader : ConfigurationReader + private class TestConfigurationReader : ConfigurationReader { private static readonly string mainSchemaDefinition = File.ReadAllText(Path.Combine(TestHelper.GetTestDataPath( @@ -101,15 +174,25 @@ public TestConfigurationReader(string xmlFilePath) : base(xmlFilePath, mainSchemaDefinition, new Dictionary()) {} - protected override TestReadConfigurationItem ParseCalculationElement(XElement calculationElement) + protected override ReadCalculation ParseCalculationElement(XElement calculationElement) { - throw new NotImplementedException(); + return new ReadCalculation(calculationElement.Attribute(ConfigurationSchemaIdentifiers.NameAttribute)?.Value); } } - private class TestReadConfigurationItem : IReadConfigurationItem + private class ReadCalculation : IReadConfigurationItem { + public ReadCalculation(string name) + { + Name = name; + } + public string Name { get; } } + + private class TestCalculation : Observable, ICalculationBase + { + public string Name { get; set; } + } } } \ No newline at end of file Index: Ringtoets/Common/test/Ringtoets.Common.IO.Test/Readers/ConfigurationReaderTest.cs =================================================================== diff -u -r1b9445050ddc7786014349d7014c7c4d85242a5d -re55a25791932bd0452d1f01e331794a57e4db15d --- Ringtoets/Common/test/Ringtoets.Common.IO.Test/Readers/ConfigurationReaderTest.cs (.../ConfigurationReaderTest.cs) (revision 1b9445050ddc7786014349d7014c7c4d85242a5d) +++ Ringtoets/Common/test/Ringtoets.Common.IO.Test/Readers/ConfigurationReaderTest.cs (.../ConfigurationReaderTest.cs) (revision e55a25791932bd0452d1f01e331794a57e4db15d) @@ -27,7 +27,6 @@ using System.Xml.Linq; using System.Xml.Schema; using Core.Common.Base.IO; -using Core.Common.IO.Exceptions; using Core.Common.TestUtil; using NUnit.Framework; using Ringtoets.Common.IO.Readers; Index: Ringtoets/Piping/src/Ringtoets.Piping.IO/Importers/PipingConfigurationImporter.cs =================================================================== diff -u -rba686f1e57553487d9e0d2ce1e8547b55a162947 -re55a25791932bd0452d1f01e331794a57e4db15d --- Ringtoets/Piping/src/Ringtoets.Piping.IO/Importers/PipingConfigurationImporter.cs (.../PipingConfigurationImporter.cs) (revision ba686f1e57553487d9e0d2ce1e8547b55a162947) +++ Ringtoets/Piping/src/Ringtoets.Piping.IO/Importers/PipingConfigurationImporter.cs (.../PipingConfigurationImporter.cs) (revision e55a25791932bd0452d1f01e331794a57e4db15d) @@ -76,7 +76,7 @@ protected override PipingConfigurationReader CreateConfigurationReader(string xmlFilePath) { - return new PipingConfigurationReader(FilePath); + return new PipingConfigurationReader(xmlFilePath); } protected override ICalculationBase ParseReadCalculation(ReadPipingCalculation readCalculation) Index: Ringtoets/Piping/test/Ringtoets.Piping.IO.Test/Importers/PipingConfigurationImporterTest.cs =================================================================== diff -u -r94066add9735c5429a60d904e8618aae1139c7b8 -re55a25791932bd0452d1f01e331794a57e4db15d --- Ringtoets/Piping/test/Ringtoets.Piping.IO.Test/Importers/PipingConfigurationImporterTest.cs (.../PipingConfigurationImporterTest.cs) (revision 94066add9735c5429a60d904e8618aae1139c7b8) +++ Ringtoets/Piping/test/Ringtoets.Piping.IO.Test/Importers/PipingConfigurationImporterTest.cs (.../PipingConfigurationImporterTest.cs) (revision e55a25791932bd0452d1f01e331794a57e4db15d) @@ -84,106 +84,6 @@ } [Test] - public void Import_FilePathIsDirectory_CancelImportWithErrorMessage() - { - // Setup - string filePath = TestHelper.GetTestDataPath(TestDataPath.Ringtoets.Piping.IO, Path.DirectorySeparatorChar.ToString()); - - var importer = new PipingConfigurationImporter(filePath, - new CalculationGroup(), - Enumerable.Empty(), - new PipingFailureMechanism()); - - // Call - var importSuccessful = true; - Action call = () => importSuccessful = importer.Import(); - - // Assert - string expectedMessage = $"Fout bij het lezen van bestand '{filePath}': bestandspad mag niet verwijzen naar een lege bestandsnaam. " + Environment.NewLine + - "Er is geen berekeningenconfiguratie geïmporteerd."; - TestHelper.AssertLogMessageIsGenerated(call, expectedMessage, 1); - Assert.IsFalse(importSuccessful); - } - - [Test] - public void Import_FileDoesNotExist_CancelImportWithErrorMessage() - { - // Setup - string filePath = TestHelper.GetTestDataPath(TestDataPath.Ringtoets.Piping.IO, "I_dont_exist"); - var importer = new PipingConfigurationImporter(filePath, - new CalculationGroup(), - Enumerable.Empty(), - new PipingFailureMechanism()); - - // Call - var importSuccessful = true; - Action call = () => importSuccessful = importer.Import(); - - // Assert - string expectedMessage = $"Fout bij het lezen van bestand '{filePath}': het bestand bestaat niet. " + Environment.NewLine + - "Er is geen berekeningenconfiguratie geïmporteerd."; - TestHelper.AssertLogMessageIsGenerated(call, expectedMessage, 1); - Assert.IsFalse(importSuccessful); - } - - [Test] - public void Import_InvalidFile_CancelImportWithErrorMessage() - { - // Setup - string filePath = Path.Combine(readerPath, "invalidConfigurationCalculationContainingEmptyStrings.xml"); - var importer = new PipingConfigurationImporter(filePath, - new CalculationGroup(), - Enumerable.Empty(), - new PipingFailureMechanism()); - - // Call - var importSuccessful = true; - Action call = () => importSuccessful = importer.Import(); - - // Assert - TestHelper.AssertLogMessages(call, messages => - { - var msgs = messages.ToArray(); - Assert.AreEqual(1, msgs.Length); - StringAssert.StartsWith($"Fout bij het lezen van bestand '{filePath}': het XML-document dat de configuratie voor de berekeningen beschrijft is niet geldig.", msgs[0]); - }); - - Assert.IsFalse(importSuccessful); - } - - [Test] - [TestCase("Inlezen")] - [TestCase("Valideren")] - public void Import_CancelingImport_CancelImportAndLog(string expectedProgressMessage) - { - // Setup - var calculationGroup = new CalculationGroup(); - - string filePath = Path.Combine(importerPath, "validConfigurationNesting.xml"); - var importer = new PipingConfigurationImporter(filePath, - calculationGroup, - Enumerable.Empty(), - new PipingFailureMechanism()); - - importer.SetProgressChanged((description, step, steps) => - { - if (description.Contains(expectedProgressMessage)) - { - importer.Cancel(); - } - }); - - // Call - var importSuccessful = true; - Action call = () => importSuccessful = importer.Import(); - - // Assert - TestHelper.AssertLogMessageIsGenerated(call, "Berekeningenconfiguratie importeren afgebroken. Geen data ingelezen.", 1); - CollectionAssert.IsEmpty(calculationGroup.Children); - Assert.IsFalse(importSuccessful); - } - - [Test] public void GivenImport_WhenImporting_ThenExpectedProgressMessagesGenerated() { // Given