Index: Application/Ringtoets/src/Application.Ringtoets.Storage/Application.Ringtoets.Storage.csproj =================================================================== diff -u -r8130cde70fa4a5ef16789bad2a8b64175d77d9a2 -rba2f4f4e1c7a194fbe370fe3beee83326e454e5d --- Application/Ringtoets/src/Application.Ringtoets.Storage/Application.Ringtoets.Storage.csproj (.../Application.Ringtoets.Storage.csproj) (revision 8130cde70fa4a5ef16789bad2a8b64175d77d9a2) +++ Application/Ringtoets/src/Application.Ringtoets.Storage/Application.Ringtoets.Storage.csproj (.../Application.Ringtoets.Storage.csproj) (revision ba2f4f4e1c7a194fbe370fe3beee83326e454e5d) @@ -53,6 +53,8 @@ Properties\GlobalAssembly.cs + + Index: Application/Ringtoets/src/Application.Ringtoets.Storage/BinaryConverters/Point2DBinaryConverter.cs =================================================================== diff -u --- Application/Ringtoets/src/Application.Ringtoets.Storage/BinaryConverters/Point2DBinaryConverter.cs (revision 0) +++ Application/Ringtoets/src/Application.Ringtoets.Storage/BinaryConverters/Point2DBinaryConverter.cs (revision ba2f4f4e1c7a194fbe370fe3beee83326e454e5d) @@ -0,0 +1,105 @@ +// 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 System.Runtime.Serialization.Formatters.Binary; + +using Core.Common.Base.Geometry; + +namespace Application.Ringtoets.Storage.BinaryConverters +{ + /// + /// Converter class that converts between a collection of and a + /// binary representation of that data. + /// + public class Point2DBinaryConverter + { + /// + /// Converts the collection of to binary data. + /// + /// The points. + /// The binary data. + public byte[] ToBytes(IEnumerable points) + { + if (points == null) + { + throw new ArgumentNullException("points"); + } + + using (MemoryStream memoryStream = new MemoryStream()) + { + SerializablePoint2D[] serializableData = GetSerializableData(points); + BinaryFormatter formatter = new BinaryFormatter(); + formatter.Serialize(memoryStream, serializableData); + return memoryStream.ToArray(); + } + } + + /// + /// Converts the binary data to a collection of . + /// + /// The binary data. + /// The collection of . + public Point2D[] ToData(byte[] serializedData) + { + if (serializedData == null) + { + throw new ArgumentNullException("serializedData"); + } + + using (MemoryStream memoryStream = new MemoryStream()) + { + memoryStream.Write(serializedData, 0, serializedData.Length); + memoryStream.Seek(0, SeekOrigin.Begin); + BinaryFormatter formatter = new BinaryFormatter(); + return ((SerializablePoint2D[])formatter.Deserialize(memoryStream)) + .Select(sp => sp.ToPoint2D()) + .ToArray(); + } + } + + private static SerializablePoint2D[] GetSerializableData(IEnumerable points) + { + return points.Select(p => new SerializablePoint2D(p)).ToArray(); + } + + [Serializable] + private class SerializablePoint2D + { + private readonly double x; + private readonly double y; + + public SerializablePoint2D(Point2D point2D) + { + x = point2D.X; + y = point2D.Y; + } + + public Point2D ToPoint2D() + { + return new Point2D(x, y); + } + } + } +} \ No newline at end of file Index: Application/Ringtoets/src/Application.Ringtoets.Storage/BinaryConverters/RoughnessPointBinaryConverter.cs =================================================================== diff -u --- Application/Ringtoets/src/Application.Ringtoets.Storage/BinaryConverters/RoughnessPointBinaryConverter.cs (revision 0) +++ Application/Ringtoets/src/Application.Ringtoets.Storage/BinaryConverters/RoughnessPointBinaryConverter.cs (revision ba2f4f4e1c7a194fbe370fe3beee83326e454e5d) @@ -0,0 +1,109 @@ +// 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 System.Runtime.Serialization.Formatters.Binary; + +using Core.Common.Base.Geometry; + +using Ringtoets.GrassCoverErosionInwards.Data; + +namespace Application.Ringtoets.Storage.BinaryConverters +{ + /// + /// Converter class that converts between a collection of + /// and a binary representation of that data. + /// + public class RoughnessPointBinaryConverter + { + /// + /// Converts the collection of to binary data. + /// + /// The points. + /// The binary data. + public byte[] ToBytes(IEnumerable points) + { + if (points == null) + { + throw new ArgumentNullException("points"); + } + + using (MemoryStream memoryStream = new MemoryStream()) + { + SerializableRoughnessPoint[] serializableData = GetSerializableData(points); + BinaryFormatter formatter = new BinaryFormatter(); + formatter.Serialize(memoryStream, serializableData); + return memoryStream.ToArray(); + } + } + + /// + /// Converts the binary data to a collection of . + /// + /// The binary data. + /// The collection of . + public RoughnessPoint[] ToData(byte[] serializedData) + { + if (serializedData == null) + { + throw new ArgumentNullException("serializedData"); + } + + using (MemoryStream memoryStream = new MemoryStream()) + { + memoryStream.Write(serializedData, 0, serializedData.Length); + memoryStream.Seek(0, SeekOrigin.Begin); + BinaryFormatter formatter = new BinaryFormatter(); + return ((SerializableRoughnessPoint[])formatter.Deserialize(memoryStream)) + .Select(sp => sp.ToRoughnessPoint()) + .ToArray(); + } + } + + private static SerializableRoughnessPoint[] GetSerializableData(IEnumerable points) + { + return points.Select(p => new SerializableRoughnessPoint(p)).ToArray(); + } + + [Serializable] + private class SerializableRoughnessPoint + { + private readonly double x; + private readonly double y; + private readonly double roughness; + + public SerializableRoughnessPoint(RoughnessPoint point) + { + x = point.Point.X; + y = point.Point.Y; + roughness = point.Roughness; + } + + public RoughnessPoint ToRoughnessPoint() + { + return new RoughnessPoint(new Point2D(x, y), roughness); + } + } + } +} \ No newline at end of file Index: Application/Ringtoets/test/Application.Ringtoets.Storage.Test/Application.Ringtoets.Storage.Test.csproj =================================================================== diff -u -r02f18ee78d2e0520b90c8bfcb13f563e7d41365a -rba2f4f4e1c7a194fbe370fe3beee83326e454e5d --- Application/Ringtoets/test/Application.Ringtoets.Storage.Test/Application.Ringtoets.Storage.Test.csproj (.../Application.Ringtoets.Storage.Test.csproj) (revision 02f18ee78d2e0520b90c8bfcb13f563e7d41365a) +++ Application/Ringtoets/test/Application.Ringtoets.Storage.Test/Application.Ringtoets.Storage.Test.csproj (.../Application.Ringtoets.Storage.Test.csproj) (revision ba2f4f4e1c7a194fbe370fe3beee83326e454e5d) @@ -82,6 +82,8 @@ + + Index: Application/Ringtoets/test/Application.Ringtoets.Storage.Test/BinaryConverters/Point2DBinaryConverterTest.cs =================================================================== diff -u --- Application/Ringtoets/test/Application.Ringtoets.Storage.Test/BinaryConverters/Point2DBinaryConverterTest.cs (revision 0) +++ Application/Ringtoets/test/Application.Ringtoets.Storage.Test/BinaryConverters/Point2DBinaryConverterTest.cs (revision ba2f4f4e1c7a194fbe370fe3beee83326e454e5d) @@ -0,0 +1,104 @@ +// 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.BinaryConverters; + +using Core.Common.Base.Geometry; + +using NUnit.Framework; + +namespace Application.Ringtoets.Storage.Test.BinaryConverters +{ + [TestFixture] + public class Point2DBinaryConverterTest + { + [Test] + public void ToBytes_PointsCollectionNull_ThrowArgumentNullException() + { + // Setup + var converter = new Point2DBinaryConverter(); + + // Call + TestDelegate call = () => converter.ToBytes(null); + + // Assert + string paramName = Assert.Throws(call).ParamName; + Assert.AreEqual("points", paramName); + } + + [Test] + public void ToData_BinaryDataNull_ThrowArgumentNullException() + { + // Setup + var converter = new Point2DBinaryConverter(); + + // Call + TestDelegate call = () => converter.ToData(null); + + // Assert + string paramName = Assert.Throws(call).ParamName; + Assert.AreEqual("serializedData", paramName); + } + + [Test] + public void GivenArrayWithPoint2D_WhenConvertingRoundTrip_ThenEqualArrayOfPoints2D() + { + // Setup + var original = new[] + { + new Point2D(-7.7, -6.6), + new Point2D(-5.5, -4.4), + new Point2D(-3.3, -2.2), + new Point2D(-1.1, 0.0), + new Point2D(1.1, 2.2), + new Point2D(3.3, 4.4), + new Point2D(5.5, 6.6), + new Point2D(7.7, 8.8), + new Point2D(9.9, 10.10) + }; + var converter = new Point2DBinaryConverter(); + + // Call + byte[] bytes = converter.ToBytes(original); + Point2D[] roundtripResult = converter.ToData(bytes); + + // Assert + CollectionAssert.AreEqual(original, roundtripResult); + } + + [Test] + public void GivenEmptyArray_WhenConvertingRoundTrip_ThenReturnEmptyArray() + { + // Setup + var original = new Point2D[0]; + var converter = new Point2DBinaryConverter(); + + // Call + byte[] bytes = converter.ToBytes(original); + Point2D[] roundtripResult = converter.ToData(bytes); + + // Assert + CollectionAssert.IsEmpty(roundtripResult); + } + } +} \ No newline at end of file Index: Application/Ringtoets/test/Application.Ringtoets.Storage.Test/BinaryConverters/RoughnessPointBinaryConverterTest.cs =================================================================== diff -u --- Application/Ringtoets/test/Application.Ringtoets.Storage.Test/BinaryConverters/RoughnessPointBinaryConverterTest.cs (revision 0) +++ Application/Ringtoets/test/Application.Ringtoets.Storage.Test/BinaryConverters/RoughnessPointBinaryConverterTest.cs (revision ba2f4f4e1c7a194fbe370fe3beee83326e454e5d) @@ -0,0 +1,121 @@ +// 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; + +using Application.Ringtoets.Storage.BinaryConverters; + +using Core.Common.Base.Geometry; + +using NUnit.Framework; + +using Ringtoets.GrassCoverErosionInwards.Data; + +namespace Application.Ringtoets.Storage.Test.BinaryConverters +{ + [TestFixture] + public class RoughnessPointBinaryConverterTest + { + [Test] + public void ToBytes_PointsCollectionNull_ThrowArgumentNullException() + { + // Setup + var converter = new RoughnessPointBinaryConverter(); + + // Call + TestDelegate call = () => converter.ToBytes(null); + + // Assert + string paramName = Assert.Throws(call).ParamName; + Assert.AreEqual("points", paramName); + } + + [Test] + public void ToData_BinaryDataNull_ThrowArgumentNullException() + { + // Setup + var converter = new RoughnessPointBinaryConverter(); + + // Call + TestDelegate call = () => converter.ToData(null); + + // Assert + string paramName = Assert.Throws(call).ParamName; + Assert.AreEqual("serializedData", paramName); + } + + [Test] + public void GivenArrayWithPoint2D_WhenConvertingRoundTrip_ThenEqualArrayOfPoints2D() + { + // Setup + var original = new[] + { + new RoughnessPoint(new Point2D(-7.7, -6.6), 0.5), + new RoughnessPoint(new Point2D(-5.5, -4.4), 0.6), + new RoughnessPoint(new Point2D(-3.3, -2.2), 0.7), + new RoughnessPoint(new Point2D(-1.1, 0.0), 0.8), + new RoughnessPoint(new Point2D(1.1, 2.2), 0.9), + new RoughnessPoint(new Point2D(3.3, 4.4), 1.0), + new RoughnessPoint(new Point2D(5.5, 6.6), 0.9), + new RoughnessPoint(new Point2D(7.7, 8.8), 0.8), + new RoughnessPoint(new Point2D(9.9, 10.10), 0.7) + }; + var converter = new RoughnessPointBinaryConverter(); + + // Call + byte[] bytes = converter.ToBytes(original); + RoughnessPoint[] roundtripResult = converter.ToData(bytes); + + // Assert + CollectionAssert.AreEqual(original, roundtripResult, new RoughnessPointComparer()); + } + + [Test] + public void GivenEmptyArray_WhenConvertingRoundTrip_ThenReturnEmptyArray() + { + // Setup + var original = new RoughnessPoint[0]; + var converter = new RoughnessPointBinaryConverter(); + + // Call + byte[] bytes = converter.ToBytes(original); + RoughnessPoint[] roundtripResult = converter.ToData(bytes); + + // Assert + CollectionAssert.IsEmpty(roundtripResult); + } + + private class RoughnessPointComparer : IComparer + { + public int Compare(object x, object y) + { + RoughnessPoint x1 = (RoughnessPoint)x; + RoughnessPoint y1 = (RoughnessPoint)y; + if (x1.Point.Equals(y1.Point) && x1.Roughness.Equals(y1.Roughness)) + { + return 0; + } + return 1; + } + } + } +} \ No newline at end of file