// 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.IO;
using System.Linq;
using Application.Ringtoets.Storage.TestUtil;
using Core.Common.Base.Data;
using Core.Common.Base.Service;
using Core.Common.TestUtil;
using NUnit.Framework;
using Ringtoets.Common.Data.AssessmentSection;
using Ringtoets.Common.IO.FileImporters;
using Ringtoets.HydraRing.Calculation.Calculator.Factory;
using Ringtoets.HydraRing.Calculation.Data;
using Ringtoets.HydraRing.Calculation.Data.Input.WaveConditions;
using Ringtoets.HydraRing.Calculation.TestUtil;
using Ringtoets.HydraRing.Calculation.TestUtil.Calculator;
using Ringtoets.Integration.Data;
using Ringtoets.Revetment.Data;
using Ringtoets.WaveImpactAsphaltCover.Data;
using Ringtoets.WaveImpactAsphaltCover.Service;
namespace Ringtoets.WaveImpactAsphaltCover.Integration.Test
{
[TestFixture]
public class WaveImpactAsphaltCoverWaveConditionsCalculationActivityIntegrationTest
{
private readonly string testDataPath = TestHelper.GetTestDataPath(TestDataPath.Ringtoets.Integration.Service, "HydraRingCalculation");
private string validFilePath;
[SetUp]
public void SetUp()
{
validFilePath = Path.Combine(testDataPath, "HRD dutch coast south.sqlite");
}
[Test]
public void Run_NoHydraulicBoundaryDatabase_DoesNotPerformCalculationAndLogsError()
{
// Setup
var assessmentSection = new AssessmentSection(AssessmentSectionComposition.Dike);
var calculation = new WaveImpactAsphaltCoverWaveConditionsCalculation()
{
InputParameters =
{
ForeshoreProfile = new TestForeshoreProfile(true),
UseForeshore = true,
UseBreakWater = true,
StepSize = WaveConditionsInputStepSize.Half,
LowerBoundaryRevetment = (RoundedDouble) 5.3,
UpperBoundaryRevetment = (RoundedDouble) 10,
UpperBoundaryWaterLevels = (RoundedDouble) 5.4,
LowerBoundaryWaterLevels = (RoundedDouble) 5
}
};
var testFilePath = Path.Combine(testDataPath, "NonExisting.sqlite");
var activity = new WaveImpactAsphaltCoverWaveConditionsCalculationActivity(calculation,
testFilePath,
assessmentSection.WaveImpactAsphaltCover,
assessmentSection);
using (new HydraRingCalculatorFactoryConfig())
{
// Call
Action call = () => activity.Run();
// Assert
TestHelper.AssertLogMessages(call, messages =>
{
var msgs = messages.ToArray();
Assert.AreEqual(3, msgs.Length);
StringAssert.StartsWith(string.Format("Validatie van '{0}' gestart om: ", calculation.Name), msgs[0]);
Assert.AreEqual(string.Format("Validatie mislukt: Fout bij het lezen van bestand '{0}': Het bestand bestaat niet.", testFilePath), msgs[1]);
StringAssert.StartsWith(string.Format("Validatie van '{0}' beëindigd om: ", calculation.Name), msgs[2]);
});
Assert.AreEqual(ActivityState.Failed, activity.State);
}
}
[Test]
public void Run_Always_SetProgressTexts()
{
// Setup
var assessmentSection = new AssessmentSection(AssessmentSectionComposition.Dike);
ImportHydraulicBoundaryDatabase(assessmentSection);
WaveImpactAsphaltCoverWaveConditionsCalculation calculation = GetValidCalculation(assessmentSection);
var activity = new WaveImpactAsphaltCoverWaveConditionsCalculationActivity(calculation,
validFilePath,
assessmentSection.WaveImpactAsphaltCover,
assessmentSection);
using (new HydraRingCalculatorFactoryConfig())
{
List progessTexts = new List();
activity.ProgressChanged += (sender, args) => { progessTexts.Add(activity.ProgressText); };
// Call
activity.Run();
// Assert
var waterLevels = calculation.InputParameters.WaterLevels.ToArray();
for (var i = 0; i < waterLevels.Length; i++)
{
var text = string.Format("Stap {0} van {1} | Waterstand '{2}' berekenen.", i + 1, waterLevels.Length, waterLevels[i]);
Assert.AreEqual(text, progessTexts[i]);
}
}
}
[Test]
public void Run_Always_InputPropertiesCorrectlySendToService()
{
// Setup
var assessmentSection = new AssessmentSection(AssessmentSectionComposition.Dike);
ImportHydraulicBoundaryDatabase(assessmentSection);
WaveImpactAsphaltCoverWaveConditionsCalculation calculation = GetValidCalculation(assessmentSection);
var activity = new WaveImpactAsphaltCoverWaveConditionsCalculationActivity(calculation,
validFilePath,
assessmentSection.WaveImpactAsphaltCover,
assessmentSection);
using (new HydraRingCalculatorFactoryConfig())
{
// Call
activity.Run();
// Assert
var testWaveConditionsCosineCalculator = ((TestHydraRingCalculatorFactory) HydraRingCalculatorFactory.Instance).WaveConditionsCosineCalculator;
WaveConditionsCosineCalculationInput[] testWaveConditionsInputs = testWaveConditionsCosineCalculator.ReceivedInputs.ToArray();
Assert.AreEqual(3, testWaveConditionsInputs.Length);
int waterLevelIndex = 0;
foreach (WaveConditionsCosineCalculationInput actualInput in testWaveConditionsInputs)
{
GeneralWaveConditionsInput generalInput = assessmentSection.WaveImpactAsphaltCover.GeneralInput;
var input = calculation.InputParameters;
var expectedInput = new WaveConditionsCosineCalculationInput(1,
input.Orientation,
input.HydraulicBoundaryLocation.Id,
assessmentSection.FailureMechanismContribution.Norm,
input.ForeshoreProfile.Geometry.Select(c => new HydraRingForelandPoint(c.X, c.Y)),
new HydraRingBreakWater((int) input.BreakWater.Type, input.BreakWater.Height),
calculation.InputParameters.WaterLevels.ElementAt(waterLevelIndex++),
generalInput.A,
generalInput.B,
generalInput.C);
HydraRingDataEqualityHelper.AreEqual(expectedInput, actualInput);
Assert.AreEqual(testDataPath, testWaveConditionsCosineCalculator.HydraulicBoundaryDatabaseDirectory);
Assert.AreEqual(assessmentSection.Id, testWaveConditionsCosineCalculator.RingId);
}
}
}
[Test]
public void Cancel_WhenPerformingCalculation_CurrentCalculationForWaterLevelCompletesAndSubsequentCalculationsDidNotRun()
{
// Setup
var assessmentSection = new AssessmentSection(AssessmentSectionComposition.Dike);
ImportHydraulicBoundaryDatabase(assessmentSection);
WaveImpactAsphaltCoverWaveConditionsCalculation calculation = GetValidCalculation(assessmentSection);
var activity = new WaveImpactAsphaltCoverWaveConditionsCalculationActivity(calculation,
validFilePath,
assessmentSection.WaveImpactAsphaltCover,
assessmentSection);
using (new HydraRingCalculatorFactoryConfig())
{
activity.ProgressChanged += (sender, args) =>
{
if (activity.State != ActivityState.Canceled)
{
// Call
activity.Cancel();
}
};
// Assert
TestHelper.AssertLogMessages(() => activity.Run(), messages =>
{
string[] msgs = messages.ToArray();
RoundedDouble firstWaterLevel = calculation.InputParameters.WaterLevels.First();
Assert.AreEqual(7, msgs.Length);
StringAssert.StartsWith(string.Format("Validatie van '{0}' gestart om: ", calculation.Name), msgs[0]);
StringAssert.StartsWith(string.Format("Validatie van '{0}' beëindigd om: ", calculation.Name), msgs[1]);
StringAssert.StartsWith(string.Format("Berekening van '{0}' gestart om: ", calculation.Name), msgs[2]);
Assert.AreEqual(string.Format("Berekening '{0}' voor waterstand '{1}' gestart.", calculation.Name, firstWaterLevel), msgs[3]);
StringAssert.StartsWith("Golfcondities berekening is uitgevoerd op de tijdelijke locatie:", msgs[4]);
Assert.AreEqual(string.Format("Berekening '{0}' voor waterstand '{1}' beëindigd.", calculation.Name, firstWaterLevel), msgs[5]);
StringAssert.StartsWith(string.Format("Berekening van '{0}' beëindigd om: ", calculation.Name), msgs[6]);
});
Assert.AreEqual(ActivityState.Canceled, activity.State);
}
}
[Test]
public void Run_CalculationFailed_OutputNull()
{
// Setup
var assessmentSection = new AssessmentSection(AssessmentSectionComposition.Dike);
ImportHydraulicBoundaryDatabase(assessmentSection);
WaveImpactAsphaltCoverWaveConditionsCalculation calculation = GetValidCalculation(assessmentSection);
var activity = new WaveImpactAsphaltCoverWaveConditionsCalculationActivity(calculation,
validFilePath,
assessmentSection.WaveImpactAsphaltCover,
assessmentSection);
using (new HydraRingCalculatorFactoryConfig())
{
var calculator = ((TestHydraRingCalculatorFactory) HydraRingCalculatorFactory.Instance).WaveConditionsCosineCalculator;
calculator.EndInFailure = true;
// Call
activity.Run();
// Assert
Assert.IsNull(calculation.Output);
}
}
[Test]
[TestCase(true, null)]
[TestCase(false, "An error occurred")]
[TestCase(true, "An error occurred")]
public void Run_ErrorInCalculation_ActivityStateFailed(bool endInFailure, string lastErrorFileContent)
{
// Setup
var assessmentSection = new AssessmentSection(AssessmentSectionComposition.Dike);
using (var importer = new HydraulicBoundaryDatabaseImporter())
{
importer.Import(assessmentSection, validFilePath);
}
var failureMechanism = new WaveImpactAsphaltCoverFailureMechanism();
WaveImpactAsphaltCoverWaveConditionsCalculation calculation = GetValidCalculation(assessmentSection);
var activity = new WaveImpactAsphaltCoverWaveConditionsCalculationActivity(calculation, testDataPath, failureMechanism, assessmentSection);
using (new HydraRingCalculatorFactoryConfig())
{
var calculator = ((TestHydraRingCalculatorFactory) HydraRingCalculatorFactory.Instance).WaveConditionsCosineCalculator;
calculator.EndInFailure = endInFailure;
calculator.LastErrorFileContent = lastErrorFileContent;
// Call
activity.Run();
// Assert
Assert.AreEqual(ActivityState.Failed, activity.State);
}
}
private static WaveImpactAsphaltCoverWaveConditionsCalculation GetValidCalculation(AssessmentSection assessmentSection)
{
var calculation = new WaveImpactAsphaltCoverWaveConditionsCalculation
{
InputParameters =
{
HydraulicBoundaryLocation = assessmentSection.HydraulicBoundaryDatabase.Locations.First(hl => hl.Id == 1300001),
ForeshoreProfile = new TestForeshoreProfile(true),
UseForeshore = true,
UseBreakWater = true,
StepSize = WaveConditionsInputStepSize.Half,
LowerBoundaryRevetment = (RoundedDouble) 4,
UpperBoundaryRevetment = (RoundedDouble) 10,
UpperBoundaryWaterLevels = (RoundedDouble) 8,
LowerBoundaryWaterLevels = (RoundedDouble) 7.1
}
};
calculation.InputParameters.HydraulicBoundaryLocation.DesignWaterLevel = (RoundedDouble) 9.3;
return calculation;
}
private void ImportHydraulicBoundaryDatabase(AssessmentSection assessmentSection)
{
string filePath = Path.Combine(testDataPath, "HRD dutch coast south.sqlite");
using (var importer = new HydraulicBoundaryDatabaseImporter())
{
importer.Import(assessmentSection, filePath);
}
}
}
}