public void UploadFilesController_Post_CorrectCall_ReturnsResult() { // Arrange var fileManagementResult = new FileManagementResult(); fileManagementResult.IsStored = true; fileManagementResult.FileName = "test.name"; fileManagementResult.Length = 10; var mockFormFile = new Mock <IFormFile>(); var user = "******"; var password = "******"; _mockFileManagementServices.Setup(x => x.StoreFilesAsync(It.IsAny <IFormFile>())).Returns(Task.FromResult(fileManagementResult)); _mockFileManagementServices.Setup(x => x.GetUnzipPath()).Returns("UnzipPath"); _mockFileManagementServices.Setup(x => x.GetFileSeparator()).Returns("\\"); _mockZipServices.Setup(x => x.UnzipFiles(fileManagementResult, It.IsAny <string>(), It.IsAny <string>())); NodeCollection nodeCollection = new NodeCollection(); nodeCollection.AddEntry("atest/unzip.txt", 0); _mockEncryptionServices.Setup(x => x.EncryptToString("atest")).Returns("atest"); _mockEncryptionServices.Setup(x => x.EncryptToString("unzip.txt")).Returns("unzip.txt"); _mockZipServices.Setup(x => x.GetFileAndFolderStructureAsync(fileManagementResult.FileName)).Returns(nodeCollection); //TODO: I havent had the time to finish all the tests for the exercise and I excuse myself. // I Hope you see this code and realize that "YEAH! this guy can write tests!" // Act var answer = _controller.Post(mockFormFile.Object, user, password); // Assert Assert.NotNull(answer); //as this is a template code, well just test is fine }
/* * method to unzip a zip file */ public void UnzipFiles(FileManagementResult fileManagementResult, string destinationPath, string fileSeparator) { using (ZipArchive zipFile = ZipFile.OpenRead(fileManagementResult.FileName)) { foreach (ZipArchiveEntry entry in zipFile.Entries) { var filePath = $"{destinationPath}{fileSeparator}{FileManagementServices.CorrectFileSeparator(entry.FullName, fileSeparator)}"; var lastIndexOfFileSeparator = filePath.LastIndexOf(fileSeparator); if (lastIndexOfFileSeparator != -1) { var folderPath = filePath.Substring(0, lastIndexOfFileSeparator); Directory.CreateDirectory(folderPath); } entry.ExtractToFile(filePath); } } }
/* * This is the first step: store the zip file sent from the front-end of the control panel * the file will have the current date as differentiator to guarantee filepath uniquenes. */ public async Task <FileManagementResult> StoreFilesAsync(IFormFile file) { string destinyPath = GetFilesPath(); string treatmentDate = DateTime.UtcNow.ToString("yyyyMMdd-Hmmss"); FileManagementResult newFile = new FileManagementResult() { FileName = Path.Combine(destinyPath, $"{file.FileName}_{treatmentDate}.zip"), Length = file.Length }; if (newFile.Length > 0) { using (var stream = new FileStream(newFile.FileName, FileMode.Create)) { await file.CopyToAsync(stream); newFile.IsStored = true; } } return(newFile); }
public async Task <IActionResult> Post(IFormFile formFile, string user, string password) { if (formFile == null || string.IsNullOrEmpty(user) || string.IsNullOrEmpty(password)) { return(View(new PostModel { ErrorMessage = "Please provide a correct user, password and zip file", HasError = true })); } else { //ensure store directory _fileManagementServices.EnsureStoreDirectory(); //delete zip files directory and recreate _fileManagementServices.EnsureUnzipDirectory(); //zip storage FileManagementResult storedFile = await _fileManagementServices.StoreFilesAsync(formFile); //unzip file into zip directory _zipServices.UnzipFiles( storedFile, _fileManagementServices.GetUnzipPath(), _fileManagementServices.GetFileSeparator()); if (storedFile.IsStored) { NodeCollection nodes = _zipServices.GetFileAndFolderStructureAsync(storedFile.FileName); // this will generate the json object with all neccessary fields encrypted JsonNode root = nodes.GenerateJsonObject( _encryptionServices, _fileManagementServices.GetUnzipPath(), _fileManagementServices.GetFileSeparator()); // then generate the json string string jsonString = JsonConvert.SerializeObject(root); // call the DataManagement System API var response = await _dataManagementSystemCallerServices.PostAsync( _fileManagementServiceUrl, jsonString, _encryptionServices.EncryptToString(user), _encryptionServices.EncryptToString(password)); // visualized the sent json string if (!response) { return(View(new PostModel { ErrorMessage = "The Data Management system answer was negative", HasError = true })); } return(View(new PostModel { JsonString = jsonString, AnswerComment = "Json accepted by Data Management Service", HasError = false })); } else { return(View(new PostModel { ErrorMessage = "Error Storing zip file", HasError = true })); } } }