diff --git a/src/Services/Basket/Basket.UnitTests/Application/BasketWebApiTest.cs b/src/Services/Basket/Basket.UnitTests/Application/BasketWebApiTest.cs index 000183dec..89f1c2b33 100644 --- a/src/Services/Basket/Basket.UnitTests/Application/BasketWebApiTest.cs +++ b/src/Services/Basket/Basket.UnitTests/Application/BasketWebApiTest.cs @@ -1,155 +1,140 @@ -using Basket.API.IntegrationEvents.Events; -using Basket.API.Model; -using Microsoft.AspNetCore.Http; -using Microsoft.AspNetCore.Mvc; -using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; -using Microsoft.eShopOnContainers.Services.Basket.API.Controllers; +namespace UnitTest.Basket.Application; + using Microsoft.eShopOnContainers.Services.Basket.API.Model; -using Microsoft.Extensions.Logging; -using Moq; -using System; -using System.Collections.Generic; -using System.Security.Claims; -using System.Threading.Tasks; -using Xunit; -using IBasketIdentityService = Microsoft.eShopOnContainers.Services.Basket.API.Services.IIdentityService; - -namespace UnitTest.Basket.Application + +public class BasketWebApiTest { - public class BasketWebApiTest + private readonly Mock _basketRepositoryMock; + private readonly Mock _identityServiceMock; + private readonly Mock _serviceBusMock; + private readonly Mock> _loggerMock; + + public BasketWebApiTest() { - private readonly Mock _basketRepositoryMock; - private readonly Mock _identityServiceMock; - private readonly Mock _serviceBusMock; - private readonly Mock> _loggerMock; + _basketRepositoryMock = new Mock(); + _identityServiceMock = new Mock(); + _serviceBusMock = new Mock(); + _loggerMock = new Mock>(); + } - public BasketWebApiTest() - { - _basketRepositoryMock = new Mock(); - _identityServiceMock = new Mock(); - _serviceBusMock = new Mock(); - _loggerMock = new Mock>(); - } - - [Fact] - public async Task Get_customer_basket_success() - { - //Arrange - var fakeCustomerId = "1"; - var fakeCustomerBasket = GetCustomerBasketFake(fakeCustomerId); + [Fact] + public async Task Get_customer_basket_success() + { + //Arrange + var fakeCustomerId = "1"; + var fakeCustomerBasket = GetCustomerBasketFake(fakeCustomerId); - _basketRepositoryMock.Setup(x => x.GetBasketAsync(It.IsAny())) - .Returns(Task.FromResult(fakeCustomerBasket)); - _identityServiceMock.Setup(x => x.GetUserIdentity()).Returns(fakeCustomerId); + _basketRepositoryMock.Setup(x => x.GetBasketAsync(It.IsAny())) + .Returns(Task.FromResult(fakeCustomerBasket)); + _identityServiceMock.Setup(x => x.GetUserIdentity()).Returns(fakeCustomerId); - _serviceBusMock.Setup(x => x.Publish(It.IsAny())); + _serviceBusMock.Setup(x => x.Publish(It.IsAny())); - //Act - var basketController = new BasketController( - _loggerMock.Object, - _basketRepositoryMock.Object, - _identityServiceMock.Object, - _serviceBusMock.Object); + //Act + var basketController = new BasketController( + _loggerMock.Object, + _basketRepositoryMock.Object, + _identityServiceMock.Object, + _serviceBusMock.Object); - var actionResult = await basketController.GetBasketByIdAsync(fakeCustomerId); + var actionResult = await basketController.GetBasketByIdAsync(fakeCustomerId); - //Assert - Assert.Equal((actionResult.Result as OkObjectResult).StatusCode, (int)System.Net.HttpStatusCode.OK); - Assert.Equal((((ObjectResult)actionResult.Result).Value as CustomerBasket).BuyerId, fakeCustomerId); - } + //Assert + Assert.Equal((actionResult.Result as OkObjectResult).StatusCode, (int)System.Net.HttpStatusCode.OK); + Assert.Equal((((ObjectResult)actionResult.Result).Value as CustomerBasket).BuyerId, fakeCustomerId); + } - [Fact] - public async Task Post_customer_basket_success() - { - //Arrange - var fakeCustomerId = "1"; - var fakeCustomerBasket = GetCustomerBasketFake(fakeCustomerId); + [Fact] + public async Task Post_customer_basket_success() + { + //Arrange + var fakeCustomerId = "1"; + var fakeCustomerBasket = GetCustomerBasketFake(fakeCustomerId); + + _basketRepositoryMock.Setup(x => x.UpdateBasketAsync(It.IsAny())) + .Returns(Task.FromResult(fakeCustomerBasket)); + _identityServiceMock.Setup(x => x.GetUserIdentity()).Returns(fakeCustomerId); + _serviceBusMock.Setup(x => x.Publish(It.IsAny())); + + //Act + var basketController = new BasketController( + _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((((ObjectResult)actionResult.Result).Value as CustomerBasket).BuyerId, fakeCustomerId); + } - _basketRepositoryMock.Setup(x => x.UpdateBasketAsync(It.IsAny())) - .Returns(Task.FromResult(fakeCustomerBasket)); - _identityServiceMock.Setup(x => x.GetUserIdentity()).Returns(fakeCustomerId); - _serviceBusMock.Setup(x => x.Publish(It.IsAny())); + [Fact] + public async Task Doing_Checkout_Without_Basket_Should_Return_Bad_Request() + { + var fakeCustomerId = "2"; + _basketRepositoryMock.Setup(x => x.GetBasketAsync(It.IsAny())) + .Returns(Task.FromResult((CustomerBasket)null)); + _identityServiceMock.Setup(x => x.GetUserIdentity()).Returns(fakeCustomerId); + + //Act + var basketController = new BasketController( + _loggerMock.Object, + _basketRepositoryMock.Object, + _identityServiceMock.Object, + _serviceBusMock.Object); + + var result = await basketController.CheckoutAsync(new BasketCheckout(), Guid.NewGuid().ToString()) as BadRequestResult; + Assert.NotNull(result); + } - //Act - var basketController = new BasketController( - _loggerMock.Object, - _basketRepositoryMock.Object, - _identityServiceMock.Object, - _serviceBusMock.Object); + [Fact] + public async Task Doing_Checkout_Wit_Basket_Should_Publish_UserCheckoutAccepted_Integration_Event() + { + var fakeCustomerId = "1"; + var fakeCustomerBasket = GetCustomerBasketFake(fakeCustomerId); - var actionResult = await basketController.UpdateBasketAsync(fakeCustomerBasket); + _basketRepositoryMock.Setup(x => x.GetBasketAsync(It.IsAny())) + .Returns(Task.FromResult(fakeCustomerBasket)); - //Assert - Assert.Equal((actionResult.Result as OkObjectResult).StatusCode, (int)System.Net.HttpStatusCode.OK); - Assert.Equal((((ObjectResult)actionResult.Result).Value as CustomerBasket).BuyerId, fakeCustomerId); - } + _identityServiceMock.Setup(x => x.GetUserIdentity()).Returns(fakeCustomerId); - [Fact] - public async Task Doing_Checkout_Without_Basket_Should_Return_Bad_Request() - { - var fakeCustomerId = "2"; - _basketRepositoryMock.Setup(x => x.GetBasketAsync(It.IsAny())) - .Returns(Task.FromResult((CustomerBasket)null)); - _identityServiceMock.Setup(x => x.GetUserIdentity()).Returns(fakeCustomerId); - - //Act - var basketController = new BasketController( - _loggerMock.Object, - _basketRepositoryMock.Object, - _identityServiceMock.Object, - _serviceBusMock.Object); - - var result = await basketController.CheckoutAsync(new BasketCheckout(), Guid.NewGuid().ToString()) as BadRequestResult; - Assert.NotNull(result); - } - - [Fact] - public async Task Doing_Checkout_Wit_Basket_Should_Publish_UserCheckoutAccepted_Integration_Event() + var basketController = new BasketController( + _loggerMock.Object, + _basketRepositoryMock.Object, + _identityServiceMock.Object, + _serviceBusMock.Object); + + basketController.ControllerContext = new ControllerContext() { - var fakeCustomerId = "1"; - var fakeCustomerBasket = GetCustomerBasketFake(fakeCustomerId); + HttpContext = new DefaultHttpContext() + { + User = new ClaimsPrincipal( + new ClaimsIdentity(new Claim[] { + new Claim("sub", "testuser"), + new Claim("unique_name", "testuser"), + new Claim(ClaimTypes.Name, "testuser") + })) + } + }; - _basketRepositoryMock.Setup(x => x.GetBasketAsync(It.IsAny())) - .Returns(Task.FromResult(fakeCustomerBasket)); + //Act + var result = await basketController.CheckoutAsync(new BasketCheckout(), Guid.NewGuid().ToString()) as AcceptedResult; - _identityServiceMock.Setup(x => x.GetUserIdentity()).Returns(fakeCustomerId); + _serviceBusMock.Verify(mock => mock.Publish(It.IsAny()), Times.Once); - var basketController = new BasketController( - _loggerMock.Object, - _basketRepositoryMock.Object, - _identityServiceMock.Object, - _serviceBusMock.Object); + Assert.NotNull(result); + } - basketController.ControllerContext = new ControllerContext() - { - HttpContext = new DefaultHttpContext() - { - User = new ClaimsPrincipal( - new ClaimsIdentity(new Claim[] { - new Claim("sub", "testuser"), - new Claim("unique_name", "testuser"), - new Claim(ClaimTypes.Name, "testuser") - })) - } - }; - - //Act - var result = await basketController.CheckoutAsync(new BasketCheckout(), Guid.NewGuid().ToString()) as AcceptedResult; - - _serviceBusMock.Verify(mock => mock.Publish(It.IsAny()), Times.Once); - - Assert.NotNull(result); - } - - private CustomerBasket GetCustomerBasketFake(string fakeCustomerId) + private CustomerBasket GetCustomerBasketFake(string fakeCustomerId) + { + return new CustomerBasket(fakeCustomerId) { - return new CustomerBasket(fakeCustomerId) + Items = new List() { - Items = new List() - { - new BasketItem() - } - }; - } + new BasketItem() + } + }; } } diff --git a/src/Services/Basket/Basket.UnitTests/Application/CartControllerTest.cs b/src/Services/Basket/Basket.UnitTests/Application/CartControllerTest.cs index bdbf8afd4..4231d6a9e 100644 --- a/src/Services/Basket/Basket.UnitTests/Application/CartControllerTest.cs +++ b/src/Services/Basket/Basket.UnitTests/Application/CartControllerTest.cs @@ -1,130 +1,117 @@ -using Microsoft.AspNetCore.Http; -using Microsoft.AspNetCore.Mvc; -using Microsoft.eShopOnContainers.WebMVC.Controllers; -using Microsoft.eShopOnContainers.WebMVC.Services; -using Microsoft.eShopOnContainers.WebMVC.ViewModels; -using Moq; -using System; -using System.Collections.Generic; -using System.Threading.Tasks; -using Xunit; -using BasketModel = Microsoft.eShopOnContainers.WebMVC.ViewModels.Basket; - -namespace UnitTest.Basket.Application +namespace UnitTest.Basket.Application; + +public class CartControllerTest { - public class CartControllerTest + private readonly Mock _catalogServiceMock; + private readonly Mock _basketServiceMock; + private readonly Mock> _identityParserMock; + private readonly Mock _contextMock; + + public CartControllerTest() { - private readonly Mock _catalogServiceMock; - private readonly Mock _basketServiceMock; - private readonly Mock> _identityParserMock; - private readonly Mock _contextMock; + _catalogServiceMock = new Mock(); + _basketServiceMock = new Mock(); + _identityParserMock = new Mock>(); + _contextMock = new Mock(); + } - public CartControllerTest() - { - _catalogServiceMock = new Mock(); - _basketServiceMock = new Mock(); - _identityParserMock = new Mock>(); - _contextMock = new Mock(); - } - - [Fact] - public async Task Post_cart_success() - { - //Arrange - var fakeBuyerId = "1"; - var action = string.Empty; - var fakeBasket = GetFakeBasket(fakeBuyerId); - var fakeQuantities = new Dictionary() - { - ["fakeProdA"] = 1, - ["fakeProdB"] = 2 - }; - - _basketServiceMock.Setup(x => x.SetQuantities(It.IsAny(), It.IsAny>())) - .Returns(Task.FromResult(fakeBasket)); - - _basketServiceMock.Setup(x => x.UpdateBasket(It.IsAny())) - .Returns(Task.FromResult(fakeBasket)); - - //Act - var cartController = new CartController(_basketServiceMock.Object, _catalogServiceMock.Object, _identityParserMock.Object); - cartController.ControllerContext.HttpContext = _contextMock.Object; - var actionResult = await cartController.Index(fakeQuantities, action); - - //Assert - var viewResult = Assert.IsType(actionResult); - } - - [Fact] - public async Task Post_cart_checkout_success() + [Fact] + public async Task Post_cart_success() + { + //Arrange + var fakeBuyerId = "1"; + var action = string.Empty; + var fakeBasket = GetFakeBasket(fakeBuyerId); + var fakeQuantities = new Dictionary() { - //Arrange - var fakeBuyerId = "1"; - var action = "[ Checkout ]"; - var fakeBasket = GetFakeBasket(fakeBuyerId); - var fakeQuantities = new Dictionary() - { - ["fakeProdA"] = 1, - ["fakeProdB"] = 2 - }; - - _basketServiceMock.Setup(x => x.SetQuantities(It.IsAny(), It.IsAny>())) - .Returns(Task.FromResult(fakeBasket)); - - _basketServiceMock.Setup(x => x.UpdateBasket(It.IsAny())) - .Returns(Task.FromResult(fakeBasket)); - - //Act - var orderController = new CartController(_basketServiceMock.Object, _catalogServiceMock.Object, _identityParserMock.Object); - orderController.ControllerContext.HttpContext = _contextMock.Object; - var actionResult = await orderController.Index(fakeQuantities, action); - - //Assert - var redirectToActionResult = Assert.IsType(actionResult); - Assert.Equal("Order", redirectToActionResult.ControllerName); - Assert.Equal("Create", redirectToActionResult.ActionName); - } - - [Fact] - public async Task Add_to_cart_success() + ["fakeProdA"] = 1, + ["fakeProdB"] = 2 + }; + + _basketServiceMock.Setup(x => x.SetQuantities(It.IsAny(), It.IsAny>())) + .Returns(Task.FromResult(fakeBasket)); + + _basketServiceMock.Setup(x => x.UpdateBasket(It.IsAny())) + .Returns(Task.FromResult(fakeBasket)); + + //Act + var cartController = new CartController(_basketServiceMock.Object, _catalogServiceMock.Object, _identityParserMock.Object); + cartController.ControllerContext.HttpContext = _contextMock.Object; + var actionResult = await cartController.Index(fakeQuantities, action); + + //Assert + var viewResult = Assert.IsType(actionResult); + } + + [Fact] + public async Task Post_cart_checkout_success() + { + //Arrange + var fakeBuyerId = "1"; + var action = "[ Checkout ]"; + var fakeBasket = GetFakeBasket(fakeBuyerId); + var fakeQuantities = new Dictionary() { - //Arrange - var fakeCatalogItem = GetFakeCatalogItem(); + ["fakeProdA"] = 1, + ["fakeProdB"] = 2 + }; + + _basketServiceMock.Setup(x => x.SetQuantities(It.IsAny(), It.IsAny>())) + .Returns(Task.FromResult(fakeBasket)); + + _basketServiceMock.Setup(x => x.UpdateBasket(It.IsAny())) + .Returns(Task.FromResult(fakeBasket)); + + //Act + var orderController = new CartController(_basketServiceMock.Object, _catalogServiceMock.Object, _identityParserMock.Object); + orderController.ControllerContext.HttpContext = _contextMock.Object; + var actionResult = await orderController.Index(fakeQuantities, action); + + //Assert + var redirectToActionResult = Assert.IsType(actionResult); + Assert.Equal("Order", redirectToActionResult.ControllerName); + Assert.Equal("Create", redirectToActionResult.ActionName); + } - _basketServiceMock.Setup(x => x.AddItemToBasket(It.IsAny(), It.IsAny())) - .Returns(Task.FromResult(1)); + [Fact] + public async Task Add_to_cart_success() + { + //Arrange + var fakeCatalogItem = GetFakeCatalogItem(); + + _basketServiceMock.Setup(x => x.AddItemToBasket(It.IsAny(), It.IsAny())) + .Returns(Task.FromResult(1)); - //Act - var orderController = new CartController(_basketServiceMock.Object, _catalogServiceMock.Object, _identityParserMock.Object); - orderController.ControllerContext.HttpContext = _contextMock.Object; - var actionResult = await orderController.AddToCart(fakeCatalogItem); + //Act + var orderController = new CartController(_basketServiceMock.Object, _catalogServiceMock.Object, _identityParserMock.Object); + orderController.ControllerContext.HttpContext = _contextMock.Object; + var actionResult = await orderController.AddToCart(fakeCatalogItem); - //Assert - var redirectToActionResult = Assert.IsType(actionResult); - Assert.Equal("Catalog", redirectToActionResult.ControllerName); - Assert.Equal("Index", redirectToActionResult.ActionName); - } + //Assert + var redirectToActionResult = Assert.IsType(actionResult); + Assert.Equal("Catalog", redirectToActionResult.ControllerName); + Assert.Equal("Index", redirectToActionResult.ActionName); + } - private BasketModel GetFakeBasket(string buyerId) + private BasketModel GetFakeBasket(string buyerId) + { + return new BasketModel() { - return new BasketModel() - { - BuyerId = buyerId - }; - } + BuyerId = buyerId + }; + } - private CatalogItem GetFakeCatalogItem() + private CatalogItem GetFakeCatalogItem() + { + return new CatalogItem() { - return new CatalogItem() - { - Id = 1, - Name = "fakeName", - CatalogBrand = "fakeBrand", - CatalogType = "fakeType", - CatalogBrandId = 2, - CatalogTypeId = 5, - Price = 20 - }; - } + Id = 1, + Name = "fakeName", + CatalogBrand = "fakeBrand", + CatalogType = "fakeType", + CatalogBrandId = 2, + CatalogTypeId = 5, + Price = 20 + }; } }