diff --git a/Covid19Radar/Covid19Radar/Services/Logs/ILogFileService.cs b/Covid19Radar/Covid19Radar/Services/Logs/ILogFileService.cs index abf8a1b55..c88485848 100644 --- a/Covid19Radar/Covid19Radar/Services/Logs/ILogFileService.cs +++ b/Covid19Radar/Covid19Radar/Services/Logs/ILogFileService.cs @@ -8,9 +8,9 @@ public interface ILogFileService { // Log upload string CreateLogId(); - string LogUploadingFileName(string logId); - bool CreateLogUploadingFileToTmpPath(string logUploadingFileName); - bool CopyLogUploadingFileToPublicPath(string logUploadingFileName); + string CreateZipFileName(string logId); + string CreateZipFile(string fileName); + string CopyLogUploadingFileToPublicPath(string logPath); bool DeleteAllLogUploadingFiles(); // Log rotate diff --git a/Covid19Radar/Covid19Radar/Services/Logs/ILogUploadService.cs b/Covid19Radar/Covid19Radar/Services/Logs/ILogUploadService.cs index 3d9c0842a..18361487f 100644 --- a/Covid19Radar/Covid19Radar/Services/Logs/ILogUploadService.cs +++ b/Covid19Radar/Covid19Radar/Services/Logs/ILogUploadService.cs @@ -8,6 +8,6 @@ namespace Covid19Radar.Services.Logs { public interface ILogUploadService { - Task UploadAsync(string zipFileName, string sasToken); + Task UploadAsync(string zipFilePath, string sasToken); } } diff --git a/Covid19Radar/Covid19Radar/Services/Logs/LogFileService.cs b/Covid19Radar/Covid19Radar/Services/Logs/LogFileService.cs index 807847219..b8bcede72 100644 --- a/Covid19Radar/Covid19Radar/Services/Logs/LogFileService.cs +++ b/Covid19Radar/Covid19Radar/Services/Logs/LogFileService.cs @@ -13,8 +13,8 @@ public class LogFileService : ILogFileService { #region Static Fields - private static readonly string logUploadFilePrefix = "cocoa_log_"; - private static readonly string logUploadFileExtension = "zip"; + private static readonly string logFilePrefix = "cocoa_log_"; + private static readonly string logFileExtension = "zip"; #endregion @@ -45,12 +45,12 @@ IBackupAttributeService backupAttributeService public string CreateLogId() => Guid.NewGuid().ToString(); - public string LogUploadingFileName(string logId) + public string CreateZipFileName(string logId) { - return logUploadFilePrefix + logId + "." + logUploadFileExtension; + return logFilePrefix + logId + "." + logFileExtension; } - public bool CreateLogUploadingFileToTmpPath(string logUploadingFileName) + public string CreateZipFile(string fileName) { loggerService.StartMethod(); try @@ -59,42 +59,50 @@ public bool CreateLogUploadingFileToTmpPath(string logUploadingFileName) var logFiles = Directory.GetFiles(logsDirPath, logPathService.LogFileWildcardName); if (logFiles.Length == 0) { - loggerService.EndMethod(); - return false; + return null; } - ZipFile.CreateFromDirectory(logsDirPath, Path.Combine(logPathService.LogUploadingTmpPath, logUploadingFileName)); - loggerService.EndMethod(); - return true; + + var zipFilePath = Path.Combine(logPathService.LogUploadingTmpPath, fileName); + ZipFile.CreateFromDirectory(logsDirPath, zipFilePath); + + return zipFilePath; } - catch (Exception) + catch (Exception exception) + { + loggerService.Exception("Failed to create uploading file", exception); + return null; + } + finally { - loggerService.Error("Failed to create uploading file"); loggerService.EndMethod(); - return false; } } - public bool CopyLogUploadingFileToPublicPath(string logUploadingFileName) + public string CopyLogUploadingFileToPublicPath(string logPath) { loggerService.StartMethod(); try { + var logFileName = Path.GetFileName(logPath); var tmpPath = logPathService.LogUploadingTmpPath; var publicPath = logPathService.LogUploadingPublicPath; if (string.IsNullOrEmpty(tmpPath) || string.IsNullOrEmpty(publicPath)) { - loggerService.EndMethod(); - return false; + return null; } - File.Copy(Path.Combine(tmpPath, logUploadingFileName), Path.Combine(publicPath, logUploadingFileName), true); - loggerService.EndMethod(); - return true; + var destPath = Path.Combine(publicPath, logFileName); + File.Copy(logPath, destPath, true); + + return destPath; } - catch (Exception) + catch (Exception exception) + { + loggerService.Exception("Failed to copy log file", exception); + return null; + } + finally { - loggerService.Error("Failed to copy log file"); loggerService.EndMethod(); - return false; } } diff --git a/Covid19Radar/Covid19Radar/Services/Logs/LogUploadService.cs b/Covid19Radar/Covid19Radar/Services/Logs/LogUploadService.cs index 0786e2dc7..15f9cad46 100644 --- a/Covid19Radar/Covid19Radar/Services/Logs/LogUploadService.cs +++ b/Covid19Radar/Covid19Radar/Services/Logs/LogUploadService.cs @@ -3,7 +3,6 @@ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ using System; -using System.IO; using System.Threading.Tasks; namespace Covid19Radar.Services.Logs @@ -24,7 +23,7 @@ public LogUploadService( this.storageService = storageService; } - public async Task UploadAsync(string zipFileName, string sasToken) + public async Task UploadAsync(string zipFilePath, string sasToken) { loggerService.StartMethod(); @@ -34,14 +33,13 @@ public async Task UploadAsync(string zipFileName, string sasToken) { // Upload to storage. var logTmpPath = logPathService.LogUploadingTmpPath; - var logZipPath = Path.Combine(logTmpPath, zipFileName); var setting = AppSettings.Instance; var endpoint = setting.LogStorageEndpoint; var uploadPath = setting.LogStorageContainerName; var accountName = setting.LogStorageAccountName; - var uploadResult = await storageService.UploadAsync(endpoint, uploadPath, accountName, sasToken, logZipPath); + var uploadResult = await storageService.UploadAsync(endpoint, uploadPath, accountName, sasToken, zipFilePath); if (!uploadResult) { throw new Exception("Failed to upload to storage."); diff --git a/Covid19Radar/Covid19Radar/ViewModels/HelpPage/InqueryPageViewModel.cs b/Covid19Radar/Covid19Radar/ViewModels/HelpPage/InqueryPageViewModel.cs index e3bf900ef..6430a01f6 100644 --- a/Covid19Radar/Covid19Radar/ViewModels/HelpPage/InqueryPageViewModel.cs +++ b/Covid19Radar/Covid19Radar/ViewModels/HelpPage/InqueryPageViewModel.cs @@ -4,6 +4,7 @@ using System; using System.Threading.Tasks; +using Acr.UserDialogs; using Covid19Radar.Resources; using Covid19Radar.Services; using Covid19Radar.Services.Logs; @@ -17,15 +18,26 @@ namespace Covid19Radar.ViewModels public class InqueryPageViewModel : ViewModelBase { private readonly ILoggerService loggerService; + private readonly ILogFileService logFileService; + private readonly ILogPathService logPathService; + private readonly IEssentialsService essentialsService; public Func BrowserOpenAsync = Browser.OpenAsync; public Func ComposeEmailAsync { get; set; } = Email.ComposeAsync; - public InqueryPageViewModel(INavigationService navigationService, ILoggerService loggerService, IEssentialsService essentialsService) : base(navigationService) + public InqueryPageViewModel( + INavigationService navigationService, + ILoggerService loggerService, + ILogFileService logFileService, + ILogPathService logPathService, + IEssentialsService essentialsService + ) : base(navigationService) { Title = AppResources.InqueryPageTitle; this.loggerService = loggerService; + this.logFileService = logFileService; + this.logPathService = logPathService; this.essentialsService = essentialsService; } @@ -43,9 +55,79 @@ public InqueryPageViewModel(INavigationService navigationService, ILoggerService { loggerService.StartMethod(); - _ = await NavigationService.NavigateAsync(nameof(SendLogConfirmationPage)); + try + { + UserDialogs.Instance.ShowLoading(AppResources.Processing); - loggerService.EndMethod(); + var (logId, zipFilePath) = CreateZipFile(); + + UserDialogs.Instance.HideLoading(); + + if (zipFilePath is null) + { + // Failed to create ZIP file + await UserDialogs.Instance.AlertAsync( + AppResources.FailedMessageToGetOperatingInformation, + AppResources.Error, + AppResources.ButtonOk); + return; + } + + loggerService.Info($"zipFilePath: {zipFilePath}"); + + INavigationParameters navigationParameters + = SendLogConfirmationPage.BuildNavigationParams(logId, zipFilePath); + + _ = await NavigationService.NavigateAsync(nameof(SendLogConfirmationPage), navigationParameters); + } + finally + { + loggerService.EndMethod(); + } + }); + + public Command OnClickShareLogCommand => new Command(async () => + { + loggerService.StartMethod(); + + try + { + UserDialogs.Instance.ShowLoading(AppResources.Processing); + + var (logId, zipFilePath) = CreateZipFile(); + + UserDialogs.Instance.HideLoading(); + + if (zipFilePath is null) + { + // Failed to create ZIP file + await UserDialogs.Instance.AlertAsync( + AppResources.FailedMessageToGetOperatingInformation, + AppResources.Error, + AppResources.ButtonOk); + return; + } + + loggerService.Info($"zipFilePath: {zipFilePath}"); + + string sharePath = logFileService.CopyLogUploadingFileToPublicPath(zipFilePath); + + try + { + await Share.RequestAsync(new ShareFileRequest + { + File = new ShareFile(sharePath) + }); + } + catch (NotImplementedInReferenceAssemblyException exception) + { + loggerService.Exception("NotImplementedInReferenceAssemblyException", exception); + } + } + finally + { + loggerService.EndMethod(); + } }); public Command OnClickEmailCommand => new Command(async () => @@ -85,5 +167,17 @@ private string CreateInquiryMailBody() + AppResources.InquiryMailOSVersionTitle + essentialsService.PlatformVersion + "\r\n" + AppResources.InquiryMailAppVersionTitle + essentialsService.AppVersion; } + + private (string, string) CreateZipFile() + { + string logId = logFileService.CreateLogId(); + string zipFileName = logFileService.CreateZipFileName(logId); + + logFileService.Rotate(); + + var zipFilePath = logFileService.CreateZipFile(zipFileName); + + return (logId, zipFilePath); + } } } diff --git a/Covid19Radar/Covid19Radar/ViewModels/HelpPage/SendLogConfirmationPageViewModel.cs b/Covid19Radar/Covid19Radar/ViewModels/HelpPage/SendLogConfirmationPageViewModel.cs index 511b35666..dc07c35df 100644 --- a/Covid19Radar/Covid19Radar/ViewModels/HelpPage/SendLogConfirmationPageViewModel.cs +++ b/Covid19Radar/Covid19Radar/ViewModels/HelpPage/SendLogConfirmationPageViewModel.cs @@ -3,7 +3,6 @@ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ using System; -using System.IO; using System.Net; using System.Threading.Tasks; using Acr.UserDialogs; @@ -31,28 +30,47 @@ public class SendLogConfirmationPageViewModel : ViewModelBase public Func TaskRun { get; set; } = Task.Run; private string LogId { get; set; } - private string ZipFileName { get; set; } + private string ZipFilePath { get; set; } public SendLogConfirmationPageViewModel( INavigationService navigationService, - ILogFileService logFileService, ILoggerService loggerService, + ILogFileService logFileService, ILogUploadService logUploadService, - ILogPathService logPathService, IHttpDataService httpDataService) : base(navigationService) { this.loggerService = loggerService; this.logFileService = logFileService; this.logUploadService = logUploadService; - this.logPathService = logPathService; this.httpDataService = httpDataService; } - public Command OnClickConfirmLogCommand => new Command(() => + public Command OnClickConfirmLogCommand => new Command(async () => { loggerService.StartMethod(); - CopyZipFileToPublicPath(); + string sharePath = logFileService.CopyLogUploadingFileToPublicPath(ZipFilePath); + + if (sharePath is null) + { + await UserDialogs.Instance.AlertAsync( + AppResources.FailedMessageToSaveOperatingInformation, + AppResources.Error, + AppResources.ButtonOk); + return; + } + + try + { + await Share.RequestAsync(new ShareFileRequest + { + File = new ShareFile(sharePath) + }); + } + catch (NotImplementedInReferenceAssemblyException exception) + { + loggerService.Exception("NotImplementedInReferenceAssemblyException", exception); + } loggerService.EndMethod(); }); @@ -81,7 +99,7 @@ await UserDialogs.Instance.AlertAsync( var uploadResult = false; if (response.StatusCode == (int)HttpStatusCode.OK && !string.IsNullOrEmpty(response.Result.SasToken)) { - uploadResult = await logUploadService.UploadAsync(ZipFileName, response.Result.SasToken); + uploadResult = await logUploadService.UploadAsync(ZipFilePath, response.Result.SasToken); } UserDialogs.Instance.HideLoading(); @@ -129,7 +147,11 @@ public override void Initialize(INavigationParameters parameters) loggerService.StartMethod(); base.Initialize(parameters); - CreateZipFile(); + + LogId = parameters.GetValue(SendLogConfirmationPage.LogIdKey); + ZipFilePath = parameters.GetValue(SendLogConfirmationPage.ZipFilePathKey); + + loggerService.Info($"ZipFilePath: {ZipFilePath}"); loggerService.EndMethod(); } @@ -141,75 +163,5 @@ public override void Destroy() logFileService.DeleteAllLogUploadingFiles(); loggerService.EndMethod(); } - - private void CreateZipFile() - { - LogId = logFileService.CreateLogId(); - ZipFileName = logFileService.LogUploadingFileName(LogId); - - UserDialogs.Instance.ShowLoading(Resources.AppResources.Processing); - - _ = TaskRun(() => - { - logFileService.Rotate(); - - var result = logFileService.CreateLogUploadingFileToTmpPath(ZipFileName); - - BeginInvokeOnMainThread(async () => - { - UserDialogs.Instance.HideLoading(); - - if (!result) - { - // Failed to create ZIP file - await UserDialogs.Instance.AlertAsync( - Resources.AppResources.FailedMessageToGetOperatingInformation, - Resources.AppResources.Error, - Resources.AppResources.ButtonOk); - - _ = await NavigationService.GoBackAsync(); - } - }); - }); - } - - private void CopyZipFileToPublicPath() - { - - _ = TaskRun(() => - { - var result = logFileService.CopyLogUploadingFileToPublicPath(ZipFileName); - - BeginInvokeOnMainThread(async () => - { - - if (!result) - { - await UserDialogs.Instance.AlertAsync( - Resources.AppResources.FailedMessageToSaveOperatingInformation, - Resources.AppResources.Error, - Resources.AppResources.ButtonOk); - } - else - { - var publicPath = logPathService.LogUploadingPublicPath; - var logUploadingFileName = logFileService.LogUploadingFileName(LogId); - var path = Path.Combine(publicPath, logUploadingFileName); - - try - { - await Share.RequestAsync(new ShareFileRequest - { - File = new ShareFile(path) - }); - } - catch (NotImplementedInReferenceAssemblyException exception) - { - loggerService.Exception("NotImplementedInReferenceAssemblyException", exception); - } - } - }); - }); - } } } diff --git a/Covid19Radar/Covid19Radar/Views/HelpPage/SendLogConfirmationPage.xaml.cs b/Covid19Radar/Covid19Radar/Views/HelpPage/SendLogConfirmationPage.xaml.cs index bbc95ad71..db838b812 100644 --- a/Covid19Radar/Covid19Radar/Views/HelpPage/SendLogConfirmationPage.xaml.cs +++ b/Covid19Radar/Covid19Radar/Views/HelpPage/SendLogConfirmationPage.xaml.cs @@ -2,6 +2,7 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ +using Prism.Navigation; using Xamarin.Forms; using Xamarin.Forms.Xaml; @@ -10,9 +11,28 @@ namespace Covid19Radar.Views [XamlCompilation(XamlCompilationOptions.Compile)] public partial class SendLogConfirmationPage : ContentPage { + public const string LogIdKey = "logIdKey"; + public const string ZipFilePathKey = "zipFilePathKey"; + public SendLogConfirmationPage() { InitializeComponent(); } + + public static INavigationParameters BuildNavigationParams( + string logId, + string zipFilePath, + INavigationParameters? baseParam = null + ) + { + var param = new NavigationParameters(); + param.CopyFrom(baseParam); + + param.Add(LogIdKey, logId); + param.Add(ZipFilePathKey, zipFilePath); + + return param; + } + } } diff --git a/Covid19Radar/Tests/Covid19Radar.UnitTests/Services/Logs/LogFileServiceTests.cs b/Covid19Radar/Tests/Covid19Radar.UnitTests/Services/Logs/LogFileServiceTests.cs index 36a0b2473..fd3428d4f 100644 --- a/Covid19Radar/Tests/Covid19Radar.UnitTests/Services/Logs/LogFileServiceTests.cs +++ b/Covid19Radar/Tests/Covid19Radar.UnitTests/Services/Logs/LogFileServiceTests.cs @@ -35,7 +35,7 @@ public void LogUploadingFileName_Success() var logFileService = CreateDefaultLogFileService(mockILogPathService); var logId = logFileService.CreateLogId(); - var fileName = logFileService.LogUploadingFileName(logId); + var fileName = logFileService.CreateZipFileName(logId); Assert.Equal(fileName, "cocoa_log_" + logId + ".zip"); } @@ -50,9 +50,9 @@ public void CreateLogUploadingFileToTmpPath_Success() RecreateDir(tmpDirPath); var logId = logFileService.CreateLogId(); - var fileName = logFileService.LogUploadingFileName(logId); - var result = logFileService.CreateLogUploadingFileToTmpPath(fileName); - Assert.True(result); + var fileName = logFileService.CreateZipFileName(logId); + var result = logFileService.CreateZipFile(fileName); + Assert.Equal(Path.Combine(tmpDirPath, fileName), result); var uploadingFilePath = tmpDirPath + fileName; Assert.True(File.Exists(uploadingFilePath)); @@ -68,9 +68,9 @@ public void CreateLogUploadingFileToTmpPath_Log_Not_Exists() RecreateDir(tmpDirPath); var logId = logFileService.CreateLogId(); - var fileName = logFileService.LogUploadingFileName(logId); - var result = logFileService.CreateLogUploadingFileToTmpPath(fileName); - Assert.False(result); + var fileName = logFileService.CreateZipFileName(logId); + var result = logFileService.CreateZipFile(fileName); + Assert.Null(result); var uploadingFilePath = tmpDirPath + fileName; Assert.False(File.Exists(uploadingFilePath)); @@ -86,9 +86,9 @@ public void CreateLogUploadingFileToTmpPath_LogDir_Not_Exists() RecreateDir(tmpDirPath); var logId = logFileService.CreateLogId(); - var fileName = logFileService.LogUploadingFileName(logId); - var result = logFileService.CreateLogUploadingFileToTmpPath(fileName); - Assert.False(result); + var fileName = logFileService.CreateZipFileName(logId); + var result = logFileService.CreateZipFile(fileName); + Assert.Null(result); var uploadingFilePath = tmpDirPath + fileName; Assert.False(File.Exists(uploadingFilePath)); @@ -106,11 +106,12 @@ public void CopyLogUploadingFileToPublicPath_Success() RecreateDir(tmpDirPath); var logId = logFileService.CreateLogId(); - var fileName = logFileService.LogUploadingFileName(logId); - var result = logFileService.CreateLogUploadingFileToTmpPath(fileName); - Assert.True(result); - result = logFileService.CopyLogUploadingFileToPublicPath(fileName); - Assert.True(result); + var fileName = logFileService.CreateZipFileName(logId); + var result = logFileService.CreateZipFile(fileName); + Assert.Equal(Path.Combine(tmpDirPath, fileName), result); + + result = logFileService.CopyLogUploadingFileToPublicPath(Path.Combine(tmpDirPath, fileName)); + Assert.Equal(Path.Combine(publicDirPath, fileName), result); var uploadingFilePath = publicDirPath + fileName; Assert.True(File.Exists(uploadingFilePath)); @@ -128,11 +129,12 @@ public void CopyLogUploadingFileToPublicPath_PublicDir_Not_Exists() RecreateDir(tmpDirPath); var logId = logFileService.CreateLogId(); - var fileName = logFileService.LogUploadingFileName(logId); - var result = logFileService.CreateLogUploadingFileToTmpPath(fileName); - Assert.True(result); - result = logFileService.CopyLogUploadingFileToPublicPath(fileName); - Assert.False(result); + var fileName = logFileService.CreateZipFileName(logId); + var result = logFileService.CreateZipFile(fileName); + Assert.Equal(result, Path.Combine(tmpDirPath, fileName)); + + result = logFileService.CopyLogUploadingFileToPublicPath(Path.Combine(tmpDirPath, fileName)); + Assert.Null(result); var uploadingFilePath = publicDirPath + fileName; Assert.False(File.Exists(uploadingFilePath)); @@ -145,9 +147,9 @@ public void CopyLogUploadingFileToPublicPath_TmpPath_Empty() var logFileService = CreateDefaultLogFileService(mockILogPathService); var logId = logFileService.CreateLogId(); - var fileName = logFileService.LogUploadingFileName(logId); + var fileName = logFileService.CreateZipFileName(logId); var result = logFileService.CopyLogUploadingFileToPublicPath(fileName); - Assert.False(result); + Assert.Null(result); var uploadingFilePath = publicDirPath + fileName; Assert.False(File.Exists(uploadingFilePath)); @@ -160,9 +162,9 @@ public void CopyLogUploadingFileToPublicPath_PublicPath_Empty() var logFileService = CreateDefaultLogFileService(mockILogPathService); var logId = logFileService.CreateLogId(); - var fileName = logFileService.LogUploadingFileName(logId); + var fileName = logFileService.CreateZipFileName(logId); var result = logFileService.CopyLogUploadingFileToPublicPath(fileName); - Assert.False(result); + Assert.Null(result); var uploadingFilePath = publicDirPath + fileName; Assert.False(File.Exists(uploadingFilePath)); @@ -179,12 +181,12 @@ public void DeleteAllLogUploadingFiles_Success() RecreateDir(tmpDirPath); var logId = logFileService.CreateLogId(); - var fileName = logFileService.LogUploadingFileName(logId); - var result = logFileService.CreateLogUploadingFileToTmpPath(fileName); - Assert.True(result); + var fileName = logFileService.CreateZipFileName(logId); + var result = logFileService.CreateZipFile(fileName); + Assert.Equal(Path.Combine(tmpDirPath, fileName), result); - result = logFileService.DeleteAllLogUploadingFiles(); - Assert.True(result); + bool deleteResult = logFileService.DeleteAllLogUploadingFiles(); + Assert.True(deleteResult); var uploadingFilePath = tmpDirPath + fileName; Assert.False(File.Exists(uploadingFilePath)); @@ -211,14 +213,14 @@ public void DeleteAllLogUploadingFiles_TmpDir_Not_Exists() RecreateDir(tmpDirPath); var logId = logFileService.CreateLogId(); - var fileName = logFileService.LogUploadingFileName(logId); - var result = logFileService.CreateLogUploadingFileToTmpPath(fileName); - Assert.True(result); + var fileName = logFileService.CreateZipFileName(logId); + var result = logFileService.CreateZipFile(fileName); + Assert.Equal(Path.Combine(tmpDirPath, fileName), result); DeleteDirIfExists(tmpDirPath); - result = logFileService.DeleteAllLogUploadingFiles(); - Assert.False(result); + bool deleteResult = logFileService.DeleteAllLogUploadingFiles(); + Assert.False(deleteResult); } [Fact] diff --git a/Covid19Radar/Tests/Covid19Radar.UnitTests/Services/Logs/LogUploadServiceTests.cs b/Covid19Radar/Tests/Covid19Radar.UnitTests/Services/Logs/LogUploadServiceTests.cs index 1e6a06fd0..218444cfd 100644 --- a/Covid19Radar/Tests/Covid19Radar.UnitTests/Services/Logs/LogUploadServiceTests.cs +++ b/Covid19Radar/Tests/Covid19Radar.UnitTests/Services/Logs/LogUploadServiceTests.cs @@ -42,14 +42,15 @@ public async void UploadAsyncTests_Success() var testZipFileName = "zip-file.zip"; var testTmpPath = Path.Combine("log", "tmp", "path"); var testSasToken = "test-sas-token"; + var testZipFilePath = Path.Combine(testTmpPath, testZipFileName); mockLogPathService.Setup(x => x.LogUploadingTmpPath).Returns(testTmpPath); - mockStorageService.Setup(x => x.UploadAsync("https://LOG_STORAGE_URL_BASE/", "LOG_STORAGE_CONTAINER_NAME", "LOG_STORAGE_ACCOUNT_NAME", testSasToken, Path.Combine(testTmpPath, testZipFileName))).ReturnsAsync(true); + mockStorageService.Setup(x => x.UploadAsync("https://LOG_STORAGE_URL_BASE/", "LOG_STORAGE_CONTAINER_NAME", "LOG_STORAGE_ACCOUNT_NAME", testSasToken, testZipFilePath)).ReturnsAsync(true); - var result = await unitUnderTest.UploadAsync(testZipFileName, testSasToken); + var result = await unitUnderTest.UploadAsync(testZipFilePath, testSasToken); Assert.True(result); - mockStorageService.Verify(x => x.UploadAsync("https://LOG_STORAGE_URL_BASE/", "LOG_STORAGE_CONTAINER_NAME", "LOG_STORAGE_ACCOUNT_NAME", testSasToken, Path.Combine(testTmpPath, testZipFileName)), Times.Once()); + mockStorageService.Verify(x => x.UploadAsync("https://LOG_STORAGE_URL_BASE/", "LOG_STORAGE_CONTAINER_NAME", "LOG_STORAGE_ACCOUNT_NAME", testSasToken, testZipFilePath), Times.Once()); } [Fact] @@ -60,11 +61,12 @@ public async void UploadAsyncTests_UploadAsyncFailure() var testZipFileName = "zip-file.zip"; var testTmpPath = Path.Combine("log", "tmp", "path"); var testSasToken = "test-sas-token"; + var testZipFilePath = Path.Combine(testTmpPath, testZipFileName); mockLogPathService.Setup(x => x.LogUploadingTmpPath).Returns(testTmpPath); mockStorageService.Setup(x => x.UploadAsync(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(false); - var result = await unitUnderTest.UploadAsync(testZipFileName, testSasToken); + var result = await unitUnderTest.UploadAsync(testZipFilePath, testSasToken); Assert.False(result); mockStorageService.Verify(x => x.UploadAsync(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Once()); @@ -78,10 +80,11 @@ public async void UploadAsyncTests_UnexpectedError() var testZipFileName = "zip-file.zip"; var testTmpPath = Path.Combine("log", "tmp", "path"); var testSasToken = "test-sas-token"; + var testZipFilePath = Path.Combine(testTmpPath, testZipFileName); - mockStorageService.Setup(x => x.UploadAsync("https://LOG_STORAGE_URL_BASE/", "LOG_STORAGE_CONTAINER_NAME", "LOG_STORAGE_ACCOUNT_NAME", testSasToken, Path.Combine(testTmpPath, testZipFileName))).ReturnsAsync(false); + mockStorageService.Setup(x => x.UploadAsync("https://LOG_STORAGE_URL_BASE/", "LOG_STORAGE_CONTAINER_NAME", "LOG_STORAGE_ACCOUNT_NAME", testSasToken, testZipFilePath)).ReturnsAsync(false); - var result = await unitUnderTest.UploadAsync(testZipFileName, testSasToken); + var result = await unitUnderTest.UploadAsync(testZipFilePath, testSasToken); Assert.False(result); } diff --git a/Covid19Radar/Tests/Covid19Radar.UnitTests/ViewModels/HelpPage/InqueryPageViewModelTests.cs b/Covid19Radar/Tests/Covid19Radar.UnitTests/ViewModels/HelpPage/InqueryPageViewModelTests.cs index 6a76d8682..b6e7a902c 100644 --- a/Covid19Radar/Tests/Covid19Radar.UnitTests/ViewModels/HelpPage/InqueryPageViewModelTests.cs +++ b/Covid19Radar/Tests/Covid19Radar.UnitTests/ViewModels/HelpPage/InqueryPageViewModelTests.cs @@ -4,6 +4,7 @@ using System; using System.Threading.Tasks; +using Acr.UserDialogs; using Covid19Radar.Services; using Covid19Radar.Services.Logs; using Covid19Radar.ViewModels; @@ -19,13 +20,23 @@ public class InqueryPageViewModelTests private readonly MockRepository mockRepository; private readonly Mock mockNavigationService; private readonly Mock mockLoggerService; + private readonly Mock mockLogFileService; + private readonly Mock mockLogPathService; private readonly Mock mockEssentialsService; + private readonly Mock mockUserDialogs; + public InqueryPageViewModelTests() { mockRepository = new MockRepository(MockBehavior.Default); mockNavigationService = mockRepository.Create(); mockLoggerService = mockRepository.Create(); + mockLogFileService = mockRepository.Create(); + mockLogPathService = mockRepository.Create(); + + mockUserDialogs = mockRepository.Create(); + UserDialogs.Instance = mockUserDialogs.Object; + mockEssentialsService = mockRepository.Create(); } @@ -34,6 +45,8 @@ private InqueryPageViewModel CreateViewModel() var vm = new InqueryPageViewModel( mockNavigationService.Object, mockLoggerService.Object, + mockLogFileService.Object, + mockLogPathService.Object, mockEssentialsService.Object ); return vm; @@ -66,12 +79,66 @@ public void OnClickQuestionCommandTests() [Fact] public void OnClickSendLogCommandTests() { + mockLogFileService.Setup(x => x.CreateZipFile(It.IsAny())) + .Returns("dummyFile"); + + var unitUnderTest = CreateViewModel(); + unitUnderTest.Initialize(new NavigationParameters()); + + unitUnderTest.OnClickSendLogCommand.Execute(null); + + mockNavigationService.Verify(x => x.NavigateAsync("SendLogConfirmationPage", It.IsAny()), Times.Once()); + } + + [Fact] + public void OnClickSendLogCommand_CreateZipSuccess() + { + var testLogId = "test-log-id"; + mockLogFileService.Setup(x => x.CreateLogId()).Returns(testLogId); + + var testZipFileName = "test-zip-file-name"; + mockLogFileService.Setup(x => x.CreateZipFileName(testLogId)).Returns(testZipFileName); + var testPublicZipFileName = "test-public-zip-file-name"; + mockLogFileService.Setup(x => x.CreateZipFile(testZipFileName)).Returns(testPublicZipFileName); + var unitUnderTest = CreateViewModel(); unitUnderTest.Initialize(new NavigationParameters()); unitUnderTest.OnClickSendLogCommand.Execute(null); - mockNavigationService.Verify(x => x.NavigateAsync("SendLogConfirmationPage"), Times.Once()); + mockLogFileService.Verify(x => x.CreateLogId(), Times.Once()); + mockLogFileService.Verify(x => x.CreateZipFileName(testLogId), Times.Once()); + mockLogFileService.Verify(x => x.CreateZipFile(testZipFileName), Times.Once()); + + mockUserDialogs.Verify(x => x.ShowLoading(It.IsAny(), null), Times.Once()); + mockUserDialogs.Verify(x => x.HideLoading(), Times.Once()); + mockUserDialogs.Verify(x => x.AlertAsync(It.IsAny(), It.IsAny(), It.IsAny(), null), Times.Never()); + + mockNavigationService.Verify(x => x.NavigateAsync(It.IsAny()), Times.Never()); + } + + [Fact] + public void OnClickSendLogCommand_CreateZipFailure() + { + var testLogId = "test-log-id"; + mockLogFileService.Setup(x => x.CreateLogId()).Returns(testLogId); + + var testZipFileName = "test-zip-file-name"; + mockLogFileService.Setup(x => x.CreateZipFileName(testLogId)).Returns(testZipFileName); + mockLogFileService.Setup(x => x.CreateZipFile(testZipFileName)).Returns(null); + + var unitUnderTest = CreateViewModel(); + unitUnderTest.Initialize(new NavigationParameters()); + + unitUnderTest.OnClickSendLogCommand.Execute(null); + + mockLogFileService.Verify(x => x.CreateLogId(), Times.Once()); + mockLogFileService.Verify(x => x.CreateZipFileName(testLogId), Times.Once()); + mockLogFileService.Verify(x => x.CreateZipFile(testZipFileName), Times.Once()); + + mockUserDialogs.Verify(x => x.ShowLoading(It.IsAny(), null), Times.Once()); + mockUserDialogs.Verify(x => x.HideLoading(), Times.Once()); + mockUserDialogs.Verify(x => x.AlertAsync(It.IsAny(), It.IsAny(), "OK", null), Times.Once()); } [Fact] diff --git a/Covid19Radar/Tests/Covid19Radar.UnitTests/ViewModels/HelpPage/SendLogConfirmationPageViewModelTests.cs b/Covid19Radar/Tests/Covid19Radar.UnitTests/ViewModels/HelpPage/SendLogConfirmationPageViewModelTests.cs index 6a6678669..c4ed0e1fe 100644 --- a/Covid19Radar/Tests/Covid19Radar.UnitTests/ViewModels/HelpPage/SendLogConfirmationPageViewModelTests.cs +++ b/Covid19Radar/Tests/Covid19Radar.UnitTests/ViewModels/HelpPage/SendLogConfirmationPageViewModelTests.cs @@ -3,13 +3,13 @@ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ using System; -using System.IO; using System.Threading.Tasks; using Acr.UserDialogs; using Covid19Radar.Model; using Covid19Radar.Services; using Covid19Radar.Services.Logs; using Covid19Radar.ViewModels; +using Covid19Radar.Views; using Moq; using Prism.Navigation; using Xamarin.Forms; @@ -25,7 +25,6 @@ public class SendLogConfirmationPageViewModelTests private readonly Mock mockLogFileService; private readonly Mock mockLoggerService; private readonly Mock mockLogUploadService; - private readonly Mock mockLogPathService; private readonly Mock mockHttpDataService; public SendLogConfirmationPageViewModelTests() @@ -39,7 +38,6 @@ public SendLogConfirmationPageViewModelTests() mockLogFileService = mockRepository.Create(); mockLoggerService = mockRepository.Create(); mockLogUploadService = mockRepository.Create(); - mockLogPathService = mockRepository.Create(); mockHttpDataService = mockRepository.Create(); } @@ -47,10 +45,9 @@ private SendLogConfirmationPageViewModel CreateViewModel() { var vm = new SendLogConfirmationPageViewModel( mockNavigationService.Object, - mockLogFileService.Object, mockLoggerService.Object, + mockLogFileService.Object, mockLogUploadService.Object, - mockLogPathService.Object, mockHttpDataService.Object) { BeginInvokeOnMainThread = new Action((a) => { a.Invoke(); }), @@ -59,54 +56,6 @@ private SendLogConfirmationPageViewModel CreateViewModel() return vm; } - [Fact] - public void InitializeTests_CreateZipSuccess() - { - var testLogId = "test-log-id"; - mockLogFileService.Setup(x => x.CreateLogId()).Returns(testLogId); - - var testZipFileName = "test-zip-file-name"; - mockLogFileService.Setup(x => x.LogUploadingFileName(testLogId)).Returns(testZipFileName); - mockLogFileService.Setup(x => x.CreateLogUploadingFileToTmpPath(testZipFileName)).Returns(true); - - var unitUnderTest = CreateViewModel(); - unitUnderTest.Initialize(new NavigationParameters()); - - mockLogFileService.Verify(x => x.CreateLogId(), Times.Once()); - mockLogFileService.Verify(x => x.LogUploadingFileName(testLogId), Times.Once()); - mockLogFileService.Verify(x => x.CreateLogUploadingFileToTmpPath(testZipFileName), Times.Once()); - - mockUserDialogs.Verify(x => x.ShowLoading(It.IsAny(), null), Times.Once()); - mockUserDialogs.Verify(x => x.HideLoading(), Times.Once()); - mockUserDialogs.Verify(x => x.AlertAsync(It.IsAny(), It.IsAny(), It.IsAny(), null), Times.Never()); - - mockNavigationService.Verify(x => x.NavigateAsync(It.IsAny()), Times.Never()); - } - - [Fact] - public void InitializeTests_CreateZipFailure() - { - var testLogId = "test-log-id"; - mockLogFileService.Setup(x => x.CreateLogId()).Returns(testLogId); - - var testZipFileName = "test-zip-file-name"; - mockLogFileService.Setup(x => x.LogUploadingFileName(testLogId)).Returns(testZipFileName); - mockLogFileService.Setup(x => x.CreateLogUploadingFileToTmpPath(testZipFileName)).Returns(false); - - var unitUnderTest = CreateViewModel(); - unitUnderTest.Initialize(new NavigationParameters()); - - mockLogFileService.Verify(x => x.CreateLogId(), Times.Once()); - mockLogFileService.Verify(x => x.LogUploadingFileName(testLogId), Times.Once()); - mockLogFileService.Verify(x => x.CreateLogUploadingFileToTmpPath(testZipFileName), Times.Once()); - - mockUserDialogs.Verify(x => x.ShowLoading(It.IsAny(), null), Times.Once()); - mockUserDialogs.Verify(x => x.HideLoading(), Times.Once()); - mockUserDialogs.Verify(x => x.AlertAsync(It.IsAny(), It.IsAny(), "OK", null), Times.Once()); - - mockNavigationService.Verify(x => x.GoBackAsync(), Times.Once()); - } - [Fact] public void OnClickConfirmLogCommandTests_Success() { @@ -114,14 +63,14 @@ public void OnClickConfirmLogCommandTests_Success() mockLogFileService.Setup(x => x.CreateLogId()).Returns(testLogId); var testZipFileName = "test-zip-file-name"; - mockLogFileService.Setup(x => x.LogUploadingFileName(testLogId)).Returns(testZipFileName); - mockLogFileService.Setup(x => x.CreateLogUploadingFileToTmpPath(testZipFileName)).Returns(true); - mockLogFileService.Setup(x => x.CopyLogUploadingFileToPublicPath(testZipFileName)).Returns(true); - - mockLogPathService.Setup(x => x.LogUploadingPublicPath).Returns("dummy_log_uploading_public_path"); + mockLogFileService.Setup(x => x.CreateZipFileName(testLogId)).Returns(testZipFileName); + var testPublicZipFileName = "test-public-zip-file-name"; + mockLogFileService.Setup(x => x.CreateZipFile(testZipFileName)).Returns(testPublicZipFileName); + var testPublicZipFilePath = "test-public-zip-file-path"; + mockLogFileService.Setup(x => x.CopyLogUploadingFileToPublicPath(testZipFileName)).Returns(testPublicZipFilePath); var unitUnderTest = CreateViewModel(); - unitUnderTest.Initialize(new NavigationParameters()); + unitUnderTest.Initialize(SendLogConfirmationPage.BuildNavigationParams(testLogId, testPublicZipFilePath)); mockUserDialogs.Invocations.Clear(); mockLogFileService.Invocations.Clear(); @@ -137,9 +86,11 @@ public void OnClickConfirmLogCommandTests_Failure() mockLogFileService.Setup(x => x.CreateLogId()).Returns(testLogId); var testZipFileName = "test-zip-file-name"; - mockLogFileService.Setup(x => x.LogUploadingFileName(testLogId)).Returns(testZipFileName); - mockLogFileService.Setup(x => x.CreateLogUploadingFileToTmpPath(testZipFileName)).Returns(true); - mockLogFileService.Setup(x => x.CopyLogUploadingFileToPublicPath(testZipFileName)).Returns(false); + mockLogFileService.Setup(x => x.CreateZipFileName(testLogId)).Returns(testZipFileName); + var testPublicZipFileName = "test-public-zip-file-name"; + mockLogFileService.Setup(x => x.CreateZipFile(testZipFileName)).Returns(testPublicZipFileName); + var testPublicZipFilePath = "test-public-zip-file-path"; + mockLogFileService.Setup(x => x.CopyLogUploadingFileToPublicPath(testZipFileName)).Returns(testPublicZipFilePath); var unitUnderTest = CreateViewModel(); unitUnderTest.Initialize(new NavigationParameters()); @@ -159,27 +110,29 @@ public void OnClickSendLogCommandTests_Success() mockLogFileService.Setup(x => x.CreateLogId()).Returns(testLogId); var testZipFileName = "test-zip-file-name"; - mockLogFileService.Setup(x => x.LogUploadingFileName(testLogId)).Returns(testZipFileName); - mockLogFileService.Setup(x => x.CreateLogUploadingFileToTmpPath(testZipFileName)).Returns(true); - mockLogFileService.Setup(x => x.CopyLogUploadingFileToPublicPath(testZipFileName)).Returns(true); + mockLogFileService.Setup(x => x.CreateZipFileName(testLogId)).Returns(testZipFileName); + var testPublicZipFileName = "test-public-zip-file-name"; + mockLogFileService.Setup(x => x.CreateZipFile(testZipFileName)).Returns(testPublicZipFileName); + var testPublicZipFilePath = "test-public-zip-file-path"; + mockLogFileService.Setup(x => x.CopyLogUploadingFileToPublicPath(testZipFileName)).Returns(testPublicZipFilePath); mockLogFileService.Setup(x => x.DeleteAllLogUploadingFiles()).Returns(true); var unitUnderTest = CreateViewModel(); - unitUnderTest.Initialize(new NavigationParameters()); - + unitUnderTest.Initialize(SendLogConfirmationPage.BuildNavigationParams(testLogId, testPublicZipFilePath)); + mockUserDialogs.Invocations.Clear(); mockLogFileService.Invocations.Clear(); var testResponse = new ApiResponse(200, new LogStorageSas() { SasToken = "test-sas-token" }); mockHttpDataService.Setup(x => x.GetLogStorageSas()).ReturnsAsync(testResponse); - mockLogUploadService.Setup(x => x.UploadAsync(testZipFileName, testResponse.Result.SasToken)).ReturnsAsync(true); + mockLogUploadService.Setup(x => x.UploadAsync(testPublicZipFilePath, testResponse.Result.SasToken)).ReturnsAsync(true); unitUnderTest.OnClickSendLogCommand.Execute(null); mockUserDialogs.Verify(x => x.ShowLoading(It.IsAny(), null), Times.Once()); mockUserDialogs.Verify(x => x.HideLoading(), Times.Once()); mockUserDialogs.Verify(x => x.AlertAsync(It.IsAny(), It.IsAny(), It.IsAny(), null), Times.Never()); - mockLogUploadService.Verify(x => x.UploadAsync(testZipFileName, testResponse.Result.SasToken), Times.Once()); + mockLogUploadService.Verify(x => x.UploadAsync(testPublicZipFilePath, testResponse.Result.SasToken), Times.Once()); mockLogFileService.Verify(x => x.DeleteAllLogUploadingFiles(), Times.Once()); var expectedParameters = new NavigationParameters { { "logId", testLogId } }; mockNavigationService.Verify(x => x.NavigateAsync("SendLogCompletePage?useModalNavigation=true/", expectedParameters), Times.Once()); @@ -192,26 +145,28 @@ public void OnClickSendLogCommandTests_Failure() mockLogFileService.Setup(x => x.CreateLogId()).Returns(testLogId); var testZipFileName = "test-zip-file-name"; - mockLogFileService.Setup(x => x.LogUploadingFileName(testLogId)).Returns(testZipFileName); - mockLogFileService.Setup(x => x.CreateLogUploadingFileToTmpPath(testZipFileName)).Returns(true); - mockLogFileService.Setup(x => x.CopyLogUploadingFileToPublicPath(testZipFileName)).Returns(true); + mockLogFileService.Setup(x => x.CreateZipFileName(testLogId)).Returns(testZipFileName); + var testPublicZipFileName = "test-public-zip-file-name"; + mockLogFileService.Setup(x => x.CreateZipFile(testZipFileName)).Returns(testPublicZipFileName); + var testPublicZipFilePath = "test-public-zip-file-path"; + mockLogFileService.Setup(x => x.CopyLogUploadingFileToPublicPath(testZipFileName)).Returns(testPublicZipFilePath); var unitUnderTest = CreateViewModel(); - unitUnderTest.Initialize(new NavigationParameters()); + unitUnderTest.Initialize(SendLogConfirmationPage.BuildNavigationParams(testLogId, testPublicZipFilePath)); mockUserDialogs.Invocations.Clear(); mockLogFileService.Invocations.Clear(); var testResponse = new ApiResponse(200, new LogStorageSas() { SasToken = "test-sas-token" }); mockHttpDataService.Setup(x => x.GetLogStorageSas()).ReturnsAsync(testResponse); - mockLogUploadService.Setup(x => x.UploadAsync(testZipFileName, testResponse.Result.SasToken)).ReturnsAsync(false); + mockLogUploadService.Setup(x => x.UploadAsync(testPublicZipFilePath, testResponse.Result.SasToken)).ReturnsAsync(false); unitUnderTest.OnClickSendLogCommand.Execute(null); mockUserDialogs.Verify(x => x.ShowLoading(It.IsAny(), null), Times.Once()); mockUserDialogs.Verify(x => x.HideLoading(), Times.Once()); mockUserDialogs.Verify(x => x.AlertAsync(It.IsAny(), It.IsAny(), "OK", null), Times.Once()); - mockLogUploadService.Verify(x => x.UploadAsync(testZipFileName, testResponse.Result.SasToken), Times.Once()); + mockLogUploadService.Verify(x => x.UploadAsync(testPublicZipFilePath, testResponse.Result.SasToken), Times.Once()); mockLogFileService.Verify(x => x.DeleteAllLogUploadingFiles(), Times.Never()); mockNavigationService.Verify(x => x.NavigateAsync(It.IsAny(), It.IsAny()), Times.Never()); } @@ -223,27 +178,31 @@ public void OnClickSendLogCommandTests_DeleteLogFalure() mockLogFileService.Setup(x => x.CreateLogId()).Returns(testLogId); var testZipFileName = "test-zip-file-name"; - mockLogFileService.Setup(x => x.LogUploadingFileName(testLogId)).Returns(testZipFileName); - mockLogFileService.Setup(x => x.CreateLogUploadingFileToTmpPath(testZipFileName)).Returns(true); - mockLogFileService.Setup(x => x.CopyLogUploadingFileToPublicPath(testZipFileName)).Returns(true); + mockLogFileService.Setup(x => x.CreateZipFileName(testLogId)).Returns(testZipFileName); + var testPublicZipFileName = "test-public-zip-file-name"; + mockLogFileService.Setup(x => x.CreateZipFile(testZipFileName)).Returns(testPublicZipFileName); + var testPublicZipFilePath = "test-public-zip-file-path"; + mockLogFileService.Setup(x => x.CopyLogUploadingFileToPublicPath(testZipFileName)).Returns(testPublicZipFilePath); mockLogFileService.Setup(x => x.DeleteAllLogUploadingFiles()).Returns(false); var unitUnderTest = CreateViewModel(); - unitUnderTest.Initialize(new NavigationParameters()); + unitUnderTest.Initialize( + SendLogConfirmationPage.BuildNavigationParams(testLogId, testPublicZipFilePath) + ); mockUserDialogs.Invocations.Clear(); mockLogFileService.Invocations.Clear(); var testResponse = new ApiResponse(200, new LogStorageSas() { SasToken = "test-sas-token" }); mockHttpDataService.Setup(x => x.GetLogStorageSas()).ReturnsAsync(testResponse); - mockLogUploadService.Setup(x => x.UploadAsync(testZipFileName, testResponse.Result.SasToken)).ReturnsAsync(true); + mockLogUploadService.Setup(x => x.UploadAsync(testPublicZipFilePath, testResponse.Result.SasToken)).ReturnsAsync(true); unitUnderTest.OnClickSendLogCommand.Execute(null); mockUserDialogs.Verify(x => x.ShowLoading(It.IsAny(), null), Times.Once()); mockUserDialogs.Verify(x => x.HideLoading(), Times.Once()); mockUserDialogs.Verify(x => x.AlertAsync(It.IsAny(), It.IsAny(), It.IsAny(), null), Times.Never()); - mockLogUploadService.Verify(x => x.UploadAsync(testZipFileName, testResponse.Result.SasToken), Times.Once()); + mockLogUploadService.Verify(x => x.UploadAsync(testPublicZipFilePath, testResponse.Result.SasToken), Times.Once()); mockLogFileService.Verify(x => x.DeleteAllLogUploadingFiles(), Times.Once()); var expectedParameters = new NavigationParameters { { "logId", testLogId } }; mockNavigationService.Verify(x => x.NavigateAsync("SendLogCompletePage?useModalNavigation=true/", expectedParameters), Times.Once());