using System; using System.IO; using System.Text; using System.Threading.Tasks; using Azure; using Azure.Storage.Blobs.Models; using LCU.API.Internal.Document.Exceptions; using LCU.API.Internal.Document.Services; using LCU.API.Internal.DocumentLib.DataAccess; using Moq; using NUnit.Framework; namespace LCU.API.Internal.DocumentTest.Services; public class BlobApiServiceTest { private BlobApiService _blobApiService; private Mock _mockBlobDao; [SetUp] public void Setup() { _mockBlobDao = new Mock(); _blobApiService = new BlobApiService(_mockBlobDao.Object); } [Test] public async Task GetDocumentContent_Success() { var docBlobId = Guid.NewGuid(); await using var testStream = new MemoryStream(Encoding.UTF8.GetBytes("whatever")); var mockContent = BlobsModelFactory.BlobDownloadStreamingResult(testStream); var mockContentResponse = new Mock>(); mockContentResponse.Setup(content => content.Value).Returns(mockContent).Verifiable(); _mockBlobDao .Setup(x => x.GetDocumentFromBlob(docBlobId)).ReturnsAsync(mockContentResponse.Object) .Verifiable(); var returnedDocInfo = await _blobApiService.GetDocumentContent(docBlobId); Assert.NotNull(returnedDocInfo); _mockBlobDao.Verify(); } [Test] public void GetDocument_ContentValueIsNull_DocumentBlobServiceExceptionThrown() { var docBlobId = Guid.NewGuid(); _mockBlobDao .Setup(x => x.GetDocumentFromBlob(docBlobId)).ReturnsAsync((Response)null) .Verifiable(); Assert.ThrowsAsync(async() => await _blobApiService.GetDocumentContent(docBlobId)); _mockBlobDao.Verify(); } [Test] public void GetDocument_ExceptionThrown_DocumentBlobServiceExceptionThrown() { var docBlobId = Guid.NewGuid(); _mockBlobDao .Setup(x => x.GetDocumentFromBlob(docBlobId)).ThrowsAsync(new Exception("Thrown from unit test")) .Verifiable(); Assert.ThrowsAsync(async() => await _blobApiService.GetDocumentContent(docBlobId)); _mockBlobDao.Verify(); } [Test] public async Task UploadDocumentContent_Success() { var docContent = new byte[10]; _mockBlobDao.Setup(x => x.SaveDocumentToBlob(docContent)).Verifiable(); await _blobApiService.UploadDocumentContent(docContent); _mockBlobDao.Verify(); } [Test] public void UploadDocumentContent_Exception_ThrowsBlobApiServiceException() { var docContent = new byte[10]; _mockBlobDao.Setup(x => x.SaveDocumentToBlob(docContent)).ThrowsAsync(new Exception("Thrown from unit test")).Verifiable(); Assert.ThrowsAsync(async() => await _blobApiService.UploadDocumentContent(docContent)); _mockBlobDao.Verify(); } [Test] public async Task UpdateDocumentContent_Success() { var docBlobId = Guid.NewGuid(); var docContent = new byte[10]; _mockBlobDao.Setup(x => x.UpdateDocumentInBlob(docBlobId, docContent)).Verifiable(); await _blobApiService.UpdateDocumentContent(docBlobId, docContent); _mockBlobDao.Verify(); } [Test] public void UpdateDocumentContent_Exception_ThrowsBlobApiServiceException() { var docBlobId = Guid.NewGuid(); var docContent = new byte[10]; _mockBlobDao.Setup(x => x.UpdateDocumentInBlob(docBlobId, docContent)).ThrowsAsync(new Exception("Thrown from unit test")).Verifiable(); Assert.ThrowsAsync(async() => await _blobApiService.UpdateDocumentContent(docBlobId, docContent)); _mockBlobDao.Verify(); } [Test] public void UpdateDocumentContent_Exception_DocumentBlobIdIsNull() { Guid? docBlobId = null; var docContent = new byte[10]; Assert.ThrowsAsync(async() => await _blobApiService.UpdateDocumentContent(docBlobId, docContent)); } [Test] public async Task DeleteDocumentContent_Success() { var docBlobId = Guid.NewGuid(); _mockBlobDao.Setup(x => x.RemoveDocumentFromBlob(docBlobId)).Verifiable(); await _blobApiService.DeleteDocumentContent(docBlobId); _mockBlobDao.Verify(); } [Test] public void DeleteDocumentContent_Exception_ThrowsBlobApiServiceException() { var docBlobId = Guid.NewGuid(); _mockBlobDao.Setup(x => x.RemoveDocumentFromBlob(docBlobId)).ThrowsAsync(new Exception("Thrown from unit test")).Verifiable(); Assert.ThrowsAsync(async() => await _blobApiService.DeleteDocumentContent(docBlobId)); _mockBlobDao.Verify(); } [Test] public void DeleteDocumentContent_Exception_DocumentBlobIdIsNull() { Guid? docBlobId = null; Assert.ThrowsAsync(async() => await _blobApiService.DeleteDocumentContent(docBlobId)); } [Test] public async Task GetDocument_BlobGuidIsEmpty_ReturnsEmptyContent() { var docBlobId = Guid.Empty; var emptyContent = Array.Empty(); var result = await _blobApiService.GetDocumentContent(docBlobId); Assert.AreEqual(emptyContent, result); _mockBlobDao.Verify(s => s.GetDocumentFromBlob(It.IsAny()), Times.Never); } [Test] public async Task GetDocument_BlobGuidIsNull_ReturnsEmptyContent() { Guid? docBlobId = null; var emptyContent = Array.Empty(); var result = await _blobApiService.GetDocumentContent(docBlobId); Assert.AreEqual(emptyContent, result); _mockBlobDao.Verify(s => s.GetDocumentFromBlob(It.IsAny()), Times.Never); } }