// 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.Collections.Generic;
using System.Linq;
using Core.Common.Base;
using Core.Common.Base.Data;
using NUnit.Framework;
using Ringtoets.Common.Data.Calculation;
using Ringtoets.GrassCoverErosionOutwards.Data;
using Ringtoets.HydraRing.Data;
using Ringtoets.Revetment.Data;
namespace Ringtoets.GrassCoverErosionOutwards.Service.Test
{
[TestFixture]
public class GrassCoverErosionOutwardsDataSynchronizationServiceTest
{
[Test]
public void ClearWaveConditionsCalculation_CalculationNull_ThrowsArgumentNullException()
{
// Call
TestDelegate test = () => GrassCoverErosionOutwardsDataSynchronizationService.ClearWaveConditionsCalculationOutput(null);
// Assert
var exception = Assert.Throws(test);
Assert.AreEqual("calculation", exception.ParamName);
}
[Test]
public void ClearWaveConditionsCalculation_WithCalculation_OutputNull()
{
// Setup
var calculation = new GrassCoverErosionOutwardsWaveConditionsCalculation
{
Output = new GrassCoverErosionOutwardsWaveConditionsOutput(Enumerable.Empty())
};
// Precondition
Assert.IsNotNull(calculation.Output);
// Call
GrassCoverErosionOutwardsDataSynchronizationService.ClearWaveConditionsCalculationOutput(calculation);
// Assert
Assert.IsNull(calculation.Output);
}
[Test]
public void ClearHydraulicBoundaryLocationOutput_LocationsNull_ThrowsArgumentNullException()
{
// Call
TestDelegate test = () => GrassCoverErosionOutwardsDataSynchronizationService.ClearHydraulicBoundaryLocationOutput(null);
// Assert
var exception = Assert.Throws(test);
Assert.AreEqual("locations", exception.ParamName);
}
[Test]
[TestCase(3.4, 5.3)]
[TestCase(3.4, double.NaN)]
[TestCase(double.NaN, 8.5)]
public void ClearHydraulicBoundaryLocationOutput_LocationWithData_ClearsDataAndReturnsTrue(double designWaterLevel, double waveHeight)
{
// Setup
var location = new HydraulicBoundaryLocation(1, string.Empty, 0, 0)
{
DesignWaterLevel = (RoundedDouble) designWaterLevel,
WaveHeight = (RoundedDouble) waveHeight
};
var locations = new ObservableList
{
location
};
// Call
bool affected = GrassCoverErosionOutwardsDataSynchronizationService.ClearHydraulicBoundaryLocationOutput(locations);
// Assert
Assert.IsNaN(location.DesignWaterLevel);
Assert.IsNaN(location.WaveHeight);
Assert.AreEqual(CalculationConvergence.NotCalculated, location.DesignWaterLevelCalculationConvergence);
Assert.AreEqual(CalculationConvergence.NotCalculated, location.WaveHeightCalculationConvergence);
Assert.IsTrue(affected);
}
[Test]
public void ClearHydraulicBoundaryLocationOutput_HydraulicBoundaryDatabaseWithoutLocations_ReturnsFalse()
{
// Setup
var locations = new ObservableList();
// Call
bool affected = GrassCoverErosionOutwardsDataSynchronizationService.ClearHydraulicBoundaryLocationOutput(locations);
// Assert
Assert.IsFalse(affected);
}
[Test]
public void ClearHydraulicBoundaryLocationOutput_LocationWithoutWaveHeightAndDesignWaterLevel_ReturnsFalse()
{
// Setup
var locations = new ObservableList
{
new HydraulicBoundaryLocation(1, string.Empty, 0, 0)
};
// Call
bool affected = GrassCoverErosionOutwardsDataSynchronizationService.ClearHydraulicBoundaryLocationOutput(locations);
// Assert
Assert.IsFalse(affected);
}
[Test]
public void ClearAllWaveConditionsCalculationOutputAndHydraulicBoundaryLocations_FailureMechanismNull_ThrowsArgumentNullException()
{
// Call
TestDelegate call = () => GrassCoverErosionOutwardsDataSynchronizationService.ClearAllWaveConditionsCalculationOutputAndHydraulicBoundaryLocations(null);
// Assert
var exception = Assert.Throws(call);
Assert.AreEqual("failureMechanism", exception.ParamName);
}
[Test]
public void ClearAllWaveConditionsCalculationOutputAndHydraulicBoundaryLocations_WithVariousCalculations_ClearsOutputAndReturnsAffectedCalculations()
{
// Setup
GrassCoverErosionOutwardsFailureMechanism failureMechanism = CreateFullyConfiguredFailureMechanism();
var expectedAffectedCalculations = failureMechanism.Calculations.Cast()
.Where(c => c.InputParameters.HydraulicBoundaryLocation != null || c.HasOutput)
.ToArray();
// Call
IEnumerable affectedItems =
GrassCoverErosionOutwardsDataSynchronizationService.ClearAllWaveConditionsCalculationOutputAndHydraulicBoundaryLocations(failureMechanism);
// Assert
Assert.IsFalse(failureMechanism.Calculations.Cast()
.Any(c => c.InputParameters.HydraulicBoundaryLocation != null || c.HasOutput));
CollectionAssert.AreEqual(expectedAffectedCalculations, affectedItems);
}
[Test]
public void ClearAllWaveConditionsCalculationOutput_FailureMechanismNull_ThrowsArgumentNullException()
{
// Call
TestDelegate call = () => GrassCoverErosionOutwardsDataSynchronizationService.ClearAllWaveConditionsCalculationOutput(null);
// Assert
var exception = Assert.Throws(call);
Assert.AreEqual("failureMechanism", exception.ParamName);
}
[Test]
public void ClearAllWaveConditionsCalculationOutput_WithVariousCalculations_ClearsCalculationsOutputAndReturnsAffectedCalculations()
{
// Setup
GrassCoverErosionOutwardsFailureMechanism failureMechanism = CreateFullyConfiguredFailureMechanism();
var expectedAffectedCalculations = failureMechanism.Calculations.Where(c => c.HasOutput)
.ToArray();
// Call
IEnumerable affectedItems =
GrassCoverErosionOutwardsDataSynchronizationService.ClearAllWaveConditionsCalculationOutput(failureMechanism);
// Assert
Assert.IsFalse(failureMechanism.Calculations.Any(c => c.HasOutput));
CollectionAssert.AreEqual(expectedAffectedCalculations, affectedItems);
}
private static GrassCoverErosionOutwardsFailureMechanism CreateFullyConfiguredFailureMechanism()
{
var failureMechanism = new GrassCoverErosionOutwardsFailureMechanism();
var hydraulicBoundaryLocation = new HydraulicBoundaryLocation(1, string.Empty, 0, 0);
var calculation = new GrassCoverErosionOutwardsWaveConditionsCalculation();
var calculationWithOutput = new GrassCoverErosionOutwardsWaveConditionsCalculation
{
Output = new GrassCoverErosionOutwardsWaveConditionsOutput(Enumerable.Empty())
};
var calculationWithOutputAndHydraulicBoundaryLocation = new GrassCoverErosionOutwardsWaveConditionsCalculation
{
InputParameters =
{
HydraulicBoundaryLocation = hydraulicBoundaryLocation
},
Output = new GrassCoverErosionOutwardsWaveConditionsOutput(Enumerable.Empty())
};
var calculationWithHydraulicBoundaryLocation = new GrassCoverErosionOutwardsWaveConditionsCalculation
{
InputParameters =
{
HydraulicBoundaryLocation = hydraulicBoundaryLocation
}
};
var subCalculation = new GrassCoverErosionOutwardsWaveConditionsCalculation();
var subCalculationWithOutput = new GrassCoverErosionOutwardsWaveConditionsCalculation
{
Output = new GrassCoverErosionOutwardsWaveConditionsOutput(Enumerable.Empty())
};
var subCalculationWithOutputAndHydraulicBoundaryLocation = new GrassCoverErosionOutwardsWaveConditionsCalculation
{
InputParameters =
{
HydraulicBoundaryLocation = hydraulicBoundaryLocation
},
Output = new GrassCoverErosionOutwardsWaveConditionsOutput(Enumerable.Empty())
};
var subCalculationWithHydraulicBoundaryLocation = new GrassCoverErosionOutwardsWaveConditionsCalculation
{
InputParameters =
{
HydraulicBoundaryLocation = hydraulicBoundaryLocation
}
};
failureMechanism.WaveConditionsCalculationGroup.Children.Add(calculation);
failureMechanism.WaveConditionsCalculationGroup.Children.Add(calculationWithOutput);
failureMechanism.WaveConditionsCalculationGroup.Children.Add(calculationWithOutputAndHydraulicBoundaryLocation);
failureMechanism.WaveConditionsCalculationGroup.Children.Add(calculationWithHydraulicBoundaryLocation);
var calculationGroup = new CalculationGroup();
calculationGroup.Children.Add(subCalculation);
calculationGroup.Children.Add(subCalculationWithOutput);
calculationGroup.Children.Add(subCalculationWithOutputAndHydraulicBoundaryLocation);
calculationGroup.Children.Add(subCalculationWithHydraulicBoundaryLocation);
failureMechanism.WaveConditionsCalculationGroup.Children.Add(calculationGroup);
return failureMechanism;
}
}
}