diff --git a/Source Code/TeamHobby.HobbyProjectGenerator.Archive/ArchiveManager.cs b/Source Code/TeamHobby.HobbyProjectGenerator.Archive/ArchiveManager.cs index 256ce31..2eee2fe 100644 --- a/Source Code/TeamHobby.HobbyProjectGenerator.Archive/ArchiveManager.cs +++ b/Source Code/TeamHobby.HobbyProjectGenerator.Archive/ArchiveManager.cs @@ -12,6 +12,10 @@ namespace TeamHobby.HobbyProjectGenerator.Archive _conn = dataSource; } + //public IDataSource GetConnection() + //{ + // return _conn; + //} // Create the folder where the compress file will be stored public bool CreateArchiveFolder(){ @@ -22,18 +26,25 @@ namespace TeamHobby.HobbyProjectGenerator.Archive // Find the root drive of the program string drive = Path.GetPathRoot(path: curPath); // Check if Drive exists or not - if (!Directory.Exists(drive)) - { - Console.WriteLine("Drive {0} not found", drive); - return false; - } + try { + if (!Directory.Exists(drive)) + { + Console.WriteLine("Drive {0} not found", drive); + return false; + } - string archiveFolder = curPath + folderName; - //Console.WriteLine("Creating a new folder at: {0}", archiveFolder); - if (!Directory.Exists(archiveFolder)) - { - Console.WriteLine("Creating a new folder at: {0}", archiveFolder); - Directory.CreateDirectory(archiveFolder); + string archiveFolder = curPath + folderName; + //Console.WriteLine("Creating a new folder at: {0}", archiveFolder); + if (!Directory.Exists(archiveFolder)) + { + Console.WriteLine("Creating a new folder at: {0}", archiveFolder); + Directory.CreateDirectory(archiveFolder); + //return true; + } + } + catch (Exception ex){ + // If the creating a folder failed, return false + return false; } return true; } @@ -91,7 +102,11 @@ namespace TeamHobby.HobbyProjectGenerator.Archive if (_conn.GetType() == typeof(SqlDAO)) { sqlDS = (SqlDAO)_conn; + } + // Check to make sure that Sql Data source connection is not null + if (sqlDS == null){ + return false; } // TODO: Remember to add try catch block here to make sure it is completed. @@ -103,11 +118,11 @@ namespace TeamHobby.HobbyProjectGenerator.Archive Console.WriteLine(""); // Creating a file name: - string filePath = @"C:\HobbyArchive"; + //string filePath = @"C:\HobbyArchive"; //Console.WriteLine("Creating file name ... "); //string curPath = archive.CreateOutFileName(); Console.WriteLine("Creating Output file name ..."); - string curPath = CreateOutFileName(); + string outPath = CreateOutFileName(); Console.WriteLine("Output file name created ..."); Console.WriteLine("----------------"); Console.WriteLine(""); @@ -116,46 +131,63 @@ namespace TeamHobby.HobbyProjectGenerator.Archive //string pathTemp = "C:/Temp/oldlogs10.txt"; //string pathTempBack = @"C:\Temp\oldlogs10.txt"; - //Output SQL to a text file - Console.WriteLine("Copying to a text file ..."); - sqlDS.CopyToFile(curPath); - Console.WriteLine("Copying completed ..."); - Console.WriteLine("----------------"); - Console.WriteLine(""); + // Try catch block to delete abort the compressing process + try{ + //Output SQL to a text file + Console.WriteLine("Copying to a text file ..."); + sqlDS.CopyToFile(outPath); + Console.WriteLine("Copying completed ..."); + Console.WriteLine("----------------"); + Console.WriteLine(""); - // Compress the file - Console.WriteLine("Copressing the text file ..."); - sqlDS.CompressFile(curPath); - Console.WriteLine("Copression completed ..."); - Console.WriteLine("----------------"); - Console.WriteLine(""); + // Compress the file + Console.WriteLine("Copressing the text file ..."); + sqlDS.CompressFile(outPath); + Console.WriteLine("Copression completed ..."); + Console.WriteLine("----------------"); + Console.WriteLine(""); - //Remove output file - Console.WriteLine("Removing the text file ..."); - sqlDS.RemoveOutputFile(curPath); - Console.WriteLine("Text File removal completed ..."); - Console.WriteLine("----------------"); - Console.WriteLine(""); + //Remove output file + Console.WriteLine("Removing the text file ..."); + sqlDS.RemoveOutputFile(outPath); + Console.WriteLine("Text File removal completed ..."); + Console.WriteLine("----------------"); + Console.WriteLine(""); - // Remove entries fromt the database - Console.WriteLine("Removing the text file ..."); - sqlDS.RemoveEntries(); - Console.WriteLine("Entries removal completed ..."); - Console.WriteLine("----------------"); - Console.WriteLine(""); + // Remove entries fromt the database + Console.WriteLine("Removing the text file ..."); + sqlDS.RemoveEntries(); + Console.WriteLine("Entries removal completed ..."); + Console.WriteLine("----------------"); + Console.WriteLine(""); + + // Return true if compressing sequence is successful + return true; + } + catch (Exception e){ + // Abort the compressing sequence and clean up uneccesary files. + Console.WriteLine("Archiving Sequenced failed. Cleaning up resources."); + + // Delete the text output file if any of the process failed + if (File.Exists(outPath)){ + File.Delete(outPath); + } + + // Delete the compressed file if Removing entries and removing text file output failed + string compFile = outPath + ".gz"; + if (File.Exists(compFile)){ + File.Delete(compFile); + } + + return false; + + } // Testing date time patters //string date = DateTime.Now.ToString("d"); //Console.WriteLine(date); //Console.WriteLine(DateTime.Now.ToString("M_d_yyyy_H:mm:ss")); - - return true; - } - - - - } } \ No newline at end of file diff --git a/Source Code/TeamHobby.HobbyProjectGenerator.ArchiveTests/ArchivingTests.cs b/Source Code/TeamHobby.HobbyProjectGenerator.ArchiveTests/ArchivingTests.cs new file mode 100644 index 0000000..88e7b50 --- /dev/null +++ b/Source Code/TeamHobby.HobbyProjectGenerator.ArchiveTests/ArchivingTests.cs @@ -0,0 +1,123 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TeamHobby.HobbyProjectGenerator.Archive; +using TeamHobby.HobbyProjectGenerator.DataAccess; + +namespace TeamHobby.HobbyProjectGenerator.ArchiveTests +{ + public class ArchivingTests + { + //private string dbInfo = "DRIVER={MariaDB ODBC 3.1 Driver};" + + // "SERVER=localhost;" + + // "DATABASE=hobby;" + + // "UID=root;" + + // "PASSWORD=Teamhobby;" + + // "OPTION=3"; + + + // [TestMethod] + public void IsArchiveFolderCreated(IDataSource sqlDAO) + { + // Arrange + //SqlDAO sqlDAO = new SqlDAO(dbInfo); + ArchiveManager archiveManager = new ArchiveManager(sqlDAO); + string folderPath = @"C:/HobbyArchive"; + bool expectedVal = true; + + // Act + archiveManager.CreateArchiveFolder(); + + + // Assert + bool actualVal = Directory.Exists(folderPath); + if (expectedVal == actualVal) + { + Console.WriteLine("Expected: {0}, Actual: {1}. Folder created correctly", expectedVal, actualVal); + try + { + Directory.Delete(folderPath, true); + } + catch { + Console.WriteLine("Folder failed to be deleted"); + } + } + else + { + Console.WriteLine("Expected: {0}, Actual: {1}. Folder created incorrectly", expectedVal, actualVal); + } + } + + // [TestMethod] + public void IsFileNameCreated(IDataSource sqlDAO) + { + // Arrange + string foldPath = @"C:\HobbyArchive"; + string fileName = DateTime.Now.ToString("M_d_yyyy") + "_archive.csv"; + string expectedFilePath = System.IO.Path.Combine(foldPath, fileName); + ArchiveManager archiveManager = new ArchiveManager(sqlDAO); + + // Act + string actualFilePath = archiveManager.CreateOutFileName(); + + // Assert + bool result = String.Equals(actualFilePath, expectedFilePath); + if (result) + { + Console.WriteLine("Expected: {0}, Actual: {1}. File name created correctly", expectedFilePath, actualFilePath); + } + else + { + Console.WriteLine("Expected: {0}, Actual: {1}. File name created incorrectly", expectedFilePath, actualFilePath); + } + + } + + public void CopyingToAFileTest(IDataSource sqlDAO) + { + // Arrange + ArchiveManager archiveManager = new ArchiveManager(sqlDAO); + SqlDAO sqlDS = null; + + if (sqlDAO.GetType() == typeof(SqlDAO)) + { + sqlDS = (SqlDAO)sqlDAO; + } + + // Act + + + // Assert + + } + + public static void Main(string[] args) + { + string dbInfo = "DRIVER={MariaDB ODBC 3.1 Driver};" + + "SERVER=localhost;" + + "DATABASE=hobby;" + + "UID=root;" + + "PASSWORD=Teamhobby;" + + "OPTION=3"; + SqlDAO sqlDAO = new SqlDAO(dbInfo); + //ArchiveManager archiveManager = new ArchiveManager(sqlDAO); + + // Testing folder creation + ArchivingTests test = new ArchivingTests(); + test.IsArchiveFolderCreated(sqlDAO); + Console.WriteLine("-----------------"); + Console.WriteLine(""); + + // Testing file creation + test.IsFileNameCreated(sqlDAO); + Console.WriteLine("-----------------"); + Console.WriteLine(""); + + + } + + } + +} diff --git a/Source Code/TeamHobby.HobbyProjectGenerator.ArchiveTests/TeamHobby.HobbyProjectGenerator.ArchiveTests.csproj b/Source Code/TeamHobby.HobbyProjectGenerator.ArchiveTests/TeamHobby.HobbyProjectGenerator.ArchiveTests.csproj new file mode 100644 index 0000000..560115c --- /dev/null +++ b/Source Code/TeamHobby.HobbyProjectGenerator.ArchiveTests/TeamHobby.HobbyProjectGenerator.ArchiveTests.csproj @@ -0,0 +1,14 @@ + + + + Exe + net6.0 + enable + enable + + + + + + + diff --git a/Source Code/TeamHobby.HobbyProjectGenerator.DataAccess/Implementations/SqlDAO.cs b/Source Code/TeamHobby.HobbyProjectGenerator.DataAccess/Implementations/SqlDAO.cs index 1843b50..06b919b 100644 --- a/Source Code/TeamHobby.HobbyProjectGenerator.DataAccess/Implementations/SqlDAO.cs +++ b/Source Code/TeamHobby.HobbyProjectGenerator.DataAccess/Implementations/SqlDAO.cs @@ -31,7 +31,7 @@ namespace TeamHobby.HobbyProjectGenerator.DataAccess // Getter and setter for Odbc public OdbcConnection Connection { get; set; } - public OdbcConnection getConnection() + public OdbcConnection GetConnection() { return _conn; } @@ -149,7 +149,6 @@ namespace TeamHobby.HobbyProjectGenerator.DataAccess // Get the stream of the original file using FileStream origFile = File.Open(fileName, FileMode.Open); - // Get the attribute of the file FileAttributes atrribute = File.GetAttributes(fileName); @@ -161,15 +160,16 @@ namespace TeamHobby.HobbyProjectGenerator.DataAccess using GZipStream compressor = new GZipStream(outputFile, CompressionMode.Compress); origFile.CopyTo(compressor); - return true; + return true; } return false; } catch (Exception ex) { + Console.WriteLine("Error when compressing a file !!, will be handled higher up the call stack"); Console.WriteLine(ex.Message); - return false; + throw; } } @@ -204,10 +204,10 @@ namespace TeamHobby.HobbyProjectGenerator.DataAccess return true; } - catch - { + catch (Exception ex) + { _conn.Close(); - Console.WriteLine("Error when copying query to a file !!"); + Console.WriteLine("Error when copying query to a file !!, will be handled higher up the call stack"); throw; } finally @@ -229,7 +229,7 @@ namespace TeamHobby.HobbyProjectGenerator.DataAccess } catch { Console.WriteLine("Removing file failed!!"); - return false; + throw; } } diff --git a/Source Code/TeamHobby.HobbyProjectGenerator/TeamHobby.HobbyProjectGenerator.sln b/Source Code/TeamHobby.HobbyProjectGenerator/TeamHobby.HobbyProjectGenerator.sln index 3f2c1f0..a3edeb3 100644 --- a/Source Code/TeamHobby.HobbyProjectGenerator/TeamHobby.HobbyProjectGenerator.sln +++ b/Source Code/TeamHobby.HobbyProjectGenerator/TeamHobby.HobbyProjectGenerator.sln @@ -22,6 +22,7 @@ EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TeamHobby.HobbyProjectGenerator.UserManagement", "..\TeamHobby.HobbyProjectGenerator.UserManagement\TeamHobby.HobbyProjectGenerator.UserManagement.csproj", "{2E7193B8-86B6-48DA-9671-CD84615A5F5D}" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TeamHobby.HobbyProjectGenerator.Logging.Tests", "..\TeamHobby.HobbyProjectGenerator.Logging.Tests\TeamHobby.HobbyProjectGenerator.Logging.Tests.csproj", "{CA8D11CF-807C-4C90-A529-0371F73518F6}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TeamHobby.HobbyProjectGenerator.ArchiveTests", "..\TeamHobby.HobbyProjectGenerator.ArchiveTests\TeamHobby.HobbyProjectGenerator.ArchiveTests.csproj", "{C5EBD1F8-C806-4BF9-B2D7-8876072630FD}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -107,6 +108,10 @@ Global {CA8D11CF-807C-4C90-A529-0371F73518F6}.Release|Any CPU.Build.0 = Release|Any CPU {CA8D11CF-807C-4C90-A529-0371F73518F6}.Release|x86.ActiveCfg = Release|Any CPU {CA8D11CF-807C-4C90-A529-0371F73518F6}.Release|x86.Build.0 = Release|Any CPU + {C5EBD1F8-C806-4BF9-B2D7-8876072630FD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C5EBD1F8-C806-4BF9-B2D7-8876072630FD}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C5EBD1F8-C806-4BF9-B2D7-8876072630FD}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C5EBD1F8-C806-4BF9-B2D7-8876072630FD}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Source Code/main/Controller.cs b/Source Code/main/Controller.cs index d44e163..730cd42 100644 --- a/Source Code/main/Controller.cs +++ b/Source Code/main/Controller.cs @@ -4,6 +4,7 @@ using TeamHobby.HobbyProjectGenerator.Archive; using TeamHobby.HobbyProjectGenerator.DataAccess; using TeamHobby.HobbyProjectGenerator.Logging; using TeamHobby.HobbyProjectGenerator.UserManagement; + namespace TeamHobby.HobbyProjectGenerator.Main { public class GetCredentials @@ -39,7 +40,7 @@ namespace TeamHobby.HobbyProjectGenerator.Main // Creating the Factory class string dbType = "sql"; - RDSFactory factory = new RDSFactory(); + RDSFactory dbFactory = new RDSFactory(); // Testing Data Access Layer string dbInfo = "DRIVER={MariaDB ODBC 3.1 Driver};" + @@ -49,11 +50,11 @@ namespace TeamHobby.HobbyProjectGenerator.Main "UID=root;" + "PASSWORD=Teamhobby;" + "PORT=3306;"; - IDataSource datasource = dbFactory.getDataSource(dbType, dbInfo); + "OPTION=3"; - IDataSource datasource = factory.getDataSource(dbType, dbInfo);*/ - + + IDataSource datasource = dbFactory.getDataSource(dbType, dbInfo); // Create manager class from UserManagement SystemAccountManager manager = new SystemAccountManager(); diff --git a/doc/LowLevel/ArchivingWithError.png b/doc/LowLevel/ArchivingLL/ArchivingWithError.png similarity index 100% rename from doc/LowLevel/ArchivingWithError.png rename to doc/LowLevel/ArchivingLL/ArchivingWithError.png diff --git a/doc/LowLevel/ArchivingWithError.svg b/doc/LowLevel/ArchivingLL/ArchivingWithError.svg similarity index 100% rename from doc/LowLevel/ArchivingWithError.svg rename to doc/LowLevel/ArchivingLL/ArchivingWithError.svg diff --git a/doc/LowLevel/ArchivingWithError.txt b/doc/LowLevel/ArchivingLL/ArchivingWithError.txt similarity index 100% rename from doc/LowLevel/ArchivingWithError.txt rename to doc/LowLevel/ArchivingLL/ArchivingWithError.txt diff --git a/doc/LowLevel/ArchivingWithSingleton.pdf b/doc/LowLevel/ArchivingLL/ArchivingWithSingleton.pdf similarity index 100% rename from doc/LowLevel/ArchivingWithSingleton.pdf rename to doc/LowLevel/ArchivingLL/ArchivingWithSingleton.pdf diff --git a/doc/LowLevel/ArchvingMasterCon.txt b/doc/LowLevel/ArchivingLL/ArchvingMasterCon.txt similarity index 100% rename from doc/LowLevel/ArchvingMasterCon.txt rename to doc/LowLevel/ArchivingLL/ArchvingMasterCon.txt diff --git a/doc/LowLevel/archiving.pdf b/doc/LowLevel/ArchivingLL/archiving.pdf similarity index 100% rename from doc/LowLevel/archiving.pdf rename to doc/LowLevel/ArchivingLL/archiving.pdf diff --git a/doc/LowLevel/archiving.txt b/doc/LowLevel/ArchivingLL/archiving.txt similarity index 100% rename from doc/LowLevel/archiving.txt rename to doc/LowLevel/ArchivingLL/archiving.txt diff --git a/doc/LowLevel/archiving2.png b/doc/LowLevel/ArchivingLL/archiving2.png similarity index 100% rename from doc/LowLevel/archiving2.png rename to doc/LowLevel/ArchivingLL/archiving2.png diff --git a/doc/LowLevel/archiving2.txt b/doc/LowLevel/ArchivingLL/archiving2.txt similarity index 100% rename from doc/LowLevel/archiving2.txt rename to doc/LowLevel/ArchivingLL/archiving2.txt diff --git a/doc/LowLevel/archivingMasterCon.pdf b/doc/LowLevel/ArchivingLL/archivingMasterCon.pdf similarity index 100% rename from doc/LowLevel/archivingMasterCon.pdf rename to doc/LowLevel/ArchivingLL/archivingMasterCon.pdf diff --git a/doc/LowLevel/archivingSubmission.pdf b/doc/LowLevel/ArchivingLL/archivingSubmission.pdf similarity index 100% rename from doc/LowLevel/archivingSubmission.pdf rename to doc/LowLevel/ArchivingLL/archivingSubmission.pdf diff --git a/doc/LowLevel/archivingWithError.pdf b/doc/LowLevel/ArchivingLL/archivingWithError.pdf similarity index 100% rename from doc/LowLevel/archivingWithError.pdf rename to doc/LowLevel/ArchivingLL/archivingWithError.pdf diff --git a/doc/LowLevel/archivingWithError2.pdf b/doc/LowLevel/ArchivingLL/archivingWithError2.pdf similarity index 100% rename from doc/LowLevel/archivingWithError2.pdf rename to doc/LowLevel/ArchivingLL/archivingWithError2.pdf diff --git a/doc/LowLevel/archiving_5.txt b/doc/LowLevel/ArchivingLL/archiving_5.txt similarity index 100% rename from doc/LowLevel/archiving_5.txt rename to doc/LowLevel/ArchivingLL/archiving_5.txt diff --git a/doc/LowLevel/archivngWithError.txt b/doc/LowLevel/ArchivingLL/archivngWithError.txt similarity index 100% rename from doc/LowLevel/archivngWithError.txt rename to doc/LowLevel/ArchivingLL/archivngWithError.txt diff --git a/doc/LowLevel/arcvhingMasterCon.pdf b/doc/LowLevel/ArchivingLL/arcvhingMasterCon.pdf similarity index 100% rename from doc/LowLevel/arcvhingMasterCon.pdf rename to doc/LowLevel/ArchivingLL/arcvhingMasterCon.pdf