using System; using System.IO; using Core.Common.Base.Data; using Core.Common.Gui; using Core.Common.Gui.Forms.MainWindow; using Core.Common.TestUtil; using NUnit.Framework; using Rhino.Mocks; using Ringtoets.Integration.Data; namespace Application.Ringtoets.Storage.Test.IntegrationTests { [TestFixture] public class StorageSqLiteIntegrationTest { private readonly string testDataPath = TestHelper.GetTestDataPath(TestDataPath.Application.Ringtoets.Storage, "DatabaseFiles"); private readonly string tempRingtoetsFile = Path.Combine(TestHelper.GetTestDataPath(TestDataPath.Application.Ringtoets.Storage, "DatabaseFiles"), "tempProjectFile.rtd"); private Project fullProject; [SetUp] public void Setup() { fullProject = new Project() { Name = "tempProjectFile", Description = "description", Items = { new DikeAssessmentSection { Name = "dikeAssessmentSection" }, new DuneAssessmentSection { Name = "duneAssessmentSection" } } }; } [TestFixtureTearDown] [TearDown] public void TearDownTempRingtoetsFile() { TearDownTempRingtoetsFile(tempRingtoetsFile); } [Test] public void SaveProjectLoadProject_CompleteProject_ProjectAsEntitiesInStorage() { // Setup var tempFile = Path.Combine(testDataPath, "tempProjectAsFile.rtd"); StorageSqLite storage = new StorageSqLite(); TestDelegate precondition = () => storage.SaveProjectAs(tempRingtoetsFile, fullProject); Assert.DoesNotThrow(precondition, String.Format("Precondition: file '{0}' must be a valid Ringtoets database file.", tempRingtoetsFile)); // Call TestDelegate test = () => storage.SaveProjectAs(tempFile, fullProject); try { Assert.DoesNotThrow(test); } finally { // TearDown TearDownTempRingtoetsFile(tempFile); } } [Test] public void SaveAs_LoadProject_ProjectAsEntitiesInNewStorage() { // Setup StorageSqLite storage = new StorageSqLite(); TestDelegate precondition = () => storage.SaveProjectAs(tempRingtoetsFile, fullProject); Assert.DoesNotThrow(precondition, String.Format("Precondition: file '{0}' must be a valid Ringtoets database file.", tempRingtoetsFile)); // Call TestDelegate test = () => storage.SaveProject(tempRingtoetsFile, fullProject); // Assert Assert.DoesNotThrow(test, String.Format("Precondition: failed to save project to file '{0}'.", tempRingtoetsFile)); // Call Project loadedProject = storage.LoadProject(tempRingtoetsFile); // Assert Assert.IsInstanceOf(loadedProject); Assert.AreNotSame(fullProject, loadedProject); Assert.AreEqual(fullProject.Items.Count, loadedProject.Items.Count); } [Test] [STAThread] public void GivenRingtoetsGuiWithStorageSql_WhenRunWithValidFile_ProjectSet() { // Setup var testFile = Path.Combine(testDataPath, "ValidRingtoetsDatabase.rtd"); var projectStore = new StorageSqLite(); using (var gui = new RingtoetsGui(new MainWindow(), projectStore)) { // Call Action action = () => gui.Run(testFile); // Assert var expectedMessages = new[] { "Openen van bestaand Ringtoetsproject.", "Bestaand Ringtoetsproject succesvol geopend.", }; TestHelper.AssertLogMessagesAreGenerated(action, expectedMessages, 13); Assert.AreEqual(testFile, gui.ProjectFilePath); Assert.NotNull(gui.Project); Assert.AreEqual("ValidRingtoetsDatabase", gui.Project.Name); Assert.AreEqual("Test description", gui.Project.Description); CollectionAssert.IsEmpty(gui.Project.Items); } } [Test] [STAThread] public void GivenRingtoetsGuiWithStorageSql_WhenRunWithInvalidFile_EmptyProjectSet() { // Setup var testFile = "SomeFile"; var projectStore = new StorageSqLite(); using (var gui = new RingtoetsGui(new MainWindow(), projectStore)) { // Call Action action = () => gui.Run(testFile); // Assert var expectedMessages = new[] { "Openen van bestaand Ringtoetsproject.", string.Format("Fout bij het lezen van bestand '{0}': ", testFile), "Het is niet gelukt om het Ringtoetsproject te laden.", "Nieuw project aanmaken..." }; TestHelper.AssertLogMessagesAreGenerated(action, expectedMessages, 15); Assert.AreEqual(null, gui.ProjectFilePath); Assert.NotNull(gui.Project); Assert.AreEqual("Project", gui.Project.Name); Assert.IsEmpty(gui.Project.Description); CollectionAssert.IsEmpty(gui.Project.Items); } } [Test] [STAThread] [TestCase(null)] [TestCase("")] [TestCase(" ")] public void GivenRingtoetsGuiWithStorageSql_WhenRunWithEmptyFile_EmptyProjectSet(string testFile) { // Setup var projectStore = new StorageSqLite(); using (var gui = new RingtoetsGui(new MainWindow(), projectStore)) { // Call Action action = () => gui.Run(testFile); // Assert var expectedMessages = new[] { "Nieuw project aanmaken..." }; TestHelper.AssertLogMessagesAreGenerated(action, expectedMessages, 12); Assert.AreEqual(null, gui.ProjectFilePath); Assert.NotNull(gui.Project); Assert.AreEqual("Project", gui.Project.Name); Assert.IsEmpty(gui.Project.Description); CollectionAssert.IsEmpty(gui.Project.Items); } } private void TearDownTempRingtoetsFile(string filePath) { GC.Collect(); GC.WaitForPendingFinalizers(); if (!string.IsNullOrWhiteSpace(filePath) && File.Exists(filePath)) { File.Delete(filePath); } } } }