Index: Ringtoets/Integration/src/Ringtoets.Integration.Plugin/FileImporters/ReferenceLineImporter.cs =================================================================== diff -u -r925d8a1d7dec6ef95d89b2ff67d78c8d5a49387f -rb3d4bbd1f4cff028627bbf1591929a0b67bfccb9 --- Ringtoets/Integration/src/Ringtoets.Integration.Plugin/FileImporters/ReferenceLineImporter.cs (.../ReferenceLineImporter.cs) (revision 925d8a1d7dec6ef95d89b2ff67d78c8d5a49387f) +++ Ringtoets/Integration/src/Ringtoets.Integration.Plugin/FileImporters/ReferenceLineImporter.cs (.../ReferenceLineImporter.cs) (revision b3d4bbd1f4cff028627bbf1591929a0b67bfccb9) @@ -22,11 +22,13 @@ using System; using System.Collections.Generic; using System.Drawing; +using System.Linq; using System.Windows.Forms; using Core.Common.Base; using Core.Common.Base.IO; using Core.Common.IO.Exceptions; +using Core.Common.IO.Readers; using log4net; @@ -98,56 +100,36 @@ { ImportIsCancelled = false; changedObservables.Clear(); + bool clearReferenceLineDependentData = false; + var importTarget = (ReferenceLineContext)targetItem; if (importTarget.Parent.ReferenceLine != null) { - DialogResult result = MessageBox.Show(Resources.ReferenceLineImporter_ConfirmImport_Confirm_referenceline_import_which_clears_data_when_performed, - Resources.ReferenceLineImporter_ConfirmImport_DialogTitle, - MessageBoxButtons.OKCancel); - if (result == DialogResult.Cancel) - { - ImportIsCancelled = true; - } - else - { - clearReferenceLineDependentData = true; - } + clearReferenceLineDependentData = ConfirmImportOfReferenceLineToClearReferenceLineDependentData(); } if (ImportIsCancelled) { - NotifyProgress(Resources.ReferenceLineImporter_ProgressText_Import_cancelled_no_data_read, - 0, clearReferenceLineDependentData ? 4 : 2); + HandleUserCancellingImport(); return false; } - ReferenceLine importedReferenceLine; - try + NotifyProgress(Resources.ReferenceLineImporter_ProgressText_Reading_referenceline, + 1, clearReferenceLineDependentData ? 4 : 2); + ReadResult readResult = ReadReferenceLine(filePath); + if (readResult.CriticalErrorOccurred) { - NotifyProgress(Resources.ReferenceLineImporter_ProgressText_Reading_referenceline, - 1, clearReferenceLineDependentData ? 4 : 2); - importedReferenceLine = new ReferenceLineReader().ReadReferenceLine(filePath); - } - catch (ArgumentException e) - { - HandleCriticalFileReadError(e); return false; } - catch (CriticalFileReadException e) - { - HandleCriticalFileReadError(e); - return false; - } if (ImportIsCancelled) { - NotifyProgress(Resources.ReferenceLineImporter_ProgressText_Import_cancelled_no_data_read, - 1, clearReferenceLineDependentData ? 4 : 2); + HandleUserCancellingImport(); return false; } - AddReferenceLineToDataModel(importTarget.Parent, importedReferenceLine, clearReferenceLineDependentData); + AddReferenceLineToDataModel(importTarget.Parent, readResult.ImportedItems.First(), clearReferenceLineDependentData); return true; } @@ -156,6 +138,60 @@ return changedObservables; } + private bool ConfirmImportOfReferenceLineToClearReferenceLineDependentData() + { + var clearReferenceLineDependentData = false; + + DialogResult result = MessageBox.Show(Resources.ReferenceLineImporter_ConfirmImport_Confirm_referenceline_import_which_clears_data_when_performed, + Resources.ReferenceLineImporter_ConfirmImport_DialogTitle, + MessageBoxButtons.OKCancel); + if (result == DialogResult.Cancel) + { + ImportIsCancelled = true; + } + else + { + clearReferenceLineDependentData = true; + } + + return clearReferenceLineDependentData; + } + + private static void HandleUserCancellingImport() + { + log.Info(Resources.ReferenceLineImporter_ProgressText_Import_cancelled_no_data_read); + } + + private ReadResult ReadReferenceLine(string filePath) + { + try + { + return new ReadResult(false) + { + ImportedItems = new[] + { + new ReferenceLineReader().ReadReferenceLine(filePath) + } + }; + } + catch (ArgumentException e) + { + return HandleCriticalFileReadError(e); + } + catch (CriticalFileReadException e) + { + return HandleCriticalFileReadError(e); + } + } + + private static ReadResult HandleCriticalFileReadError(Exception e) + { + var errorMessage = String.Format(Resources.ReferenceLineImporter_HandleCriticalFileReadError_Error_0_no_referenceline_imported, + e.Message); + log.Error(errorMessage); + return new ReadResult(true); + } + private void AddReferenceLineToDataModel(AssessmentSectionBase assessmentSection, ReferenceLine importedReferenceLine, bool clearReferenceLineDependentData) { NotifyProgress(Resources.ReferenceLineImporter_ProgressText_Adding_imported_referenceline_to_assessmentsection, @@ -202,12 +238,5 @@ // TODO: WTI-360 - Clear all 'Toetspeil' calculation output //changedObservables.Add(clearedInstance); } - - private static void HandleCriticalFileReadError(Exception e) - { - var errorMessage = String.Format(Resources.ReferenceLineImporter_HandleCriticalFileReadError_Error_0_no_referenceline_imported, - e.Message); - log.Error(errorMessage); - } } } \ No newline at end of file Index: Ringtoets/Integration/test/Ringtoets.Integration.Plugin.Test/FileImporters/ReferenceLineImporterTest.cs =================================================================== diff -u -r925d8a1d7dec6ef95d89b2ff67d78c8d5a49387f -rb3d4bbd1f4cff028627bbf1591929a0b67bfccb9 --- Ringtoets/Integration/test/Ringtoets.Integration.Plugin.Test/FileImporters/ReferenceLineImporterTest.cs (.../ReferenceLineImporterTest.cs) (revision 925d8a1d7dec6ef95d89b2ff67d78c8d5a49387f) +++ Ringtoets/Integration/test/Ringtoets.Integration.Plugin.Test/FileImporters/ReferenceLineImporterTest.cs (.../ReferenceLineImporterTest.cs) (revision b3d4bbd1f4cff028627bbf1591929a0b67bfccb9) @@ -397,7 +397,7 @@ [Test] [TestCase(true)] [TestCase(false)] - public void Import_CancelImportDuringDialogInteraction_GenerateExpectedProgressMessages(bool acceptRemovalOfReferenceLineDependentData) + public void Import_CancelImportDuringDialogInteraction_GenerateCancelledLogMessage(bool acceptRemovalOfReferenceLineDependentData) { // Setup var originalReferenceLine = new ReferenceLine(); @@ -411,23 +411,8 @@ var referenceLineContext = new ReferenceLineContext(assessmentSection); - var expectedProgressMessages = new[] - { - new ExpectedProgressNotification { Text = "Referentielijn importeren afgebroken. Geen data ingelezen.", CurrentStep = 0, MaxNrOfSteps = acceptRemovalOfReferenceLineDependentData ? 4 : 2 } - }; + var importer = new ReferenceLineImporter(); - var progressChangedCount = 0; - var importer = new ReferenceLineImporter - { - ProgressChanged = (description, step, steps) => - { - Assert.AreEqual(expectedProgressMessages[progressChangedCount].Text, description); - Assert.AreEqual(expectedProgressMessages[progressChangedCount].CurrentStep, step); - Assert.AreEqual(expectedProgressMessages[progressChangedCount].MaxNrOfSteps, steps); - progressChangedCount++; - } - }; - DialogBoxHandler = (name, wnd) => { importer.Cancel(); @@ -444,10 +429,10 @@ }; // Call - importer.Import(referenceLineContext, path); + Action call = () => importer.Import(referenceLineContext, path); // Assert - Assert.AreEqual(expectedProgressMessages.Length, progressChangedCount); + TestHelper.AssertLogMessageIsGenerated(call, "Referentielijn importeren afgebroken. Geen data ingelezen.", 1); mocks.VerifyAll(); }