using System.Collections.Generic; using LCU.Api.Internal.Dispatcher.Services; using LCU.Api.Internal.Dispatcher.Services.DispatcherOperationServices; using LCU.Api.Internal.Dispatcher.Services.QueueServices; using LCU.Library.Internal.DispatcherModels.Enums; using LCU.Library.Internal.DispatcherModels.Models.Requests; namespace LCU.Api.Internal.DispatcherTests.ServiceTests.DispatcherOperationServices; public class JobFailureServiceTest { private readonly JobFailureService _jobFailureService; private readonly Mock _mockWorkItemService; private readonly Mock _mockWorkItemExecutionService; private readonly Mock _mockJobService; private readonly Mock _mockExecutionMessageService; private readonly Mock _mockQueueDetermineOperationService; private readonly Mock _mockQueueSendErroredJobAlertService; public JobFailureServiceTest() { _mockWorkItemService = new Mock(); _mockWorkItemExecutionService = new Mock(); _mockJobService = new Mock(); _mockExecutionMessageService = new Mock(); _mockQueueDetermineOperationService = new Mock(); _mockQueueSendErroredJobAlertService = new Mock(); _jobFailureService = new JobFailureService(_mockWorkItemService.Object, _mockWorkItemExecutionService.Object, _mockJobService.Object, _mockExecutionMessageService.Object, _mockQueueDetermineOperationService.Object, _mockQueueSendErroredJobAlertService.Object); } [Fact] public async Task HandleJobFailureRequest_QueuesErroredJobAlert_WithOriginalJobErrorRecipients() { var workItemExecution = new WorkItemExecution { WorkItemExecutionId = 10, WorkItemId = 7 }; var workItem = new WorkItem { WorkItemId = 7, CurrentWorkItemExecution = workItemExecution, CancelJobOnFailure = false, RequiresJobCancellation = false }; var job = new Job { JobId = 22, JobStatus = JobStatus.Processing, JobErrorRecipients = new List { new JobErrorRecipient { Recipient = "ops@landmarkcu.com" } } }; var updatedJob = new Job { JobId = 22, JobStatus = JobStatus.Failed, JobErrorRecipients = new List() }; var request = new JobFailureRequest { WorkItemId = 7, ExecutionMessages = new List() }; _mockWorkItemService.Setup(x => x.GetByIdAsync(request.WorkItemId)).ReturnsAsync(workItem); _mockWorkItemExecutionService.Setup(x => x.UpdateFailedWorkItemExecution(workItemExecution)).ReturnsAsync(workItemExecution); _mockJobService.Setup(x => x.GetByWorkItemIdExcludingWorkItemsIncludeErrorRecipientsAsync(request.WorkItemId)).ReturnsAsync(job); _mockExecutionMessageService.Setup(x => x.SaveExecutionMessages(workItemExecution.WorkItemExecutionId, request.ExecutionMessages)).Returns(Task.CompletedTask); _mockJobService.Setup(x => x.UpdateAsync(job)).ReturnsAsync(updatedJob); await _jobFailureService.HandleJobFailureRequest(request); _mockQueueSendErroredJobAlertService.Verify(x => x.QueueSendErroredJobAlert( It.Is(queuedJob => queuedJob.JobId == job.JobId && queuedJob.JobStatus == JobStatus.Failed && queuedJob.JobErrorRecipients.Count == 1 && queuedJob.JobErrorRecipients.First().Recipient == "ops@landmarkcu.com"), workItem), Times.Once); _mockQueueDetermineOperationService.Verify(x => x.QueueDetermineOperation(It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); } }