Index: Ringtoets/Integration/test/Ringtoets.Integration.Forms.Test/WmtsConnectionInfoReaderTest.cs =================================================================== diff -u -r41837216a2f3d66c3084500f4dcef57c478bfef5 -r4f2d8bc47cb1935643c427e0d3bd69a06bddaa7c --- Ringtoets/Integration/test/Ringtoets.Integration.Forms.Test/WmtsConnectionInfoReaderTest.cs (.../WmtsConnectionInfoReaderTest.cs) (revision 41837216a2f3d66c3084500f4dcef57c478bfef5) +++ Ringtoets/Integration/test/Ringtoets.Integration.Forms.Test/WmtsConnectionInfoReaderTest.cs (.../WmtsConnectionInfoReaderTest.cs) (revision 4f2d8bc47cb1935643c427e0d3bd69a06bddaa7c) @@ -20,8 +20,10 @@ // All rights reserved. using System; -using System.Collections.Generic; using System.IO; +using System.Linq; +using System.Xml; +using Core.Common.IO.Exceptions; using Core.Common.TestUtil; using NUnit.Framework; @@ -86,17 +88,33 @@ } [Test] + public void ReadWmtsConnectionInfos_FileMissing_ThrowsCriticalFileReadException() + { + // Setup + string filePath = Path.Combine(testPath, Path.GetRandomFileName()); + var reader = new WmtsConnectionInfoReader(); + + // Call + TestDelegate call = () => reader.ReadWmtsConnectionInfos(filePath); + + // Assert + var expectedMessage = $"Fout bij het lezen van bestand '{filePath}': het bestand bestaat niet."; + string actualMessage = Assert.Throws(call).Message; + Assert.AreEqual(expectedMessage, actualMessage); + } + + [Test] public void ReadWmtsConnectionInfos_FileWithTwoWmtsConnectionInfos_ReturnsExpectedWmtsConnectionInfos() { // Setup string filePath = Path.Combine(testPath, "twoValidWmtsConnectionInfos.txt"); var reader = new WmtsConnectionInfoReader(); // Call - List readConnectionInfos = reader.ReadWmtsConnectionInfos(filePath); + WmtsConnectionInfo[] readConnectionInfos = reader.ReadWmtsConnectionInfos(filePath).ToArray(); // Assert - Assert.AreEqual(2, readConnectionInfos.Count); + Assert.AreEqual(2, readConnectionInfos.Length); var firstExpected = new WmtsConnectionInfo(@"Actueel Hoogtebestand Nederland (AHN1)", @"https://geodata.nationaalgeoregister.nl/tiles/service/wmts/ahn1?request=GetCapabilities"); var secondExpected = new WmtsConnectionInfo(@"Zeegraskartering", @"https://geodata.nationaalgeoregister.nl/zeegraskartering/wfs?request=GetCapabilities"); @@ -112,17 +130,133 @@ var reader = new WmtsConnectionInfoReader(); // Call - List readConnectionInfos = reader.ReadWmtsConnectionInfos(filePath); + WmtsConnectionInfo[] readConnectionInfos = reader.ReadWmtsConnectionInfos(filePath).ToArray(); // Assert - Assert.AreEqual(2, readConnectionInfos.Count); + Assert.AreEqual(2, readConnectionInfos.Length); var firstExpected = new WmtsConnectionInfo(@"Actueel Hoogtebestand Nederland (AHN1)", @"https://geodata.nationaalgeoregister.nl/tiles/service/wmts/ahn1?request=GetCapabilities"); var secondExpected = new WmtsConnectionInfo(@"Zeegraskartering", @"https://geodata.nationaalgeoregister.nl/zeegraskartering/wfs?request=GetCapabilities"); AssertAreEqual(firstExpected, readConnectionInfos[0]); AssertAreEqual(secondExpected, readConnectionInfos[1]); } + [Test] + public void ReadWmtsConnectionInfos_FileWithoutWmtsConnectionInfos_ReturnsEmptyList() + { + // Setup + string filePath = Path.Combine(testPath, "WmtsConnectionInfosZeroWmtsConnections.txt"); + var reader = new WmtsConnectionInfoReader(); + + // Call + WmtsConnectionInfo[] readConnectionInfos = reader.ReadWmtsConnectionInfos(filePath).ToArray(); + + // Assert + Assert.AreEqual(0, readConnectionInfos.Length); + } + + [Test] + public void ReadWmtsConnectionInfos_FileWithoutWmtsConnectionsElement_ThrowsCriticalFileReadException() + { + // Setup + string filePath = Path.Combine(testPath, "WmtsConnectionInfosWithoutWmtsConnectionsElement.txt"); + var reader = new WmtsConnectionInfoReader(); + + // Call + TestDelegate call = () => reader.ReadWmtsConnectionInfos(filePath); + + // Assert + var exception = Assert.Throws(call); + string expectedMessage = $"Fout bij het lezen van bestand '{filePath}': het bestand " + + "kon niet worden geopend. Mogelijk is het bestand corrupt " + + "of in gebruik door een andere applicatie."; + Assert.AreEqual(expectedMessage, exception.Message); + Assert.IsInstanceOf(exception.InnerException); + } + + [Test] + public void ReadWmtsConnectionInfos_FileEmptyUrlElement_WarnsAndReadsRestOfFile() + { + // Setup + string filePath = Path.Combine(testPath, "twoWmtsConnectionInfosOneEmptyUrl.txt"); + var reader = new WmtsConnectionInfoReader(); + + WmtsConnectionInfo[] readConnectionInfos = null; + + // Call + Action action = () => { readConnectionInfos = reader.ReadWmtsConnectionInfos(filePath).ToArray(); }; + + // Assert + string expectedMessage = $"Fout bij het lezen van bestand '{filePath}' waarbij Naam='First name' " + + "en URL='': het is niet mogelijk om een WMTS connectie aan te maken."; + TestHelper.AssertLogMessageWithLevelIsGenerated(action, Tuple.Create(expectedMessage, LogLevelConstant.Warn)); + + Assert.IsNotNull(readConnectionInfos); + Assert.AreEqual(1, readConnectionInfos.Length); + var expectedWmtsConnectionInfo = new WmtsConnectionInfo(@"second name", + @"https://domain.com"); + + AssertAreEqual(expectedWmtsConnectionInfo, readConnectionInfos[0]); + } + + [Test] + public void ReadWmtsConnectionInfos_FileMissingOneUrlElement_SkipdAndReadsRestOfFile() + { + // Setup + string filePath = Path.Combine(testPath, "twoWmtsConnectionInfosOneWithoutUrlElement.txt"); + var reader = new WmtsConnectionInfoReader(); + + // Call + WmtsConnectionInfo[] readConnectionInfos = reader.ReadWmtsConnectionInfos(filePath).ToArray(); + + // Assert + Assert.AreEqual(1, readConnectionInfos.Length); + var expectedWmtsConnectionInfo = new WmtsConnectionInfo(@"second name", @"https://domain.com"); + + AssertAreEqual(expectedWmtsConnectionInfo, readConnectionInfos[0]); + } + + [Test] + public void ReadWmtsConnectionInfos_FileMissingOneNameElement_SkipdAndReadsRestOfFile() + { + // Setup + string filePath = Path.Combine(testPath, "twoWmtsConnectionInfosOneWithoutNameElement.txt"); + var reader = new WmtsConnectionInfoReader(); + + // Call + WmtsConnectionInfo[] readConnectionInfos = reader.ReadWmtsConnectionInfos(filePath).ToArray(); + + // Assert + Assert.AreEqual(1, readConnectionInfos.Length); + var expectedWmtsConnectionInfo = new WmtsConnectionInfo(@"second name", @"https://domain.com"); + + AssertAreEqual(expectedWmtsConnectionInfo, readConnectionInfos[0]); + } + + [Test] + public void ReadWmtsConnectionInfos_FileLocked_ThrowsCriticalFileReadException() + { + // Setup + string filePath = Path.Combine(testPath, Path.GetRandomFileName()); + var reader = new WmtsConnectionInfoReader(); + + using (var fileDisposeHelper = new FileDisposeHelper(filePath)) + { + fileDisposeHelper.LockFiles(); + + // Call + TestDelegate call = () => reader.ReadWmtsConnectionInfos(filePath); + + // Assert + var exception = Assert.Throws(call); + string expectedMessage = $"Fout bij het lezen van bestand '{filePath}': het bestand " + + "kon niet worden geopend. Mogelijk is het bestand corrupt " + + "of in gebruik door een andere applicatie."; + Assert.AreEqual(expectedMessage, exception.Message); + Assert.IsInstanceOf(exception.InnerException); + } + } + private static void AssertAreEqual(WmtsConnectionInfo expected, WmtsConnectionInfo actual) { Assert.AreEqual(expected.Name, actual.Name);