public void GetByPersonId_should_handle_exception() { // Arrange BaseMock.ShouldThrowException = true; ByPersonIdRequest request = new() { PersonId = 1 }; OtherPaymentsResponse expectedResponse = new() { Status = new BaseResponse { Code = Code.UnknownError, ErrorMessage = "An error ocured while loading other payments" } }; LogData expectedLog = new() { CallSide = nameof(OtherPaymentsService), CallerMethodName = nameof(_otherPaymentsService.GetByPersonId), CreatedOn = _dateTimeUtil.GetCurrentDateTime(), Request = request, Response = new Exception("Test exception") }; // Act OtherPaymentsResponse actual = _otherPaymentsService.GetByPersonId(request, null).Result; // Assert Assert.AreEqual(expectedResponse, actual, "Response as expected"); _loggerMock.Verify(mocks => mocks.AddErrorLog(expectedLog), Times.Once); } [Test]
public IActionResult GetByPersonIdAndDateRange([FromQuery] ByPersonIdAndDateRangeRequest request) { try { OtherPaymentsResponse response = _otherPaymentsClient.GetByPersonIdAndDateRange(request); LogData logData = new() { CallSide = nameof(OtherPaymentsController), CallerMethodName = nameof(GetByPersonIdAndDateRange), CreatedOn = _dateTimeUtil.GetCurrentDateTime(), Request = request, Response = response }; _logger.AddLog(logData); return(Ok(response)); } catch (Exception ex) { LogData logData = new() { CallSide = nameof(OtherPaymentsController), CallerMethodName = nameof(GetByPersonIdAndDateRange), CreatedOn = _dateTimeUtil.GetCurrentDateTime(), Request = request, Response = ex }; _logger.AddErrorLog(logData); return(InternalServerError()); } } }
public void GetByPersonIdAndDateRange_should_return_response_from_grpc_client() { // Arrange ByPersonIdAndDateRangeRequest request = new() { Person = new ByPersonIdRequest { PersonId = 1 }, Range = new ByDateRangeRequest { From = Timestamp.FromDateTime(_dateTimeUtil.GetCurrentDateTime()), To = Timestamp.FromDateTime(_dateTimeUtil.GetCurrentDateTime().AddDays(15)) } }; OtherPaymentsResponse response = new OtherPaymentsResponse { Status = new BaseResponse { Code = Code.Success, ErrorMessage = string.Empty } }; response.Data.Add(new OtherPaymentData { Id = 1, PersonId = 1, CreatedOn = Timestamp.FromDateTime(_dateTimeUtil.GetCurrentDateTime()), Comment = "test", Value = 10 }); BaseMock.Response = response; LogData expectedLog = new() { CallSide = nameof(OtherPaymentsController), CallerMethodName = nameof(_otherPaymentsController.GetByPersonIdAndDateRange), CreatedOn = _dateTimeUtil.GetCurrentDateTime(), Request = request, Response = response }; // Act ObjectResult actual = _otherPaymentsController.GetByPersonIdAndDateRange(request) as ObjectResult; OtherPaymentsResponse actualData = actual.Value as OtherPaymentsResponse; // 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); _otherPaymentsClientMock.Verify(m => m.GetByPersonIdAndDateRange(request, null, null, new CancellationToken()), Times.Once); } [Test]
public void GetByPersonId_should_return_all_other_payments_by_specified_person() { // Arrange ByPersonIdRequest request = new() { PersonId = 1 }; OtherPaymentsResponse expectedResponse = new() { Status = new BaseResponse { Code = Code.Success, ErrorMessage = string.Empty } }; expectedResponse.Data.Add(new OtherPaymentData { Id = _otherPayment1.Id, Comment = _otherPayment1.Comment, CreatedOn = Timestamp.FromDateTime(_otherPayment1.CreatedOn), PersonId = _otherPayment1.PersonId, Value = _otherPayment1.Value }); expectedResponse.Data.Add(new OtherPaymentData { Id = _otherPayment2.Id, Comment = _otherPayment2.Comment, CreatedOn = Timestamp.FromDateTime(_otherPayment2.CreatedOn), PersonId = _otherPayment2.PersonId, Value = _otherPayment2.Value }); LogData expectedLog = new() { CallSide = nameof(OtherPaymentsService), CallerMethodName = nameof(_otherPaymentsService.GetByPersonId), CreatedOn = _dateTimeUtil.GetCurrentDateTime(), Request = request, Response = expectedResponse }; // Act OtherPaymentsResponse actual = _otherPaymentsService.GetByPersonId(request, null).Result; // Assert Assert.AreEqual(expectedResponse, actual, "Response as expected"); _loggerMock.Verify(mocks => mocks.AddLog(expectedLog), Times.Once); } [Test]
public override Task <OtherPaymentsResponse> GetByPersonIdAndDateRange(ByPersonIdAndDateRangeRequest request, ServerCallContext context) { OtherPaymentsResponse response = new OtherPaymentsResponse { Status = new BaseResponse { Code = Code.Success, ErrorMessage = string.Empty } }; try { IQueryable <OtherPayment> otherPayments = _otherPaymentsRepository.GetByPersonIdAndDateRange(request.Person.PersonId, request.Range.From.ToDateTime(), request.Range.To.ToDateTime()); foreach (OtherPayment otherPayment in otherPayments) { response.Data.Add(ToRpcModel(otherPayment)); } LogData logData = new() { CallSide = nameof(OtherPaymentsService), CallerMethodName = nameof(GetByPersonIdAndDateRange), CreatedOn = _dateTimeUtil.GetCurrentDateTime(), Request = request, Response = response }; _logger.AddLog(logData); } catch (Exception ex) { LogData logData = new() { CallSide = nameof(OtherPaymentsService), CallerMethodName = nameof(GetByPersonIdAndDateRange), CreatedOn = _dateTimeUtil.GetCurrentDateTime(), Request = request, Response = ex }; _logger.AddErrorLog(logData); response.Status.Code = Code.UnknownError; response.Status.ErrorMessage = "An error ocured while loading other payments"; } return(Task.FromResult(response)); }