Index: Application/Ringtoets/src/Application.Ringtoets.Storage/Application.Ringtoets.Storage.csproj =================================================================== diff -u -r6d514ec60f68620d78015ac58ba6a966ef6b14e3 -rd09b94cdc3d7c18ffa7ce53b727c34d189f588a9 --- Application/Ringtoets/src/Application.Ringtoets.Storage/Application.Ringtoets.Storage.csproj (.../Application.Ringtoets.Storage.csproj) (revision 6d514ec60f68620d78015ac58ba6a966ef6b14e3) +++ Application/Ringtoets/src/Application.Ringtoets.Storage/Application.Ringtoets.Storage.csproj (.../Application.Ringtoets.Storage.csproj) (revision d09b94cdc3d7c18ffa7ce53b727c34d189f588a9) @@ -53,6 +53,7 @@ Properties\GlobalAssembly.cs + Index: Application/Ringtoets/src/Application.Ringtoets.Storage/Converters/FailureMechanismConverterBase.cs =================================================================== diff -u --- Application/Ringtoets/src/Application.Ringtoets.Storage/Converters/FailureMechanismConverterBase.cs (revision 0) +++ Application/Ringtoets/src/Application.Ringtoets.Storage/Converters/FailureMechanismConverterBase.cs (revision d09b94cdc3d7c18ffa7ce53b727c34d189f588a9) @@ -0,0 +1,71 @@ +// 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 Application.Ringtoets.Storage.DbContext; + +using Ringtoets.Common.Data.FailureMechanism; + +namespace Application.Ringtoets.Storage.Converters +{ + public abstract class FailureMechanismConverterBase : IEntityConverter where T : IFailureMechanism + { + public T ConvertEntityToModel(FailureMechanismEntity entity) + { + if (entity == null) + { + throw new ArgumentNullException("entity"); + } + if (entity.FailureMechanismType != (short)GetFailureMechanismType()) + { + throw new ArgumentException("Incorrect modelType", "entity"); + } + + T failureMechanism = ConstructFailureMechanism(); + failureMechanism.StorageId = entity.FailureMechanismEntityId; + failureMechanism.IsRelevant = entity.IsRelevant == 1; + + return failureMechanism; + } + + public void ConvertModelToEntity(T modelObject, FailureMechanismEntity entity) + { + if (modelObject == null) + { + throw new ArgumentNullException("modelObject"); + } + + if (entity == null) + { + throw new ArgumentNullException("entity"); + } + + entity.FailureMechanismEntityId = modelObject.StorageId; + entity.FailureMechanismType = (short)GetFailureMechanismType(); + entity.IsRelevant = modelObject.IsRelevant ? (byte)1 : (byte)0; + } + + protected abstract T ConstructFailureMechanism(); + + protected abstract FailureMechanismType GetFailureMechanismType(); + } +} \ No newline at end of file Index: Application/Ringtoets/src/Application.Ringtoets.Storage/Converters/PipingFailureMechanismConverter.cs =================================================================== diff -u -r35dd019140ab3d11241908744c43ddac43a8989e -rd09b94cdc3d7c18ffa7ce53b727c34d189f588a9 --- Application/Ringtoets/src/Application.Ringtoets.Storage/Converters/PipingFailureMechanismConverter.cs (.../PipingFailureMechanismConverter.cs) (revision 35dd019140ab3d11241908744c43ddac43a8989e) +++ Application/Ringtoets/src/Application.Ringtoets.Storage/Converters/PipingFailureMechanismConverter.cs (.../PipingFailureMechanismConverter.cs) (revision d09b94cdc3d7c18ffa7ce53b727c34d189f588a9) @@ -19,7 +19,6 @@ // Stichting Deltares and remain full property of Stichting Deltares at all times. // All rights reserved. -using System; using Application.Ringtoets.Storage.DbContext; using Ringtoets.Piping.Data; @@ -29,44 +28,16 @@ /// Converter for to /// and to . /// - public class PipingFailureMechanismConverter : IEntityConverter + public class PipingFailureMechanismConverter : FailureMechanismConverterBase { - public PipingFailureMechanism ConvertEntityToModel(FailureMechanismEntity entity) + protected override PipingFailureMechanism ConstructFailureMechanism() { - if (entity == null) - { - throw new ArgumentNullException("entity"); - } - - if (entity.FailureMechanismType != (int) FailureMechanismType.PipingFailureMechanism) - { - throw new ArgumentException(@"Incorrect modelType", "entity"); - } - - var failureMechanism = new PipingFailureMechanism - { - StorageId = entity.FailureMechanismEntityId, - IsRelevant = entity.IsRelevant == 1 - }; - - return failureMechanism; + return new PipingFailureMechanism(); } - public void ConvertModelToEntity(PipingFailureMechanism modelObject, FailureMechanismEntity entity) + protected override FailureMechanismType GetFailureMechanismType() { - if (modelObject == null) - { - throw new ArgumentNullException("modelObject"); - } - - if (entity == null) - { - throw new ArgumentNullException("entity"); - } - - entity.FailureMechanismEntityId = modelObject.StorageId; - entity.FailureMechanismType = (int) FailureMechanismType.PipingFailureMechanism; - entity.IsRelevant = modelObject.IsRelevant ? (byte)1 : (byte)0; + return FailureMechanismType.PipingFailureMechanism; } } } \ No newline at end of file Index: Application/Ringtoets/test/Application.Ringtoets.Storage.Test/Application.Ringtoets.Storage.Test.csproj =================================================================== diff -u -r6d514ec60f68620d78015ac58ba6a966ef6b14e3 -rd09b94cdc3d7c18ffa7ce53b727c34d189f588a9 --- Application/Ringtoets/test/Application.Ringtoets.Storage.Test/Application.Ringtoets.Storage.Test.csproj (.../Application.Ringtoets.Storage.Test.csproj) (revision 6d514ec60f68620d78015ac58ba6a966ef6b14e3) +++ Application/Ringtoets/test/Application.Ringtoets.Storage.Test/Application.Ringtoets.Storage.Test.csproj (.../Application.Ringtoets.Storage.Test.csproj) (revision d09b94cdc3d7c18ffa7ce53b727c34d189f588a9) @@ -81,6 +81,7 @@ + Index: Application/Ringtoets/test/Application.Ringtoets.Storage.Test/Converters/FailureMechanismConverterBaseTest.cs =================================================================== diff -u --- Application/Ringtoets/test/Application.Ringtoets.Storage.Test/Converters/FailureMechanismConverterBaseTest.cs (revision 0) +++ Application/Ringtoets/test/Application.Ringtoets.Storage.Test/Converters/FailureMechanismConverterBaseTest.cs (revision d09b94cdc3d7c18ffa7ce53b727c34d189f588a9) @@ -0,0 +1,181 @@ +using System; + +using Application.Ringtoets.Storage.Converters; +using Application.Ringtoets.Storage.DbContext; + +using Core.Common.TestUtil; + +using NUnit.Framework; + +using Rhino.Mocks; + +using Ringtoets.Common.Data.FailureMechanism; + +namespace Application.Ringtoets.Storage.Test.Converters +{ + [TestFixture] + public class FailureMechanismConverterBaseTest + { + [Test] + public void Constructor_ExpectedValues() + { + // Call + var converter = new SimpleFailureMechanismConverter(); + + // Assert + Assert.IsInstanceOf>(converter); + } + + [Test] + public void ConvertEntityToModel_EntityIsNull_ThrowArgumentNullException() + { + // Setup + var converter = new SimpleFailureMechanismConverter(); + + // Call + TestDelegate call = () => converter.ConvertEntityToModel(null); + + // Assert + Assert.Throws(call); + } + + [Test] + public void ConvertEntityToModel_EntityOfIncorrectType_ThrowArgumentException() + { + // Setup + var converter = new SimpleFailureMechanismConverter + { + FailureMechanismType = FailureMechanismType.AsphaltRevetmentFailureMechanism + }; + + var entity = new FailureMechanismEntity + { + FailureMechanismType = (short)FailureMechanismType.PipingFailureMechanism + }; + + // Call + TestDelegate call = () => converter.ConvertEntityToModel(entity); + + // Assert + const string expectedMessage = "Incorrect modelType"; + TestHelper.AssertThrowsArgumentExceptionAndTestMessage(call, expectedMessage); + } + + [Test] + [TestCase(0, FailureMechanismType.PipingFailureMechanism, 1)] + [TestCase(1, FailureMechanismType.OvertoppingFailureMechanism, 5)] + public void ConvertEntityToModel_ValidEntity_ReturnInitializedFailureMechanism( + byte isRelevant, FailureMechanismType type, long id) + { + // Setup + var mocks = new MockRepository(); + var failureMechanism = mocks.Stub(); + mocks.ReplayAll(); + + var converter = new SimpleFailureMechanismConverter + { + ConstructedFailureMechanismInstance = failureMechanism, + FailureMechanismType = type + }; + + var entity = new FailureMechanismEntity + { + IsRelevant = isRelevant, + FailureMechanismType = (short)type, + FailureMechanismEntityId = id + }; + + // Call + IFailureMechanism result = converter.ConvertEntityToModel(entity); + + // Assert + bool expectedIsRelevantValue = isRelevant == 1; + Assert.AreEqual(expectedIsRelevantValue, result.IsRelevant); + Assert.AreEqual(id, result.StorageId); + + mocks.VerifyAll(); + } + + [Test] + [TestCase(true, FailureMechanismType.MacrostabilityInwardsFailureMechanism, 123456789)] + [TestCase(false, FailureMechanismType.StructuresClosureFailureMechanism, 986532)] + public void ConvertModelToEntity_ValidFailureMechanism_ProperlyInitializeEntity( + bool isRelevant, FailureMechanismType type, long id) + { + // Setup + var mocks = new MockRepository(); + var failureMechanism = mocks.Stub(); + failureMechanism.IsRelevant = isRelevant; + failureMechanism.StorageId = id; + mocks.ReplayAll(); + + var converter = new SimpleFailureMechanismConverter + { + FailureMechanismType = type + }; + + var entity = new FailureMechanismEntity(); + + // Call + converter.ConvertModelToEntity(failureMechanism, entity); + + // Assert + byte expectedIsRelevantValue = isRelevant ? (byte)1 : (byte)0; + Assert.AreEqual(expectedIsRelevantValue, entity.IsRelevant); + Assert.AreEqual(id, entity.FailureMechanismEntityId); + Assert.AreEqual((short)type, entity.FailureMechanismType); + + mocks.VerifyAll(); + } + + [Test] + public void ConvertModelToEntity_FailureMechanismIsNull_ThrowArgumentNullException() + { + // Setup + var converter = new SimpleFailureMechanismConverter(); + + var entity = new FailureMechanismEntity(); + + // Call + TestDelegate call = () => converter.ConvertModelToEntity(null, entity); + + // Assert + Assert.Throws(call); + } + + [Test] + public void ConvertModelToEntity_EntityIsNull_ThrowArgumentNullException() + { + // Setup + var mocks = new MockRepository(); + var failureMechanism = mocks.Stub(); + mocks.ReplayAll(); + + var converter = new SimpleFailureMechanismConverter(); + + // Call + TestDelegate call = () => converter.ConvertModelToEntity(failureMechanism, null); + + // Assert + Assert.Throws(call); + + mocks.VerifyAll(); + } + + private class SimpleFailureMechanismConverter : FailureMechanismConverterBase where T : IFailureMechanism + { + protected override T ConstructFailureMechanism() + { + return ConstructedFailureMechanismInstance; + } + + protected override FailureMechanismType GetFailureMechanismType() + { + return FailureMechanismType; + } + + public T ConstructedFailureMechanismInstance { get; set; } + public FailureMechanismType FailureMechanismType { get; set; } + } + } +} \ No newline at end of file Index: Ringtoets/Common/src/Ringtoets.Common.Forms/TreeNodeInfos/DefaultFailureMechanismTreeNodeInfo.cs =================================================================== diff -u -r684b8c0ca681ea2da3cc988bd60e9223037e5a16 -rd09b94cdc3d7c18ffa7ce53b727c34d189f588a9 --- Ringtoets/Common/src/Ringtoets.Common.Forms/TreeNodeInfos/DefaultFailureMechanismTreeNodeInfo.cs (.../DefaultFailureMechanismTreeNodeInfo.cs) (revision 684b8c0ca681ea2da3cc988bd60e9223037e5a16) +++ Ringtoets/Common/src/Ringtoets.Common.Forms/TreeNodeInfos/DefaultFailureMechanismTreeNodeInfo.cs (.../DefaultFailureMechanismTreeNodeInfo.cs) (revision d09b94cdc3d7c18ffa7ce53b727c34d189f588a9) @@ -1,4 +1,25 @@ -using System; +// 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.Drawing; using System.Windows.Forms; @@ -12,15 +33,30 @@ namespace Ringtoets.Common.Forms.TreeNodeInfos { - public class DefaultFailureMechanismTreeNodeInfo : TreeNodeInfo where TContext : FailureMechanismContext where TFailureMechanism: IFailureMechanism + /// + /// This class provides an initial configuration of for + /// related presentation objects. + /// + /// The type of the presentation object for the failure mechanism. + /// The type of the failure mechanism. + public class DefaultFailureMechanismTreeNodeInfo : TreeNodeInfo where TContext : FailureMechanismContext where TFailureMechanism : IFailureMechanism { private readonly Func getEnabledFailureMechanismChildNodes; private readonly Func getEnabledFailureMechanismContextMenuStrip; private readonly IContextMenuBuilderProvider contextMenuBuilderProvider; + /// + /// Initializes a new instance of the class. + /// + /// The implementation of + /// when is true. Can be null. + /// The implementation of + /// when + /// is true. Can be null. + /// The provider of the . public DefaultFailureMechanismTreeNodeInfo(Func getEnabledFailureMechanismChildNodes, - Func getEnabledFailureMechanismContextMenuStrip, - IContextMenuBuilderProvider provider) + Func getEnabledFailureMechanismContextMenuStrip, + IContextMenuBuilderProvider provider) { this.getEnabledFailureMechanismChildNodes = getEnabledFailureMechanismChildNodes; this.getEnabledFailureMechanismContextMenuStrip = getEnabledFailureMechanismContextMenuStrip; @@ -49,9 +85,9 @@ { if (failureMechanismContext.WrappedData.IsRelevant) { - return getEnabledFailureMechanismChildNodes != null ? - getEnabledFailureMechanismChildNodes(failureMechanismContext) : - new object[0]; + return getEnabledFailureMechanismChildNodes != null ? + getEnabledFailureMechanismChildNodes(failureMechanismContext) : + new object[0]; } return GetDisbledFailureMechanismChildNodeObjects(failureMechanismContext);