// 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 Core.Common.Base;
using Core.Common.Base.Geometry;
using Core.Common.TestUtil;
using NUnit.Framework;
using Rhino.Mocks;
using Ringtoets.Common.Data.Calculation;
using Ringtoets.Common.Data.FailureMechanism;
using Ringtoets.Common.Data.TestUtil;
using Ringtoets.Common.IO.FileImporters;
namespace Ringtoets.Common.IO.Test.FileImporters
{
[TestFixture]
public class FailureMechanismSectionUpdateStrategyTest
{
[Test]
public void Constructor_FailureMechanismNull_ThrowsArgumentNullException()
{
// Setup
var mocks = new MockRepository();
var sectionResultUpdateStrategy = mocks.Stub>();
mocks.ReplayAll();
// Call
TestDelegate call = () => new FailureMechanismSectionUpdateStrategy(null, sectionResultUpdateStrategy);
// Assert
string paramName = Assert.Throws(call).ParamName;
Assert.AreEqual("failureMechanism", paramName);
mocks.VerifyAll();
}
[Test]
public void Constructor_SectionResultUpdateStrategyNull_ThrowsArgumentNullException()
{
// Setup
var mocks = new MockRepository();
var failureMechanism = mocks.Stub>();
mocks.ReplayAll();
// Call
TestDelegate call = () => new FailureMechanismSectionUpdateStrategy(failureMechanism, null);
// Assert
string paramName = Assert.Throws(call).ParamName;
Assert.AreEqual("sectionResultUpdateStrategy", paramName);
}
[Test]
public void Constructor_ExpectedValues()
{
// Setup
var mocks = new MockRepository();
var failureMechanism = mocks.Stub>();
var sectionResultUpdateStrategy = mocks.Stub>();
mocks.ReplayAll();
// Call
var importer = new FailureMechanismSectionUpdateStrategy(failureMechanism, sectionResultUpdateStrategy);
// Assert
Assert.IsInstanceOf(importer);
mocks.VerifyAll();
}
[Test]
public void UpdateSectionsWithImportedData_ImportedFailureMechanismSectionsNull_ThrowsArgumentNullException()
{
// Setup
var mocks = new MockRepository();
var failureMechanism = mocks.Stub>();
var sectionResultUpdateStrategy = mocks.Stub>();
mocks.ReplayAll();
// Call
TestDelegate call = () => new FailureMechanismSectionUpdateStrategy(failureMechanism, sectionResultUpdateStrategy).UpdateSectionsWithImportedData(null, "");
// Assert
string paramName = Assert.Throws(call).ParamName;
Assert.AreEqual("importedFailureMechanismSections", paramName);
mocks.VerifyAll();
}
[Test]
public void UpdateSectionsWithImportedData_SourcePathNull_ThrowsArgumentNullException()
{
// Setup
var mocks = new MockRepository();
var failureMechanism = mocks.Stub>();
var sectionResultUpdateStrategy = mocks.Stub>();
mocks.ReplayAll();
var failureMechanismSectionUpdateStrategy = new FailureMechanismSectionUpdateStrategy(failureMechanism, sectionResultUpdateStrategy);
// Call
TestDelegate call = () => failureMechanismSectionUpdateStrategy.UpdateSectionsWithImportedData(
Enumerable.Empty(), null);
// Assert
string paramName = Assert.Throws(call).ParamName;
Assert.AreEqual("sourcePath", paramName);
mocks.VerifyAll();
}
[Test]
public void UpdateSectionsWithImportedData_WithValidData_SetsSectionsToFailureMechanismAndCopiesPropertiesOfEqualSections()
{
// Setup
string sourcePath = TestHelper.GetScratchPadPath();
var failureMechanism = new TestUpdateFailureMechanism();
FailureMechanismSection failureMechanismSection1 = FailureMechanismSectionTestFactory.CreateFailureMechanismSection(new[]
{
new Point2D(0.0, 0.0),
new Point2D(5.0, 5.0)
});
FailureMechanismSection failureMechanismSection2 = FailureMechanismSectionTestFactory.CreateFailureMechanismSection(new[]
{
new Point2D(5.0, 5.0),
new Point2D(10.0, 10.0)
});
failureMechanism.SetSections(new[]
{
failureMechanismSection1,
failureMechanismSection2
}, sourcePath);
int testValue = new Random(39).Next(1, 20);
IObservableEnumerable failureMechanismSectionResults = failureMechanism.SectionResults;
failureMechanismSectionResults.First().TestValue = testValue;
failureMechanismSectionResults.ElementAt(1).TestValue = testValue;
var sectionResultUpdateStrategy = new TestUpdateFailureMechanismSectionResultUpdateStrategy();
var failureMechanismSectionUpdateStrategy = new FailureMechanismSectionUpdateStrategy(failureMechanism, sectionResultUpdateStrategy);
FailureMechanismSection[] sections =
{
failureMechanismSection1,
FailureMechanismSectionTestFactory.CreateFailureMechanismSection(new[]
{
new Point2D(5.0, 5.0),
new Point2D(15.0, 15.0)
})
};
// Call
failureMechanismSectionUpdateStrategy.UpdateSectionsWithImportedData(sections, sourcePath);
// Assert
Assert.AreEqual(sourcePath, failureMechanism.FailureMechanismSectionSourcePath);
IEnumerable failureMechanismSections = failureMechanism.Sections;
Assert.AreEqual(2, failureMechanismSections.Count());
CollectionAssert.AreEqual(sections, failureMechanismSections);
Assert.AreEqual(testValue, failureMechanismSectionResults.First().TestValue);
Assert.Zero(failureMechanismSectionResults.ElementAt(1).TestValue);
}
private class TestUpdateFailureMechanism : FailureMechanismBase, IHasSectionResults
{
private readonly ObservableList sectionResults;
public TestUpdateFailureMechanism() : base("Test", "TST", 2)
{
sectionResults = new ObservableList();
}
public override IEnumerable Calculations { get; }
public IObservableEnumerable SectionResults
{
get
{
return sectionResults;
}
}
protected override void AddSectionResult(FailureMechanismSection section)
{
sectionResults.Add(new TestUpdateFailureMechanismSectionResult(section));
}
protected override void ClearSectionResults()
{
sectionResults.Clear();
}
}
private class TestUpdateFailureMechanismSectionResult : FailureMechanismSectionResult
{
public TestUpdateFailureMechanismSectionResult(FailureMechanismSection section) : base(section)
{
TestValue = 0;
}
public int TestValue { get; set; }
}
private class TestUpdateFailureMechanismSectionResultUpdateStrategy : IFailureMechanismSectionResultUpdateStrategy
{
public void UpdateSectionResult(TestUpdateFailureMechanismSectionResult origin, TestUpdateFailureMechanismSectionResult target)
{
target.TestValue = origin.TestValue;
}
}
}
}