From 2911d5821e6d41a4a5c52684d2cf98a8abbf5260 Mon Sep 17 00:00:00 2001 From: Steve Smith Date: Fri, 3 Jul 2020 12:46:33 -0400 Subject: [PATCH] Updated CatalogItem to support more updates; added tests --- src/ApplicationCore/Entities/CatalogItem.cs | 30 +++++++++- .../Update.UpdateCatalogItemRequest.cs | 11 +++- src/PublicApi/CatalogItemEndpoints/Update.cs | 4 +- .../CatalogItemTests/UpdateDetails.cs | 60 +++++++++++++++++++ 4 files changed, 101 insertions(+), 4 deletions(-) create mode 100644 tests/UnitTests/ApplicationCore/Entities/CatalogItemTests/UpdateDetails.cs diff --git a/src/ApplicationCore/Entities/CatalogItem.cs b/src/ApplicationCore/Entities/CatalogItem.cs index caa8562..1dbdcf6 100644 --- a/src/ApplicationCore/Entities/CatalogItem.cs +++ b/src/ApplicationCore/Entities/CatalogItem.cs @@ -1,5 +1,6 @@ using Ardalis.GuardClauses; using Microsoft.eShopWeb.ApplicationCore.Interfaces; +using System.Security.Cryptography; namespace Microsoft.eShopWeb.ApplicationCore.Entities { @@ -14,7 +15,12 @@ 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, string pictureUri) + public CatalogItem(int catalogTypeId, + int catalogBrandId, + string description, + string name, + decimal price, + string pictureUri) { CatalogTypeId = catalogTypeId; CatalogBrandId = catalogBrandId; @@ -30,5 +36,27 @@ namespace Microsoft.eShopWeb.ApplicationCore.Entities Name = name; Price = price; } + + public void UpdateDetails(string name, string description, decimal price) + { + Guard.Against.NullOrEmpty(name, nameof(name)); + Guard.Against.NullOrEmpty(description, nameof(description)); + Guard.Against.NegativeOrZero(price, nameof(price)); + Name = name; + Description = description; + Price = price; + } + + public void UpdateBrand(int catalogBrandId) + { + Guard.Against.Zero(catalogBrandId, nameof(catalogBrandId)); + CatalogBrandId = catalogBrandId; + } + + public void UpdateType(int catalogTypeId) + { + Guard.Against.Zero(catalogTypeId, nameof(catalogTypeId)); + CatalogTypeId = catalogTypeId; + } } } \ No newline at end of file diff --git a/src/PublicApi/CatalogItemEndpoints/Update.UpdateCatalogItemRequest.cs b/src/PublicApi/CatalogItemEndpoints/Update.UpdateCatalogItemRequest.cs index c04de4a..30b36b5 100644 --- a/src/PublicApi/CatalogItemEndpoints/Update.UpdateCatalogItemRequest.cs +++ b/src/PublicApi/CatalogItemEndpoints/Update.UpdateCatalogItemRequest.cs @@ -1,14 +1,21 @@ -namespace Microsoft.eShopWeb.PublicApi.CatalogItemEndpoints +using System.ComponentModel.DataAnnotations; + +namespace Microsoft.eShopWeb.PublicApi.CatalogItemEndpoints { public class UpdateCatalogItemRequest : BaseRequest { + [Range(1, 10000)] public int Id { get; set; } + [Range(1, 10000)] public int CatalogBrandId { get; set; } + [Range(1, 10000)] public int CatalogTypeId { get; set; } + [Required] public string Description { get; set; } + [Required] public string Name { get; set; } public string PictureUri { get; set; } + [Range(0.01, 10000)] public decimal Price { get; set; } } - } diff --git a/src/PublicApi/CatalogItemEndpoints/Update.cs b/src/PublicApi/CatalogItemEndpoints/Update.cs index 9baa9ae..4a17074 100644 --- a/src/PublicApi/CatalogItemEndpoints/Update.cs +++ b/src/PublicApi/CatalogItemEndpoints/Update.cs @@ -33,7 +33,9 @@ namespace Microsoft.eShopWeb.PublicApi.CatalogItemEndpoints var existingItem = await _itemRepository.GetByIdAsync(request.Id); - existingItem.Update(request.Name, request.Price); + existingItem.UpdateDetails(request.Name, request.Description, request.Price); + existingItem.UpdateBrand(request.CatalogBrandId); + existingItem.UpdateType(request.CatalogTypeId); await _itemRepository.UpdateAsync(existingItem); diff --git a/tests/UnitTests/ApplicationCore/Entities/CatalogItemTests/UpdateDetails.cs b/tests/UnitTests/ApplicationCore/Entities/CatalogItemTests/UpdateDetails.cs new file mode 100644 index 0000000..bef89b5 --- /dev/null +++ b/tests/UnitTests/ApplicationCore/Entities/CatalogItemTests/UpdateDetails.cs @@ -0,0 +1,60 @@ +using Microsoft.eShopWeb.ApplicationCore.Entities.OrderAggregate; +using System.Collections.Generic; +using Microsoft.eShopWeb.UnitTests.Builders; +using Xunit; +using Microsoft.eShopWeb.ApplicationCore.Entities; +using System; + +namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Entities.OrderTests +{ + public class UpdateDetails + { + private CatalogItem _testItem; + private decimal _testUnitPrice = 42m; + private int _validTypeId = 1; + private int _validBrandId = 2; + private string _validDescription = "test description"; + private string _validName = "test name"; + private decimal _validPrice = 1.23m; + private string _validUri = "/123"; + + public UpdateDetails() + { + _testItem = new CatalogItem(_validTypeId, _validBrandId, _validDescription, _validName, _validPrice, _validUri); + } + + [Fact] + public void ThrowsArgumentExceptionGivenEmptyName() + { + string newValue = ""; + Assert.Throws(() => _testItem.UpdateDetails(newValue, _validDescription, _validPrice)); + } + + [Fact] + public void ThrowsArgumentExceptionGivenEmptyDescription() + { + string newValue = ""; + Assert.Throws(() => _testItem.UpdateDetails(_validName, newValue, _validPrice)); + } + + [Fact] + public void ThrowsArgumentNullExceptionGivenNullName() + { + Assert.Throws(() => _testItem.UpdateDetails(null, _validDescription, _validPrice)); + } + + [Fact] + public void ThrowsArgumentNullExceptionGivenNullDescription() + { + Assert.Throws(() => _testItem.UpdateDetails(_validName, null, _validPrice)); + } + + [Theory] + [InlineData(0)] + [InlineData(-1.23)] + public void ThrowsArgumentExceptionGivenNonPositivePrice(decimal newPrice) + { + Assert.Throws(() => _testItem.UpdateDetails(_validName, _validDescription, newPrice)); + } + } +}