using System; using System.Threading.Tasks; using System.Collections.Generic; using Moq; using NUnit.Framework; using Newtonsoft.Json; using LCU.API.Internal.DocumentIngestor.Services; using LCU.API.Internal.DocumentIngestor.Services.IngestionOperationServices; using LCU.API.Internal.DocumentIngestor.Services.MemberNotificationInfoDeterminingServices; using LCU.API.Internal.DocumentIngestor.Services.MicroApiServices; using LCU.API.Internal.DocumentIngestor.Services.QueueServices; using LCU.API.Internal.DocumentIngestorLib.Exceptions; using LCU.Library.Internal.DocumentIngestorModels.Models; using LCU.Library.Internal.ApiResponse.Models; using LCU.Library.Internal.DocumentIngestorModels.Constants; using LCU.Library.Internal.DocumentIngestorModels.Entities; using LCU.Library.Internal.Exceptions; using LCU.Library.Internal.DocumentIngestorModels.Enums; using LCU.Library.Internal.FileOperationsModels.Models; namespace LCU.API.Internal.DocumentIngestorTests.Services.IngestionOperationServices; public class UploadCompletionServiceTest { private UploadCompletionService _uploadCompletionService; private Mock _mockQueueCompletionService; private Mock _mockFileOperationsService; private Mock _mockDocumentProcessingInfoService; private Mock _mockMemberNotificationInfoDeterminingService; private Mock _mockQueueGatherMemberNotificationInfoService; [SetUp] public void Setup() { _mockQueueCompletionService = new Mock(); _mockFileOperationsService = new Mock(); _mockDocumentProcessingInfoService = new Mock(); _mockMemberNotificationInfoDeterminingService = new Mock(); _mockQueueGatherMemberNotificationInfoService = new Mock(); _uploadCompletionService = new UploadCompletionService(_mockQueueCompletionService.Object, _mockFileOperationsService.Object, _mockDocumentProcessingInfoService.Object, _mockMemberNotificationInfoDeterminingService.Object, _mockQueueGatherMemberNotificationInfoService.Object); } [Test] public async Task HandleUploadCompletion_Success() { var documentProcessingInfoId = 1234; var response = GenerateUploadResponse(documentProcessingInfoId); var deserializedResponse = JsonConvert.DeserializeObject(response.ResponsePayload); var info = new DocumentProcessingInfo() { DocumentProcessingInfoId = documentProcessingInfoId }; var documentProcessingInfo = new List { new DocumentProcessingInfo() { DocumentProcessingInfoId = documentProcessingInfoId } }; _mockDocumentProcessingInfoService.Setup(x => x.GetListByIdAsync(It.IsAny>())).ReturnsAsync(documentProcessingInfo).Verifiable(); _mockDocumentProcessingInfoService.Setup(x => x.UpdateRangeAsync(It.IsAny>())).ReturnsAsync(documentProcessingInfo).Verifiable(); await _uploadCompletionService.HandleUploadCompletion(new List() { response }); _mockDocumentProcessingInfoService.Verify(x => x.GetListByIdAsync(It.IsAny>()), Times.Once); _mockDocumentProcessingInfoService.Verify(x => x.UpdateRangeAsync(It.IsAny>()), Times.Once); _mockQueueGatherMemberNotificationInfoService.Verify(x => x.QueueGatherMemberNotificationInfoRequest(It.IsAny()), Times.Once); _mockQueueCompletionService.Verify(x => x.QueueCompletionRequest(It.IsAny(), It.IsAny()), Times.Never); } [Test] public async Task HandleUploadCompletion_Failure_ResponseFailure() { var documentProcessingInfoId = 1234; var uploadResponse = GenerateUploadResponse(documentProcessingInfoId); uploadResponse.Successful = false; uploadResponse.ResponseStack = new List { "Test Failure" }; var moveResponse = new RequestResponse { IsSuccessful = true }; _mockFileOperationsService.Setup(x => x.DirectMoveFile(It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(moveResponse).Verifiable(); await _uploadCompletionService.HandleUploadCompletion(new List() { uploadResponse }); _mockFileOperationsService.Verify(x => x.DirectMoveFile(It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); _mockQueueCompletionService.Verify(x => x.QueueCompletionRequest(It.IsAny(), It.IsAny()), Times.Once); } private static CompletionRequest GenerateCompletionRequest() => new() { ProcessingRequest = new ProcessingRequest { DocumentFileName = Guid.NewGuid().ToString(), IngestorRequest = new() { RootPath = Guid.NewGuid().ToString() } }, ProcessResult = new ProcessResult { ProcessStatusType = ProcessStatusTypes.Processing } }; private static CompletionRequest GenerateCompletionRequest(int documentProcessingInfoId) => new() { ProcessingRequest = new ProcessingRequest { DocumentFileName = Guid.NewGuid().ToString(), IngestorRequest = new() { RootPath = Guid.NewGuid().ToString() }, DocumentProcessingInfoId = documentProcessingInfoId }, ProcessResult = new ProcessResult { ProcessStatusType = ProcessStatusTypes.Processing } }; private static Response GenerateUploadResponse() => new() { Successful = true, ResponsePayload = JsonConvert.SerializeObject(GenerateCompletionRequest()), ReturnCollection = new Dictionary { {"DocumentId", Guid.NewGuid().ToString()} } }; private static Response GenerateUploadResponse(int documentProcessingInfoId) => new() { Successful = true, ResponsePayload = JsonConvert.SerializeObject(GenerateCompletionRequest(documentProcessingInfoId)), ReturnCollection = new Dictionary { {"DocumentId", Guid.NewGuid().ToString()} } }; }