public async Task CalculateAsync_Should_Return_Correct_Result()
        {
            // Arrange
            var employeeServiceMock      = new Mock <IEmployeeService>();
            var mappingServiceMock       = new Mock <IMappingService>();
            var calculationServiceMock   = new Mock <ICalculationService>();
            int bonusPoolAmount          = 10000;
            int selectedEmployeeId       = 1;
            int totalCompanySalaryBudget = 100000;
            var employee   = new Employee(1, "Amanda Woods", "Product Owner", 50000, 1);
            var department = new DepartmentDto()
            {
                Title       = "IT",
                Description = "The software development department for the company"
            };
            var mappedEmployee = new EmployeeDto
            {
                Id         = 1,
                Fullname   = "Amanda Woods",
                JobTitle   = "Product Owner",
                Salary     = 60000,
                Department = department
            };

            decimal expectedEmployeeBonusPercentage = (decimal)employee.Salary / (decimal)totalCompanySalaryBudget;
            int     expectedBonusAmount             = (int)(expectedEmployeeBonusPercentage * bonusPoolAmount);
            var     mappedBonusPoolCalculatorResult = new BonusPoolCalculatorResultDto
            {
                Employee = mappedEmployee,
                Amount   = expectedBonusAmount
            };

            calculationServiceMock
            .Setup(x => x.CalculateTotalSalaryBudgetForCompany())
            .ReturnsAsync(totalCompanySalaryBudget);
            employeeServiceMock.Setup(x => x.GetEmployeeByIdAsync(It.IsAny <int>())).ReturnsAsync(employee);
            calculationServiceMock
            .Setup(x => x.CalculateEmployeeBonusAllocation(It.IsAny <decimal>(), It.IsAny <decimal>()))
            .Returns(expectedEmployeeBonusPercentage);
            calculationServiceMock
            .Setup(x => x.CalculateEmployeeBonus(It.IsAny <decimal>(), It.IsAny <int>()))
            .Returns(expectedBonusAmount);
            mappingServiceMock
            .Setup(x => x.MapBonusPoolCalculatorResultToDto(It.IsAny <Employee>(), It.IsAny <int>()))
            .Returns(mappedBonusPoolCalculatorResult);
            var service = new BonusPoolService(employeeServiceMock.Object,
                                               calculationServiceMock.Object,
                                               mappingServiceMock.Object);

            // Act
            var result = await service.CalculateAsync(bonusPoolAmount, selectedEmployeeId);

            // Assert
            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(BonusPoolCalculatorResultDto));
            Assert.AreEqual(mappedBonusPoolCalculatorResult, result);
            Assert.AreEqual(expectedBonusAmount, result.Amount);
            Assert.AreEqual(mappedEmployee, result.Employee);
        }
Exemple #2
0
        public async Task <IActionResult> CalculateBonus([FromBody] CalculateBonusDto request)
        {
            var bonusPoolService = new BonusPoolService();

            return(Ok(await bonusPoolService.CalculateAsync(
                          request.TotalBonusPoolAmount,
                          request.SelectedEmployeeId)));
        }
        public async Task GetEmployeesAsync_Should_Return_Correct_Results()
        {
            // Arrange
            var employeeServiceMock    = new Mock <IEmployeeService>();
            var mappingServiceMock     = new Mock <IMappingService>();
            var calculationServiceMock = new Mock <ICalculationService>();
            var employees = new List <Employee>
            {
                new Employee(1, "Amanda Woods", "Product Owner", 60000, 1),
                new Employee(2, "Ross Green", "Software Developer", 70000, 1),
            };
            var department = new DepartmentDto()
            {
                Title       = "IT",
                Description = "The software development department for the company"
            };
            var mappedEmployees = new List <EmployeeDto>
            {
                new EmployeeDto()
                {
                    Id         = 1,
                    Fullname   = "Amanda Woods",
                    JobTitle   = "Product Owner",
                    Salary     = 60000,
                    Department = department
                },
                new EmployeeDto()
                {
                    Id         = 2,
                    Fullname   = "Ross Green",
                    JobTitle   = "Software Developer",
                    Salary     = 70000,
                    Department = department
                },
            };
            var expectedNameFirstEmployee  = "Amanda Woods";
            var expectedNameSecondEmployee = "Ross Green";
            var expectedCollectionCount    = 2;

            mappingServiceMock
            .Setup(x => x.MapEmployeeCollectionToDto(It.IsAny <IEnumerable <Employee> >()))
            .Returns(mappedEmployees);
            var service = new BonusPoolService(employeeServiceMock.Object,
                                               calculationServiceMock.Object,
                                               mappingServiceMock.Object);

            // Act
            var result = await service.GetEmployeesAsync();

            // Assert
            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(IEnumerable <EmployeeDto>));
            Assert.AreEqual(expectedCollectionCount, result.Count());
            Assert.AreEqual(mappedEmployees, result);
            Assert.AreEqual(expectedNameFirstEmployee, result.FirstOrDefault().Fullname);
            Assert.AreEqual(expectedNameSecondEmployee, result.LastOrDefault().Fullname);
        }
        public void Calculate_CorrectPercentage(
            int bonusPoolAmount,
            int totalSalary,
            int employeeSalary,
            double expected)
        {
            var bonusService = new BonusPoolService();
            var actual       = bonusService.Calculate(bonusPoolAmount, totalSalary, employeeSalary);

            Assert.AreEqual((decimal)expected, actual);
        }
Exemple #5
0
        public BonusPoolService GetBonusPoolService(bool populateRepository = false)
        {
            var context = new Mock <IRepository <EmployeeDto> >();

            if (populateRepository)
            {
                context.Setup(x => x.GetAll()).Returns(GetStubEmployees());
                context.Setup(x => x.Get(3)).Returns(GetStubEmployees().LastOrDefault(x => x.Id == 3));
            }

            var mapper           = new MappingHelper <DomainMapperProfile>();
            var bonusPoolService = new BonusPoolService(context.Object, mapper);

            return(bonusPoolService);
        }
        public async Task <IActionResult> PutEmployee([FromRoute] int id, [FromBody] Employee employee)
        {
            BonusPoolService bonuspool = new BonusPoolService(_context);

            int      amount = Convert.ToInt32(employee.Bonus);
            Employee e      = await bonuspool.CalculateAsync(id, amount);

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != employee.EmployeeId)
            {
                return(BadRequest());
            }

            _context.Entry(e).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!EmployeeExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
 public void TestSetup()
 {
     _employeeRepositoryMock = new Mock <IEmployeeRepository>();
     _bonusPoolService       = new BonusPoolService(_employeeRepositoryMock.Object);
 }
Exemple #8
0
        public async Task <IActionResult> GetAll()
        {
            var bonusPoolService = new BonusPoolService();

            return(Ok(await bonusPoolService.GetEmployeesAsync()));
        }