242 lines
9.5 KiB
C#
242 lines
9.5 KiB
C#
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.QueueServices;
|
|
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;
|
|
namespace LCU.API.Internal.DocumentIngestorTests.Services.IngestionOperationServices;
|
|
|
|
public class CompletionServiceTest
|
|
{
|
|
private CompletionService _completionService;
|
|
private Mock<IIngestorRunInfoService> _mockIngestorRunInfoService;
|
|
private Mock<IDocumentProcessingInfoService> _mockDocumentProcessingInfoService;
|
|
private readonly Mock<IResponseQueue> _responseQueue = new();
|
|
|
|
[SetUp]
|
|
public void Setup()
|
|
{
|
|
_mockIngestorRunInfoService = new Mock<IIngestorRunInfoService>();
|
|
_mockDocumentProcessingInfoService = new Mock<IDocumentProcessingInfoService>();
|
|
_completionService = new CompletionService(_mockIngestorRunInfoService.Object, _mockDocumentProcessingInfoService.Object, _responseQueue.Object);
|
|
}
|
|
|
|
[Test]
|
|
public void HandleCompletion_Exception_ResponseNull()
|
|
{
|
|
Response request = null;
|
|
Assert.ThrowsAsync<ArgumentNullException>(async () => await _completionService.HandleCompletion(null));
|
|
|
|
_mockDocumentProcessingInfoService.Verify(x => x.UpdateStatusAndAddLog(It.IsAny<int>(),
|
|
It.IsAny<ProcessStatusTypes>(),
|
|
It.IsAny<List<string>>(),
|
|
It.IsAny<string>(),
|
|
It.IsAny<bool?>()), Times.Never);
|
|
}
|
|
|
|
[Test]
|
|
public void HandleCompletion_Exception_RequestNull()
|
|
{
|
|
var request = new Response();
|
|
Assert.ThrowsAsync<ArgumentNullException>(async () => await _completionService.HandleCompletion(new List<Response>() { request }));
|
|
_mockDocumentProcessingInfoService.Verify(x => x.UpdateStatusAndAddLog(It.IsAny<int>(),
|
|
It.IsAny<ProcessStatusTypes>(),
|
|
It.IsAny<List<string>>(),
|
|
It.IsAny<string>(),
|
|
It.IsAny<bool?>()), Times.Never);
|
|
}
|
|
|
|
[Test]
|
|
public async Task HandleCompletion_Update_Success_Complete()
|
|
{
|
|
var documentProcessInfo = new DocumentProcessingInfo()
|
|
{
|
|
DocumentProcessingInfoId = 1,
|
|
IngestorRunInfoId = Guid.NewGuid(),
|
|
DocumentValuePayload = "{test}",
|
|
StatusType = ProcessStatusTypes.Completed
|
|
};
|
|
|
|
var ingestorRunInfo = new IngestorRunInfo()
|
|
{
|
|
IngestorRunInfoId = documentProcessInfo.IngestorRunInfoId,
|
|
IndexFilePath = "\\Test\\",
|
|
IndexTotal = 1
|
|
};
|
|
|
|
_mockIngestorRunInfoService.Setup(service =>
|
|
service.GetListByIdAsync(It.IsAny<List<Guid>>())).ReturnsAsync(new List<IngestorRunInfo>() { ingestorRunInfo });
|
|
|
|
var processResult = new ProcessResult()
|
|
{
|
|
Success = true,
|
|
AttemptToArchive = false,
|
|
};
|
|
|
|
var processingRequest = new ProcessingRequest()
|
|
{
|
|
IngestorRunInfoId = documentProcessInfo.IngestorRunInfoId
|
|
};
|
|
|
|
var completionRequest = new CompletionRequest()
|
|
{
|
|
ProcessingRequest = processingRequest,
|
|
ProcessResult = processResult
|
|
};
|
|
var response = new Response()
|
|
{
|
|
Successful = true,
|
|
ResponsePayload = JsonConvert.SerializeObject(completionRequest)
|
|
};
|
|
|
|
var completionProcessingData = new CompletionProcessingData()
|
|
{
|
|
DocumentProcessingInfo = documentProcessInfo,
|
|
CompletionRequest = completionRequest
|
|
};
|
|
|
|
_mockDocumentProcessingInfoService.Setup(service => service.UpdateProcessCountAndStatusAndAddLog(
|
|
It.IsAny<List<CompletionRequest>>()))
|
|
.ReturnsAsync(new List<CompletionProcessingData>() { completionProcessingData});
|
|
|
|
_mockDocumentProcessingInfoService.Setup(service => service.GetCompletedFirstPassByIngestorRunInfoIdAsync(
|
|
It.IsAny<Guid>())).ReturnsAsync(
|
|
new List<DocumentProcessingInfo>() { documentProcessInfo });
|
|
|
|
await _completionService.HandleCompletion(new List<Response>() { response });
|
|
_mockDocumentProcessingInfoService.Verify(x => x.UpdateProcessCountAndStatusAndAddLog(It.IsAny<List<CompletionRequest>>()), Times.Once);
|
|
_mockIngestorRunInfoService.Verify(x => x.GetListByIdAsync(It.IsAny<List<Guid>>()), Times.Once);
|
|
_mockDocumentProcessingInfoService.Verify(x => x.GetCompletedFirstPassByIngestorRunInfoIdAsync(It.IsAny<Guid>()), Times.Once);
|
|
_mockIngestorRunInfoService.Verify(x => x.Update(It.IsAny<IngestorRunInfo>()), Times.Once);
|
|
}
|
|
|
|
[Test]
|
|
public async Task HandleCompletion_Update_Success_Incomplete()
|
|
{
|
|
var documentProcessInfo = new DocumentProcessingInfo()
|
|
{
|
|
DocumentProcessingInfoId = 1,
|
|
IngestorRunInfoId = Guid.NewGuid(),
|
|
DocumentValuePayload = "{test}",
|
|
StatusType = ProcessStatusTypes.Completed
|
|
};
|
|
|
|
var ingestorRunInfo = new IngestorRunInfo()
|
|
{
|
|
IngestorRunInfoId = documentProcessInfo.IngestorRunInfoId,
|
|
IndexFilePath = "\\Test\\",
|
|
IndexTotal = 5
|
|
};
|
|
|
|
_mockIngestorRunInfoService.Setup(service =>
|
|
service.GetListByIdAsync(It.IsAny<List<Guid>>())).ReturnsAsync(new List<IngestorRunInfo>() { ingestorRunInfo });
|
|
|
|
var processResult = new ProcessResult()
|
|
{
|
|
Success = true,
|
|
AttemptToArchive = false,
|
|
};
|
|
|
|
var processingRequest = new ProcessingRequest()
|
|
{
|
|
IngestorRunInfoId = documentProcessInfo.IngestorRunInfoId
|
|
};
|
|
|
|
var completionRequest = new CompletionRequest()
|
|
{
|
|
ProcessingRequest = processingRequest,
|
|
ProcessResult = processResult
|
|
};
|
|
var response = new Response()
|
|
{
|
|
Successful = true,
|
|
ResponsePayload = JsonConvert.SerializeObject(completionRequest)
|
|
};
|
|
|
|
var completionProcessingData = new CompletionProcessingData()
|
|
{
|
|
DocumentProcessingInfo = documentProcessInfo,
|
|
CompletionRequest = completionRequest
|
|
};
|
|
|
|
_mockDocumentProcessingInfoService.Setup(service => service.UpdateProcessCountAndStatusAndAddLog(
|
|
It.IsAny<List<CompletionRequest>>()))
|
|
.ReturnsAsync(new List<CompletionProcessingData>() { completionProcessingData });
|
|
|
|
_mockDocumentProcessingInfoService.Setup(service => service.GetCompletedFirstPassByIngestorRunInfoIdAsync(
|
|
It.IsAny<Guid>())).ReturnsAsync(
|
|
new List<DocumentProcessingInfo>() { documentProcessInfo });
|
|
|
|
await _completionService.HandleCompletion(new List<Response>() { response });
|
|
_mockDocumentProcessingInfoService.Verify(x => x.UpdateProcessCountAndStatusAndAddLog(It.IsAny<List<CompletionRequest>>()), Times.Once);
|
|
_mockIngestorRunInfoService.Verify(x => x.GetListByIdAsync(It.IsAny<List<Guid>>()), Times.Once);
|
|
_mockDocumentProcessingInfoService.Verify(x => x.GetCompletedFirstPassByIngestorRunInfoIdAsync(It.IsAny<Guid>()), Times.Never);
|
|
_mockIngestorRunInfoService.Verify(x => x.Update(It.IsAny<IngestorRunInfo>()), Times.Once);
|
|
}
|
|
|
|
[Test]
|
|
public void HandleCompletion_UpdateRunInfoStatusById_ThrowsException()
|
|
{
|
|
var documentProcessInfo = new DocumentProcessingInfo()
|
|
{
|
|
DocumentProcessingInfoId = 1,
|
|
IngestorRunInfoId = Guid.NewGuid(),
|
|
DocumentValuePayload = "{test}",
|
|
StatusType = ProcessStatusTypes.Completed
|
|
};
|
|
|
|
var ingestorRunInfo = new IngestorRunInfo()
|
|
{
|
|
IngestorRunInfoId = new Guid(),
|
|
IndexFilePath = "\\Test\\",
|
|
IndexTotal = 1
|
|
};
|
|
|
|
var processResult = new ProcessResult()
|
|
{
|
|
Success = true,
|
|
AttemptToArchive = false,
|
|
};
|
|
|
|
var processingRequest = new ProcessingRequest()
|
|
{
|
|
IngestorRunInfoId = documentProcessInfo.IngestorRunInfoId
|
|
};
|
|
|
|
var completionRequest = new CompletionRequest()
|
|
{
|
|
ProcessingRequest = processingRequest,
|
|
ProcessResult = processResult
|
|
};
|
|
var response = new Response()
|
|
{
|
|
Successful = true,
|
|
ResponsePayload = JsonConvert.SerializeObject(completionRequest)
|
|
};
|
|
|
|
var completionProcessingData = new CompletionProcessingData()
|
|
{
|
|
DocumentProcessingInfo = documentProcessInfo,
|
|
CompletionRequest = completionRequest
|
|
};
|
|
|
|
_mockDocumentProcessingInfoService.Setup(service => service.UpdateProcessCountAndStatusAndAddLog(
|
|
It.IsAny<List<CompletionRequest>>()))
|
|
.ReturnsAsync(new List<CompletionProcessingData>() { completionProcessingData });
|
|
|
|
_mockIngestorRunInfoService.Setup(x => x.GetListByIdAsync(It.IsAny<List<Guid>>())).ThrowsAsync(new Exception("Test Exception"));
|
|
|
|
Assert.ThrowsAsync<ServiceException>(async () => await _completionService.HandleCompletion(new List<Response>() { response }));
|
|
_mockDocumentProcessingInfoService.Verify(x => x.UpdateProcessCountAndStatusAndAddLog(It.IsAny<List<CompletionRequest>>()), Times.Once);
|
|
_mockIngestorRunInfoService.Verify(x => x.Update(It.IsAny<IngestorRunInfo>()), Times.Never);
|
|
}
|
|
} |