Sumit Ghosh c37320b3e5
Updates all the services to .NET 6.0 (#1770)
* Created global using file for catalog.api

* Moved individual usings statements to globalusing

* Updated catalog.api project

* Fixed local run bug for catalog.api

* Included globalusing for payment.api

* Refactored namespace statement for payment.api

* Moved namespaces to ordering.domain project

* Included globalusing for ordering.domain project

* Included globalusings for ordering.infrastructure project

* Refactored namespaces for ordering.infrastructure project

* Updated relevant packages in ordering.infrastructure project

* Included globalusings for ordering.signalrHub project

* Moved all the namespace to globalusings

* Updated packages in ordering.signalrHub csproj file

* Refactored namespace statements in catalog.api project

* Fixed namespace name in ordering.domain

* Included global usings for ordering.api project

* Moved all usings to globalusing file

* Updated ordering.api csproj project

* Fixed bug in statup.cs

* Updated ordering.unittests.csproj file

* Included globalusings in webhooks.api project

* Moved using statements to globalusing file in webhooks.api

* Included globalusing for web.bff.shoppping aggregator project

* Moved namespaces to globalusing shopping aggregator

* Included globalusing mobile.bff.shoppping project

* Moved namespaces to globalusing file

* Included globalusing for eventbus project

* Moved namespaces to global usings for eventbus

* Included globalusing for EventBusRabbitMQ project

* Moved using statements to EventBusRabbitMQ project

* Included global using in EventBusServiceBus project

* Moved using statements to globalusing for EventBusServiceBus

* Included globalusing file for IntegrationEventLogEF project

* Move using statements to globalusing file

* Updated packages of IntegrationEventLogEF project

* Included globalusing to Devspaces.Support project

* Moved using statements to globalusing Devspaces

* Updated dependent packages for Devspaces.Support.csproj

* Fixed bug in Basket API

* Fixed bug in catalog.api

* Fixed bug Identity.API

* Included globalusing to Basket.UnitTest project

* Moved namespaces to Basket.UnitTest project

* Updated packages of Basket.UnitTest csproj

* Included globalusing for Basket.FunctionalTests project

* Included file-scoped namespaces Basket.FunctionalTests

* Updated packages of Basket.FunctionalTests.csproj file

* Updated catalog unit test project to Net 6.0

* Included global usings for Catalog.FunctionalTests

* Included file-scope namespace catalog.functionaltests

* Updated packages of catalog.functionaltest csproj

* Included MigrateDbContext method in HostExtensions

* Included globalusing for ordering.UnitTests project

* Included file-scope statement for Ordering.UnitTest project

* Included globalusing for Ordering.FunctionalTests

* Included file-scope namespace statement for using

* Updated packages in  Ordering.FunctionalTests.csproj

* Apply suggestions from code review

Co-authored-by: David Pine <david.pine@microsoft.com>

* Apply suggestions from code review

Co-authored-by: David Pine <david.pine@microsoft.com>

* Update src/Services/Ordering/Ordering.API/Startup.cs

Co-authored-by: David Pine <david.pine@microsoft.com>

* Update src/Services/Ordering/Ordering.Domain/Events/OrderStatusChangedToPaidDomainEvent.cs

Co-authored-by: David Pine <david.pine@microsoft.com>

* Update src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/EventHandling/OrderStatusChangedToSubmittedIntegrationEventHandler.cs

Co-authored-by: David Pine <david.pine@microsoft.com>

* Update src/Services/Ordering/Ordering.SignalrHub/IntegrationEvents/Events/OrderStatusChangedToAwaitingValidationIntegrationEvent.cs

Co-authored-by: David Pine <david.pine@microsoft.com>

* Apply suggestions from code review

Co-authored-by: David Pine <david.pine@microsoft.com>

* Apply suggestions from code review

Co-authored-by: David Pine <david.pine@microsoft.com>

Co-authored-by: David Pine <david.pine@microsoft.com>
2021-10-20 08:23:30 -05:00

141 lines
5.1 KiB
C#

namespace UnitTest.Basket.Application;
using Microsoft.eShopOnContainers.Services.Basket.API.Model;
public class BasketWebApiTest
{
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]
public async Task Get_customer_basket_success()
{
//Arrange
var fakeCustomerId = "1";
var fakeCustomerBasket = GetCustomerBasketFake(fakeCustomerId);
_basketRepositoryMock.Setup(x => x.GetBasketAsync(It.IsAny<string>()))
.Returns(Task.FromResult(fakeCustomerBasket));
_identityServiceMock.Setup(x => x.GetUserIdentity()).Returns(fakeCustomerId);
_serviceBusMock.Setup(x => x.Publish(It.IsAny<UserCheckoutAcceptedIntegrationEvent>()));
//Act
var basketController = new BasketController(
_loggerMock.Object,
_basketRepositoryMock.Object,
_identityServiceMock.Object,
_serviceBusMock.Object);
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);
}
[Fact]
public async Task Post_customer_basket_success()
{
//Arrange
var fakeCustomerId = "1";
var fakeCustomerBasket = GetCustomerBasketFake(fakeCustomerId);
_basketRepositoryMock.Setup(x => x.UpdateBasketAsync(It.IsAny<CustomerBasket>()))
.Returns(Task.FromResult(fakeCustomerBasket));
_identityServiceMock.Setup(x => x.GetUserIdentity()).Returns(fakeCustomerId);
_serviceBusMock.Setup(x => x.Publish(It.IsAny<UserCheckoutAcceptedIntegrationEvent>()));
//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);
}
[Fact]
public async Task Doing_Checkout_Without_Basket_Should_Return_Bad_Request()
{
var fakeCustomerId = "2";
_basketRepositoryMock.Setup(x => x.GetBasketAsync(It.IsAny<string>()))
.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 fakeCustomerId = "1";
var fakeCustomerBasket = GetCustomerBasketFake(fakeCustomerId);
_basketRepositoryMock.Setup(x => x.GetBasketAsync(It.IsAny<string>()))
.Returns(Task.FromResult(fakeCustomerBasket));
_identityServiceMock.Setup(x => x.GetUserIdentity()).Returns(fakeCustomerId);
var basketController = new BasketController(
_loggerMock.Object,
_basketRepositoryMock.Object,
_identityServiceMock.Object,
_serviceBusMock.Object);
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<UserCheckoutAcceptedIntegrationEvent>()), Times.Once);
Assert.NotNull(result);
}
private CustomerBasket GetCustomerBasketFake(string fakeCustomerId)
{
return new CustomerBasket(fakeCustomerId)
{
Items = new List<BasketItem>()
{
new BasketItem()
}
};
}
}