Index: Application/Ringtoets/src/Application.Ringtoets.Migration/Application.Ringtoets.Migration.csproj =================================================================== diff -u -red998a9f0da19ea9bdf8f790fb78b52652ec9116 -r0d01d77a85f997d3cbe2e3f83d6bf3438ec6806d --- Application/Ringtoets/src/Application.Ringtoets.Migration/Application.Ringtoets.Migration.csproj (.../Application.Ringtoets.Migration.csproj) (revision ed998a9f0da19ea9bdf8f790fb78b52652ec9116) +++ Application/Ringtoets/src/Application.Ringtoets.Migration/Application.Ringtoets.Migration.csproj (.../Application.Ringtoets.Migration.csproj) (revision 0d01d77a85f997d3cbe2e3f83d6bf3438ec6806d) @@ -60,6 +60,7 @@ ResXFileCodeGenerator Resources.Designer.cs + Designer Index: Application/Ringtoets/src/Application.Ringtoets.Migration/Properties/Resources.Designer.cs =================================================================== diff -u -r5af1b7c1e22ec02a2a7395e2de7482cc91681a4e -r0d01d77a85f997d3cbe2e3f83d6bf3438ec6806d --- Application/Ringtoets/src/Application.Ringtoets.Migration/Properties/Resources.Designer.cs (.../Resources.Designer.cs) (revision 5af1b7c1e22ec02a2a7395e2de7482cc91681a4e) +++ Application/Ringtoets/src/Application.Ringtoets.Migration/Properties/Resources.Designer.cs (.../Resources.Designer.cs) (revision 0d01d77a85f997d3cbe2e3f83d6bf3438ec6806d) @@ -140,13 +140,31 @@ } /// - /// Looks up a localized string similar to Bronprojectpad moet een geldig projectpad zijn.. + /// Looks up a localized string similar to Bron. /// - internal static string RingtoetsProjectMigrator_ValidateProjectPath_Source_filepath_must_be_a_valid_path { + internal static string RingtoetsProjectMigrator_Source_Descriptor { get { - return ResourceManager.GetString("RingtoetsProjectMigrator_ValidateProjectPath_Source_filepath_must_be_a_valid_path" + - "", resourceCulture); + return ResourceManager.GetString("RingtoetsProjectMigrator_Source_Descriptor", resourceCulture); } } + + /// + /// Looks up a localized string similar to Doel. + /// + internal static string RingtoetsProjectMigrator_Target_Descriptor { + get { + return ResourceManager.GetString("RingtoetsProjectMigrator_Target_Descriptor", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to {0}projectpad moet een geldig projectpad zijn.. + /// + internal static string RingtoetsProjectMigrator_ValidateProjectPath_TypeDescriptor_0_filepath_must_be_a_valid_path { + get { + return ResourceManager.GetString("RingtoetsProjectMigrator_ValidateProjectPath_TypeDescriptor_0_filepath_must_be_a_" + + "valid_path", resourceCulture); + } + } } } Index: Application/Ringtoets/src/Application.Ringtoets.Migration/Properties/Resources.resx =================================================================== diff -u -r5af1b7c1e22ec02a2a7395e2de7482cc91681a4e -r0d01d77a85f997d3cbe2e3f83d6bf3438ec6806d --- Application/Ringtoets/src/Application.Ringtoets.Migration/Properties/Resources.resx (.../Resources.resx) (revision 5af1b7c1e22ec02a2a7395e2de7482cc91681a4e) +++ Application/Ringtoets/src/Application.Ringtoets.Migration/Properties/Resources.resx (.../Resources.resx) (revision 0d01d77a85f997d3cbe2e3f83d6bf3438ec6806d) @@ -135,7 +135,13 @@ Het migreren van het projectbestand '{0}' is geannuleerd. - - Bronprojectpad moet een geldig projectpad zijn. + + {0}projectpad moet een geldig projectpad zijn. + + Bron + + + Doel + \ No newline at end of file Index: Application/Ringtoets/src/Application.Ringtoets.Migration/RingtoetsProjectMigrator.cs =================================================================== diff -u -rac9f1f110b53f4bf66b4f2fe04d34178273ac4fd -r0d01d77a85f997d3cbe2e3f83d6bf3438ec6806d --- Application/Ringtoets/src/Application.Ringtoets.Migration/RingtoetsProjectMigrator.cs (.../RingtoetsProjectMigrator.cs) (revision ac9f1f110b53f4bf66b4f2fe04d34178273ac4fd) +++ Application/Ringtoets/src/Application.Ringtoets.Migration/RingtoetsProjectMigrator.cs (.../RingtoetsProjectMigrator.cs) (revision 0d01d77a85f997d3cbe2e3f83d6bf3438ec6806d) @@ -69,7 +69,7 @@ throw new ArgumentNullException(nameof(filePath)); } - ValidateProjectPath(filePath); + ValidateProjectPath(filePath, nameof(filePath), Resources.RingtoetsProjectMigrator_Source_Descriptor); var versionedFile = new RingtoetsVersionedFile(filePath); string version = versionedFile.GetVersion(); @@ -93,34 +93,42 @@ { return MigrationNeeded.Yes; } - else - { - GenerateMigrationCancelledLogMessage(filePath); - return MigrationNeeded.Aborted; - } + + GenerateMigrationCancelledLogMessage(filePath); + return MigrationNeeded.Aborted; } - public string Migrate(string filePath) + public string DetermineMigrationLocation(string originalFilePath) { - if (filePath == null) + if (originalFilePath == null) { - throw new ArgumentNullException(nameof(filePath)); + throw new ArgumentNullException(nameof(originalFilePath)); } - ValidateProjectPath(filePath); + ValidateProjectPath(originalFilePath, nameof(originalFilePath), Resources.RingtoetsProjectMigrator_Source_Descriptor); - string suggestedFileName = GetSuggestedFileName(filePath); - string targetLocation = inquiryHelper.GetTargetFileLocation(fileFilter, suggestedFileName); - if (!string.IsNullOrEmpty(targetLocation)) + string suggestedFileName = GetSuggestedFileName(originalFilePath); + return inquiryHelper.GetTargetFileLocation(fileFilter, suggestedFileName); + } + + public bool Migrate(string sourceFilePath, string targetFilePath) + { + if (sourceFilePath == null) { - return MigrateToTargetLocation(filePath, targetLocation); + throw new ArgumentNullException(nameof(sourceFilePath)); } + if (targetFilePath == null) + { + throw new ArgumentNullException(nameof(targetFilePath)); + } - GenerateMigrationCancelledLogMessage(filePath); - return null; + ValidateProjectPath(sourceFilePath, nameof(sourceFilePath), Resources.RingtoetsProjectMigrator_Source_Descriptor); + ValidateProjectPath(targetFilePath, nameof(targetFilePath), Resources.RingtoetsProjectMigrator_Target_Descriptor); + + return MigrateToTargetLocation(sourceFilePath, targetFilePath); } - private string MigrateToTargetLocation(string sourceFilePath, string targetLocation) + private bool MigrateToTargetLocation(string sourceFilePath, string targetLocation) { try { @@ -131,14 +139,14 @@ sourceFilePath, targetLocation, currentDatabaseVersion); log.Info(message); - return targetLocation; + return true; } catch (CriticalMigrationException e) { string errorMessage = string.Format(Resources.RingtoetsProjectMigrator_MigrateToTargetLocation_Updating_outdated_projectfile_0_failed_with_exception_1_, sourceFilePath, e.Message); log.Error(errorMessage, e); - return null; + return false; } } @@ -157,12 +165,14 @@ log.Warn(warningMessage); } - private static void ValidateProjectPath(string sourceFilePath) + private static void ValidateProjectPath(string filePath, string argumentName, string pathDescription) { - if (!IOUtils.IsValidFilePath(sourceFilePath)) + if (!IOUtils.IsValidFilePath(filePath)) { - throw new ArgumentException(Resources.RingtoetsProjectMigrator_ValidateProjectPath_Source_filepath_must_be_a_valid_path, - nameof(sourceFilePath)); + string message = string.Format(Resources.RingtoetsProjectMigrator_ValidateProjectPath_TypeDescriptor_0_filepath_must_be_a_valid_path, + pathDescription); + throw new ArgumentException(message, + argumentName); } } } Index: Application/Ringtoets/test/Application.Ringtoets.Migration.Test/RingtoetsProjectMigratorTest.cs =================================================================== diff -u -rac9f1f110b53f4bf66b4f2fe04d34178273ac4fd -r0d01d77a85f997d3cbe2e3f83d6bf3438ec6806d --- Application/Ringtoets/test/Application.Ringtoets.Migration.Test/RingtoetsProjectMigratorTest.cs (.../RingtoetsProjectMigratorTest.cs) (revision ac9f1f110b53f4bf66b4f2fe04d34178273ac4fd) +++ Application/Ringtoets/test/Application.Ringtoets.Migration.Test/RingtoetsProjectMigratorTest.cs (.../RingtoetsProjectMigratorTest.cs) (revision 0d01d77a85f997d3cbe2e3f83d6bf3438ec6806d) @@ -105,7 +105,7 @@ // Assert string paramName = TestHelper.AssertThrowsArgumentExceptionAndTestMessage( call, "Bronprojectpad moet een geldig projectpad zijn.").ParamName; - Assert.AreEqual("sourceFilePath", paramName); + Assert.AreEqual("filePath", paramName); mocks.VerifyAll(); } @@ -203,7 +203,7 @@ } [Test] - public void Migrate_SourcePathNull_ThrowsArgumentNullException() + public void DetermineMigrationLocation_FilePathNull_ThrownArgumentNullException() { // Setup var mocks = new MockRepository(); @@ -213,17 +213,19 @@ var migrator = new RingtoetsProjectMigrator(inquiryHelper); // Call - TestDelegate call = () => migrator.Migrate(null); + TestDelegate call = () => migrator.DetermineMigrationLocation(null); // Assert string paramName = Assert.Throws(call).ParamName; - Assert.AreEqual("filePath", paramName); + Assert.AreEqual("originalFilePath", paramName); + + mocks.VerifyAll(); } [Test] [TestCase("")] [TestCase(" ")] - public void Migrate_InvalidFilePath_ThrowsArgumentException(string invalidFilePath) + public void DetermineMigrationLocation_InvalidFilePath_ThrowsArgumentException(string invalidFilePath) { // Setup var mocks = new MockRepository(); @@ -233,106 +235,175 @@ var migrator = new RingtoetsProjectMigrator(inquiryHelper); // Call - TestDelegate call = () => migrator.Migrate(invalidFilePath); + TestDelegate call = () => migrator.DetermineMigrationLocation(invalidFilePath); // Assert string paramName = TestHelper.AssertThrowsArgumentExceptionAndTestMessage( call, "Bronprojectpad moet een geldig projectpad zijn.").ParamName; - Assert.AreEqual("sourceFilePath", paramName); + Assert.AreEqual("originalFilePath", paramName); mocks.VerifyAll(); } [Test] - public void Migrate_Always_ReturnsSuggestedFileNameAndFileFilter() + public void DetermineMigrationLocation_ValidPath_AsksUserForTargetPathAndReturnsIt() { // Setup - const string projectName = "FullTestProject164"; - string sourceFilePath = $"Some/Path/{projectName}.rtd"; + const string originalFileName = "Im_a_valid_file_path"; + const string expectedFileExtension = "rtd"; - string versionSuffix = currentDatabaseVersion.Replace(".", "-"); - string expectedSuggestedFileName = $"{projectName}_{versionSuffix}"; + string validFilePath = TestHelper.GetScratchPadPath($"{originalFileName}.{expectedFileExtension}"); + string versionWithUnderscores = RingtoetsVersionHelper.GetCurrentDatabaseVersion().Replace('.', '-'); + var expectedFileFilter = new FileFilterGenerator(expectedFileExtension, "Ringtoets project"); + string expectedSuggestedFileName = $"{originalFileName}_{versionWithUnderscores}"; + + string expectedReturnPath = TestHelper.GetScratchPadPath("Im_a_file_path_to_the_migrated_file.rtd"); + var mocks = new MockRepository(); var inquiryHelper = mocks.StrictMock(); - inquiryHelper.Expect(helper => helper.GetTargetFileLocation(new FileFilterGenerator("rtd", "Ringtoets project"), - expectedSuggestedFileName)) - .Return(null); + inquiryHelper.Expect(h => h.GetTargetFileLocation(expectedFileFilter, expectedSuggestedFileName)) + .Return(expectedReturnPath); mocks.ReplayAll(); var migrator = new RingtoetsProjectMigrator(inquiryHelper); - // Call - migrator.Migrate(sourceFilePath); + // Call + string targetFilePath = migrator.DetermineMigrationLocation(validFilePath); // Assert + Assert.AreEqual(expectedReturnPath, targetFilePath); mocks.VerifyAll(); } [Test] - public void GivenMigratorAndSupportedFile_WhenValidTargetLocationGiven_ThenFileSuccessFullyMigrates() + public void Migrate_SourcePathNull_ThrowsArgumentNullException() { - // Given - string sourceFilePath = TestHelper.GetTestDataPath(TestDataPath.Application.Ringtoets.Migration, "FullTestProject164.rtd"); + // Setup + var mocks = new MockRepository(); + var inquiryHelper = mocks.Stub(); + mocks.ReplayAll(); - string targetFile = $"{nameof(RingtoetsProjectMigratorTest)}." + - $"{nameof(GivenMigratorAndSupportedFile_WhenValidTargetLocationGiven_ThenFileSuccessFullyMigrates)}.rtd"; - string targetFilePath = Path.Combine(TestHelper.GetScratchPadPath(), targetFile); + var migrator = new RingtoetsProjectMigrator(inquiryHelper); + string targetFileName = $"{nameof(RingtoetsProjectMigratorTest)}." + + $"{nameof(Migrate_SourcePathNull_ThrowsArgumentNullException)}.rtd"; + string targetFilePath = TestHelper.GetScratchPadPath(targetFileName); + + // Call + TestDelegate call = () => migrator.Migrate(null, targetFilePath); + + // Assert + string paramName = Assert.Throws(call).ParamName; + Assert.AreEqual("sourceFilePath", paramName); + } + + [Test] + public void Migrate_TargetPathNull_ThrowsArgumentNullException() + { + // Setup var mocks = new MockRepository(); - var inquiryHelper = mocks.StrictMock(); + var inquiryHelper = mocks.Stub(); + mocks.ReplayAll(); - inquiryHelper.Expect(helper => helper.GetTargetFileLocation(null, null)) - .IgnoreArguments() - .Return(targetFilePath); + var migrator = new RingtoetsProjectMigrator(inquiryHelper); + + string sourceFilePath = TestHelper.GetTestDataPath(TestDataPath.Application.Ringtoets.Migration, "FullTestProject164.rtd"); + + // Call + TestDelegate call = () => migrator.Migrate(sourceFilePath, null); + + // Assert + string paramName = Assert.Throws(call).ParamName; + Assert.AreEqual("targetFilePath", paramName); + } + + [Test] + [TestCase("")] + [TestCase(" ")] + public void Migrate_InvalidSourceFilePath_ThrowsArgumentException(string invalidFilePath) + { + // Setup + var mocks = new MockRepository(); + var inquiryHelper = mocks.Stub(); mocks.ReplayAll(); var migrator = new RingtoetsProjectMigrator(inquiryHelper); - using (new FileDisposeHelper(targetFilePath)) - { - string actualTargetFilePath = null; + string targetFileName = $"{nameof(RingtoetsProjectMigratorTest)}." + + $"{nameof(Migrate_InvalidSourceFilePath_ThrowsArgumentException)}.rtd"; + string targetFilePath = TestHelper.GetScratchPadPath(targetFileName); - // When - Action call = () => actualTargetFilePath = migrator.Migrate(sourceFilePath); + // Call + TestDelegate call = () => migrator.Migrate(invalidFilePath, targetFilePath); - // Then - string expectedMessage = $"Het projectbestand '{sourceFilePath}' is succesvol gemigreerd naar '{targetFilePath}' (versie {currentDatabaseVersion})."; - TestHelper.AssertLogMessageIsGenerated(call, expectedMessage, 1); + // Assert + string paramName = TestHelper.AssertThrowsArgumentExceptionAndTestMessage( + call, "Bronprojectpad moet een geldig projectpad zijn.").ParamName; + Assert.AreEqual("sourceFilePath", paramName); - Assert.AreEqual(targetFilePath, actualTargetFilePath); - var toVersionedFile = new RingtoetsVersionedFile(targetFilePath); - Assert.AreEqual(currentDatabaseVersion, toVersionedFile.GetVersion()); - } + mocks.VerifyAll(); + } + [Test] + [TestCase("")] + [TestCase(" ")] + public void Migrate_InvalidTargetFilePath_ThrowsArgumentException(string invalidFilePath) + { + // Setup + var mocks = new MockRepository(); + var inquiryHelper = mocks.Stub(); + mocks.ReplayAll(); + + var migrator = new RingtoetsProjectMigrator(inquiryHelper); + + string sourceFilePath = TestHelper.GetTestDataPath(TestDataPath.Application.Ringtoets.Migration, + "FullTestProject164.rtd"); + + // Call + TestDelegate call = () => migrator.Migrate(sourceFilePath, invalidFilePath); + + // Assert + string paramName = TestHelper.AssertThrowsArgumentExceptionAndTestMessage( + call, "Doelprojectpad moet een geldig projectpad zijn.").ParamName; + Assert.AreEqual("targetFilePath", paramName); + mocks.VerifyAll(); } [Test] - public void GivenMigratorAndSupportedFile_WhenCancelledSaveFileDialog_ThenFileMigrationCancelledAndLogsMessage() + public void GivenMigratorAndSupportedFile_WhenValidTargetLocationGiven_ThenFileSuccessFullyMigrates() { // Given string sourceFilePath = TestHelper.GetTestDataPath(TestDataPath.Application.Ringtoets.Migration, "FullTestProject164.rtd"); - var mocks = new MockRepository(); - var inquiryHelper = mocks.StrictMock(); + string targetFile = $"{nameof(RingtoetsProjectMigratorTest)}." + + $"{nameof(GivenMigratorAndSupportedFile_WhenValidTargetLocationGiven_ThenFileSuccessFullyMigrates)}.rtd"; + string targetFilePath = Path.Combine(TestHelper.GetScratchPadPath(), targetFile); - inquiryHelper.Expect(helper => helper.GetTargetFileLocation(null, null)) - .IgnoreArguments() - .Return(null); + var mocks = new MockRepository(); + var inquiryHelper = mocks.Stub(); mocks.ReplayAll(); var migrator = new RingtoetsProjectMigrator(inquiryHelper); - string actualTargetFilePath = string.Empty; - // When - Action call = () => actualTargetFilePath = migrator.Migrate(sourceFilePath); + using (new FileDisposeHelper(targetFilePath)) + { + bool migrationSuccessful = false; - // Then - string expectedMessage = $"Het migreren van het projectbestand '{sourceFilePath}' is geannuleerd."; - TestHelper.AssertLogMessageIsGenerated(call, expectedMessage, 1); - Assert.IsNull(actualTargetFilePath); + // When + Action call = () => migrationSuccessful = migrator.Migrate(sourceFilePath, targetFilePath); + // Then + string expectedMessage = $"Het projectbestand '{sourceFilePath}' is succesvol gemigreerd naar '{targetFilePath}' (versie {currentDatabaseVersion})."; + TestHelper.AssertLogMessageIsGenerated(call, expectedMessage, 1); + + Assert.IsTrue(migrationSuccessful); + + var toVersionedFile = new RingtoetsVersionedFile(targetFilePath); + Assert.AreEqual(currentDatabaseVersion, toVersionedFile.GetVersion()); + } + mocks.VerifyAll(); } @@ -345,11 +416,7 @@ $"{nameof(Migrate_UnableToSaveAtTargetFilePath_MigrationFailsAndLogsError)}.rtd"; var mocks = new MockRepository(); - var inquiryHelper = mocks.StrictMock(); - - inquiryHelper.Expect(helper => helper.GetTargetFileLocation(null, null)) - .IgnoreArguments() - .Return(sourceFilePath); + var inquiryHelper = mocks.Stub(); mocks.ReplayAll(); var migrator = new RingtoetsProjectMigrator(inquiryHelper); @@ -359,10 +426,10 @@ { fileDisposeHelper.LockFiles(); - string actualTargetFilePath = string.Empty; + bool migrationSuccessful = true; // Call - Action call = () => actualTargetFilePath = migrator.Migrate(sourceFilePath); + Action call = () => migrationSuccessful = migrator.Migrate(sourceFilePath, targetFilePath); // Assert TestHelper.AssertLogMessages(call, messages => @@ -371,7 +438,7 @@ Assert.AreEqual(1, msgs.Length); StringAssert.StartsWith($"Het migreren van het projectbestand '{sourceFilePath}' is mislukt: ", msgs[0]); }); - Assert.IsNull(actualTargetFilePath); + Assert.IsFalse(migrationSuccessful); } mocks.VerifyAll(); @@ -387,21 +454,17 @@ string targetFilePath = Path.Combine(TestHelper.GetScratchPadPath(), targetFile); var mocks = new MockRepository(); - var inquiryHelper = mocks.StrictMock(); - - inquiryHelper.Expect(helper => helper.GetTargetFileLocation(null, null)) - .IgnoreArguments() - .Return(targetFilePath); + var inquiryHelper = mocks.Stub(); mocks.ReplayAll(); var migrator = new RingtoetsProjectMigrator(inquiryHelper); using (new FileDisposeHelper(targetFilePath)) { - string actualTargetFilePath = string.Empty; + bool migrationSuccessful = true; // Call - Action call = () => actualTargetFilePath = migrator.Migrate(sourceFilePath); + Action call = () => migrationSuccessful = migrator.Migrate(sourceFilePath, targetFilePath); // Assert TestHelper.AssertLogMessages(call, messages => @@ -410,7 +473,7 @@ Assert.AreEqual(1, msgs.Length); StringAssert.StartsWith($"Het migreren van het projectbestand '{sourceFilePath}' is mislukt: ", msgs[0]); }); - Assert.IsNull(actualTargetFilePath); + Assert.IsFalse(migrationSuccessful); } mocks.VerifyAll(); @@ -423,19 +486,15 @@ string sourceFilePath = TestHelper.GetTestDataPath(TestDataPath.Application.Ringtoets.Migration, "FullTestProject164.rtd"); var mocks = new MockRepository(); - var inquiryHelper = mocks.StrictMock(); - - inquiryHelper.Expect(helper => helper.GetTargetFileLocation(null, null)) - .IgnoreArguments() - .Return(sourceFilePath); + var inquiryHelper = mocks.Stub(); mocks.ReplayAll(); var migrator = new RingtoetsProjectMigrator(inquiryHelper); - string actualTargetFilePath = string.Empty; + bool migrationSuccessful = true; // Call - Action call = () => actualTargetFilePath = migrator.Migrate(sourceFilePath); + Action call = () => migrationSuccessful = migrator.Migrate(sourceFilePath, sourceFilePath); // Assert TestHelper.AssertLogMessages(call, messages => @@ -444,7 +503,7 @@ Assert.AreEqual(1, msgs.Length); StringAssert.StartsWith($"Het migreren van het projectbestand '{sourceFilePath}' is mislukt: ", msgs[0]); }); - Assert.IsNull(actualTargetFilePath); + Assert.IsFalse(migrationSuccessful); mocks.VerifyAll(); } Index: Application/Ringtoets/test/Application.Ringtoets.Storage.Test/IntegrationTests/StorageMigrationIntegrationTest.cs =================================================================== diff -u -r7963bd77ea5540754906c7e994f2687e33a89dde -r0d01d77a85f997d3cbe2e3f83d6bf3438ec6806d --- Application/Ringtoets/test/Application.Ringtoets.Storage.Test/IntegrationTests/StorageMigrationIntegrationTest.cs (.../StorageMigrationIntegrationTest.cs) (revision 7963bd77ea5540754906c7e994f2687e33a89dde) +++ Application/Ringtoets/test/Application.Ringtoets.Storage.Test/IntegrationTests/StorageMigrationIntegrationTest.cs (.../StorageMigrationIntegrationTest.cs) (revision 0d01d77a85f997d3cbe2e3f83d6bf3438ec6806d) @@ -53,6 +53,7 @@ var mocks = new MockRepository(); var projectMigrator = mocks.Stub(); + projectMigrator.Stub(pm => pm.ShouldMigrate(targetFilePath)).Return(MigrationNeeded.No); mocks.ReplayAll(); using (var gui = new GuiCore(new MainWindow(), projectStore, projectMigrator, new RingtoetsProjectFactory(), new GuiCoreSettings())) Index: Application/Ringtoets/test/Application.Ringtoets.Storage.Test/IntegrationTests/StorageSqLiteIntegrationTest.cs =================================================================== diff -u -r7963bd77ea5540754906c7e994f2687e33a89dde -r0d01d77a85f997d3cbe2e3f83d6bf3438ec6806d --- Application/Ringtoets/test/Application.Ringtoets.Storage.Test/IntegrationTests/StorageSqLiteIntegrationTest.cs (.../StorageSqLiteIntegrationTest.cs) (revision 7963bd77ea5540754906c7e994f2687e33a89dde) +++ Application/Ringtoets/test/Application.Ringtoets.Storage.Test/IntegrationTests/StorageSqLiteIntegrationTest.cs (.../StorageSqLiteIntegrationTest.cs) (revision 0d01d77a85f997d3cbe2e3f83d6bf3438ec6806d) @@ -187,15 +187,17 @@ public void GivenRingtoetsGuiWithStorageSqlAndMigrator_WhenRunWithValidFile_ProjectSet() { // Given + string tempRingtoetsFile = GetRandomRingtoetsFile(); + var expectedProjectName = Path.GetFileNameWithoutExtension(tempRingtoetsFile); + var mocks = new MockRepository(); var projectMigrator = mocks.Stub(); + projectMigrator.Stub(pm => pm.ShouldMigrate(tempRingtoetsFile)).Return(MigrationNeeded.No); mocks.ReplayAll(); var projectStore = new StorageSqLite(); RingtoetsProject fullProject = RingtoetsProjectTestHelper.GetFullTestProject(); - string tempRingtoetsFile = GetRandomRingtoetsFile(); - var expectedProjectName = Path.GetFileNameWithoutExtension(tempRingtoetsFile); var expectedProjectDescription = fullProject.Description; // Precondition Index: Core/Common/src/Core.Common.Base/Storage/IMigrateProject.cs =================================================================== diff -u -rac9f1f110b53f4bf66b4f2fe04d34178273ac4fd -r0d01d77a85f997d3cbe2e3f83d6bf3438ec6806d --- Core/Common/src/Core.Common.Base/Storage/IMigrateProject.cs (.../IMigrateProject.cs) (revision ac9f1f110b53f4bf66b4f2fe04d34178273ac4fd) +++ Core/Common/src/Core.Common.Base/Storage/IMigrateProject.cs (.../IMigrateProject.cs) (revision 0d01d77a85f997d3cbe2e3f83d6bf3438ec6806d) @@ -39,17 +39,22 @@ MigrationNeeded ShouldMigrate(string filePath); /// - /// Migrates an outdated project file from + /// Determines the target filepath to place the migrated Ringtoets project based + /// on it's original filepath. + /// + /// The filepath to be used as location for the migration result. Value + /// will be null if no target filepath an be provided. + string DetermineMigrationLocation(string originalFilePath); + + /// + /// Migrates an outdated project file from /// to the newest project version version at a user defined target filepath. /// - /// The filepath of the project which needs to be migrated. - /// A filepath to the updated project file. null if: - /// - /// The user did not provide a target filepath. - /// The migration failed. - /// - /// Thrown when is null. - /// Thrown when is an invalid file path. - string Migrate(string filePath); + /// The filepath of the project which needs to be migrated. + /// The filepath where the migrated project is written to. + /// Returns true if the migration was successful; return false otherwise. + /// Thrown when is null. + /// Thrown when is an invalid file path. + bool Migrate(string sourceFilePath, string targetFilePath); } } Index: Core/Common/src/Core.Common.Gui/Commands/StorageCommandHandler.cs =================================================================== diff -u -rac9f1f110b53f4bf66b4f2fe04d34178273ac4fd -r0d01d77a85f997d3cbe2e3f83d6bf3438ec6806d --- Core/Common/src/Core.Common.Gui/Commands/StorageCommandHandler.cs (.../StorageCommandHandler.cs) (revision ac9f1f110b53f4bf66b4f2fe04d34178273ac4fd) +++ Core/Common/src/Core.Common.Gui/Commands/StorageCommandHandler.cs (.../StorageCommandHandler.cs) (revision 0d01d77a85f997d3cbe2e3f83d6bf3438ec6806d) @@ -131,26 +131,32 @@ log.Info(Resources.StorageCommandHandler_OpenExistingProject_Opening_existing_project); string migratedProjectFilePath = filePath; + var isOpenProjectSuccessful = false; try { MigrationNeeded migrationNeeded = projectMigrator.ShouldMigrate(filePath); if (migrationNeeded == MigrationNeeded.Yes) { - migratedProjectFilePath = projectMigrator.Migrate(filePath); + migratedProjectFilePath = projectMigrator.DetermineMigrationLocation(filePath); + if (!string.IsNullOrWhiteSpace(migratedProjectFilePath)) + { + isOpenProjectSuccessful = projectMigrator.Migrate(filePath, migratedProjectFilePath); + } } else if (migrationNeeded == MigrationNeeded.Aborted) { migratedProjectFilePath = null; + isOpenProjectSuccessful = false; } } catch (ArgumentException e) { migratedProjectFilePath = null; + isOpenProjectSuccessful = false; log.Error(e.Message, e); } - var isOpenProjectSuccessful = false; IProject newProject = null; if (!string.IsNullOrEmpty(migratedProjectFilePath)) { Index: Core/Common/test/Core.Common.Gui.Test/Commands/StorageCommandHandlerTest.cs =================================================================== diff -u -rac9f1f110b53f4bf66b4f2fe04d34178273ac4fd -r0d01d77a85f997d3cbe2e3f83d6bf3438ec6806d --- Core/Common/test/Core.Common.Gui.Test/Commands/StorageCommandHandlerTest.cs (.../StorageCommandHandlerTest.cs) (revision ac9f1f110b53f4bf66b4f2fe04d34178273ac4fd) +++ Core/Common/test/Core.Common.Gui.Test/Commands/StorageCommandHandlerTest.cs (.../StorageCommandHandlerTest.cs) (revision 0d01d77a85f997d3cbe2e3f83d6bf3438ec6806d) @@ -193,8 +193,12 @@ .Return(loadedProject); var projectMigrator = mocks.StrictMock(); - projectMigrator.Expect(pm => pm.ShouldMigrate(pathToSomeValidFile)).Return(MigrationNeeded.Yes); - projectMigrator.Expect(pm => pm.Migrate(pathToSomeValidFile)).Return(pathToMigratedFile); + using (mocks.Ordered()) + { + projectMigrator.Expect(pm => pm.ShouldMigrate(pathToSomeValidFile)).Return(MigrationNeeded.Yes); + projectMigrator.Expect(pm => pm.DetermineMigrationLocation(pathToSomeValidFile)).Return(pathToMigratedFile); + projectMigrator.Expect(pm => pm.Migrate(pathToSomeValidFile, pathToMigratedFile)).Return(true); + } var mainWindowController = mocks.Stub(); @@ -235,16 +239,19 @@ var projectStorage = mocks.StrictMock(); var projectMigrator = mocks.StrictMock(); - projectMigrator.Expect(pm => pm.ShouldMigrate(pathToSomeValidFile)).Return(MigrationNeeded.Yes); - projectMigrator.Expect(pm => pm.Migrate(pathToSomeValidFile)).Return(null); + using (mocks.Ordered()) + { + projectMigrator.Expect(pm => pm.ShouldMigrate(pathToSomeValidFile)).Return(MigrationNeeded.Yes); + projectMigrator.Expect(pm => pm.DetermineMigrationLocation(pathToSomeValidFile)).Return(null); + } var project = mocks.Stub(); var projectFactory = mocks.StrictMock(); - projectFactory.Stub(pf => pf.CreateNewProject()).Return(project); + projectFactory.Expect(pf => pf.CreateNewProject()).Return(project); - var projectOwner = mocks.Stub(); + var projectOwner = mocks.StrictMock(); projectOwner.Stub(po => po.Project).Return(project); - projectOwner.Stub(po => po.SetProject(project, null)); + projectOwner.Expect(po => po.SetProject(project, null)); var mainWindowController = mocks.Stub(); mocks.ReplayAll(); @@ -321,26 +328,29 @@ } [Test] - public void OpenExistingProject_ShouldMigrateTrueAndMigrateThrowsArgumentException_LogFailureAndCreateNewProjectAndReturnsFalse() + public void OpenExistingProject_ShouldMigrateYesAndDetermineMigrationLocationThrowsArgumentException_LogFailureAndCreateNewProjectAndReturnsFalse() { // Setup const string errorMessage = "I am an error message."; - const string pathToSomeValidFile = " "; + const string pathToSomeValidFile = "C://folder/directory/newProject.rtd"; var projectStorage = mocks.StrictMock(); var projectMigrator = mocks.StrictMock(); - projectMigrator.Expect(pm => pm.ShouldMigrate(pathToSomeValidFile)).Return(MigrationNeeded.Yes); - projectMigrator.Expect(pm => pm.Migrate(pathToSomeValidFile)) - .Throw(new ArgumentException(errorMessage)); + using (mocks.Ordered()) + { + projectMigrator.Expect(pm => pm.ShouldMigrate(pathToSomeValidFile)).Return(MigrationNeeded.Yes); + projectMigrator.Expect(pm => pm.DetermineMigrationLocation(pathToSomeValidFile)) + .Throw(new ArgumentException(errorMessage)); + } var project = mocks.Stub(); var projectFactory = mocks.StrictMock(); - projectFactory.Stub(pf => pf.CreateNewProject()).Return(project); + projectFactory.Expect(pf => pf.CreateNewProject()).Return(project); - var projectOwner = mocks.Stub(); + var projectOwner = mocks.StrictMock(); projectOwner.Stub(po => po.Project).Return(project); - projectOwner.Stub(po => po.SetProject(project, null)); + projectOwner.Expect(po => po.SetProject(project, null)); var mainWindowController = mocks.Stub(); mocks.ReplayAll(); @@ -370,6 +380,61 @@ } [Test] + public void OpenExistingProject_ShouldMigrateTrueAndMigrateThrowsArgumentException_LogFailureAndCreateNewProjectAndReturnsFalse() + { + // Setup + const string errorMessage = "I am an error message."; + const string fileName = "newProject"; + string pathToSomeValidFile = $"C://folder/directory/{fileName}.rtd"; + string pathToMigratedFile = $"C://folder/directory/{fileName}-newerVersion.rtd"; + + var projectStorage = mocks.StrictMock(); + + var projectMigrator = mocks.StrictMock(); + using (mocks.Ordered()) + { + projectMigrator.Expect(pm => pm.ShouldMigrate(pathToSomeValidFile)).Return(MigrationNeeded.Yes); + projectMigrator.Expect(pm => pm.DetermineMigrationLocation(pathToSomeValidFile)).Return(pathToMigratedFile); + projectMigrator.Expect(pm => pm.Migrate(pathToSomeValidFile, pathToMigratedFile)) + .Throw(new ArgumentException(errorMessage)); + } + + var project = mocks.Stub(); + var projectFactory = mocks.StrictMock(); + projectFactory.Expect(pf => pf.CreateNewProject()).Return(project); + + var projectOwner = mocks.StrictMock(); + projectOwner.Stub(po => po.Project).Return(project); + projectOwner.Expect(po => po.SetProject(project, null)); + + var mainWindowController = mocks.Stub(); + mocks.ReplayAll(); + + var storageCommandHandler = new StorageCommandHandler( + projectStorage, + projectMigrator, + projectFactory, + projectOwner, + mainWindowController); + + // Call + bool result = true; + Action call = () => result = storageCommandHandler.OpenExistingProject(pathToSomeValidFile); + + // Assert + var expectedMessages = new[] + { + "Openen van bestaand Ringtoetsproject...", + errorMessage, + "Het is niet gelukt om het Ringtoetsproject te laden." + }; + TestHelper.AssertLogMessagesAreGenerated(call, expectedMessages, 3); + Assert.IsFalse(result); + + mocks.VerifyAll(); + } + + [Test] public void OpenExistingProject_LoadingProjectThrowsStorageException_LogFailureCreateNewProjectAndReturnFalse() { // Setup Index: Core/Common/test/Core.Common.Gui.Test/GuiCoreTest.cs =================================================================== diff -u -rac9f1f110b53f4bf66b4f2fe04d34178273ac4fd -r0d01d77a85f997d3cbe2e3f83d6bf3438ec6806d --- Core/Common/test/Core.Common.Gui.Test/GuiCoreTest.cs (.../GuiCoreTest.cs) (revision ac9f1f110b53f4bf66b4f2fe04d34178273ac4fd) +++ Core/Common/test/Core.Common.Gui.Test/GuiCoreTest.cs (.../GuiCoreTest.cs) (revision 0d01d77a85f997d3cbe2e3f83d6bf3438ec6806d) @@ -563,7 +563,7 @@ var projectMigrator = mocks.Stub(); projectMigrator.Stub(pm => pm.ShouldMigrate(testFile)).Return(MigrationNeeded.Yes); - projectMigrator.Stub(pm => pm.Migrate(testFile)).Return(null); + projectMigrator.Stub(pm => pm.DetermineMigrationLocation(testFile)).Return(null); const string expectedProjectName = "Project"; var project = mocks.Stub(); @@ -658,6 +658,7 @@ // Setup const string fileName = "SomeFile"; string testFile = $"{fileName}.rtd"; + string targetFile = $"{fileName}_17_1.rtd"; const string expectedErrorMessage = "You shall not migrate!"; @@ -666,7 +667,8 @@ var projectMigrator = mocks.Stub(); projectMigrator.Stub(pm => pm.ShouldMigrate(testFile)).Return(MigrationNeeded.Yes); - projectMigrator.Stub(pm => pm.Migrate(testFile)) + projectMigrator.Stub(pm => pm.DetermineMigrationLocation(testFile)).Return(targetFile); + projectMigrator.Stub(pm => pm.Migrate(testFile, targetFile)) .Throw(new ArgumentException(expectedErrorMessage)); const string expectedProjectName = "Project";