Added traces for UserCheckoutAcceptedIntegrationEvent
This commit is contained in:
parent
24b660f339
commit
b9839d15c6
@ -5,6 +5,8 @@ using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
|
||||
using Microsoft.eShopOnContainers.Services.Basket.API.Model;
|
||||
using Microsoft.eShopOnContainers.Services.Basket.API.Services;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Serilog.Context;
|
||||
using System;
|
||||
using System.Net;
|
||||
using System.Threading.Tasks;
|
||||
@ -19,9 +21,15 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API.Controllers
|
||||
private readonly IBasketRepository _repository;
|
||||
private readonly IIdentityService _identityService;
|
||||
private readonly IEventBus _eventBus;
|
||||
private readonly ILogger<BasketController> _logger;
|
||||
|
||||
public BasketController(IBasketRepository repository, IIdentityService identityService, IEventBus eventBus)
|
||||
public BasketController(
|
||||
ILogger<BasketController> logger,
|
||||
IBasketRepository repository,
|
||||
IIdentityService identityService,
|
||||
IEventBus eventBus)
|
||||
{
|
||||
_logger = logger;
|
||||
_repository = repository;
|
||||
_identityService = identityService;
|
||||
_eventBus = eventBus;
|
||||
@ -70,7 +78,21 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API.Controllers
|
||||
// Once basket is checkout, sends an integration event to
|
||||
// ordering.api to convert basket to order and proceeds with
|
||||
// order creation process
|
||||
_eventBus.Publish(eventMessage);
|
||||
using (LogContext.PushProperty("IntegrationEventId", eventMessage.Id))
|
||||
{
|
||||
try
|
||||
{
|
||||
_logger.LogInformation("----- BasketController - Publishing integration event: {IntegrationEventId} ({@IntegrationEvent})", eventMessage.Id, eventMessage);
|
||||
|
||||
_eventBus.Publish(eventMessage);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "----- BasketController - ERROR Publishing integration event");
|
||||
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
return Accepted();
|
||||
}
|
||||
|
@ -5,6 +5,7 @@ using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
|
||||
using Microsoft.eShopOnContainers.Services.Basket.API.Controllers;
|
||||
using Microsoft.eShopOnContainers.Services.Basket.API.Model;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Moq;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
@ -20,12 +21,14 @@ namespace UnitTest.Basket.Application
|
||||
private readonly Mock<IBasketRepository> _basketRepositoryMock;
|
||||
private readonly Mock<IBasketIdentityService> _identityServiceMock;
|
||||
private readonly Mock<IEventBus> _serviceBusMock;
|
||||
private readonly Mock<ILogger<BasketController>> _loggerMock;
|
||||
|
||||
public BasketWebApiTest()
|
||||
{
|
||||
_basketRepositoryMock = new Mock<IBasketRepository>();
|
||||
_identityServiceMock = new Mock<IBasketIdentityService>();
|
||||
_serviceBusMock = new Mock<IEventBus>();
|
||||
_loggerMock = new Mock<ILogger<BasketController>>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@ -43,7 +46,11 @@ namespace UnitTest.Basket.Application
|
||||
|
||||
//Act
|
||||
var basketController = new BasketController(
|
||||
_basketRepositoryMock.Object, _identityServiceMock.Object, _serviceBusMock.Object);
|
||||
_loggerMock.Object,
|
||||
_basketRepositoryMock.Object,
|
||||
_identityServiceMock.Object,
|
||||
_serviceBusMock.Object);
|
||||
|
||||
var actionResult = await basketController.GetBasketByIdAsync(fakeCustomerId);
|
||||
|
||||
//Assert
|
||||
@ -65,14 +72,17 @@ namespace UnitTest.Basket.Application
|
||||
|
||||
//Act
|
||||
var basketController = new BasketController(
|
||||
_basketRepositoryMock.Object, _identityServiceMock.Object, _serviceBusMock.Object);
|
||||
_loggerMock.Object,
|
||||
_basketRepositoryMock.Object,
|
||||
_identityServiceMock.Object,
|
||||
_serviceBusMock.Object);
|
||||
|
||||
var actionResult = await basketController.UpdateBasketAsync(fakeCustomerBasket);
|
||||
|
||||
//Assert
|
||||
Assert.Equal((actionResult.Result as OkObjectResult).StatusCode, (int)System.Net.HttpStatusCode.OK);
|
||||
Assert.Equal(((CustomerBasket)actionResult.Value).BuyerId, fakeCustomerId);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Doing_Checkout_Without_Basket_Should_Return_Bad_Request()
|
||||
@ -84,7 +94,10 @@ namespace UnitTest.Basket.Application
|
||||
|
||||
//Act
|
||||
var basketController = new BasketController(
|
||||
_basketRepositoryMock.Object, _identityServiceMock.Object, _serviceBusMock.Object);
|
||||
_loggerMock.Object,
|
||||
_basketRepositoryMock.Object,
|
||||
_identityServiceMock.Object,
|
||||
_serviceBusMock.Object);
|
||||
|
||||
var result = await basketController.CheckoutAsync(new BasketCheckout(), Guid.NewGuid().ToString()) as BadRequestResult;
|
||||
Assert.NotNull(result);
|
||||
@ -102,7 +115,10 @@ namespace UnitTest.Basket.Application
|
||||
_identityServiceMock.Setup(x => x.GetUserIdentity()).Returns(fakeCustomerId);
|
||||
|
||||
var basketController = new BasketController(
|
||||
_basketRepositoryMock.Object, _identityServiceMock.Object, _serviceBusMock.Object);
|
||||
_loggerMock.Object,
|
||||
_basketRepositoryMock.Object,
|
||||
_identityServiceMock.Object,
|
||||
_serviceBusMock.Object);
|
||||
|
||||
basketController.ControllerContext = new ControllerContext()
|
||||
{
|
||||
@ -122,7 +138,7 @@ namespace UnitTest.Basket.Application
|
||||
}
|
||||
|
||||
private CustomerBasket GetCustomerBasketFake(string fakeCustomerId)
|
||||
{
|
||||
{
|
||||
return new CustomerBasket(fakeCustomerId)
|
||||
{
|
||||
Items = new List<BasketItem>()
|
||||
|
@ -5,19 +5,21 @@ using System.Threading.Tasks;
|
||||
using Microsoft.eShopOnContainers.Services.Ordering.API.Application.Commands;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Ordering.API.Application.IntegrationEvents.Events;
|
||||
using Serilog.Context;
|
||||
|
||||
namespace Ordering.API.Application.IntegrationEvents.EventHandling
|
||||
{
|
||||
public class UserCheckoutAcceptedIntegrationEventHandler : IIntegrationEventHandler<UserCheckoutAcceptedIntegrationEvent>
|
||||
{
|
||||
private readonly IMediator _mediator;
|
||||
private readonly ILoggerFactory _logger;
|
||||
private readonly ILogger<UserCheckoutAcceptedIntegrationEventHandler> _logger;
|
||||
|
||||
public UserCheckoutAcceptedIntegrationEventHandler(IMediator mediator,
|
||||
ILoggerFactory logger)
|
||||
public UserCheckoutAcceptedIntegrationEventHandler(
|
||||
IMediator mediator,
|
||||
ILogger<UserCheckoutAcceptedIntegrationEventHandler> logger)
|
||||
{
|
||||
_mediator = mediator ?? throw new ArgumentNullException(nameof(mediator));
|
||||
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
||||
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -31,22 +33,38 @@ namespace Ordering.API.Application.IntegrationEvents.EventHandling
|
||||
/// <returns></returns>
|
||||
public async Task Handle(UserCheckoutAcceptedIntegrationEvent eventMsg)
|
||||
{
|
||||
var result = false;
|
||||
|
||||
if (eventMsg.RequestId != Guid.Empty)
|
||||
using (LogContext.PushProperty("IntegrationEventId", eventMsg.Id))
|
||||
{
|
||||
var createOrderCommand = new CreateOrderCommand(eventMsg.Basket.Items, eventMsg.UserId, eventMsg.UserName, eventMsg.City, eventMsg.Street,
|
||||
eventMsg.State, eventMsg.Country, eventMsg.ZipCode,
|
||||
eventMsg.CardNumber, eventMsg.CardHolderName, eventMsg.CardExpiration,
|
||||
eventMsg.CardSecurityNumber, eventMsg.CardTypeId);
|
||||
_logger.LogInformation("----- UserCheckoutAcceptedIntegrationEventHandler - Handling integration event: {IntegrationEventId} ({@IntegrationEvent})", eventMsg.Id, eventMsg);
|
||||
|
||||
var requestCreateOrder = new IdentifiedCommand<CreateOrderCommand, bool>(createOrderCommand, eventMsg.RequestId);
|
||||
result = await _mediator.Send(requestCreateOrder);
|
||||
}
|
||||
var result = false;
|
||||
|
||||
_logger.CreateLogger(nameof(UserCheckoutAcceptedIntegrationEventHandler))
|
||||
.LogTrace(result ? $"UserCheckoutAccepted integration event has been received and a create new order process is started with requestId: {eventMsg.RequestId}" :
|
||||
$"UserCheckoutAccepted integration event has been received but a new order process has failed with requestId: {eventMsg.RequestId}");
|
||||
if (eventMsg.RequestId != Guid.Empty)
|
||||
{
|
||||
var createOrderCommand = new CreateOrderCommand(eventMsg.Basket.Items, eventMsg.UserId, eventMsg.UserName, eventMsg.City, eventMsg.Street,
|
||||
eventMsg.State, eventMsg.Country, eventMsg.ZipCode,
|
||||
eventMsg.CardNumber, eventMsg.CardHolderName, eventMsg.CardExpiration,
|
||||
eventMsg.CardSecurityNumber, eventMsg.CardTypeId);
|
||||
|
||||
_logger.LogInformation("----- UserCheckoutAcceptedIntegrationEventHandler - CreateOrderCommand: {@CreateOrderCommand}", createOrderCommand);
|
||||
|
||||
var requestCreateOrder = new IdentifiedCommand<CreateOrderCommand, bool>(createOrderCommand, eventMsg.RequestId);
|
||||
result = await _mediator.Send(requestCreateOrder);
|
||||
|
||||
if (result)
|
||||
{
|
||||
_logger.LogInformation("----- UserCheckoutAcceptedIntegrationEventHandler - CreateOrderCommand suceeded - RequestId: {RequestId}", eventMsg.RequestId);
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogWarning("----- UserCheckoutAcceptedIntegrationEventHandler - CreateOrderCommand failed - RequestId: {RequestId}", eventMsg.RequestId);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogWarning("----- UserCheckoutAcceptedIntegrationEventHandler - Invalid IntegrationEvent - RequestId is missing}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user