// 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 NUnit.Framework;
using Ringtoets.Common.Data.Calculation;
using Ringtoets.Common.Service;
using Ringtoets.Piping.Data;
using Ringtoets.Piping.Data.TestUtil;
using Ringtoets.Piping.Integration.TestUtils;
using Ringtoets.Piping.KernelWrapper.TestUtil;
using Ringtoets.Piping.Primitives;
namespace Ringtoets.Piping.Service.Test
{
[TestFixture]
public class PipingDataSynchronizationServiceTest
{
[Test]
public void ClearCalculationOutput_CalculationNull_ThrowsArgumentNullException()
{
// Call
TestDelegate test = () => PipingDataSynchronizationService.ClearCalculationOutput(null);
// Assert
var exception = Assert.Throws(test);
Assert.AreEqual("calculation", exception.ParamName);
}
[Test]
public void ClearCalculationOutput_WithCalculation_ClearsOutputAndReturnAffectedCaculations()
{
// Setup
var calculation = new PipingCalculation(new GeneralPipingInput())
{
Output = new TestPipingOutput(),
SemiProbabilisticOutput = new TestPipingSemiProbabilisticOutput()
};
// Call
IEnumerable changedObjects = PipingDataSynchronizationService.ClearCalculationOutput(calculation);
// Assert
// Note: To make sure the clear is performed regardless of what is done with
// the return result, no ToArray() should be called before these assertions:
Assert.IsNull(calculation.Output);
Assert.IsNull(calculation.SemiProbabilisticOutput);
CollectionAssert.AreEqual(new[]
{
calculation
}, changedObjects);
}
[Test]
public void ClearCalculationOutput_CalculationWithoutOutput_DoNothing()
{
// Setup
var calculation = new PipingCalculation(new GeneralPipingInput())
{
Output = null,
SemiProbabilisticOutput = null
};
// Call
IEnumerable changedObjects = PipingDataSynchronizationService.ClearCalculationOutput(calculation);
// Assert
CollectionAssert.IsEmpty(changedObjects);
}
[Test]
public void ClearAllCalculationOutput_FailureMechanismNull_ThrowsArgumentNullException()
{
// Call
TestDelegate call = () => PipingDataSynchronizationService.ClearAllCalculationOutput(null);
// Assert
var exception = Assert.Throws(call);
Assert.AreEqual("failureMechanism", exception.ParamName);
}
[Test]
public void ClearAllCalculationOutput_WithVariousCalculations_ClearsCalculationsOutputAndReturnsAffectedCalculations()
{
// Setup
PipingFailureMechanism failureMechanism = PipingTestDataGenerator.GetPipingFailureMechanismWithAllCalculationConfigurations();
ICalculation[] expectedAffectedCalculations = failureMechanism.Calculations
.Where(c => c.HasOutput)
.ToArray();
// Call
IEnumerable affectedItems = PipingDataSynchronizationService.ClearAllCalculationOutput(failureMechanism);
// Assert
// Note: To make sure the clear is performed regardless of what is done with
// the return result, no ToArray() should be called before these assertions:
Assert.IsTrue(failureMechanism.Calculations.All(c => !c.HasOutput));
CollectionAssert.AreEquivalent(expectedAffectedCalculations, affectedItems);
}
[Test]
public void ClearAllCalculationOutputAndHydraulicBoundaryLocations_FailureMechanismNull_ThrowsArgumentNullException()
{
// Call
TestDelegate call = () => PipingDataSynchronizationService.ClearAllCalculationOutputAndHydraulicBoundaryLocations(null);
// Assert
var exception = Assert.Throws(call);
Assert.AreEqual("failureMechanism", exception.ParamName);
}
[Test]
public void ClearAllCalculationOutputAndHydraulicBoundaryLocations_WithVariousCalculations_ClearsHydraulicBoundaryLocationAndCalculationsAndReturnsAffectedObjects()
{
// Setup
PipingFailureMechanism failureMechanism = PipingTestDataGenerator.GetPipingFailureMechanismWithAllCalculationConfigurations();
PipingCalculation[] calculations = failureMechanism.Calculations.Cast().ToArray();
IObservable[] expectedAffectedCalculations = calculations
.Where(c => c.HasOutput)
.Cast()
.ToArray();
IObservable[] expectedAffectedCalculationInputs = calculations
.Select(c => c.InputParameters)
.Where(i => i.HydraulicBoundaryLocation != null)
.Cast()
.ToArray();
// Call
IEnumerable affectedItems = PipingDataSynchronizationService.ClearAllCalculationOutputAndHydraulicBoundaryLocations(failureMechanism);
// Assert
// Note: To make sure the clear is performed regardless of what is done with
// the return result, no ToArray() should be called before these assertions:
Assert.IsTrue(failureMechanism.Calculations
.Cast()
.All(c => c.InputParameters.HydraulicBoundaryLocation == null &&
!c.HasOutput));
CollectionAssert.AreEquivalent(expectedAffectedCalculations.Concat(expectedAffectedCalculationInputs),
affectedItems);
}
[Test]
public void ClearReferenceLineDependentData_FailureMechanismNull_ThrowArgumentNullException()
{
// Call
TestDelegate call = () => PipingDataSynchronizationService.ClearReferenceLineDependentData(null);
// Assert
string paramName = Assert.Throws(call).ParamName;
Assert.AreEqual("failureMechanism", paramName);
}
[Test]
public void ClearReferenceLineDependentData_FullyConfiguredFailureMechanism_RemoveReferenceLineDependentDataAndReturnAffectedObjects()
{
// Setup
PipingFailureMechanism failureMechanism = PipingTestDataGenerator.GetPipingFailureMechanismWithAllCalculationConfigurations();
var expectedRemovedObjects = failureMechanism.Sections.OfType