// Copyright (C) Stichting Deltares 2017. 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.Linq;
using Core.Common.Base;
using Core.Common.Base.Geometry;
using Core.Common.Gui.Converters;
using Core.Common.TestUtil;
using NUnit.Framework;
using Rhino.Mocks;
using Ringtoets.Common.Data.Hydraulics;
using Ringtoets.Common.Data.IllustrationPoints;
using Ringtoets.Common.Data.TestUtil;
using Ringtoets.Common.Data.TestUtil.IllustrationPoints;
using Ringtoets.Integration.Forms.PropertyClasses;
namespace Ringtoets.Integration.Forms.Test.PropertyClasses
{
[TestFixture]
public class HydraulicBoundaryLocationPropertiesTest
{
private const int idPropertyIndex = 0;
private const int namePropertyIndex = 1;
private const int coordinatesPropertyIndex = 2;
private const int targetProbabilityPropertyIndex = 3;
private const int targetReliabilityPropertyIndex = 4;
private const int calculatedProbabilityPropertyIndex = 5;
private const int calculatedReliabilityPropertyIndex = 6;
private const int shouldCalculateIllustrationPointsIndex = 7;
private const int governingWindDirectionIndex = 8;
private const int alphaValuesIndex = 9;
private const int durationsIndex = 10;
private const int illustrationPointsIndex = 11;
[Test]
public void Constructor_HydraulicBoundaryLocationCalculationNull_ThrowsArgumentNullException()
{
// Call
TestDelegate test = () => new TestHydraulicBoundaryLocationProperties(null);
// Assert
string paramName = Assert.Throws(test).ParamName;
Assert.AreEqual("hydraulicBoundaryLocationCalculation", paramName);
}
[Test]
public void Constructor_WithoutGeneralIllustrationPointsResult_PropertiesHaveExpectedAttributesValues()
{
// Setup
var hydraulicBoundaryLocationCalculation = new HydraulicBoundaryLocationCalculation(new TestHydraulicBoundaryLocation());
// Call
var properties = new TestHydraulicBoundaryLocationProperties(hydraulicBoundaryLocationCalculation);
// Assert
TypeConverter classTypeConverter = TypeDescriptor.GetConverter(properties, true);
Assert.IsInstanceOf(classTypeConverter);
PropertyDescriptorCollection dynamicProperties = PropertiesTestHelper.GetAllVisiblePropertyDescriptors(properties);
Assert.AreEqual(8, dynamicProperties.Count);
const string generalCategory = "Algemeen";
const string resultCategory = "Resultaat";
const string illustrationPointsCategory = "Illustratiepunten";
PropertyDescriptor idProperty = dynamicProperties[idPropertyIndex];
PropertiesTestHelper.AssertRequiredPropertyDescriptorProperties(idProperty,
generalCategory,
"ID",
"ID van de hydraulische randvoorwaardenlocatie in de database.",
true);
PropertyDescriptor nameProperty = dynamicProperties[namePropertyIndex];
PropertiesTestHelper.AssertRequiredPropertyDescriptorProperties(nameProperty,
generalCategory,
"Naam",
"Naam van de hydraulische randvoorwaardenlocatie.",
true);
PropertyDescriptor coordinatesProperty = dynamicProperties[coordinatesPropertyIndex];
PropertiesTestHelper.AssertRequiredPropertyDescriptorProperties(coordinatesProperty,
generalCategory,
"Coördinaten [m]",
"Coördinaten van de hydraulische randvoorwaardenlocatie.",
true);
PropertyDescriptor targetProbabilityProperty = dynamicProperties[targetProbabilityPropertyIndex];
PropertiesTestHelper.AssertRequiredPropertyDescriptorProperties(targetProbabilityProperty,
resultCategory,
"Doelkans [1/jaar]",
"De ingevoerde kans waarvoor het resultaat moet worden berekend.",
true);
PropertyDescriptor targetReliabilityProperty = dynamicProperties[targetReliabilityPropertyIndex];
PropertiesTestHelper.AssertRequiredPropertyDescriptorProperties(targetReliabilityProperty,
resultCategory,
"Betrouwbaarheidsindex doelkans [-]",
"Betrouwbaarheidsindex van de ingevoerde kans waarvoor het resultaat moet worden berekend.",
true);
PropertyDescriptor calculatedProbabilityProperty = dynamicProperties[calculatedProbabilityPropertyIndex];
PropertiesTestHelper.AssertRequiredPropertyDescriptorProperties(calculatedProbabilityProperty,
resultCategory,
"Berekende kans [1/jaar]",
"De berekende kans van voorkomen van het berekende resultaat.",
true);
PropertyDescriptor calculatedReliabilityProperty = dynamicProperties[calculatedReliabilityPropertyIndex];
PropertiesTestHelper.AssertRequiredPropertyDescriptorProperties(calculatedReliabilityProperty,
resultCategory,
"Betrouwbaarheidsindex berekende kans [-]",
"Betrouwbaarheidsindex van de berekende kans van voorkomen van het berekende resultaat.",
true);
PropertyDescriptor calculateIllustrationPointsProperty = dynamicProperties[shouldCalculateIllustrationPointsIndex];
PropertiesTestHelper.AssertRequiredPropertyDescriptorProperties(calculateIllustrationPointsProperty,
illustrationPointsCategory,
"Illustratiepunten inlezen",
"Neem de informatie over de illustratiepunten op in het berekeningsresultaat.");
}
[Test]
public void Constructor_WithGeneralIllustrationPointsResult_PropertiesHaveExpectedAttributesValues()
{
// Setup
var hydraulicBoundaryLocationCalculation = new HydraulicBoundaryLocationCalculation(new TestHydraulicBoundaryLocation())
{
Output = new TestHydraulicBoundaryLocationOutput(new TestGeneralResultSubMechanismIllustrationPoint())
};
// Call
var properties = new TestHydraulicBoundaryLocationProperties(hydraulicBoundaryLocationCalculation);
// Assert
TypeConverter classTypeConverter = TypeDescriptor.GetConverter(properties, true);
Assert.IsInstanceOf(classTypeConverter);
PropertyDescriptorCollection dynamicProperties = PropertiesTestHelper.GetAllVisiblePropertyDescriptors(properties);
Assert.AreEqual(12, dynamicProperties.Count);
const string generalCategory = "Algemeen";
const string resultCategory = "Resultaat";
const string illustrationPointsCategory = "Illustratiepunten";
PropertyDescriptor idProperty = dynamicProperties[idPropertyIndex];
PropertiesTestHelper.AssertRequiredPropertyDescriptorProperties(idProperty,
generalCategory,
"ID",
"ID van de hydraulische randvoorwaardenlocatie in de database.",
true);
PropertyDescriptor nameProperty = dynamicProperties[namePropertyIndex];
PropertiesTestHelper.AssertRequiredPropertyDescriptorProperties(nameProperty,
generalCategory,
"Naam",
"Naam van de hydraulische randvoorwaardenlocatie.",
true);
PropertyDescriptor coordinatesProperty = dynamicProperties[coordinatesPropertyIndex];
PropertiesTestHelper.AssertRequiredPropertyDescriptorProperties(coordinatesProperty,
generalCategory,
"Coördinaten [m]",
"Coördinaten van de hydraulische randvoorwaardenlocatie.",
true);
PropertyDescriptor targetProbabilityProperty = dynamicProperties[targetProbabilityPropertyIndex];
PropertiesTestHelper.AssertRequiredPropertyDescriptorProperties(targetProbabilityProperty,
resultCategory,
"Doelkans [1/jaar]",
"De ingevoerde kans waarvoor het resultaat moet worden berekend.",
true);
PropertyDescriptor targetReliabilityProperty = dynamicProperties[targetReliabilityPropertyIndex];
PropertiesTestHelper.AssertRequiredPropertyDescriptorProperties(targetReliabilityProperty,
resultCategory,
"Betrouwbaarheidsindex doelkans [-]",
"Betrouwbaarheidsindex van de ingevoerde kans waarvoor het resultaat moet worden berekend.",
true);
PropertyDescriptor calculatedProbabilityProperty = dynamicProperties[calculatedProbabilityPropertyIndex];
PropertiesTestHelper.AssertRequiredPropertyDescriptorProperties(calculatedProbabilityProperty,
resultCategory,
"Berekende kans [1/jaar]",
"De berekende kans van voorkomen van het berekende resultaat.",
true);
PropertyDescriptor calculatedReliabilityProperty = dynamicProperties[calculatedReliabilityPropertyIndex];
PropertiesTestHelper.AssertRequiredPropertyDescriptorProperties(calculatedReliabilityProperty,
resultCategory,
"Betrouwbaarheidsindex berekende kans [-]",
"Betrouwbaarheidsindex van de berekende kans van voorkomen van het berekende resultaat.",
true);
PropertyDescriptor calculateIllustrationPointsProperty = dynamicProperties[shouldCalculateIllustrationPointsIndex];
PropertiesTestHelper.AssertRequiredPropertyDescriptorProperties(calculateIllustrationPointsProperty,
illustrationPointsCategory,
"Illustratiepunten inlezen",
"Neem de informatie over de illustratiepunten op in het berekeningsresultaat.");
PropertiesTestHelper.AssertRequiredPropertyDescriptorProperties(dynamicProperties[governingWindDirectionIndex],
illustrationPointsCategory,
"Maatgevende windrichting",
"De windrichting waarvoor de berekende betrouwbaarheidsindex het laagst is.",
true);
TestHelper.AssertTypeConverter(nameof(HydraulicBoundaryLocationProperties.AlphaValues));
PropertyDescriptor alphaValuesProperty = dynamicProperties[alphaValuesIndex];
Assert.NotNull(alphaValuesProperty.Attributes[typeof(KeyValueElementAttribute)]);
PropertiesTestHelper.AssertRequiredPropertyDescriptorProperties(alphaValuesProperty,
illustrationPointsCategory,
"Invloedscoëfficiënten [-]",
"Berekende invloedscoëfficiënten voor alle beschouwde stochasten.",
true);
TestHelper.AssertTypeConverter(nameof(HydraulicBoundaryLocationProperties.Durations));
PropertyDescriptor durationsProperty = dynamicProperties[durationsIndex];
Assert.NotNull(durationsProperty.Attributes[typeof(KeyValueElementAttribute)]);
PropertiesTestHelper.AssertRequiredPropertyDescriptorProperties(durationsProperty,
illustrationPointsCategory,
"Tijdsduren [uur]",
"Tijdsduren waarop de stochasten betrekking hebben.",
true);
TestHelper.AssertTypeConverter(nameof(HydraulicBoundaryLocationProperties.IllustrationPoints));
PropertiesTestHelper.AssertRequiredPropertyDescriptorProperties(dynamicProperties[illustrationPointsIndex],
illustrationPointsCategory,
"Illustratiepunten",
"De lijst van illustratiepunten voor de berekening.",
true);
}
[Test]
[TestCase(true)]
[TestCase(false)]
public void GetProperties_ValidData_ReturnsExpectedValues(bool withIllustrationPoints)
{
// Setup
var random = new Random();
const long id = 1234L;
const double x = 567.0;
const double y = 890.0;
const string name = "";
double targetProbability = random.NextDouble();
double targetReliability = random.NextDouble();
double calculatedProbability = random.NextDouble();
double calculatedReliability = random.NextDouble();
double result = random.NextDouble();
var convergence = random.NextEnumValue();
var illustrationPoints = new[]
{
new TopLevelSubMechanismIllustrationPoint(new WindDirection("WEST", 4), "sluit", new TestSubMechanismIllustrationPoint())
};
var stochasts = new[]
{
new Stochast("a", 2, 3)
};
const string governingWindDirection = "EAST";
GeneralResult generalResult =
withIllustrationPoints
? new GeneralResult(new WindDirection(governingWindDirection, 2),
stochasts,
illustrationPoints)
: null;
var hydraulicBoundaryLocationOutput = new HydraulicBoundaryLocationOutput(result,
targetProbability,
targetReliability,
calculatedProbability,
calculatedReliability,
convergence,
generalResult);
var hydraulicBoundaryLocation = new HydraulicBoundaryLocation(id, name, x, y);
var hydraulicBoundaryLocationCalculation = new HydraulicBoundaryLocationCalculation(hydraulicBoundaryLocation)
{
Output = hydraulicBoundaryLocationOutput
};
// Call
var properties = new TestHydraulicBoundaryLocationProperties(hydraulicBoundaryLocationCalculation);
// Assert
Assert.AreEqual(id, properties.Id);
Assert.AreEqual(name, properties.Name);
var coordinates = new Point2D(x, y);
Assert.AreEqual(coordinates, properties.Location);
Assert.AreEqual(targetProbability, properties.TargetProbability);
Assert.AreEqual(targetReliability, properties.TargetReliability, properties.TargetReliability.GetAccuracy());
Assert.AreEqual(calculatedProbability, properties.CalculatedProbability);
Assert.AreEqual(calculatedReliability, properties.CalculatedReliability, properties.CalculatedReliability.GetAccuracy());
if (withIllustrationPoints)
{
GeneralResult expectedGeneralResult = hydraulicBoundaryLocationOutput.GeneralResult;
CollectionAssert.AreEqual(expectedGeneralResult.Stochasts, properties.AlphaValues);
CollectionAssert.AreEqual(expectedGeneralResult.Stochasts, properties.Durations);
CollectionAssert.AreEqual(expectedGeneralResult.TopLevelIllustrationPoints, properties.IllustrationPoints.Select(ip => ip.Data));
Assert.AreEqual(expectedGeneralResult.GoverningWindDirection.Name, properties.GoverningWindDirection);
}
}
[Test]
[TestCase("")]
[TestCase("some name")]
public void ToString_WithName_ReturnsName(string name)
{
// Setup
const long id = 1234L;
const double x = 567.0;
const double y = 890.0;
var hydraulicBoundaryLocation = new HydraulicBoundaryLocation(id, name, x, y);
var hydraulicBoundaryLocationCalculation = new HydraulicBoundaryLocationCalculation(hydraulicBoundaryLocation);
// Call
HydraulicBoundaryLocationProperties hydraulicBoundaryLocationProperties = new TestHydraulicBoundaryLocationProperties(hydraulicBoundaryLocationCalculation);
// Assert
string expectedString = $"{name} {new Point2D(x, y)}";
Assert.AreEqual(expectedString, hydraulicBoundaryLocationProperties.ToString());
}
[Test]
public void ShouldIllustrationPointsBeCalculated_SetNewValue_NotifyObservers()
{
// Setup
var mocks = new MockRepository();
var observer = mocks.StrictMock();
observer.Expect(o => o.UpdateObserver());
mocks.ReplayAll();
var hydraulicBoundaryLocationCalculation = new HydraulicBoundaryLocationCalculation(new TestHydraulicBoundaryLocation())
{
InputParameters =
{
ShouldIllustrationPointsBeCalculated = false
}
};
hydraulicBoundaryLocationCalculation.Attach(observer);
var properties = new TestHydraulicBoundaryLocationProperties(hydraulicBoundaryLocationCalculation);
// Call
properties.ShouldIllustrationPointsBeCalculated = true;
// Assert
Assert.IsTrue(hydraulicBoundaryLocationCalculation.InputParameters.ShouldIllustrationPointsBeCalculated);
mocks.VerifyAll();
}
private class TestHydraulicBoundaryLocationProperties : HydraulicBoundaryLocationProperties
{
public TestHydraulicBoundaryLocationProperties(HydraulicBoundaryLocationCalculation hydraulicBoundaryLocationCalculation)
: base(hydraulicBoundaryLocationCalculation) {}
}
}
}