diff --git a/src/ApplicationCore/Entities/CatalogItem.cs b/src/ApplicationCore/Entities/CatalogItem.cs index 82f681a..6239815 100644 --- a/src/ApplicationCore/Entities/CatalogItem.cs +++ b/src/ApplicationCore/Entities/CatalogItem.cs @@ -1,12 +1,9 @@ -using System; -using Ardalis.GuardClauses; +using Ardalis.GuardClauses; using Microsoft.eShopWeb.ApplicationCore.Interfaces; -using System.Collections.Generic; +using System; namespace Microsoft.eShopWeb.ApplicationCore.Entities { - - public class CatalogItem : BaseEntity, IAggregateRoot { public string Name { get; private set; } @@ -18,11 +15,11 @@ namespace Microsoft.eShopWeb.ApplicationCore.Entities public int CatalogBrandId { get; private set; } public CatalogBrand CatalogBrand { get; private set; } - public CatalogItem(int catalogTypeId, - int catalogBrandId, - string description, - string name, - decimal price, + public CatalogItem(int catalogTypeId, + int catalogBrandId, + string description, + string name, + decimal price, string pictureUri) { CatalogTypeId = catalogTypeId; diff --git a/src/ApplicationCore/Interfaces/IAsyncRepository.cs b/src/ApplicationCore/Interfaces/IAsyncRepository.cs index baad6e1..94eff20 100644 --- a/src/ApplicationCore/Interfaces/IAsyncRepository.cs +++ b/src/ApplicationCore/Interfaces/IAsyncRepository.cs @@ -1,20 +1,21 @@ using Ardalis.Specification; using Microsoft.eShopWeb.ApplicationCore.Entities; using System.Collections.Generic; +using System.Threading; using System.Threading.Tasks; namespace Microsoft.eShopWeb.ApplicationCore.Interfaces { public interface IAsyncRepository where T : BaseEntity, IAggregateRoot { - Task GetByIdAsync(int id); - Task> ListAllAsync(); - Task> ListAsync(ISpecification spec); - Task AddAsync(T entity); - Task UpdateAsync(T entity); - Task DeleteAsync(T entity); - Task CountAsync(ISpecification spec); - Task FirstAsync(ISpecification spec); - Task FirstOrDefaultAsync(ISpecification spec); + Task GetByIdAsync(int id, CancellationToken cancellationToken = default); + Task> ListAllAsync(CancellationToken cancellationToken = default); + Task> ListAsync(ISpecification spec, CancellationToken cancellationToken = default); + Task AddAsync(T entity, CancellationToken cancellationToken = default); + Task UpdateAsync(T entity, CancellationToken cancellationToken = default); + Task DeleteAsync(T entity, CancellationToken cancellationToken = default); + Task CountAsync(ISpecification spec, CancellationToken cancellationToken = default); + Task FirstAsync(ISpecification spec, CancellationToken cancellationToken = default); + Task FirstOrDefaultAsync(ISpecification spec, CancellationToken cancellationToken = default); } } diff --git a/src/ApplicationCore/Interfaces/IFileSystem.cs b/src/ApplicationCore/Interfaces/IFileSystem.cs index 38be286..29d9f04 100644 --- a/src/ApplicationCore/Interfaces/IFileSystem.cs +++ b/src/ApplicationCore/Interfaces/IFileSystem.cs @@ -1,9 +1,10 @@ -using System.Threading.Tasks; +using System.Threading; +using System.Threading.Tasks; namespace Microsoft.eShopWeb.ApplicationCore.Interfaces { public interface IFileSystem { - Task SavePicture(string pictureName, string pictureBase64); + Task SavePicture(string pictureName, string pictureBase64, CancellationToken cancellationToken); } } diff --git a/src/Infrastructure/Data/EfRepository.cs b/src/Infrastructure/Data/EfRepository.cs index 45f9af1..bd1fdee 100644 --- a/src/Infrastructure/Data/EfRepository.cs +++ b/src/Infrastructure/Data/EfRepository.cs @@ -5,6 +5,7 @@ using Microsoft.eShopWeb.ApplicationCore.Entities; using Microsoft.eShopWeb.ApplicationCore.Interfaces; using System.Collections.Generic; using System.Linq; +using System.Threading; using System.Threading.Tasks; namespace Microsoft.eShopWeb.Infrastructure.Data @@ -23,58 +24,59 @@ namespace Microsoft.eShopWeb.Infrastructure.Data _dbContext = dbContext; } - public virtual async Task GetByIdAsync(int id) + public virtual async Task GetByIdAsync(int id, CancellationToken cancellationToken = default) { - return await _dbContext.Set().FindAsync(id); + var keyValues = new object[] { id }; + return await _dbContext.Set().FindAsync(keyValues, cancellationToken); } - public async Task> ListAllAsync() + public async Task> ListAllAsync(CancellationToken cancellationToken = default) { - return await _dbContext.Set().ToListAsync(); + return await _dbContext.Set().ToListAsync(cancellationToken); } - public async Task> ListAsync(ISpecification spec) + public async Task> ListAsync(ISpecification spec, CancellationToken cancellationToken = default) { var specificationResult = ApplySpecification(spec); - return await specificationResult.ToListAsync(); + return await specificationResult.ToListAsync(cancellationToken); } - public async Task CountAsync(ISpecification spec) + public async Task CountAsync(ISpecification spec, CancellationToken cancellationToken = default) { var specificationResult = ApplySpecification(spec); - return await specificationResult.CountAsync(); + return await specificationResult.CountAsync(cancellationToken); } - public async Task AddAsync(T entity) + public async Task AddAsync(T entity, CancellationToken cancellationToken = default) { await _dbContext.Set().AddAsync(entity); - await _dbContext.SaveChangesAsync(); + await _dbContext.SaveChangesAsync(cancellationToken); return entity; } - public async Task UpdateAsync(T entity) + public async Task UpdateAsync(T entity, CancellationToken cancellationToken = default) { _dbContext.Entry(entity).State = EntityState.Modified; - await _dbContext.SaveChangesAsync(); + await _dbContext.SaveChangesAsync(cancellationToken); } - public async Task DeleteAsync(T entity) + public async Task DeleteAsync(T entity, CancellationToken cancellationToken = default) { _dbContext.Set().Remove(entity); - await _dbContext.SaveChangesAsync(); + await _dbContext.SaveChangesAsync(cancellationToken); } - public async Task FirstAsync(ISpecification spec) + public async Task FirstAsync(ISpecification spec, CancellationToken cancellationToken = default) { var specificationResult = ApplySpecification(spec); - return await specificationResult.FirstAsync(); + return await specificationResult.FirstAsync(cancellationToken); } - public async Task FirstOrDefaultAsync(ISpecification spec) + public async Task FirstOrDefaultAsync(ISpecification spec, CancellationToken cancellationToken = default) { var specificationResult = ApplySpecification(spec); - return await specificationResult.FirstOrDefaultAsync(); + return await specificationResult.FirstOrDefaultAsync(cancellationToken); } private IQueryable ApplySpecification(ISpecification spec) diff --git a/src/Infrastructure/Services/WebFileSystem.cs b/src/Infrastructure/Services/WebFileSystem.cs index edd4c23..b51ba54 100644 --- a/src/Infrastructure/Services/WebFileSystem.cs +++ b/src/Infrastructure/Services/WebFileSystem.cs @@ -1,15 +1,16 @@ -using System; +using Microsoft.eShopWeb.ApplicationCore.Interfaces; +using Microsoft.eShopWeb.Infrastructure.Data; +using System; using System.IO; using System.Net.Http; using System.Text; using System.Text.Json; +using System.Threading; using System.Threading.Tasks; -using Microsoft.eShopWeb.ApplicationCore.Interfaces; -using Microsoft.eShopWeb.Infrastructure.Data; namespace Microsoft.eShopWeb.Infrastructure.Services { - public class WebFileSystem: IFileSystem + public class WebFileSystem : IFileSystem { private readonly HttpClient _httpClient; private readonly string _url; @@ -22,9 +23,9 @@ namespace Microsoft.eShopWeb.Infrastructure.Services _httpClient.DefaultRequestHeaders.Add("auth-key", AUTH_KEY); } - public async Task SavePicture(string pictureName, string pictureBase64) + public async Task SavePicture(string pictureName, string pictureBase64, CancellationToken cancellationToken) { - if (string.IsNullOrEmpty(pictureBase64) || !await UploadFile(pictureName, Convert.FromBase64String(pictureBase64))) + if (string.IsNullOrEmpty(pictureBase64) || !await UploadFile(pictureName, Convert.FromBase64String(pictureBase64), cancellationToken)) { return false; } @@ -32,26 +33,26 @@ namespace Microsoft.eShopWeb.Infrastructure.Services return true; } - private async Task UploadFile(string fileName, byte[] fileData) + private async Task UploadFile(string fileName, byte[] fileData, CancellationToken cancellationToken) { if (!fileData.IsValidImage(fileName)) { return false; } - return await UploadToWeb(fileName, fileData); + return await UploadToWeb(fileName, fileData, cancellationToken); } - private async Task UploadToWeb(string fileName, byte[] fileData) + private async Task UploadToWeb(string fileName, byte[] fileData, CancellationToken cancellationToken) { var request = new FileItem { - DataBase64 = Convert.ToBase64String(fileData), + DataBase64 = Convert.ToBase64String(fileData), FileName = fileName }; var content = new StringContent(JsonSerializer.Serialize(request), Encoding.UTF8, "application/json"); - using var message = await _httpClient.PostAsync(_url, content); + using var message = await _httpClient.PostAsync(_url, content, cancellationToken); if (!message.IsSuccessStatusCode) { return false; diff --git a/src/PublicApi/CatalogBrandEndpoints/List.cs b/src/PublicApi/CatalogBrandEndpoints/List.cs index 40eeef0..197e8ab 100644 --- a/src/PublicApi/CatalogBrandEndpoints/List.cs +++ b/src/PublicApi/CatalogBrandEndpoints/List.cs @@ -33,7 +33,7 @@ namespace Microsoft.eShopWeb.PublicApi.CatalogBrandEndpoints { var response = new ListCatalogBrandsResponse(); - var items = await _catalogBrandRepository.ListAllAsync(); + var items = await _catalogBrandRepository.ListAllAsync(cancellationToken); response.CatalogBrands.AddRange(items.Select(_mapper.Map)); diff --git a/src/PublicApi/CatalogItemEndpoints/Create.cs b/src/PublicApi/CatalogItemEndpoints/Create.cs index 559358b..c1c1068 100644 --- a/src/PublicApi/CatalogItemEndpoints/Create.cs +++ b/src/PublicApi/CatalogItemEndpoints/Create.cs @@ -1,12 +1,12 @@ -using System.IO; -using System.Threading; -using Ardalis.ApiEndpoints; +using Ardalis.ApiEndpoints; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.eShopWeb.ApplicationCore.Entities; using Microsoft.eShopWeb.ApplicationCore.Interfaces; using Swashbuckle.AspNetCore.Annotations; +using System.IO; +using System.Threading; using System.Threading.Tasks; namespace Microsoft.eShopWeb.PublicApi.CatalogItemEndpoints @@ -39,15 +39,15 @@ namespace Microsoft.eShopWeb.PublicApi.CatalogItemEndpoints var newItem = new CatalogItem(request.CatalogTypeId, request.CatalogBrandId, request.Description, request.Name, request.Price, request.PictureUri); - newItem = await _itemRepository.AddAsync(newItem); + newItem = await _itemRepository.AddAsync(newItem, cancellationToken); if (newItem.Id != 0) { var picName = $"{newItem.Id}{Path.GetExtension(request.PictureName)}"; - if (await _webFileSystem.SavePicture(picName, request.PictureBase64)) + if (await _webFileSystem.SavePicture(picName, request.PictureBase64, cancellationToken)) { newItem.UpdatePictureUri(picName); - await _itemRepository.UpdateAsync(newItem); + await _itemRepository.UpdateAsync(newItem, cancellationToken); } } @@ -65,6 +65,6 @@ namespace Microsoft.eShopWeb.PublicApi.CatalogItemEndpoints return response; } - + } } diff --git a/src/PublicApi/CatalogItemEndpoints/Delete.cs b/src/PublicApi/CatalogItemEndpoints/Delete.cs index 654ba4a..e5750a3 100644 --- a/src/PublicApi/CatalogItemEndpoints/Delete.cs +++ b/src/PublicApi/CatalogItemEndpoints/Delete.cs @@ -1,12 +1,11 @@ -using System.Threading; -using Ardalis.ApiEndpoints; +using Ardalis.ApiEndpoints; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; -using Microsoft.eShopWeb.ApplicationCore.Constants; using Microsoft.eShopWeb.ApplicationCore.Entities; using Microsoft.eShopWeb.ApplicationCore.Interfaces; using Swashbuckle.AspNetCore.Annotations; +using System.Threading; using System.Threading.Tasks; namespace Microsoft.eShopWeb.PublicApi.CatalogItemEndpoints @@ -28,14 +27,14 @@ namespace Microsoft.eShopWeb.PublicApi.CatalogItemEndpoints OperationId = "catalog-items.Delete", Tags = new[] { "CatalogItemEndpoints" }) ] - public override async Task> HandleAsync([FromRoute]DeleteCatalogItemRequest request, CancellationToken cancellationToken) + public override async Task> HandleAsync([FromRoute] DeleteCatalogItemRequest request, CancellationToken cancellationToken) { var response = new DeleteCatalogItemResponse(request.CorrelationId()); - var itemToDelete = await _itemRepository.GetByIdAsync(request.CatalogItemId); + var itemToDelete = await _itemRepository.GetByIdAsync(request.CatalogItemId, cancellationToken); if (itemToDelete is null) return NotFound(); - await _itemRepository.DeleteAsync(itemToDelete); + await _itemRepository.DeleteAsync(itemToDelete, cancellationToken); return Ok(response); } diff --git a/src/PublicApi/CatalogItemEndpoints/GetById.cs b/src/PublicApi/CatalogItemEndpoints/GetById.cs index 3a0f62f..5c733ee 100644 --- a/src/PublicApi/CatalogItemEndpoints/GetById.cs +++ b/src/PublicApi/CatalogItemEndpoints/GetById.cs @@ -1,9 +1,9 @@ -using System.Threading; -using Ardalis.ApiEndpoints; +using Ardalis.ApiEndpoints; using Microsoft.AspNetCore.Mvc; using Microsoft.eShopWeb.ApplicationCore.Entities; using Microsoft.eShopWeb.ApplicationCore.Interfaces; using Swashbuckle.AspNetCore.Annotations; +using System.Threading; using System.Threading.Tasks; namespace Microsoft.eShopWeb.PublicApi.CatalogItemEndpoints @@ -30,7 +30,7 @@ namespace Microsoft.eShopWeb.PublicApi.CatalogItemEndpoints { var response = new GetByIdCatalogItemResponse(request.CorrelationId()); - var item = await _itemRepository.GetByIdAsync(request.CatalogItemId); + var item = await _itemRepository.GetByIdAsync(request.CatalogItemId, cancellationToken); if (item is null) return NotFound(); response.CatalogItem = new CatalogItemDto diff --git a/src/PublicApi/CatalogItemEndpoints/ListPaged.cs b/src/PublicApi/CatalogItemEndpoints/ListPaged.cs index a67d681..8c8759d 100644 --- a/src/PublicApi/CatalogItemEndpoints/ListPaged.cs +++ b/src/PublicApi/CatalogItemEndpoints/ListPaged.cs @@ -34,12 +34,12 @@ namespace Microsoft.eShopWeb.PublicApi.CatalogItemEndpoints OperationId = "catalog-items.ListPaged", Tags = new[] { "CatalogItemEndpoints" }) ] - public override async Task> HandleAsync([FromQuery]ListPagedCatalogItemRequest request, CancellationToken cancellationToken) + public override async Task> HandleAsync([FromQuery] ListPagedCatalogItemRequest request, CancellationToken cancellationToken) { var response = new ListPagedCatalogItemResponse(request.CorrelationId()); var filterSpec = new CatalogFilterSpecification(request.CatalogBrandId, request.CatalogTypeId); - int totalItems = await _itemRepository.CountAsync(filterSpec); + int totalItems = await _itemRepository.CountAsync(filterSpec, cancellationToken); var pagedSpec = new CatalogFilterPaginatedSpecification( skip: request.PageIndex * request.PageSize, @@ -47,7 +47,7 @@ namespace Microsoft.eShopWeb.PublicApi.CatalogItemEndpoints brandId: request.CatalogBrandId, typeId: request.CatalogTypeId); - var items = await _itemRepository.ListAsync(pagedSpec); + var items = await _itemRepository.ListAsync(pagedSpec, cancellationToken); response.CatalogItems.AddRange(items.Select(_mapper.Map)); foreach (CatalogItemDto item in response.CatalogItems) diff --git a/src/PublicApi/CatalogItemEndpoints/Update.cs b/src/PublicApi/CatalogItemEndpoints/Update.cs index c93ce91..c24bfb6 100644 --- a/src/PublicApi/CatalogItemEndpoints/Update.cs +++ b/src/PublicApi/CatalogItemEndpoints/Update.cs @@ -37,7 +37,7 @@ namespace Microsoft.eShopWeb.PublicApi.CatalogItemEndpoints { var response = new UpdateCatalogItemResponse(request.CorrelationId()); - var existingItem = await _itemRepository.GetByIdAsync(request.Id); + var existingItem = await _itemRepository.GetByIdAsync(request.Id, cancellationToken); existingItem.UpdateDetails(request.Name, request.Description, request.Price); existingItem.UpdateBrand(request.CatalogBrandId); @@ -50,13 +50,13 @@ namespace Microsoft.eShopWeb.PublicApi.CatalogItemEndpoints else { var picName = $"{existingItem.Id}{Path.GetExtension(request.PictureName)}"; - if (await _webFileSystem.SavePicture($"{picName}", request.PictureBase64)) + if (await _webFileSystem.SavePicture($"{picName}", request.PictureBase64, cancellationToken)) { existingItem.UpdatePictureUri(picName); } } - await _itemRepository.UpdateAsync(existingItem); + await _itemRepository.UpdateAsync(existingItem, cancellationToken); var dto = new CatalogItemDto { diff --git a/src/PublicApi/CatalogTypeEndpoints/List.cs b/src/PublicApi/CatalogTypeEndpoints/List.cs index f13366a..d2f4e32 100644 --- a/src/PublicApi/CatalogTypeEndpoints/List.cs +++ b/src/PublicApi/CatalogTypeEndpoints/List.cs @@ -33,7 +33,7 @@ namespace Microsoft.eShopWeb.PublicApi.CatalogTypeEndpoints { var response = new ListCatalogTypesResponse(); - var items = await _catalogTypeRepository.ListAllAsync(); + var items = await _catalogTypeRepository.ListAllAsync(cancellationToken); response.CatalogTypes.AddRange(items.Select(_mapper.Map)); diff --git a/src/Web/Features/MyOrders/GetMyOrdersHandler.cs b/src/Web/Features/MyOrders/GetMyOrdersHandler.cs index 60e8eda..e66c38a 100644 --- a/src/Web/Features/MyOrders/GetMyOrdersHandler.cs +++ b/src/Web/Features/MyOrders/GetMyOrdersHandler.cs @@ -21,7 +21,7 @@ namespace Microsoft.eShopWeb.Web.Features.MyOrders public async Task> Handle(GetMyOrders request, CancellationToken cancellationToken) { var specification = new CustomerOrdersWithItemsSpecification(request.UserName); - var orders = await _orderRepository.ListAsync(specification); + var orders = await _orderRepository.ListAsync(specification, cancellationToken); return orders.Select(o => new OrderViewModel { diff --git a/src/Web/Features/OrderDetails/GetOrderDetailsHandler.cs b/src/Web/Features/OrderDetails/GetOrderDetailsHandler.cs index b715a57..420e18e 100644 --- a/src/Web/Features/OrderDetails/GetOrderDetailsHandler.cs +++ b/src/Web/Features/OrderDetails/GetOrderDetailsHandler.cs @@ -19,7 +19,7 @@ namespace Microsoft.eShopWeb.Web.Features.OrderDetails public async Task Handle(GetOrderDetails request, CancellationToken cancellationToken) { - var customerOrders = await _orderRepository.ListAsync(new CustomerOrdersWithItemsSpecification(request.UserName)); + var customerOrders = await _orderRepository.ListAsync(new CustomerOrdersWithItemsSpecification(request.UserName), cancellationToken); var order = customerOrders.FirstOrDefault(o => o.Id == request.OrderId); if (order == null) diff --git a/tests/UnitTests/ApplicationCore/Services/BasketServiceTests/AddItemToBasket.cs b/tests/UnitTests/ApplicationCore/Services/BasketServiceTests/AddItemToBasket.cs index 8c31f74..7ee38ec 100644 --- a/tests/UnitTests/ApplicationCore/Services/BasketServiceTests/AddItemToBasket.cs +++ b/tests/UnitTests/ApplicationCore/Services/BasketServiceTests/AddItemToBasket.cs @@ -23,13 +23,13 @@ namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Services.BasketServiceTes { var basket = new Basket(_buyerId); basket.AddItem(1, It.IsAny(), It.IsAny()); - _mockBasketRepo.Setup(x => x.FirstOrDefaultAsync(It.IsAny())).ReturnsAsync(basket); + _mockBasketRepo.Setup(x => x.FirstOrDefaultAsync(It.IsAny(), default)).ReturnsAsync(basket); var basketService = new BasketService(_mockBasketRepo.Object, null); await basketService.AddItemToBasket(basket.Id, 1, 1.50m); - _mockBasketRepo.Verify(x => x.FirstOrDefaultAsync(It.IsAny()), Times.Once); + _mockBasketRepo.Verify(x => x.FirstOrDefaultAsync(It.IsAny(),default), Times.Once); } [Fact] @@ -37,13 +37,13 @@ namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Services.BasketServiceTes { var basket = new Basket(_buyerId); basket.AddItem(1, It.IsAny(), It.IsAny()); - _mockBasketRepo.Setup(x => x.FirstOrDefaultAsync(It.IsAny())).ReturnsAsync(basket); + _mockBasketRepo.Setup(x => x.FirstOrDefaultAsync(It.IsAny(),default)).ReturnsAsync(basket); var basketService = new BasketService(_mockBasketRepo.Object, null); await basketService.AddItemToBasket(basket.Id, 1, 1.50m); - _mockBasketRepo.Verify(x => x.UpdateAsync(basket), Times.Once); + _mockBasketRepo.Verify(x => x.UpdateAsync(basket,default), Times.Once); } } } diff --git a/tests/UnitTests/ApplicationCore/Services/BasketServiceTests/DeleteBasket.cs b/tests/UnitTests/ApplicationCore/Services/BasketServiceTests/DeleteBasket.cs index 4e09a72..17ca679 100644 --- a/tests/UnitTests/ApplicationCore/Services/BasketServiceTests/DeleteBasket.cs +++ b/tests/UnitTests/ApplicationCore/Services/BasketServiceTests/DeleteBasket.cs @@ -23,13 +23,13 @@ namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Services.BasketServiceTes var basket = new Basket(_buyerId); basket.AddItem(1, It.IsAny(), It.IsAny()); basket.AddItem(2, It.IsAny(), It.IsAny()); - _mockBasketRepo.Setup(x => x.GetByIdAsync(It.IsAny())) + _mockBasketRepo.Setup(x => x.GetByIdAsync(It.IsAny(),default)) .ReturnsAsync(basket); var basketService = new BasketService(_mockBasketRepo.Object, null); await basketService.DeleteBasketAsync(It.IsAny()); - _mockBasketRepo.Verify(x => x.DeleteAsync(It.IsAny()), Times.Once); + _mockBasketRepo.Verify(x => x.DeleteAsync(It.IsAny(),default), Times.Once); } } } diff --git a/tests/UnitTests/ApplicationCore/Services/BasketServiceTests/TransferBasket.cs b/tests/UnitTests/ApplicationCore/Services/BasketServiceTests/TransferBasket.cs index deee7c0..8843d87 100644 --- a/tests/UnitTests/ApplicationCore/Services/BasketServiceTests/TransferBasket.cs +++ b/tests/UnitTests/ApplicationCore/Services/BasketServiceTests/TransferBasket.cs @@ -1,12 +1,9 @@ -using Ardalis.Specification; -using Microsoft.eShopWeb.ApplicationCore.Entities.BasketAggregate; +using Microsoft.eShopWeb.ApplicationCore.Entities.BasketAggregate; using Microsoft.eShopWeb.ApplicationCore.Interfaces; using Microsoft.eShopWeb.ApplicationCore.Services; using Microsoft.eShopWeb.ApplicationCore.Specifications; using Moq; using System; -using System.Collections.Generic; -using System.Linq; using System.Threading.Tasks; using Xunit; @@ -46,12 +43,12 @@ namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Services.BasketServiceTes { var anonymousBasket = null as Basket; var userBasket = new Basket(_existentUserBasketBuyerId); - _mockBasketRepo.SetupSequence(x => x.FirstOrDefaultAsync(It.IsAny())) + _mockBasketRepo.SetupSequence(x => x.FirstOrDefaultAsync(It.IsAny(), default)) .ReturnsAsync(anonymousBasket) .ReturnsAsync(userBasket); var basketService = new BasketService(_mockBasketRepo.Object, null); await basketService.TransferBasketAsync(_nonexistentAnonymousBasketBuyerId, _existentUserBasketBuyerId); - _mockBasketRepo.Verify(x => x.FirstOrDefaultAsync(It.IsAny()), Times.Once); + _mockBasketRepo.Verify(x => x.FirstOrDefaultAsync(It.IsAny(), default), Times.Once); } [Fact] @@ -63,12 +60,12 @@ namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Services.BasketServiceTes var userBasket = new Basket(_existentUserBasketBuyerId); userBasket.AddItem(1, 10, 4); userBasket.AddItem(2, 99, 3); - _mockBasketRepo.SetupSequence(x => x.FirstOrDefaultAsync(It.IsAny())) + _mockBasketRepo.SetupSequence(x => x.FirstOrDefaultAsync(It.IsAny(), default)) .ReturnsAsync(anonymousBasket) .ReturnsAsync(userBasket); var basketService = new BasketService(_mockBasketRepo.Object, null); await basketService.TransferBasketAsync(_nonexistentAnonymousBasketBuyerId, _existentUserBasketBuyerId); - _mockBasketRepo.Verify(x => x.UpdateAsync(userBasket), Times.Once); + _mockBasketRepo.Verify(x => x.UpdateAsync(userBasket, default), Times.Once); Assert.Equal(3, userBasket.Items.Count); Assert.Contains(userBasket.Items, x => x.CatalogItemId == 1 && x.UnitPrice == 10 && x.Quantity == 5); Assert.Contains(userBasket.Items, x => x.CatalogItemId == 2 && x.UnitPrice == 99 && x.Quantity == 3); @@ -80,13 +77,13 @@ namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Services.BasketServiceTes { var anonymousBasket = new Basket(_existentAnonymousBasketBuyerId); var userBasket = new Basket(_existentUserBasketBuyerId); - _mockBasketRepo.SetupSequence(x => x.FirstOrDefaultAsync(It.IsAny())) + _mockBasketRepo.SetupSequence(x => x.FirstOrDefaultAsync(It.IsAny(), default)) .ReturnsAsync(anonymousBasket) .ReturnsAsync(userBasket); var basketService = new BasketService(_mockBasketRepo.Object, null); await basketService.TransferBasketAsync(_nonexistentAnonymousBasketBuyerId, _existentUserBasketBuyerId); - _mockBasketRepo.Verify(x => x.UpdateAsync(userBasket), Times.Once); - _mockBasketRepo.Verify(x => x.DeleteAsync(anonymousBasket), Times.Once); + _mockBasketRepo.Verify(x => x.UpdateAsync(userBasket, default), Times.Once); + _mockBasketRepo.Verify(x => x.DeleteAsync(anonymousBasket, default), Times.Once); } [Fact] @@ -94,12 +91,12 @@ namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Services.BasketServiceTes { var anonymousBasket = new Basket(_existentAnonymousBasketBuyerId); var userBasket = null as Basket; - _mockBasketRepo.SetupSequence(x => x.FirstOrDefaultAsync(It.IsAny())) + _mockBasketRepo.SetupSequence(x => x.FirstOrDefaultAsync(It.IsAny(), default)) .ReturnsAsync(anonymousBasket) .ReturnsAsync(userBasket); var basketService = new BasketService(_mockBasketRepo.Object, null); await basketService.TransferBasketAsync(_existentAnonymousBasketBuyerId, _nonexistentUserBasketBuyerId); - _mockBasketRepo.Verify(x => x.AddAsync(It.Is(x => x.BuyerId == _nonexistentUserBasketBuyerId)), Times.Once); + _mockBasketRepo.Verify(x => x.AddAsync(It.Is(x => x.BuyerId == _nonexistentUserBasketBuyerId), default), Times.Once); } } } diff --git a/tests/UnitTests/MediatorHandlers/OrdersTests/GetMyOrders.cs b/tests/UnitTests/MediatorHandlers/OrdersTests/GetMyOrders.cs index 224fd28..3ee3e5c 100644 --- a/tests/UnitTests/MediatorHandlers/OrdersTests/GetMyOrders.cs +++ b/tests/UnitTests/MediatorHandlers/OrdersTests/GetMyOrders.cs @@ -21,7 +21,7 @@ namespace Microsoft.eShopWeb.UnitTests.MediatorHandlers.OrdersTests Order order = new Order("buyerId", address, new List { item }); _mockOrderRepository = new Mock(); - _mockOrderRepository.Setup(x => x.ListAsync(It.IsAny>())).ReturnsAsync(new List { order }); + _mockOrderRepository.Setup(x => x.ListAsync(It.IsAny>(),default)).ReturnsAsync(new List { order }); } [Fact] diff --git a/tests/UnitTests/MediatorHandlers/OrdersTests/GetOrderDetails.cs b/tests/UnitTests/MediatorHandlers/OrdersTests/GetOrderDetails.cs index 720a7f7..bd1d032 100644 --- a/tests/UnitTests/MediatorHandlers/OrdersTests/GetOrderDetails.cs +++ b/tests/UnitTests/MediatorHandlers/OrdersTests/GetOrderDetails.cs @@ -21,7 +21,7 @@ namespace Microsoft.eShopWeb.UnitTests.MediatorHandlers.OrdersTests Order order = new Order("buyerId", address, new List { item }); _mockOrderRepository = new Mock(); - _mockOrderRepository.Setup(x => x.ListAsync(It.IsAny>())).ReturnsAsync(new List { order }); + _mockOrderRepository.Setup(x => x.ListAsync(It.IsAny>(),default)).ReturnsAsync(new List { order }); } [Fact]