// 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.Linq;
using Core.Common.Base.Geometry;
using Core.Common.TestUtil;
using log4net;
using NUnit.Framework;
using Rhino.Mocks;
using Ringtoets.Common.Data.TestUtil;
using Ringtoets.Common.IO.Configurations;
using Ringtoets.Common.IO.Configurations.Helpers;
namespace Ringtoets.Common.IO.Test.Configurations.Helpers
{
[TestFixture]
public class ConfigurationValidationExtensionsTest
{
[Test]
public void ValidateWaveReduction_NoLog_ThrowsArgumentNullException()
{
// Setup
const string calculationName = "calculation";
// Call
TestDelegate test = () => ((WaveReductionConfiguration) null).ValidateWaveReduction(null, calculationName, null);
// Assert
var exception = Assert.Throws(test);
Assert.AreEqual("log", exception.ParamName);
}
[Test]
public void ValidateWaveReduction_NoCalculationName_ThrowsArgumentNullException()
{
// Setup
var mocks = new MockRepository();
var log = mocks.StrictMock();
mocks.ReplayAll();
// Call
TestDelegate test = () => ((WaveReductionConfiguration) null).ValidateWaveReduction(null, null, log);
// Assert
var exception = Assert.Throws(test);
Assert.AreEqual("calculationName", exception.ParamName);
mocks.VerifyAll();
}
[Test]
public void ValidateWaveReduction_NoForeshoreProfileNoParameters_ReturnsTrue()
{
// Setup
const string calculationName = "calculation";
var mocks = new MockRepository();
var log = mocks.StrictMock();
mocks.ReplayAll();
// Call
bool valid = ((WaveReductionConfiguration) null).ValidateWaveReduction(null, calculationName, log);
// Assert
Assert.IsTrue(valid);
mocks.VerifyAll();
}
[Test]
public void ValidateWaveReduction_NoForeshoreProfileWaveReductionWithoutParameters_ReturnsTrue()
{
// Setup
const string calculationName = "calculation";
var mocks = new MockRepository();
var log = mocks.StrictMock();
mocks.ReplayAll();
// Call
bool valid = new WaveReductionConfiguration().ValidateWaveReduction(null, calculationName, log);
// Assert
Assert.IsTrue(valid);
mocks.VerifyAll();
}
[Test]
public void ValidateWaveReduction_NoForeshoreProfileWaveReductionWithParameter_LogsErrorReturnsFalse([Values(0, 1, 2, 3)] int propertyToSet)
{
// Setup
const string calculationName = "calculation";
const string error = "Er is geen voorlandprofiel opgegeven om golfreductie parameters aan toe te voegen.";
const string expectedMessage = "{0} Berekening '{1}' is overgeslagen.";
var mocks = new MockRepository();
var log = mocks.StrictMock();
log.Expect(l => l.ErrorFormat(expectedMessage, error, calculationName));
mocks.ReplayAll();
var waveReductionConfiguration = new WaveReductionConfiguration();
var random = new Random(21);
switch (propertyToSet)
{
case 0:
waveReductionConfiguration.BreakWaterType = random.NextEnumValue();
break;
case 1:
waveReductionConfiguration.BreakWaterHeight = random.NextDouble();
break;
case 2:
waveReductionConfiguration.UseBreakWater = random.NextBoolean();
break;
case 3:
waveReductionConfiguration.UseForeshoreProfile = random.NextBoolean();
break;
}
// Call
bool valid = waveReductionConfiguration.ValidateWaveReduction(null, calculationName, log);
// Assert
Assert.IsFalse(valid);
mocks.VerifyAll();
}
[Test]
public void ValidateWaveReduction_ForeshoreProfileWithoutGeometryNoWaveReduction_ReturnsTrue()
{
// Setup
const string calculationName = "calculation";
var mocks = new MockRepository();
var log = mocks.StrictMock();
mocks.ReplayAll();
// Call
bool valid = ((WaveReductionConfiguration) null).ValidateWaveReduction(
new TestForeshoreProfile("voorland", Enumerable.Empty()),
calculationName,
log);
// Assert
Assert.IsTrue(valid);
mocks.VerifyAll();
}
[Test]
public void ValidateWaveReduction_ForeshoreProfileWithGeometryForeshoreProfileUsed_ReturnsTrue()
{
// Setup
const string calculationName = "calculation";
var mocks = new MockRepository();
var log = mocks.StrictMock();
mocks.ReplayAll();
var waveReductionConfiguration = new WaveReductionConfiguration
{
UseForeshoreProfile = true
};
// Call
bool valid = waveReductionConfiguration.ValidateWaveReduction(new TestForeshoreProfile("voorland", new[]
{
new Point2D(0, 2)
}), calculationName, log);
// Assert
Assert.IsTrue(valid);
mocks.VerifyAll();
}
[Test]
public void ValidateWaveReduction_ForeshoreProfileWithoutGeometryForeshoreProfileUsed_LogsErrorReturnsFalse()
{
// Setup
const string profileName = "voorland";
const string calculationName = "calculation";
const string expectedMessage = "{0} Berekening '{1}' is overgeslagen.";
string error = $"Het opgegeven voorlandprofiel '{profileName}' heeft geen voorlandgeometrie en kan daarom niet gebruikt worden.";
var mocks = new MockRepository();
var log = mocks.StrictMock();
log.Expect(l => l.ErrorFormat(expectedMessage, error, calculationName));
mocks.ReplayAll();
var waveReductionConfiguration = new WaveReductionConfiguration
{
UseForeshoreProfile = true
};
// Call
bool valid = waveReductionConfiguration.ValidateWaveReduction(new TestForeshoreProfile(profileName), calculationName, log);
// Assert
Assert.IsFalse(valid);
mocks.VerifyAll();
}
[Test]
public void ValidateStochasts_NoLog_ThrowsArgumentNullException()
{
// Setup
const string calculationName = "calculation";
// Call
TestDelegate test = () => new TestStructuresCalculationConfiguration(calculationName).ValidateStructureBaseStochasts(null);
// Assert
var exception = Assert.Throws(test);
Assert.AreEqual("log", exception.ParamName);
}
[Test]
public void ValidateStochasts_NoConfiguration_ThrowsArgumentNullException()
{
// Setup
var mocks = new MockRepository();
var log = mocks.StrictMock();
mocks.ReplayAll();
// Call
TestDelegate test = () => ((StructuresCalculationConfiguration) null).ValidateStructureBaseStochasts(log);
// Assert
var exception = Assert.Throws(test);
Assert.AreEqual("configuration", exception.ParamName);
mocks.VerifyAll();
}
[Test]
public void ValidateStochasts_ConfigurationWithoutStochasts_ReturnsTrue()
{
// Setup
const string calculationName = "calculation";
var mocks = new MockRepository();
var log = mocks.StrictMock();
mocks.ReplayAll();
var configuration = new TestStructuresCalculationConfiguration(calculationName);
// Call
bool valid = configuration.ValidateStructureBaseStochasts(log);
// Assert
Assert.IsTrue(valid);
mocks.VerifyAll();
}
[Test]
[TestCase(true, false)]
[TestCase(false, true)]
[TestCase(true, true)]
public void ValidateStochasts_StormDurationWithVariationCoefficient_ReturnsFalseLogsError(bool standardDeviation, bool variationCoefficient)
{
// Setup
const string calculationName = "calculation";
const string expectedMessage = "{0} Berekening '{1}' is overgeslagen.";
const string error = "Er kan geen spreiding voor stochast 'stormduur' opgegeven worden.";
var mocks = new MockRepository();
var log = mocks.StrictMock();
log.Expect(l => l.ErrorFormat(expectedMessage, error, calculationName));
mocks.ReplayAll();
var stochastConfiguration = new StochastConfiguration();
if (standardDeviation)
{
stochastConfiguration.StandardDeviation = new Random(21).NextDouble();
}
if (variationCoefficient)
{
stochastConfiguration.VariationCoefficient = new Random(21).NextDouble();
}
var configuration = new TestStructuresCalculationConfiguration(calculationName)
{
StormDuration = stochastConfiguration
};
// Call
bool valid = configuration.ValidateStructureBaseStochasts(log);
// Assert
Assert.IsFalse(valid);
mocks.VerifyAll();
}
[Test]
public void ValidateStochasts_StormDurationWithMean_ReturnsTrue()
{
// Setup
const string calculationName = "calculation";
var mocks = new MockRepository();
var log = mocks.StrictMock();
mocks.ReplayAll();
var configuration = new TestStructuresCalculationConfiguration(calculationName)
{
StormDuration = new StochastConfiguration
{
Mean = new Random(21).NextDouble()
}
};
// Call
bool valid = configuration.ValidateStructureBaseStochasts(log);
// Assert
Assert.IsTrue(valid);
mocks.VerifyAll();
}
[Test]
[TestCase(true, false)]
[TestCase(false, true)]
[TestCase(true, true)]
public void ValidateStochasts_ModelFactorSuperCriticalFlowWithStandardDeviation_ReturnsFalseLogsError(bool standardDeviation, bool variationCoefficient)
{
// Setup
const string calculationName = "calculation";
const string expectedMessage = "{0} Berekening '{1}' is overgeslagen.";
const string error = "Er kan geen spreiding voor stochast 'modelfactoroverloopdebiet' opgegeven worden.";
var mocks = new MockRepository();
var log = mocks.StrictMock();
log.Expect(l => l.ErrorFormat(expectedMessage, error, calculationName));
mocks.ReplayAll();
var stochastConfiguration = new StochastConfiguration();
if (standardDeviation)
{
stochastConfiguration.StandardDeviation = new Random(21).NextDouble();
}
if (variationCoefficient)
{
stochastConfiguration.VariationCoefficient = new Random(21).NextDouble();
}
var configuration = new TestStructuresCalculationConfiguration(calculationName)
{
ModelFactorSuperCriticalFlow = stochastConfiguration
};
// Call
bool valid = configuration.ValidateStructureBaseStochasts(log);
// Assert
Assert.IsFalse(valid);
mocks.VerifyAll();
}
[Test]
public void ValidateStochasts_ModelFactorSuperCriticalFlowWithMean_ReturnsTrue()
{
// Setup
const string calculationName = "calculation";
var mocks = new MockRepository();
var log = mocks.StrictMock();
mocks.ReplayAll();
var configuration = new TestStructuresCalculationConfiguration(calculationName)
{
ModelFactorSuperCriticalFlow = new StochastConfiguration
{
Mean = new Random(21).NextDouble()
}
};
// Call
bool valid = configuration.ValidateStructureBaseStochasts(log);
// Assert
Assert.IsTrue(valid);
mocks.VerifyAll();
}
private class TestStructuresCalculationConfiguration : StructuresCalculationConfiguration
{
public TestStructuresCalculationConfiguration(string name) : base(name) {}
}
}
}