This commit is contained in:
Zeeshaun
2026-03-13 09:11:35 -05:00
parent f004c8205a
commit c2d6bda458
197 changed files with 14970 additions and 0 deletions
@@ -0,0 +1,175 @@
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);
}
}
@@ -0,0 +1,557 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using FluentAssertions;
using LCU.API.Internal.Document.Exceptions;
using LCU.API.Internal.Document.Services;
using LCU.API.Internal.DocumentLib.Containers;
using LCU.API.Internal.DocumentLib.Repositories;
using LCU.Library.Internal.DocumentModels.Requests;
using Moq;
using NUnit.Framework;
namespace LCU.API.Internal.DocumentTest.Services;
public class DocumentAuditServiceTest
{
private DocumentAuditService _documentAuditService;
private Mock<IDocumentAuditRepository> _mockDocumentAuditRepository;
[SetUp]
public void Setup()
{
_mockDocumentAuditRepository = new Mock<IDocumentAuditRepository>();
_documentAuditService = new DocumentAuditService(_mockDocumentAuditRepository.Object);
}
[Test]
public async Task GetDocumentAudits_RepositoryReturnsActionAudits_ReturnsAuditsTransformedToResponse()
{
var documentId = Guid.NewGuid();
var actionAudits = new List<DocumentAudit>
{
new DocumentActionAudit
(
documentId,
Guid.NewGuid().ToString(),
DateTime.Now,
Guid.NewGuid().ToString(),
Valid<DocumentAction>(),
Guid.NewGuid().ToString()
)
};
_mockDocumentAuditRepository
.Setup(r => r.GetDocumentAudits(documentId))
.ReturnsAsync(actionAudits);
var audits = await _documentAuditService.GetDocumentAudits(documentId);
actionAudits.Should().HaveSameCount(audits);
actionAudits.Should().AllSatisfy(a => audits.Should().ContainEquivalentOf(a));
}
[Test]
public async Task GetDocumentAudits_RepositoryReturnsFieldAudits_ReturnsAuditsTransformedToResponse()
{
var documentId = Guid.NewGuid();
var actionAudits = new List<DocumentAudit>
{
new DocumentFieldAudit
(
documentId,
Guid.NewGuid().ToString(),
DateTime.Now,
Guid.NewGuid().ToString(),
Guid.NewGuid().ToString(),
Guid.NewGuid(),
Guid.NewGuid()
)
};
_mockDocumentAuditRepository
.Setup(r => r.GetDocumentAudits(documentId))
.ReturnsAsync(actionAudits);
var audits = await _documentAuditService.GetDocumentAudits(documentId);
actionAudits.Should().HaveSameCount(audits);
actionAudits.Should().AllSatisfy(a => audits.Should().ContainEquivalentOf(a));
}
[Test]
public async Task GetDocumentAudits_RepositoryReturnsAnyAuditsOfUnknownType_ThrowsDocumentAuditServiceException()
{
var documentId = Guid.NewGuid();
var actionAudits = new List<DocumentAudit>
{
new UnknownAuditType
(
documentId,
Guid.NewGuid().ToString(),
DateTime.Now,
Guid.NewGuid().ToString()
)
};
_mockDocumentAuditRepository
.Setup(r => r.GetDocumentAudits(documentId))
.ReturnsAsync(actionAudits);
Assert.ThrowsAsync<DocumentAuditServiceException>(() => _documentAuditService.GetDocumentAudits(documentId));
}
[Test]
public void GetDocumentAudits_DocumentAuditRepositoryThrowsException_ThrowsDocumentAuditServiceExceptionThrown()
{
var docId = Guid.NewGuid();
_mockDocumentAuditRepository
.Setup(x => x.GetDocumentAudits(docId))
.ThrowsAsync(new Exception("Thrown From Unit Test"))
.Verifiable();
Assert.ThrowsAsync<DocumentAuditServiceException>(async () => await _documentAuditService.GetDocumentAudits(docId));
_mockDocumentAuditRepository.Verify();
}
[Test]
public void GetDocumentAudits_Exception_DocumentAuditServiceExceptionThrown()
{
var docId = Guid.NewGuid();
_mockDocumentAuditRepository
.Setup(x => x.GetDocumentAudits(docId))
.ThrowsAsync(new Exception("Thrown From Unit Test"))
.Verifiable();
Assert.ThrowsAsync<DocumentAuditServiceException>(async () => await _documentAuditService.GetDocumentAudits(docId));
_mockDocumentAuditRepository.Verify();
}
[Test]
public async Task CreateDocumentUploadedAudit_Success()
{
var uploadDate = DateTime.Parse("2022-04-28T17:32:35.093Z");
var documentInformation = GenerateDocumentInformation();
var audits = new DocumentAudit[]
{
new DocumentActionAudit
(
documentInformation.DocumentId,
"System",
uploadDate,
"System",
DocumentAction.Upload,
"Document Uploaded"
)
};
_mockDocumentAuditRepository
.Setup(x => x.SaveDocumentAudits(It.Is<DocumentAudit[]>(list =>
list.Length == 1
&& list.All(a => a is DocumentActionAudit)
&& ((DocumentActionAudit)list.FirstOrDefault()!).Action == DocumentAction.Upload
&& ((DocumentActionAudit)list.FirstOrDefault()!).UserId == "System"
&& ((DocumentActionAudit)list.FirstOrDefault()!).Source == "System"
&& ((DocumentActionAudit)list.FirstOrDefault()!).ActionDescription == "Document Uploaded")))
.ReturnsAsync(audits)
.Verifiable();
var result = await _documentAuditService.CreateDocumentUploadedAudit(documentInformation.DocumentId, uploadDate);
var resultArray = result.ToArray();
Assert.NotNull(result);
audits.Should().HaveSameCount(resultArray);
audits.Should().AllSatisfy(a => resultArray.Should().ContainEquivalentOf(a));
_mockDocumentAuditRepository.Verify();
}
[Test]
public void CreateDocumentUploadedAudit_DocumentAuditRepositoryThrowsException_ThrowsNewDocumentAuditServiceExceptionThrown()
{
var uploadDate = DateTime.Parse("2022-04-28T17:32:35.093Z");
var documentInformation = GenerateDocumentInformation();
_mockDocumentAuditRepository
.Setup(x => x.SaveDocumentAudits(It.Is<DocumentAudit[]>(list => list.Any())))
.ThrowsAsync(new DocumentAuditServiceException("Thrown From Unit Test"))
.Verifiable();
Assert.ThrowsAsync<DocumentAuditServiceException>(() => _documentAuditService.CreateDocumentUploadedAudit(documentInformation.DocumentId, uploadDate));
_mockDocumentAuditRepository.Verify();
}
[Test]
public void CreateDocumentUploadedAudit_Exception_DocumentAuditServiceExceptionThrown()
{
var uploadDate = DateTime.Parse("2022-04-28T17:32:35.093Z");
var documentInformation = GenerateDocumentInformation();
_mockDocumentAuditRepository
.Setup(x => x.SaveDocumentAudits(It.Is<DocumentAudit[]>(list => list.Any())))
.ThrowsAsync(new Exception("Thrown From Unit Test"))
.Verifiable();
Assert.ThrowsAsync<DocumentAuditServiceException>(() => _documentAuditService.CreateDocumentUploadedAudit(documentInformation.DocumentId, uploadDate));
_mockDocumentAuditRepository.Verify();
}
[Test]
public async Task CreateDocumentDeletedAudit_Success()
{
var deleteDate = DateTime.Parse("2022-04-28T17:32:35.093Z");
var documentInformation = GenerateDocumentInformation();
var audits = new DocumentAudit[]
{
new DocumentActionAudit
(
documentInformation.DocumentId,
"System",
deleteDate,
"System",
DocumentAction.Delete,
"Document Deleted"
)
};
_mockDocumentAuditRepository
.Setup(x => x.SaveDocumentAudits(It.Is<DocumentAudit[]>(list =>
list.Length == 1
&& list.All(a => a is DocumentActionAudit)
&& ((DocumentActionAudit)list.FirstOrDefault()!).Action == DocumentAction.Delete
&& ((DocumentActionAudit)list.FirstOrDefault()!).UserId == "System"
&& ((DocumentActionAudit)list.FirstOrDefault()!).Source == "System"
&& ((DocumentActionAudit)list.FirstOrDefault()!).ActionDescription == "Document Deleted")))
.ReturnsAsync(audits)
.Verifiable();
var result = await _documentAuditService.CreateDocumentDeletedAudit(documentInformation.DocumentId, deleteDate);
var resultArray = result.ToArray();
Assert.NotNull(result);
audits.Should().HaveSameCount(resultArray);
audits.Should().AllSatisfy(a => resultArray.Should().ContainEquivalentOf(a));
_mockDocumentAuditRepository.Verify();
}
[Test]
public void CreateDocumentDeletedAudit_DocumentAuditRepositoryThrowsException_ThrowsNewDocumentAuditServiceExceptionThrown()
{
var deleteDate = DateTime.Parse("2022-04-28T17:32:35.093Z");
var documentInformation = GenerateDocumentInformation();
_mockDocumentAuditRepository
.Setup(x => x.SaveDocumentAudits(It.Is<DocumentAudit[]>(list => list.Any())))
.ThrowsAsync(new DocumentAuditServiceException("Thrown From Unit Test"))
.Verifiable();
Assert.ThrowsAsync<DocumentAuditServiceException>(() => _documentAuditService.CreateDocumentDeletedAudit(documentInformation.DocumentId, deleteDate));
_mockDocumentAuditRepository.Verify();
}
[Test]
public void CreateDocumentDeletedAudit_Exception_DocumentAuditServiceExceptionThrown()
{
var deleteDate = DateTime.Parse("2022-04-28T17:32:35.093Z");
var documentInformation = GenerateDocumentInformation();
_mockDocumentAuditRepository
.Setup(x => x.SaveDocumentAudits(It.Is<DocumentAudit[]>(list => list.Any())))
.ThrowsAsync(new Exception("Thrown From Unit Test"))
.Verifiable();
Assert.ThrowsAsync<DocumentAuditServiceException>(() => _documentAuditService.CreateDocumentDeletedAudit(documentInformation.DocumentId, deleteDate));
_mockDocumentAuditRepository.Verify();
}
[Test]
public async Task CreateDocumentContentDeletedAudit_Success()
{
var deleteDate = DateTime.Parse("2022-04-28T17:32:35.093Z");
var documentInformation = GenerateDocumentInformation();
var reason = Guid.NewGuid().ToString();
var reasonFull = $"DocumentContent Deleted. Reason: {reason}";
var audits = new DocumentAudit[]
{
new DocumentActionAudit
(
documentInformation.DocumentId,
"System",
deleteDate,
"System",
DocumentAction.DeleteContent,
reasonFull
)
};
_mockDocumentAuditRepository
.Setup(x => x.SaveDocumentAudits(It.Is<DocumentAudit[]>(list =>
list.Length == 1
&& list.All(a => a is DocumentActionAudit)
&& ((DocumentActionAudit)list.FirstOrDefault()!).Action == DocumentAction.DeleteContent
&& ((DocumentActionAudit)list.FirstOrDefault()!).UserId == "System"
&& ((DocumentActionAudit)list.FirstOrDefault()!).Source == "System"
&& ((DocumentActionAudit)list.FirstOrDefault()!).ActionDescription == reasonFull)))
.ReturnsAsync(audits)
.Verifiable();
var result = await _documentAuditService.CreateDocumentContentDeletedAudit(documentInformation.DocumentId, deleteDate, reason);
var resultArray = result.ToArray();
Assert.NotNull(result);
audits.Should().HaveSameCount(resultArray);
audits.Should().AllSatisfy(a => resultArray.Should().ContainEquivalentOf(a));
_mockDocumentAuditRepository.Verify();
}
[Test]
public void CreateDocumentContentDeletedAudit_DocumentAuditRepositoryThrowsException_ThrowsNewDocumentAuditServiceExceptionThrown()
{
var deleteDate = DateTime.Parse("2022-04-28T17:32:35.093Z");
var documentInformation = GenerateDocumentInformation();
var reason = Guid.NewGuid().ToString();
_mockDocumentAuditRepository
.Setup(x => x.SaveDocumentAudits(It.Is<DocumentAudit[]>(list => list.Any())))
.ThrowsAsync(new DocumentAuditServiceException("Thrown From Unit Test"))
.Verifiable();
Assert.ThrowsAsync<DocumentAuditServiceException>(() => _documentAuditService.CreateDocumentContentDeletedAudit(documentInformation.DocumentId, deleteDate, reason));
_mockDocumentAuditRepository.Verify();
}
[Test]
public void CreateDocumentContentDeletedAudit_Exception_DocumentAuditServiceExceptionThrown()
{
var deleteDate = DateTime.Parse("2022-04-28T17:32:35.093Z");
var documentInformation = GenerateDocumentInformation();
var reason = Guid.NewGuid().ToString();
_mockDocumentAuditRepository
.Setup(x => x.SaveDocumentAudits(It.Is<DocumentAudit[]>(list => list.Any())))
.ThrowsAsync(new Exception("Thrown From Unit Test"))
.Verifiable();
Assert.ThrowsAsync<DocumentAuditServiceException>(() => _documentAuditService.CreateDocumentContentDeletedAudit(documentInformation.DocumentId, deleteDate, reason));
_mockDocumentAuditRepository.Verify();
}
[Test]
public async Task CreateDocumentAudits_No_Differences_Success()
{
var updateDate = DateTime.Parse("2022-04-28T17:32:35.093Z");
var docInformationOld = GenerateDocumentInformation();
var docInformationNew = GenerateDocumentInformation();
var result = await _documentAuditService.CreateDocumentAudits(docInformationOld, docInformationNew, false, updateDate, "testUser");
Assert.NotNull(result);
Assert.IsEmpty(result);
_mockDocumentAuditRepository.Verify(x => x.SaveDocumentAudits(It.IsAny<DocumentAudit[]>()), Times.Never);
}
[Test]
public async Task CreateDocumentAudits_No_Differences_ContentChange_Success()
{
var updateDate = DateTime.Parse("2022-04-28T17:32:35.093Z");
var userId = Guid.NewGuid().ToString();
var docInformationOld = GenerateDocumentInformation();
var docInformationNew = GenerateDocumentInformation();
_mockDocumentAuditRepository
.Setup(x => x.SaveDocumentAudits(It.Is<DocumentAudit[]>(list =>
list.Length == 1
&& list.All(a => a is DocumentFieldAudit)
&& ((DocumentFieldAudit)list.FirstOrDefault()!).Field == "Content"
&& list.All(audit => string.Equals(userId, ((DocumentFieldAudit)audit).UserId)))))
.ReturnsAsync(new List<DocumentAudit>())
.Verifiable();
await _documentAuditService.CreateDocumentAudits(docInformationOld, docInformationNew, true, updateDate, userId);
_mockDocumentAuditRepository.Verify();
}
[Test]
public async Task CreateDocumentAudits_All_Differences_Success()
{
var updateDate = DateTime.Parse("2022-04-28T17:32:35.093Z");
var docInformationOld = GenerateDocumentInformation();
var docInformationEverythingChanges = GenerateDocumentInformation();
var userId = Guid.NewGuid().ToString();
docInformationEverythingChanges.Name = "BrandNewName";
docInformationEverythingChanges.Type = "BrandNewType";
docInformationEverythingChanges.ScanStatus = true;
docInformationEverythingChanges.DataSource = "NEW SOURCE";
docInformationEverythingChanges.Attributes = new Dictionary<string, string>
{
{ "NEW_FIELD", "NEW_VALUE" }
};
_mockDocumentAuditRepository
.Setup(x => x.SaveDocumentAudits(It.Is<DocumentAudit[]>(list =>
list.Length == 4
&& list.All(a => a is DocumentFieldAudit)
&& list.Any(audit => string.Equals("Name", ((DocumentFieldAudit)audit).Field))
&& list.Any(audit => string.Equals("Type", ((DocumentFieldAudit)audit).Field))
&& list.Any(audit => string.Equals("ScanStatus", ((DocumentFieldAudit)audit).Field))
&& list.Any(audit => string.Equals("Attributes", ((DocumentFieldAudit)audit).Field))
&& list.All(audit => string.Equals(userId, ((DocumentFieldAudit)audit).UserId)))))
.ReturnsAsync(new List<DocumentAudit>())
.Verifiable();
await _documentAuditService.CreateDocumentAudits(docInformationOld, docInformationEverythingChanges, false, updateDate, userId);
_mockDocumentAuditRepository.Verify();
}
[Test]
public void CreateDocumentAudits_DocumentAuditRepositoryThrowsException_ThrowsNewDocumentAuditServiceExceptionThrown()
{
var updateDate = DateTime.Parse("2022-04-28T17:32:35.093Z");
var docInformationOld = GenerateDocumentInformation();
var docInformationNew = GenerateDocumentInformation();
var userId = Guid.NewGuid().ToString();
docInformationNew.Name = "NewName.txt";
_mockDocumentAuditRepository
.Setup(x => x.SaveDocumentAudits(It.Is<DocumentAudit[]>(list => list.Any())))
.ThrowsAsync(new DocumentAuditServiceException("Thrown From Unit Test"))
.Verifiable();
Assert.ThrowsAsync<DocumentAuditServiceException>(() => _documentAuditService.CreateDocumentAudits(docInformationOld, docInformationNew, false, updateDate, userId));
_mockDocumentAuditRepository.Verify();
}
[Test]
public void CreateDocumentAudits_Exception_DocumentAuditServiceExceptionThrown()
{
var updateDate = DateTime.Parse("2022-04-28T17:32:35.093Z");
var docInformationOld = GenerateDocumentInformation();
var docInformationNew = GenerateDocumentInformation();
var userId = Guid.NewGuid().ToString();
docInformationNew.Name = "NewName.txt";
_mockDocumentAuditRepository
.Setup(x => x.SaveDocumentAudits(It.Is<DocumentAudit[]>(list => list.Any())))
.ThrowsAsync(new Exception("Thrown From Unit Test"))
.Verifiable();
Assert.ThrowsAsync<DocumentAuditServiceException>(() => _documentAuditService.CreateDocumentAudits(docInformationOld, docInformationNew, false, updateDate, userId));
_mockDocumentAuditRepository.Verify();
}
[Test]
public void AuditAction_RepositoryThrowsException_ThrowsDocumentAuditServiceException()
{
var documentId = Guid.NewGuid();
var userId = Guid.NewGuid().ToString();
var source = Guid.NewGuid().ToString();
var action = Valid<DocumentAction>();
_mockDocumentAuditRepository
.Setup(r => r.SaveDocumentAudits(It.Is<DocumentActionAudit>(a =>
a.DocumentId == documentId
&& a.UserId == userId
&& a.Source == source
&& a.Action == action)))
.ThrowsAsync(new Exception());
Assert.ThrowsAsync<DocumentAuditServiceException>(() => _documentAuditService.AuditAction(documentId, userId, source, action));
}
[Test]
public void AuditAction_UnknownActionType_ThrowsDocumentAuditServiceException()
{
var documentId = Guid.NewGuid();
var userId = Guid.NewGuid().ToString();
var source = Guid.NewGuid().ToString();
var action = Invalid<DocumentAction>();
Assert.ThrowsAsync<DocumentAuditServiceException>(() => _documentAuditService.AuditAction(documentId, userId, source, action));
}
private static DocumentAction[] AllDocumentActions => AllValid<DocumentAction>();
[TestCaseSource(nameof(AllDocumentActions))]
public async Task AuditAction_ValidAction_SavesActionAudit(DocumentAction action)
{
var documentId = Guid.NewGuid();
var userId = Guid.NewGuid().ToString();
var source = Guid.NewGuid().ToString();
_mockDocumentAuditRepository
.Setup(r => r.SaveDocumentAudits(It.Is<DocumentActionAudit>(a =>
a.DocumentId == documentId
&& a.UserId == userId
&& a.Source == source
&& a.Action == action)))
.Verifiable();
await _documentAuditService.AuditAction(documentId, userId, source, action);
_mockDocumentAuditRepository.Verify();
}
private static List<DocumentAudit> GenerateDocumentAudits()
{
return new List<DocumentAudit>
{
new DocumentFieldAudit(Guid.NewGuid(), "UnitTest", DateTime.Parse("2022-04-28T17:32:35.093Z"), "Test", "Test", "0", "1")
};
}
private static DocumentInformation GenerateDocumentInformation()
{
return new DocumentInformation
{
DocumentId = Guid.NewGuid(),
Name = "TestFile.txt",
Type = "Test",
Source = "Developer's Laptop",
ScanStatus = null,
UploadDate = DateTime.Parse("2022-03-25T19:16:12.3665642Z"),
LastModifiedDate = DateTime.Parse("0001-01-01T00:00:00"),
Attributes = new Dictionary<string, string>
{
{ "IsTest", "true" },
{ "Owner", "Me Myself" }
}
};
}
private static T[] AllValid<T>()
where T : struct, Enum
{
return Enum.GetValues<T>();
}
private static T Valid<T>()
where T : struct, Enum
{
var values = Enum.GetValues<T>();
return values[DateTime.Now.Millisecond % values.Length];
}
private static T Invalid<T>()
where T : struct, Enum
{
return (T)Convert.ChangeType(Enum.GetValues<T>().Length + 1, typeof(int));
}
private class UnknownAuditType : DocumentAudit
{
public UnknownAuditType(Guid documentId, string source, DateTime date, string userId)
: base(documentId, source, date, userId)
{
}
}
}
@@ -0,0 +1,455 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using LCU.API.Internal.Document.Configurations;
using LCU.API.Internal.Document.Exceptions;
using LCU.API.Internal.Document.Services;
using LCU.API.Internal.DocumentLib.Containers;
using LCU.API.Internal.DocumentLib.Repositories;
using LCU.Library.Internal.DocumentModels.Requests.Search;
using Moq;
using NUnit.Framework;
namespace LCU.API.Internal.DocumentTest.Services;
public class DocumentInformationServiceTest
{
private DocumentInformationService _documentInformationService;
private Mock<IDocumentInformationRepository> _mockDocumentRepository;
private ApiConfiguration _apiConfiguration;
[SetUp]
public void Setup()
{
_mockDocumentRepository = new Mock<IDocumentInformationRepository>();
_apiConfiguration = new ApiConfiguration() { FileUploadCutoffHours = -2 };
_documentInformationService = new DocumentInformationService(_mockDocumentRepository.Object,
_apiConfiguration);
}
[Test]
public async Task GetDocumentInformation_Success()
{
var docId = Guid.NewGuid();
var docBlobId = Guid.NewGuid();
var docInformation = GenerateDocumentInformation();
docInformation.DocumentBlobId = docBlobId;
docInformation.UploadDate = DateTime.UtcNow.AddDays(-1);
docInformation.ScanStatus = true;
_mockDocumentRepository
.Setup(x => x.GetDocumentInformation(docId)).ReturnsAsync(docInformation)
.Verifiable();
var returnedDocInfo = await _documentInformationService.GetDocumentInformation(docId);
Assert.NotNull(returnedDocInfo);
Assert.AreEqual(docInformation.DocumentId, returnedDocInfo?.DocumentId);
Assert.AreEqual(docInformation.Name, returnedDocInfo?.Name);
Assert.AreEqual(docInformation.Type, returnedDocInfo?.Type);
Assert.AreEqual(docInformation.Source, returnedDocInfo?.Source);
Assert.AreEqual(docInformation.ScanStatus, returnedDocInfo?.ScanStatus);
Assert.AreEqual(docInformation.UploadDate, returnedDocInfo?.UploadDate);
Assert.AreEqual(docInformation.LastModifiedDate, returnedDocInfo?.LastModifiedDate);
Assert.AreEqual(docInformation.PersonNumbers, returnedDocInfo?.PersonNumbers);
Assert.AreEqual(docInformation.OrganizationNumbers, returnedDocInfo?.OrganizationNumbers);
Assert.AreEqual(docInformation.AccountNumber, returnedDocInfo?.AccountNumber);
Assert.AreEqual(docInformation.Attributes, returnedDocInfo?.Attributes);
_mockDocumentRepository.Verify();
}
[Test]
public async Task GetDocument_NoInformationPresent_Null()
{
var docId = Guid.NewGuid();
_mockDocumentRepository
.Setup(x => x.GetDocumentInformation(docId)).ReturnsAsync(null as DocumentInformation)
.Verifiable();
var result = await _documentInformationService.GetDocumentInformation(docId);
Assert.Null(result);
_mockDocumentRepository.Verify();
}
[Test]
public async Task GetDocument_ScanStatusIsFalse_DocumentIsNull()
{
var docId = Guid.NewGuid();
var docInformation = GenerateDocumentInformation();
docInformation.ScanStatus = false;
_mockDocumentRepository
.Setup(x => x.GetDocumentInformation(docId)).ReturnsAsync(docInformation)
.Verifiable();
var result = await _documentInformationService.GetDocumentInformation(docId);
Assert.IsNull(result);
_mockDocumentRepository.Verify();
}
[Test]
public void GetDocument_ExceptionThrown_ThrowDocumentInformationServiceException()
{
var docId = Guid.NewGuid();
_mockDocumentRepository
.Setup(x => x.GetDocumentInformation(docId))
.ThrowsAsync(new Exception("Thrown from unit test"))
.Verifiable();
Assert.ThrowsAsync<DocumentInformationServiceException>(async () => await _documentInformationService.GetDocumentInformation(docId));
_mockDocumentRepository.Verify();
}
[Test]
public async Task GetDocumentInformationByBlobId_Success()
{
var docBlobId = Guid.NewGuid();
var docInformation = GenerateDocumentInformation();
docInformation.DocumentBlobId = docBlobId;
docInformation.ScanStatus = true;
_mockDocumentRepository
.Setup(x => x.GetDocumentInformationByBlobId(docBlobId)).ReturnsAsync(docInformation)
.Verifiable();
var returnedDocInfo = await _documentInformationService.GetDocumentInformationByBlobId(docBlobId);
Assert.NotNull(returnedDocInfo);
Assert.AreEqual(docInformation.DocumentId, returnedDocInfo?.DocumentId);
Assert.AreEqual(docInformation.Name, returnedDocInfo?.Name);
Assert.AreEqual(docInformation.Type, returnedDocInfo?.Type);
Assert.AreEqual(docInformation.Source, returnedDocInfo?.Source);
Assert.AreEqual(docInformation.ScanStatus, returnedDocInfo?.ScanStatus);
Assert.AreEqual(docInformation.UploadDate, returnedDocInfo?.UploadDate);
Assert.AreEqual(docInformation.LastModifiedDate, returnedDocInfo?.LastModifiedDate);
Assert.AreEqual(docInformation.PersonNumbers, returnedDocInfo?.PersonNumbers);
Assert.AreEqual(docInformation.OrganizationNumbers, returnedDocInfo?.OrganizationNumbers);
Assert.AreEqual(docInformation.AccountNumber, returnedDocInfo?.AccountNumber);
Assert.AreEqual(docInformation.Attributes, returnedDocInfo?.Attributes);
_mockDocumentRepository.Verify();
}
[Test]
public async Task GetDocumentByBlobId_NoInformationPresent_Null()
{
var docBlobId = Guid.NewGuid();
_mockDocumentRepository
.Setup(x => x.GetDocumentInformationByBlobId(docBlobId)).ReturnsAsync(null as DocumentInformation)
.Verifiable();
var result = await _documentInformationService.GetDocumentInformationByBlobId(docBlobId);
Assert.Null(result);
_mockDocumentRepository.Verify();
}
[Test]
public void GetDocumentByBlobId_ExceptionThrown_ThrowDocumentInformationServiceException()
{
var docBlobId = Guid.NewGuid();
_mockDocumentRepository
.Setup(x => x.GetDocumentInformationByBlobId(docBlobId))
.ThrowsAsync(new Exception("Thrown from unit test"))
.Verifiable();
Assert.ThrowsAsync<DocumentInformationServiceException>(async () => await _documentInformationService.GetDocumentInformationByBlobId(docBlobId));
_mockDocumentRepository.Verify();
}
[Test]
public async Task DeleteDocument_Success()
{
var docId = Guid.NewGuid();
var docBlobId = Guid.NewGuid();
var docInformation = GenerateDocumentInformation();
docInformation.DocumentBlobId = docBlobId;
_mockDocumentRepository
.Setup(x => x.GetDocumentInformation(docId)).ReturnsAsync(docInformation)
.Verifiable();
_mockDocumentRepository
.Setup(x => x.RemoveDocumentInformation(docId)).ReturnsAsync(docInformation)
.Verifiable();
await _documentInformationService.DeleteDocumentInformation(docId);
_mockDocumentRepository.Verify();
}
[Test]
public async Task DeleteDocument_NoInformationPresent_Null()
{
var docId = Guid.NewGuid();
var docBlobId = Guid.NewGuid();
var docInformation = GenerateDocumentInformation();
docInformation.DocumentBlobId = docBlobId;
_mockDocumentRepository
.Setup(x => x.GetDocumentInformation(docId)).ReturnsAsync(null as DocumentInformation)
.Verifiable();
await _documentInformationService.DeleteDocumentInformation(docId);
_mockDocumentRepository.Verify(x => x.RemoveDocumentInformation(It.IsAny<Guid>()), Times.Never);
_mockDocumentRepository.Verify();
}
[Test]
public void DeleteDocument_RemoveDocumentInfoThrowsException_ThrowDocumentInformationServiceException()
{
var docId = Guid.NewGuid();
var docBlobId = Guid.NewGuid();
var docInformation = GenerateDocumentInformation();
docInformation.DocumentBlobId = docBlobId;
_mockDocumentRepository
.Setup(x => x.GetDocumentInformation(docId)).ReturnsAsync(docInformation)
.Verifiable();
_mockDocumentRepository.Setup(x => x.RemoveDocumentInformation(docId))
.ThrowsAsync(new Exception("thrown from unit test")).Verifiable();
Assert.ThrowsAsync<DocumentInformationServiceException>(async () => await _documentInformationService.DeleteDocumentInformation(docId));
_mockDocumentRepository.Verify();
}
[Test]
public async Task UploadDocument_Success()
{
var docBlobId = Guid.NewGuid();
var docInformation = GenerateDocumentInformation();
docInformation.DocumentBlobId = docBlobId;
_mockDocumentRepository
.Setup(x => x.SaveDocumentInformation(It.Is<DocumentInformation>(info =>
info.DocumentBlobId == docBlobId)))
.ReturnsAsync(docInformation)
.Verifiable();
var result = await _documentInformationService.UploadDocumentInformation(docInformation);
Assert.AreEqual(docInformation, result);
_mockDocumentRepository.Verify();
}
[Test]
public void UploadDocument_ExceptionThrown_ThrowDocumentInformation()
{
var docInformation = GenerateDocumentInformation();
_mockDocumentRepository.Setup(x => x.SaveDocumentInformation(It.IsAny<DocumentInformation>()))
.ThrowsAsync(new Exception("Error thrown from unit test.")).Verifiable();
Assert.ThrowsAsync<DocumentInformationServiceException>(async () => await _documentInformationService.UploadDocumentInformation(docInformation));
_mockDocumentRepository.Verify();
}
[Test]
public async Task UpdateDocument_Success()
{
var docId = Guid.NewGuid();
var docInformation = GenerateDocumentInformation();
docInformation.DocumentId = docId;
_mockDocumentRepository.Setup(x => x.GetDocumentInformation(docId)).ReturnsAsync(docInformation).Verifiable();
_mockDocumentRepository
.Setup(x => x.UpdateDocumentInformation(docId, It.Is<DocumentInformation>(info =>
info.DocumentId == docId))).ReturnsAsync(docInformation)
.Verifiable();
await _documentInformationService.UpdateDocumentInformation(docId, docInformation);
_mockDocumentRepository.Verify();
}
[Test]
public async Task UpdateDocument_NoInformation_Null()
{
var docId = Guid.NewGuid();
var docInformation = GenerateDocumentInformation();
_mockDocumentRepository
.Setup(x => x.GetDocumentInformation(docId)).ReturnsAsync(null as DocumentInformation)
.Verifiable();
var result = await _documentInformationService.UpdateDocumentInformation(docId, docInformation);
Assert.Null(result);
_mockDocumentRepository.Verify();
}
[Test]
public void UpdateDocument_ExceptionThrown_ThrowDocumentInformationServiceException()
{
var docBlobId = Guid.NewGuid();
var docId = Guid.NewGuid();
var docInformation = GenerateDocumentInformation();
docInformation.DocumentId = docId;
docInformation.DocumentBlobId = docBlobId;
_mockDocumentRepository.Setup(x => x.GetDocumentInformation(docId)).ThrowsAsync(new Exception("Thrown from unit test")).Verifiable();
Assert.ThrowsAsync<DocumentInformationServiceException>(async () => await _documentInformationService.UpdateDocumentInformation(docId, docInformation));
_mockDocumentRepository.Verify(
x => x.UpdateDocumentInformation(It.IsAny<Guid>(), It.IsAny<DocumentInformation>()), Times.Never);
_mockDocumentRepository.Verify();
}
[Test]
public async Task SearchDocumentInformation_Success()
{
var searchFilter = GenerateDocumentInformationFilter();
var random = new Random(DateTime.Now.Millisecond);
var skip = random.Next();
var take = random.Next();
var docInformation = new List<DocumentInformation> { GenerateDocumentInformation() };
_mockDocumentRepository
.Setup(x => x.SearchDocumentInformation(searchFilter, skip, take, null))
.ReturnsAsync(docInformation);
var result = await _documentInformationService.SearchDocumentInformation(searchFilter, skip, take);
Assert.AreEqual(docInformation, result);
}
[Test]
public async Task SearchDocumentInformation_NoneFound_EmptyList()
{
var searchFilter = GenerateDocumentInformationFilter();
_mockDocumentRepository
.Setup(x => x.SearchDocumentInformation(searchFilter, It.IsAny<int?>(), It.IsAny<int?>(), null))
.ReturnsAsync((List<DocumentInformation>)null)
.Verifiable();
var result = await _documentInformationService.SearchDocumentInformation(searchFilter);
_mockDocumentRepository.Verify();
Assert.IsEmpty(result);
}
[Test]
public void SearchDocumentInformation_ThrowsException_DocumentInformationServiceException()
{
var searchFilter = GenerateDocumentInformationFilter();
_mockDocumentRepository
.Setup(x => x.SearchDocumentInformation(searchFilter, It.IsAny<int?>(), It.IsAny<int?>(), null))
.ThrowsAsync(new Exception("Thrown from unit test"))
.Verifiable();
Assert.ThrowsAsync<DocumentInformationServiceException>(async() => await _documentInformationService.SearchDocumentInformation(searchFilter));
_mockDocumentRepository.Verify();
}
[Test]
public void CountDocuments_RepositoryCountThrows_ThrowsDocumentInformationServiceException()
{
var filter = new DocumentInformationFilter();
_mockDocumentRepository
.Setup(r => r.CountDocuments(filter))
.ThrowsAsync(new Exception());
Assert.ThrowsAsync<DocumentInformationServiceException>(() => _documentInformationService.CountDocuments(filter));
}
[Test]
public async Task CountDocuments_RepositoryReturnsCount_ReturnsCount()
{
var filter = new DocumentInformationFilter();
var expectedCount = DateTime.Now.Millisecond;
_mockDocumentRepository
.Setup(r => r.CountDocuments(filter))
.ReturnsAsync(expectedCount);
var count = await _documentInformationService.CountDocuments(filter);
Assert.AreEqual(expectedCount, count);
}
[Test]
public async Task GetDocumentsForCleanup_Success()
{
var documents = new List<Guid> { Guid.NewGuid(), Guid.NewGuid(), Guid.NewGuid() };
_mockDocumentRepository.Setup(x => x.GetDocumentsForCleanup(It.IsAny<GetDocumentCleanupRequest>()))
.ReturnsAsync(documents).Verifiable();
var result = await _documentInformationService.GetDocumentsForCleanup(It.IsAny<GetDocumentCleanupRequest>());
Assert.NotNull(result);
Assert.IsNotEmpty(result);
Assert.AreSame(documents, result);
_mockDocumentRepository.Verify(x => x.GetDocumentsForCleanup(It.IsAny<GetDocumentCleanupRequest>()), Times.Once);
}
[Test]
public async Task GetDocumentsForCleanup_Success_NoDocumentsFound()
{
var documents = new List<Guid>();
_mockDocumentRepository.Setup(x => x.GetDocumentsForCleanup(It.IsAny<GetDocumentCleanupRequest>()))
.ReturnsAsync(documents).Verifiable();
var result = await _documentInformationService.GetDocumentsForCleanup(It.IsAny<GetDocumentCleanupRequest>());
Assert.NotNull(result);
Assert.IsEmpty(result);
Assert.AreSame(documents, result);
_mockDocumentRepository.Verify(x => x.GetDocumentsForCleanup(It.IsAny<GetDocumentCleanupRequest>()), Times.Once);
}
[Test]
public void GetDocumentsForCleanup_ThrowsDocumentInformationServiceException()
{
_mockDocumentRepository
.Setup(r => r.GetDocumentsForCleanup(It.IsAny<GetDocumentCleanupRequest>()))
.ThrowsAsync(new Exception());
Assert.ThrowsAsync<DocumentInformationServiceException>(() =>
_documentInformationService.GetDocumentsForCleanup(It.IsAny<GetDocumentCleanupRequest>()));
}
private static DocumentInformationFilter GenerateDocumentInformationFilter()
{
return new DocumentInformationFilter
{
Name = "TestSearch"
};
}
private static DocumentInformation GenerateDocumentInformation()
{
return new DocumentInformation
{
DocumentId = Guid.NewGuid(),
Name = "TestFile.txt",
Type = "Test",
Source = "Developer's Laptop",
DataSource = "DNA",
ScanStatus = null,
UploadDate = DateTime.Parse("2022-03-25T19:16:12.3665642Z"),
LastModifiedDate = DateTime.Parse("0001-01-01T00:00:00"),
PersonNumbers = new string[] { "1", "2" },
OrganizationNumbers = new string[] { "3", "4" },
AccountNumber = "5",
Attributes = new Dictionary<string, string>
{
{ "IsTest", "true" },
{ "Owner", "Me Myself" }
}
};
}
}
@@ -0,0 +1,56 @@
using System.Threading.Tasks;
using System;
using Moq;
using NUnit.Framework;
using LCU.Library.Internal.Exceptions;
using LCU.API.Internal.Document.Services;
using LCU.API.Internal.DocumentLib.DataAccess;
using LCU.API.Internal.NotificationsApiLib.Models;
using System.Collections.Generic;
namespace LCU.API.Internal.DocumentTest.Services;
public class NotificationApiServiceTest
{
private NotificationApiService _notificationApiService;
private Mock<INotificationApiDao> _mockNotificationApiDao;
[SetUp]
public void Setup()
{
_mockNotificationApiDao = new Mock<INotificationApiDao>();
_notificationApiService = new NotificationApiService(_mockNotificationApiDao.Object);
}
[Test]
public void SendEmailAsync_Exception()
{
_mockNotificationApiDao.Setup(x => x.SendEmailAsync(It.IsAny<EmailRequest>()))
.ThrowsAsync(new Exception("Error"));
var request = new EmailRequest()
{
Subject = "",
Body = "",
Recipient = new List<string>(new string[] {""})
};
Assert.ThrowsAsync<ServiceException>(async () => await _notificationApiService.SendEmailAsync(request));
_mockNotificationApiDao.Verify(x => x.SendEmailAsync(It.IsAny<EmailRequest>()), Times.Once);
}
[Test]
public async Task SendEmailAsync_Return()
{
_mockNotificationApiDao.Setup(x => x.SendEmailAsync(It.IsAny<EmailRequest>()));
var request = new EmailRequest()
{
Subject = "",
Body = "",
Recipient = new List<string>(new string[] { "" })
};
await _notificationApiService.SendEmailAsync(request);
_mockNotificationApiDao.Verify(x => x.SendEmailAsync(It.IsAny<EmailRequest>()), Times.Once);
}
}