using System; using System.Collections.Generic; using System.Security.Claims; using System.Text; using System.Threading.Tasks; using LCU.API.Internal.Document.Controllers; using LCU.API.Internal.Document.Services; using LCU.Library.Internal.DocumentIngestorModels.Models.Pagination; 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 Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.Azure.Cosmos; using Moq; using NUnit.Framework; namespace LCU.API.Internal.DocumentTest.Controllers; public class DocumentControllerTest { private DocumentController _controller = null!; private Mock _mockDocumentService = null!; private Mock _mockDocumentAuditService = null!; [SetUp] public void Setup() { _mockDocumentService = new Mock(); _mockDocumentAuditService = new Mock(); _controller = new DocumentController(_mockDocumentService.Object, _mockDocumentAuditService.Object) { ControllerContext = { HttpContext = new DefaultHttpContext() } }; } [Test] public async Task Get_Document_OK() { var docId = Guid.NewGuid(); var documentResponse = GenerateDocumentResponse(docId); _mockDocumentService.Setup(service => service.GetDocument(docId)).ReturnsAsync(documentResponse).Verifiable(); var result = await _controller.GetDocumentById(docId); Assert.NotNull(result); var okResult = result as OkObjectResult; Assert.NotNull(okResult); Assert.AreEqual(200, okResult!.StatusCode); var okValue = okResult.Value as DocumentResponse; Assert.NotNull(okValue); var expectedDocContent = documentResponse.Content; var expectedDocInformation = documentResponse.DocumentInformation; var returnedDocInformation = okValue!.DocumentInformation; Assert.AreEqual(expectedDocContent, okValue.Content); Assert.AreEqual(expectedDocInformation.DocumentId, returnedDocInformation.DocumentId); Assert.AreEqual(expectedDocInformation.Name, returnedDocInformation.Name); Assert.AreEqual(expectedDocInformation.Type, returnedDocInformation.Type); Assert.AreEqual(expectedDocInformation.Source, returnedDocInformation.Source); Assert.AreEqual(expectedDocInformation.ScanStatus, returnedDocInformation.ScanStatus); Assert.AreEqual(expectedDocInformation.UploadDate, returnedDocInformation.UploadDate); Assert.AreEqual(expectedDocInformation.LastModifiedDate, returnedDocInformation.LastModifiedDate); Assert.AreEqual(expectedDocInformation.Attributes, returnedDocInformation.Attributes); _mockDocumentService.Verify(); } [Test] public async Task Get_Document_Null_NoContent() { var docId = Guid.NewGuid(); _mockDocumentService.Setup(service => service.GetDocument(docId)).ReturnsAsync((DocumentResponse)null).Verifiable(); var result = await _controller.GetDocumentById(docId); Assert.NotNull(result); var noContent = result as NotFoundObjectResult; Assert.NotNull(noContent); Assert.AreEqual(404, noContent!.StatusCode); _mockDocumentService.Verify(); } [Test] public async Task Get_Document_ServiceException() { var docId = Guid.NewGuid(); _mockDocumentService.Setup(service => service.GetDocument(docId)) .ThrowsAsync(new ServiceException("Thrown from unit test")).Verifiable(); var result = await _controller.GetDocumentById(docId); Assert.NotNull(result); var okResult = result as ObjectResult; Assert.NotNull(okResult); Assert.AreEqual(500, okResult!.StatusCode); _mockDocumentService.Verify(); } [Test] public async Task Get_Document_UnexpectedException() { var docId = Guid.NewGuid(); _mockDocumentService.Setup(service => service.GetDocument(docId)) .ThrowsAsync(new Exception("Thrown from unit test")).Verifiable(); var result = await _controller.GetDocumentById(docId); Assert.NotNull(result); var okResult = result as StatusCodeResult; Assert.NotNull(okResult); Assert.AreEqual(500, okResult!.StatusCode); _mockDocumentService.Verify(); } [Test] public async Task Get_DocumentByBlobId_Null_NoContent() { var blobId = Guid.NewGuid(); _mockDocumentService.Setup(service => service.GetDocumentByBlobId(blobId)).ReturnsAsync((byte[]?)null).Verifiable(); var result = await _controller.GetDocumentByBlobId(blobId); Assert.NotNull(result); var noContent = result as NotFoundObjectResult; Assert.NotNull(noContent); Assert.AreEqual(404, noContent!.StatusCode); _mockDocumentService.Verify(); } [Test] public async Task Get_DocumentByBlobId_ServiceException() { var blobId = Guid.NewGuid(); _mockDocumentService.Setup(service => service.GetDocumentByBlobId(blobId)) .ThrowsAsync(new ServiceException("Thrown from unit test")).Verifiable(); var result = await _controller.GetDocumentByBlobId(blobId); Assert.NotNull(result); var okResult = result as ObjectResult; Assert.NotNull(okResult); Assert.AreEqual(500, okResult!.StatusCode); _mockDocumentService.Verify(); } [Test] public async Task Get_DocumentByBlobId_UnexpectedException() { var blobId = Guid.NewGuid(); _mockDocumentService.Setup(service => service.GetDocumentByBlobId(blobId)) .ThrowsAsync(new Exception("Thrown from unit test")).Verifiable(); var result = await _controller.GetDocumentByBlobId(blobId); Assert.NotNull(result); var okResult = result as StatusCodeResult; Assert.NotNull(okResult); Assert.AreEqual(500, okResult!.StatusCode); _mockDocumentService.Verify(); } [Test] public async Task Post_SearchDocumentInformation_Content_Ok() { var random = new Random(DateTime.Now.Millisecond); var searchRequest = new DocumentInformationFilteredSearch { Filter = new DocumentInformationFilter(), Skip = random.Next(), Take = random.Next(), SortDefinition = new List { new() { Field = "CreatedDate", Position = 0, IsDescending = false } } }; var docIdOne = Guid.NewGuid(); var docIdTwo = Guid.NewGuid(); var documentInformationResponse = new List { new() { DocumentId = docIdOne }, new() { DocumentId = docIdTwo } }; _mockDocumentService .Setup(x => x.SearchDocumentInformation(searchRequest.Filter, searchRequest.Skip, searchRequest.Take, searchRequest.SortDefinition)) .ReturnsAsync(documentInformationResponse) .Verifiable(); var result = await _controller.PostDocumentInformationByFilter(searchRequest); var okResult = result as OkObjectResult; Assert.NotNull(okResult); Assert.AreEqual(200, okResult!.StatusCode); var documentsInformation = okResult.Value as List; CollectionAssert.AreEqual(documentInformationResponse, documentsInformation); } [Test] public async Task Post_SearchDocumentInformation_Null_NoContent() { var documentInformationResponse = new List { Capacity = 0 }; var searchRequest = new DocumentInformationFilteredSearch { Filter = new DocumentInformationFilter() }; _mockDocumentService .Setup(x => x.SearchDocumentInformation(searchRequest.Filter, It.IsAny(), It.IsAny(), It.IsAny?>())) .ReturnsAsync(documentInformationResponse) .Verifiable(); var result = await _controller.PostDocumentInformationByFilter(searchRequest); var noContentResult = result as NoContentResult; Assert.NotNull(noContentResult); Assert.AreEqual(204, noContentResult!.StatusCode); _mockDocumentService.Verify(); } [Test] public async Task Post_SearchDocumentInformation_ServiceException() { var searchRequest = new DocumentInformationFilteredSearch { Filter = new DocumentInformationFilter() }; _mockDocumentService .Setup(x => x.SearchDocumentInformation(searchRequest.Filter, It.IsAny(), It.IsAny(), It.IsAny?>())) .ThrowsAsync(new ServiceException("Thrown from unit test")) .Verifiable(); var result = await _controller.PostDocumentInformationByFilter(searchRequest); var okResult = result as ObjectResult; Assert.NotNull(okResult); Assert.AreEqual(500, okResult!.StatusCode); _mockDocumentService.Verify(); } [Test] public async Task Post_SearchDocumentInformation_UnexpectedException() { var searchRequest = new DocumentInformationFilteredSearch { Filter = new DocumentInformationFilter() }; _mockDocumentService .Setup(x => x.SearchDocumentInformation(searchRequest.Filter, It.IsAny(), It.IsAny(), It.IsAny?>())) .ThrowsAsync(new Exception("Thrown from unit test")) .Verifiable(); var result = await _controller.PostDocumentInformationByFilter(searchRequest); var statusCodeResult = result as StatusCodeResult; Assert.NotNull(statusCodeResult); Assert.AreEqual(500, statusCodeResult!.StatusCode); _mockDocumentService.Verify(); } [Test] public async Task Get_DocumentAudit_Content_Ok() { var docId = Guid.NewGuid(); var documentAuditResponse = new List { new DocumentFieldAuditResponse { DocumentId = docId, Source = "Unit Test", Date = DateTime.Parse("2022-04-28T17:32:35.093Z"), UserId = "Test", Field = "Test", PreviousValue = "0", CurrentValue = "1" }, new DocumentActionAuditResponse { DocumentId = docId, Source = "Unit Test", Date = DateTime.Parse("2022-04-28T17:32:35.093Z"), UserId = "Test", Action = DocumentAction.Download, ActionDescription = "Testing" } }; _mockDocumentAuditService .Setup(service => service.GetDocumentAudits(docId)) .ReturnsAsync(documentAuditResponse); var result = await _controller.GetDocumentAuditInformationById(docId); Assert.NotNull(result); var okResult = result as OkObjectResult; Assert.NotNull(okResult); Assert.AreEqual(200, okResult!.StatusCode); Assert.AreSame(documentAuditResponse, okResult.Value!); } [Test] public async Task Get_DocumentAudit_NoContent_Ok() { var docId = Guid.NewGuid(); var documentAuditResponse = new List(); _mockDocumentAuditService .Setup(service => service.GetDocumentAudits(docId)) .ReturnsAsync(documentAuditResponse); var result = await _controller.GetDocumentAuditInformationById(docId); Assert.NotNull(result); var okResult = result as NoContentResult; Assert.NotNull(okResult); Assert.AreEqual(204, okResult!.StatusCode); } [Test] public async Task Get_DocumentAudit_ServiceException() { var docId = Guid.NewGuid(); _mockDocumentAuditService.Setup(service => service.GetDocumentAudits(docId)) .ThrowsAsync(new ServiceException("Thrown from unit test")).Verifiable(); var result = await _controller.GetDocumentAuditInformationById(docId); Assert.NotNull(result); var okResult = result as ObjectResult; Assert.NotNull(okResult); Assert.AreEqual(500, okResult!.StatusCode); _mockDocumentService.Verify(); } [Test] public async Task Get_DocumentAudit_UnexpectedException() { var docId = Guid.NewGuid(); _mockDocumentAuditService.Setup(service => service.GetDocumentAudits(docId)) .ThrowsAsync(new Exception("Thrown from unit test")).Verifiable(); var result = await _controller.GetDocumentAuditInformationById(docId); Assert.NotNull(result); var okResult = result as StatusCodeResult; Assert.NotNull(okResult); Assert.AreEqual(500, okResult!.StatusCode); _mockDocumentAuditService.Verify(); } [Test] public async Task GetDocumentAuditInformationByIds_AuditServiceThrowsServiceException_Returns500() { var documentIds = new[] { Guid.NewGuid(), Guid.NewGuid() }; _mockDocumentAuditService .Setup(s => s.GetDocumentAudits(documentIds)) .ThrowsAsync(new ServiceException(string.Empty)); var response = await _controller.GetDocumentAuditInformationByIds(documentIds); Assert.IsInstanceOf(response); Assert.AreEqual(StatusCodes.Status500InternalServerError, ((ObjectResult)response).StatusCode); } [Test] public async Task GetDocumentAuditInformationByIds_AuditServiceThrowsException_Returns500() { var documentIds = new[] { Guid.NewGuid(), Guid.NewGuid() }; _mockDocumentAuditService .Setup(s => s.GetDocumentAudits(documentIds)) .ThrowsAsync(new Exception()); var response = await _controller.GetDocumentAuditInformationByIds(documentIds); Assert.IsInstanceOf(response); Assert.AreEqual(StatusCodes.Status500InternalServerError, ((StatusCodeResult)response).StatusCode); } [Test] public async Task GetDocumentAuditInformationByIds_AuditServiceDoesNotThrow_ReturnsOkWithLogs() { var documentIds = new[] { Guid.NewGuid(), Guid.NewGuid() }; var auditLogs = new List { new DocumentFieldAuditResponse(), new DocumentActionAuditResponse() }; _mockDocumentAuditService .Setup(s => s.GetDocumentAudits(documentIds)) .ReturnsAsync(auditLogs); var response = await _controller.GetDocumentAuditInformationByIds(documentIds); Assert.IsInstanceOf(response); Assert.AreEqual(StatusCodes.Status200OK, ((OkObjectResult)response).StatusCode); Assert.AreEqual(auditLogs, ((OkObjectResult)response).Value); } [Test] public async Task ExternalActionPerformed_ExternalActionNotAllowed_Returns400() { var documentId = Guid.NewGuid(); var request = new DocumentActionNotificationRequest { Action = (DocumentAction)DateTime.Now.Millisecond, Source = Guid.NewGuid().ToString() }; var response = await _controller.ExternalActionPerformed(documentId, request); Assert.IsInstanceOf(response); Assert.AreEqual(StatusCodes.Status400BadRequest, ((ObjectResult)response).StatusCode); } [Test] public async Task ExternalActionPerformed_AuditServiceThrowsException_Returns500() { var userId = Guid.NewGuid().ToString(); var documentId = Guid.NewGuid(); var request = new DocumentActionNotificationRequest { Action = DocumentAction.Download, Source = Guid.NewGuid().ToString() }; _controller.HttpContext.User = new ClaimsPrincipal(new ClaimsIdentity(new[] { new Claim("name", userId) })); _mockDocumentAuditService .Setup(s => s.AuditAction(documentId, userId, request.Source, request.Action)) .ThrowsAsync(new Exception()); var response = await _controller.ExternalActionPerformed(documentId, request); Assert.IsInstanceOf(response); Assert.AreEqual(StatusCodes.Status500InternalServerError, ((StatusCodeResult)response).StatusCode); } [Test] public async Task ExternalActionPerformed_AuditServiceDoesNotThrow_ReturnsOk() { var userId = Guid.NewGuid().ToString(); var documentId = Guid.NewGuid(); var request = new DocumentActionNotificationRequest { Action = DocumentAction.Download, Source = Guid.NewGuid().ToString() }; _controller.HttpContext.User = new ClaimsPrincipal(new ClaimsIdentity(new[] { new Claim("name", userId) })); _mockDocumentAuditService .Setup(s => s.AuditAction(documentId, userId, request.Source, request.Action)) .Verifiable(); var response = await _controller.ExternalActionPerformed(documentId, request); _mockDocumentAuditService.Verify(); Assert.IsInstanceOf(response); Assert.AreEqual(StatusCodes.Status200OK, ((OkResult)response).StatusCode); } [Test] public async Task Upload_Document_OK() { var docId = Guid.NewGuid(); var docRequest = GenerateDocumentRequest(); var docInformation = new DocumentInformationResponse { DocumentId = docId }; _mockDocumentService.Setup(service => service.UploadDocument(docRequest)).ReturnsAsync(docInformation) .Verifiable(); var result = await _controller.UploadDocument(docRequest); Assert.NotNull(result); var okResult = result as CreatedAtActionResult; Assert.NotNull(okResult); Assert.AreEqual(201, okResult!.StatusCode); var okValue = okResult.Value as DocumentInformationResponse; Assert.NotNull(okValue); Assert.AreEqual(docInformation.DocumentId, okValue!.DocumentId); _mockDocumentService.Verify(); } [Test] public async Task Upload_Document_ServiceException() { var docRequest = GenerateDocumentRequest(); _mockDocumentService.Setup(service => service.UploadDocument(docRequest)) .ThrowsAsync(new ServiceException("Thrown from unit test")).Verifiable(); var result = await _controller.UploadDocument(docRequest); Assert.NotNull(result); var okResult = result as ObjectResult; Assert.NotNull(okResult); Assert.AreEqual(500, okResult!.StatusCode); _mockDocumentService.Verify(); } [Test] public async Task Upload_Document_UnexpectedException() { var docRequest = GenerateDocumentRequest(); _mockDocumentService.Setup(service => service.UploadDocument(docRequest)) .ThrowsAsync(new Exception("Thrown from unit test")).Verifiable(); var result = await _controller.UploadDocument(docRequest); Assert.NotNull(result); var okResult = result as StatusCodeResult; Assert.NotNull(okResult); Assert.AreEqual(500, okResult!.StatusCode); _mockDocumentService.Verify(); } [Test] public async Task Update_Document_OK() { var userId = Guid.NewGuid().ToString(); var docId = Guid.NewGuid(); var updateDocRequest = GenerateUpdateDocumentRequest(); var docInformation = new DocumentInformationResponse { DocumentId = docId }; _controller.HttpContext.User = new ClaimsPrincipal(new ClaimsIdentity(new[] { new Claim("name", userId) })); _mockDocumentService.Setup(service => service.UpdateDocument(updateDocRequest, docId, userId)) .ReturnsAsync(docInformation).Verifiable(); var result = await _controller.UpdateDocument(updateDocRequest, docId); Assert.NotNull(result); var okResult = result as OkObjectResult; Assert.NotNull(okResult); Assert.AreEqual(200, okResult!.StatusCode); var okValue = okResult.Value as DocumentInformationResponse; Assert.NotNull(okValue); Assert.AreEqual(docInformation.DocumentId, okValue!.DocumentId); _mockDocumentService.Verify(); } [Test] public async Task Update_Document_Null_NotFound() { var userId = Guid.NewGuid().ToString(); var docId = Guid.NewGuid(); var updateDocRequest = GenerateUpdateDocumentRequest(); _controller.HttpContext.User = new ClaimsPrincipal(new ClaimsIdentity(new[] { new Claim("name", userId) })); _mockDocumentService.Setup(service => service.UpdateDocument(updateDocRequest, docId, userId)) .ReturnsAsync((DocumentInformationResponse?)null).Verifiable(); var result = await _controller.UpdateDocument(updateDocRequest, docId); Assert.NotNull(result); var okResult = result as NotFoundObjectResult; Assert.NotNull(okResult); Assert.AreEqual(404, okResult!.StatusCode); _mockDocumentService.Verify(); } [Test] public async Task Update_Document_ServiceException() { var userId = Guid.NewGuid().ToString(); var docId = Guid.NewGuid(); var updateDocRequest = GenerateUpdateDocumentRequest(); _controller.HttpContext.User = new ClaimsPrincipal(new ClaimsIdentity(new[] { new Claim("name", userId) })); _mockDocumentService.Setup(service => service.UpdateDocument(updateDocRequest, docId, userId)) .ThrowsAsync(new ServiceException("Thrown from unit test")).Verifiable(); var result = await _controller.UpdateDocument(updateDocRequest, docId); Assert.NotNull(result); var okResult = result as ObjectResult; Assert.NotNull(okResult); Assert.AreEqual(500, okResult!.StatusCode); _mockDocumentService.Verify(); } [Test] public async Task Update_Document_UnexpectedException() { var userId = Guid.NewGuid().ToString(); var docId = Guid.NewGuid(); var updateDocRequest = GenerateUpdateDocumentRequest(); _controller.HttpContext.User = new ClaimsPrincipal(new ClaimsIdentity(new[] { new Claim("name", userId) })); _mockDocumentService.Setup(service => service.UpdateDocument(updateDocRequest, docId, userId)) .ThrowsAsync(new Exception("Thrown from unit test")).Verifiable(); var result = await _controller.UpdateDocument(updateDocRequest, docId); Assert.NotNull(result); var okResult = result as StatusCodeResult; Assert.NotNull(okResult); Assert.AreEqual(500, okResult!.StatusCode); _mockDocumentService.Verify(); } [Test] public async Task Update_DocumentInformation_OK() { var userId = Guid.NewGuid().ToString(); var docId = Guid.NewGuid(); var updateDocInformationRequest = GenerateUpdateDocumentInformationRequest(); var docInformation = new DocumentInformationResponse { DocumentId = docId }; _controller.HttpContext.User = new ClaimsPrincipal(new ClaimsIdentity(new[] { new Claim("name", userId) })); _mockDocumentService.Setup(service => service.UpdateDocumentInformation(updateDocInformationRequest, docId, userId)) .ReturnsAsync(docInformation).Verifiable(); var result = await _controller.UpdateDocumentInformation(updateDocInformationRequest, docId); Assert.NotNull(result); var okResult = result as OkObjectResult; Assert.NotNull(okResult); Assert.AreEqual(200, okResult!.StatusCode); var okValue = okResult.Value as DocumentInformationResponse; Assert.NotNull(okValue); Assert.AreEqual(docInformation.DocumentId, okValue!.DocumentId); _mockDocumentService.Verify(); } [Test] public async Task Update_DocumentInformation_Null_NotFound() { var userId = Guid.NewGuid().ToString(); var docId = Guid.NewGuid(); var updateDocInformationRequest = GenerateUpdateDocumentInformationRequest(); _controller.HttpContext.User = new ClaimsPrincipal(new ClaimsIdentity(new[] { new Claim("name", userId) })); _mockDocumentService.Setup(service => service.UpdateDocumentInformation(updateDocInformationRequest, docId, userId)) .ReturnsAsync((DocumentInformationResponse?)null).Verifiable(); var result = await _controller.UpdateDocumentInformation(updateDocInformationRequest, docId); Assert.NotNull(result); var okResult = result as NotFoundObjectResult; Assert.NotNull(okResult); Assert.AreEqual(404, okResult!.StatusCode); _mockDocumentService.Verify(); } [Test] public async Task Update_DocumentInformation_ServiceException() { var userId = Guid.NewGuid().ToString(); var docId = Guid.NewGuid(); var updateDocInformationRequest = GenerateUpdateDocumentInformationRequest(); _controller.HttpContext.User = new ClaimsPrincipal(new ClaimsIdentity(new[] { new Claim("name", userId) })); _mockDocumentService.Setup(service => service.UpdateDocumentInformation(updateDocInformationRequest, docId, userId)) .ThrowsAsync(new ServiceException("Thrown from unit test")).Verifiable(); var result = await _controller.UpdateDocumentInformation(updateDocInformationRequest, docId); Assert.NotNull(result); var okResult = result as ObjectResult; Assert.NotNull(okResult); Assert.AreEqual(500, okResult!.StatusCode); _mockDocumentService.Verify(); } [Test] public async Task Update_DocumentInformation_UnexpectedException() { var userId = Guid.NewGuid().ToString(); var docId = Guid.NewGuid(); var updateDocInformationRequest = GenerateUpdateDocumentInformationRequest(); _controller.HttpContext.User = new ClaimsPrincipal(new ClaimsIdentity(new[] { new Claim("name", userId) })); _mockDocumentService.Setup(service => service.UpdateDocumentInformation(updateDocInformationRequest, docId, userId)) .ThrowsAsync(new Exception("Thrown from unit test")).Verifiable(); var result = await _controller.UpdateDocumentInformation(updateDocInformationRequest, docId); Assert.NotNull(result); var okResult = result as StatusCodeResult; Assert.NotNull(okResult); Assert.AreEqual(500, okResult!.StatusCode); _mockDocumentService.Verify(); } [Test] public async Task Get_DocumentInformation_OK() { var docId = Guid.NewGuid(); var documentResponse = GenerateDocumentResponse(docId).DocumentInformation; _mockDocumentService.Setup(service => service.GetDocumentInformation(docId)).ReturnsAsync(documentResponse).Verifiable(); var result = await _controller.GetDocumentInformation(docId); Assert.NotNull(result); var okResult = result as OkObjectResult; Assert.NotNull(okResult); Assert.AreEqual(200, okResult!.StatusCode); var okValue = okResult.Value as DocumentInformationResponse; Assert.NotNull(okValue); var expectedDocInformation = documentResponse; var returnedDocInformation = okValue!; Assert.AreEqual(expectedDocInformation.DocumentId, returnedDocInformation.DocumentId); Assert.AreEqual(expectedDocInformation.Name, returnedDocInformation.Name); Assert.AreEqual(expectedDocInformation.Type, returnedDocInformation.Type); Assert.AreEqual(expectedDocInformation.Source, returnedDocInformation.Source); Assert.AreEqual(expectedDocInformation.ScanStatus, returnedDocInformation.ScanStatus); Assert.AreEqual(expectedDocInformation.UploadDate, returnedDocInformation.UploadDate); Assert.AreEqual(expectedDocInformation.LastModifiedDate, returnedDocInformation.LastModifiedDate); Assert.AreEqual(expectedDocInformation.Attributes, returnedDocInformation.Attributes); _mockDocumentService.Verify(); } [Test] public async Task Get_DocumentInformation_Null_NotFound() { var docId = Guid.NewGuid(); _mockDocumentService.Setup(service => service.GetDocumentInformation(docId)).ReturnsAsync((DocumentInformationResponse)null).Verifiable(); var result = await _controller.GetDocumentInformation(docId); Assert.NotNull(result); var notFound = result as NotFoundObjectResult; Assert.NotNull(notFound); Assert.AreEqual(404, notFound!.StatusCode); _mockDocumentService.Verify(); } [Test] public async Task Get_DocumentInformation_ServiceException() { var docId = Guid.NewGuid(); _mockDocumentService.Setup(service => service.GetDocumentInformation(docId)) .ThrowsAsync(new ServiceException("Thrown from unit test")).Verifiable(); var result = await _controller.GetDocumentInformation(docId); Assert.NotNull(result); var okResult = result as ObjectResult; Assert.NotNull(okResult); Assert.AreEqual(500, okResult!.StatusCode); _mockDocumentService.Verify(); } [Test] public async Task Get_DocumentInformation_UnexpectedException() { var docId = Guid.NewGuid(); _mockDocumentService.Setup(service => service.GetDocumentInformation(docId)) .ThrowsAsync(new Exception("Thrown from unit test")).Verifiable(); var result = await _controller.GetDocumentInformation(docId); Assert.NotNull(result); var okResult = result as StatusCodeResult; Assert.NotNull(okResult); Assert.AreEqual(500, okResult!.StatusCode); _mockDocumentService.Verify(); } [Test] public async Task Update_DocumentSetStatus_OK() { var userId = Guid.NewGuid().ToString(); var updateDocumentScanStatusRequest = GenerateUpdateDocumentScanStatusRequest(); _controller.HttpContext.User = new ClaimsPrincipal(new ClaimsIdentity(new[] { new Claim("name", userId) })); _mockDocumentService.Setup(service => service.UpdateDocumentScanStatus(true, updateDocumentScanStatusRequest.DocumentBlobIds, userId)) .Verifiable(); var result = await _controller.UpdateDocumentScanStatus(updateDocumentScanStatusRequest); Assert.NotNull(result); var okResult = result as OkResult; Assert.NotNull(okResult); Assert.AreEqual(200, okResult!.StatusCode); _mockDocumentService.Verify(); } [Test] public async Task Update_DocumentSetStatus_ServiceException() { var userId = Guid.NewGuid().ToString(); var updateDocumentScanStatusRequest = GenerateUpdateDocumentScanStatusRequest(); _controller.HttpContext.User = new ClaimsPrincipal(new ClaimsIdentity(new[] { new Claim("name", userId) })); _mockDocumentService.Setup(service => service.UpdateDocumentScanStatus(true, updateDocumentScanStatusRequest.DocumentBlobIds, userId)) .ThrowsAsync(new ServiceException("Thrown from unit test")).Verifiable(); var result = await _controller.UpdateDocumentScanStatus(updateDocumentScanStatusRequest); Assert.NotNull(result); var okResult = result as ObjectResult; Assert.NotNull(okResult); Assert.AreEqual(500, okResult!.StatusCode); _mockDocumentService.Verify(); } [Test] public async Task Update_DocumentSetStatus_UnexpectedException() { var userId = Guid.NewGuid().ToString(); var updateDocumentScanStatusRequest = GenerateUpdateDocumentScanStatusRequest(); _controller.HttpContext.User = new ClaimsPrincipal(new ClaimsIdentity(new[] { new Claim("name", userId) })); _mockDocumentService.Setup(service => service.UpdateDocumentScanStatus(true, updateDocumentScanStatusRequest.DocumentBlobIds, userId)) .ThrowsAsync(new Exception("Thrown from unit test")).Verifiable(); var result = await _controller.UpdateDocumentScanStatus(updateDocumentScanStatusRequest); Assert.NotNull(result); var okResult = result as StatusCodeResult; Assert.NotNull(okResult); Assert.AreEqual(500, okResult!.StatusCode); _mockDocumentService.Verify(); } [Test] public async Task Update_DocumentContent_OK() { var userId = Guid.NewGuid().ToString(); var docId = Guid.NewGuid(); var updateDocContentRequest = GenerateUpdateDocumentContentRequest(); var docInformation = new DocumentInformationResponse { DocumentId = docId }; _controller.HttpContext.User = new ClaimsPrincipal(new ClaimsIdentity(new[] { new Claim("name", userId) })); _mockDocumentService.Setup(service => service.UpdateDocumentContent(updateDocContentRequest, docId, userId)) .ReturnsAsync(docInformation).Verifiable(); var result = await _controller.UpdateDocumentContent(updateDocContentRequest, docId); Assert.NotNull(result); var okResult = result as OkObjectResult; Assert.NotNull(okResult); Assert.AreEqual(200, okResult!.StatusCode); var okValue = okResult.Value as DocumentInformationResponse; Assert.NotNull(okValue); Assert.AreEqual(docInformation.DocumentId, okValue!.DocumentId); _mockDocumentService.Verify(); } [Test] public async Task Update_DocumentContent_Null_NotFound() { var userId = Guid.NewGuid().ToString(); var docId = Guid.NewGuid(); var updateDocContentRequest = GenerateUpdateDocumentContentRequest(); _controller.HttpContext.User = new ClaimsPrincipal(new ClaimsIdentity(new[] { new Claim("name", userId) })); _mockDocumentService.Setup(service => service.UpdateDocumentContent(updateDocContentRequest, docId, userId)) .ReturnsAsync((DocumentInformationResponse?)null).Verifiable(); var result = await _controller.UpdateDocumentContent(updateDocContentRequest, docId); Assert.NotNull(result); var okResult = result as NotFoundObjectResult; Assert.NotNull(okResult); Assert.AreEqual(404, okResult!.StatusCode); _mockDocumentService.Verify(); } [Test] public async Task Update_DocumentContent_ServiceException() { var userId = Guid.NewGuid().ToString(); var docId = Guid.NewGuid(); var updateDocContentRequest = GenerateUpdateDocumentContentRequest(); _controller.HttpContext.User = new ClaimsPrincipal(new ClaimsIdentity(new[] { new Claim("name", userId) })); _mockDocumentService.Setup(service => service.UpdateDocumentContent(updateDocContentRequest, docId, userId)) .ThrowsAsync(new ServiceException("Thrown from unit test")).Verifiable(); var result = await _controller.UpdateDocumentContent(updateDocContentRequest, docId); Assert.NotNull(result); var okResult = result as ObjectResult; Assert.NotNull(okResult); Assert.AreEqual(500, okResult!.StatusCode); _mockDocumentService.Verify(); } [Test] public async Task Update_DocumentContent_UnexpectedException() { var userId = Guid.NewGuid().ToString(); var docId = Guid.NewGuid(); var updateDocContentRequest = GenerateUpdateDocumentContentRequest(); _controller.HttpContext.User = new ClaimsPrincipal(new ClaimsIdentity(new[] { new Claim("name", userId) })); _mockDocumentService.Setup(service => service.UpdateDocumentContent(updateDocContentRequest, docId, userId)) .ThrowsAsync(new Exception("Thrown from unit test")).Verifiable(); var result = await _controller.UpdateDocumentContent(updateDocContentRequest, docId); Assert.NotNull(result); var okResult = result as StatusCodeResult; Assert.NotNull(okResult); Assert.AreEqual(500, okResult!.StatusCode); _mockDocumentService.Verify(); } [Test] public async Task Delete_Document_OK() { var docId = Guid.NewGuid(); _mockDocumentService.Setup(service => service.DeleteDocument(docId)).Verifiable(); var result = await _controller.DeleteDocument(docId); Assert.NotNull(result); var okResult = result as OkResult; Assert.AreEqual(200, okResult!.StatusCode); _mockDocumentService.Verify(); } [Test] public async Task Delete_Document_ServiceException() { var docId = Guid.NewGuid(); _mockDocumentService.Setup(service => service.DeleteDocument(docId)) .ThrowsAsync(new ServiceException("Thrown from unit test")).Verifiable(); var result = await _controller.DeleteDocument(docId); Assert.NotNull(result); var okResult = result as ObjectResult; Assert.NotNull(okResult); Assert.AreEqual(500, okResult!.StatusCode); _mockDocumentService.Verify(); } [Test] public async Task Delete_Document_UnexpectedException() { var docId = Guid.NewGuid(); _mockDocumentService.Setup(service => service.DeleteDocument(docId)) .ThrowsAsync(new Exception("Thrown from unit test")).Verifiable(); var result = await _controller.DeleteDocument(docId); Assert.NotNull(result); var okResult = result as StatusCodeResult; Assert.NotNull(okResult); Assert.AreEqual(500, okResult!.StatusCode); _mockDocumentService.Verify(); } [Test] public async Task Delete_DocumentContent_OK() { var request = new DeleteDocumentContentRequest() { DocumentId = Guid.NewGuid(), Reason = Guid.NewGuid().ToString() }; _mockDocumentService.Setup(service => service.DeleteDocumentContent(request)).Verifiable(); var result = await _controller.DeleteDocumentContent(request); Assert.NotNull(result); var okResult = result as OkResult; Assert.AreEqual(200, okResult!.StatusCode); _mockDocumentService.Verify(); } [Test] public async Task Delete_DocumentContent_ServiceException() { var request = new DeleteDocumentContentRequest() { DocumentId = Guid.NewGuid(), Reason = Guid.NewGuid().ToString() }; _mockDocumentService.Setup(service => service.DeleteDocumentContent(request)) .ThrowsAsync(new ServiceException("Thrown from unit test")).Verifiable(); var result = await _controller.DeleteDocumentContent(request); Assert.NotNull(result); var okResult = result as ObjectResult; Assert.NotNull(okResult); Assert.AreEqual(500, okResult!.StatusCode); _mockDocumentService.Verify(); } [Test] public async Task Delete_DocumentContent_UnexpectedException() { var request = new DeleteDocumentContentRequest() { DocumentId = Guid.NewGuid(), Reason = Guid.NewGuid().ToString() }; _mockDocumentService.Setup(service => service.DeleteDocumentContent(request)) .ThrowsAsync(new Exception("Thrown from unit test")).Verifiable(); var result = await _controller.DeleteDocumentContent(request); Assert.NotNull(result); var okResult = result as StatusCodeResult; Assert.NotNull(okResult); Assert.AreEqual(500, okResult!.StatusCode); _mockDocumentService.Verify(); } [Test] public async Task CountDocuments_ServiceThrowsServiceException_Returns500() { var filter = new DocumentInformationFilter(); _mockDocumentService .Setup(d => d.CountDocuments(filter)) .ThrowsAsync(new ServiceException(string.Empty)); var response = await _controller.CountDocuments(filter); Assert.IsInstanceOf(response); Assert.AreEqual(StatusCodes.Status500InternalServerError, ((StatusCodeResult)response).StatusCode); } [Test] public async Task CountDocuments_ServiceThrowsException_Returns500() { var filter = new DocumentInformationFilter(); _mockDocumentService .Setup(d => d.CountDocuments(filter)) .ThrowsAsync(new Exception()); var response = await _controller.CountDocuments(filter); Assert.IsInstanceOf(response); Assert.AreEqual(StatusCodes.Status500InternalServerError, ((StatusCodeResult)response).StatusCode); } [Test] public async Task CountDocuments_ServiceDoesNotThrow_ReturnsOkAndCount() { var filter = new DocumentInformationFilter(); var expectedCount = DateTime.Now.Millisecond; _mockDocumentService .Setup(d => d.CountDocuments(filter)) .ReturnsAsync(expectedCount); var response = await _controller.CountDocuments(filter); Assert.IsInstanceOf(response); Assert.AreEqual(StatusCodes.Status200OK, ((OkObjectResult)response).StatusCode); Assert.AreEqual(expectedCount, ((OkObjectResult)response).Value); } [Test] public async Task GetMemberDocumentById_DocumentServiceReturnsNull_Returns404() { var number = DateTime.Now.Millisecond; var documentId = Guid.NewGuid(); _mockDocumentService .Setup(d => d.GetDocument(number, documentId)) .ReturnsAsync((DocumentResponse?)null); var response = await _controller.GetMemberDocumentById(number, documentId); Assert.IsInstanceOf(response); } [Test] public async Task GetMemberDocumentById_DocumentServiceReturnsDocumentResponse_Returns200WithDocumentResponse() { var number = DateTime.Now.Millisecond; var documentId = Guid.NewGuid(); var document = new DocumentResponse(); _mockDocumentService .Setup(d => d.GetDocument(number, documentId)) .ReturnsAsync(document); var response = await _controller.GetMemberDocumentById(number, documentId); Assert.IsInstanceOf(response); Assert.AreEqual(StatusCodes.Status200OK, ((ObjectResult)response).StatusCode); Assert.AreSame(document, ((OkObjectResult)response).Value); } [Test] public async Task GetMemberDocumentById_DocumentServiceThrowsServiceException_Returns500() { var number = DateTime.Now.Millisecond; var documentId = Guid.NewGuid(); _mockDocumentService .Setup(d => d.GetDocument(number, documentId)) .ThrowsAsync(new ServiceException(string.Empty)); var response = await _controller.GetMemberDocumentById(number, documentId); Assert.IsInstanceOf(response); Assert.AreEqual(StatusCodes.Status500InternalServerError, ((ObjectResult)response).StatusCode); } [Test] public async Task GetMemberDocumentById_DocumentServiceThrowsException_Returns500() { var number = DateTime.Now.Millisecond; var documentId = Guid.NewGuid(); _mockDocumentService .Setup(d => d.GetDocument(number, documentId)) .ThrowsAsync(new Exception()); var response = await _controller.GetMemberDocumentById(number, documentId); Assert.IsInstanceOf(response); Assert.AreEqual(StatusCodes.Status500InternalServerError, ((StatusCodeResult)response).StatusCode); } [Test] public async Task GetDocumentContentByBlobId_DocumentServiceReturnsNull_Returns404() { var documentId = Guid.NewGuid(); _mockDocumentService .Setup(d => d.GetDocumentByBlobId(documentId)) .ReturnsAsync((byte[]?)null); var response = await _controller.GetDocumentContentByBlobId(documentId); Assert.IsInstanceOf(response); } [Test] public async Task GetDocumentContentByBlobId_DocumentServiceReturnsBytes_Returns200WithBytes() { var documentId = Guid.NewGuid(); var bytes = Encoding.Default.GetBytes(Guid.NewGuid().ToString()); _mockDocumentService .Setup(d => d.GetDocumentByBlobId(documentId)) .ReturnsAsync(bytes); var response = await _controller.GetDocumentContentByBlobId(documentId); Assert.IsInstanceOf(response); Assert.AreEqual(StatusCodes.Status200OK, ((ObjectResult)response).StatusCode); Assert.AreSame(bytes, ((OkObjectResult)response).Value); } [Test] public async Task GetDocumentContentByBlobId_DocumentServiceThrowsServiceException_Returns500() { var documentId = Guid.NewGuid(); _mockDocumentService .Setup(d => d.GetDocumentByBlobId(documentId)) .ThrowsAsync(new ServiceException(string.Empty)); var response = await _controller.GetDocumentContentByBlobId(documentId); Assert.IsInstanceOf(response); Assert.AreEqual(StatusCodes.Status500InternalServerError, ((ObjectResult)response).StatusCode); } [Test] public async Task GetDocumentContentByBlobId_DocumentServiceThrowsException_Returns500() { var documentId = Guid.NewGuid(); _mockDocumentService .Setup(d => d.GetDocumentByBlobId(documentId)) .ThrowsAsync(new Exception()); var response = await _controller.GetDocumentContentByBlobId(documentId); Assert.IsInstanceOf(response); Assert.AreEqual(StatusCodes.Status500InternalServerError, ((StatusCodeResult)response).StatusCode); } [Test] public async Task GetMemberDocumentContentByBlobId_DocumentServiceReturnsNull_Returns404() { var documentId = Guid.NewGuid(); var number = DateTime.Now.Millisecond; _mockDocumentService .Setup(d => d.GetDocumentByBlobId(number, documentId)) .ReturnsAsync((byte[]?)null); var response = await _controller.GetMemberDocumentContentByBlobId(number, documentId); Assert.IsInstanceOf(response); } [Test] public async Task GetMemberDocumentContentByBlobId_DocumentServiceReturnsBytes_Returns200WithBytes() { var documentId = Guid.NewGuid(); var number = DateTime.Now.Millisecond; var bytes = Encoding.Default.GetBytes(Guid.NewGuid().ToString()); _mockDocumentService .Setup(d => d.GetDocumentByBlobId(number, documentId)) .ReturnsAsync(bytes); var response = await _controller.GetMemberDocumentContentByBlobId(number, documentId); Assert.IsInstanceOf(response); Assert.AreEqual(StatusCodes.Status200OK, ((ObjectResult)response).StatusCode); Assert.AreSame(bytes, ((OkObjectResult)response).Value); } [Test] public async Task GetMemberDocumentContentByBlobId_DocumentServiceThrowsServiceException_Returns500() { var documentId = Guid.NewGuid(); var number = DateTime.Now.Millisecond; _mockDocumentService .Setup(d => d.GetDocumentByBlobId(number, documentId)) .ThrowsAsync(new ServiceException(string.Empty)); var response = await _controller.GetMemberDocumentContentByBlobId(number, documentId); Assert.IsInstanceOf(response); Assert.AreEqual(StatusCodes.Status500InternalServerError, ((ObjectResult)response).StatusCode); } [Test] public async Task GetMemberDocumentContentByBlobId_DocumentServiceThrowsException_Returns500() { var documentId = Guid.NewGuid(); var number = DateTime.Now.Millisecond; _mockDocumentService .Setup(d => d.GetDocumentByBlobId(number, documentId)) .ThrowsAsync(new Exception()); var response = await _controller.GetMemberDocumentContentByBlobId(number, documentId); Assert.IsInstanceOf(response); Assert.AreEqual(StatusCodes.Status500InternalServerError, ((StatusCodeResult)response).StatusCode); } [Test] public async Task GetDocumentsForCleanup_Success() { var documents = new List { Guid.NewGuid(), Guid.NewGuid(), Guid.NewGuid() }; _mockDocumentService.Setup(x => x.GetDocumentsForCleanup(It.IsAny())) .ReturnsAsync(documents).Verifiable(); var response = await _controller.GetDocumentsForCleanup(It.IsAny()); Assert.AreSame(((OkObjectResult)response).Value, documents); Assert.IsInstanceOf(response); Assert.AreEqual(StatusCodes.Status200OK, ((OkObjectResult)response).StatusCode); _mockDocumentService.Verify(x => x.GetDocumentsForCleanup(It.IsAny()), Times.Once); } [Test] public async Task GetDocumentsForCleanup_Success_NoDocumentsFound() { var documents = new List(); _mockDocumentService.Setup(x => x.GetDocumentsForCleanup(It.IsAny())) .ReturnsAsync(documents).Verifiable(); var response = await _controller.GetDocumentsForCleanup(It.IsAny()); Assert.IsInstanceOf(response); Assert.AreEqual(StatusCodes.Status204NoContent, ((StatusCodeResult)response).StatusCode); _mockDocumentService.Verify(x => x.GetDocumentsForCleanup(It.IsAny()), Times.Once); } [Test] public async Task GetDocumentsForCleanup_Failure_Exception() { _mockDocumentService.Setup(x => x.GetDocumentsForCleanup(It.IsAny())) .ThrowsAsync(new Exception()); var response = await _controller.GetDocumentsForCleanup(It.IsAny()); Assert.IsInstanceOf(response); Assert.AreEqual(StatusCodes.Status500InternalServerError, ((StatusCodeResult)response).StatusCode); _mockDocumentService.Verify(x => x.GetDocumentsForCleanup(It.IsAny()), Times.Once); } private static DocumentRequest GenerateDocumentRequest() { return new DocumentRequest { Name = "TestFile.txt", Type = "Test", Content = new byte[10], Source = "Unit Test", Attributes = new Dictionary { { "IsTest", "true" } } }; } private static UpdateDocumentRequest GenerateUpdateDocumentRequest() { return new UpdateDocumentRequest { Content = new byte[10], Name = "TestFile.txt", Type = "Test", UpdateSource = "Unit Test", Attributes = new Dictionary { { "IsTest", "true" } } }; } private static UpdateDocumentInformationRequest GenerateUpdateDocumentInformationRequest() { return new UpdateDocumentInformationRequest { Name = "TestFile.txt", Type = "Test", UpdateSource = "Unit Test", Attributes = new Dictionary { { "IsTest", "true" } } }; } private static UpdateDocumentScanStatusRequest GenerateUpdateDocumentScanStatusRequest() { return new UpdateDocumentScanStatusRequest { ScanStatus = true, DocumentBlobIds = new List() }; } private static UpdateDocumentContentRequest GenerateUpdateDocumentContentRequest() { return new UpdateDocumentContentRequest { Content = new byte[10], UpdateSource = "Unit Test" }; } private static DocumentResponse GenerateDocumentResponse(Guid docId) { return new DocumentResponse { DocumentInformation = new DocumentInformationResponse { DocumentId = docId, 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 { { "IsTest", "true" }, { "Owner", "Me Myself" } } } }; } }