// Copyright (C) Stichting Deltares 2020. All rights reserved.
//
// This file is part of the Layer On Slope Tool.
//
// The Layer On Slope Tool is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero 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 Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero 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.Diagnostics;
using System.IO;
using NUnit.Framework;
namespace Deltares.LayerOnSlopeTool.Application.Tests
{
[TestFixture]
public class ApplicationTests
{
private List outputMessages = new List();
private const string outputMessageFileName = "Output.log";
private const string invalidOutputMessageFileName = "InvalidOutput.log";
private const string validOutputMessageFileName = "ValidOutput.log";
private const string testFilesFolder = @"TestFiles\";
private const string inputFilesFolder = "ApplicationTestInput";
private const string geometryFilesFolder = "ApplicationTestInput\\Geometry";
private const string outputFilesFolder = "ApplicationTestOutput";
[SetUp]
public void Setup()
{
outputMessages.Clear();
if (File.Exists(outputMessageFileName))
{
File.Delete(outputMessageFileName);
}
}
[TearDown]
public void TearDown()
{
if (File.Exists(outputMessageFileName))
{
File.Delete(outputMessageFileName);
}
}
[Test]
public void GivenInvalidDataWhenProcessingThenCorrectMessageLogIsCreated()
{
// Given Invalid Data
string commandline = "";
// When Processing
int exitCode = RunApplication(commandline);
// Then Correct MessageLog Is Created
Assert.AreEqual(-1, exitCode);
// List invalidLogMessages = new List();
string invalidLogMessages = File.ReadAllText(Path.Combine(testFilesFolder, invalidOutputMessageFileName));
string logMessages = File.ReadAllText(outputMessageFileName);
Assert.AreEqual(true, invalidLogMessages.Equals(logMessages), "Logile does not contain the correct messages");
}
[Test]
public void GivenValidDataWhenProcessingThenCorrectMessageLogIsCreated()
{
// Given Invalid Data
string commandline = string.Format(" -i {0} -g {1} -o {2}",
Path.Combine(testFilesFolder, inputFilesFolder),
Path.Combine(testFilesFolder, geometryFilesFolder),
Path.Combine(testFilesFolder, outputFilesFolder));
// When Processing
int exitCode = RunApplication(commandline);
// Then Correct MessageLog Is Created
Assert.AreEqual(0, exitCode);
// List invalidLogMessages = new List();
string invalidLogMessages = File.ReadAllText(Path.Combine(testFilesFolder, validOutputMessageFileName));
string logMessages = File.ReadAllText(outputMessageFileName);
Assert.AreEqual(true, invalidLogMessages.Equals(logMessages), "Logile does not contain the correct messages");
}
private int RunApplication(string commandLine)
{
const string applicationName = "LayerOnSlopeTool.exe";
var process = new Process
{
StartInfo =
{
RedirectStandardOutput = true,
FileName = applicationName
}
};
process.StartInfo.Arguments = commandLine;
process.StartInfo.UseShellExecute = false;
process.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
process.Start();
while (!process.StandardOutput.EndOfStream)
{
string line = process.StandardOutput.ReadLine();
outputMessages.Add(line);
}
process.WaitForExit();
int exitCode = process.ExitCode;
File.WriteAllLines(outputMessageFileName, outputMessages);
return exitCode;
}
}
}