// 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 Lesser 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser 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.IO;
using System.Linq;
using System.Security.AccessControl;
using Core.Common.IO.Exceptions;
using Core.Common.TestUtil;
using Core.Components.Gis.IO.Writers;
using NUnit.Framework;
namespace Core.Components.Gis.IO.Test.Writers
{
[TestFixture]
public class WmtsConnectionInfoWriterTest
{
[Test]
[TestCase("")]
[TestCase(" ")]
[TestCase(null)]
public void Constructor_NoFilePath_ThrowArgumentException(string filePath)
{
// Call
TestDelegate call = () => new WmtsConnectionInfoWriter(filePath);
// Assert
const string expectedMessage = "bestandspad mag niet leeg of ongedefinieerd zijn.";
string message = Assert.Throws(call).Message;
StringAssert.Contains(expectedMessage, message);
}
[Test]
public void Constructor_FilePathIsDirectory_ThrowArgumentException()
{
// Call
TestDelegate call = () => new WmtsConnectionInfoWriter("c:/");
// Assert
const string expectedMessage = "bestandspad mag niet verwijzen naar een lege bestandsnaam.";
string message = Assert.Throws(call).Message;
StringAssert.Contains(expectedMessage, message);
}
[Test]
public void Constructor_FilePathHasInvalidPathCharacter_ThrowArgumentException()
{
// Setup
char[] invalidPathChars = Path.GetInvalidPathChars();
var filePath = "c:/_.config".Replace('_', invalidPathChars[0]);
// Call
TestDelegate call = () => new WmtsConnectionInfoWriter(filePath);
// Assert
var expectedMessage = $"Fout bij het lezen van bestand '{filePath}': "
+ "er zitten ongeldige tekens in het bestandspad. Alle tekens in het bestandspad moeten geldig zijn.";
TestHelper.AssertThrowsArgumentExceptionAndTestMessage(call, expectedMessage);
}
[Test]
public void WriteWmtsConnectionInfo_DirectoryDoesNotExist_CreatesDirectoryAndFile()
{
// Setup
string directoryPath = TestHelper.GetScratchPadPath(nameof(WriteWmtsConnectionInfo_DirectoryDoesNotExist_CreatesDirectoryAndFile));
string filePath = Path.Combine(directoryPath, Path.GetRandomFileName());
var wmtsConfigurationWriter = new WmtsConnectionInfoWriter(filePath);
try
{
// Call
wmtsConfigurationWriter.WriteWmtsConnectionInfo(Enumerable.Empty());
// Assert
Assert.IsTrue(File.Exists(filePath));
}
finally
{
Directory.Delete(directoryPath, true);
}
}
[Test]
public void WriteWmtsConnectionInfo_InvalidDirectoryRights_ThrowCriticalFileWriteException()
{
// Setup
string directoryPath = TestHelper.GetScratchPadPath(nameof(WriteWmtsConnectionInfo_InvalidDirectoryRights_ThrowCriticalFileWriteException));
Directory.CreateDirectory(directoryPath);
string filePath = Path.Combine(directoryPath, Path.GetRandomFileName());
var wmtsConfigurationWriter = new WmtsConnectionInfoWriter(filePath);
try
{
using (new DirectoryPermissionsRevoker(directoryPath, FileSystemRights.Write))
{
// Call
TestDelegate call = () => wmtsConfigurationWriter.WriteWmtsConnectionInfo(Enumerable.Empty());
// Assert
string message = Assert.Throws(call).Message;
var expectedMessage = $"Er is een onverwachte fout opgetreden tijdens het schrijven van het bestand '{filePath}'.";
Assert.AreEqual(expectedMessage, message);
}
}
finally
{
Directory.Delete(directoryPath, true);
}
}
[Test]
public void WriteWmtsConnectionInfo_WmtsConnectionInfosNull_ThrowArgumentNullException()
{
// Setup
string filePath = TestHelper.GetScratchPadPath(nameof(WriteWmtsConnectionInfo_WmtsConnectionInfosNull_ThrowArgumentNullException));
var wmtsConfigurationWriter = new WmtsConnectionInfoWriter(filePath);
// Call
TestDelegate call = () => wmtsConfigurationWriter.WriteWmtsConnectionInfo(null);
// Assert
string paramName = Assert.Throws(call).ParamName;
Assert.AreEqual("wmtsConnectionInfos", paramName);
}
[Test]
public void WriteWmtsConnectionInfo_ValidWmtsConnectionInfo_SavesWmtsConnectionInfoToFile()
{
// Setup
string filePath = TestHelper.GetScratchPadPath(nameof(WriteWmtsConnectionInfo_ValidWmtsConnectionInfo_SavesWmtsConnectionInfoToFile));
var wmtsConfigurationWriter = new WmtsConnectionInfoWriter(filePath);
var wmtsConnectionInfos = new[]
{
new WmtsConnectionInfo("name1", "url1"),
new WmtsConnectionInfo("name2", "url2")
};
using (new FileDisposeHelper(filePath))
{
// Call
wmtsConfigurationWriter.WriteWmtsConnectionInfo(wmtsConnectionInfos);
// Assert
string actualContent = GetFileContent(filePath);
string expectedContent = "" + Environment.NewLine +
"" + Environment.NewLine +
" " + Environment.NewLine +
" name1" + Environment.NewLine +
" url1" + Environment.NewLine +
" " + Environment.NewLine +
" " + Environment.NewLine +
" name2" + Environment.NewLine +
" url2" + Environment.NewLine +
" " + Environment.NewLine +
"";
Assert.AreEqual(expectedContent, actualContent);
}
}
private static string GetFileContent(string filePath)
{
using (var reader = new StreamReader(filePath))
{
return reader.ReadToEnd();
}
}
}
}