internal async Task RunFileBrowserValidateTask(FileBrowserValidateParams fileBrowserParams, RequestContext <bool> requestContext) { FileBrowserValidatedParams result = new FileBrowserValidatedParams(); try { ValidatePathsCallback callback; ConnectionInfo connInfo; this.ConnectionServiceInstance.TryFindConnection(fileBrowserParams.OwnerUri, out connInfo); if (validatePathsCallbackMap.TryGetValue(fileBrowserParams.ServiceType, out callback) && callback != null && connInfo != null && fileBrowserParams.SelectedFiles != null && fileBrowserParams.SelectedFiles.Length > 0) { QueueItem queueItem = fileBrowserQueue.QueueBindingOperation( key: fileBrowserQueue.AddConnectionContext(connInfo, this.serviceName), bindingTimeout: DefaultTimeout, waitForLockTimeout: DefaultTimeout, bindOperation: (bindingContext, cancelToken) => { string errorMessage; result.Succeeded = callback(new FileBrowserValidateEventArgs { ServiceType = fileBrowserParams.ServiceType, OwnerUri = fileBrowserParams.OwnerUri, FilePaths = fileBrowserParams.SelectedFiles }, out errorMessage); result.Message = errorMessage; return(result); }); queueItem.ItemProcessed.WaitOne(); if (queueItem.GetResultAsT <FileBrowserValidatedParams>() != null) { result = queueItem.GetResultAsT <FileBrowserValidatedParams>(); } } else { result.Succeeded = true; } } catch (Exception ex) { result.Message = ex.Message; } await requestContext.SendEvent(FileBrowserValidatedNotification.Type, result); }
internal async Task HandleFileBrowserValidateRequest(FileBrowserValidateParams fileBrowserParams, RequestContext <bool> requestContext) { try { var task = Task.Run(() => RunFileBrowserValidateTask(fileBrowserParams, requestContext)) .ContinueWithOnFaulted(null); await requestContext.SendResult(true); } catch (Exception ex) { Logger.Write(TraceEventType.Error, "Unexpected exception while handling file browser validate request: " + ex.Message); await requestContext.SendResult(false); } }
public async void HandleFileBrowserValidateRequestTest() { var liveConnection = LiveConnectionHelper.InitLiveConnectionInfo(); FileBrowserService service = new FileBrowserService(); var requestContext = new Mock <RequestContext <bool> >(); requestContext.Setup(x => x.SendResult(It.IsAny <bool>())).Returns(Task.FromResult(new object())); var inputParams = new FileBrowserValidateParams { OwnerUri = liveConnection.ConnectionInfo.OwnerUri, ServiceType = "" }; await service.HandleFileBrowserValidateRequest(inputParams, requestContext.Object); requestContext.Verify(x => x.SendResult(It.Is <bool>(p => p == true))); }
public async void ValidateSelectedFilesWithNullValidatorTest() { var liveConnection = LiveConnectionHelper.InitLiveConnectionInfo(); FileBrowserService service = new FileBrowserService(); var validateParams = new FileBrowserValidateParams { // Do not pass any service so that the file validator will be null ServiceType = "", OwnerUri = liveConnection.ConnectionInfo.OwnerUri, SelectedFiles = new[] { "" } }; var efv = new EventFlowValidator <bool>() .AddEventValidation(FileBrowserValidatedNotification.Type, eventParams => Assert.True(eventParams.Succeeded)) .Complete(); // Validate files with null file validator await service.RunFileBrowserValidateTask(validateParams, efv.Object); efv.Validate(); }
public async void InvalidFileValidationTest() { FileBrowserService service = new FileBrowserService(); service.RegisterValidatePathsCallback("TestService", ValidatePaths); var validateParams = new FileBrowserValidateParams { // Do not pass any service so that the file validator will be null ServiceType = "TestService", SelectedFiles = new[] { "" } }; var efv = new EventFlowValidator <bool>() .AddEventValidation(FileBrowserValidatedNotification.Type, eventParams => Assert.False(eventParams.Succeeded)) .Complete(); // Validate files with null file validator await service.RunFileBrowserValidateTask(validateParams, efv.Object); // Verify complete notification event was fired and the result efv.Validate(); }
//[Fact] public async void BackupFileBrowserTest() { string databaseName = "testfilebrowser_" + new Random().Next(10000000, 99999999); SqlTestDb testDb = SqlTestDb.CreateNew(TestServerType.OnPrem, false, databaseName); // Initialize backup service var liveConnection = LiveConnectionHelper.InitLiveConnectionInfo(databaseName); DatabaseTaskHelper helper = AdminService.CreateDatabaseTaskHelper(liveConnection.ConnectionInfo, databaseExists: true); SqlConnection sqlConn = ConnectionService.OpenSqlConnection(liveConnection.ConnectionInfo); DisasterRecoveryService disasterRecoveryService = new DisasterRecoveryService(); BackupConfigInfo backupConfigInfo = disasterRecoveryService.GetBackupConfigInfo(helper.DataContainer, sqlConn, sqlConn.Database); // Create backup file string backupPath = Path.Combine(backupConfigInfo.DefaultBackupFolder, databaseName + ".bak"); string query = $"BACKUP DATABASE [{databaseName}] TO DISK = N'{backupPath}' WITH NOFORMAT, NOINIT, NAME = N'{databaseName}-Full Database Backup', SKIP, NOREWIND, NOUNLOAD, STATS = 10"; await TestServiceProvider.Instance.RunQueryAsync(TestServerType.OnPrem, "master", query); FileBrowserService service = new FileBrowserService(); string[] backupFilters = new string[2] { "*.bak", "*.trn" }; var openParams = new FileBrowserOpenParams { OwnerUri = liveConnection.ConnectionInfo.OwnerUri, ExpandPath = backupConfigInfo.DefaultBackupFolder, FileFilters = backupFilters }; var openBrowserEventFlowValidator = new EventFlowValidator <bool>() .AddEventValidation(FileBrowserOpenedNotification.Type, eventParams => { Assert.True(eventParams.Succeeded); Assert.NotNull(eventParams.FileTree); Assert.NotNull(eventParams.FileTree.RootNode); Assert.NotNull(eventParams.FileTree.RootNode.Children); Assert.True(eventParams.FileTree.RootNode.Children.Count > 0); Assert.True(ContainsFileInTheFolder(eventParams.FileTree.SelectedNode, backupPath)); }) .Complete(); await service.RunFileBrowserOpenTask(openParams, openBrowserEventFlowValidator.Object); // Verify complete notification event was fired and the result openBrowserEventFlowValidator.Validate(); var expandParams = new FileBrowserExpandParams { OwnerUri = liveConnection.ConnectionInfo.OwnerUri, ExpandPath = backupConfigInfo.DefaultBackupFolder }; var expandEventFlowValidator = new EventFlowValidator <bool>() .AddEventValidation(FileBrowserExpandedNotification.Type, eventParams => { Assert.True(eventParams.Succeeded); Assert.NotNull(eventParams.Children); Assert.True(eventParams.Children.Length > 0); }) .Complete(); // Expand the node in file browser await service.RunFileBrowserExpandTask(expandParams, expandEventFlowValidator.Object); // Verify result expandEventFlowValidator.Validate(); var validateParams = new FileBrowserValidateParams { OwnerUri = liveConnection.ConnectionInfo.OwnerUri, ServiceType = FileValidationServiceConstants.Backup, SelectedFiles = new[] { backupPath } }; var validateEventFlowValidator = new EventFlowValidator <bool>() .AddEventValidation(FileBrowserValidatedNotification.Type, eventParams => Assert.True(eventParams.Succeeded)) .Complete(); // Validate selected files in the browser await service.RunFileBrowserValidateTask(validateParams, validateEventFlowValidator.Object); // Verify complete notification event was fired and the result validateEventFlowValidator.Validate(); // Remove the backup file if (File.Exists(backupPath)) { File.Delete(backupPath); } }