public void GetByStaffId_should_handle_exception() { // Arrange BaseMock.ShouldThrowException = true; MotivationModificatorResponse expectedResponse = new() { Status = new BaseResponse { Code = Code.UnknownError, ErrorMessage = "An error occured while loading motivation modificator data" }, Data = null }; ByStaffIdRequest request = new() { StaffId = _staff2.Id }; LogData expectedLog = new() { CallSide = nameof(MotivationModificatorsService), CallerMethodName = nameof(_motivationModificatorsService.GetByStaffId), CreatedOn = _dateTimeUtil.GetCurrentDateTime(), Request = request, Response = new Exception("Test exception") }; // Act MotivationModificatorResponse actual = _motivationModificatorsService.GetByStaffId(request, null).Result; // Assert Assert.AreEqual(expectedResponse, actual, "Response as expected"); _loggerMock.Verify(mocks => mocks.AddErrorLog(expectedLog), Times.Once); } } }
public void GetByStaffId_should_return_empty_modificator() { // Arrange MotivationModificatorResponse expectedResponse = new() { Status = new BaseResponse { Code = Code.Success, ErrorMessage = string.Empty }, Data = null }; ByStaffIdRequest request = new() { StaffId = _staff2.Id }; LogData expectedLog = new() { CallSide = nameof(MotivationModificatorsService), CallerMethodName = nameof(_motivationModificatorsService.GetByStaffId), CreatedOn = _dateTimeUtil.GetCurrentDateTime(), Request = request, Response = expectedResponse }; // Act MotivationModificatorResponse actual = _motivationModificatorsService.GetByStaffId(request, null).Result; // Assert Assert.AreEqual(expectedResponse, actual, "Response as expected"); _loggerMock.Verify(mocks => mocks.AddLog(expectedLog), Times.Once); } [Test]
public void GetByStaffId_should_return_response_from_grpc_client() { // Arrange ByStaffIdRequest request = new() { StaffId = 1 }; MotivationModificatorResponse response = new() { Status = new BaseResponse { Code = Code.Success, ErrorMessage = string.Empty }, Data = new MotivationModificatorData { Id = 1, ModValue = 1, CreatedOn = Timestamp.FromDateTime(_dateTimeUtil.GetCurrentDateTime()), StaffId = 1 } }; BaseMock.Response = response; LogData expectedLog = new() { CallSide = nameof(MotivationModificatorController), CallerMethodName = nameof(_motivationModificatorController.GetBystaffId), CreatedOn = _dateTimeUtil.GetCurrentDateTime(), Request = request, Response = response }; // Act ObjectResult actual = _motivationModificatorController.GetBystaffId(request.StaffId) as ObjectResult; MotivationModificatorResponse actualData = actual.Value as MotivationModificatorResponse; // 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); _motivationModificatorsClientMock.Verify(m => m.GetByStaffId(request, null, null, new CancellationToken()), Times.Once); } [Test]
public IActionResult GetBystaffId([FromQuery] long staffId) { ByStaffIdRequest request = new() { StaffId = staffId }; try { MotivationModificatorResponse response = _motivationModificatorsClient.GetByStaffId(request); LogData logData = new() { CallSide = nameof(MotivationModificatorController), CallerMethodName = nameof(GetBystaffId), CreatedOn = _dateTimeUtil.GetCurrentDateTime(), Request = request, Response = response }; _logger.AddLog(logData); return(Ok(response)); } catch (Exception ex) { LogData logData = new() { CallSide = nameof(MotivationModificatorController), CallerMethodName = nameof(GetBystaffId), CreatedOn = _dateTimeUtil.GetCurrentDateTime(), Request = request, Response = ex }; _logger.AddErrorLog(logData); return(InternalServerError()); } } } }