using System; using System.Collections.Generic; using System.Threading.Tasks; using LCU.API.Internal.NotificationsApiLib.Models; using LCU.API.Internal.Operations.IncentiveApi.Configuration; using LCU.API.Internal.Operations.IncentiveApi.DataAccess; using LCU.API.Internal.Operations.IncentiveApi.Models.Dayforce; using LCU.API.Internal.Operations.IncentiveApi.Models.GoalDispersal; using LCU.API.Internal.Operations.IncentiveApi.Services.MicroApiServices; using LCU.API.Internal.Operations.IncentiveApiLib.Services; using LCU.Library.Internal.IncentiveApiModels.Entities; using LCU.Library.Internal.IncentiveApiModels.Enums; using Moq; using NUnit.Framework; namespace Application.UnitTests.Services { public class NotificationServiceTest { private INotificationApiService _notificationService; private Mock _mockNotificationsDao; private EmailRecipientsConfiguration _emailConfig; private Mock _incentiveApiConfig; private Mock _mockUserService; [SetUp] public void SetUp() { _mockNotificationsDao = new Mock(); _mockUserService = new Mock(); _incentiveApiConfig = new Mock(); _emailConfig = new EmailRecipientsConfiguration() { TransferRequestInitiatedNotification = new List { "test@test.com" } }; _notificationService = new NotificationApiService(_emailConfig, _incentiveApiConfig.Object, _mockNotificationsDao.Object, _mockUserService.Object); } [Test] public async Task SendMonthStartGoalDispersalReceipt_Ok() { var noErrorsReceipt = new GoalDispersalReceipt(1, GoalDispersalStrategy.ExtrapolateFromExistingGoals); var withErrorsReceipt = new GoalDispersalReceipt(2, GoalDispersalStrategy.ExtrapolateFromExistingGoals); withErrorsReceipt.AddNewDispersalError(GoalDispersalError.ErrorsDuringDisperse, "1234", "Error1", "Batmam"); withErrorsReceipt.AddNewDispersalError(GoalDispersalError.DispersedToUserWithoutPermission, "4567", "Error2", "Ironman"); var defaultReceipt = new GoalDispersalReceipt(3, GoalDispersalStrategy.DefaultDisperseWithWeights); var defaultReceiptWithErrors = new GoalDispersalReceipt(4, GoalDispersalStrategy.DefaultDisperseWithWeights); defaultReceiptWithErrors.AddNewDispersalError(GoalDispersalError.ErrorsDuringDisperse, "8910", "Error3", "Superman"); var reportData = new Dictionary() { { "DeptA - 1", noErrorsReceipt }, { "DeptB - 2", withErrorsReceipt }, { "DeptC - 3", defaultReceipt }, { "DeptD - 4", defaultReceiptWithErrors }, { "DeptE - 6", null } }; _mockNotificationsDao.Setup(x => x.SendMessageAsync(It.IsAny())); await _notificationService.SendMonthStartGoalDispersalReceipt(reportData, new List()); _mockNotificationsDao.Verify(x => x.SendMessageAsync(It.IsAny())); } [Test] public void SendMonthStartGoalDispersalReceipt_Does_Not_Except() { _mockNotificationsDao.Setup(x => x.SendMessageAsync(It.IsAny())) .ThrowsAsync(new Exception("Ahhh")); var result = _notificationService.SendMonthStartGoalDispersalReceipt(new Dictionary() { { "Test", new GoalDispersalReceipt(10, GoalDispersalStrategy.DefaultDisperseWithWeights) } }, new List()); } [Test] public async Task SendMissingRoleNotification_Ok() { var users = new List() { new AppUser() { FullName = "Test One", EmployeeId = "11771", BaseRole = "Base Role", HomeDepartment = "Home" }, new AppUser() { FullName = "Test Two", EmployeeId = "C00002", BaseRole = "Base Role", HomeDepartment = "Home" } }; _mockNotificationsDao.Setup(x => x.SendMessageAsync(It.IsAny())); await _notificationService.SendMissingRoleNotification(users, new List()); _mockNotificationsDao.Verify(x => x.SendMessageAsync(It.IsAny())); } [Test] public async Task SendTerminatedEmployeeNotification_Ok() { var employee = new AppUser() { FullName = "Test One", EmployeeId = "11771", BaseRole = "Base Role", HomeDepartment = "Home", Terminated = new DateTime(2020, 3, 1) }; var manager = new AppUser() { FullName = "Test Two", EmployeeId = "C00002", BaseRole = "Base Role", HomeDepartment = "Home" }; var lastMonth = new PayoutEntry() { Code = "ABC", EmployeeId = "11771", EmployeeName = "Test One", PayoutTotal = 500 }; var thisMonth = new PayoutEntry() { Code = "ABC", EmployeeId = "11771", EmployeeName = "Test One", PayoutTotal = 200 }; var receipts = new List() { new TerminatedEmployeeReceipt(employee,lastMonth,thisMonth,manager) }; _mockNotificationsDao.Setup(x => x.SendMessageAsync(It.IsAny())); await _notificationService.SendTerminatedEmployeeNotification(receipts, new List()); _mockNotificationsDao.Verify(x => x.SendMessageAsync(It.IsAny())); } [Test] public async Task SendTransferRequestInitiatedNotification_Ok() { var transferAction = new ActivityTransferAction { RequestedActivityId = 1, RequestedActivity = new Activity { DepartmentId = 1, Product = Product.CheckCard, Type = ActivityType.Referral }, CreatedBy = "Employee One", NewActivityEmployeeId = "123", NewActivityDepartmentId = 2 }; var recipients = new List { "user1@landmarkcu.com", "user2@landmarkcu.com" }; _mockNotificationsDao.Setup(x => x.SendMessageAsync(It.IsAny())); await _notificationService.SendTransferRequestInitiatedNotification(transferAction, recipients); _mockNotificationsDao.Verify(x => x.SendMessageAsync(It.IsAny())); } [Test] public async Task SendTransferRequestInitiatedNotification_OverrideRecipients() { var transferAction = new ActivityTransferAction { RequestedActivityId = 1, RequestedActivity = new Activity { DepartmentId = 1, Product = Product.CheckCard, Type = ActivityType.Referral }, CreatedBy = "Employee One", NewActivityEmployeeId = "123", NewActivityDepartmentId = 2 }; var originalRecipients = new List { "user1@landmarkcu.com", "user2@landmarkcu.com" }; var actualRecipients = new List { "developer@landmarkcu.com" }; _emailConfig.OverrideRecipients = true; _emailConfig.TransferRequestInitiatedNotification = actualRecipients; var emailRequest = new EmailRequest { Recipient = originalRecipients, Body = "test", Subject = "Test" }; _mockNotificationsDao.Setup(x => x.SendMessageAsync(emailRequest)); await _notificationService.SendTransferRequestInitiatedNotification(transferAction, actualRecipients); emailRequest = new EmailRequest { Recipient = actualRecipients, Body = "test", Subject = "Test" }; _mockNotificationsDao.Verify(notification => notification.SendMessageAsync(It.IsAny()), Times.Once); } } }