public IActionResult GetSalary([FromQuery] SalaryRequest request) { try { ISalaryResponse response = _salaryClient.GetSalary(request); LogData logData = new() { CallSide = nameof(SalaryController), CallerMethodName = nameof(GetSalary), CreatedOn = _dateTimeUtil.GetCurrentDateTime(), Request = request, Response = response }; _logger.AddLog(logData); return(Ok(response)); } catch (Exception ex) { LogData logData = new() { CallSide = nameof(SalaryController), CallerMethodName = nameof(GetSalary), CreatedOn = _dateTimeUtil.GetCurrentDateTime(), Request = request, Response = ex }; _logger.AddErrorLog(logData); return(InternalServerError()); } } }
public void GetSalary_should_return_response_from_grpc_client() { // Arrange ISalaryResponse response = new() { Status = new BaseResponse { Code = Code.Success, ErrorMessage = string.Empty } }; response.SalaryResponse.Add(new SalaryResponse { CurrentPosition = 1, ManagerId = 1, PersonId = 2, Salary = 100, StartedOn = Timestamp.FromDateTime(_dateTimeUtil.GetCurrentDateTime()) }); BaseMock.Response = response; SalaryRequest request = new() { ManagerId = 1, StartDate = Timestamp.FromDateTime(_dateTimeUtil.GetCurrentDateTime()), EndDate = Timestamp.FromDateTime(_dateTimeUtil.GetCurrentDateTime().AddMonths(1)) }; LogData expectedLog = new() { CallSide = nameof(SalaryController), CallerMethodName = nameof(_salaryController.GetSalary), CreatedOn = _dateTimeUtil.GetCurrentDateTime(), Request = request, Response = response }; // Act ObjectResult actual = _salaryController.GetSalary(request) as ObjectResult; ISalaryResponse actualData = actual.Value as ISalaryResponse; // Assert Assert.AreEqual(200, actual.StatusCode, "StatusCode as expected"); Assert.AreEqual(response, actualData, "Response data as expected"); _loggerMock.Verify(m => m.AddLog(expectedLog), Times.Once); _salaryClientMock.Verify(m => m.GetSalary(request, null, null, new CancellationToken()), Times.Once); } [Test]
public void GetSalary_should_return_month_salary_only_with_work_days() { // Arrange SalaryResponse expected = new() { CurrentPosition = _position1.Id, PersonId = _staff1.PersonId.GetValueOrDefault(), Salary = 1680, StartedOn = Timestamp.FromDateTime(_dateTimeUtil.GetCurrentDateTime().ToUniversalTime()) }; ISalaryResponse salaryResponse = new() { Status = new BaseResponse { Code = Code.Success, ErrorMessage = string.Empty } }; salaryResponse.SalaryResponse.Add(expected); SalaryRequest request = new() { StartDate = Timestamp.FromDateTime(new DateTime(2021, 1, 1, 0, 0, 0, DateTimeKind.Utc)), EndDate = Timestamp.FromDateTime(new DateTime(2021, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddMonths(1).AddDays(-1)) }; LogData expectedLog = new() { CallSide = nameof(SalaryService), CallerMethodName = nameof(_salaryService.GetSalary), CreatedOn = _dateTimeUtil.GetCurrentDateTime(), Request = request, Response = salaryResponse }; // Act ISalaryResponse response = _salaryService.GetSalary(request, null).Result; // Assert Assert.AreEqual(salaryResponse, response, "Calculated as expected"); _loggerMock.Verify(m => m.AddLog(expectedLog), Times.Once); } [Test] public void GetSalary_should_return_month_salary_for_preson_who_started_work_later_than_start_period_date() { // Arrange _staff1.CreatedOn = new DateTime(2021, 1, 15, 12, 00, 00); _dbContext.Staff.Update(_staff1); SalaryResponse expected = new() { CurrentPosition = _position1.Id, PersonId = _staff1.PersonId.GetValueOrDefault(), Salary = 880, StartedOn = Timestamp.FromDateTime(new DateTime(2021, 1, 15, 12, 00, 00).ToUniversalTime()) }; ISalaryResponse salaryResponse = new() { Status = new BaseResponse { Code = Code.Success, ErrorMessage = string.Empty } }; salaryResponse.SalaryResponse.Add(expected); SalaryRequest request = new() { StartDate = Timestamp.FromDateTime(new DateTime(2021, 1, 1, 0, 0, 0, DateTimeKind.Utc)), EndDate = Timestamp.FromDateTime(new DateTime(2021, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddMonths(1).AddDays(-1)) }; LogData expectedLog = new() { CallSide = nameof(SalaryService), CallerMethodName = nameof(_salaryService.GetSalary), CreatedOn = _dateTimeUtil.GetCurrentDateTime(), Request = request, Response = salaryResponse }; // Act ISalaryResponse response = _salaryService.GetSalary(request, null).Result; // Assert Assert.AreEqual(salaryResponse, response, "Calculated as expected"); _loggerMock.Verify(m => m.AddLog(expectedLog), Times.Once); } [Test] public void GetSalary_should_return_month_salary_with_paid_holidays() { // Arrange SalaryResponse expected = new() { CurrentPosition = _position1.Id, PersonId = _staff1.PersonId.GetValueOrDefault(), Salary = 1680, StartedOn = Timestamp.FromDateTime(_dateTimeUtil.GetCurrentDateTime().ToUniversalTime()) }; Holiday holiday = new() { HolidayDate = new DateTime(2021, 1, 1, 0, 0, 0, DateTimeKind.Utc), Id = 1, Description = "Test" }; _dbContext.Holidays.Add(holiday); ISalaryResponse salaryResponse = new() { Status = new BaseResponse { Code = Code.Success, ErrorMessage = string.Empty } }; salaryResponse.SalaryResponse.Add(expected); SalaryRequest request = new() { StartDate = Timestamp.FromDateTime(new DateTime(2021, 1, 1, 0, 0, 0, DateTimeKind.Utc)), EndDate = Timestamp.FromDateTime(new DateTime(2021, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddMonths(1).AddDays(-1)), ManagerId = 1 }; // Act ISalaryResponse response = _salaryService.GetSalary(request, null).Result; // Assert Assert.AreEqual(salaryResponse, response, "Calculated as expected"); } [Test] public void GetSalary_should_return_month_salary_with_unpaid_holidays() { // Arrange SalaryResponse expected = new() { CurrentPosition = _position1.Id, PersonId = _staff1.PersonId.GetValueOrDefault(), Salary = 1680, StartedOn = Timestamp.FromDateTime(_dateTimeUtil.GetCurrentDateTime().ToUniversalTime()) }; Holiday holiday = new() { HolidayDate = new DateTime(2021, 1, 1, 0, 0, 0, DateTimeKind.Utc), ToDoDate = new DateTime(2021, 1, 2, 0, 0, 0, DateTimeKind.Utc), Id = 1, Description = "Test" }; _dbContext.Holidays.Add(holiday); ISalaryResponse salaryResponse = new() { Status = new BaseResponse { Code = Code.Success, ErrorMessage = string.Empty } }; salaryResponse.SalaryResponse.Add(expected); SalaryRequest request = new() { StartDate = Timestamp.FromDateTime(new DateTime(2021, 1, 1, 0, 0, 0, DateTimeKind.Utc)), EndDate = Timestamp.FromDateTime(new DateTime(2021, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddMonths(1).AddDays(-1)) }; // Act ISalaryResponse response = _salaryService.GetSalary(request, null).Result; // Assert Assert.AreEqual(salaryResponse, response, "Calculated as expected"); } [Test] public void GetSalary_should_return_month_salary_with_paid_dayoff() { // Arrange SalaryResponse expected = new() { CurrentPosition = _position1.Id, PersonId = _staff1.PersonId.GetValueOrDefault(), Salary = 1680, StartedOn = Timestamp.FromDateTime(_dateTimeUtil.GetCurrentDateTime().ToUniversalTime()) }; DayOff dayOff = new() { DayOffType = Enums.DayOffType.Vacation, IsPaid = true, PersonId = _staff1.PersonId.GetValueOrDefault(), Hours = 8, Id = 1, CreatedOn = new DateTime(2021, 1, 1, 0, 0, 0, DateTimeKind.Utc) }; expected.DayOffs.Add(new DayOffInfo { DayOffType = (int)dayOff.DayOffType, Hours = dayOff.Hours }); _dbContext.DaysOff.Add(dayOff); SalaryRequest request = new() { StartDate = Timestamp.FromDateTime(new DateTime(2021, 1, 1, 0, 0, 0, DateTimeKind.Utc)), EndDate = Timestamp.FromDateTime(new DateTime(2021, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddMonths(1).AddDays(-1)) }; ISalaryResponse salaryResponse = new() { Status = new BaseResponse { Code = Code.Success, ErrorMessage = string.Empty } }; salaryResponse.SalaryResponse.Add(expected); // Act ISalaryResponse response = _salaryService.GetSalary(request, null).Result; // Assert Assert.AreEqual(salaryResponse, response, "Calculated as expected"); } [Test] public void GetSalary_should_return_month_salary_with_unpaid_dayoff() { // Arrange SalaryResponse expected = new() { CurrentPosition = _position1.Id, PersonId = _staff1.PersonId.GetValueOrDefault(), Salary = 1600, StartedOn = Timestamp.FromDateTime(_dateTimeUtil.GetCurrentDateTime().ToUniversalTime()) }; DayOff dayOff = new() { DayOffType = Enums.DayOffType.Vacation, IsPaid = false, PersonId = _staff1.PersonId.GetValueOrDefault(), Hours = 8, Id = 1, CreatedOn = new DateTime(2021, 1, 1, 0, 0, 0, DateTimeKind.Utc) }; expected.DayOffs.Add(new DayOffInfo { DayOffType = (int)dayOff.DayOffType, Hours = dayOff.Hours }); _dbContext.DaysOff.Add(dayOff); SalaryRequest request = new() { StartDate = Timestamp.FromDateTime(new DateTime(2021, 1, 1, 0, 0, 0, DateTimeKind.Utc)), EndDate = Timestamp.FromDateTime(new DateTime(2021, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddMonths(1).AddDays(-1)) }; ISalaryResponse salaryResponse = new() { Status = new BaseResponse { Code = Code.Success, ErrorMessage = string.Empty } }; salaryResponse.SalaryResponse.Add(expected); // Act ISalaryResponse response = _salaryService.GetSalary(request, null).Result; // Assert Assert.AreEqual(salaryResponse, response, "Calculated as expected"); } [Test] public void GetSalary_should_return_month_salary_with_partly_unpaid_dayoff() { // Arrange SalaryResponse expected = new() { CurrentPosition = _position1.Id, PersonId = _staff1.PersonId.GetValueOrDefault(), Salary = 1640, StartedOn = Timestamp.FromDateTime(_dateTimeUtil.GetCurrentDateTime().ToUniversalTime()) }; DayOff dayOff = new() { DayOffType = Enums.DayOffType.Vacation, IsPaid = false, PersonId = _staff1.PersonId.GetValueOrDefault(), Hours = 4, Id = 1, CreatedOn = new DateTime(2021, 1, 1, 0, 0, 0, DateTimeKind.Utc) }; expected.DayOffs.Add(new DayOffInfo { DayOffType = (int)dayOff.DayOffType, Hours = dayOff.Hours }); _dbContext.DaysOff.Add(dayOff); SalaryRequest request = new() { StartDate = Timestamp.FromDateTime(new DateTime(2021, 1, 1, 0, 0, 0, DateTimeKind.Utc)), EndDate = Timestamp.FromDateTime(new DateTime(2021, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddMonths(1).AddDays(-1)) }; ISalaryResponse salaryResponse = new() { Status = new BaseResponse { Code = Code.Success, ErrorMessage = string.Empty } }; salaryResponse.SalaryResponse.Add(expected); // Act ISalaryResponse response = _salaryService.GetSalary(request, null).Result; // Assert Assert.AreEqual(salaryResponse, response, "Calculated as expected"); } [Test] public void GetSalary_should_return_month_salary_with_partly_payed_dayoff() { // Arrange SalaryResponse expected = new() { CurrentPosition = _position1.Id, PersonId = _staff1.PersonId.GetValueOrDefault(), Salary = 1680, StartedOn = Timestamp.FromDateTime(_dateTimeUtil.GetCurrentDateTime().ToUniversalTime()) }; DayOff dayOff = new() { DayOffType = Enums.DayOffType.Vacation, IsPaid = true, PersonId = _staff1.PersonId.GetValueOrDefault(), Hours = 4, Id = 1, CreatedOn = new DateTime(2021, 1, 1, 0, 0, 0, DateTimeKind.Utc) }; expected.DayOffs.Add(new DayOffInfo { DayOffType = (int)dayOff.DayOffType, Hours = dayOff.Hours }); _dbContext.DaysOff.Add(dayOff); SalaryRequest request = new() { StartDate = Timestamp.FromDateTime(new DateTime(2021, 1, 1, 0, 0, 0, DateTimeKind.Utc)), EndDate = Timestamp.FromDateTime(new DateTime(2021, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddMonths(1).AddDays(-1)) }; ISalaryResponse salaryResponse = new() { Status = new BaseResponse { Code = Code.Success, ErrorMessage = string.Empty } }; salaryResponse.SalaryResponse.Add(expected); // Act ISalaryResponse response = _salaryService.GetSalary(request, null).Result; // Assert Assert.AreEqual(salaryResponse, response, "Calculated as expected"); } [Test] public void GetSalary_should_return_month_salary_with_motivation_modificator() { // Arrange SalaryResponse expected = new() { CurrentPosition = _position1.Id, PersonId = _staff1.PersonId.GetValueOrDefault(), Salary = 840, StartedOn = Timestamp.FromDateTime(_dateTimeUtil.GetCurrentDateTime().ToUniversalTime()) }; MotivationModificator modificator = new() { Id = 1, ModValue = 0.5, StaffId = _staff1.Id }; _dbContext.MotivationModificators.Add(modificator); _staff1.MotivationModificatorId = modificator.Id; SalaryRequest request = new() { StartDate = Timestamp.FromDateTime(new DateTime(2021, 1, 1, 0, 0, 0, DateTimeKind.Utc)), EndDate = Timestamp.FromDateTime(new DateTime(2021, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddMonths(1).AddDays(-1)) }; ISalaryResponse salaryResponse = new() { Status = new BaseResponse { Code = Code.Success, ErrorMessage = string.Empty } }; salaryResponse.SalaryResponse.Add(expected); // Act ISalaryResponse response = _salaryService.GetSalary(request, null).Result; // Assert Assert.AreEqual(salaryResponse, response, "Calculated as expected"); } [Test] public void GetSalary_should_return_month_salary_with_other_payments() { // Arrange SalaryResponse expected = new() { CurrentPosition = _position1.Id, PersonId = _staff1.PersonId.GetValueOrDefault(), Salary = 1700, StartedOn = Timestamp.FromDateTime(_dateTimeUtil.GetCurrentDateTime().ToUniversalTime()) }; OtherPayment otherPayment = new() { CreatedOn = new DateTime(2021, 1, 15, 0, 0, 0, DateTimeKind.Utc), Id = 1, PersonId = _staff1.PersonId.Value, Value = 20 }; _dbContext.OtherPayments.Add(otherPayment); SalaryRequest request = new() { StartDate = Timestamp.FromDateTime(new DateTime(2021, 1, 1, 0, 0, 0, DateTimeKind.Utc)), EndDate = Timestamp.FromDateTime(new DateTime(2021, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddMonths(1).AddDays(-1)) }; ISalaryResponse salaryResponse = new() { Status = new BaseResponse { Code = Code.Success, ErrorMessage = string.Empty } }; salaryResponse.SalaryResponse.Add(expected); // Act ISalaryResponse response = _salaryService.GetSalary(request, null).Result; // Assert Assert.AreEqual(salaryResponse, response, "Calculated as expected"); } [Test] public void GetSalary_should_handle_null_reference_exception() { // Arrange _dbContext.Positions.Remove(_position1); SalaryRequest request = new() { StartDate = Timestamp.FromDateTime(new DateTime(2021, 1, 1, 0, 0, 0, DateTimeKind.Utc)), EndDate = Timestamp.FromDateTime(new DateTime(2021, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddMonths(1).AddDays(-1)) }; LogData expectedLog = new() { CallSide = nameof(SalaryService), CallerMethodName = nameof(_salaryService.GetSalary), CreatedOn = _dateTimeUtil.GetCurrentDateTime(), Request = request, Response = new Exception("Object reference not set to an instance of an object.") }; // Act ISalaryResponse actual = _salaryService.GetSalary(request, null).Result; // Assert Assert.AreEqual(Code.DataError, actual.Status.Code, "Code returned as expected"); Assert.AreEqual("Some data has not found (type: NullReferenceException)", actual.Status.ErrorMessage, "Error message as expected"); _loggerMock.Verify(m => m.AddErrorLog(expectedLog), Times.Once); } [Test] public void GetSalary_should_handle_base_exception() { // Arrange _dbContext.Positions = null; SalaryRequest request = new() { StartDate = Timestamp.FromDateTime(new DateTime(2021, 1, 1, 0, 0, 0, DateTimeKind.Utc)), EndDate = Timestamp.FromDateTime(new DateTime(2021, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddMonths(1).AddDays(-1)) }; LogData expectedLog = new() { CallSide = nameof(SalaryService), CallerMethodName = nameof(_salaryService.GetSalary), CreatedOn = _dateTimeUtil.GetCurrentDateTime(), Request = request, Response = new Exception("Value cannot be null. (Parameter 'source')") }; // Act ISalaryResponse actual = _salaryService.GetSalary(request, null).Result; // Assert Assert.AreEqual(Code.UnknownError, actual.Status.Code, "Code returned as expected"); Assert.AreEqual("Value cannot be null. (Parameter 'source')", actual.Status.ErrorMessage, "Error message as expected"); _loggerMock.Verify(m => m.AddErrorLog(expectedLog), Times.Once); } } }