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

390 lines
14 KiB
C#

using LCU.Api.Internal.Dispatcher.Configurations;
using LCU.Api.Internal.Dispatcher.Services.DispatcherOperationServices;
using LCU.Api.Internal.Dispatcher.Services;
using System.Collections.Generic;
using LCU.Library.Internal.DispatcherModels.Enums;
using LCU.Library.Internal.DispatcherModels.Models.Requests;
namespace LCU.Api.Internal.DispatcherTests.ServiceTests
{
public class SendErroredJobAlertServiceTests
{
private readonly Mock<INotificationService> _mockNotificationService;
private readonly ApiConfiguration _apiConfigiguration;
private readonly SendErroredJobAlertService _sendErroredJobAlertService;
public SendErroredJobAlertServiceTests()
{
_mockNotificationService = new Mock<INotificationService>();
_apiConfigiguration = new ApiConfiguration
{
DefaultErrorRecipient = "[email protected]",
BatchErroredJobsTemplate = "./TemplatesTest/Test_BatchErroredJobsTemplate.html",
ErrorRowTemplate = "./TemplatesTest/Test_ErrorRowTemplate.html"
};
_sendErroredJobAlertService = new SendErroredJobAlertService(_apiConfigiguration, _mockNotificationService.Object);
}
[Fact]
public void GroupJobsByRecipients_Returns_OnlyDefault()
{
var job = new Job()
{
JobId = 1
};
var erroredJobAlertRequests = new List<ErroredJobAlertRequest>
{
new ErroredJobAlertRequest
{
CurrentJob = job,
CurrentWorkItem = new WorkItem()
}
};
var result = _sendErroredJobAlertService.GroupJobsByRecipients(erroredJobAlertRequests);
Assert.NotNull(result);
Assert.Single(result);
var resultErroredJobAlertRequest = result["[email protected]"].FirstOrDefault();
Assert.Equal(job, resultErroredJobAlertRequest.CurrentJob);
}
[Fact]
public void GroupJobsByRecipients_Returns_FourRecipients_And_Default()
{
var job1 = new Job()
{
JobId = 1
};
var job2 = new Job()
{
JobErrorRecipients = new List<JobErrorRecipient>()
{
new JobErrorRecipient()
{
Recipient = "Test_Recipient_1",
},
new JobErrorRecipient()
{
Recipient = "Test_Recipient_2",
}
}
};
var job3 = new Job()
{
JobErrorRecipients = new List<JobErrorRecipient>()
{
new JobErrorRecipient()
{
Recipient = "Test_Recipient_2",
},
new JobErrorRecipient()
{
Recipient = "Test_Recipient_3",
},
new JobErrorRecipient()
{
Recipient = "Test_Recipient_4",
}
}
};
var erroredJobAlertRequests = new List<ErroredJobAlertRequest>
{
new ErroredJobAlertRequest
{
CurrentJob = job1,
CurrentWorkItem = new WorkItem()
},
new ErroredJobAlertRequest
{
CurrentJob = job2,
CurrentWorkItem = new WorkItem()
},
new ErroredJobAlertRequest
{
CurrentJob = job3,
CurrentWorkItem = new WorkItem()
}
};
var result = _sendErroredJobAlertService.GroupJobsByRecipients(erroredJobAlertRequests);
Assert.NotNull(result);
Assert.Equal(job1, result["[email protected]"].FirstOrDefault().CurrentJob);
Assert.Equal(job2, result["test_recipient_1"].FirstOrDefault().CurrentJob);
Assert.Equal(job3, result["test_recipient_4"].FirstOrDefault().CurrentJob);
}
[Fact]
public void GroupJobsByRecipients_Returns_FourRecipients_And_DefaultAsTwoJobs()
{
var job1 = new Job()
{
JobId = 1
};
var job2 = new Job()
{
JobErrorRecipients = new List<JobErrorRecipient>()
{
new JobErrorRecipient()
{
Recipient = "[email protected]",
},
new JobErrorRecipient()
{
Recipient = "Test_Recipient_1",
},
new JobErrorRecipient()
{
Recipient = "Test_Recipient_2",
}
}
};
var job3 = new Job()
{
JobErrorRecipients = new List<JobErrorRecipient>()
{
new JobErrorRecipient()
{
Recipient = "Test_Recipient_2",
},
new JobErrorRecipient()
{
Recipient = "Test_Recipient_3",
},
new JobErrorRecipient()
{
Recipient = "Test_Recipient_4",
}
}
};
var erroredJobAlertRequests = new List<ErroredJobAlertRequest>
{
new ErroredJobAlertRequest
{
CurrentJob = job1,
CurrentWorkItem = new WorkItem()
},
new ErroredJobAlertRequest
{
CurrentJob = job2,
CurrentWorkItem = new WorkItem()
},
new ErroredJobAlertRequest
{
CurrentJob = job3,
CurrentWorkItem = new WorkItem()
}
};
var result = _sendErroredJobAlertService.GroupJobsByRecipients(erroredJobAlertRequests);
Assert.NotNull(result);
Assert.Equal(2, result["[email protected]"].Count);
Assert.Equal(job2, result["test_recipient_1"].FirstOrDefault().CurrentJob);
Assert.Equal(job3, result["test_recipient_4"].FirstOrDefault().CurrentJob);
}
[Fact]
public void GroupJobsByRecipients_Returns_FourRecipients_And_DefaultWithLower_DefaultAsTwoJobs()
{
var job1 = new Job()
{
JobId = 1
};
var job2 = new Job()
{
JobErrorRecipients = new List<JobErrorRecipient>()
{
new JobErrorRecipient()
{
Recipient = "[email protected]",
},
new JobErrorRecipient()
{
Recipient = "Test_Recipient_1",
},
new JobErrorRecipient()
{
Recipient = "Test_Recipient_2",
}
}
};
var job3 = new Job()
{
JobErrorRecipients = new List<JobErrorRecipient>()
{
new JobErrorRecipient()
{
Recipient = "Test_Recipient_2",
},
new JobErrorRecipient()
{
Recipient = "Test_Recipient_3",
},
new JobErrorRecipient()
{
Recipient = "Test_Recipient_4",
}
}
};
var erroredJobAlertRequests = new List<ErroredJobAlertRequest>
{
new ErroredJobAlertRequest
{
CurrentJob = job1,
CurrentWorkItem = new WorkItem()
},
new ErroredJobAlertRequest
{
CurrentJob = job2,
CurrentWorkItem = new WorkItem()
},
new ErroredJobAlertRequest
{
CurrentJob = job3,
CurrentWorkItem = new WorkItem()
}
};
var result = _sendErroredJobAlertService.GroupJobsByRecipients(erroredJobAlertRequests);
Assert.NotNull(result);
Assert.Equal(2, result["[email protected]"].Count);
Assert.Equal(job2, result["test_recipient_1"].FirstOrDefault().CurrentJob);
Assert.Equal(job3, result["test_recipient_4"].FirstOrDefault().CurrentJob);
}
[Fact]
public void AddRecipients_Returns_AddsThree_Groups()
{
var job = new Job()
{
JobErrorRecipients = new List<JobErrorRecipient>()
{
new JobErrorRecipient()
{
Recipient = "Test_Recipient_2",
},
new JobErrorRecipient()
{
Recipient = "Test_Recipient_3",
},
new JobErrorRecipient()
{
Recipient = "Test_Recipient_4",
}
}
};
var groupedRecipients = new Dictionary<string, List<ErroredJobAlertRequest>>();
var erroredJobAlertRequest = new ErroredJobAlertRequest
{
CurrentJob = job,
CurrentWorkItem = new WorkItem()
};
_sendErroredJobAlertService.AddRecipients(erroredJobAlertRequest, groupedRecipients);
Assert.NotNull(groupedRecipients);
Assert.Equal(3, groupedRecipients.Count);
Assert.NotNull(groupedRecipients["test_recipient_2"]);
Assert.NotNull(groupedRecipients["test_recipient_3"]);
Assert.NotNull(groupedRecipients["test_recipient_4"]);
}
[Fact]
public void SetUpJobErrorDetails_NoWorkItem_ErroredOrProcessing_Throws()
{
var erroredJobAlertRequest = new ErroredJobAlertRequest
{
CurrentJob = new Job()
};
Assert.ThrowsAsync<ArgumentNullException>(async () => _sendErroredJobAlertService.SetUpJobErrorDetails(erroredJobAlertRequest));
}
[Fact]
public void SetUpJobErrorDetails_NoWorkItem_ErroredOrProcessing_Return_JobErrorDetail()
{
var workItem = new WorkItem()
{
Name = "Test WorkItem",
WorkItemExecutions = new List<WorkItemExecution>(){
new WorkItemExecution()
{
Status = StatusType.Failed,
StartedTimestamp = Convert.ToDateTime("4/1/2021")
}
}
};
var job = new Job()
{
Name = "Test Job",
NextRunTime = DateTime.Now.AddHours(-1),
WorkItems = new List<WorkItem>() {
workItem
},
JobStatus = JobStatus.Failed
};
var erroredJobAlertRequest = new ErroredJobAlertRequest
{
CurrentJob = job,
CurrentWorkItem = workItem
};
var result = _sendErroredJobAlertService.SetUpJobErrorDetails(erroredJobAlertRequest);
Assert.NotNull(result);
Assert.Equal("Test Job", result.JobName);
Assert.Equal("Test WorkItem", result.WorkItemName);
Assert.Equal("Failed", result.JobStatus);
Assert.Equal(Convert.ToDateTime("4/1/2021").ToString(), result.StartTime);
}
[Fact]
public void CreateErrorRow_Return_Filled_Test_ErrorRowTemplate()
{
var jobErrorDetails = new JobErrorDetail()
{
JobName = "Test Job",
WorkItemName = "Test WorkItem",
JobStatus = "Failed",
StartTime = Convert.ToDateTime("4/1/2021").ToString()
};
var result = _sendErroredJobAlertService.CreateErrorRow(jobErrorDetails);
Assert.Contains("Test Job", result);
Assert.Contains("Test WorkItem", result);
Assert.Contains("Failed", result);
Assert.Contains(Convert.ToDateTime("4/1/2021").ToString(), result);
}
[Fact]
public void CreateBatchErroredJobsEmail_Return_Filled_Test_ErrorRowTemplate()
{
var stringHtmlErrors = "<div><div>Test Job</div><div>Test WorkItem</div><div>Failed</div><div>4/1/2021 12:00:00 AM</div></div>";
var result = _sendErroredJobAlertService.CreateBatchErroredJobsEmail(stringHtmlErrors);
Assert.Contains("Test Job", result);
Assert.Contains("Test WorkItem", result);
Assert.Contains("Failed", result);
Assert.Contains(Convert.ToDateTime("4/1/2021").ToString(), result);
}
}
}