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(); }