Index: Ringtoets/Common/src/Ringtoets.Common.Forms/Ringtoets.Common.Forms.csproj =================================================================== diff -u -r26af58f85567c97bc68cc744a25334c2bd0b0fca -r650ed7277d1de02c544ed73e34ea7bd997d320fc --- Ringtoets/Common/src/Ringtoets.Common.Forms/Ringtoets.Common.Forms.csproj (.../Ringtoets.Common.Forms.csproj) (revision 26af58f85567c97bc68cc744a25334c2bd0b0fca) +++ Ringtoets/Common/src/Ringtoets.Common.Forms/Ringtoets.Common.Forms.csproj (.../Ringtoets.Common.Forms.csproj) (revision 650ed7277d1de02c544ed73e34ea7bd997d320fc) @@ -115,6 +115,7 @@ Form + Index: Ringtoets/Common/src/Ringtoets.Common.Forms/TypeConverters/BackgroundDataConverter.cs =================================================================== diff -u --- Ringtoets/Common/src/Ringtoets.Common.Forms/TypeConverters/BackgroundDataConverter.cs (revision 0) +++ Ringtoets/Common/src/Ringtoets.Common.Forms/TypeConverters/BackgroundDataConverter.cs (revision 650ed7277d1de02c544ed73e34ea7bd997d320fc) @@ -0,0 +1,142 @@ +// 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 Core.Components.Gis.Data; +using Ringtoets.Common.Data.AssessmentSection; + +namespace Ringtoets.Common.Forms.TypeConverters +{ + /// + /// Converter to convert to + /// and back. + /// + public static class BackgroundDataConverter + { + /// + /// Converts to . + /// + /// The to convert. + /// The converted . + /// Thrown when + /// is null. + public static BackgroundData ConvertTo(ImageBasedMapData mapData) + { + if (mapData == null) + { + throw new ArgumentNullException(nameof(mapData)); + } + + var backgroundData = new BackgroundData + { + Name = mapData.Name, + IsConfigured = mapData.IsConfigured, + IsVisible = mapData.IsVisible, + Transparency = mapData.Transparency + }; + + var wmtsMapData = mapData as WmtsMapData; + if (wmtsMapData != null) + { + ConvertWmtsMapDataParameters(wmtsMapData, backgroundData); + } + var wellKnownMapData = mapData as WellKnownTileSourceMapData; + if (wellKnownMapData != null) + { + ConvertWellKnownMapDataParameters(wellKnownMapData, backgroundData); + } + + return backgroundData; + } + + /// + /// Converts to . + /// + /// The to convert. + /// The converted . + /// Thrown when + /// is null. + public static ImageBasedMapData ConvertFrom(BackgroundData backgroundData) + { + if (backgroundData == null) + { + throw new ArgumentNullException(nameof(backgroundData)); + } + + ImageBasedMapData mapData; + + switch (backgroundData.BackgroundMapDataType) + { + case BackgroundMapDataType.Wmts: + mapData = CreateWmtsMapData(backgroundData); + break; + case BackgroundMapDataType.WellKnown: + mapData = CreateWellKnownMapdata(backgroundData); + break; + default: + throw new NotSupportedException(); + } + + mapData.Name = backgroundData.Name; + mapData.IsVisible = backgroundData.IsVisible; + mapData.Transparency = backgroundData.Transparency; + + return mapData; + } + + private static void ConvertWellKnownMapDataParameters(WellKnownTileSourceMapData mapData, BackgroundData backgroundData) + { + backgroundData.BackgroundMapDataType = BackgroundMapDataType.WellKnown; + backgroundData.Parameters[BackgroundDataIdentifiers.WellKnownTileSource] = ((int) mapData.TileSource).ToString(); + } + + private static void ConvertWmtsMapDataParameters(WmtsMapData mapData, BackgroundData backgroundData) + { + backgroundData.BackgroundMapDataType = BackgroundMapDataType.Wmts; + + if (backgroundData.IsConfigured) + { + backgroundData.Parameters[BackgroundDataIdentifiers.SourceCapabilitiesUrl] = mapData.SourceCapabilitiesUrl; + backgroundData.Parameters[BackgroundDataIdentifiers.SelectedCapabilityIdentifier] = mapData.SelectedCapabilityIdentifier; + backgroundData.Parameters[BackgroundDataIdentifiers.PreferredFormat] = mapData.PreferredFormat; + } + } + + private static WmtsMapData CreateWmtsMapData(BackgroundData backgroundData) + { + WmtsMapData wmtsMapData = WmtsMapData.CreateUnconnectedMapData(); + + if (backgroundData.IsConfigured) + { + wmtsMapData.Configure(backgroundData.Parameters[BackgroundDataIdentifiers.SourceCapabilitiesUrl], + backgroundData.Parameters[BackgroundDataIdentifiers.SelectedCapabilityIdentifier], + backgroundData.Parameters[BackgroundDataIdentifiers.PreferredFormat]); + } + return wmtsMapData; + } + + private static WellKnownTileSourceMapData CreateWellKnownMapdata(BackgroundData backgroundData) + { + var tileSource = (WellKnownTileSource)Convert.ToInt32(backgroundData.Parameters[BackgroundDataIdentifiers.WellKnownTileSource]); + return new WellKnownTileSourceMapData(tileSource); + } + } +} \ No newline at end of file Index: Ringtoets/Common/test/Ringtoets.Common.Data.TestUtil/BackgroundDataTestDataGenerator.cs =================================================================== diff -u -r26af58f85567c97bc68cc744a25334c2bd0b0fca -r650ed7277d1de02c544ed73e34ea7bd997d320fc --- Ringtoets/Common/test/Ringtoets.Common.Data.TestUtil/BackgroundDataTestDataGenerator.cs (.../BackgroundDataTestDataGenerator.cs) (revision 26af58f85567c97bc68cc744a25334c2bd0b0fca) +++ Ringtoets/Common/test/Ringtoets.Common.Data.TestUtil/BackgroundDataTestDataGenerator.cs (.../BackgroundDataTestDataGenerator.cs) (revision 650ed7277d1de02c544ed73e34ea7bd997d320fc) @@ -19,6 +19,7 @@ // Stichting Deltares and remain full property of Stichting Deltares at all times. // All rights reserved. +using Core.Common.Utils.Reflection; using Core.Components.Gis.Data; using Ringtoets.Common.Data.AssessmentSection; @@ -68,7 +69,7 @@ BackgroundMapDataType = BackgroundMapDataType.WellKnown, IsConfigured = true, IsVisible = true, - Name = wellKnownTileSource.ToString(), + Name = TypeUtils.GetDisplayName(wellKnownTileSource), Parameters = { { BackgroundDataIdentifiers.WellKnownTileSource, ((int) wellKnownTileSource).ToString() } Index: Ringtoets/Common/test/Ringtoets.Common.Data.TestUtil/Ringtoets.Common.Data.TestUtil.csproj =================================================================== diff -u -r5735c9b5c663a0bc7762d1b1eb1483e5c4cfe526 -r650ed7277d1de02c544ed73e34ea7bd997d320fc --- Ringtoets/Common/test/Ringtoets.Common.Data.TestUtil/Ringtoets.Common.Data.TestUtil.csproj (.../Ringtoets.Common.Data.TestUtil.csproj) (revision 5735c9b5c663a0bc7762d1b1eb1483e5c4cfe526) +++ Ringtoets/Common/test/Ringtoets.Common.Data.TestUtil/Ringtoets.Common.Data.TestUtil.csproj (.../Ringtoets.Common.Data.TestUtil.csproj) (revision 650ed7277d1de02c544ed73e34ea7bd997d320fc) @@ -80,6 +80,10 @@ {3BBFD65B-B277-4E50-AE6D-BD24C3434609} Core.Common.Base + + {F49BD8B2-332A-4C91-A196-8CCE0A2C7D98} + Core.Common.Utils + {318ba582-88c9-4816-a54a-a7e431461de3} Core.Components.Gis Index: Ringtoets/Common/test/Ringtoets.Common.Forms.Test/Ringtoets.Common.Forms.Test.csproj =================================================================== diff -u -r26af58f85567c97bc68cc744a25334c2bd0b0fca -r650ed7277d1de02c544ed73e34ea7bd997d320fc --- Ringtoets/Common/test/Ringtoets.Common.Forms.Test/Ringtoets.Common.Forms.Test.csproj (.../Ringtoets.Common.Forms.Test.csproj) (revision 26af58f85567c97bc68cc744a25334c2bd0b0fca) +++ Ringtoets/Common/test/Ringtoets.Common.Forms.Test/Ringtoets.Common.Forms.Test.csproj (.../Ringtoets.Common.Forms.Test.csproj) (revision 650ed7277d1de02c544ed73e34ea7bd997d320fc) @@ -102,6 +102,7 @@ + Index: Ringtoets/Common/test/Ringtoets.Common.Forms.Test/TypeConverters/BackgroundDataConverterTest.cs =================================================================== diff -u --- Ringtoets/Common/test/Ringtoets.Common.Forms.Test/TypeConverters/BackgroundDataConverterTest.cs (revision 0) +++ Ringtoets/Common/test/Ringtoets.Common.Forms.Test/TypeConverters/BackgroundDataConverterTest.cs (revision 650ed7277d1de02c544ed73e34ea7bd997d320fc) @@ -0,0 +1,168 @@ +// 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 Core.Components.Gis.Data; +using NUnit.Framework; +using Ringtoets.Common.Data.AssessmentSection; +using Ringtoets.Common.Data.TestUtil; +using Ringtoets.Common.Forms.TestUtil; +using Ringtoets.Common.Forms.TypeConverters; + +namespace Ringtoets.Common.Forms.Test.TypeConverters +{ + [TestFixture] + public class BackgroundDataConverterTest + { + [Test] + public void ConvertTo_MapDataNull_ThrowArgumentNullException() + { + // Call + TestDelegate test = () => BackgroundDataConverter.ConvertTo(null); + + // Assert + var exception = Assert.Throws(test); + Assert.AreEqual("mapData", exception.ParamName); + } + + [Test] + [TestCase(true)] + [TestCase(false)] + public void ConvertTo_WmtsMapData_ReturnBackgroundData(bool configured) + { + // Setup + WmtsMapData mapData = configured + ? WmtsMapData.CreateDefaultPdokMapData() + : WmtsMapData.CreateUnconnectedMapData(); + + // Call + BackgroundData backgroundData = BackgroundDataConverter.ConvertTo(mapData); + + // Assert + Assert.AreEqual(mapData.Name, backgroundData.Name); + Assert.AreEqual(mapData.IsVisible, backgroundData.IsVisible); + Assert.AreEqual(mapData.IsConfigured, backgroundData.IsConfigured); + Assert.AreEqual(mapData.Transparency, backgroundData.Transparency); + Assert.AreEqual(BackgroundMapDataType.Wmts, backgroundData.BackgroundMapDataType); + + if (configured) + { + Assert.AreEqual(3, backgroundData.Parameters.Count); + Assert.AreEqual(mapData.SourceCapabilitiesUrl, backgroundData.Parameters[BackgroundDataIdentifiers.SourceCapabilitiesUrl]); + Assert.AreEqual(mapData.SelectedCapabilityIdentifier, backgroundData.Parameters[BackgroundDataIdentifiers.SelectedCapabilityIdentifier]); + Assert.AreEqual(mapData.PreferredFormat, backgroundData.Parameters[BackgroundDataIdentifiers.PreferredFormat]); + } + else + { + CollectionAssert.IsEmpty(backgroundData.Parameters); + } + } + + [Test] + public void ConvertTo_WellKnownMapData_ReturnBackgroundData() + { + // Setup + var mapData = new WellKnownTileSourceMapData(WellKnownTileSource.BingAerial); + + // Call + BackgroundData backgroundData = BackgroundDataConverter.ConvertTo(mapData); + + // Assert + Assert.AreEqual(mapData.Name, backgroundData.Name); + Assert.AreEqual(mapData.IsVisible, backgroundData.IsVisible); + Assert.AreEqual(mapData.IsConfigured, backgroundData.IsConfigured); + Assert.AreEqual(mapData.Transparency, backgroundData.Transparency); + Assert.AreEqual(BackgroundMapDataType.WellKnown, backgroundData.BackgroundMapDataType); + + Assert.AreEqual(1, backgroundData.Parameters.Count); + var wellKnownTileSource = (WellKnownTileSource) Convert.ToInt32(backgroundData.Parameters[BackgroundDataIdentifiers.WellKnownTileSource]); + Assert.AreEqual(mapData.TileSource, wellKnownTileSource); + } + + [Test] + public void ConvertFrom_BackgroundDataNull_ThrowArgumentNullException() + { + // Call + TestDelegate test = () => BackgroundDataConverter.ConvertFrom(null); + + // Assert + var exception = Assert.Throws(test); + Assert.AreEqual("backgroundData", exception.ParamName); + } + + [Test] + public void ConvertFrom_UnknownBackgroundMapDataType_ThrowNotSupportedException() + { + // Setup + var backgroundData = new BackgroundData + { + BackgroundMapDataType = (BackgroundMapDataType) 10 + }; + + // Call + TestDelegate test = () => BackgroundDataConverter.ConvertFrom(backgroundData); + + // Assert + Assert.Throws(test); + } + + [Test] + [TestCaseSource(nameof(WmtsMapDatas))] + public void ConvertFrom_BackgroundData_ReturnWmtsMapData(WmtsMapData mapData) + { + // Setup + BackgroundData backgroundData = BackgroundDataTestDataGenerator.GetWmtsBackgroundMapData(mapData); + + // Call + ImageBasedMapData convertedMapData = BackgroundDataConverter.ConvertFrom(backgroundData); + + // Assert + MapDataTestHelper.AssertImageBasedMapData(mapData, convertedMapData); + } + + [Test] + public void ConvertFrom_BackgroundData_ReturnWellKnownMapData() + { + // Setup + BackgroundData backgroundData = BackgroundDataTestDataGenerator.GetWellKnownBackgroundMapData(); + + // Call + ImageBasedMapData convertedMapData = BackgroundDataConverter.ConvertFrom(backgroundData); + + // Assert + var expectedMapData = new WellKnownTileSourceMapData(WellKnownTileSource.BingAerial); + MapDataTestHelper.AssertImageBasedMapData(expectedMapData, convertedMapData); + + } + + private static IEnumerable WmtsMapDatas + { + get + { + yield return new TestCaseData(WmtsMapData.CreateDefaultPdokMapData()) + .SetName("Configured WMTS map data."); + yield return new TestCaseData(WmtsMapData.CreateUnconnectedMapData()) + .SetName("Not configured WMTS map data."); + } + } + } +} \ No newline at end of file