Exemplo n.º 1
0
        public void ScenariosView_ChangeStructureOfCalculation_ChangesCorrectlyObservedAndSynced()
        {
            // Setup
            var mocks           = new MockRepository();
            var messageProvider = mocks.Stub <IImporterMessageProvider>();

            mocks.ReplayAll();

            using (var form = new Form())
            {
                var assessmentSection = new AssessmentSection(AssessmentSectionComposition.Dike);
                DataImportHelper.ImportReferenceLine(assessmentSection);
                HeightStructuresFailureMechanism failureMechanism = assessmentSection.HeightStructures;
                DataImportHelper.ImportFailureMechanismSections(assessmentSection, failureMechanism);

                var view = new HeightStructuresScenariosView(assessmentSection.HeightStructures.CalculationsGroup, assessmentSection.HeightStructures);
                form.Controls.Add(view);
                form.Show();

                var structuresImporter = new HeightStructuresImporter(assessmentSection.HeightStructures.HeightStructures,
                                                                      assessmentSection.ReferenceLine, filePath, messageProvider,
                                                                      new HeightStructureReplaceDataStrategy(failureMechanism));
                structuresImporter.Import();

                foreach (HeightStructure structure in assessmentSection.HeightStructures.HeightStructures)
                {
                    assessmentSection.HeightStructures.CalculationsGroup.Children.Add(new StructuresCalculationScenario <HeightStructuresInput>
                    {
                        Name            = NamingHelper.GetUniqueName(assessmentSection.HeightStructures.CalculationsGroup.Children, structure.Name + " Calculation", c => c.Name),
                        InputParameters =
                        {
                            Structure = structure
                        }
                    });
                }

                var listBox      = (ListBox) new ControlTester("listBox").TheObject;
                var dataGridView = (DataGridView) new ControlTester("dataGridView").TheObject;

                listBox.SelectedItem = failureMechanism.Sections.ElementAt(13);

                // Precondition
                DataGridViewRowCollection rows = dataGridView.Rows;
                Assert.AreEqual(1, rows.Count);
                Assert.AreEqual("Eerste kunstwerk 6-3 Calculation", rows[0].Cells[nameColumnIndex].FormattedValue);

                // Call
                CalculationGroup calculationsGroup = assessmentSection.HeightStructures.CalculationsGroup;
                ((StructuresCalculation <HeightStructuresInput>)calculationsGroup.Children[1]).InputParameters.Structure =
                    ((StructuresCalculation <HeightStructuresInput>)calculationsGroup.Children[0]).InputParameters.Structure;
                calculationsGroup.NotifyObservers();

                // Assert
                Assert.AreEqual(2, rows.Count);
                Assert.AreEqual("Eerste kunstwerk 6-3 Calculation", rows[0].Cells[nameColumnIndex].FormattedValue);
                Assert.AreEqual("Tweede kunstwerk 6-3 Calculation", rows[1].Cells[nameColumnIndex].FormattedValue);
            }

            mocks.VerifyAll();
        }
        public void Import_InvalidCsvFile_LogAndFalse()
        {
            // Setup
            var messageProvider = mocks.Stub <IImporterMessageProvider>();
            var strategy        = mocks.StrictMock <IStructureUpdateStrategy <HeightStructure> >();

            mocks.ReplayAll();

            var           importTarget  = new StructureCollection <HeightStructure>();
            ReferenceLine referenceLine = CreateReferenceLine();
            string        filePath      = Path.Combine(commonIoTestDataPath, "CorrectShpIncompleteCsv", "Kunstwerken.shp");

            var importer = new HeightStructuresImporter(importTarget, referenceLine, filePath,
                                                        messageProvider, strategy);

            var importResult = false;

            // Call
            Action call = () => importResult = importer.Import();

            // Assert
            string csvFilePath = Path.ChangeExtension(filePath, "csv");
            string message     = CreateExpectedErrorMessage(
                csvFilePath, "Coupure Den Oever (90k1)", "KUNST1",
                new[]
            {
                "De waarde voor parameter 'KW_HOOGTE1' op regel 2, kolom 'Numeriekewaarde', moet in het bereik [0,0, 360,0] liggen.",
                "Parameter 'KW_HOOGTE2' komt meerdere keren voor."
            });

            TestHelper.AssertLogMessageWithLevelIsGenerated(call, Tuple.Create(message, LogLevelConstant.Error), 1);
            Assert.IsFalse(importResult);
        }
        public void Import_ValidIncompleteFile_LogAndFalse()
        {
            // Setup
            var messageProvider = mocks.Stub <IImporterMessageProvider>();
            var strategy        = mocks.StrictMock <IStructureUpdateStrategy <HeightStructure> >();

            mocks.ReplayAll();

            var           importTarget  = new StructureCollection <HeightStructure>();
            ReferenceLine referenceLine = CreateReferenceLine();
            string        filePath      = Path.Combine(commonIoTestDataPath, "CorrectFiles", "Kunstwerken.shp");

            var importer = new HeightStructuresImporter(importTarget, referenceLine, filePath,
                                                        messageProvider, strategy);

            var importResult = false;

            // Call
            Action call = () => importResult = importer.Import();

            // Assert
            string csvFilePath = Path.ChangeExtension(filePath, "csv");
            string message     = CreateExpectedErrorMessage(csvFilePath, "Gemaal Leemans (93k3)", "KUNST2", new[]
            {
                "Geen geldige parameter definities gevonden."
            });

            TestHelper.AssertLogMessageWithLevelIsGenerated(call, Tuple.Create(message, LogLevelConstant.Error));
            Assert.IsFalse(importResult);
            Assert.AreEqual(0, importTarget.Count);
            Assert.IsNull(importTarget.SourcePath);
        }
        public void Import_MissingParametersAndDuplicateIrrelevantParameter_LogWarningAndContinueImport()
        {
            // Setup
            var    importTarget = new StructureCollection <HeightStructure>();
            string filePath     = Path.Combine(testDataPath, nameof(HeightStructuresImporter),
                                               "MissingAndDuplicateIrrelevantParameters", "Kunstwerken.shp");

            var messageProvider = mocks.Stub <IImporterMessageProvider>();
            var strategy        = mocks.StrictMock <IStructureUpdateStrategy <HeightStructure> >();

            strategy.Expect(s => s.UpdateStructuresWithImportedData(null, null)).IgnoreArguments()
            .WhenCalled(invocation =>
            {
                Assert.AreSame(invocation.Arguments[1], filePath);

                var readStructures = (IEnumerable <HeightStructure>)invocation.Arguments[0];
                Assert.AreEqual(1, readStructures.Count());
                HeightStructure structure = readStructures.First();
                var defaultStructure      = new HeightStructure(new HeightStructure.ConstructionProperties
                {
                    Name     = "test",
                    Location = new Point2D(0, 0),
                    Id       = "id"
                });
                Assert.AreEqual(defaultStructure.StructureNormalOrientation, structure.StructureNormalOrientation);
                DistributionAssert.AreEqual(defaultStructure.FlowWidthAtBottomProtection, structure.FlowWidthAtBottomProtection);
                Assert.AreEqual(defaultStructure.FailureProbabilityStructureWithErosion, structure.FailureProbabilityStructureWithErosion);
            })
            .Return(Enumerable.Empty <IObservable>());
            mocks.ReplayAll();

            ReferenceLine referenceLine = CreateReferenceLine();

            var importer = new HeightStructuresImporter(importTarget, referenceLine, filePath,
                                                        messageProvider, strategy);

            var importResult = false;

            // Call
            Action call = () => importResult = importer.Import();

            // Assert
            TestHelper.AssertLogMessages(call, msgs =>
            {
                string[] messages = msgs.ToArray();
                Assert.AreEqual(5, messages.Length);

                const string structure = "'Coupure Den Oever (90k1)' (KUNST1)";

                Assert.AreEqual($"Geen definitie gevonden voor parameter 'KW_HOOGTE1' van kunstwerk {structure}. Er wordt een standaard waarde gebruikt.", messages[0]);
                Assert.AreEqual($"Geen definitie gevonden voor parameter 'KW_HOOGTE3' van kunstwerk {structure}. Er wordt een standaard waarde gebruikt.", messages[1]);
                Assert.AreEqual($"Geen definitie gevonden voor parameter 'KW_HOOGTE6' van kunstwerk {structure}. Er wordt een standaard waarde gebruikt.", messages[2]);
                // Don't care about the other message.
            });
            Assert.IsTrue(importResult);
        }
        public void Import_ValidFileWithConversionsBetweenVarianceTypes_WarnUserAboutConversion()
        {
            // Setup
            var    importTarget = new StructureCollection <HeightStructure>();
            string filePath     = Path.Combine(testDataPath, "HeightStructuresVarianceConvert", "StructureNeedVarianceValueConversion.shp");

            var messageProvider = mocks.Stub <IImporterMessageProvider>();
            var strategy        = mocks.StrictMock <IStructureUpdateStrategy <HeightStructure> >();

            strategy.Expect(s => s.UpdateStructuresWithImportedData(null, null)).IgnoreArguments()
            .WhenCalled(invocation =>
            {
                Assert.AreSame(invocation.Arguments[1], filePath);

                var readStructures = (IEnumerable <HeightStructure>)invocation.Arguments[0];
                Assert.AreEqual(1, readStructures.Count());
                HeightStructure structure = readStructures.First();
                Assert.AreEqual(0.12, structure.LevelCrestStructure.StandardDeviation.Value);
                Assert.AreEqual(0.24, structure.FlowWidthAtBottomProtection.StandardDeviation.Value);
                Assert.AreEqual(1.0, structure.CriticalOvertoppingDischarge.CoefficientOfVariation.Value);
                Assert.AreEqual(0.97, structure.WidthFlowApertures.StandardDeviation.Value);
                Assert.AreEqual(1.84, structure.StorageStructureArea.CoefficientOfVariation.Value);
                Assert.AreEqual(2.18, structure.AllowedLevelIncreaseStorage.StandardDeviation.Value);
            })
            .Return(Enumerable.Empty <IObservable>());
            mocks.ReplayAll();

            ReferenceLine referenceLine = CreateReferenceLine();

            var importer = new HeightStructuresImporter(importTarget, referenceLine, filePath,
                                                        messageProvider, strategy);

            var importResult = false;

            // Call
            Action call = () => importResult = importer.Import();

            // Assert
            string[] expectedMessages =
            {
                "De variatie voor parameter 'KW_HOOGTE2' van kunstwerk 'Coupure Den Oever (90k1)' (KUNST1) wordt omgerekend in een standaardafwijking (regel 3).",
                "De variatie voor parameter 'KW_HOOGTE3' van kunstwerk 'Coupure Den Oever (90k1)' (KUNST1) wordt omgerekend in een standaardafwijking (regel 4).",
                "De variatie voor parameter 'KW_HOOGTE4' van kunstwerk 'Coupure Den Oever (90k1)' (KUNST1) wordt omgerekend in een variatiecoëfficiënt (regel 5).",
                "De variatie voor parameter 'KW_HOOGTE5' van kunstwerk 'Coupure Den Oever (90k1)' (KUNST1) wordt omgerekend in een standaardafwijking (regel 6).",
                "De variatie voor parameter 'KW_HOOGTE7' van kunstwerk 'Coupure Den Oever (90k1)' (KUNST1) wordt omgerekend in een variatiecoëfficiënt (regel 8).",
                "De variatie voor parameter 'KW_HOOGTE8' van kunstwerk 'Coupure Den Oever (90k1)' (KUNST1) wordt omgerekend in een standaardafwijking (regel 9)."
            };
            TestHelper.AssertLogMessagesAreGenerated(call, expectedMessages);
            Assert.IsTrue(importResult);
        }
        public void Constructor_Always_ExpectedValues()
        {
            // Setup
            var messageProvider = mocks.Stub <IImporterMessageProvider>();

            mocks.ReplayAll();

            var replaceDataStrategy = new HeightStructureReplaceDataStrategy(new HeightStructuresFailureMechanism());

            // Call
            var importer = new HeightStructuresImporter(testImportTarget, testReferenceLine, testFilePath,
                                                        messageProvider, replaceDataStrategy);

            // Assert
            Assert.IsInstanceOf <StructuresImporter <HeightStructure> >(importer);
        }
        public void Import_NoParameterIdsDefinedAndOnlyDuplicateUnknownParameterId_TrueAndImportTargetUpdated()
        {
            // Setup
            var    importTarget = new StructureCollection <HeightStructure>();
            string filePath     = Path.Combine(commonIoTestDataPath, "StructuresWithOnlyDuplicateIrrelevantParameterInCsv",
                                               "Kunstwerken.shp");

            var messageProvider = mocks.Stub <IImporterMessageProvider>();
            var updateStrategy  = mocks.Stub <IStructureUpdateStrategy <HeightStructure> >();

            mocks.ReplayAll();

            var referencePoints = new List <Point2D>
            {
                new Point2D(154493.618, 568995.991),
                new Point2D(156844.169, 574771.498),
                new Point2D(157910.502, 579115.458),
                new Point2D(163625.153, 585151.261)
            };
            var referenceLine = new ReferenceLine();

            referenceLine.SetGeometry(referencePoints);

            var structuresImporter = new HeightStructuresImporter(importTarget,
                                                                  referenceLine,
                                                                  filePath,
                                                                  messageProvider,
                                                                  updateStrategy);

            // Call
            var    importResult = false;
            Action call         = () => importResult = structuresImporter.Import();

            // Assert
            string csvFilePath = Path.ChangeExtension(filePath, "csv");
            string message     = CreateExpectedErrorMessage(csvFilePath, "Eerste kunstwerk 6-3", "KWK_1", new[]
            {
                "Geen geldige parameter definities gevonden."
            });

            TestHelper.AssertLogMessageWithLevelIsGenerated(call, Tuple.Create(message, LogLevelConstant.Error));
            Assert.IsFalse(importResult);
            Assert.AreEqual(0, importTarget.Count);
        }
        public void Import_CancelOfImportWhenAddingDataToModel_ImportCompletedSuccessfullyNonetheless()
        {
            // Setup
            string validFilePath = Path.Combine(testDataPath, nameof(HeightStructuresImporter),
                                                "MissingParameters", "Kunstwerken.shp");
            var importTarget = new StructureCollection <HeightStructure>();

            const string progressText    = "ProgressText";
            var          messageProvider = mocks.StrictMock <IImporterMessageProvider>();

            messageProvider.Expect(mp => mp.GetAddDataToModelProgressText()).Return(progressText);

            var strategy = mocks.StrictMock <IStructureUpdateStrategy <HeightStructure> >();

            strategy.Expect(s => s.UpdateStructuresWithImportedData(Arg <IEnumerable <HeightStructure> > .Is.NotNull,
                                                                    Arg.Is(validFilePath)))
            .Return(Enumerable.Empty <IObservable>());
            mocks.ReplayAll();

            ReferenceLine referenceLine = CreateReferenceLine();
            var           importer      = new HeightStructuresImporter(importTarget, referenceLine, validFilePath,
                                                                       messageProvider, strategy);

            importer.SetProgressChanged((description, step, steps) =>
            {
                if (description.Contains(progressText))
                {
                    importer.Cancel();
                }
            });

            var importResult = false;

            // Call
            Action call = () => importResult = importer.Import();

            // Assert
            Tuple <string, LogLevelConstant> expectedLogMessage = Tuple.Create(
                "Huidige actie was niet meer te annuleren en is daarom voortgezet.", LogLevelConstant.Warn);

            TestHelper.AssertLogMessageWithLevelIsGenerated(call, expectedLogMessage);
            Assert.IsTrue(importResult);
        }
        public void Import_ParameterIdsWithVaryingCase_TrueAndImportTargetUpdated()
        {
            // Setup
            var    importTarget = new StructureCollection <HeightStructure>();
            string filePath     = Path.Combine(commonIoTestDataPath, "CorrectShpRandomCaseHeaderCsv",
                                               "Kunstwerken.shp");

            var messageProvider = mocks.Stub <IImporterMessageProvider>();
            var strategy        = mocks.StrictMock <IStructureUpdateStrategy <HeightStructure> >();

            strategy.Expect(s => s.UpdateStructuresWithImportedData(null, null)).IgnoreArguments()
            .WhenCalled(invocation =>
            {
                Assert.AreSame(invocation.Arguments[1], filePath);

                var readStructures = (IEnumerable <HeightStructure>)invocation.Arguments[0];
                Assert.AreEqual(4, readStructures.Count());
            })
            .Return(Enumerable.Empty <IObservable>());
            mocks.ReplayAll();

            var referencePoints = new List <Point2D>
            {
                new Point2D(154493.618, 568995.991),
                new Point2D(156844.169, 574771.498),
                new Point2D(157910.502, 579115.458),
                new Point2D(163625.153, 585151.261)
            };
            var referenceLine = new ReferenceLine();

            referenceLine.SetGeometry(referencePoints);

            var importer = new HeightStructuresImporter(importTarget, referenceLine, filePath,
                                                        messageProvider, strategy);

            // Call
            var    importResult = false;
            Action call         = () => importResult = importer.Import();

            // Assert
            TestHelper.AssertLogMessageIsGenerated(call, $"Gegevens zijn geïmporteerd vanuit bestand '{filePath}'.", 13);
            Assert.IsTrue(importResult);
        }
        public void Import_AllParameterIdsDefinedAndDuplicateUnknownParameterId_TrueAndImportTargetUpdated()
        {
            // Setup
            var    importTarget = new StructureCollection <HeightStructure>();
            string filePath     = Path.Combine(commonIoTestDataPath, "StructuresWithDuplicateIrrelevantParameterInCsv",
                                               "Kunstwerken.shp");

            var messageProvider = mocks.Stub <IImporterMessageProvider>();
            var strategy        = mocks.StrictMock <IStructureUpdateStrategy <HeightStructure> >();

            strategy.Expect(s => s.UpdateStructuresWithImportedData(null, null)).IgnoreArguments()
            .WhenCalled(invocation =>
            {
                Assert.AreSame(invocation.Arguments[1], filePath);

                var readStructures = (IEnumerable <HeightStructure>)invocation.Arguments[0];
                Assert.AreEqual(1, readStructures.Count());
            })
            .Return(Enumerable.Empty <IObservable>());
            mocks.ReplayAll();

            var referencePoints = new List <Point2D>
            {
                new Point2D(154493.618, 568995.991),
                new Point2D(156844.169, 574771.498),
                new Point2D(157910.502, 579115.458),
                new Point2D(163625.153, 585151.261)
            };
            var referenceLine = new ReferenceLine();

            referenceLine.SetGeometry(referencePoints);

            var importer = new HeightStructuresImporter(importTarget, referenceLine, filePath,
                                                        messageProvider, strategy);

            // Call
            bool importResult = importer.Import();

            // Assert
            Assert.IsTrue(importResult);
        }
        public void Import_CancelOfImportWhenReadingStructureData_CancelsImportAndLogs()
        {
            // Setup
            const string cancelledLogMessage = "Operation Cancelled";
            var          messageProvider     = mocks.StrictMock <IImporterMessageProvider>();

            messageProvider.Expect(mp => mp.GetCancelledLogMessageText("Kunstwerken")).Return(cancelledLogMessage);

            var strategy = mocks.StrictMock <IStructureUpdateStrategy <HeightStructure> >();

            mocks.ReplayAll();

            string validFilePath = Path.Combine(commonIoTestDataPath, "CorrectFiles", "Kunstwerken.shp");

            var           importTarget  = new StructureCollection <HeightStructure>();
            ReferenceLine referenceLine = CreateReferenceLine();

            var importer = new HeightStructuresImporter(importTarget, referenceLine, validFilePath,
                                                        messageProvider, strategy);

            importer.SetProgressChanged(delegate(string description, int step, int steps)
            {
                if (description.Contains("Inlezen van kunstwerkgegevens uit een kommagescheiden bestand."))
                {
                    importer.Cancel();
                }
            });

            var importResult = true;

            // Call
            Action call = () => importResult = importer.Import();

            // Assert
            Tuple <string, LogLevelConstant> expectedLogMessage = Tuple.Create(cancelledLogMessage, LogLevelConstant.Info);

            TestHelper.AssertLogMessageWithLevelIsGenerated(call, expectedLogMessage);
            Assert.IsFalse(importResult);
        }
        public void DoPostImport_UpdateStrategyReturningObservables_AllObservablesNotified()
        {
            var messageProvider = mocks.Stub <IImporterMessageProvider>();

            var observableA = mocks.StrictMock <IObservable>();

            observableA.Expect(o => o.NotifyObservers());
            var observableB = mocks.StrictMock <IObservable>();

            observableB.Expect(o => o.NotifyObservers());
            IObservable[] observables =
            {
                observableA,
                observableB
            };

            var strategy = mocks.StrictMock <IStructureUpdateStrategy <HeightStructure> >();

            strategy.Expect(s => s.UpdateStructuresWithImportedData(null, null)).IgnoreArguments().Return(observables);
            mocks.ReplayAll();

            string validFilePath = Path.Combine(testDataPath, nameof(HeightStructuresImporter),
                                                "MissingParameters", "Kunstwerken.shp");

            var           importTarget  = new StructureCollection <HeightStructure>();
            ReferenceLine referenceLine = CreateReferenceLine();

            var importer = new HeightStructuresImporter(importTarget, referenceLine, validFilePath,
                                                        messageProvider, strategy);

            importer.Import();

            // Call
            importer.DoPostImport();

            // Assert
            // Assertions performed in TearDown
        }
Exemplo n.º 13
0
        public void ScenariosView_GenerateCalculations_ChangesCorrectlyObservedAndSynced()
        {
            // Setup
            var mocks           = new MockRepository();
            var messageProvider = mocks.Stub <IImporterMessageProvider>();

            mocks.ReplayAll();

            using (var form = new Form())
            {
                var assessmentSection = new AssessmentSection(AssessmentSectionComposition.Dike);
                DataImportHelper.ImportReferenceLine(assessmentSection);
                HeightStructuresFailureMechanism failureMechanism = assessmentSection.HeightStructures;
                DataImportHelper.ImportFailureMechanismSections(assessmentSection, failureMechanism);

                CalculationGroup calculationsGroup = assessmentSection.HeightStructures.CalculationsGroup;
                var view = new HeightStructuresScenariosView(calculationsGroup, assessmentSection.HeightStructures);

                form.Controls.Add(view);
                form.Show();

                var structuresImporter = new HeightStructuresImporter(assessmentSection.HeightStructures.HeightStructures,
                                                                      assessmentSection.ReferenceLine, filePath, messageProvider,
                                                                      new HeightStructureReplaceDataStrategy(failureMechanism));
                structuresImporter.Import();

                var listBox      = (ListBox) new ControlTester("listBox").TheObject;
                var dataGridView = (DataGridView) new ControlTester("dataGridView").TheObject;

                listBox.SelectedItem = failureMechanism.Sections.ElementAt(13);

                // Precondition
                DataGridViewRowCollection rows = dataGridView.Rows;
                CollectionAssert.IsEmpty(rows);

                // Call
                foreach (HeightStructure structure in assessmentSection.HeightStructures.HeightStructures)
                {
                    calculationsGroup.Children.Add(new StructuresCalculationScenario <HeightStructuresInput>
                    {
                        Name            = NamingHelper.GetUniqueName(((CalculationGroup)view.Data).Children, structure.Name, c => c.Name),
                        InputParameters =
                        {
                            Structure = structure
                        }
                    });
                }

                calculationsGroup.NotifyObservers();

                // Assert
                Assert.AreEqual(1, rows.Count);

                DataGridViewCellCollection cells = rows[0].Cells;
                Assert.AreEqual(4, cells.Count);
                Assert.IsTrue(Convert.ToBoolean(cells[isRelevantColumnIndex].FormattedValue));
                Assert.AreEqual(new RoundedDouble(2, 100).ToString(), cells[contributionColumnIndex].FormattedValue);
                Assert.AreEqual("Eerste kunstwerk 6-3", cells[nameColumnIndex].FormattedValue);
                Assert.AreEqual("-", cells[failureProbabilityColumnIndex].FormattedValue);
            }

            mocks.VerifyAll();
        }