adding
This commit is contained in:
+455
@@ -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" }
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user