public void ChangeProbability_WithAction_ConfirmationRequired()
        {
            // Setup
            var title   = "";
            var message = "";

            DialogBoxHandler = (name, wnd) =>
            {
                var tester = new MessageBoxTester(wnd);
                title   = tester.Title;
                message = tester.Text;

                tester.ClickCancel();
            };

            var mocks             = new MockRepository();
            var assessmentSection = mocks.Stub <IAssessmentSection>();

            mocks.ReplayAll();

            var handler = new FailureMechanismContributionNormChangeHandler(assessmentSection);

            // Call
            handler.ChangeProbability(() => {});

            // Assert
            Assert.AreEqual("Bevestigen", title);
            string expectedMessage = "Als u de norm aanpast, dan worden de rekenresultaten van alle hydraulische belastingenlocaties behorende bij deze norm verwijderd."
                                     + Environment.NewLine
                                     + Environment.NewLine
                                     + "Weet u zeker dat u wilt doorgaan?";

            Assert.AreEqual(expectedMessage, message);
            mocks.VerifyAll();
        }
        public void GivenCalculationsWithoutOutput_WhenChangingProbability_ThenActionPerformedAndContributionNotified(
            NormativeProbabilityType normativeProbabilityType, Func <AssessmentSection, IEnumerable <HydraulicBoundaryLocationCalculation> > getLocationCalculationsFunc,
            WaveConditionsInputWaterLevelType waterLevelType)
        {
            // Given
            DialogBoxHandler = (name, wnd) =>
            {
                var tester = new MessageBoxTester(wnd);
                tester.ClickOk();
            };

            AssessmentSection assessmentSection = TestDataGenerator.GetAssessmentSectionWithAllCalculationConfigurations();

            assessmentSection.FailureMechanismContribution.NormativeProbabilityType = normativeProbabilityType;

            IEnumerable <HydraulicBoundaryLocationCalculation> calculationsBelongingToNorm = getLocationCalculationsFunc(assessmentSection);

            var waveConditionsCalculations = new List <ICalculation <WaveConditionsInput> >();

            waveConditionsCalculations.AddRange(assessmentSection.GrassCoverErosionOutwards.Calculations
                                                .Cast <GrassCoverErosionOutwardsWaveConditionsCalculation>());
            waveConditionsCalculations.AddRange(assessmentSection.StabilityStoneCover.Calculations
                                                .Cast <StabilityStoneCoverWaveConditionsCalculation>());
            waveConditionsCalculations.AddRange(assessmentSection.WaveImpactAsphaltCover.Calculations
                                                .Cast <WaveImpactAsphaltCoverWaveConditionsCalculation>());

            var mocks    = new MockRepository();
            var observer = mocks.StrictMock <IObserver>();

            observer.Expect(o => o.UpdateObserver()).Repeat.Once();
            mocks.ReplayAll();

            calculationsBelongingToNorm.ForEachElementDo(c =>
            {
                c.Output = null;
                c.Attach(observer);
            });
            waveConditionsCalculations.ForEachElementDo(c =>
            {
                c.InputParameters.WaterLevelType = waterLevelType;
                c.ClearOutput();
                c.Attach(observer);
            });

            assessmentSection.FailureMechanismContribution.Attach(observer);

            var handler = new FailureMechanismContributionNormChangeHandler(assessmentSection);

            // When
            var actionPerformed = false;

            void Call() => handler.ChangeProbability(() => actionPerformed = true);

            // Then
            TestHelper.AssertLogMessagesCount(Call, 0);
            Assert.IsTrue(actionPerformed);
            mocks.VerifyAll();
        }
        public void ChangeProbability_ActionNull_ThrowsArgumentNullException()
        {
            // Setup
            var mocks             = new MockRepository();
            var assessmentSection = mocks.Stub <IAssessmentSection>();

            mocks.ReplayAll();

            var handler = new FailureMechanismContributionNormChangeHandler(assessmentSection);

            // Call
            void Call() => handler.ChangeProbability(null);

            // Assert
            var exception = Assert.Throws <ArgumentNullException>(Call);

            Assert.AreEqual("action", exception.ParamName);
            mocks.VerifyAll();
        }
        public void GivenCalculationsWithOutput_WhenChangingProbability_ThenAllDependingOutputClearedAndActionPerformedAndAllAffectedObjectsNotified(
            NormativeProbabilityType normativeProbabilityType, Func <AssessmentSection, IEnumerable <HydraulicBoundaryLocationCalculation> > getCalculationsFunc,
            WaveConditionsInputWaterLevelType waterLevelType)
        {
            // Given
            DialogBoxHandler = (name, wnd) =>
            {
                var tester = new MessageBoxTester(wnd);
                tester.ClickOk();
            };

            AssessmentSection assessmentSection = TestDataGenerator.GetAssessmentSectionWithAllCalculationConfigurations();

            assessmentSection.FailureMechanismContribution.NormativeProbabilityType = normativeProbabilityType;

            IEnumerable <HydraulicBoundaryLocationCalculation> expectedCalculationsToClear = getCalculationsFunc(assessmentSection);

            var waveConditionsCalculations = new List <ICalculation <WaveConditionsInput> >();

            waveConditionsCalculations.AddRange(assessmentSection.GrassCoverErosionOutwards.Calculations
                                                .Cast <GrassCoverErosionOutwardsWaveConditionsCalculation>()
                                                .Where(c => c.HasOutput));
            waveConditionsCalculations.AddRange(assessmentSection.StabilityStoneCover.Calculations
                                                .Cast <StabilityStoneCoverWaveConditionsCalculation>()
                                                .Where(c => c.HasOutput));
            waveConditionsCalculations.AddRange(assessmentSection.WaveImpactAsphaltCover.Calculations
                                                .Cast <WaveImpactAsphaltCoverWaveConditionsCalculation>()
                                                .Where(c => c.HasOutput));

            waveConditionsCalculations.ForEachElementDo(c => c.InputParameters.WaterLevelType = waterLevelType);

            IEnumerable <IObservable> expectedAffectedObjects = new IObservable[]
            {
                assessmentSection.FailureMechanismContribution
            }
            .Concat(expectedCalculationsToClear)
            .Concat(waveConditionsCalculations)
            .ToArray();

            var mocks    = new MockRepository();
            var observer = mocks.StrictMock <IObserver>();

            observer.Expect(o => o.UpdateObserver()).Repeat.Times(expectedAffectedObjects.Count());
            mocks.ReplayAll();

            expectedAffectedObjects.ForEachElementDo(obj => obj.Attach(observer));

            var handler = new FailureMechanismContributionNormChangeHandler(assessmentSection);

            // Precondition
            CollectionAssert.IsNotEmpty(expectedCalculationsToClear.Where(c => c.HasOutput));
            CollectionAssert.IsNotEmpty(waveConditionsCalculations.Where(c => c.HasOutput));

            // When
            var actionPerformed = false;

            void Call() => handler.ChangeProbability(() => actionPerformed = true);

            // Then
            var expectedMessages = new[]
            {
                "Alle berekende hydraulische belastingen behorende bij de gewijzigde norm zijn verwijderd."
            };

            TestHelper.AssertLogMessagesAreGenerated(Call, expectedMessages, 1);

            Assert.IsTrue(actionPerformed);
            CollectionAssert.IsEmpty(expectedCalculationsToClear.Where(c => c.HasOutput));
            CollectionAssert.IsEmpty(waveConditionsCalculations.Where(c => c.HasOutput));
            mocks.VerifyAll();
        }