public async Task <DocumentDownloadReturnModel> DownloadDocument(int documentId)
        {
            using (var transaction = await _unitOfWork.BeginTransactionAsync().ConfigureAwait(false))
            {
                try
                {
                    var document = await GetDocument(documentId).ConfigureAwait(false);
                    await UpdateDocumentLastAccesedDate(document).ConfigureAwait(false);

                    var file = await _fileService.GetFile(document.DocumentPath, document.DocumentName).ConfigureAwait(false);

                    var result = new DocumentDownloadReturnModel
                    {
                        DocumentContent     = file.fileContent,
                        DocumentContentType = document.DocumentType.ContentType,
                        DocumentPath        = file.filePath
                    };

                    transaction.Commit();

                    return(result);
                }
                catch (Exception exception)
                {
                    transaction.Rollback();
                    throw new ServiceException(ErrorCodes.DownloadDocumentException, "Something went wrong while downlading a document", exception);
                }
            }
        }
Пример #2
0
        public async Task DownloadDocument_Should_Return_NotNullModel()
        {
            // Arrange
            var fakeStream = new MemoryStream();
            var expected   = new DocumentDownloadReturnModel
            {
                DocumentContent     = fakeStream.ToArray(),
                DocumentContentType = "application/pdf",
                DocumentPath        = "/wwwroot/documents/"
            };

            // Act
            var actual = await _documentsFileService.DownloadDocument(20);

            // Assert
            actual.Should().NotBeNull();
            actual.Should().Equals(expected);
        }
        public async Task DownloadDocumentsResultOkTest()
        {
            //Arrange
            var fakeStream = new MemoryStream();
            var model      = new DocumentDownloadReturnModel
            {
                DocumentPath        = "/documents/",
                DocumentContentType = "application/pdf",
                DocumentContent     = fakeStream.ToArray()
            };

            _documentsDownloadFileServiceMock.Setup(service => service.DownloadDocument(1))
            .Returns(Task.FromResult(model));

            //Act
            var actual = await _documentsFileController.DownloadDocument(1) as FileContentResult;

            //Assert
            actual.Should().NotBeNull();
        }
        public async Task DownloadDocumentsCallServiceTest()
        {
            //Arrange
            var fakeStream = new MemoryStream();
            var model      = new DocumentDownloadReturnModel
            {
                DocumentPath        = "/documents/",
                DocumentContentType = "application/pdf",
                DocumentContent     = fakeStream.ToArray()
            };

            _documentsDownloadFileServiceMock.Setup(service => service.DownloadDocument(1))
            .Returns(Task.FromResult(model));

            //Act
            await _documentsFileController.DownloadDocument(1);

            //Assert
            _documentsDownloadFileServiceMock.Verify(x => x.DownloadDocument(1));
        }