Files
monorepo/LCU.API.Internal.Dispatcher/LCU.Api.Internal.DispatcherTests/ServiceTests/DispatcherOperationServices/ExecutionServiceTest.cs
T
2026-03-24 15:26:03 -05:00

127 lines
5.9 KiB
C#

using System.Collections.Generic;
using System.Net.Http;
using LCU.Library.Internal.DispatcherModels.Enums;
using LCU.Api.Internal.Dispatcher.Services;
using LCU.Api.Internal.Dispatcher.Services.DispatcherOperationServices;
using LCU.Library.Internal.LcuHttpClient.HttpRequest;
using LCU.Api.Internal.Dispatcher.Services.QueueServices;
namespace LCU.Api.Internal.DispatcherTests.ServiceTests.DispatcherOperationServices;
public class ExecutionServiceTest
{
private readonly ExecutionService _executionService;
private readonly Mock<IPostRequest> _mockPostRequest;
private readonly Mock<IWorkItemService> _mockWorkItemService;
private readonly Mock<IJobService> _mockJobService;
private readonly Mock<IQueueCompletionService> _mockQueueCompletionService;
public ExecutionServiceTest()
{
_mockPostRequest = new Mock<IPostRequest>();
_mockWorkItemService = new Mock<IWorkItemService>();
_mockJobService = new Mock<IJobService>();
_mockQueueCompletionService = new Mock<IQueueCompletionService>();
_executionService = new ExecutionService(_mockPostRequest.Object,
_mockWorkItemService.Object,
_mockJobService.Object,
_mockQueueCompletionService.Object);
}
[Fact]
public async Task HandleExecutionRequest_Execution_Successful()
{
var workItemExecution = new WorkItemExecution
{
WorkItemExecutionId = 1,
WorkItemId = 1,
CompletedTimestamp = null,
Status = StatusType.Processing
};
var workItem = new WorkItem
{
WorkItemId = 1,
CancelJobOnFailure = false,
Url = "http://testing.com",
SerializedPayload = "{\"Test\":\"Test\"}",
HttpMethod = HttpMethodType.Post,
WorkItemExecutions = new List<WorkItemExecution>
{
workItemExecution
}
};
var job = new Job
{
JobId = 1
};
var httpRequestMessage = new HttpRequestMessage();
var httpResponseMessage = new HttpResponseMessage
{
StatusCode = System.Net.HttpStatusCode.OK
};
_mockWorkItemService.Setup(x => x.GetByIdAsync(It.IsAny<int>())).ReturnsAsync(workItem);
_mockPostRequest.Setup(x => x.AsyncWithNewtonSerializedNoThrow(It.IsAny<Uri>(), It.IsAny<string>(), It.IsAny<int>(), It.IsAny<int>()))
.ReturnsAsync(() => httpResponseMessage);
_mockJobService.Setup(x => x.GetByWorkItemIdExcludingWorkItemsAsync(It.IsAny<int>())).ReturnsAsync(job);
_mockJobService.Setup(x => x.SaveAsync(It.IsAny<Job>())).ReturnsAsync(job);
_mockQueueCompletionService.Setup(x => x.QueueCompletion(It.IsAny<DispatcherReturnResponse>(), It.IsAny<List<string>>(), It.IsAny<bool>())).Verifiable();
await _executionService.HandleExecutionRequest(workItemExecution);
_mockWorkItemService.Verify(x => x.GetByIdAsync(It.IsAny<int>()), Times.Once);
_mockPostRequest.Verify(x => x.AsyncWithNewtonSerializedNoThrow(It.IsAny<Uri>(), It.IsAny<string>(), It.IsAny<int>(), It.IsAny<int>()), Times.Once);
_mockJobService.Verify(x => x.GetByWorkItemIdExcludingWorkItemsAsync(It.IsAny<int>()), Times.Once);
_mockJobService.Verify(x => x.UpdateAsync(It.IsAny<Job>()), Times.Once);
_mockQueueCompletionService.Verify(x => x.QueueCompletion(It.IsAny<DispatcherReturnResponse>(), It.IsAny<List<string>>(), It.IsAny<bool>()), Times.Never);
}
[Fact]
public async Task HandleExecutionRequest_Execution_NOT_Successful()
{
var workItemExecution = new WorkItemExecution
{
WorkItemExecutionId = 1,
WorkItemId = 1,
CompletedTimestamp = null,
Status = StatusType.Processing
};
var workItem = new WorkItem
{
WorkItemId = 1,
CancelJobOnFailure = false,
Url = "http://testing.com",
SerializedPayload = "{\"Test\":\"Test\"}",
HttpMethod = HttpMethodType.Post,
WorkItemExecutions = new List<WorkItemExecution>
{
workItemExecution
}
};
var job = new Job
{
JobId = 1
};
var httpRequestMessage = new HttpRequestMessage();
var httpResponseMessage = new HttpResponseMessage
{
StatusCode = System.Net.HttpStatusCode.BadRequest
};
_mockWorkItemService.Setup(x => x.GetByIdAsync(It.IsAny<int>())).ReturnsAsync(workItem);
_mockPostRequest.Setup(x => x.AsyncWithNewtonSerializedNoThrow(It.IsAny<Uri>(), It.IsAny<string>(), It.IsAny<int>(), It.IsAny<int>()))
.ReturnsAsync(() => httpResponseMessage);
_mockJobService.Setup(x => x.GetByWorkItemIdExcludingWorkItemsAsync(It.IsAny<int>())).ReturnsAsync(job);
_mockJobService.Setup(x => x.SaveAsync(It.IsAny<Job>())).ReturnsAsync(job);
_mockQueueCompletionService.Setup(x => x.QueueCompletion(It.IsAny<DispatcherReturnResponse>(), It.IsAny<List<string>>(), It.IsAny<bool>())).Verifiable();
await _executionService.HandleExecutionRequest(workItemExecution);
_mockWorkItemService.Verify(x => x.GetByIdAsync(It.IsAny<int>()), Times.Once);
_mockPostRequest.Verify(x => x.AsyncWithNewtonSerializedNoThrow(It.IsAny<Uri>(), It.IsAny<string>(), It.IsAny<int>(), It.IsAny<int>()), Times.Once);
_mockJobService.Verify(x => x.GetByWorkItemIdExcludingWorkItemsAsync(It.IsAny<int>()), Times.Once);
_mockJobService.Verify(x => x.UpdateAsync(It.IsAny<Job>()), Times.Once);
_mockQueueCompletionService.Verify(x => x.QueueCompletion(It.IsAny<DispatcherReturnResponse>(), It.IsAny<List<string>>(), It.IsAny<bool>()), Times.Once);
}
}