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);
        }
示例#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 <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());
        }