// 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.Drawing;
using System.Linq;
using Core.Common.Base.IO;
using Core.Common.Gui;
using Core.Common.Gui.Forms.MainWindow;
using Core.Common.Gui.Plugin;
using Core.Common.TestUtil;
using Core.Common.Utils;
using NUnit.Extensions.Forms;
using NUnit.Framework;
using Rhino.Mocks;
using Ringtoets.Common.Data.AssessmentSection;
using Ringtoets.Common.Data.Calculation;
using Ringtoets.Common.Data.DikeProfiles;
using Ringtoets.Common.Data.FailureMechanism;
using Ringtoets.Common.Forms.PresentationObjects;
using Ringtoets.Common.IO.FileImporters;
using Ringtoets.Integration.Plugin.Properties;
namespace Ringtoets.Integration.Plugin.Test.ImportInfos
{
[TestFixture]
public class ForeshoreProfilesContextImportInfoTest : NUnitFormTest
{
[Test]
public void CreateFileImporter_Always_ReturnFileImporter()
{
// Setup
var mocks = new MockRepository();
var assessmentSection = mocks.Stub();
var failureMechanism = mocks.Stub();
mocks.ReplayAll();
assessmentSection.ReferenceLine = new ReferenceLine();
var foreshoreProfiles = new ForeshoreProfileCollection();
var importTarget = new ForeshoreProfilesContext(foreshoreProfiles, failureMechanism, assessmentSection);
using (var plugin = new RingtoetsPlugin())
{
ImportInfo importInfo = GetImportInfo(plugin);
// Call
IFileImporter importer = importInfo.CreateFileImporter(importTarget, "test");
// Assert
Assert.IsInstanceOf>(importer);
}
mocks.VerifyAll();
}
[Test]
public void Name_Always_ReturnExpectedName()
{
// Setup
using (var plugin = new RingtoetsPlugin())
{
ImportInfo importInfo = GetImportInfo(plugin);
// Call
string name = importInfo.Name;
// Assert
Assert.AreEqual("Voorlandprofiellocaties", name);
}
}
[Test]
public void Category_Always_ReturnExpectedCategory()
{
// Setup
using (var plugin = new RingtoetsPlugin())
{
ImportInfo importInfo = GetImportInfo(plugin);
// Call
string category = importInfo.Category;
// Assert
Assert.AreEqual("Algemeen", category);
}
}
[Test]
public void Image_Always_ReturnExpectedIcon()
{
// Setup
using (var plugin = new RingtoetsPlugin())
{
ImportInfo importInfo = GetImportInfo(plugin);
// Call
Image image = importInfo.Image;
// Assert
TestHelper.AssertImagesAreEqual(Resources.Foreshore, image);
}
}
[Test]
public void IsEnabled_ReferenceLineSet_ReturnTrue()
{
// Setup
var mocks = new MockRepository();
var assessmentSection = mocks.Stub();
assessmentSection.ReferenceLine = new ReferenceLine();
var failureMechanism = mocks.Stub();
mocks.ReplayAll();
var foreshoreProfiles = new ForeshoreProfileCollection();
var context = new ForeshoreProfilesContext(foreshoreProfiles, failureMechanism, assessmentSection);
using (var plugin = new RingtoetsPlugin())
{
ImportInfo importInfo = GetImportInfo(plugin);
// Call
bool isEnabled = importInfo.IsEnabled(context);
// Assert
Assert.IsTrue(isEnabled);
}
mocks.VerifyAll();
}
[Test]
public void IsEnabled_ReferenceLineNotSet_ReturnFalse()
{
// Setup
var mocks = new MockRepository();
var assessmentSection = mocks.Stub();
assessmentSection.ReferenceLine = null;
var failureMechanism = mocks.Stub();
mocks.ReplayAll();
var foreshoreProfiles = new ForeshoreProfileCollection();
var context = new ForeshoreProfilesContext(foreshoreProfiles, failureMechanism, assessmentSection);
using (var plugin = new RingtoetsPlugin())
{
ImportInfo importInfo = GetImportInfo(plugin);
// Call
bool isEnabled = importInfo.IsEnabled(context);
// Assert
Assert.IsFalse(isEnabled);
}
mocks.VerifyAll();
}
[Test]
public void FileFilterGenerator_Always_ReturnExpectedFileFilter()
{
// Setup
using (var plugin = new RingtoetsPlugin())
{
ImportInfo importInfo = GetImportInfo(plugin);
// Call
FileFilterGenerator fileFilterGenerator = importInfo.FileFilterGenerator;
// Assert
Assert.AreEqual("Shapebestand (*.shp)|*.shp", fileFilterGenerator.Filter);
}
}
[Test]
public void VerifyUpdates_CalculationWithoutOutputs_ReturnsTrue()
{
// Setup
var mocks = new MockRepository();
var assessmentSection = mocks.Stub();
var mainWindow = mocks.Stub();
var gui = mocks.Stub();
gui.Stub(g => g.MainWindow).Return(mainWindow);
gui.Stub(g => g.ProjectOpened += null).IgnoreArguments();
gui.Stub(g => g.ProjectOpened -= null).IgnoreArguments();
var calculationWithoutOutput = mocks.Stub();
calculationWithoutOutput.Stub(calc => calc.HasOutput).Return(false);
var failureMechanism = mocks.Stub();
failureMechanism.Stub(fm => fm.Calculations).Return(new[]
{
calculationWithoutOutput
});
mocks.ReplayAll();
var foreshoreProfiles = new ForeshoreProfileCollection();
var context = new ForeshoreProfilesContext(foreshoreProfiles, failureMechanism, assessmentSection);
using (var plugin = new RingtoetsPlugin())
{
plugin.Gui = gui;
ImportInfo importInfo = GetImportInfo(plugin);
// Call
bool updatesVerified = importInfo.VerifyUpdates(context);
// Assert
Assert.IsTrue(updatesVerified);
}
mocks.VerifyAll();
}
[Test]
[TestCase(true)]
[TestCase(false)]
public void VerifyUpdates_CalculationWithOutputs_AlwaysReturnInquiryMessage(bool isActionConfirmed)
{
// Setup
var mocks = new MockRepository();
var assessmentSection = mocks.Stub();
var mainWindow = mocks.Stub();
var gui = mocks.Stub();
gui.Stub(g => g.MainWindow).Return(mainWindow);
gui.Stub(g => g.ProjectOpened += null).IgnoreArguments();
gui.Stub(g => g.ProjectOpened -= null).IgnoreArguments();
var calculationWithoutOutput = mocks.Stub();
calculationWithoutOutput.Stub(calc => calc.HasOutput).Return(true);
var failureMechanism = mocks.Stub();
failureMechanism.Stub(fm => fm.Calculations).Return(new[]
{
calculationWithoutOutput
});
mocks.ReplayAll();
var foreshoreProfiles = new ForeshoreProfileCollection();
var context = new ForeshoreProfilesContext(foreshoreProfiles, failureMechanism, assessmentSection);
string textBoxMessage = null;
DialogBoxHandler = (name, wnd) =>
{
var helper = new MessageBoxTester(wnd);
textBoxMessage = helper.Text;
if (isActionConfirmed)
{
helper.ClickOk();
}
else
{
helper.ClickCancel();
}
};
using (var plugin = new RingtoetsPlugin())
{
plugin.Gui = gui;
ImportInfo importInfo = GetImportInfo(plugin);
// Call
bool updatesVerified = importInfo.VerifyUpdates(context);
// Assert
Assert.AreEqual(isActionConfirmed, updatesVerified);
string expectedInquiryMessage = "Als u voorlandprofielen importeert, " +
"dan worden de resultaten van alle berekeningen in dit toetsspoor " +
"die voorlandprofielen gebruiken verwijderd." +
$"{Environment.NewLine}{Environment.NewLine}Weet u zeker dat u wilt doorgaan?";
Assert.AreEqual(expectedInquiryMessage, textBoxMessage);
}
mocks.VerifyAll();
}
private static ImportInfo GetImportInfo(RingtoetsPlugin plugin)
{
return plugin.GetImportInfos().First(ii => ii.DataType == typeof(ForeshoreProfilesContext));
}
}
}