using LCU.API.Internal.DocumentIngestor.Configurations; using LCU.API.Internal.DocumentIngestor.Controllers; using LCU.API.Internal.DocumentIngestor.Services; using LCU.Library.Internal.DocumentIngestorModels.Entities; using LCU.Library.Internal.DocumentIngestorModels.Enums; using LCU.Library.Internal.DocumentIngestorModels.Models; using LCU.Library.Internal.Exceptions; using Microsoft.AspNetCore.Mvc; using Moq; using NUnit.Framework; using System; using System.Collections.Generic; using System.Security.Claims; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; namespace LCU.API.Internal.DocumentIngestorTests.Controllers; public class MemberNotificationInfoControllerTest { private MemberNotificationInfoController _memberNotificationInfoController; private Mock _mockMemberNotificationInfoService; private ApiConfiguration _apiConfiguration; [SetUp] public void Setup() { _apiConfiguration = new ApiConfiguration() { MaxMemberNotificationAttempts = 3, TimeZone = "America/Chicago" }; _mockMemberNotificationInfoService = new Mock(); _memberNotificationInfoController = new MemberNotificationInfoController ( _mockMemberNotificationInfoService.Object, _apiConfiguration ) { ControllerContext = { HttpContext = new DefaultHttpContext() } }; } [Test] public async Task Get_OK() { var memberNotificationInfos = new List() { new MemberNotificationInfo() { MemberNotificationInfoId = 1 } }; _mockMemberNotificationInfoService.Setup(service => service.GetAsync()) .ReturnsAsync(memberNotificationInfos).Verifiable(); var result = await _memberNotificationInfoController.Get(); Assert.NotNull(result); var okResult = result as OkObjectResult; Assert.NotNull(okResult); Assert.AreEqual(200, okResult!.StatusCode); _mockMemberNotificationInfoService.Verify(); } [Test] public async Task Get_ServiceException_InternalServerError() { var memberNotificationInfos = new List(); _mockMemberNotificationInfoService.Setup(service => service.GetAsync()) .ThrowsAsync(new ServiceException("Thrown From Unit Test")).Verifiable(); var result = await _memberNotificationInfoController.Get(); Assert.NotNull(result); var statusResult = result as ObjectResult; Assert.NotNull(statusResult); Assert.AreEqual(500, statusResult!.StatusCode); _mockMemberNotificationInfoService.Verify(); } [Test] public async Task Get_UnexpectedException_InternalServerError() { var memberNotificationInfos = new List(); _mockMemberNotificationInfoService.Setup(service => service.GetAsync()) .ThrowsAsync(new Exception("Thrown From Unit Test")).Verifiable(); var result = await _memberNotificationInfoController.Get(); Assert.NotNull(result); var statusResult = result as StatusCodeResult; Assert.NotNull(statusResult); Assert.AreEqual(500, statusResult!.StatusCode); _mockMemberNotificationInfoService.Verify(); } [Test] public async Task GetById_OK() { var id = 1; var memberNotificationInfo = new MemberNotificationInfo(); _mockMemberNotificationInfoService.Setup(service => service.GetByIdAsync(id)) .ReturnsAsync(memberNotificationInfo).Verifiable(); var result = await _memberNotificationInfoController.GetById(id); Assert.NotNull(result); var okResult = result as OkObjectResult; Assert.NotNull(okResult); Assert.AreEqual(200, okResult!.StatusCode); _mockMemberNotificationInfoService.Verify(); } [Test] public async Task GetById_ServiceException_InternalServerError() { var id = 1; _mockMemberNotificationInfoService.Setup(service => service.GetByIdAsync(id)) .ThrowsAsync(new ServiceException("Thrown From Unit Test")).Verifiable(); var result = await _memberNotificationInfoController.GetById(id); Assert.NotNull(result); var statusResult = result as ObjectResult; Assert.NotNull(statusResult); Assert.AreEqual(500, statusResult!.StatusCode); _mockMemberNotificationInfoService.Verify(); } [Test] public async Task GetById_UnexpectedException_InternalServerError() { var id = 1; _mockMemberNotificationInfoService.Setup(service => service.GetByIdAsync(id)) .ThrowsAsync(new Exception("Thrown From Unit Test")).Verifiable(); var result = await _memberNotificationInfoController.GetById(id); Assert.NotNull(result); var statusResult = result as StatusCodeResult; Assert.NotNull(statusResult); Assert.AreEqual(500, statusResult!.StatusCode); _mockMemberNotificationInfoService.Verify(); } [Test] public async Task GetByDocumentProcessingInfoId_OK() { var documentProcessingInfoId = 1; var memberNotificationInfos = new List() { new MemberNotificationInfo() { DocumentProcessingInfoId = documentProcessingInfoId } }; _mockMemberNotificationInfoService.Setup(service => service.GetByDocumentProcessingInfoIdAsync(documentProcessingInfoId)) .ReturnsAsync(memberNotificationInfos).Verifiable(); var result = await _memberNotificationInfoController.GetByDocumentProcessingInfoId(documentProcessingInfoId); Assert.NotNull(result); var okResult = result as OkObjectResult; Assert.NotNull(okResult); Assert.AreEqual(200, okResult!.StatusCode); _mockMemberNotificationInfoService.Verify(); } [Test] public async Task GetByDocumentProcessingInfoId_ServiceException_InternalServerError() { var documentProcessingInfoId = 1; _mockMemberNotificationInfoService.Setup(service => service.GetByDocumentProcessingInfoIdAsync(documentProcessingInfoId)) .ThrowsAsync(new ServiceException("Thrown From Unit Test")).Verifiable(); var result = await _memberNotificationInfoController.GetByDocumentProcessingInfoId(documentProcessingInfoId); Assert.NotNull(result); var statusResult = result as ObjectResult; Assert.NotNull(statusResult); Assert.AreEqual(500, statusResult!.StatusCode); _mockMemberNotificationInfoService.Verify(); } [Test] public async Task GetByDocumentProcessingInfoId_UnexpectedException_InternalServerError() { var documentProcessingInfoId = 1; _mockMemberNotificationInfoService.Setup(service => service.GetByDocumentProcessingInfoIdAsync(documentProcessingInfoId)) .ThrowsAsync(new Exception("Thrown From Unit Test")).Verifiable(); var result = await _memberNotificationInfoController.GetByDocumentProcessingInfoId(documentProcessingInfoId); Assert.NotNull(result); var statusResult = result as StatusCodeResult; Assert.NotNull(statusResult); Assert.AreEqual(500, statusResult!.StatusCode); _mockMemberNotificationInfoService.Verify(); } [Test] public async Task Post_CreatedAtAction() { var documentProcessingInfoId = 1; var request = new MemberNotificationInfo() { DocumentProcessingInfoId = documentProcessingInfoId, NotificationStatusType = NotificationStatusTypes.Pending }; var requestReturn = new MemberNotificationInfo() { DocumentProcessingInfoId = documentProcessingInfoId, NotificationStatusType = NotificationStatusTypes.Complete }; _mockMemberNotificationInfoService.Setup(service => service.SaveAsync(request)).ReturnsAsync(requestReturn).Verifiable(); var result = await _memberNotificationInfoController.Post(request); Assert.NotNull(result); var okResult = result as CreatedAtActionResult; Assert.NotNull(okResult); Assert.AreEqual(201, okResult!.StatusCode); _mockMemberNotificationInfoService.Verify(); } [Test] public async Task Post_ServiceException_InternalServerError() { var request = new MemberNotificationInfo(); _mockMemberNotificationInfoService.Setup(service => service.SaveAsync(request)) .ThrowsAsync(new ServiceException("Thrown From Unit Test")).Verifiable(); var result = await _memberNotificationInfoController.Post(request); Assert.NotNull(result); var statusResult = result as ObjectResult; Assert.NotNull(statusResult); Assert.AreEqual(500, statusResult!.StatusCode); _mockMemberNotificationInfoService.Verify(); } [Test] public async Task Post_UnexpectedException_InternalServerError() { var request = new MemberNotificationInfo(); _mockMemberNotificationInfoService.Setup(service => service.SaveAsync(request)) .ThrowsAsync(new ServiceException("Thrown From Unit Test")).Verifiable(); var result = await _memberNotificationInfoController.Post(request); Assert.NotNull(result); var statusResult = result as ObjectResult; Assert.NotNull(statusResult); Assert.AreEqual(500, statusResult!.StatusCode); _mockMemberNotificationInfoService.Verify(); } [Test] public async Task Put_OK() { var request = new MemberNotificationInfo(); _mockMemberNotificationInfoService.Setup(service => service.Update(request)).ReturnsAsync(request).Verifiable(); var result = await _memberNotificationInfoController.Put(request); Assert.NotNull(result); var okResult = result as OkObjectResult; Assert.NotNull(okResult); Assert.AreEqual(200, okResult!.StatusCode); _mockMemberNotificationInfoService.Verify(); } [Test] public async Task Put_ServiceException_InternalServerError() { var request = new MemberNotificationInfo(); _mockMemberNotificationInfoService.Setup(service => service.Update(request)) .Throws(new ServiceException("Thrown From Unit Test")).Verifiable(); var result = await _memberNotificationInfoController.Put(request); Assert.NotNull(result); var statusResult = result as ObjectResult; Assert.NotNull(statusResult); Assert.AreEqual(500, statusResult!.StatusCode); _mockMemberNotificationInfoService.Verify(); } [Test] public async Task Put_UnexpectedException_InternalServerError() { var request = new MemberNotificationInfo(); _mockMemberNotificationInfoService.Setup(service => service.Update(request)) .Throws(new ServiceException("Thrown From Unit Test")).Verifiable(); var result = await _memberNotificationInfoController.Put(request); Assert.NotNull(result); var statusResult = result as ObjectResult; Assert.NotNull(statusResult); Assert.AreEqual(500, statusResult!.StatusCode); _mockMemberNotificationInfoService.Verify(); } [Test] public async Task PutRange_OK() { var request = new List(); _mockMemberNotificationInfoService.Setup(service => service.UpdateRange(request)).ReturnsAsync(request).Verifiable(); var result = await _memberNotificationInfoController.Put(request); Assert.NotNull(result); var okResult = result as OkObjectResult; Assert.NotNull(okResult); Assert.AreEqual(200, okResult!.StatusCode); _mockMemberNotificationInfoService.Verify(); } [Test] public async Task PutRange_ServiceException_InternalServerError() { var request = new List(); _mockMemberNotificationInfoService.Setup(service => service.UpdateRange(request)) .Throws(new ServiceException("Thrown From Unit Test")).Verifiable(); var result = await _memberNotificationInfoController.Put(request); Assert.NotNull(result); var statusResult = result as ObjectResult; Assert.NotNull(statusResult); Assert.AreEqual(500, statusResult!.StatusCode); _mockMemberNotificationInfoService.Verify(); } [Test] public async Task PutRange_UnexpectedException_InternalServerError() { var request = new List(); _mockMemberNotificationInfoService.Setup(service => service.UpdateRange(request)) .Throws(new ServiceException("Thrown From Unit Test")).Verifiable(); var result = await _memberNotificationInfoController.Put(request); Assert.NotNull(result); var statusResult = result as ObjectResult; Assert.NotNull(statusResult); Assert.AreEqual(500, statusResult!.StatusCode); _mockMemberNotificationInfoService.Verify(); } [Test] public async Task Delete_OK() { var id = 1; _mockMemberNotificationInfoService.Setup(service => service.Delete(id)).Verifiable(); var result = await _memberNotificationInfoController.Delete(id); Assert.NotNull(result); var okResult = result as OkResult; Assert.NotNull(okResult); Assert.AreEqual(200, okResult!.StatusCode); _mockMemberNotificationInfoService.Verify(); } [Test] public async Task Delete_ServiceException_InternalServerError() { var id = 1; _mockMemberNotificationInfoService.Setup(service => service.Delete(id)) .Throws(new ServiceException("Thrown From Unit Test")).Verifiable(); var result = await _memberNotificationInfoController.Delete(id); Assert.NotNull(result); var statusResult = result as ObjectResult; Assert.NotNull(statusResult); Assert.AreEqual(500, statusResult!.StatusCode); _mockMemberNotificationInfoService.Verify(); } [Test] public async Task Delete_UnexpectedException_InternalServerError() { var id = 1; _mockMemberNotificationInfoService.Setup(service => service.Delete(id)) .Throws(new ServiceException("Thrown From Unit Test")).Verifiable(); var result = await _memberNotificationInfoController.Delete(id); Assert.NotNull(result); var statusResult = result as ObjectResult; Assert.NotNull(statusResult); Assert.AreEqual(500, statusResult!.StatusCode); _mockMemberNotificationInfoService.Verify(); } [Test] public async Task GetByNotificationStatusAsync_Ok() { var status = NotificationStatusTypes.Pending; _mockMemberNotificationInfoService.Setup(service => service.GetByNotificationStatusAsync(status)) .ReturnsAsync(new List() { new MemberNotificationInfo() {NotificationStatusType = NotificationStatusTypes.Pending}, new MemberNotificationInfo() {NotificationStatusType = NotificationStatusTypes.Pending}, new MemberNotificationInfo() {NotificationStatusType = NotificationStatusTypes.Pending} }).Verifiable(); var result = await _memberNotificationInfoController.GetByNotificationStatus(status); Assert.NotNull(result); var okResult = result as OkObjectResult; Assert.NotNull(okResult); Assert.AreEqual(200, okResult!.StatusCode); _mockMemberNotificationInfoService.Verify(); } [Test] public async Task GetSendableMemberNotificationInfos_Ok() { var uploadDate = new DateTime(2022, 10, 2); var memberNotificationInfos = new List() { new MemberNotificationInfo() { MemberNotificationInfoId = 1, DocumentProcessingInfoId = 2, UploadDate = new DateTime(2022, 8, 1), NotificationStatusType = NotificationStatusTypes.Pending, DeliveryMethod = DeliveryMethod.Electronic, Attempts = 1, EmailAddress = "test@test.com" } }; _mockMemberNotificationInfoService.Setup(service => service.GetSendableMemberNotificationInfosAsync(uploadDate, _apiConfiguration.MaxMemberNotificationAttempts)) .ReturnsAsync(memberNotificationInfos).Verifiable(); var result = await _memberNotificationInfoController.GetSendableMemberNotificationInfos(uploadDate); Assert.NotNull(result); var okResult = result as OkObjectResult; Assert.NotNull(okResult); Assert.AreEqual(200, okResult!.StatusCode); } [Test] public async Task SetDocumentNotificationStatus_SetDocumentNotificationStatusSuccessfully_Returns200() { var documentProcessingInfoId = DateTime.Now.Millisecond; var notificationStatus = NotificationStatusTypes.CriticalFailure; var userName = Guid.NewGuid().ToString(); var reason = Guid.NewGuid().ToString(); _mockMemberNotificationInfoService .Setup(s => s.SetDocumentNotificationStatus(documentProcessingInfoId, notificationStatus, userName, reason)) .ReturnsAsync(true); _memberNotificationInfoController.HttpContext.User = new ClaimsPrincipal(new ClaimsIdentity(new[] { new Claim("name", userName) })); var response = await _memberNotificationInfoController.SetDocumentNotificationStatus(documentProcessingInfoId, notificationStatus, reason); Assert.IsInstanceOf(response); Assert.AreEqual(StatusCodes.Status200OK, ((OkResult)response).StatusCode); } [Test] public async Task SetDocumentNotificationStatus_SetDocumentNotificationStatusFailed_Returns400() { var documentProcessingInfoId = DateTime.Now.Millisecond; var notificationStatus = NotificationStatusTypes.Complete; var userName = Guid.NewGuid().ToString(); var reason = Guid.NewGuid().ToString(); _mockMemberNotificationInfoService .Setup(s => s.SetDocumentNotificationStatus(documentProcessingInfoId, notificationStatus, userName, reason)) .ReturnsAsync(false); _memberNotificationInfoController.HttpContext.User = new ClaimsPrincipal(new ClaimsIdentity(new[] { new Claim("name", userName) })); var response = await _memberNotificationInfoController.SetDocumentNotificationStatus(documentProcessingInfoId, notificationStatus, reason); Assert.IsInstanceOf(response); Assert.AreEqual(StatusCodes.Status400BadRequest, ((BadRequestResult)response).StatusCode); } [Test] public async Task SetDocumentNotificationStatus_SetDocumentNotificationStatusThrowsServiceException_Returns500() { var documentProcessingInfoId = DateTime.Now.Millisecond; var notificationStatus = NotificationStatusTypes.Failure; var message = Guid.NewGuid().ToString(); var userName = Guid.NewGuid().ToString(); var reason = Guid.NewGuid().ToString(); _mockMemberNotificationInfoService .Setup(s => s.SetDocumentNotificationStatus(documentProcessingInfoId, notificationStatus, userName, reason)) .ThrowsAsync(new ServiceException(message)); _memberNotificationInfoController.HttpContext.User = new ClaimsPrincipal(new ClaimsIdentity(new[] { new Claim("name", userName) })); var response = await _memberNotificationInfoController.SetDocumentNotificationStatus(documentProcessingInfoId, notificationStatus, reason); Assert.IsInstanceOf(response); Assert.AreEqual(StatusCodes.Status500InternalServerError, ((ObjectResult)response).StatusCode); Assert.AreEqual(message, ((ObjectResult)response).Value); } [Test] public async Task SetDocumentNotificationStatus_SetDocumentNotificationStatusThrowsException_Returns500() { var documentProcessingInfoId = DateTime.Now.Millisecond; var notificationStatus = NotificationStatusTypes.Pending; var userName = Guid.NewGuid().ToString(); var reason = Guid.NewGuid().ToString(); _mockMemberNotificationInfoService .Setup(s => s.SetDocumentNotificationStatus(documentProcessingInfoId, notificationStatus, userName, reason)) .ThrowsAsync(new Exception()); var response = await _memberNotificationInfoController.SetDocumentNotificationStatus(documentProcessingInfoId, notificationStatus, reason); Assert.IsInstanceOf(response); Assert.AreEqual(StatusCodes.Status500InternalServerError, ((StatusCodeResult)response).StatusCode); } [Test] public async Task BulkSetDocumentNotificationStatus_Success() { var request = new BulkNotificationStatusRequest() { Filter = new() { IngestorRunInfoId = Guid.NewGuid() }, NotificationStatus = NotificationStatusTypes.Complete, ReasonMessage = Guid.NewGuid().ToString() }; var userName = Guid.NewGuid().ToString(); _mockMemberNotificationInfoService .Setup(s => s.BulkSetDocumentNotificationStatus(request, userName)) .ReturnsAsync(true); _memberNotificationInfoController.HttpContext.User = new ClaimsPrincipal(new ClaimsIdentity(new[] { new Claim("name", userName) })); var response = await _memberNotificationInfoController.BulkSetDocumentNotificationStatus(request); Assert.IsInstanceOf(response); Assert.AreEqual(StatusCodes.Status200OK, ((OkResult)response).StatusCode); } [Test] public async Task BulkSetDocumentNotificationStatus_BadRequest_Returns400() { var request = new BulkNotificationStatusRequest() { Filter = new() { IngestorRunInfoId = Guid.NewGuid() }, NotificationStatus = NotificationStatusTypes.Complete, ReasonMessage = Guid.NewGuid().ToString() }; var userName = Guid.NewGuid().ToString(); _mockMemberNotificationInfoService .Setup(s => s.BulkSetDocumentNotificationStatus(request, userName)) .ReturnsAsync(false); _memberNotificationInfoController.HttpContext.User = new ClaimsPrincipal(new ClaimsIdentity(new[] { new Claim("name", userName) })); var response = await _memberNotificationInfoController.BulkSetDocumentNotificationStatus(request); Assert.IsInstanceOf(response); Assert.AreEqual(StatusCodes.Status400BadRequest, ((BadRequestResult)response).StatusCode); } [Test] public async Task BulkSetDocumentNotificationStatus_ServiceException_Returns500() { var request = new BulkNotificationStatusRequest() { Filter = new() { IngestorRunInfoId = Guid.NewGuid() }, NotificationStatus = NotificationStatusTypes.Complete, ReasonMessage = Guid.NewGuid().ToString() }; var userName = Guid.NewGuid().ToString(); var message = Guid.NewGuid().ToString(); _mockMemberNotificationInfoService .Setup(s => s.BulkSetDocumentNotificationStatus(request, userName)) .ThrowsAsync(new ServiceException(message)); _memberNotificationInfoController.HttpContext.User = new ClaimsPrincipal(new ClaimsIdentity(new[] { new Claim("name", userName) })); var response = await _memberNotificationInfoController.BulkSetDocumentNotificationStatus(request); Assert.IsInstanceOf(response); Assert.AreEqual(StatusCodes.Status500InternalServerError, ((ObjectResult)response).StatusCode); Assert.AreEqual(message, ((ObjectResult)response).Value); } [Test] public async Task BulkSetDocumentNotificationStatus_Exception_Returns500() { var request = new BulkNotificationStatusRequest() { Filter = new() { IngestorRunInfoId = Guid.NewGuid() }, NotificationStatus = NotificationStatusTypes.Complete, ReasonMessage = Guid.NewGuid().ToString() }; var userName = Guid.NewGuid().ToString(); _mockMemberNotificationInfoService .Setup(s => s.BulkSetDocumentNotificationStatus(request, userName)) .ThrowsAsync(new Exception()); var response = await _memberNotificationInfoController.BulkSetDocumentNotificationStatus(request); Assert.IsInstanceOf(response); Assert.AreEqual(StatusCodes.Status500InternalServerError, ((StatusCodeResult)response).StatusCode); } }