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

1630 lines
71 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FluentAssertions;
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.Library.Internal.DocumentModels.Requests;
using LCU.Library.Internal.DocumentModels.Requests.Search;
using LCU.Library.Internal.DocumentModels.Responses;
using LCU.Library.Internal.Exceptions;
using Moq;
using NUnit.Framework;
namespace LCU.API.Internal.DocumentTest.Services;
public class DocumentServiceTest
{
private IDocumentService _documentService;
private IDocumentService DocumentServiceFactory(DocumentService.DocumentIsForMemberStrategy documentIsForMember) => new DocumentService
(
_mockBlobApiService.Object,
_mockDocumentInformationService.Object,
_mockDocumentAuditService.Object,
_mockNotificationApiService.Object,
_apiConfiguration,
documentIsForMember
);
private Mock<IBlobApiService> _mockBlobApiService;
private Mock<IDocumentInformationService> _mockDocumentInformationService;
private Mock<IDocumentAuditService> _mockDocumentAuditService;
private Mock<INotificationApiService> _mockNotificationApiService;
private ApiConfiguration _apiConfiguration;
[SetUp]
public void Setup()
{
_mockBlobApiService = new Mock<IBlobApiService>();
_mockDocumentInformationService = new Mock<IDocumentInformationService>();
_mockDocumentAuditService = new Mock<IDocumentAuditService>();
_mockNotificationApiService = new Mock<INotificationApiService>();
_apiConfiguration = new ApiConfiguration();
_documentService = DocumentServiceFactory(DocumentService.Ownership.CheckPersonOrOrgNumber.Strategy);
}
[Test]
public async Task GetDocument_Success()
{
var docId = Guid.NewGuid();
var docBlobId = Guid.NewGuid();
var docInformation = GenerateDocumentInformation();
docInformation.DocumentBlobId = docBlobId;
var docContent = new byte[10];
_mockDocumentInformationService
.Setup(x => x.GetDocumentInformation(docId)).ReturnsAsync(docInformation)
.Verifiable();
_mockBlobApiService.Setup(x => x.GetDocumentContent(docBlobId)).ReturnsAsync(docContent).Verifiable();
var result = await _documentService.GetDocument(docId);
Assert.NotNull(result);
var returnedDocInfo = result!.DocumentInformation;
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.CreatedDate, returnedDocInfo.CreatedDate);
Assert.AreEqual(docInformation.Attributes, returnedDocInfo.Attributes);
_mockBlobApiService.Verify();
_mockDocumentInformationService.Verify();
}
[Test]
public async Task GetDocument_NoInformationPresent_Null()
{
var docId = Guid.NewGuid();
var docBlobId = Guid.NewGuid();
var docInformation = GenerateDocumentInformation();
docInformation.DocumentBlobId = docBlobId;
_mockDocumentInformationService
.Setup(x => x.GetDocumentInformation(docId)).ReturnsAsync(null as DocumentInformation)
.Verifiable();
var result = await _documentService.GetDocument(docId);
Assert.Null(result);
_mockBlobApiService.Verify();
_mockDocumentInformationService.Verify();
}
[Test]
public void GetDocument_DocumentInformationServiceExceptionThrown_ServiceException()
{
var docId = Guid.NewGuid();
var docBlobId = Guid.NewGuid();
var docInformation = GenerateDocumentInformation();
docInformation.DocumentBlobId = docBlobId;
_mockDocumentInformationService
.Setup(x => x.GetDocumentInformation(docId))
.ThrowsAsync(new DocumentInformationServiceException("Thrown from unit test"))
.Verifiable();
Assert.ThrowsAsync<ServiceException>(async () => await _documentService.GetDocument(docId));
_mockBlobApiService.Verify(x => x.GetDocumentContent(docBlobId), Times.Never);
_mockDocumentInformationService.Verify();
}
[Test]
public void GetDocument_BlobApiServiceException_ServiceException()
{
var docId = Guid.NewGuid();
var docBlobId = Guid.NewGuid();
var docInformation = GenerateDocumentInformation();
docInformation.DocumentBlobId = docBlobId;
_mockDocumentInformationService
.Setup(x => x.GetDocumentInformation(docId)).ReturnsAsync(docInformation)
.Verifiable();
_mockBlobApiService.Setup(x => x.GetDocumentContent(docBlobId))
.ThrowsAsync(new BlobApiServiceException("Thrown from Unit Test")).Verifiable();
Assert.ThrowsAsync<ServiceException>(async () => await _documentService.GetDocument(docId));
_mockBlobApiService.Verify();
_mockDocumentInformationService.Verify();
}
[Test]
public void GetDocument_Exception_ServiceException()
{
var docId = Guid.NewGuid();
var docBlobId = Guid.NewGuid();
var docInformation = GenerateDocumentInformation();
docInformation.DocumentBlobId = docBlobId;
_mockDocumentInformationService
.Setup(x => x.GetDocumentInformation(docId)).ReturnsAsync(docInformation)
.Verifiable();
_mockBlobApiService.Setup(x => x.GetDocumentContent(docBlobId))
.ThrowsAsync(new Exception("Thrown from Unit Test")).Verifiable();
Assert.ThrowsAsync<ServiceException>(async () => await _documentService.GetDocument(docId));
_mockBlobApiService.Verify();
_mockDocumentInformationService.Verify();
}
[Test]
public async Task DeleteDocument_Success()
{
var docId = Guid.NewGuid();
var docBlobId = Guid.NewGuid();
var docInformation = GenerateDocumentInformation();
docInformation.DocumentBlobId = docBlobId;
_mockDocumentInformationService
.Setup(x => x.GetDocumentInformation(docId)).ReturnsAsync(docInformation)
.Verifiable();
_mockDocumentInformationService
.Setup(x => x.DeleteDocumentInformation(docId)).Verifiable();
_mockBlobApiService.Setup(x => x.DeleteDocumentContent(docBlobId)).Verifiable();
await _documentService.DeleteDocument(docId);
_mockBlobApiService.Verify();
_mockDocumentInformationService.Verify();
_mockDocumentAuditService.Verify(x => x.CreateDocumentDeletedAudit(It.IsAny<Guid>(), It.IsAny<DateTime>()), Times.Once);
}
[Test]
public async Task DeleteDocument_NoInformationPresent_Null()
{
var docId = Guid.NewGuid();
var docBlobId = Guid.NewGuid();
var docInformation = GenerateDocumentInformation();
docInformation.DocumentBlobId = docBlobId;
_mockDocumentInformationService
.Setup(x => x.GetDocumentInformation(docId)).ReturnsAsync(null as DocumentInformation)
.Verifiable();
await _documentService.DeleteDocument(docId);
_mockBlobApiService.Verify(x => x.DeleteDocumentContent(It.IsAny<Guid>()), Times.Never);
_mockDocumentInformationService.Verify(x => x.DeleteDocumentInformation(It.IsAny<Guid>()), Times.Never);
_mockDocumentAuditService.Verify(x => x.CreateDocumentDeletedAudit(It.IsAny<Guid>(), It.IsAny<DateTime>()), Times.Never);
_mockDocumentInformationService.Verify();
}
[Test]
public void DeleteDocument_RemoveDocumentInfoThrowsDocumentInformationServiceException_ServiceException()
{
var docId = Guid.NewGuid();
var docBlobId = Guid.NewGuid();
var docInformation = GenerateDocumentInformation();
docInformation.DocumentBlobId = docBlobId;
_mockDocumentInformationService
.Setup(x => x.GetDocumentInformation(docId)).ReturnsAsync(docInformation)
.Verifiable();
_mockDocumentInformationService.Setup(x => x.DeleteDocumentInformation(docId))
.ThrowsAsync(new DocumentInformationServiceException("thrown from unit test")).Verifiable();
Assert.ThrowsAsync<ServiceException>(async () => await _documentService.DeleteDocument(docId));
_mockBlobApiService.Verify(x => x.DeleteDocumentContent(It.IsAny<Guid>()), Times.Never);
_mockDocumentAuditService.Verify(x => x.CreateDocumentDeletedAudit(It.IsAny<Guid>(), It.IsAny<DateTime>()), Times.Never);
_mockDocumentInformationService.Verify();
}
[Test]
public void DeleteDocument_RemoveDocumentInfoThrowsException_ServiceException()
{
var docId = Guid.NewGuid();
var docBlobId = Guid.NewGuid();
var docInformation = GenerateDocumentInformation();
docInformation.DocumentBlobId = docBlobId;
_mockDocumentInformationService
.Setup(x => x.GetDocumentInformation(docId)).ReturnsAsync(docInformation)
.Verifiable();
_mockDocumentInformationService.Setup(x => x.DeleteDocumentInformation(docId))
.ThrowsAsync(new Exception("thrown from unit test")).Verifiable();
Assert.ThrowsAsync<ServiceException>(async () => await _documentService.DeleteDocument(docId));
_mockBlobApiService.Verify(x => x.DeleteDocumentContent(It.IsAny<Guid>()), Times.Never);
_mockDocumentAuditService.Verify(x => x.CreateDocumentDeletedAudit(It.IsAny<Guid>(), It.IsAny<DateTime>()), Times.Never);
_mockDocumentInformationService.Verify();
}
[Test]
public void DeleteDocument_RemoveDocumentContentThrowsException_ServiceException()
{
var docId = Guid.NewGuid();
var docBlobId = Guid.NewGuid();
var docInformation = GenerateDocumentInformation();
docInformation.DocumentBlobId = docBlobId;
_mockDocumentInformationService
.Setup(x => x.GetDocumentInformation(docId)).ReturnsAsync(docInformation)
.Verifiable();
_mockDocumentInformationService.Setup(x => x.DeleteDocumentInformation(docId)).Verifiable();
_mockBlobApiService.Setup(x => x.DeleteDocumentContent(docBlobId))
.ThrowsAsync(new BlobApiServiceException("thrown from test.")).Verifiable();
Assert.ThrowsAsync<ServiceException>(async () => await _documentService.DeleteDocument(docId));
_mockBlobApiService.Verify();
_mockDocumentInformationService.Verify();
}
[Test]
public async Task DeleteDocumentContent_Success()
{
var docId = Guid.NewGuid();
var docBlobId = Guid.NewGuid();
var docInformation = GenerateDocumentInformation();
docInformation.DocumentBlobId = docBlobId;
var request = new DeleteDocumentContentRequest()
{
DocumentId = docId,
Reason = Guid.NewGuid().ToString()
};
_mockDocumentInformationService
.Setup(x => x.GetDocumentInformation(docId)).ReturnsAsync(docInformation)
.Verifiable();
_mockBlobApiService.Setup(x => x.DeleteDocumentContent(docBlobId)).Verifiable();
await _documentService.DeleteDocumentContent(request);
_mockDocumentInformationService.Verify(x => x.GetDocumentInformation(It.IsAny<Guid>()), Times.Once);
_mockBlobApiService.Verify(x => x.DeleteDocumentContent(It.IsAny<Guid>()), Times.Once);
_mockDocumentAuditService.Verify(x => x.CreateDocumentContentDeletedAudit(It.IsAny<Guid>(), It.IsAny<DateTime>(), It.IsAny<string?>()), Times.Once);
}
[Test]
public async Task DeleteDocumentContent_NoInformationPresent_Null()
{
var docId = Guid.NewGuid();
var docBlobId = Guid.NewGuid();
var docInformation = GenerateDocumentInformation();
docInformation.DocumentBlobId = docBlobId;
var request = new DeleteDocumentContentRequest()
{
DocumentId = docId,
Reason = Guid.NewGuid().ToString()
};
_mockDocumentInformationService
.Setup(x => x.GetDocumentInformation(docId)).ReturnsAsync(null as DocumentInformation)
.Verifiable();
await _documentService.DeleteDocumentContent(request);
_mockBlobApiService.Verify(x => x.DeleteDocumentContent(It.IsAny<Guid>()), Times.Never);
_mockDocumentInformationService.Verify(x => x.DeleteDocumentInformation(It.IsAny<Guid>()), Times.Never);
_mockDocumentAuditService.Verify(x => x.CreateDocumentContentDeletedAudit(It.IsAny<Guid>(), It.IsAny<DateTime>(), It.IsAny<string?>()), Times.Never);
_mockDocumentInformationService.Verify();
}
[Test]
public void DeleteDocumentContent_RemoveDocumentContentThrowsException_ServiceException()
{
var docId = Guid.NewGuid();
var docBlobId = Guid.NewGuid();
var docInformation = GenerateDocumentInformation();
docInformation.DocumentBlobId = docBlobId;
var request = new DeleteDocumentContentRequest()
{
DocumentId = docId,
Reason = Guid.NewGuid().ToString()
};
_mockDocumentInformationService
.Setup(x => x.GetDocumentInformation(docId)).ReturnsAsync(docInformation)
.Verifiable();
_mockBlobApiService.Setup(x => x.DeleteDocumentContent(docBlobId))
.ThrowsAsync(new BlobApiServiceException("thrown from test.")).Verifiable();
Assert.ThrowsAsync<ServiceException>(async () => await _documentService.DeleteDocumentContent(request));
_mockBlobApiService.Verify();
_mockDocumentInformationService.Verify();
}
[Test]
public async Task UploadDocument_Success()
{
var docBlobId = Guid.NewGuid();
var uploadRequest = GenerateDocumentRequest();
var docInformation = GenerateDocumentInformation();
_mockBlobApiService.Setup(x => x.UploadDocumentContent(uploadRequest.Content)).ReturnsAsync(docBlobId).Verifiable();
_mockDocumentInformationService
.Setup(x => x.UploadDocumentInformation(It.Is<DocumentInformation>(info =>
info.DocumentBlobId == docBlobId && info.UploadDate.Date == DateTime.UtcNow.Date)))
.ReturnsAsync(docInformation)
.Verifiable();
var result = await _documentService.UploadDocument(uploadRequest);
Assert.AreEqual(docInformation.DocumentId, result.DocumentId);
Assert.AreEqual(uploadRequest.Name, result.Name);
Assert.AreEqual(uploadRequest.Type, result.Type);
Assert.AreEqual(docInformation.Source, result.Source);
Assert.IsNull(docInformation.ScanStatus);
Assert.AreEqual(docInformation.UploadDate, result.UploadDate);
Assert.AreEqual(docInformation.CreatedDate, result.CreatedDate);
_mockBlobApiService.Verify();
_mockDocumentInformationService.Verify();
}
[Test]
public void UploadDocument_ExceptionThrown_DuringSaveDocumentToBlob()
{
var uploadRequest = GenerateDocumentRequest();
_mockBlobApiService.Setup(x => x.UploadDocumentContent(uploadRequest.Content))
.ThrowsAsync(new Exception("Error thrown from unit test.")).Verifiable();
Assert.ThrowsAsync<ServiceException>(async () => await _documentService.UploadDocument(uploadRequest));
_mockBlobApiService.Verify();
_mockDocumentInformationService.Verify(x => x.UploadDocumentInformation(It.IsAny<DocumentInformation>()), Times.Never);
}
[Test]
public void UploadDocument_BlobApiDaoExceptionThrown_DuringSaveDocumentToBlob()
{
var uploadRequest = GenerateDocumentRequest();
_mockBlobApiService.Setup(x => x.UploadDocumentContent(uploadRequest.Content))
.ThrowsAsync(new BlobApiServiceException("Error thrown from unit test.")).Verifiable();
Assert.ThrowsAsync<ServiceException>(async () => await _documentService.UploadDocument(uploadRequest));
_mockBlobApiService.Verify();
_mockDocumentInformationService.Verify(x => x.UploadDocumentInformation(It.IsAny<DocumentInformation>()), Times.Never);
}
[Test]
public void UploadDocument_DocumentInformationServiceExceptionThrown_DuringSaveDocumentInformation()
{
var docBlobId = Guid.NewGuid();
var uploadRequest = GenerateDocumentRequest();
_mockBlobApiService.Setup(x => x.UploadDocumentContent(uploadRequest.Content)).ReturnsAsync(docBlobId).Verifiable();
_mockBlobApiService.Setup(x => x.DeleteDocumentContent(docBlobId)).Verifiable();
_mockDocumentInformationService
.Setup(x => x.UploadDocumentInformation(It.Is<DocumentInformation>(info =>
info.DocumentBlobId == docBlobId && info.UploadDate.Date == DateTime.UtcNow.Date)))
.ThrowsAsync(new DocumentInformationServiceException("Error thrown from unit test."))
.Verifiable();
Assert.ThrowsAsync<ServiceException>(async () => await _documentService.UploadDocument(uploadRequest));
_mockBlobApiService.Verify();
_mockDocumentInformationService.Verify();
}
[Test]
public async Task UpdateDocument_Success()
{
var docBlobId = Guid.NewGuid();
var docId = Guid.NewGuid();
var userId = Guid.NewGuid().ToString();
var updateRequest = GenerateUpdateDocumentRequest();
var docInformation = GenerateDocumentInformation();
docInformation.DocumentId = docId;
docInformation.DocumentBlobId = docBlobId;
_mockDocumentInformationService.Setup(x => x.GetDocumentInformation(docId)).ReturnsAsync(docInformation).Verifiable();
_mockBlobApiService.Setup(x => x.UpdateDocumentContent(docBlobId, updateRequest.Content)).Verifiable();
_mockDocumentInformationService
.Setup(x => x.UpdateDocumentInformation(docId, It.Is<DocumentInformation>(info =>
info.DocumentId == docId && info.DocumentBlobId == docBlobId &&
info.LastModifiedDate!.Value.Date == DateTime.UtcNow.Date))).ReturnsAsync(docInformation)
.Verifiable();
_mockDocumentAuditService.Setup(x => x.CreateDocumentAudits(It.IsAny<DocumentInformation>(),
It.IsAny<DocumentInformation>(), true, It.Is<DateTime>(time => time.Date == DateTime.UtcNow.Date), It.Is<string>(name => name == userId)))
.Verifiable();
await _documentService.UpdateDocument(updateRequest, docId, userId);
_mockBlobApiService.Verify();
_mockDocumentInformationService.Verify();
_mockDocumentAuditService.Verify();
}
[Test]
public async Task UpdateDocument_NoInformation_Null()
{
var docId = Guid.NewGuid();
var updateRequest = GenerateUpdateDocumentRequest();
var userId = Guid.NewGuid().ToString();
_mockDocumentInformationService
.Setup(x => x.GetDocumentInformation(docId)).ReturnsAsync(null as DocumentInformation)
.Verifiable();
var result = await _documentService.UpdateDocument(updateRequest, docId, userId);
Assert.Null(result);
_mockBlobApiService.Verify(x => x.UpdateDocumentContent(It.IsAny<Guid>(), It.IsAny<byte[]>()), Times.Never);
_mockDocumentInformationService.Verify();
}
[Test]
public void UpdateDocument_BlobApiDaoExceptionThrown_DuringSaveDocumentToBlob()
{
var docBlobId = Guid.NewGuid();
var docId = Guid.NewGuid();
var userId = Guid.NewGuid().ToString();
var updateRequest = GenerateUpdateDocumentRequest();
var docInformation = GenerateDocumentInformation();
docInformation.DocumentId = docId;
docInformation.DocumentBlobId = docBlobId;
_mockDocumentInformationService.Setup(x => x.GetDocumentInformation(docId)).ReturnsAsync(docInformation).Verifiable();
_mockBlobApiService.Setup(x => x.UpdateDocumentContent(docBlobId, updateRequest.Content))
.ThrowsAsync(new BlobApiServiceException("Error thrown from unit test.")).Verifiable();
Assert.ThrowsAsync<ServiceException>(async () => await _documentService.UpdateDocument(updateRequest, docId, userId));
_mockBlobApiService.Verify();
// Two calls total with one call to revert document information
_mockDocumentInformationService.Verify(
x => x.UpdateDocumentInformation(It.IsAny<Guid>(), It.IsAny<DocumentInformation>()), Times.Exactly(2));
_mockDocumentInformationService.Verify(
x => x.UpdateDocumentInformation(It.IsAny<Guid>(), docInformation), Times.Once);
}
[Test]
public void UpdateDocument_DocumentAuditRepositoryExceptionThrown_DuringCreateDocumentAudits()
{
var docBlobId = Guid.NewGuid();
var docId = Guid.NewGuid();
var userId = Guid.NewGuid().ToString();
var updateRequest = GenerateUpdateDocumentRequest();
var docInformation = GenerateDocumentInformation();
docInformation.DocumentId = docId;
docInformation.DocumentBlobId = docBlobId;
_mockDocumentInformationService.Setup(x => x.GetDocumentInformation(docId)).ReturnsAsync(docInformation).Verifiable();
_mockDocumentAuditService.Setup(x => x.CreateDocumentAudits(It.IsAny<DocumentInformation>(), It.IsAny<DocumentInformation>(), It.IsAny<bool>(), It.IsAny<DateTime>(), It.Is<string>(name => name == userId)))
.ThrowsAsync(new DocumentAuditServiceException("Error thrown from unit test.")).Verifiable();
Assert.ThrowsAsync<ServiceException>(async () =>
await _documentService.UpdateDocument(updateRequest, docId, userId));
// Two calls total with one call to revert document information
_mockDocumentInformationService.Verify(
x => x.UpdateDocumentInformation(It.IsAny<Guid>(), It.IsAny<DocumentInformation>()), Times.Exactly(2));
_mockDocumentInformationService.Verify(
x => x.UpdateDocumentInformation(It.IsAny<Guid>(), docInformation), Times.Once);
_mockDocumentAuditService.Verify();
}
[Test]
public void UpdateDocument_DocumentInformationRepositoryExceptionThrown_DuringSaveDocumentInformation()
{
var docBlobId = Guid.NewGuid();
var docId = Guid.NewGuid();
var userId = Guid.NewGuid().ToString();
var updateRequest = GenerateUpdateDocumentRequest();
var docInformation = GenerateDocumentInformation();
docInformation.DocumentId = docId;
docInformation.DocumentBlobId = docBlobId;
_mockDocumentInformationService.Setup(x => x.GetDocumentInformation(docId)).ReturnsAsync(docInformation).Verifiable();
_mockDocumentInformationService
.Setup(x => x.UpdateDocumentInformation(docId, It.Is<DocumentInformation>(info =>
info.DocumentBlobId == docBlobId && info.LastModifiedDate!.Value.Date == DateTime.UtcNow.Date)))
.ThrowsAsync(new DocumentInformationServiceException("Error thrown from unit test."))
.Verifiable();
Assert.ThrowsAsync<ServiceException>(async () => await _documentService.UpdateDocument(updateRequest, docId, userId));
// _mockBlobApiDao.Verify();
_mockDocumentInformationService.Verify();
}
[Test]
public void UpdateDocument_ExceptionThrown_ServiceExceptionThrown()
{
var docBlobId = Guid.NewGuid();
var docId = Guid.NewGuid();
var userId = Guid.NewGuid().ToString();
var updateRequest = GenerateUpdateDocumentRequest();
var docInformation = GenerateDocumentInformation();
docInformation.DocumentId = docId;
docInformation.DocumentBlobId = docBlobId;
_mockDocumentInformationService.Setup(x => x.GetDocumentInformation(docId))
.ThrowsAsync(new Exception("Exception thrown from unit test")).Verifiable();
Assert.ThrowsAsync<ServiceException>(async () => await _documentService.UpdateDocument(updateRequest, docId, userId));
_mockBlobApiService.Verify();
_mockDocumentInformationService.Verify();
}
[Test]
public async Task UpdateDocumentContent_Success()
{
var docBlobId = Guid.NewGuid();
var docId = Guid.NewGuid();
var userId = Guid.NewGuid().ToString();
var updateRequest = GenerateUpdateDocumentContentRequest();
var docInformation = GenerateDocumentInformation();
docInformation.DocumentId = docId;
docInformation.DocumentBlobId = docBlobId;
_mockDocumentInformationService.Setup(x => x.GetDocumentInformation(docId)).ReturnsAsync(docInformation).Verifiable();
_mockBlobApiService.Setup(x => x.UpdateDocumentContent(docBlobId, updateRequest.Content)).Verifiable();
_mockDocumentInformationService
.Setup(x => x.UpdateDocumentInformation(docId, It.Is<DocumentInformation>(info =>
info.DocumentId == docId && info.DocumentBlobId == docBlobId &&
info.LastModifiedDate!.Value.Date == DateTime.UtcNow.Date))).ReturnsAsync(docInformation)
.Verifiable();
_mockDocumentAuditService.Setup(x => x.CreateDocumentAudits(It.IsAny<DocumentInformation>(),
It.IsAny<DocumentInformation>(), true, It.Is<DateTime>(time => time.Date == DateTime.UtcNow.Date), It.Is<string>(name => name == userId)))
.Verifiable();
await _documentService.UpdateDocumentContent(updateRequest, docId, userId);
_mockBlobApiService.Verify();
_mockDocumentInformationService.Verify();
_mockDocumentAuditService.Verify();
}
[Test]
public async Task UpdateDocumentContent_NoInformation_Null()
{
var docId = Guid.NewGuid();
var userId = Guid.NewGuid().ToString();
var updateRequest = GenerateUpdateDocumentContentRequest();
_mockDocumentInformationService
.Setup(x => x.GetDocumentInformation(docId)).ReturnsAsync(null as DocumentInformation)
.Verifiable();
var result = await _documentService.UpdateDocumentContent(updateRequest, docId, userId);
Assert.Null(result);
_mockBlobApiService.Verify();
_mockDocumentInformationService.Verify();
}
[Test]
public void UpdateDocumentContent_BlobApiDaoExceptionThrown_DuringUpdateDocumentBlob()
{
var docBlobId = Guid.NewGuid();
var docId = Guid.NewGuid();
var userId = Guid.NewGuid().ToString();
var updateRequest = GenerateUpdateDocumentContentRequest();
var docInformation = GenerateDocumentInformation();
docInformation.DocumentId = docId;
docInformation.DocumentBlobId = docBlobId;
_mockDocumentInformationService.Setup(x => x.GetDocumentInformation(docId)).ReturnsAsync(docInformation).Verifiable();
_mockBlobApiService.Setup(x => x.UpdateDocumentContent(docBlobId, updateRequest.Content))
.ThrowsAsync(new BlobApiServiceException("Error thrown from unit test.")).Verifiable();
Assert.ThrowsAsync<ServiceException>(async () =>
await _documentService.UpdateDocumentContent(updateRequest, docId, userId));
_mockBlobApiService.Verify();
// Two calls total with one call to revert document information
_mockDocumentInformationService.Verify(
x => x.UpdateDocumentInformation(It.IsAny<Guid>(), It.IsAny<DocumentInformation>()), Times.Exactly(2));
_mockDocumentInformationService.Verify(
x => x.UpdateDocumentInformation(It.IsAny<Guid>(), docInformation), Times.Once);
}
[Test]
public void UpdateDocumentContent_DocumentAuditExceptionThrown_DuringCreateDocumentAudits()
{
var docBlobId = Guid.NewGuid();
var docId = Guid.NewGuid();
var userId = Guid.NewGuid().ToString();
var updateRequest = GenerateUpdateDocumentContentRequest();
var docInformation = GenerateDocumentInformation();
docInformation.DocumentId = docId;
docInformation.DocumentBlobId = docBlobId;
_mockDocumentInformationService.Setup(x => x.GetDocumentInformation(docId)).ReturnsAsync(docInformation).Verifiable();
_mockDocumentAuditService.Setup(x => x.CreateDocumentAudits(docInformation, It.IsAny<DocumentInformation>(), It.IsAny<bool>(), It.IsAny<DateTime>(), It.Is<string>(name => name == userId)))
.ThrowsAsync(new DocumentAuditServiceException("Error thrown from unit test.")).Verifiable();
Assert.ThrowsAsync<ServiceException>(async () =>
await _documentService.UpdateDocumentContent(updateRequest, docId, userId));
// Two calls total with one call to revert document information
_mockDocumentInformationService.Verify(
x => x.UpdateDocumentInformation(It.IsAny<Guid>(), It.IsAny<DocumentInformation>()), Times.Exactly(2));
_mockDocumentInformationService.Verify(
x => x.UpdateDocumentInformation(It.IsAny<Guid>(), docInformation), Times.Once);
}
[Test]
public void UpdateDocumentContent_DocumentInformationRepositoryExceptionThrown_DuringUpdateDocumentInformation()
{
var docBlobId = Guid.NewGuid();
var docId = Guid.NewGuid();
var userId = Guid.NewGuid().ToString();
var updateRequest = GenerateUpdateDocumentContentRequest();
var docInformation = GenerateDocumentInformation();
docInformation.DocumentId = docId;
docInformation.DocumentBlobId = docBlobId;
_mockDocumentInformationService.Setup(x => x.GetDocumentInformation(docId)).ReturnsAsync(docInformation).Verifiable();
_mockDocumentInformationService
.Setup(x => x.UpdateDocumentInformation(docId, It.Is<DocumentInformation>(info =>
info.DocumentBlobId == docBlobId && info.LastModifiedDate!.Value.Date == DateTime.UtcNow.Date)))
.ThrowsAsync(new DocumentInformationServiceException("Error thrown from unit test."))
.Verifiable();
Assert.ThrowsAsync<ServiceException>(async () =>
await _documentService.UpdateDocumentContent(updateRequest, docId, userId));
_mockDocumentInformationService.Verify();
}
[Test]
public void UpdateDocumentContent_ExceptionThrown_DuringUpdateDocumentInformation()
{
var docBlobId = Guid.NewGuid();
var docId = Guid.NewGuid();
var userId = Guid.NewGuid().ToString();
var updateRequest = GenerateUpdateDocumentContentRequest();
var docInformation = GenerateDocumentInformation();
docInformation.DocumentId = docId;
docInformation.DocumentBlobId = docBlobId;
_mockDocumentInformationService.Setup(x => x.GetDocumentInformation(docId)).ReturnsAsync(docInformation).Verifiable();
_mockDocumentInformationService
.Setup(x => x.UpdateDocumentInformation(docId, It.Is<DocumentInformation>(info =>
info.DocumentBlobId == docBlobId && info.LastModifiedDate!.Value.Date == DateTime.UtcNow.Date)))
.ThrowsAsync(new Exception("Error thrown from unit test."))
.Verifiable();
Assert.ThrowsAsync<ServiceException>(async () =>
await _documentService.UpdateDocumentContent(updateRequest, docId, userId));
_mockDocumentInformationService.Verify();
}
[Test]
public async Task UpdateDocumentInformation_Success()
{
var docBlobId = Guid.NewGuid();
var docId = Guid.NewGuid();
var userId = Guid.NewGuid().ToString();
var updateRequest = GenerateUpdateDocumentInformationRequest();
var docInformation = GenerateDocumentInformation();
docInformation.DocumentId = docId;
docInformation.DocumentBlobId = docBlobId;
_mockDocumentInformationService.Setup(x => x.GetDocumentInformation(docId)).ReturnsAsync(docInformation).Verifiable();
_mockDocumentInformationService
.Setup(x => x.UpdateDocumentInformation(docId, It.Is<DocumentInformation>(info =>
info.DocumentId == docId && info.DocumentBlobId == docBlobId &&
info.LastModifiedDate!.Value.Date == DateTime.UtcNow.Date))).ReturnsAsync(docInformation)
.Verifiable();
_mockDocumentAuditService.Setup(x => x.CreateDocumentAudits(It.IsAny<DocumentInformation>(),
It.IsAny<DocumentInformation>(), false, It.Is<DateTime>(time => time.Date == DateTime.UtcNow.Date), It.Is<string>(name => name == userId)))
.Verifiable();
await _documentService.UpdateDocumentInformation(updateRequest, docId, userId);
_mockBlobApiService.Verify();
_mockDocumentInformationService.Verify();
_mockDocumentAuditService.Verify();
}
[Test]
public async Task UpdateDocumentInformation_NoInformation_Null()
{
var docId = Guid.NewGuid();
var userId = Guid.NewGuid().ToString();
var updateRequest = GenerateUpdateDocumentInformationRequest();
_mockDocumentInformationService
.Setup(x => x.GetDocumentInformation(docId)).ReturnsAsync(null as DocumentInformation)
.Verifiable();
var result = await _documentService.UpdateDocumentInformation(updateRequest, docId, userId);
Assert.Null(result);
_mockBlobApiService.Verify();
_mockDocumentInformationService.Verify();
}
[Test]
public void UpdateDocumentInformation_DocumentInformationRepositoryExceptionThrown_DuringUpdateDocumentInformation()
{
var docBlobId = Guid.NewGuid();
var docId = Guid.NewGuid();
var userId = Guid.NewGuid().ToString();
var updateRequest = GenerateUpdateDocumentInformationRequest();
var docInformation = GenerateDocumentInformation();
docInformation.DocumentId = docId;
docInformation.DocumentBlobId = docBlobId;
_mockDocumentInformationService.Setup(x => x.GetDocumentInformation(docId)).ReturnsAsync(docInformation).Verifiable();
_mockDocumentInformationService
.Setup(x => x.UpdateDocumentInformation(docId, It.Is<DocumentInformation>(info =>
info.DocumentBlobId == docBlobId && info.LastModifiedDate!.Value.Date == DateTime.UtcNow.Date)))
.ThrowsAsync(new DocumentInformationServiceException("Error thrown from unit test."))
.Verifiable();
Assert.ThrowsAsync<ServiceException>(async () =>
await _documentService.UpdateDocumentInformation(updateRequest, docId, userId));
_mockBlobApiService.Verify();
_mockDocumentInformationService.Verify();
}
[Test]
public void UpdateDocumentInformation_DocumentAuditRepositoryExceptionThrown_DuringCreateDocumentAudits()
{
var docBlobId = Guid.NewGuid();
var docId = Guid.NewGuid();
var userId = Guid.NewGuid().ToString();
var updateRequest = GenerateUpdateDocumentInformationRequest();
var docInformation = GenerateDocumentInformation();
docInformation.DocumentId = docId;
docInformation.DocumentBlobId = docBlobId;
_mockDocumentInformationService.Setup(x => x.GetDocumentInformation(docId)).ReturnsAsync(docInformation).Verifiable();
_mockDocumentAuditService.Setup(x => x.CreateDocumentAudits(docInformation, It.IsAny<DocumentInformation>(), It.IsAny<bool>(), It.IsAny<DateTime>(), It.Is<string>(name => name == userId)))
.ThrowsAsync(new DocumentAuditServiceException("Error thrown from unit test.")).Verifiable();
Assert.ThrowsAsync<ServiceException>(async () =>
await _documentService.UpdateDocumentInformation(updateRequest, docId, userId));
// Two calls total with one call to revert document information
_mockDocumentInformationService.Verify(
x => x.UpdateDocumentInformation(It.IsAny<Guid>(), It.IsAny<DocumentInformation>()), Times.Exactly(2));
_mockDocumentInformationService.Verify(
x => x.UpdateDocumentInformation(It.IsAny<Guid>(), docInformation), Times.Once);
}
[Test]
public void UpdateDocumentInformation_ExceptionThrown_ServiceExceptionThrown()
{
var docBlobId = Guid.NewGuid();
var docId = Guid.NewGuid();
var userId = Guid.NewGuid().ToString();
var updateRequest = GenerateUpdateDocumentInformationRequest();
var docInformation = GenerateDocumentInformation();
docInformation.DocumentId = docId;
docInformation.DocumentBlobId = docBlobId;
_mockDocumentInformationService.Setup(x => x.GetDocumentInformation(docId)).ReturnsAsync(docInformation).Verifiable();
_mockDocumentInformationService
.Setup(x => x.UpdateDocumentInformation(docId, It.Is<DocumentInformation>(info =>
info.DocumentBlobId == docBlobId && info.LastModifiedDate!.Value.Date == DateTime.UtcNow.Date)))
.ThrowsAsync(new Exception("Error thrown from unit test."))
.Verifiable();
Assert.ThrowsAsync<ServiceException>(async () =>
await _documentService.UpdateDocumentInformation(updateRequest, docId, userId));
_mockBlobApiService.Verify();
_mockDocumentInformationService.Verify();
}
[Test]
public async Task GetDocumentInformation_Success()
{
var docId = Guid.NewGuid();
var docInformation = GenerateDocumentInformation();
_mockDocumentInformationService
.Setup(x => x.GetDocumentInformation(docId)).ReturnsAsync(docInformation)
.Verifiable();
var result = await _documentService.GetDocumentInformation(docId);
Assert.NotNull(result);
var returnedDocInfo = result!;
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.CreatedDate, returnedDocInfo.CreatedDate);
Assert.AreEqual(docInformation.Attributes, returnedDocInfo.Attributes);
_mockBlobApiService.Verify();
_mockDocumentInformationService.Verify();
}
[Test]
public async Task GetDocumentInformation_NoInformationPresent_Null()
{
var docId = Guid.NewGuid();
_mockDocumentInformationService
.Setup(x => x.GetDocumentInformation(docId)).ReturnsAsync(null as DocumentInformation)
.Verifiable();
var result = await _documentService.GetDocumentInformation(docId);
Assert.Null(result);
_mockBlobApiService.Verify();
_mockDocumentInformationService.Verify();
}
[Test]
public void GetDocumentInformation_DocumentInformationServiceExceptionThrown_ServiceException()
{
var docId = Guid.NewGuid();
_mockDocumentInformationService
.Setup(x => x.GetDocumentInformation(docId))
.ThrowsAsync(new DocumentInformationServiceException("Thrown from unit test"))
.Verifiable();
Assert.ThrowsAsync<ServiceException>(async () => await _documentService.GetDocumentInformation(docId));
_mockDocumentInformationService.Verify();
}
[Test]
public void GetDocumentInformation_Exception_ServiceException()
{
var docId = Guid.NewGuid();
_mockDocumentInformationService
.Setup(x => x.GetDocumentInformation(docId))
.ThrowsAsync(new Exception("Thrown from Unit Test")).Verifiable();
Assert.ThrowsAsync<ServiceException>(async () => await _documentService.GetDocumentInformation(docId));
_mockDocumentInformationService.Verify();
}
[Test]
public async Task UpdateDocumentScanStatus_Success()
{
var docBlobId = Guid.NewGuid();
var docId = Guid.NewGuid();
var userId = Guid.NewGuid().ToString();
List<Guid> docBlobIds = new()
{
docBlobId
};
var updateRequest = GenerateUpdateDocumentScanStatusRequest(docBlobIds);
var docInformation = GenerateDocumentInformation();
docInformation.DocumentId = docId;
docInformation.DocumentBlobId = docBlobId;
_mockDocumentInformationService.Setup(x => x.GetDocumentInformationByBlobId(docBlobId)).ReturnsAsync(docInformation)
.Verifiable();
_mockDocumentAuditService.Setup(x => x.CreateDocumentAudits(It.IsAny<DocumentInformation>(),
It.IsAny<DocumentInformation>(), false, It.Is<DateTime>(time => time.Date == DateTime.UtcNow.Date), It.Is<string>(name => name == userId)))
.Verifiable();
_mockDocumentInformationService
.Setup(x => x.UpdateDocumentInformation(docId, It.Is<DocumentInformation>(info =>
info.ScanStatus == true
&& info.LastModifiedDate!.Value.Date == DateTime.UtcNow.Date)))
.ReturnsAsync(docInformation)
.Verifiable();
await _documentService.UpdateDocumentScanStatus(true, updateRequest.DocumentBlobIds, userId);
_mockDocumentAuditService.Verify();
_mockBlobApiService.Verify();
_mockDocumentInformationService.Verify();
}
[Test]
public async Task UpdateDocumentScanStatus_FailureThenSuccess()
{
var docBlobIdSuccessful = Guid.NewGuid();
var docBlobIdFailure = Guid.NewGuid();
var docId = Guid.NewGuid();
var userId = Guid.NewGuid().ToString();
List<Guid> docBlobIds = new()
{
docBlobIdFailure,
docBlobIdSuccessful
};
var updateRequest = GenerateUpdateDocumentScanStatusRequest(docBlobIds);
var docInformation = GenerateDocumentInformation();
docInformation.DocumentId = docId;
docInformation.DocumentBlobId = docBlobIdSuccessful;
_mockDocumentInformationService.Setup(x => x.GetDocumentInformationByBlobId(docBlobIdFailure))
.ReturnsAsync((DocumentInformation?)null).Verifiable();
_mockDocumentInformationService.Setup(x => x.GetDocumentInformationByBlobId(docBlobIdSuccessful))
.ReturnsAsync(docInformation).Verifiable();
_mockDocumentAuditService.Setup(x => x.CreateDocumentAudits(It.IsAny<DocumentInformation>(),
It.IsAny<DocumentInformation>(), false, It.Is<DateTime>(time => time.Date == DateTime.UtcNow.Date), It.Is<string>(name => name == userId)))
.Verifiable();
_mockDocumentInformationService
.Setup(x => x.UpdateDocumentInformation(docId, It.Is<DocumentInformation>(info =>
info.ScanStatus == true && info.LastModifiedDate!.Value.Date == DateTime.UtcNow.Date)))
.ReturnsAsync(docInformation)
.Verifiable();
await _documentService.UpdateDocumentScanStatus(true, updateRequest.DocumentBlobIds, userId);
_mockDocumentAuditService.Verify();
_mockDocumentAuditService.Verify(
x => x.CreateDocumentAudits(It.IsAny<DocumentInformation>(), It.IsAny<DocumentInformation>(),
It.IsAny<bool>(), It.IsAny<DateTime>(), It.Is<string>(name => name == userId)), Times.Once);
_mockBlobApiService.Verify();
_mockDocumentInformationService.Verify();
_mockDocumentInformationService.Verify(x => x.UpdateDocumentInformation(docId, It.IsAny<DocumentInformation>()),
Times.Once);
}
[Test]
public async Task UpdateDocumentScanStatus_FailureByServiceExceptionThenSuccess()
{
var docBlobIdSuccessful = Guid.NewGuid();
var docBlobIdFailure = Guid.NewGuid();
var docId = Guid.NewGuid();
var userId = Guid.NewGuid().ToString();
List<Guid> docBlobIds = new()
{
docBlobIdFailure,
docBlobIdSuccessful
};
var updateRequest = GenerateUpdateDocumentScanStatusRequest(docBlobIds);
var docInformation = GenerateDocumentInformation();
docInformation.DocumentId = docId;
docInformation.DocumentBlobId = docBlobIdSuccessful;
_mockDocumentInformationService.Setup(x => x.GetDocumentInformationByBlobId(docBlobIdFailure))
.ThrowsAsync(new DocumentInformationServiceException("thrown from unit test")).Verifiable();
_mockDocumentInformationService.Setup(x => x.GetDocumentInformationByBlobId(docBlobIdSuccessful))
.ReturnsAsync(docInformation).Verifiable();
_mockDocumentAuditService.Setup(x => x.CreateDocumentAudits(It.IsAny<DocumentInformation>(),
It.IsAny<DocumentInformation>(), false, It.Is<DateTime>(time => time.Date == DateTime.UtcNow.Date), It.Is<string>(name => name == userId)))
.Verifiable();
_mockDocumentInformationService
.Setup(x => x.UpdateDocumentInformation(docId, It.Is<DocumentInformation>(info =>
info.ScanStatus == true && info.LastModifiedDate!.Value.Date == DateTime.UtcNow.Date)))
.ReturnsAsync(docInformation)
.Verifiable();
await _documentService.UpdateDocumentScanStatus(true, updateRequest.DocumentBlobIds, userId);
_mockDocumentAuditService.Verify();
_mockDocumentAuditService.Verify(
x => x.CreateDocumentAudits(It.IsAny<DocumentInformation>(), It.IsAny<DocumentInformation>(),
It.IsAny<bool>(), It.IsAny<DateTime>(), It.Is<string>(name => name == userId)), Times.Once);
_mockBlobApiService.Verify();
_mockDocumentInformationService.Verify();
_mockDocumentInformationService.Verify(x => x.UpdateDocumentInformation(docId, It.IsAny<DocumentInformation>()),
Times.Once);
}
[Test]
public async Task UpdateDocumentScanStatus_FailureByExceptionThenSuccess()
{
var docBlobIdSuccessful = Guid.NewGuid();
var docBlobIdFailure = Guid.NewGuid();
var docId = Guid.NewGuid();
var userId = Guid.NewGuid().ToString();
List<Guid> docBlobIds = new()
{
docBlobIdFailure,
docBlobIdSuccessful
};
var updateRequest = GenerateUpdateDocumentScanStatusRequest(docBlobIds);
var docInformation = GenerateDocumentInformation();
docInformation.DocumentId = docId;
docInformation.DocumentBlobId = docBlobIdSuccessful;
_mockDocumentInformationService.Setup(x => x.GetDocumentInformationByBlobId(docBlobIdFailure))
.ThrowsAsync(new Exception("thrown from unit test")).Verifiable();
_mockDocumentInformationService.Setup(x => x.GetDocumentInformationByBlobId(docBlobIdSuccessful))
.ReturnsAsync(docInformation).Verifiable();
_mockDocumentAuditService.Setup(x => x.CreateDocumentAudits(It.IsAny<DocumentInformation>(),
It.IsAny<DocumentInformation>(), false, It.Is<DateTime>(time => time.Date == DateTime.UtcNow.Date), It.Is<string>(name => name == userId)))
.Verifiable();
_mockDocumentInformationService
.Setup(x => x.UpdateDocumentInformation(docId, It.Is<DocumentInformation>(info =>
info.ScanStatus == true && info.LastModifiedDate!.Value.Date == DateTime.UtcNow.Date)))
.ReturnsAsync(docInformation)
.Verifiable();
await _documentService.UpdateDocumentScanStatus(true, updateRequest.DocumentBlobIds, userId);
_mockDocumentAuditService.Verify();
_mockDocumentAuditService.Verify(
x => x.CreateDocumentAudits(It.IsAny<DocumentInformation>(), It.IsAny<DocumentInformation>(),
It.IsAny<bool>(), It.IsAny<DateTime>(), It.Is<string>(name => name == userId)), Times.Once);
_mockBlobApiService.Verify();
_mockDocumentInformationService.Verify();
_mockDocumentInformationService.Verify(x => x.UpdateDocumentInformation(docId, It.IsAny<DocumentInformation>()),
Times.Once);
}
[Test]
public async Task UpdateDocumentScanStatus_FailureByDocumentInformationServiceException()
{
var docBlobId1 = Guid.NewGuid();
var docBlobId2 = Guid.NewGuid();
var docId = Guid.NewGuid();
var userId = Guid.NewGuid().ToString();
List<Guid> docBlobIds = new()
{
docBlobId1,
docBlobId2
};
var updateRequest = GenerateUpdateDocumentScanStatusRequest(docBlobIds);
var docInformation = GenerateDocumentInformation();
docInformation.DocumentId = docId;
docInformation.DocumentBlobId = docBlobId1;
_mockDocumentInformationService.Setup(x => x.GetDocumentInformationByBlobId(It.IsAny<Guid>()))
.ReturnsAsync(docInformation).Verifiable();
_mockDocumentInformationService
.Setup(x => x.UpdateDocumentInformation(It.IsAny<Guid>(), It.IsAny<DocumentInformation>()))
.ReturnsAsync(docInformation)
.Verifiable();
_mockDocumentAuditService.Setup(x => x.CreateDocumentAudits(It.IsAny<DocumentInformation>(), It.IsAny<DocumentInformation>(), It.IsAny<bool>(), It.IsAny<DateTime>(), It.IsAny<string>()))
.ThrowsAsync(new DocumentAuditServiceException("Error thrown from unit test.")).Verifiable();
await _documentService.UpdateDocumentScanStatus(true, updateRequest.DocumentBlobIds, userId);
// There should be two calls to repository, one initial update and one to rollback for both Ids
_mockDocumentInformationService.Verify(x => x.UpdateDocumentInformation(It.IsAny<Guid>(), It.IsAny<DocumentInformation>()),
Times.Exactly(4));
}
[Test]
public async Task SearchDocumentInformation_Success()
{
var searchRequest = GenerateDocumentInformationFilter();
var random = new Random(DateTime.Now.Millisecond);
var skip = random.Next();
var take = random.Next();
var docInformation = new List<DocumentInformation> { GenerateDocumentInformation() };
_mockDocumentInformationService
.Setup(x => x.SearchDocumentInformation(searchRequest, skip, take, null))
.ReturnsAsync(docInformation);
var result = await _documentService.SearchDocumentInformation(searchRequest, skip, take);
DocumentInformationResponse AsInformationResponse(DocumentInformation information)
{
var response = new DocumentInformationResponse();
foreach (var property in typeof(DocumentInformation).GetProperties())
{
response.GetType().GetProperty(property.Name)?.SetValue(response, property.GetValue(information));
}
return response;
}
docInformation.Select(AsInformationResponse).Should().BeEquivalentTo(result);
}
[Test]
public void SearchDocumentInformation_DocumentInformationException_ServiceExceptionThrown()
{
var searchRequest = GenerateDocumentInformationFilter();
_mockDocumentInformationService
.Setup(x => x.SearchDocumentInformation(searchRequest, It.IsAny<int?>(), It.IsAny<int?>(), null))
.ThrowsAsync(new DocumentInformationServiceException("Thrown from unit test"))
.Verifiable();
Assert.ThrowsAsync<ServiceException>(async () => await _documentService.SearchDocumentInformation(searchRequest));
_mockDocumentInformationService.Verify();
}
[Test]
public void SearchDocumentInformation_UnexpectedException_ServiceExceptionThrown()
{
var searchRequest = GenerateDocumentInformationFilter();
_mockDocumentInformationService
.Setup(x => x.SearchDocumentInformation(searchRequest, It.IsAny<int?>(), It.IsAny<int?>(), null))
.ThrowsAsync(new Exception("Thrown from unit test"))
.Verifiable();
Assert.ThrowsAsync<ServiceException>(async () => await _documentService.SearchDocumentInformation(searchRequest));
_mockDocumentInformationService.Verify();
}
[Test]
public void CountDocuments_DocumentServiceThrowsDocumentInformationServiceException_ThrowsServiceException()
{
var filter = new DocumentInformationFilter();
_mockDocumentInformationService
.Setup(s => s.CountDocuments(filter))
.ThrowsAsync(new DocumentInformationServiceException(string.Empty));
Assert.ThrowsAsync<ServiceException>(() => _documentService.CountDocuments(filter));
}
[Test]
public void CountDocuments_DocumentServiceThrowsException_ThrowsServiceException()
{
var filter = new DocumentInformationFilter();
_mockDocumentInformationService
.Setup(s => s.CountDocuments(filter))
.ThrowsAsync(new Exception());
Assert.ThrowsAsync<ServiceException>(() => _documentService.CountDocuments(filter));
}
[Test]
public async Task CountDocuments_DocumentServiceReturnsCount_ReturnsCount()
{
var filter = new DocumentInformationFilter();
var expectedCount = DateTime.Now.Millisecond;
_mockDocumentInformationService
.Setup(s => s.CountDocuments(filter))
.ReturnsAsync(expectedCount);
var count = await _documentService.CountDocuments(filter);
Assert.AreEqual(expectedCount, count);
}
[Test]
public async Task GetDocumentWithNumber_InformationServiceReturnsNull_ReturnsNull()
{
var number = DateTime.Now.Millisecond;
var documentId = Guid.NewGuid();
_mockDocumentInformationService
.Setup(s => s.GetDocumentInformation(documentId))
.ReturnsAsync((DocumentInformation?)null);
var document = await _documentService.GetDocument(number, documentId);
Assert.IsNull(document);
}
[Test]
public void GetDocumentWithNumber_InformationServiceThrowsDocumentInformationServiceException_ThrowsServiceException()
{
var number = DateTime.Now.Millisecond;
var documentId = Guid.NewGuid();
_mockDocumentInformationService
.Setup(s => s.GetDocumentInformation(documentId))
.ThrowsAsync(new DocumentInformationServiceException(string.Empty));
Assert.ThrowsAsync<ServiceException>(() => _documentService.GetDocument(number, documentId));
}
[Test]
public void GetDocumentWithNumber_InformationServiceThrowsBlobApiServiceException_ThrowsServiceException()
{
var number = DateTime.Now.Millisecond;
var documentId = Guid.NewGuid();
_mockDocumentInformationService
.Setup(s => s.GetDocumentInformation(documentId))
.ThrowsAsync(new BlobApiServiceException(string.Empty));
Assert.ThrowsAsync<ServiceException>(() => _documentService.GetDocument(number, documentId));
}
[Test]
public void GetDocumentWithNumber_InformationServiceThrowsException_ThrowsServiceException()
{
var number = DateTime.Now.Millisecond;
var documentId = Guid.NewGuid();
_mockDocumentInformationService
.Setup(s => s.GetDocumentInformation(documentId))
.ThrowsAsync(new Exception());
Assert.ThrowsAsync<ServiceException>(() => _documentService.GetDocument(number, documentId));
}
[Test]
public async Task GetDocumentWithNumber_InformationServiceReturnsDocumentWithNeitherPersonNorOrgNumberMatchingNumber_ReturnsNull()
{
var number = DateTime.Now.Millisecond;
var documentId = Guid.NewGuid();
var information = new DocumentInformation
{
PersonNumbers = new [] { (number + 1).ToString(), (number - 1).ToString() },
OrganizationNumbers = new [] { (number + 2).ToString(), (number - 2).ToString() }
};
_mockDocumentInformationService
.Setup(s => s.GetDocumentInformation(documentId))
.ReturnsAsync(information);
var document = await DocumentServiceFactory(DocumentService.Ownership.CheckPersonOrOrgNumber.Strategy).GetDocument(number, documentId);
Assert.IsNull(document);
}
[Test]
public async Task GetDocumentWithNumberUnchecked_InformationServiceReturnsDocumentWithNeitherPersonNorOrgNumberMatchingNumber_ReturnsDocumentAnyways()
{
var number = DateTime.Now.Millisecond;
var documentId = Guid.NewGuid();
var information = new DocumentInformation
{
PersonNumbers = new [] { (number + 1).ToString(), (number - 1).ToString() },
OrganizationNumbers = new [] { (number + 2).ToString(), (number - 2).ToString() }
};
var content = Encoding.Default.GetBytes(Guid.NewGuid().ToString());
_mockDocumentInformationService
.Setup(s => s.GetDocumentInformation(documentId))
.ReturnsAsync(information);
_mockBlobApiService
.Setup(b => b.GetDocumentContent(information.DocumentBlobId))
.ReturnsAsync(content);
var document = await DocumentServiceFactory(DocumentService.Ownership.Unchecked.Strategy).GetDocument(number, documentId);
Assert.AreSame(content, document.Content);
Assert.AreEqual(information.DocumentId, document.DocumentInformation.DocumentId);
Assert.AreEqual(information.DocumentBlobId, document.DocumentInformation.DocumentBlobId);
Assert.AreEqual(information.TraceId, document.DocumentInformation.TraceId);
Assert.AreEqual(information.Name, document.DocumentInformation.Name);
Assert.AreEqual(information.Type, document.DocumentInformation.Type);
Assert.AreEqual(information.Source, document.DocumentInformation.Source);
Assert.AreEqual(information.DataSource, document.DocumentInformation.DataSource);
Assert.AreEqual(information.FileSize, document.DocumentInformation.FileSize);
Assert.AreEqual(information.ScanStatus, document.DocumentInformation.ScanStatus);
Assert.AreEqual(information.PersonNumbers, document.DocumentInformation.PersonNumbers);
Assert.AreEqual(information.DocumentId, document.DocumentInformation.DocumentId);
Assert.AreEqual(information.OrganizationNumbers, document.DocumentInformation.OrganizationNumbers);
Assert.AreEqual(information.AccountNumber, document.DocumentInformation.AccountNumber);
Assert.AreEqual(information.Attributes, document.DocumentInformation.Attributes);
Assert.AreEqual(information.UploadDate, document.DocumentInformation.UploadDate);
Assert.AreEqual(information.LastModifiedDate, document.DocumentInformation.LastModifiedDate);
Assert.AreEqual(information.CreatedDate, document.DocumentInformation.CreatedDate);
}
private static IEnumerable<object[]> InformationMatchingNumber()
{
var number = DateTime.Now.Millisecond;
var matchingPerson = new DocumentInformation
{
PersonNumbers = new [] { (number + 1).ToString(), number.ToString() },
OrganizationNumbers = new [] { (number + 2).ToString(), (number - 2).ToString() }
};
var matchingOrganization = new DocumentInformation
{
PersonNumbers = new [] { (number + 1).ToString(), (number - 1).ToString() },
OrganizationNumbers = new [] { (number + 2).ToString(), number.ToString() }
};
yield return new object[] { number, matchingPerson };
yield return new object[] { number, matchingOrganization };
}
[TestCaseSource(nameof(InformationMatchingNumber))]
public async Task GetDocumentWithNumber_InformationServiceReturnsDocumentMatchingNumber_ReturnsDocumentResponse(long number, DocumentInformation information)
{
var documentId = Guid.NewGuid();
var content = Encoding.Default.GetBytes(Guid.NewGuid().ToString());
information.DocumentBlobId = Guid.NewGuid();
_mockDocumentInformationService
.Setup(s => s.GetDocumentInformation(documentId))
.ReturnsAsync(information);
_mockBlobApiService
.Setup(b => b.GetDocumentContent(information.DocumentBlobId))
.ReturnsAsync(content);
var document = await _documentService.GetDocument(number, documentId);
Assert.AreSame(content, document.Content);
Assert.AreEqual(information.DocumentId, document.DocumentInformation.DocumentId);
Assert.AreEqual(information.DocumentBlobId, document.DocumentInformation.DocumentBlobId);
Assert.AreEqual(information.TraceId, document.DocumentInformation.TraceId);
Assert.AreEqual(information.Name, document.DocumentInformation.Name);
Assert.AreEqual(information.Type, document.DocumentInformation.Type);
Assert.AreEqual(information.Source, document.DocumentInformation.Source);
Assert.AreEqual(information.DataSource, document.DocumentInformation.DataSource);
Assert.AreEqual(information.FileSize, document.DocumentInformation.FileSize);
Assert.AreEqual(information.ScanStatus, document.DocumentInformation.ScanStatus);
Assert.AreEqual(information.PersonNumbers, document.DocumentInformation.PersonNumbers);
Assert.AreEqual(information.DocumentId, document.DocumentInformation.DocumentId);
Assert.AreEqual(information.OrganizationNumbers, document.DocumentInformation.OrganizationNumbers);
Assert.AreEqual(information.AccountNumber, document.DocumentInformation.AccountNumber);
Assert.AreEqual(information.Attributes, document.DocumentInformation.Attributes);
Assert.AreEqual(information.UploadDate, document.DocumentInformation.UploadDate);
Assert.AreEqual(information.LastModifiedDate, document.DocumentInformation.LastModifiedDate);
Assert.AreEqual(information.CreatedDate, document.DocumentInformation.CreatedDate);
}
[Test]
public async Task GetDocumentByBlobId_InformationServiceReturnsNull_ReturnsNull()
{
var number = DateTime.Now.Millisecond;
var documentId = Guid.NewGuid();
_mockDocumentInformationService
.Setup(s => s.GetDocumentInformationByBlobId(documentId))
.ReturnsAsync((DocumentInformation?)null);
var content = await _documentService.GetDocumentByBlobId(number, documentId);
Assert.IsNull(content);
}
[Test]
public void GetDocumentByBlobId_InformationServiceThrowsBlobApiServiceException_ThrowsServiceException()
{
var number = DateTime.Now.Millisecond;
var documentId = Guid.NewGuid();
_mockDocumentInformationService
.Setup(s => s.GetDocumentInformationByBlobId(documentId))
.ThrowsAsync(new BlobApiServiceException(string.Empty));
Assert.ThrowsAsync<ServiceException>(() => _documentService.GetDocumentByBlobId(number, documentId));
}
[Test]
public void GetDocumentByBlobId_InformationServiceThrowsException_ThrowsServiceException()
{
var number = DateTime.Now.Millisecond;
var documentId = Guid.NewGuid();
_mockDocumentInformationService
.Setup(s => s.GetDocumentInformationByBlobId(documentId))
.ThrowsAsync(new Exception());
Assert.ThrowsAsync<ServiceException>(() => _documentService.GetDocumentByBlobId(number, documentId));
}
[Test]
public async Task GetDocumentByBlobId_InformationServiceReturnsDocumentWithNeitherPersonNorOrgNumberMatchingNumber_ReturnsNull()
{
var number = DateTime.Now.Millisecond;
var documentId = Guid.NewGuid();
var information = new DocumentInformation
{
PersonNumbers = new [] { (number + 1).ToString(), (number - 1).ToString() },
OrganizationNumbers = new [] { (number + 2).ToString(), (number - 2).ToString() }
};
_mockDocumentInformationService
.Setup(s => s.GetDocumentInformationByBlobId(documentId))
.ReturnsAsync(information);
var content = await DocumentServiceFactory(DocumentService.Ownership.CheckPersonOrOrgNumber.Strategy).GetDocumentByBlobId(number, documentId);
Assert.IsNull(content);
}
[Test]
public async Task GetDocumentByBlobIdWithNumberUnchecked_InformationServiceReturnsDocumentWithNeitherPersonNorOrgNumberMatchingNumber_ReturnsBytesAnyways()
{
var number = DateTime.Now.Millisecond;
var documentId = Guid.NewGuid();
var information = new DocumentInformation
{
PersonNumbers = new [] { (number + 1).ToString(), (number - 1).ToString() },
OrganizationNumbers = new [] { (number + 2).ToString(), (number - 2).ToString() }
};
var content = Encoding.Default.GetBytes(Guid.NewGuid().ToString());
_mockDocumentInformationService
.Setup(s => s.GetDocumentInformationByBlobId(documentId))
.ReturnsAsync(information);
_mockBlobApiService
.Setup(b => b.GetDocumentContent(documentId))
.ReturnsAsync(content);
var documentContent = await DocumentServiceFactory(DocumentService.Ownership.Unchecked.Strategy).GetDocumentByBlobId(number, documentId);
Assert.AreSame(content, documentContent);
}
[Test]
public async Task GetDocumentsForCleanup_Success()
{
var documents = new List<Guid> { Guid.NewGuid(), Guid.NewGuid(), Guid.NewGuid() };
_mockDocumentInformationService.Setup(x => x.GetDocumentsForCleanup(It.IsAny<GetDocumentCleanupRequest>()))
.ReturnsAsync(documents).Verifiable();
var result = await _documentService.GetDocumentsForCleanup(It.IsAny<GetDocumentCleanupRequest>());
Assert.IsNotEmpty(result);
Assert.IsNotNull(result);
Assert.AreSame(documents, result);
_mockDocumentInformationService.Verify(x => x.GetDocumentsForCleanup(It.IsAny<GetDocumentCleanupRequest>()), Times.Once);
}
[Test]
public async Task GetDocumentsForCleanup_Success_NoDocumentsFound()
{
var documents = new List<Guid>();
_mockDocumentInformationService.Setup(x => x.GetDocumentsForCleanup(It.IsAny<GetDocumentCleanupRequest>()))
.ReturnsAsync(documents).Verifiable();
var result = await _documentService.GetDocumentsForCleanup(It.IsAny<GetDocumentCleanupRequest>());
Assert.IsEmpty(result);
Assert.IsNotNull(result);
Assert.AreSame(documents, result);
_mockDocumentInformationService.Verify(x => x.GetDocumentsForCleanup(It.IsAny<GetDocumentCleanupRequest>()), Times.Once);
}
[Test]
public void GetDocumentsForCleanup_ThrowsServiceException()
{
_mockDocumentInformationService
.Setup(s => s.GetDocumentsForCleanup(It.IsAny<GetDocumentCleanupRequest>()))
.ThrowsAsync(new Exception("Test"));
Assert.ThrowsAsync<ServiceException>(() => _documentService.GetDocumentsForCleanup(It.IsAny<GetDocumentCleanupRequest>()));
}
[Test]
public void GetDocumentsForCleanup_ThrowsDocumentInformationServiceException()
{
_mockDocumentInformationService
.Setup(s => s.GetDocumentsForCleanup(It.IsAny<GetDocumentCleanupRequest>()))
.ThrowsAsync(new DocumentInformationServiceException("Test"));
Assert.ThrowsAsync<ServiceException>(() => _documentService.GetDocumentsForCleanup(It.IsAny<GetDocumentCleanupRequest>()));
}
[TestCaseSource(nameof(InformationMatchingNumber))]
public async Task GetDocumentByBlobId_InformationServiceReturnsDocumentMatchingNumber_GetsAndReturnsDocumentBytes(long number, DocumentInformation information)
{
var documentId = Guid.NewGuid();
var content = Encoding.Default.GetBytes(Guid.NewGuid().ToString());
_mockDocumentInformationService
.Setup(s => s.GetDocumentInformationByBlobId(documentId))
.ReturnsAsync(information);
_mockBlobApiService
.Setup(b => b.GetDocumentContent(documentId))
.ReturnsAsync(content);
var documentContent = await _documentService.GetDocumentByBlobId(number, documentId);
Assert.AreSame(content, documentContent);
}
private static DocumentInformationFilter GenerateDocumentInformationFilter()
{
return new DocumentInformationFilter
{
Name = "TestSearch"
};
}
private static DocumentRequest GenerateDocumentRequest()
{
return new DocumentRequest
{
Name = "TestFile.txt",
Type = "Test",
Content = new byte[10],
Source = "Unit Test",
CreatedDate = DateTime.Parse("2022-02-28T17:32:35.093Z"),
Attributes = new Dictionary<string, string>
{
{ "IsTest", "true" }
}
};
}
private static UpdateDocumentRequest GenerateUpdateDocumentRequest()
{
return new UpdateDocumentRequest
{
Content = new byte[10],
Name = "TestFile.txt",
Type = "Test",
UpdateSource = "Unit Test",
Attributes = new Dictionary<string, string>
{
{ "IsTest", "true" }
}
};
}
private static UpdateDocumentInformationRequest GenerateUpdateDocumentInformationRequest()
{
return new UpdateDocumentInformationRequest
{
Name = "TestFile.txt",
Type = "Test",
UpdateSource = "Unit Test",
Attributes = new Dictionary<string, string>
{
{ "IsTest", "true" }
}
};
}
private static UpdateDocumentScanStatusRequest GenerateUpdateDocumentScanStatusRequest(List<Guid> docBlobIds)
{
return new UpdateDocumentScanStatusRequest
{
ScanStatus = true,
DocumentBlobIds = docBlobIds
};
}
private static UpdateDocumentContentRequest GenerateUpdateDocumentContentRequest()
{
return new UpdateDocumentContentRequest
{
Content = new byte[10]
};
}
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"),
CreatedDate = DateTime.Parse("2022-02-28T17:32:35.093Z"),
Attributes = new Dictionary<string, string>
{
{ "IsTest", "true" },
{ "Owner", "Me Myself" }
}
};
}
}