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.BuildingBlocks.EventBus.Abstractions;
|
||||||
using Microsoft.eShopOnContainers.Services.Basket.API.Model;
|
using Microsoft.eShopOnContainers.Services.Basket.API.Model;
|
||||||
using Microsoft.eShopOnContainers.Services.Basket.API.Services;
|
using Microsoft.eShopOnContainers.Services.Basket.API.Services;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Serilog.Context;
|
||||||
using System;
|
using System;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
@ -19,9 +21,15 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API.Controllers
|
|||||||
private readonly IBasketRepository _repository;
|
private readonly IBasketRepository _repository;
|
||||||
private readonly IIdentityService _identityService;
|
private readonly IIdentityService _identityService;
|
||||||
private readonly IEventBus _eventBus;
|
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;
|
_repository = repository;
|
||||||
_identityService = identityService;
|
_identityService = identityService;
|
||||||
_eventBus = eventBus;
|
_eventBus = eventBus;
|
||||||
@ -70,7 +78,21 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API.Controllers
|
|||||||
// Once basket is checkout, sends an integration event to
|
// Once basket is checkout, sends an integration event to
|
||||||
// ordering.api to convert basket to order and proceeds with
|
// ordering.api to convert basket to order and proceeds with
|
||||||
// order creation process
|
// 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();
|
return Accepted();
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,7 @@ using Microsoft.AspNetCore.Mvc;
|
|||||||
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
|
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
|
||||||
using Microsoft.eShopOnContainers.Services.Basket.API.Controllers;
|
using Microsoft.eShopOnContainers.Services.Basket.API.Controllers;
|
||||||
using Microsoft.eShopOnContainers.Services.Basket.API.Model;
|
using Microsoft.eShopOnContainers.Services.Basket.API.Model;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
using Moq;
|
using Moq;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
@ -20,12 +21,14 @@ namespace UnitTest.Basket.Application
|
|||||||
private readonly Mock<IBasketRepository> _basketRepositoryMock;
|
private readonly Mock<IBasketRepository> _basketRepositoryMock;
|
||||||
private readonly Mock<IBasketIdentityService> _identityServiceMock;
|
private readonly Mock<IBasketIdentityService> _identityServiceMock;
|
||||||
private readonly Mock<IEventBus> _serviceBusMock;
|
private readonly Mock<IEventBus> _serviceBusMock;
|
||||||
|
private readonly Mock<ILogger<BasketController>> _loggerMock;
|
||||||
|
|
||||||
public BasketWebApiTest()
|
public BasketWebApiTest()
|
||||||
{
|
{
|
||||||
_basketRepositoryMock = new Mock<IBasketRepository>();
|
_basketRepositoryMock = new Mock<IBasketRepository>();
|
||||||
_identityServiceMock = new Mock<IBasketIdentityService>();
|
_identityServiceMock = new Mock<IBasketIdentityService>();
|
||||||
_serviceBusMock = new Mock<IEventBus>();
|
_serviceBusMock = new Mock<IEventBus>();
|
||||||
|
_loggerMock = new Mock<ILogger<BasketController>>();
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@ -43,7 +46,11 @@ namespace UnitTest.Basket.Application
|
|||||||
|
|
||||||
//Act
|
//Act
|
||||||
var basketController = new BasketController(
|
var basketController = new BasketController(
|
||||||
_basketRepositoryMock.Object, _identityServiceMock.Object, _serviceBusMock.Object);
|
_loggerMock.Object,
|
||||||
|
_basketRepositoryMock.Object,
|
||||||
|
_identityServiceMock.Object,
|
||||||
|
_serviceBusMock.Object);
|
||||||
|
|
||||||
var actionResult = await basketController.GetBasketByIdAsync(fakeCustomerId);
|
var actionResult = await basketController.GetBasketByIdAsync(fakeCustomerId);
|
||||||
|
|
||||||
//Assert
|
//Assert
|
||||||
@ -65,7 +72,10 @@ namespace UnitTest.Basket.Application
|
|||||||
|
|
||||||
//Act
|
//Act
|
||||||
var basketController = new BasketController(
|
var basketController = new BasketController(
|
||||||
_basketRepositoryMock.Object, _identityServiceMock.Object, _serviceBusMock.Object);
|
_loggerMock.Object,
|
||||||
|
_basketRepositoryMock.Object,
|
||||||
|
_identityServiceMock.Object,
|
||||||
|
_serviceBusMock.Object);
|
||||||
|
|
||||||
var actionResult = await basketController.UpdateBasketAsync(fakeCustomerBasket);
|
var actionResult = await basketController.UpdateBasketAsync(fakeCustomerBasket);
|
||||||
|
|
||||||
@ -84,7 +94,10 @@ namespace UnitTest.Basket.Application
|
|||||||
|
|
||||||
//Act
|
//Act
|
||||||
var basketController = new BasketController(
|
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;
|
var result = await basketController.CheckoutAsync(new BasketCheckout(), Guid.NewGuid().ToString()) as BadRequestResult;
|
||||||
Assert.NotNull(result);
|
Assert.NotNull(result);
|
||||||
@ -102,7 +115,10 @@ namespace UnitTest.Basket.Application
|
|||||||
_identityServiceMock.Setup(x => x.GetUserIdentity()).Returns(fakeCustomerId);
|
_identityServiceMock.Setup(x => x.GetUserIdentity()).Returns(fakeCustomerId);
|
||||||
|
|
||||||
var basketController = new BasketController(
|
var basketController = new BasketController(
|
||||||
_basketRepositoryMock.Object, _identityServiceMock.Object, _serviceBusMock.Object);
|
_loggerMock.Object,
|
||||||
|
_basketRepositoryMock.Object,
|
||||||
|
_identityServiceMock.Object,
|
||||||
|
_serviceBusMock.Object);
|
||||||
|
|
||||||
basketController.ControllerContext = new ControllerContext()
|
basketController.ControllerContext = new ControllerContext()
|
||||||
{
|
{
|
||||||
|
@ -5,16 +5,18 @@ using System.Threading.Tasks;
|
|||||||
using Microsoft.eShopOnContainers.Services.Ordering.API.Application.Commands;
|
using Microsoft.eShopOnContainers.Services.Ordering.API.Application.Commands;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using Ordering.API.Application.IntegrationEvents.Events;
|
using Ordering.API.Application.IntegrationEvents.Events;
|
||||||
|
using Serilog.Context;
|
||||||
|
|
||||||
namespace Ordering.API.Application.IntegrationEvents.EventHandling
|
namespace Ordering.API.Application.IntegrationEvents.EventHandling
|
||||||
{
|
{
|
||||||
public class UserCheckoutAcceptedIntegrationEventHandler : IIntegrationEventHandler<UserCheckoutAcceptedIntegrationEvent>
|
public class UserCheckoutAcceptedIntegrationEventHandler : IIntegrationEventHandler<UserCheckoutAcceptedIntegrationEvent>
|
||||||
{
|
{
|
||||||
private readonly IMediator _mediator;
|
private readonly IMediator _mediator;
|
||||||
private readonly ILoggerFactory _logger;
|
private readonly ILogger<UserCheckoutAcceptedIntegrationEventHandler> _logger;
|
||||||
|
|
||||||
public UserCheckoutAcceptedIntegrationEventHandler(IMediator mediator,
|
public UserCheckoutAcceptedIntegrationEventHandler(
|
||||||
ILoggerFactory logger)
|
IMediator mediator,
|
||||||
|
ILogger<UserCheckoutAcceptedIntegrationEventHandler> logger)
|
||||||
{
|
{
|
||||||
_mediator = mediator ?? throw new ArgumentNullException(nameof(mediator));
|
_mediator = mediator ?? throw new ArgumentNullException(nameof(mediator));
|
||||||
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
||||||
@ -31,22 +33,38 @@ namespace Ordering.API.Application.IntegrationEvents.EventHandling
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public async Task Handle(UserCheckoutAcceptedIntegrationEvent eventMsg)
|
public async Task Handle(UserCheckoutAcceptedIntegrationEvent eventMsg)
|
||||||
{
|
{
|
||||||
var result = false;
|
using (LogContext.PushProperty("IntegrationEventId", eventMsg.Id))
|
||||||
|
|
||||||
if (eventMsg.RequestId != Guid.Empty)
|
|
||||||
{
|
{
|
||||||
var createOrderCommand = new CreateOrderCommand(eventMsg.Basket.Items, eventMsg.UserId, eventMsg.UserName, eventMsg.City, eventMsg.Street,
|
_logger.LogInformation("----- UserCheckoutAcceptedIntegrationEventHandler - Handling integration event: {IntegrationEventId} ({@IntegrationEvent})", eventMsg.Id, eventMsg);
|
||||||
eventMsg.State, eventMsg.Country, eventMsg.ZipCode,
|
|
||||||
eventMsg.CardNumber, eventMsg.CardHolderName, eventMsg.CardExpiration,
|
|
||||||
eventMsg.CardSecurityNumber, eventMsg.CardTypeId);
|
|
||||||
|
|
||||||
var requestCreateOrder = new IdentifiedCommand<CreateOrderCommand, bool>(createOrderCommand, eventMsg.RequestId);
|
var result = false;
|
||||||
result = await _mediator.Send(requestCreateOrder);
|
|
||||||
|
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}");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_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}");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user