using LCU.Api.Internal.Dispatcher.Configurations; using LCU.Api.Internal.Dispatcher.Services; using LCU.Api.Internal.Dispatcher.Services.DispatcherOperationServices; using LCU.Api.Internal.DispatcherLib.Repositories; using LCU.Library.Internal.DispatcherModels.Models.Results; using System.Collections.Generic; namespace LCU.Api.Internal.DispatcherTests.ServiceTests.DispatcherOperationServices; public class CleanUpJobsServiceTest { private readonly Mock _mockJobService; private readonly Mock _mockCleanUpJobSettingService; private readonly Mock _mockUnitOfWork; private readonly CleanUpJobsService _cleanUpJobsService; public CleanUpJobsServiceTest() { var apiConfiguration = new ApiConfiguration() { CleanUpJobsDaysToDelete = -20 }; _mockJobService = new Mock(); _mockCleanUpJobSettingService = new Mock(); _mockUnitOfWork = new Mock(); _cleanUpJobsService = new CleanUpJobsService(_mockJobService.Object, _mockCleanUpJobSettingService.Object, _mockUnitOfWork.Object, apiConfiguration); } [Fact] public void Basic_Construct_Test() { Assert.NotNull(_cleanUpJobsService); } [Fact] public void ShouldSoftDeleteByDate_True() { // Arrange var setting = new CleanUpJobSetting() { CompletionDaysPast = -15 }; var execution = new WorkItemExecution() { WorkItemExecutionId = 1, WorkItemId = 2, CompletedTimestamp = DateTime.Now.AddDays(-30), StartedTimestamp = DateTime.Now.AddDays(-30).AddMinutes(-1) }; var workItem = new WorkItem() { JobId = 1, WorkItemId = 2, WorkItemExecutions = new List() { execution }, Order = 1 }; var job = new Job() { JobId = 1, WorkItems = new List() { workItem }, }; var result = new CleanUpJobBatchResult(); // Act var shouldDelete = _cleanUpJobsService.ShouldSoftDeleteByDate(setting, job, result); // Assert Assert.True(shouldDelete); } [Fact] public void ShouldSoftDeleteByDate_False() { // Arrange var setting = new CleanUpJobSetting() { CompletionDaysPast = -45 }; var execution = new WorkItemExecution() { WorkItemExecutionId = 1, WorkItemId = 2, CompletedTimestamp = DateTime.Now.AddDays(-30), StartedTimestamp = DateTime.Now.AddDays(-30).AddMinutes(-1) }; var workItem = new WorkItem() { JobId = 1, WorkItemId = 2, WorkItemExecutions = new List() { execution }, Order = 1 }; var job = new Job() { JobId = 1, WorkItems = new List() { workItem }, }; var result = new CleanUpJobBatchResult(); // Act var shouldDelete = _cleanUpJobsService.ShouldSoftDeleteByDate(setting, job, result); // Assert Assert.False(shouldDelete); } [Fact] public void ShouldSoftDeleteByDate_NoWorkItemExecutions_False() { // Arrange var setting = new CleanUpJobSetting() { CompletionDaysPast = -45 }; var workItem = new WorkItem() { JobId = 1, WorkItemId = 2, WorkItemExecutions = new List(), Order = 1 }; var job = new Job() { JobId = 1, WorkItems = new List() { workItem }, }; var result = new CleanUpJobBatchResult(); // Act var shouldDelete = _cleanUpJobsService.ShouldSoftDeleteByDate(setting, job, result); // Assert Assert.False(shouldDelete); Assert.Equal(1, result.FailureCount); } [Fact] public void ShouldSoftDeleteByDate_NoWorkItems_False() { // Arrange var setting = new CleanUpJobSetting() { CompletionDaysPast = -45 }; var job = new Job() { JobId = 1, WorkItems = new List(), }; var result = new CleanUpJobBatchResult(); // Act var shouldDelete = _cleanUpJobsService.ShouldSoftDeleteByDate(setting, job, result); // Assert Assert.False(shouldDelete); Assert.Equal(1, result.FailureCount); } [Fact] public void ShouldSoftDeleteByTag_Okay() { // Arrange var setting = new CleanUpJobSetting() { CleanUpJobTags = new List() { new CleanUpJobTag() { TagId = 1 }, new CleanUpJobTag() { TagId = 3 }, new CleanUpJobTag() { TagId = 5 } } }; var job = new Job() { JobTags = new List() { new JobTag() { TagId = 1 }, new JobTag() { TagId = 3 }, new JobTag() { TagId = 5 } } }; // Act var shouldDelete = _cleanUpJobsService.ShouldSoftDeleteByTag(setting, job); // Assert Assert.True(shouldDelete); } [Fact] public void ShouldSoftDeleteByTag_Okay_UnorderedTagId() { // Arrange var setting = new CleanUpJobSetting() { CleanUpJobTags = new List() { new CleanUpJobTag() { TagId = 1 }, new CleanUpJobTag() { TagId = 3 }, new CleanUpJobTag() { TagId = 5 } } }; var job = new Job() { JobTags = new List() { new JobTag() { TagId = 3 }, new JobTag() { TagId = 5 }, new JobTag() { TagId = 1 } } }; // Act var shouldDelete = _cleanUpJobsService.ShouldSoftDeleteByTag(setting, job); // Assert Assert.True(shouldDelete); } [Fact] public void ShouldSoftDeleteByTag_False_NumberOfTags() { // Arrange var setting = new CleanUpJobSetting() { CleanUpJobTags = new List() { new CleanUpJobTag() { TagId = 1 }, new CleanUpJobTag() { TagId = 3 }, new CleanUpJobTag() { TagId = 5 } } }; var job = new Job() { JobTags = new List() { new JobTag() { TagId = 3 }, new JobTag() { TagId = 5 } } }; // Act var shouldDelete = _cleanUpJobsService.ShouldSoftDeleteByTag(setting, job); // Assert Assert.False(shouldDelete); } [Fact] public void ShouldSoftDeleteByTag_False_DifferentTags() { // Arrange var setting = new CleanUpJobSetting() { CleanUpJobTags = new List() { new CleanUpJobTag() { TagId = 1 }, new CleanUpJobTag() { TagId = 3 }, new CleanUpJobTag() { TagId = 5 } } }; var job = new Job() { JobTags = new List() { new JobTag() { TagId = 3 }, new JobTag() { TagId = 5 }, new JobTag() { TagId = 100 } } }; // Act var shouldDelete = _cleanUpJobsService.ShouldSoftDeleteByTag(setting, job); // Assert Assert.False(shouldDelete); } }