using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using LCU.API.Internal.Operations.IncentiveApiLib.Repositories; using LCU.API.Internal.Operations.IncentiveApi.Services; using LCU.Library.Internal.IncentiveApiModels.Entities; using LCU.Library.Internal.IncentiveApiModels.Enums; using Moq; using NUnit.Framework; using LCU.API.Internal.Operations.IncentiveApiLib.Persistence; using LCU.API.Internal.Operations.IncentiveApi.Services.DispersalStrategy; using LCU.API.Internal.Operations.IncentiveApiLib.Services; using LCU.Library.Internal.IncentiveApiModels.Requests; using LCU.Library.Internal.IncentiveApiModels.Common; namespace Application.UnitTests.Services { public class GoalServiceTest { private GoalService _goalService; private IGoalRepository _goalRepository; private IGoalChangeRepository _goalChangeRepository; private ICurrentUserService _currentUserService; private IDepartmentRepository _departmentRepository; private Mock _mockDispersalStrategyService; private IUserService _userService; private IUnitOfWork _unitOfWork; [SetUp] public void Setup() { _currentUserService = Mock.Of(); _userService = Mock.Of(); Mock.Get(_currentUserService).Setup(s => s.GetCurrentUser()).Returns(new ApplicationUser() { Email = "JohnTest@test.com" }); _goalRepository = Mock.Of(); _goalChangeRepository = Mock.Of(); _unitOfWork = Mock.Of(); _mockDispersalStrategyService = new Mock(); _departmentRepository = Mock.Of(); _goalService = new GoalService(_goalRepository, _goalChangeRepository, _currentUserService, _mockDispersalStrategyService.Object, _unitOfWork, _userService, _departmentRepository); } [Test] public async Task GetMonthlyPersonnelGoals_Basic() { var searchRequest = new DepartmentPersonnelSearchRequest() { DepartmentId = 55, Year = 2020, Month = 3 }; var users = new List() { new AppUser() { FullName = "Tester Doe", EmployeeId = "11771" } }; var goals = new List() { new Goal() { Product = Product.Mortgage, Value = 4, EmployeeId = "11771", DepartmentId = 55 }, new Goal() { Product = Product.Vehicle, Value = 4, EmployeeId = "11771", DepartmentId = 55 } }; Mock.Get(_userService).Setup(s => s.FindDepartmentAssignments(searchRequest)) .ReturnsAsync(users); Mock.Get(_goalRepository).Setup(s => s.FindMonthlyPersonnelGoals(searchRequest.DepartmentId, users, searchRequest.Year, searchRequest.Month)) .ReturnsAsync(goals); var result = await _goalService.GetMonthlyPersonnelGoalsAsync(searchRequest); Assert.NotNull(result); Assert.NotNull(result.GoalAudit); Assert.IsNotEmpty(result.GoalAudit); } [Test] public async Task GetEmployeeDepartmentsGoalsForMonth_Basic() { var range = new MonthRange(2021, 1); var user = new AppUser() { FullName = "Tester Doe", EmployeeId = "11771", Departments = new List() }; var userDept = new UserDepartment() { Role = Role.Teller, DepartmentId = 55, Employee = user, IsHome = true, SlidingPayout = 100, Department = new Department() { Id = 55, DepartmentName = "Dept1", State = DepartmentState.Active } }; user.Departments.Add(userDept); var goals = new List() { new Goal() { Product = Product.Mortgage, Value = 4, EmployeeId = "11771", DepartmentId = 55 }, new Goal() { Product = Product.Vehicle, Value = 4, EmployeeId = "11771", DepartmentId = 55 } }; Mock.Get(_goalRepository).Setup(s => s.FindDepartmentGoalsByMonthRange(userDept.DepartmentId, It.IsAny())) .ReturnsAsync(goals); var result = await _goalService.GetEmployeeDepartmentsGoalsForMonthRangeAsync(user, range); Assert.NotNull(result); Assert.NotNull(result); } [Test] public async Task GetEmployeeGoalsForMonth_Basic() { var searchRequest = new DepartmentPersonnelSearchRequest() { DepartmentId = 55, Year = 2020, Month = 3 }; var range = new MonthRange(2021, 1); var user = new AppUser() { FullName = "Tester Doe", EmployeeId = "11771", Departments = new List() }; var userDept = new UserDepartment() { Role = Role.Teller, DepartmentId = 55, Employee = user, IsHome = true, SlidingPayout = 100, Department = new Department() { Id = 55, DepartmentName = "Dept1", State = DepartmentState.Active } }; user.Departments.Add(userDept); var goals = new List() { new Goal() { Product = Product.Mortgage, Value = 4, EmployeeId = "11771", DepartmentId = 55 }, new Goal() { Product = Product.Vehicle, Value = 4, EmployeeId = "11771", DepartmentId = 55 } }; Mock.Get(_goalRepository).Setup(s => s.FindPersonnelGoalsByMonthRange(userDept.DepartmentId, It.IsAny>(), range)) .ReturnsAsync(goals); var result = await _goalService.GetEmployeeGoalsForMonthRangeAsync(user, range); Assert.NotNull(result); Assert.NotNull(result.GoalAudit); Assert.IsNotEmpty(result.GoalAudit); } [Test] public async Task GetDeptGoals() { var goal = new Goal() { Product = Product.CheckCard, DepartmentId = 1, Value = 98, StartDate = DateTime.Parse("2021-07-01T00:00:00.000"), EndDate = DateTime.Parse("2021-07-31T23:59:00.000") }; var goal2 = new Goal() { Product = Product.CheckCard, DepartmentId = 1, Value = 99, StartDate = DateTime.Parse("2021-08-01T00:00:00.000"), EndDate = DateTime.Parse("2021-08-31T23:59:00.000") }; var goal3 = new Goal() { Product = Product.CheckCard, DepartmentId = 1, Value = 100, StartDate = DateTime.Parse("2020-09-01T00:00:00.000"), EndDate = DateTime.Parse("2020-09-30T23:59:00.000") }; var goalDiffDept = new Goal() { Product = Product.CheckCard, DepartmentId = 2, Value = 101, StartDate = DateTime.Parse("2021-09-01T00:00:00.000"), EndDate = DateTime.Parse("2021-09-30T23:59:00.000") }; var inputGoals = new List() { goal, goal2, goal3, goalDiffDept }; var startDate = goal.StartDate; Mock.Get(_goalRepository).Setup(mock => mock.FindAnnualDepartmentGoals(It.IsAny(), It.IsAny())) .ReturnsAsync(inputGoals); var goals = await _goalService.GetAnnualDepartmentGoalsAsync(goal.DepartmentId, startDate.Year); Assert.NotNull(goals); var checkCardGoals = goals.MonthlyGoals[goal.StartDate.Month - 1].ProductGoals[Product.CheckCard]; Assert.AreEqual(goal.Value, checkCardGoals); } [Test] public async Task GetAllDeptGoals() { var goal = new Goal() { Product = Product.CheckCard, DepartmentId = 1, Value = 98, StartDate = DateTime.Parse("2021-07-01T00:00:00.000"), EndDate = DateTime.Parse("2021-07-31T23:59:00.000") }; var goal2 = new Goal() { Product = Product.CheckCard, DepartmentId = 1, Value = 99, StartDate = DateTime.Parse("2021-08-01T00:00:00.000"), EndDate = DateTime.Parse("2021-08-31T23:59:00.000") }; var goalDiffDept = new Goal() { Product = Product.CheckCard, DepartmentId = 2, Value = 100, StartDate = DateTime.Parse("2020-09-01T00:00:00.000"), EndDate = DateTime.Parse("2020-09-30T23:59:00.000") }; var goalDiffDept2 = new Goal() { Product = Product.CheckCard, DepartmentId = 2, Value = 101, StartDate = DateTime.Parse("2021-09-01T00:00:00.000"), EndDate = DateTime.Parse("2021-09-30T23:59:00.000") }; var inputGoals = new List() { goal, goal2 }; var inputGoals2 = new List() { goalDiffDept, goalDiffDept2 }; var departments = new List() { new Department() { Id = 1, DepartmentName = "dept1" }, new Department() { Id = 2, DepartmentName = "dept2" } }; var startDate = goal.StartDate; Mock.Get(_goalRepository).Setup(mock => mock.FindAnnualDepartmentGoals(1, It.IsAny())) .ReturnsAsync(inputGoals); Mock.Get(_goalRepository).Setup(mock => mock.FindAnnualDepartmentGoals(2, It.IsAny())) .ReturnsAsync(inputGoals2); Mock.Get(_departmentRepository).Setup(mock => mock.GetAllDepartmentsAsync()).ReturnsAsync(departments); var goals = await _goalService.GetAllAnnualDepartmentGoalsAsync(startDate.Year); Assert.NotNull(goals); var checkCardGoals = goals.First().MonthlyGoals[goal.StartDate.Month - 1].ProductGoals[Product.CheckCard]; Assert.AreEqual(goal.Value, checkCardGoals); } [Test] public async Task GetGoalChanges_Basic() { var goals = new List() { new GoalChange() { Product = Product.Mortgage, Value = 4, DepartmentId = 55 }, new GoalChange() { Product = Product.Vehicle, Value = 4, DepartmentId = 55 } }; Mock.Get(_goalChangeRepository).Setup(s => s.GetGoalChangesAsync()) .ReturnsAsync(goals); var result = await _goalService.GetGoalChangesAsync(); Assert.NotNull(result); Assert.IsNotEmpty(result); } } }