// 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.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using Rhino.Mocks;
using Ringtoets.AssemblyTool.Data;
using Ringtoets.AssemblyTool.KernelWrapper.Calculators;
using Ringtoets.AssemblyTool.KernelWrapper.TestUtil.Calculators;
using Ringtoets.AssemblyTool.KernelWrapper.TestUtil.Calculators.Assembly;
using Ringtoets.Common.Data.AssessmentSection;
using Ringtoets.Common.Data.TestUtil;
using Ringtoets.Common.Primitives;
using Ringtoets.Integration.Data.StandAlone;
using Ringtoets.Integration.Data.StandAlone.SectionResults;
using Ringtoets.Integration.IO.Assembly;
using Ringtoets.Integration.IO.Factories;
using Ringtoets.Integration.IO.TestUtil;
namespace Ringtoets.Integration.IO.Test.Factories
{
[TestFixture]
public class ExportableGrassCoverSlipOffOutwardsFailureMechanismFactoryTest
{
[Test]
public void CreateExportableFailureMechanism_FailureMechanismNull_ThrowsArgumentNullException()
{
// Setup
var mocks = new MockRepository();
var assessmentSection = mocks.Stub();
mocks.ReplayAll();
// Call
TestDelegate call = () =>
ExportableGrassCoverSlipOffOutwardsFailureMechanismFactory.CreateExportableFailureMechanism(null, assessmentSection);
// Assert
var exception = Assert.Throws(call);
Assert.AreEqual("failureMechanism", exception.ParamName);
mocks.VerifyAll();
}
[Test]
public void CreateExportableFailureMechanism_AssessmentSectionNull_ThrowsArgumentNullException()
{
// Call
TestDelegate call = () =>
ExportableGrassCoverSlipOffOutwardsFailureMechanismFactory.CreateExportableFailureMechanism(new GrassCoverSlipOffOutwardsFailureMechanism(), null);
// Assert
var exception = Assert.Throws(call);
Assert.AreEqual("assessmentSection", exception.ParamName);
}
[Test]
public void CreateExportableFailureMechanism_WithFailureMechanismNotRelevant_ReturnsDefaultExportableFailureMechanism()
{
// Setup
var mocks = new MockRepository();
var assessmentSection = mocks.Stub();
mocks.ReplayAll();
assessmentSection.ReferenceLine = ReferenceLineTestFactory.CreateReferenceLineWithGeometry();
var random = new Random(21);
var failureMechanism = new GrassCoverSlipOffOutwardsFailureMechanism
{
IsRelevant = false
};
FailureMechanismTestHelper.AddSections(failureMechanism, random.Next(2, 10));
// Call
ExportableFailureMechanism exportableFailureMechanism =
ExportableGrassCoverSlipOffOutwardsFailureMechanismFactory.CreateExportableFailureMechanism(failureMechanism, assessmentSection);
// Assert
ExportableFailureMechanismTestHelper.AssertDefaultFailureMechanismWithoutProbability(assessmentSection.ReferenceLine.Points,
ExportableFailureMechanismType.GABU,
ExportableFailureMechanismGroup.Group4,
ExportableAssemblyMethod.WBI1A1,
exportableFailureMechanism);
mocks.VerifyAll();
}
[Test]
public void CreateExportableFailureMechanism_WithFailureMechanismRelevant_ReturnsExportableFailureMechanism()
{
// Setup
var mocks = new MockRepository();
var assessmentSection = mocks.Stub();
mocks.ReplayAll();
var random = new Random(21);
var failureMechanism = new GrassCoverSlipOffOutwardsFailureMechanism();
FailureMechanismTestHelper.AddSections(failureMechanism, random.Next(2, 10));
using (new AssemblyToolCalculatorFactoryConfig())
{
var calculatorFactory = (TestAssemblyToolCalculatorFactory) AssemblyToolCalculatorFactory.Instance;
FailureMechanismAssemblyCalculatorStub failureMechanismAssemblyCalculator = calculatorFactory.LastCreatedFailureMechanismAssemblyCalculator;
FailureMechanismSectionAssemblyCalculatorStub failureMechanismSectionAssemblyCalculator = calculatorFactory.LastCreatedFailureMechanismSectionAssemblyCalculator;
// Call
ExportableFailureMechanism exportableFailureMechanism =
ExportableGrassCoverSlipOffOutwardsFailureMechanismFactory.CreateExportableFailureMechanism(failureMechanism, assessmentSection);
// Assert
Assert.AreEqual(ExportableFailureMechanismType.GABU, exportableFailureMechanism.Code);
Assert.AreEqual(ExportableFailureMechanismGroup.Group4, exportableFailureMechanism.Group);
ExportableFailureMechanismAssemblyResult exportableFailureMechanismAssembly = exportableFailureMechanism.FailureMechanismAssembly;
Assert.AreEqual(failureMechanismAssemblyCalculator.FailureMechanismAssemblyCategoryGroupOutput, exportableFailureMechanismAssembly.AssemblyCategory);
Assert.AreEqual(ExportableAssemblyMethod.WBI1A1, exportableFailureMechanismAssembly.AssemblyMethod);
IEnumerable exportableFailureMechanismSections = exportableFailureMechanism.SectionAssemblyResults
.Select(sar => sar.FailureMechanismSection);
ExportableFailureMechanismSectionTestHelper.AssertExportableFailureMechanismSections(failureMechanism.Sections, exportableFailureMechanismSections);
AssertExportableFailureMechanismSectionResults(failureMechanismSectionAssemblyCalculator.SimpleAssessmentAssemblyOutput.Group,
failureMechanismSectionAssemblyCalculator.DetailedAssessmentAssemblyGroupOutput.Value,
failureMechanismSectionAssemblyCalculator.TailorMadeAssemblyCategoryOutput.Value,
failureMechanismSectionAssemblyCalculator.CombinedAssemblyCategoryOutput.Value,
exportableFailureMechanismSections,
exportableFailureMechanism.SectionAssemblyResults.Cast());
}
mocks.VerifyAll();
}
[Test]
public void GivenFailureMechanismWithManualAssessment_WhenCreatingExportableFailureMechanism_ThenManualAssemblyIgnored()
{
// Given
var mocks = new MockRepository();
var assessmentSection = mocks.Stub();
mocks.ReplayAll();
var failureMechanism = new GrassCoverSlipOffOutwardsFailureMechanism();
FailureMechanismTestHelper.AddSections(failureMechanism, 1);
GrassCoverSlipOffOutwardsFailureMechanismSectionResult sectionResult = failureMechanism.SectionResults.Single();
sectionResult.UseManualAssemblyCategoryGroup = true;
sectionResult.ManualAssemblyCategoryGroup = ManualFailureMechanismSectionAssemblyCategoryGroup.VIIv;
using (new AssemblyToolCalculatorFactoryConfig())
{
var calculatorFactory = (TestAssemblyToolCalculatorFactory) AssemblyToolCalculatorFactory.Instance;
FailureMechanismAssemblyCalculatorStub failureMechanismAssemblyCalculator = calculatorFactory.LastCreatedFailureMechanismAssemblyCalculator;
// When
ExportableGrassCoverSlipOffOutwardsFailureMechanismFactory.CreateExportableFailureMechanism(failureMechanism, assessmentSection);
// Then
Assert.AreEqual(FailureMechanismSectionAssemblyCategoryGroup.IIv, failureMechanismAssemblyCalculator.FailureMechanismSectionCategories.Single());
}
mocks.VerifyAll();
}
private static void AssertExportableFailureMechanismSectionResults(FailureMechanismSectionAssemblyCategoryGroup expectedSimpleAssembly,
FailureMechanismSectionAssemblyCategoryGroup expectedDetailedAssembly,
FailureMechanismSectionAssemblyCategoryGroup expectedTailorMadeAssembly,
FailureMechanismSectionAssemblyCategoryGroup expectedCombinedAssembly,
IEnumerable sections,
IEnumerable results)
{
int expectedNrOfResults = sections.Count();
Assert.AreEqual(expectedNrOfResults, results.Count());
for (var i = 0; i < expectedNrOfResults; i++)
{
ExportableFailureMechanismSection section = sections.ElementAt(i);
ExportableAggregatedFailureMechanismSectionAssemblyResult result = results.ElementAt(i);
AssertExportableFailureMechanismSectionResult(expectedSimpleAssembly,
expectedDetailedAssembly,
expectedTailorMadeAssembly,
expectedCombinedAssembly,
section,
result);
}
}
private static void AssertExportableFailureMechanismSectionResult(FailureMechanismSectionAssemblyCategoryGroup expectedSimpleAssembly,
FailureMechanismSectionAssemblyCategoryGroup expectedDetailedAssembly,
FailureMechanismSectionAssemblyCategoryGroup expectedTailorMadeAssembly,
FailureMechanismSectionAssemblyCategoryGroup expectedCombinedAssembly,
ExportableFailureMechanismSection expectedSection,
ExportableAggregatedFailureMechanismSectionAssemblyResult actualResult)
{
Assert.AreSame(expectedSection, actualResult.FailureMechanismSection);
ExportableSectionAssemblyResultTestHelper.AssertExportableSectionAssemblyResult(expectedSimpleAssembly,
ExportableAssemblyMethod.WBI0E1,
actualResult.SimpleAssembly);
ExportableSectionAssemblyResultTestHelper.AssertExportableSectionAssemblyResult(expectedDetailedAssembly,
ExportableAssemblyMethod.WBI0G1,
actualResult.DetailedAssembly);
ExportableSectionAssemblyResultTestHelper.AssertExportableSectionAssemblyResult(expectedTailorMadeAssembly,
ExportableAssemblyMethod.WBI0T1,
actualResult.TailorMadeAssembly);
ExportableSectionAssemblyResultTestHelper.AssertExportableSectionAssemblyResult(expectedCombinedAssembly,
ExportableAssemblyMethod.WBI0A1,
actualResult.CombinedAssembly);
}
}
}