// Copyright (C) Stichting Deltares 2016. All rights reserved.
//
// This file is part of Ringtoets.
//
// Ringtoets is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see .
//
// All names, logos, and references to "Deltares" are registered trademarks of
// Stichting Deltares and remain full property of Stichting Deltares at all times.
// All rights reserved.
using System;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using Application.Ringtoets.Storage.TestUtil;
using Core.Common.Base;
using Core.Common.Base.Data;
using Core.Common.Base.Geometry;
using Core.Common.Gui.PropertyBag;
using Core.Common.TestUtil;
using NUnit.Framework;
using Rhino.Mocks;
using Ringtoets.Common.Data.AssessmentSection;
using Ringtoets.Common.Data.DikeProfiles;
using Ringtoets.Common.Data.TestUtil;
using Ringtoets.Common.Forms.Helpers;
using Ringtoets.Common.Forms.PropertyClasses;
using Ringtoets.Common.Forms.UITypeEditors;
using Ringtoets.HeightStructures.Data;
using Ringtoets.HeightStructures.Data.TestUtil;
using Ringtoets.HeightStructures.Forms.PresentationObjects;
using Ringtoets.HeightStructures.Forms.PropertyClasses;
using Ringtoets.HydraRing.Data;
namespace Ringtoets.HeightStructures.Forms.Test.PropertyClasses
{
[TestFixture]
public class HeightStructuresInputContextPropertiesTest
{
private const int heightStructurePropertyIndex = 0;
private const int heightStructureLocationPropertyIndex = 1;
private const int structureNormalOrientationPropertyIndex = 2;
private const int flowWidthAtBottomProtectionPropertyIndex = 3;
private const int widthFlowAperturesPropertyIndex = 4;
private const int storageStructureAreaPropertyIndex = 5;
private const int allowedLevelIncreaseStoragePropertyIndex = 6;
private const int levelCrestStructurePropertyIndex = 7;
private const int criticalOvertoppingDischargePropertyIndex = 8;
private const int failureProbabilityStructureWithErosionPropertyIndex = 9;
private const int foreshoreProfilePropertyIndex = 10;
private const int breakWaterPropertyIndex = 11;
private const int foreshoreGeometryPropertyIndex = 12;
private const int modelFactorSuperCriticalFlowPropertyIndex = 13;
private const int hydraulicBoundaryLocationPropertyIndex = 14;
private const int stormDurationPropertyIndex = 15;
private const int deviationWaveDirectionPropertyIndex = 16;
private MockRepository mockRepository;
[SetUp]
public void SetUp()
{
mockRepository = new MockRepository();
}
[Test]
public void Constructor_ExpectedValues()
{
// Call
var properties = new HeightStructuresInputContextProperties();
// Assert
Assert.IsInstanceOf>(properties);
Assert.IsInstanceOf>(properties);
Assert.IsInstanceOf(properties);
Assert.IsInstanceOf(properties);
Assert.IsNull(properties.Data);
}
[Test]
public void Data_SetNewInputContextInstance_ReturnCorrectPropertyValues()
{
// Setup
var assessmentSectionMock = mockRepository.StrictMock();
mockRepository.ReplayAll();
var failureMechanism = new HeightStructuresFailureMechanism();
var calculation = new HeightStructuresCalculation();
var properties = new HeightStructuresInputContextProperties();
var inputContext = new HeightStructuresInputContext(calculation, failureMechanism, assessmentSectionMock);
// Call
properties.Data = inputContext;
// Assert
HeightStructuresInput input = calculation.InputParameters;
var expectedFailureProbabilityStructureWithErosion = ProbabilityFormattingHelper.Format(input.FailureProbabilityStructureWithErosion);
Assert.IsNull(properties.Structure);
Assert.IsNull(properties.HeightStructureLocation);
Assert.AreSame(input.ModelFactorSuperCriticalFlow, properties.ModelFactorSuperCriticalFlow.Data);
Assert.AreEqual(input.StructureNormalOrientation, properties.StructureNormalOrientation);
Assert.AreSame(input.LevelCrestStructure, properties.LevelCrestStructure.Data);
Assert.AreSame(input.AllowedLevelIncreaseStorage, properties.AllowedLevelIncreaseStorage.Data);
Assert.AreSame(input.StorageStructureArea, properties.StorageStructureArea.Data);
Assert.AreSame(input.FlowWidthAtBottomProtection, properties.FlowWidthAtBottomProtection.Data);
Assert.AreSame(input.WidthFlowApertures, properties.WidthFlowApertures.Data);
Assert.AreSame(input.CriticalOvertoppingDischarge, properties.CriticalOvertoppingDischarge.Data);
Assert.IsNull(properties.ForeshoreProfile);
Assert.IsInstanceOf(properties.UseBreakWater);
Assert.IsInstanceOf(properties.UseForeshore);
Assert.AreEqual(expectedFailureProbabilityStructureWithErosion, properties.FailureProbabilityStructureWithErosion);
Assert.AreSame(input.HydraulicBoundaryLocation, properties.HydraulicBoundaryLocation);
Assert.AreSame(input.StormDuration, properties.StormDuration.Data);
Assert.AreEqual(input.DeviationWaveDirection, properties.DeviationWaveDirection);
mockRepository.VerifyAll();
}
[Test]
public void Data_SetNewInputContextInstanceWithData_ReturnCorrectPropertyValues()
{
// Setup
var hydraulicBoundaryDatabase = new HydraulicBoundaryDatabase
{
Locations =
{
new HydraulicBoundaryLocation(0, "", 0, 0)
}
};
var assessmentSectionMock = mockRepository.Stub();
assessmentSectionMock.HydraulicBoundaryDatabase = hydraulicBoundaryDatabase;
mockRepository.ReplayAll();
var failureMechanism = new HeightStructuresFailureMechanism
{
ForeshoreProfiles =
{
new TestForeshoreProfile()
},
HeightStructures =
{
new TestHeightStructure()
}
};
var calculation = new HeightStructuresCalculation
{
InputParameters =
{
Structure = new TestHeightStructure(),
HydraulicBoundaryLocation = CreateValidHydraulicBoundaryLocation(),
ForeshoreProfile = CreateValidForeshoreProfile()
}
};
var inputContext = new HeightStructuresInputContext(calculation,
failureMechanism,
assessmentSectionMock);
var properties = new HeightStructuresInputContextProperties();
// Call
properties.Data = inputContext;
// Assert
HeightStructuresInput input = calculation.InputParameters;
var expectedHeightStructureLocation = new Point2D(new RoundedDouble(0, input.Structure.Location.X), new RoundedDouble(0, input.Structure.Location.Y));
var expectedFailureProbabilityStructureWithErosion = ProbabilityFormattingHelper.Format(input.FailureProbabilityStructureWithErosion);
Assert.AreSame(input.Structure, properties.Structure);
Assert.AreEqual(expectedHeightStructureLocation, properties.HeightStructureLocation);
Assert.AreSame(input.ModelFactorSuperCriticalFlow, properties.ModelFactorSuperCriticalFlow.Data);
Assert.AreEqual(input.StructureNormalOrientation, properties.StructureNormalOrientation);
Assert.AreSame(input.LevelCrestStructure, properties.LevelCrestStructure.Data);
Assert.AreSame(input.AllowedLevelIncreaseStorage, properties.AllowedLevelIncreaseStorage.Data);
Assert.AreSame(input.StorageStructureArea, properties.StorageStructureArea.Data);
Assert.AreSame(input.FlowWidthAtBottomProtection, properties.FlowWidthAtBottomProtection.Data);
Assert.AreSame(input.WidthFlowApertures, properties.WidthFlowApertures.Data);
Assert.AreSame(input.CriticalOvertoppingDischarge, properties.CriticalOvertoppingDischarge.Data);
Assert.AreSame(input.ForeshoreProfile, properties.ForeshoreProfile);
Assert.IsInstanceOf(properties.UseBreakWater);
Assert.IsInstanceOf(properties.UseForeshore);
Assert.AreEqual(expectedFailureProbabilityStructureWithErosion, properties.FailureProbabilityStructureWithErosion);
Assert.AreSame(input.HydraulicBoundaryLocation, properties.HydraulicBoundaryLocation);
Assert.AreSame(input.StormDuration, properties.StormDuration.Data);
Assert.AreEqual(input.DeviationWaveDirection, properties.DeviationWaveDirection);
Assert.AreEqual(1, properties.GetAvailableHydraulicBoundaryLocations().Count());
CollectionAssert.AreEqual(inputContext.AvailableHydraulicBoundaryLocations, properties.GetAvailableHydraulicBoundaryLocations());
Assert.AreEqual(1, properties.GetAvailableStructures().Count());
CollectionAssert.AreEqual(failureMechanism.HeightStructures, properties.GetAvailableStructures());
Assert.AreEqual(1, properties.GetAvailableForeshoreProfiles().Count());
CollectionAssert.AreEqual(failureMechanism.ForeshoreProfiles, properties.GetAvailableForeshoreProfiles());
mockRepository.VerifyAll();
}
[Test]
public void SetProperties_IndividualProperties_UpdateDataAndNotifyObservers()
{
// Setup
var observerMock = mockRepository.StrictMock();
const int numberOfChangedProperties = 6;
observerMock.Expect(o => o.UpdateObserver()).Repeat.Times(numberOfChangedProperties);
var hydraulicBoundaryLocation = CreateValidHydraulicBoundaryLocation();
var assessmentSectionMock = mockRepository.Stub();
mockRepository.ReplayAll();
var failureMechanism = new HeightStructuresFailureMechanism();
var calculation = new HeightStructuresCalculation();
var input = calculation.InputParameters;
input.Attach(observerMock);
var inputContext = new HeightStructuresInputContext(calculation, failureMechanism, assessmentSectionMock);
var properties = new HeightStructuresInputContextProperties
{
Data = inputContext
};
var random = new Random(100);
double newStructureNormalOrientation = random.NextDouble();
HeightStructure newHeightStructure = new TestHeightStructure();
ForeshoreProfile newForeshoreProfile = CreateValidForeshoreProfile();
double newDeviationWaveDirection = random.NextDouble();
// Call
properties.Structure = newHeightStructure;
properties.StructureNormalOrientation = (RoundedDouble) newStructureNormalOrientation;
properties.FailureProbabilityStructureWithErosion = "1e-2";
properties.HydraulicBoundaryLocation = hydraulicBoundaryLocation;
properties.ForeshoreProfile = newForeshoreProfile;
properties.DeviationWaveDirection = (RoundedDouble) newDeviationWaveDirection;
// Assert
Assert.AreSame(newHeightStructure, properties.Structure);
Assert.AreEqual(newStructureNormalOrientation, properties.StructureNormalOrientation,
properties.StructureNormalOrientation.GetAccuracy());
Assert.AreEqual(0.01, input.FailureProbabilityStructureWithErosion);
Assert.AreEqual("1/100", properties.FailureProbabilityStructureWithErosion);
Assert.AreSame(hydraulicBoundaryLocation, properties.HydraulicBoundaryLocation);
Assert.AreSame(newForeshoreProfile, properties.ForeshoreProfile);
Assert.AreEqual(newDeviationWaveDirection, properties.DeviationWaveDirection,
properties.DeviationWaveDirection.GetAccuracy());
mockRepository.VerifyAll();
}
[Test]
[TestCase(double.MinValue)]
[TestCase(double.MaxValue)]
public void SetFailureProbabilityStructureWithErosion_InvalidValues_ThrowsArgumentException(double newValue)
{
// Setup
var assessmentSectionMock = mockRepository.Stub();
mockRepository.ReplayAll();
var failureMechanism = new HeightStructuresFailureMechanism();
var calculation = new HeightStructuresCalculation();
var inputContext = new HeightStructuresInputContext(calculation, failureMechanism, assessmentSectionMock);
var properties = new HeightStructuresInputContextProperties
{
Data = inputContext
};
// Call
TestDelegate call = () => properties.FailureProbabilityStructureWithErosion = newValue.ToString(CultureInfo.InvariantCulture);
// Assert
var expectedMessage = "De waarde voor de faalkans is te groot of te klein.";
TestHelper.AssertThrowsArgumentExceptionAndTestMessage(call, expectedMessage);
mockRepository.VerifyAll();
}
[Test]
[TestCase("no double value")]
[TestCase("")]
public void SetFailureProbabilityStructureWithErosion_ValuesUnableToParse_ThrowsArgumentException(string newValue)
{
// Setup
var assessmentSectionMock = mockRepository.Stub();
mockRepository.ReplayAll();
var failureMechanism = new HeightStructuresFailureMechanism();
var calculation = new HeightStructuresCalculation();
var inputContext = new HeightStructuresInputContext(calculation, failureMechanism, assessmentSectionMock);
var properties = new HeightStructuresInputContextProperties
{
Data = inputContext
};
// Call
TestDelegate call = () => properties.FailureProbabilityStructureWithErosion = newValue;
// Assert
var expectedMessage = "De waarde voor de faalkans kon niet geïnterpreteerd worden als een getal.";
TestHelper.AssertThrowsArgumentExceptionAndTestMessage(call, expectedMessage);
mockRepository.VerifyAll();
}
[Test]
public void SetFailureProbabilityStructureWithErosion_NullValue_ThrowsArgumentNullException()
{
// Setup
var assessmentSectionMock = mockRepository.Stub();
mockRepository.ReplayAll();
var failureMechanism = new HeightStructuresFailureMechanism();
var calculation = new HeightStructuresCalculation();
var inputContext = new HeightStructuresInputContext(calculation, failureMechanism, assessmentSectionMock);
var properties = new HeightStructuresInputContextProperties
{
Data = inputContext
};
// Call
TestDelegate call = () => properties.FailureProbabilityStructureWithErosion = null;
// Assert
var expectedMessage = "De waarde voor de faalkans moet ingevuld zijn.";
TestHelper.AssertThrowsArgumentExceptionAndTestMessage(call, expectedMessage);
mockRepository.VerifyAll();
}
[Test]
public void PropertyAttributes_ReturnExpectedValues()
{
// Setup
var assessmentSectionMock = mockRepository.Stub();
mockRepository.ReplayAll();
var failureMechanism = new HeightStructuresFailureMechanism();
var calculation = new HeightStructuresCalculation();
var inputContext = new HeightStructuresInputContext(calculation, failureMechanism, assessmentSectionMock);
// Call
var properties = new HeightStructuresInputContextProperties
{
Data = inputContext
};
// Assert
const string schematizationCategory = "Schematisatie";
const string hydraulicDataCategory = "Hydraulische gegevens";
const string modelSettingsCategory = "Modelinstellingen";
var dynamicPropertyBag = new DynamicPropertyBag(properties);
PropertyDescriptorCollection dynamicProperties = dynamicPropertyBag.GetProperties(new Attribute[]
{
new BrowsableAttribute(true)
});
Assert.AreEqual(17, dynamicProperties.Count);
PropertyDescriptor heightStructureProperty = dynamicProperties[heightStructurePropertyIndex];
Assert.IsFalse(heightStructureProperty.IsReadOnly);
Assert.AreEqual(schematizationCategory, heightStructureProperty.Category);
Assert.AreEqual("Kunstwerk", heightStructureProperty.DisplayName);
Assert.AreEqual("Het kunstwerk dat gebruikt wordt in de berekening.", heightStructureProperty.Description);
PropertyDescriptor heightStructureLocationProperty = dynamicProperties[heightStructureLocationPropertyIndex];
Assert.IsTrue(heightStructureLocationProperty.IsReadOnly);
Assert.AreEqual(schematizationCategory, heightStructureLocationProperty.Category);
Assert.AreEqual("Locatie (RD) [m]", heightStructureLocationProperty.DisplayName);
Assert.AreEqual("De coördinaten van de locatie van het kunstwerk in het Rijksdriehoeksstelsel.", heightStructureLocationProperty.Description);
PropertyDescriptor structureNormalOrientationProperty = dynamicProperties[structureNormalOrientationPropertyIndex];
Assert.IsFalse(structureNormalOrientationProperty.IsReadOnly);
Assert.AreEqual(schematizationCategory, structureNormalOrientationProperty.Category);
Assert.AreEqual("Oriëntatie [°]", structureNormalOrientationProperty.DisplayName);
Assert.AreEqual("Oriëntatie van de normaal van het kunstwerk ten opzichte van het noorden.", structureNormalOrientationProperty.Description);
PropertyDescriptor flowWidthAtBottomProtectionProperty = dynamicProperties[flowWidthAtBottomProtectionPropertyIndex];
Assert.IsInstanceOf(flowWidthAtBottomProtectionProperty.Converter);
Assert.AreEqual(schematizationCategory, flowWidthAtBottomProtectionProperty.Category);
Assert.AreEqual("Stroomvoerende breedte bodembescherming [m]", flowWidthAtBottomProtectionProperty.DisplayName);
Assert.AreEqual("Stroomvoerende breedte bodembescherming.", flowWidthAtBottomProtectionProperty.Description);
PropertyDescriptor widthFlowAperturesProperty = dynamicProperties[widthFlowAperturesPropertyIndex];
Assert.IsInstanceOf(widthFlowAperturesProperty.Converter);
Assert.AreEqual(schematizationCategory, widthFlowAperturesProperty.Category);
Assert.AreEqual("Breedte van doorstroomopening [m]", widthFlowAperturesProperty.DisplayName);
Assert.AreEqual("Breedte van de doorstroomopening.", widthFlowAperturesProperty.Description);
PropertyDescriptor storageStructureAreaProperty = dynamicProperties[storageStructureAreaPropertyIndex];
Assert.IsInstanceOf(storageStructureAreaProperty.Converter);
Assert.AreEqual(schematizationCategory, storageStructureAreaProperty.Category);
Assert.AreEqual("Kombergend oppervlak [m²]", storageStructureAreaProperty.DisplayName);
Assert.AreEqual("Kombergend oppervlak.", storageStructureAreaProperty.Description);
PropertyDescriptor allowedLevelIncreaseStorageProperty = dynamicProperties[allowedLevelIncreaseStoragePropertyIndex];
Assert.IsInstanceOf(allowedLevelIncreaseStorageProperty.Converter);
Assert.AreEqual(schematizationCategory, allowedLevelIncreaseStorageProperty.Category);
Assert.AreEqual("Toegestane peilverhoging komberging [m]", allowedLevelIncreaseStorageProperty.DisplayName);
Assert.AreEqual("Toegestane peilverhoging komberging.", allowedLevelIncreaseStorageProperty.Description);
PropertyDescriptor levelCrestStructureProperty = dynamicProperties[levelCrestStructurePropertyIndex];
Assert.IsInstanceOf(levelCrestStructureProperty.Converter);
Assert.AreEqual(schematizationCategory, levelCrestStructureProperty.Category);
Assert.AreEqual("Kerende hoogte [m+NAP]", levelCrestStructureProperty.DisplayName);
Assert.AreEqual("Kerende hoogte van het kunstwerk.", levelCrestStructureProperty.Description);
PropertyDescriptor criticalOvertoppingDischargeProperty = dynamicProperties[criticalOvertoppingDischargePropertyIndex];
Assert.IsInstanceOf(criticalOvertoppingDischargeProperty.Converter);
Assert.AreEqual(schematizationCategory, criticalOvertoppingDischargeProperty.Category);
Assert.AreEqual("Kritiek instromend debiet [m³/s/m]", criticalOvertoppingDischargeProperty.DisplayName);
Assert.AreEqual("Kritiek instromend debiet directe invoer per strekkende meter.", criticalOvertoppingDischargeProperty.Description);
PropertyDescriptor failureProbabilityStructureWithErosionProperty = dynamicProperties[failureProbabilityStructureWithErosionPropertyIndex];
Assert.IsFalse(failureProbabilityStructureWithErosionProperty.IsReadOnly);
Assert.AreEqual(schematizationCategory, failureProbabilityStructureWithErosionProperty.Category);
Assert.AreEqual("Faalkans gegeven erosie bodem [1/jaar]", failureProbabilityStructureWithErosionProperty.DisplayName);
Assert.AreEqual("Faalkans kunstwerk gegeven erosie bodem.", failureProbabilityStructureWithErosionProperty.Description);
PropertyDescriptor modelFactorSuperCriticalFlowProperty = dynamicProperties[modelFactorSuperCriticalFlowPropertyIndex];
Assert.IsInstanceOf(modelFactorSuperCriticalFlowProperty.Converter);
Assert.AreEqual(modelSettingsCategory, modelFactorSuperCriticalFlowProperty.Category);
Assert.AreEqual("Modelfactor overloopdebiet volkomen overlaat [-]", modelFactorSuperCriticalFlowProperty.DisplayName);
Assert.AreEqual("Modelfactor voor het overloopdebiet over een volkomen overlaat.", modelFactorSuperCriticalFlowProperty.Description);
PropertyDescriptor foreshoreProfileProperty = dynamicProperties[foreshoreProfilePropertyIndex];
Assert.IsFalse(foreshoreProfileProperty.IsReadOnly);
Assert.AreEqual(schematizationCategory, foreshoreProfileProperty.Category);
Assert.AreEqual("Voorlandprofiel", foreshoreProfileProperty.DisplayName);
Assert.AreEqual("De schematisatie van het voorlandprofiel.", foreshoreProfileProperty.Description);
PropertyDescriptor breakWaterProperty = dynamicProperties[breakWaterPropertyIndex];
Assert.IsInstanceOf(breakWaterProperty.Converter);
Assert.IsTrue(breakWaterProperty.IsReadOnly);
Assert.AreEqual(schematizationCategory, breakWaterProperty.Category);
Assert.AreEqual("Dam", breakWaterProperty.DisplayName);
Assert.AreEqual("Eigenschappen van de dam.", breakWaterProperty.Description);
PropertyDescriptor foreshoreGeometryProperty = dynamicProperties[foreshoreGeometryPropertyIndex];
Assert.IsInstanceOf(foreshoreGeometryProperty.Converter);
Assert.IsTrue(foreshoreGeometryProperty.IsReadOnly);
Assert.AreEqual(schematizationCategory, foreshoreGeometryProperty.Category);
Assert.AreEqual("Voorlandgeometrie", foreshoreGeometryProperty.DisplayName);
Assert.AreEqual("Eigenschappen van de voorlandgeometrie.", foreshoreGeometryProperty.Description);
PropertyDescriptor hydraulicBoundaryLocationProperty = dynamicProperties[hydraulicBoundaryLocationPropertyIndex];
Assert.IsFalse(hydraulicBoundaryLocationProperty.IsReadOnly);
Assert.AreEqual(hydraulicDataCategory, hydraulicBoundaryLocationProperty.Category);
Assert.AreEqual("Locatie met hydraulische randvoorwaarden", hydraulicBoundaryLocationProperty.DisplayName);
Assert.AreEqual("De locatie met hydraulische randvoorwaarden.", hydraulicBoundaryLocationProperty.Description);
PropertyDescriptor stormDurationProperty = dynamicProperties[stormDurationPropertyIndex];
Assert.IsInstanceOf(stormDurationProperty.Converter);
Assert.AreEqual(hydraulicDataCategory, stormDurationProperty.Category);
Assert.AreEqual("Stormduur [uur]", stormDurationProperty.DisplayName);
Assert.AreEqual("Stormduur.", stormDurationProperty.Description);
PropertyDescriptor deviationWaveDirectionProperty = dynamicProperties[deviationWaveDirectionPropertyIndex];
Assert.AreEqual(hydraulicDataCategory, deviationWaveDirectionProperty.Category);
Assert.AreEqual("Afwijking golfrichting [°]", deviationWaveDirectionProperty.DisplayName);
Assert.AreEqual("Afwijking van de golfrichting.", deviationWaveDirectionProperty.Description);
mockRepository.VerifyAll();
}
private static ForeshoreProfile CreateValidForeshoreProfile()
{
return new ForeshoreProfile(new Point2D(0, 0), Enumerable.Empty(), new BreakWater(BreakWaterType.Caisson, 0), new ForeshoreProfile.ConstructionProperties());
}
private static HydraulicBoundaryLocation CreateValidHydraulicBoundaryLocation()
{
return new HydraulicBoundaryLocation(0, "name", 0.0, 1.1);
}
}
}