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 _mockPostRequest; private readonly Mock _mockWorkItemService; private readonly Mock _mockJobService; private readonly Mock _mockQueueCompletionService; public ExecutionServiceTest() { _mockPostRequest = new Mock(); _mockWorkItemService = new Mock(); _mockJobService = new Mock(); _mockQueueCompletionService = new Mock(); _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 } }; 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())).ReturnsAsync(workItem); _mockPostRequest.Setup(x => x.AsyncWithNewtonSerializedNoThrow(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())) .ReturnsAsync(() => httpResponseMessage); _mockJobService.Setup(x => x.GetByWorkItemIdExcludingWorkItemsAsync(It.IsAny())).ReturnsAsync(job); _mockJobService.Setup(x => x.SaveAsync(It.IsAny())).ReturnsAsync(job); _mockQueueCompletionService.Setup(x => x.QueueCompletion(It.IsAny(), It.IsAny>(), It.IsAny())).Verifiable(); await _executionService.HandleExecutionRequest(workItemExecution); _mockWorkItemService.Verify(x => x.GetByIdAsync(It.IsAny()), Times.Once); _mockPostRequest.Verify(x => x.AsyncWithNewtonSerializedNoThrow(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); _mockJobService.Verify(x => x.GetByWorkItemIdExcludingWorkItemsAsync(It.IsAny()), Times.Once); _mockJobService.Verify(x => x.UpdateAsync(It.IsAny()), Times.Once); _mockQueueCompletionService.Verify(x => x.QueueCompletion(It.IsAny(), It.IsAny>(), It.IsAny()), 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 } }; 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())).ReturnsAsync(workItem); _mockPostRequest.Setup(x => x.AsyncWithNewtonSerializedNoThrow(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())) .ReturnsAsync(() => httpResponseMessage); _mockJobService.Setup(x => x.GetByWorkItemIdExcludingWorkItemsAsync(It.IsAny())).ReturnsAsync(job); _mockJobService.Setup(x => x.SaveAsync(It.IsAny())).ReturnsAsync(job); _mockQueueCompletionService.Setup(x => x.QueueCompletion(It.IsAny(), It.IsAny>(), It.IsAny())).Verifiable(); await _executionService.HandleExecutionRequest(workItemExecution); _mockWorkItemService.Verify(x => x.GetByIdAsync(It.IsAny()), Times.Once); _mockPostRequest.Verify(x => x.AsyncWithNewtonSerializedNoThrow(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); _mockJobService.Verify(x => x.GetByWorkItemIdExcludingWorkItemsAsync(It.IsAny()), Times.Once); _mockJobService.Verify(x => x.UpdateAsync(It.IsAny()), Times.Once); _mockQueueCompletionService.Verify(x => x.QueueCompletion(It.IsAny(), It.IsAny>(), It.IsAny()), Times.Once); } }