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.

128 lines
5.4 KiB

  1. using FunctionalTests.Services.Basket;
  2. using FunctionalTests.Services.Catalog;
  3. using Microsoft.eShopOnContainers.Services.Basket.API.Model;
  4. using Microsoft.eShopOnContainers.Services.Catalog.API.Model;
  5. using Microsoft.eShopOnContainers.Services.Catalog.API.ViewModel;
  6. using Newtonsoft.Json;
  7. using System;
  8. using System.Linq;
  9. using System.Collections.Generic;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using Xunit;
  13. using System.Net.Http;
  14. using System.Threading;
  15. namespace FunctionalTests.Services
  16. {
  17. public class IntegrationEventsScenarios
  18. {
  19. [Fact]
  20. public async Task Post_update_product_price_and_catalog_and_basket_list_modified()
  21. {
  22. decimal priceModification = 0.15M;
  23. string userId = "JohnId";
  24. using (var catalogServer = new CatalogScenariosBase().CreateServer())
  25. using (var basketServer = new BasketScenariosBase().CreateServer())
  26. {
  27. var catalogClient = catalogServer.CreateClient();
  28. var basketClient = basketServer.CreateClient();
  29. // GIVEN a product catalog list
  30. var originalCatalogProducts = await GetCatalogAsync(catalogClient);
  31. // AND a user basket filled with products
  32. var basket = ComposeBasket(userId, originalCatalogProducts.Data.Take(3));
  33. var res = await basketClient.PostAsync(
  34. BasketScenariosBase.Post.CreateBasket,
  35. new StringContent(JsonConvert.SerializeObject(basket), UTF8Encoding.UTF8, "application/json")
  36. );
  37. // WHEN the price of one product is modified in the catalog
  38. var itemToModify = basket.Items[2];
  39. var oldPrice = itemToModify.UnitPrice;
  40. var newPrice = oldPrice + priceModification;
  41. var pRes = await catalogClient.PutAsync(CatalogScenariosBase.Put.UpdateCatalogProduct, new StringContent(ChangePrice(itemToModify, newPrice, originalCatalogProducts), UTF8Encoding.UTF8, "application/json"));
  42. var modifiedCatalogProducts = await GetCatalogAsync(catalogClient);
  43. var itemUpdated = await GetUpdatedBasketItem(newPrice, itemToModify.ProductId, userId, basketClient);
  44. if (itemUpdated == null)
  45. {
  46. Assert.False(true, $"The basket service has not been updated.");
  47. }
  48. else
  49. {
  50. //THEN the product price changes in the catalog
  51. Assert.Equal(newPrice, modifiedCatalogProducts.Data.Single(it => it.Id == int.Parse(itemToModify.ProductId)).Price);
  52. // AND the products in the basket reflects the changed priced and the original price
  53. Assert.Equal(newPrice, itemUpdated.UnitPrice);
  54. Assert.Equal(oldPrice, itemUpdated.OldUnitPrice);
  55. }
  56. }
  57. }
  58. private async Task<BasketItem> GetUpdatedBasketItem(decimal newPrice, string productId, string userId, HttpClient basketClient)
  59. {
  60. bool continueLoop = true;
  61. var counter = 0;
  62. BasketItem itemUpdated = null;
  63. while (continueLoop && counter < 20)
  64. {
  65. //get the basket and verify that the price of the modified product is updated
  66. var basketGetResponse = await basketClient.GetAsync(BasketScenariosBase.Get.GetBasketByCustomer(userId));
  67. var basketUpdated = JsonConvert.DeserializeObject<CustomerBasket>(await basketGetResponse.Content.ReadAsStringAsync());
  68. itemUpdated = basketUpdated.Items.Single(pr => pr.ProductId == productId);
  69. if (itemUpdated.UnitPrice == newPrice)
  70. {
  71. continueLoop = false;
  72. }
  73. else
  74. {
  75. counter++;
  76. await Task.Delay(100);
  77. }
  78. }
  79. return itemUpdated;
  80. }
  81. private async Task<PaginatedItemsViewModel<CatalogItem>> GetCatalogAsync(HttpClient catalogClient)
  82. {
  83. var response = await catalogClient.GetAsync(CatalogScenariosBase.Get.Items);
  84. var items = await response.Content.ReadAsStringAsync();
  85. return JsonConvert.DeserializeObject<PaginatedItemsViewModel<CatalogItem>>(items);
  86. }
  87. private string ChangePrice(BasketItem itemToModify, decimal newPrice, PaginatedItemsViewModel<CatalogItem> catalogProducts)
  88. {
  89. var catalogProduct = catalogProducts.Data.Single(pr => pr.Id == int.Parse(itemToModify.ProductId));
  90. catalogProduct.Price = newPrice;
  91. return JsonConvert.SerializeObject(catalogProduct);
  92. }
  93. private CustomerBasket ComposeBasket(string customerId, IEnumerable<CatalogItem> items)
  94. {
  95. var basket = new CustomerBasket(customerId);
  96. foreach (var item in items)
  97. {
  98. basket.Items.Add(new BasketItem()
  99. {
  100. Id = Guid.NewGuid().ToString(),
  101. UnitPrice = item.Price,
  102. PictureUrl = item.PictureUri,
  103. ProductId = item.Id.ToString(),
  104. OldUnitPrice = 0,
  105. ProductName = item.Name,
  106. Quantity = 1
  107. });
  108. }
  109. return basket;
  110. }
  111. }
  112. }