Files
monorepo/LCU.API.Internal.Document/LCU.API.Internal.DocumentTest/Services/BlobApiServiceTest.cs
T
2026-03-13 09:11:35 -05:00

176 lines
5.8 KiB
C#

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<IBlobApiDao> _mockBlobDao;
[SetUp]
public void Setup()
{
_mockBlobDao = new Mock<IBlobApiDao>();
_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<Response<BlobDownloadStreamingResult>>();
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<BlobDownloadStreamingResult>)null)
.Verifiable();
Assert.ThrowsAsync<BlobApiServiceException>(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<BlobApiServiceException>(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<BlobApiServiceException>(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<BlobApiServiceException>(async() => await _blobApiService.UpdateDocumentContent(docBlobId, docContent));
_mockBlobDao.Verify();
}
[Test]
public void UpdateDocumentContent_Exception_DocumentBlobIdIsNull()
{
Guid? docBlobId = null;
var docContent = new byte[10];
Assert.ThrowsAsync<BlobApiServiceException>(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<BlobApiServiceException>(async() => await _blobApiService.DeleteDocumentContent(docBlobId));
_mockBlobDao.Verify();
}
[Test]
public void DeleteDocumentContent_Exception_DocumentBlobIdIsNull()
{
Guid? docBlobId = null;
Assert.ThrowsAsync<BlobApiServiceException>(async() => await _blobApiService.DeleteDocumentContent(docBlobId));
}
[Test]
public async Task GetDocument_BlobGuidIsEmpty_ReturnsEmptyContent()
{
var docBlobId = Guid.Empty;
var emptyContent = Array.Empty<byte>();
var result = await _blobApiService.GetDocumentContent(docBlobId);
Assert.AreEqual(emptyContent, result);
_mockBlobDao.Verify(s => s.GetDocumentFromBlob(It.IsAny<Guid>()), Times.Never);
}
[Test]
public async Task GetDocument_BlobGuidIsNull_ReturnsEmptyContent()
{
Guid? docBlobId = null;
var emptyContent = Array.Empty<byte>();
var result = await _blobApiService.GetDocumentContent(docBlobId);
Assert.AreEqual(emptyContent, result);
_mockBlobDao.Verify(s => s.GetDocumentFromBlob(It.IsAny<Guid>()), Times.Never);
}
}