// 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.Globalization; using System.IO; using System.Linq; using System.Xml; using System.Xml.Linq; using Core.Common.Base.Geometry; using Core.Common.TestUtil; using NUnit.Framework; using Ringtoets.Common.IO.Exceptions; using Ringtoets.Common.IO.SoilProfile; namespace Ringtoets.Common.IO.Test.SoilProfile { [TestFixture] public class SoilLayer2DGeometryReaderTest { [Test] public void Constructor_ReturnsNewInstance() { // Call var result = new SoilLayer2DGeometryReader(); // Assert Assert.NotNull(result); } [Test] public void Read_NullByteArray_ThrowsArgumentNullException() { // Setup var reader = new SoilLayer2DGeometryReader(); // Call TestDelegate test = () => reader.Read((byte[]) null); // Assert var exception = TestHelper.AssertThrowsArgumentExceptionAndTestMessage(test, "De geometrie is leeg."); Assert.AreEqual("geometry", exception.ParamName); } [Test] public void Read_NullXmlDocument_ThrowsArgumentNullException() { // Setup var reader = new SoilLayer2DGeometryReader(); // Call TestDelegate test = () => reader.Read((XDocument) null); // Assert string paramName = Assert.Throws(test).ParamName; Assert.AreEqual("geometry", paramName); } [Test] public void Read_XmlDocumentWithoutSaneContent_ThrowsSoilLayerConversionException() { // Setup XDocument xmlDoc = GetXmlDocument(""); var reader = new SoilLayer2DGeometryReader(); // Call TestDelegate test = () => reader.Read(xmlDoc); // Assert var exception = Assert.Throws(test); Assert.AreEqual("Het XML-document dat de geometrie beschrijft voor de laag is niet geldig.", exception.Message); } [Test] public void Read_XmlDocumentWithNoInnerLoops_ThrowsSoilLayerConversionException() { // Setup XDocument xmlDoc = GetXmlDocument(""); var reader = new SoilLayer2DGeometryReader(); // Call TestDelegate test = () => reader.Read(xmlDoc); // Assert var exception = Assert.Throws(test); Assert.AreEqual("Het XML-document dat de geometrie beschrijft voor de laag is niet geldig.", exception.Message); } [Test] public void Read_XmlDocumentWithNoOuterLoop_ThrowsSoilLayerConversionException() { // Setup XDocument xmlDoc = GetXmlDocument(""); var reader = new SoilLayer2DGeometryReader(); // Call TestDelegate test = () => reader.Read(xmlDoc); // Assert var exception = Assert.Throws(test); Assert.AreEqual("Het XML-document dat de geometrie beschrijft voor de laag is niet geldig.", exception.Message); } [Test] public void Read_XmlDocumentWithEmptyInnerLoopAndOuterLoop_ReturnsLayerWithEmptyInnerLoopAndEmptyOuterLoop() { // Setup XDocument xmlDoc = GetXmlDocument(""); var reader = new SoilLayer2DGeometryReader(); // Call SoilLayer2D result = reader.Read(xmlDoc); // Assert Assert.NotNull(result); CollectionAssert.IsEmpty(result.OuterLoop); Assert.AreEqual(1, result.InnerLoops.Count()); CollectionAssert.IsEmpty(result.InnerLoops.ElementAt(0)); } [Test] [TestCase("x")] [TestCase("")] public void Read_XmlDocumentWithInvalidPointCoordinate_ThrowsSoilLayerConversionException(string incorrectNumber) { // Setup XDocument xmlDoc = GetXmlDocument( "" + $"{incorrectNumber}1.2" + "1.21.2" + ""); var reader = new SoilLayer2DGeometryReader(); // Call TestDelegate test = () => reader.Read(xmlDoc); // Assert var exception = Assert.Throws(test); Assert.AreEqual("Coördinaat van een punt bevat ongeldige waarde.", exception.Message); } [Test] public void Read_XmlDocumentWithOverflowingPointCoordinate_ThrowsSoilLayerConversionException() { // Setup XDocument xmlDoc = GetXmlDocument( "" + $"{double.MaxValue}1.2" + "1.21.2" + ""); var reader = new SoilLayer2DGeometryReader(); // Call TestDelegate test = () => reader.Read(xmlDoc); // Assert var exception = Assert.Throws(test); Assert.AreEqual("Coördinaat van een punt bevat ongeldige waarde.", exception.Message); } [Test] [SetCulture("nl-NL")] public void Read_NLXmlDocumentPointInOuterLoop_ReturnsLayerWithOuterLoopWithPoint() { Read_XmlDocumentPointInOuterLoop_ReturnsLayerWithOuterLoopWithPoint(); } [Test] [SetCulture("en-US")] public void Read_ENXmlDocumentPointInOuterLoop_ReturnsLayerWithOuterLoopWithPoint() { Read_XmlDocumentPointInOuterLoop_ReturnsLayerWithOuterLoopWithPoint(); } [Test] public void Read_XmlDocumentPointsInInnerLoop_ReturnsLayerWithInnerLoopWithSegment() { // Setup var random = new Random(22); CultureInfo invariantCulture = CultureInfo.InvariantCulture; double x1 = random.NextDouble(); double x2 = random.NextDouble(); double y1 = random.NextDouble(); double y2 = random.NextDouble(); string x1String = XmlConvert.ToString(x1); string x2String = XmlConvert.ToString(x2); string y1String = XmlConvert.ToString(y1); string y2String = XmlConvert.ToString(y2); XDocument xmlDoc = GetXmlDocument( string.Format(invariantCulture, "" + "{0}0.1{1}" + "{2}0.1{3}" + "" + "{0}0.1{1}" + "{2}0.1{3}" + "", x1String, y1String, x2String, y2String)); var reader = new SoilLayer2DGeometryReader(); // Call SoilLayer2D result = reader.Read(xmlDoc); // Assert Assert.NotNull(result); var expectedSegment = new Segment2D(new Point2D(x1, y1), new Point2D(x2, y2)); var expectedCollection = new[] { new List { expectedSegment, expectedSegment } }; CollectionAssert.AreEqual(expectedCollection, result.InnerLoops); } [Test] public void Read_XmlDocumentSinglePointOuterLoopGeometryCurve_ThrowsSoilLayerConversionException() { // Setup XDocument xmlDoc = GetXmlDocument( "" + "10.11.1" + ""); var reader = new SoilLayer2DGeometryReader(); // Call TestDelegate test = () => reader.Read(xmlDoc); // Assert Assert.Throws(test); } [Test] public void Read_XmlDocumentSinglePointInnerLoopGeometryCurve_ThrowsSoilLayerConversionException() { // Setup XDocument xmlDoc = GetXmlDocument( "" + "00.11.1" + ""); var reader = new SoilLayer2DGeometryReader(); // Call TestDelegate test = () => reader.Read(xmlDoc); // Assert Assert.Throws(test); } [Test] public void Read_XmlDocumentEqualSegments_ReturnsTwoEqualSegments() { // Setup XDocument xmlDoc = GetXmlDocument( "" + "" + "001.1101.1" + "" + "" + "001.1101.1" + "" + ""); var reader = new SoilLayer2DGeometryReader(); // Call SoilLayer2D result = reader.Read(xmlDoc); // Assert Assert.AreEqual(2, result.OuterLoop.Count()); Assert.AreEqual(result.OuterLoop.ElementAt(0), result.OuterLoop.ElementAt(1)); } [Test] public void Read_XmlDocumentSegmentsNotSubsequentAndReversed_ReturnsSegmentsSubsequentInSameDirection() { // Setup XDocument xmlDoc = GetXmlDocument( "" + "" + "001.2511101.25" + "" + "" + "002.92001.25" + "" + "" + "002.924203.23" + "" + "" + "11101.254203.23" + "" + ""); var reader = new SoilLayer2DGeometryReader(); // Call SoilLayer2D result = reader.Read(xmlDoc); // Assert Assert.NotNull(result); var expectedSegments = new[] { new Segment2D(new Point2D(0, 1.25), new Point2D(111, 1.25)), new Segment2D(new Point2D(111, 1.25), new Point2D(42, 3.23)), new Segment2D(new Point2D(42, 3.23), new Point2D(0, 2.92)), new Segment2D(new Point2D(0, 2.92), new Point2D(0, 1.25)) }; CollectionAssert.AreEqual(expectedSegments, result.OuterLoop); } [Test] public void Read_XmlDocumentOneSegment_ThrowSoilLayerConversionException() { // Setup XDocument xmlDoc = GetXmlDocument( "" + "" + "001.2511101.25" + "" + ""); var reader = new SoilLayer2DGeometryReader(); // Call TestDelegate test = () => reader.Read(xmlDoc); // Assert var exception = Assert.Throws(test); Assert.AreEqual("Het XML-document dat de geometrie beschrijft voor de laag is niet geldig.", exception.Message); } [Test] public void Read_XmlDocumentSegmentsNotConnected_ThrowSoilLayerConversionException() { // Setup XDocument xmlDoc = GetXmlDocument( "" + "" + "001.2511101.25" + "" + "" + "002.924203.23" + "" + ""); var reader = new SoilLayer2DGeometryReader(); // Call TestDelegate test = () => reader.Read(xmlDoc); // Assert var exception = Assert.Throws(test); Assert.AreEqual("Het XML-document dat de geometrie beschrijft voor de laag is niet geldig.", exception.Message); } private static void Read_XmlDocumentPointInOuterLoop_ReturnsLayerWithOuterLoopWithPoint() { // Setup var random = new Random(22); CultureInfo invariantCulture = CultureInfo.InvariantCulture; double x1 = random.NextDouble(); double x2 = random.NextDouble(); double y1 = random.NextDouble(); double y2 = random.NextDouble(); string x1String = XmlConvert.ToString(x1); string x2String = XmlConvert.ToString(x2); string y1String = XmlConvert.ToString(y1); string y2String = XmlConvert.ToString(y2); XDocument bytes = GetXmlDocument( string.Format(invariantCulture, "" + "{0}0.1{1}" + "{2}0.1{3}" + "" + "{0}0.1{1}" + "{2}0.1{3}" + "", x1String, y1String, x2String, y2String)); var reader = new SoilLayer2DGeometryReader(); // Call SoilLayer2D result = reader.Read(bytes); // Assert Assert.NotNull(result); var expectedSegment = new Segment2D(new Point2D(x1, y1), new Point2D(x2, y2)); CollectionAssert.AreEqual(new List { expectedSegment, expectedSegment }, result.OuterLoop); } /// /// Takes a which describes an XML document and returns /// an from this. /// /// The to convert to an . /// The constructed from . /// Thrown when is null. /// Thrown when does not describe /// a valid XML document. private static XDocument GetXmlDocument(string xmlString) { return XDocument.Load(new MemoryStream(GetByteArray(xmlString))); } /// /// Takes a and returns an of /// which contains the same information as the original . /// /// The to convert to an of /// . /// The of constructed from /// . private static byte[] GetByteArray(string str) { var bytes = new byte[str.Length * sizeof(char)]; Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length); return bytes; } } }