Index: Application/Ringtoets/src/Application.Ringtoets.Storage/Application.Ringtoets.Storage.csproj =================================================================== diff -u -rf104a6357c644e0ddb7c9fd94271cc7e14d49c59 -ra670da380b593fb1cec05bcb5df2da347eb8d384 --- Application/Ringtoets/src/Application.Ringtoets.Storage/Application.Ringtoets.Storage.csproj (.../Application.Ringtoets.Storage.csproj) (revision f104a6357c644e0ddb7c9fd94271cc7e14d49c59) +++ Application/Ringtoets/src/Application.Ringtoets.Storage/Application.Ringtoets.Storage.csproj (.../Application.Ringtoets.Storage.csproj) (revision a670da380b593fb1cec05bcb5df2da347eb8d384) @@ -146,6 +146,7 @@ ResXFileCodeGenerator Resources.Designer.cs + Designer Index: Application/Ringtoets/src/Application.Ringtoets.Storage/Converters/ProjectEntityConverter.cs =================================================================== diff -u -r265c86f31e68d374bbeda5379b678ae774baca9c -ra670da380b593fb1cec05bcb5df2da347eb8d384 --- Application/Ringtoets/src/Application.Ringtoets.Storage/Converters/ProjectEntityConverter.cs (.../ProjectEntityConverter.cs) (revision 265c86f31e68d374bbeda5379b678ae774baca9c) +++ Application/Ringtoets/src/Application.Ringtoets.Storage/Converters/ProjectEntityConverter.cs (.../ProjectEntityConverter.cs) (revision a670da380b593fb1cec05bcb5df2da347eb8d384) @@ -17,7 +17,7 @@ /// /// Database set of . /// A new or null when not found. - /// is null. + /// Thrown when is null. public static Project GetProject(IDbSet dbSet) { var entry = dbSet.FirstOrDefault(); @@ -30,7 +30,7 @@ /// Execute .SaveChanges() afterwards to update the storage. /// Database set of . /// to be saved in the database. - /// or is null. + /// Thrown when or is null. /// When multiple instances are found that refer to . /// When no entities was found that refer to . public static void UpdateProjectEntity(IDbSet dbSet, Project project) @@ -48,12 +48,31 @@ } /// + /// Insert the , based upon the , in the . + /// + /// Execute .SaveChanges() afterwards to update the storage. + /// Database set of . + /// to be saved in the database. + /// Thrown when or is null. + public static void InsertProjectEntity(IDbSet dbSet, Project project) + { + var projectEntity = new ProjectEntity(); + ProjectToProjectEntity(project, projectEntity); + dbSet.Add(projectEntity); + } + + /// /// Converts to . /// /// The to convert. /// A reference to the to be saved. - public static void ProjectToProjectEntity(Project project, ProjectEntity projectEntity) + /// Thrown when or is null. + private static void ProjectToProjectEntity(Project project, ProjectEntity projectEntity) { + if (project == null || projectEntity == null) + { + throw new ArgumentNullException(); + } projectEntity.Name = project.Name; projectEntity.Description = project.Description; projectEntity.LastUpdated = new DateTime().Ticks; Index: Application/Ringtoets/src/Application.Ringtoets.Storage/StorageSqLite.cs =================================================================== diff -u -rbcbae879035d5472efff973990ba1194e38b2ea5 -ra670da380b593fb1cec05bcb5df2da347eb8d384 --- Application/Ringtoets/src/Application.Ringtoets.Storage/StorageSqLite.cs (.../StorageSqLite.cs) (revision bcbae879035d5472efff973990ba1194e38b2ea5) +++ Application/Ringtoets/src/Application.Ringtoets.Storage/StorageSqLite.cs (.../StorageSqLite.cs) (revision a670da380b593fb1cec05bcb5df2da347eb8d384) @@ -23,6 +23,10 @@ /// Path to database file. /// to save. /// Returns the number of changes that were saved in . + /// Thrown when is null. + /// is invalid. + /// Thrown when the database does not contain the table version. + /// Thrown when was not created. /// Thrown when /// /// Saving the to the database failed. @@ -34,11 +38,9 @@ ConnectToNew(databaseFilePath); using (var dbContext = new RingtoetsEntities(ConnectionString)) { + ProjectEntityConverter.InsertProjectEntity(dbContext.ProjectEntities, project); try { - var projectEntity = new ProjectEntity(); - ProjectEntityConverter.ProjectToProjectEntity(project, projectEntity); - dbContext.ProjectEntities.Add(projectEntity); return dbContext.SaveChanges(); } catch (DataException exception) Index: Application/Ringtoets/src/Application.Ringtoets.Storage/StorageSqliteCreator.cs =================================================================== diff -u -rbcbae879035d5472efff973990ba1194e38b2ea5 -ra670da380b593fb1cec05bcb5df2da347eb8d384 --- Application/Ringtoets/src/Application.Ringtoets.Storage/StorageSqliteCreator.cs (.../StorageSqliteCreator.cs) (revision bcbae879035d5472efff973990ba1194e38b2ea5) +++ Application/Ringtoets/src/Application.Ringtoets.Storage/StorageSqliteCreator.cs (.../StorageSqliteCreator.cs) (revision a670da380b593fb1cec05bcb5df2da347eb8d384) @@ -29,17 +29,16 @@ { using (var command = dbContext.CreateCommand()) { - dbContext.Open(); - command.CommandText = Resources.DatabaseStructure; try { + dbContext.Open(); + command.CommandText = Resources.DatabaseStructure; command.ExecuteNonQuery(); } catch (SQLiteException exception) { throw CreateUpdateStorageException(Resources.Error_Write_Structure_to_Database, exception); } - dbContext.Close(); } } } Index: Application/Ringtoets/test/Application.Ringtoets.Storage.Test/StorageSQLiteTest.cs =================================================================== diff -u -rf104a6357c644e0ddb7c9fd94271cc7e14d49c59 -ra670da380b593fb1cec05bcb5df2da347eb8d384 --- Application/Ringtoets/test/Application.Ringtoets.Storage.Test/StorageSQLiteTest.cs (.../StorageSQLiteTest.cs) (revision f104a6357c644e0ddb7c9fd94271cc7e14d49c59) +++ Application/Ringtoets/test/Application.Ringtoets.Storage.Test/StorageSQLiteTest.cs (.../StorageSQLiteTest.cs) (revision a670da380b593fb1cec05bcb5df2da347eb8d384) @@ -16,6 +16,7 @@ [Test] [TestCase(null)] [TestCase("")] + [TestCase(" ")] public void LoadProject_InvalidPath_ThrowsArgumentException(string invalidPath) { // Call @@ -74,5 +75,119 @@ // Assert Assert.IsInstanceOf(loadedProject); } + + [Test] + [TestCase(null)] + [TestCase("")] + [TestCase(" ")] + public void SaveProjectAs_InvalidPath_ThrowsArgumentException(string invalidPath) + { + // Setup + Project project = new Project(); + // Call + TestDelegate test = () => new StorageSqLite().SaveProjectAs(invalidPath, project); + + // Assert + ArgumentException exception = Assert.Throws(test); + string expectedMessage = String.Format("Fout bij het lezen van bestand '{0}': {1}", + invalidPath, UtilsResources.Error_Path_must_be_specified); + Assert.AreEqual(expectedMessage, exception.Message); + } + + [Test] + public void SaveProjectAs_InvalidProject_ThrowsArgumentNullException() + { + // Setup + var tempFile = Path.Combine(testDataPath, "tempProjectFile.rtd"); + var storage = new StorageSqLite(); + + // Call + TestDelegate test = () => storage.SaveProjectAs(tempFile, null); + + // Assert + Assert.Throws(test); + + // Tear Down + TearDownRingtoetsFile(tempFile); + } + + [Test] + public void SaveProjectAs_ValidPathToNonExistingFile_DoesNotThrowException() + { + // Setup + var tempFile = Path.Combine(testDataPath, "tempProjectFile.rtd"); + var project = new Project(); + var storage = new StorageSqLite(); + + // Call + TestDelegate test = () => storage.SaveProjectAs(tempFile, project); + + // Assert + Assert.DoesNotThrow(test); + + // Tear Down + TearDownRingtoetsFile(tempFile); + } + + [Test] + public void SaveProjectAs_ValidPathToExistingFile_ThrowsStorageValidationException() + { + // Setup + var tempFile = Path.Combine(testDataPath, "tempProjectFile.rtd"); + SetUpRingtoetsFile(tempFile); + var project = new Project(); + var storage = new StorageSqLite(); + + // Call + TestDelegate test = () => storage.SaveProjectAs(tempFile, project); + + // Assert + Assert.DoesNotThrow(test); + + // Tear Down + TearDownRingtoetsFile(tempFile); + } + + [Test] + public void SaveProjectAs_ValidPathToLockedFile_ThrowsException() + { + // Setup + var tempFile = Path.Combine(testDataPath, "tempProjectFile.rtd"); + var project = new Project(); + var storage = new StorageSqLite(); + + // Call + TestDelegate test = () => storage.SaveProjectAs(tempFile, project); + + // Assert + UpdateStorageException updateStorageException; + using (File.Create(tempFile)) // Locks file + { + updateStorageException = Assert.Throws(test); + } + string expectedMessage = String.Format("Fout bij het schrijven van bestand '{0}': {1}", + tempFile, + "Een fout is opgetreden met schrijven naar het nieuwe Ringtoets bestand."); + Assert.AreEqual(expectedMessage, updateStorageException.Message); + + // Tear Down + TearDownRingtoetsFile(tempFile); + } + + private void SetUpRingtoetsFile(string filePath) + { + if (File.Exists(filePath)) + { + TearDownRingtoetsFile(filePath); + } + using (File.Create(filePath)) {} + } + + private void TearDownRingtoetsFile(string filePath) + { + GC.Collect(); + GC.WaitForPendingFinalizers(); + File.Delete(filePath); + } } } \ No newline at end of file