public async Task UploadDocument_Verfiy_CallAddDocumentAsync() { // Arrange var fakeStream = new MemoryStream(); var uploadDocumentInputModel = new UploadDocumentInputModel { UploadedBy = 1, DocumentName = "test", DocumentStream = fakeStream, DocumentContentType = "application/pdf" }; // Act await _documentsFileService.UploadDocument(uploadDocumentInputModel); // Assert var document = new Document() { OriginalDocumentName = "test", DocumentName = "test", DocumentTypeId = 1, DocumentPath = "Documents", DocumentSize = 0, UploadedBy = 1, UploadedDate = new DateTime(2018, 05, 01, 1, 1, 1) }; _documentsRepositoryMock.Verify(service => service.AddDocumentAsync(It.IsAny <Document>())); }
public async Task <IActionResult> OnPostUploadDocumentAsync( [Bind("DocumentType", "DocumentTypes", "FileToUpload", "StartConnection", "EndConnection", "Date", "Id", "RouteName", "AreaName")] UploadDocumentInputModel model) { if (!this.ModelState.IsValid) { return(this.PartialView(UploadDocumentPartialView, model)); } string path = await FileHelpers.ProcessFormFile(model.FileToUpload, this.ModelState, this.permittedExtensions, this.fileSizeLimit, this.environment.WebRootPath, model.AreaName, false); if (!this.ModelState.IsValid) { return(this.PartialView(UploadDocumentPartialView, model)); } var dto = AutoMapperConfig.MapperInstance.Map <UploadOnSuccessDto>(model); await this.service.UploadDocument(model); using (var stream = new FileStream(path, FileMode.Create)) { await model.FileToUpload.CopyToAsync(stream); stream.Close(); } return(this.Json(new { success = true, dto = dto })); }
public async Task UploadDocument_Should_Return_NotNullModel() { // Arrange var fakeStream = new MemoryStream(); var uploadDocumentInputModel = new UploadDocumentInputModel { UploadedBy = 1, DocumentName = "test", DocumentStream = fakeStream, DocumentContentType = "application/pdf" }; var expected = new UploadDocumentReturnModel { DocumentId = 0, OriginalDocumentName = "test" }; // Act var actual = await _documentsFileService.UploadDocument(uploadDocumentInputModel); // Assert actual.Should().NotBeNull(); actual.Should().Equals(expected); }
private async Task <DocumentType> GetDocumentType(UploadDocumentInputModel uploadDocumentInputModel) { var documentType = await _documentsTypesRepository.GetDocumentTypeAsync(uploadDocumentInputModel.DocumentContentType).ConfigureAwait(false); if (documentType == null) { throw new ServiceException(ErrorCodes.FileNotSuported, "this document type is not supported"); } return(documentType); }
// ________________________________________________________ // // Documents public async Task UploadDocument(UploadDocumentInputModel model) { UploadDocumentDto dto = AutoMapperConfig.MapperInstance.Map <UploadDocumentDto>(model); dto.EndConnection = DateTimeExtensions.ToSqlFormat(model.EndConnection); dto.DocumentType = await this.service.ByIdDocumentType(model.DocumentType); Validator.ArgumentNullExceptionInt(dto.DocumentType, ErrorMessages.InvalidDocType); string query = StringSwapper.ByArea(model.AreaName, SqlProcedureDictionary.DocumentFund, SqlProcedureDictionary.DocumentSubFund, SqlProcedureDictionary.DocumentShareClass); query += " @file_name, @entity_id, @start_connection, @end_connection, @file_ext, @filetype_id"; SqlCommand command = new SqlCommand(query); command.Parameters.AddRange(new[] { new SqlParameter("@file_name", SqlDbType.NVarChar) { Value = dto.FileName }, new SqlParameter("@entity_id", SqlDbType.Int) { Value = dto.Id }, new SqlParameter("@file_ext", SqlDbType.NVarChar) { Value = dto.FileExt }, new SqlParameter("@start_connection", SqlDbType.NVarChar) { Value = dto.StartConnection }, new SqlParameter("@end_connection", SqlDbType.NVarChar) { Value = dto.EndConnection }, new SqlParameter("@filetype_id", SqlDbType.Int) { Value = dto.DocumentType }, }); await this.sqlManager.ExecuteProcedure(command); }
public async Task UploadDocument_Verfiy_CallSaveChanges() { // Arrange var fakeStream = new MemoryStream(); var uploadDocumentInputModel = new UploadDocumentInputModel { UploadedBy = 1, DocumentName = "test", DocumentStream = fakeStream, DocumentContentType = "application/pdf" }; // Act await _documentsFileService.UploadDocument(uploadDocumentInputModel); // Assert _unitOfWorkMock.Verify(service => service.SaveChangesAsync()); }
public static UploadDocumentInputModel GenerateDocument(int fundId, DateTime startConnection, string docType = "Prospectus") { var file = new FormFile(new MemoryStream(Encoding.UTF8.GetBytes("This is a test document")), 0, 0, "Data", "test.pdf") { Headers = new HeaderDictionary(), ContentType = "text/plain" }; UploadDocumentInputModel model = new UploadDocumentInputModel() { Id = fundId, StartConnection = startConnection, DocumentType = docType, FileToUpload = file, AreaName = "Fund", }; return(model); }
public async Task <IActionResult> UploadDocument(IFormFile file) { using (var stream = new MemoryStream()) { await file.CopyToAsync(stream).ConfigureAwait(false); var uploadDocumentInputModel = new UploadDocumentInputModel { DocumentName = file.FileName, DocumentContentType = file.ContentType, DocumentStream = stream, UploadedBy = UserId }; var uploadedDocument = await _documentsFileService.UploadDocument(uploadDocumentInputModel).ConfigureAwait(false); return(Ok(uploadedDocument)); } }
public async Task <UploadDocumentReturnModel> UploadDocument(UploadDocumentInputModel uploadDocumentInputModel) { using (var transaction = await _unitOfWork.BeginTransactionAsync().ConfigureAwait(false)) { try { DocumentType documentType = await GetDocumentType(uploadDocumentInputModel).ConfigureAwait(false); var uploadFileName = _fileService.UploadFile(uploadDocumentInputModel.DocumentStream, uploadDocumentInputModel.DocumentName); var document = new Document() { OriginalDocumentName = uploadDocumentInputModel.DocumentName, DocumentName = uploadFileName, DocumentTypeId = documentType.TypeId, DocumentPath = _fileService.GetUploadsPath(), DocumentSize = uploadDocumentInputModel.DocumentStream.Length, UploadedBy = uploadDocumentInputModel.UploadedBy, UploadedDate = _dateService.UtcNow }; await _documentsRepository.AddDocumentAsync(document).ConfigureAwait(false); await _unitOfWork.SaveChangesAsync().ConfigureAwait(false); var returnModel = new UploadDocumentReturnModel { DocumentId = document.DocumentId, OriginalDocumentName = document.OriginalDocumentName, }; transaction.Commit(); return(returnModel); } catch (Exception exception) { transaction.Rollback(); throw new ServiceException(ErrorCodes.UploadDocumentException, "Something went wrong while uploading a document", exception); } } }
public async Task UploadDocument_Should_Throw_Exception() { // Arrange var fakeStream = new MemoryStream(); var uploadDocumentInputModel = new UploadDocumentInputModel { UploadedBy = 1, DocumentName = "test", DocumentStream = fakeStream, DocumentContentType = "application/pdf" }; _documentsTypesRepositoryMock.Setup(service => service.GetDocumentTypeAsync("application/pdf")) .ReturnsAsync(() => null); // Act var exception = await Assert.ThrowsAsync <ServiceException>(() => _documentsFileService.UploadDocument(uploadDocumentInputModel)); // Assert exception.ErrorCode.Should().Equals(ErrorCodes.UploadDocumentException); }