// [TestMethod] public void ChangeFile_RunAddUserAndAddFileAndChangeFile_AndResultChangedFile() { string login = "******"; string name = "Name"; string surName = "SurName"; string password = "******"; AddUserOperations addUserOperations = AddUserOperations.CreateUserOperations(login, password, name, surName); // HiveDatabaseService databaseService = new HiveDatabaseService(connectionString); sut.DoUserOperations(addUserOperations).Wait();; User user = sut.GetUser(addUserOperations.KeyUser).Result; string fileName = "filename"; string extension = "extension"; AddFileOperations addFileOperations = AddFileOperations.CreateFileOperations(user, fileName, extension).OnError(a => throw new Exception(a)).Value;; sut.DoFileOperations(addFileOperations).Wait();; File file = sut.GetFile(addFileOperations.KeyUserFile).Result; string newFileName = "filename2"; string newExtension = "extension2"; ChangeFileOperations changeFileOperations = ChangeFileOperations.CreateFileOperations(file) .OnError(a => throw new Exception(a)).Value .ChangeFileName(newFileName) .OnError(a => throw new Exception(a)).Value .ChangeExtension(newExtension).OnError(a => throw new Exception(a)).Value;; //Act sut.DoFileOperations(changeFileOperations).Wait();; // Assert File fileRead = sut.GetFile(changeFileOperations.KeyUserFile).Result; Assert.AreEqual(newFileName, fileRead.FileName); Assert.AreEqual(newExtension, fileRead.Extension); Assert.AreEqual(changeFileOperations.KeyUser, fileRead.KeyUser); Assert.AreEqual(changeFileOperations.KeyUserFile, fileRead.Key); Assert.AreEqual(changeFileOperations.Key, fileRead.LongKey); Assert.AreEqual(changeFileOperations.OperationDate.ToString("yyyy-MM-dd HH:mm:ss"), file.LastOperationDate.ToString("yyyy-MM-dd HH:mm:ss")); Assert.IsFalse(fileRead.IsDeleted); Assert.IsFalse(fileRead.IsCloned); }
public async Task <ResultService <File> > ChangeFileToUser(IFileServiceLogger fileServiceLogger, string userKey, string userFileKey, Stream stream, string fileName, string extension) { fileServiceLogger.LogInfo($"Dewey.Dms.FileService.Services.ChangeFileToUser(userKey={userKey}, userFileKey={userFileKey}, fileName={fileName},extension={extension})"); try { User user = await _databaseService.GetUser(userKey); if (user == null) { fileServiceLogger.LogInfo( $"Dewey.Dms.FileService.Services.ChangeFileToUser(userKey={userKey}, userFileKey={userFileKey},fileName={fileName},extension={extension}) - no such user"); return(ResultService <File> .Error($"No such user {userKey}")); } File file = await _databaseService.GetFile(userFileKey); if (file == null) { fileServiceLogger.LogDebug( $"Dewey.Dms.FileService.Services.ChangeFileToUser(userKey={userKey}, userFileKey={userFileKey},fileName={fileName},extension={extension}): No such file"); return(ResultService <File> .Error("No such file")); } if (file.KeyUser != userKey) { fileServiceLogger.LogDebug( $"Dewey.Dms.FileService.Services.ChangeFileToUser(userKey={userKey}, userFileKey={userFileKey},fileName={fileName},extension={extension}): Permission denied to file"); return(ResultService <File> .Error("No such file")); } ResultService <ChangeFileOperations> resultChangeFileOperations = ChangeFileOperations.CreateFileOperations(file) .NextResult(a => a.ChangeFileName(fileName)) .NextResult(a => a.ChangeExtension(extension)); ChangeFileOperations changeFileOperations = resultChangeFileOperations.Value; if (resultChangeFileOperations.IsError) { fileServiceLogger.LogDebug( $"Dewey.Dms.FileService.Services.ChangeFileToUser(userKey={userKey}, userFileKey={userFileKey},fileName={fileName},extension={extension}): {resultChangeFileOperations.ErrorMessage}"); return(ResultService <File> .Error(resultChangeFileOperations.ErrorMessage)); } bool result = await _hdfsClient.WriteStream(stream, $"/dewey/{changeFileOperations.Key}"); if (!result) { fileServiceLogger.LogInfo( $"Dewey.Dms.FileService.Services.ChangeFileToUser(userKey={userKey}, userFileKey={userFileKey},fileName={fileName},extension={extension}):- problem writing file"); return(ResultService <File> .Error($"Internal server error")); } await _databaseService.DoFileOperations(changeFileOperations); File fileChange = await _databaseService.GetFile(changeFileOperations.KeyUserFile); if (fileChange == null) { fileServiceLogger.LogInfo( $"Dewey.Dms.FileService.Services.ChangeFileToUser(userKey={userKey}, userFileKey={userFileKey},fileName={fileName},extension={extension}):- problem with read updating file"); return(ResultService <File> .Error($"Internal server error")); } return(ResultService <File> .Ok(fileChange)); } catch (Exception ex) { fileServiceLogger.LogError( $"Dewey.Dms.FileService.Services.AddFileToUser(userKey={userKey}, userFileKey={userFileKey},fileName={fileName},extension={extension})", ex); return(ResultService <File> .Error("Internal server error")); } }