You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

115 lines
5.0 KiB

  1. using Basket.API.IntegrationEvents.Events;
  2. using Basket.API.Model;
  3. using Microsoft.AspNetCore.Mvc;
  4. using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
  5. using Microsoft.eShopOnContainers.Services.Basket.API.Controllers;
  6. using Microsoft.eShopOnContainers.Services.Basket.API.Model;
  7. using Moq;
  8. using System.Collections.Generic;
  9. using System.Threading.Tasks;
  10. using Xunit;
  11. using IBasketIdentityService = Microsoft.eShopOnContainers.Services.Basket.API.Services.IIdentityService;
  12. namespace UnitTest.Basket.Application
  13. {
  14. public class BasketWebApiTest
  15. {
  16. private readonly Mock<IBasketRepository> _basketRepositoryMock;
  17. private readonly Mock<IBasketIdentityService> _identityServiceMock;
  18. private readonly Mock<IEventBus> _serviceBusMock;
  19. public BasketWebApiTest()
  20. {
  21. _basketRepositoryMock = new Mock<IBasketRepository>();
  22. _identityServiceMock = new Mock<IBasketIdentityService>();
  23. _serviceBusMock = new Mock<IEventBus>();
  24. }
  25. [Fact]
  26. public async Task Get_customer_basket_success()
  27. {
  28. //Arrange
  29. var fakeCustomerId = "1";
  30. var fakeCustomerBasket = GetCustomerBasketFake(fakeCustomerId);
  31. _basketRepositoryMock.Setup(x => x.GetBasketAsync(It.IsAny<string>()))
  32. .Returns(Task.FromResult(fakeCustomerBasket));
  33. _identityServiceMock.Setup(x => x.GetUserIdentity()).Returns(fakeCustomerId);
  34. _serviceBusMock.Setup(x => x.Publish(It.IsAny<UserCheckoutAcceptedIntegrationEvent>()));
  35. //Act
  36. var basketController = new BasketController(
  37. _basketRepositoryMock.Object, _identityServiceMock.Object, _serviceBusMock.Object);
  38. var actionResult = await basketController.Get(fakeCustomerId) as OkObjectResult;
  39. //Assert
  40. Assert.Equal(actionResult.StatusCode, (int)System.Net.HttpStatusCode.OK);
  41. Assert.Equal(((CustomerBasket)actionResult.Value).BuyerId, fakeCustomerId);
  42. }
  43. [Fact]
  44. public async Task Post_customer_basket_success()
  45. {
  46. //Arrange
  47. var fakeCustomerId = "1";
  48. var fakeCustomerBasket = GetCustomerBasketFake(fakeCustomerId);
  49. _basketRepositoryMock.Setup(x => x.UpdateBasketAsync(It.IsAny<CustomerBasket>()))
  50. .Returns(Task.FromResult(fakeCustomerBasket));
  51. _identityServiceMock.Setup(x => x.GetUserIdentity()).Returns(fakeCustomerId);
  52. _serviceBusMock.Setup(x => x.Publish(It.IsAny<UserCheckoutAcceptedIntegrationEvent>()));
  53. //Act
  54. var basketController = new BasketController(
  55. _basketRepositoryMock.Object, _identityServiceMock.Object, _serviceBusMock.Object);
  56. var actionResult = await basketController.Post(fakeCustomerBasket) as OkObjectResult;
  57. //Assert
  58. Assert.Equal(actionResult.StatusCode, (int)System.Net.HttpStatusCode.OK);
  59. Assert.Equal(((CustomerBasket)actionResult.Value).BuyerId, fakeCustomerId);
  60. }
  61. [Fact]
  62. public async Task Doing_Checkout_Without_Basket_Should_Return_Bad_Request()
  63. {
  64. var fakeCustomerId = "2";
  65. _basketRepositoryMock.Setup(x => x.GetBasketAsync(It.IsAny<string>()))
  66. .Returns(Task.FromResult((CustomerBasket)null));
  67. _identityServiceMock.Setup(x => x.GetUserIdentity()).Returns(fakeCustomerId);
  68. //Act
  69. var basketController = new BasketController(
  70. _basketRepositoryMock.Object, _identityServiceMock.Object, _serviceBusMock.Object);
  71. var result = await basketController.Checkout(new BasketCheckout()) as BadRequestResult;
  72. Assert.NotNull(result);
  73. }
  74. [Fact]
  75. public async Task Doing_Checkout_Wit_Basket_Should_Publish_UserCheckoutAccepted_Integration_Event()
  76. {
  77. var fakeCustomerId = "1";
  78. var fakeCustomerBasket = GetCustomerBasketFake(fakeCustomerId);
  79. _basketRepositoryMock.Setup(x => x.GetBasketAsync(It.IsAny<string>()))
  80. .Returns(Task.FromResult(fakeCustomerBasket));
  81. _identityServiceMock.Setup(x => x.GetUserIdentity()).Returns(fakeCustomerId);
  82. //Act
  83. var basketController = new BasketController(
  84. _basketRepositoryMock.Object, _identityServiceMock.Object, _serviceBusMock.Object);
  85. var result = await basketController.Checkout(new BasketCheckout()) as AcceptedResult;
  86. _serviceBusMock.Verify(mock => mock.Publish(It.IsAny<UserCheckoutAcceptedIntegrationEvent>()), Times.Once);
  87. Assert.NotNull(result);
  88. }
  89. private CustomerBasket GetCustomerBasketFake(string fakeCustomerId)
  90. {
  91. return new CustomerBasket(fakeCustomerId)
  92. {
  93. Items = new List<BasketItem>()
  94. {
  95. new BasketItem()
  96. }
  97. };
  98. }
  99. }
  100. }