private void ValidateNull(ProductMetric productSize) { if (productSize == null) { throw new ArgumentNullException("ProductMetric cannot be null"); } }
private void ValidateMetricXValue(ProductMetric productMetric) { if (string.IsNullOrEmpty(productMetric.MetricX)) { throw new ArgumentException("You need to specify a MetricX for the ProductMetric"); } }
public void Create_ProductMetricNonExisting_ThrowsArgumentException() { //Arrange ProductSize invalidProductSize = new ProductSize { ProductMetric = new ProductMetric { Id = 1 }, Size = "L", MetricXValue = 70, MetricYValue = 100, MetricZValue = 75 }; ProductMetric nullProductMetric = null; Mock <IProductSizeRepository> productSizeRepository = new Mock <IProductSizeRepository>(); Mock <IProductMetricRepository> productMetricRepository = new Mock <IProductMetricRepository>(); productMetricRepository.Setup(repo => repo.Read(invalidProductSize.ProductMetric.Id)). Returns(nullProductMetric); IProductSizeService productSizeService = new ProductSizeService(productSizeRepository.Object, productMetricRepository.Object); //Act Action actual = () => productSizeService.Create(invalidProductSize); //Assert Assert.Throws <ArgumentException>(actual); }
public void Delete_IdExisting_ReturnsDeletedProductMetricWithSpecifiedId() { //Arrange int existingId = 12; ProductMetric expected = new ProductMetric { Id = existingId, Name = "Oversized Hoodie", MetricX = "", MetricY = "Length", MetricZ = "Sleeve Length" }; Mock <IProductMetricRepository> productMetricRepository = new Mock <IProductMetricRepository>(); productMetricRepository.Setup(repo => repo.Delete(existingId)). Returns(expected); IProductMetricService productMetricService = new ProductMetricService(productMetricRepository.Object); //Act ProductMetric actual = productMetricService.Delete(existingId); //Assert Assert.Equal(expected, actual); }
public void Update_IdNonExisting_ThrowsArgumentException() { //Arrange ProductMetric invalidProductMetric = new ProductMetric { Id = 1, Name = "Oversized Hoodie", MetricX = "Width", MetricY = "Length", MetricZ = "Sleeve Length" }; ProductMetric nullProductMetric = null; Mock <IProductMetricRepository> productMetricRepository = new Mock <IProductMetricRepository>(); productMetricRepository.Setup(repo => repo.Read(invalidProductMetric.Id)). Returns(nullProductMetric); IProductMetricService productMetricService = new ProductMetricService(productMetricRepository.Object); //Act Action actual = () => productMetricService.Update(invalidProductMetric); //Assert Assert.Throws <ArgumentException>(actual); }
public void Update_ProductMetricValid_ReturnsUpdatedProductMetric() { //Arrange ProductMetric validProductMetric = new ProductMetric { Id = 1, Name = "Oversized Hoodie", MetricX = "Width", MetricY = "Length", MetricZ = "Sleeve Length" }; ProductMetric expected = validProductMetric; Mock <IProductMetricRepository> productMetricRepository = new Mock <IProductMetricRepository>(); productMetricRepository.Setup(repo => repo.Read(validProductMetric.Id)). Returns(validProductMetric); productMetricRepository.Setup(repo => repo.Update(validProductMetric)). Returns(expected); IProductMetricService productMetricService = new ProductMetricService(productMetricRepository.Object); //Act ProductMetric actual = productMetricService.Update(validProductMetric); //Assert Assert.Equal(expected, actual); }
private void ValidateMetricValues(ProductSize productSize, ProductMetric productMetric) { if (!string.IsNullOrEmpty(productMetric.MetricX)) { ValidateMetricXValue(productSize); } else if (productSize.MetricXValue != default) { throw new ArgumentException("You are not allowed to specify MetricXValue for this ProductSize"); } if (!string.IsNullOrEmpty(productMetric.MetricY)) { ValidateMetricYValue(productSize); } else if (productSize.MetricYValue != default) { throw new ArgumentException("You are not allowed to specify MetricYValue for this ProductSize"); } if (!string.IsNullOrEmpty(productMetric.MetricZ)) { ValidateMetricZValue(productSize); } else if (productSize.MetricZValue != default) { throw new ArgumentException("You are not allowed to specify MetricZValue for this ProductSize"); } }
private void ValidateName(ProductMetric productMetric) { if (string.IsNullOrEmpty(productMetric.Name)) { throw new ArgumentException("You need to specify a Name for the ProductMetric."); } }
private void ValidateCreate(ProductMetric productMetric) { ValidateNull(productMetric); if (productMetric.Id != default) { throw new ArgumentException("You are not allowed to specify an ID when creating a ProductMetric."); } ValidateName(productMetric); ValidateMetricXValue(productMetric); }
private void ValidateUpdate(ProductMetric productMetric) { ValidateNull(productMetric); ValidateName(productMetric); ValidateMetricXValue(productMetric); if (_productMetricRepository.Read(productMetric.Id) == null) { throw new ArgumentException($"Cannot find a Product Metric with the ID: {productMetric.Id}"); } }
public ActionResult <ProductMetric> Delete(int id) { ProductMetric deletedProductMetric = _productMetricService.Delete(id); if (deletedProductMetric == null) { return(NotFound($"Did not find ProductMetric with ID: {id}")); } return(Ok(deletedProductMetric)); }
public ActionResult <ProductMetric> Post([FromBody] ProductMetric value) { try { return(Ok(_productMetricService.Create(value))); } catch (Exception e) { return(BadRequest(e.Message)); } }
public void Create_ProductSizeValid_ReturnsCreatedProductSizeWithId() { //Arrange ProductSize validProductSize = new ProductSize { ProductMetric = new ProductMetric { Id = 1 }, Size = "L", MetricXValue = 70, MetricYValue = 100, MetricZValue = 75 }; ProductSize expected = new ProductSize { Id = 1, ProductMetric = new ProductMetric { Id = 1 }, Size = "L", MetricXValue = 70, MetricYValue = 100, MetricZValue = 75 }; ProductMetric fetchedProductMetric = new ProductMetric { Id = 1, Name = "Oversized Hoodie", MetricX = "Length", MetricY = "Width", MetricZ = "Sleeve Length" }; Mock <IProductSizeRepository> productSizeRepository = new Mock <IProductSizeRepository>(); productSizeRepository.Setup(repo => repo.Create(validProductSize)). Returns(expected); Mock <IProductMetricRepository> productMetricRepository = new Mock <IProductMetricRepository>(); productMetricRepository.Setup(repo => repo.Read(validProductSize.ProductMetric.Id)). Returns(fetchedProductMetric); IProductSizeService productSizeService = new ProductSizeService(productSizeRepository.Object, productMetricRepository.Object); //Act ProductSize actual = productSizeService.Create(validProductSize); //Assert Assert.Equal(expected, actual); }
public void Create_ProductMetricNull_ThrowsArgumentNullException() { //Arrange ProductMetric invalidProductMetric = null; Mock <IProductMetricRepository> productMetricRepository = new Mock <IProductMetricRepository>(); IProductMetricService productMetricService = new ProductMetricService(productMetricRepository.Object); //Act Action actual = () => productMetricService.Create(invalidProductMetric); //Assert Assert.Throws <ArgumentNullException>(actual); }
public ActionResult <ProductMetric> Put(int id, [FromBody] ProductMetric value) { try { if (id != value.Id) { return(Conflict("Parameter ID does not match ProductMetric id")); } return(Ok(_productMetricService.Update(value))); } catch (Exception e) { return(BadRequest(e.Message)); } }
public void Update_ProductSizeValid_ReturnsUpdatedProductSize() { //Arrange ProductSize validProductSize = new ProductSize { Id = 1, ProductMetric = null, Size = "L", MetricXValue = 70, MetricYValue = 100, MetricZValue = 75 }; ProductSize expected = validProductSize; ProductMetric fetchedProductMetric = new ProductMetric { Id = 1, MetricX = "Length", MetricY = "Width", MetricZ = "Sleeve Length" }; ProductSize fetchedProductSize = new ProductSize { Id = 1, ProductMetric = fetchedProductMetric, Size = "XL", MetricXValue = 70, MetricYValue = 100, MetricZValue = 150 }; Mock <IProductSizeRepository> productSizeRepository = new Mock <IProductSizeRepository>(); productSizeRepository.Setup(repo => repo.Read(validProductSize.Id)). Returns(expected); productSizeRepository.Setup((repo => repo.ReadIncludeProductMetric(validProductSize.Id))) .Returns(fetchedProductSize); productSizeRepository.Setup(repo => repo.Update(validProductSize)). Returns(expected); Mock <IProductMetricRepository> productMetricRepository = new Mock <IProductMetricRepository>(); IProductSizeService productSizeService = new ProductSizeService(productSizeRepository.Object, productMetricRepository.Object); //Act ProductSize actual = productSizeService.Update(validProductSize); //Assert Assert.Equal(expected, actual); }
public void Update_IdNonExisting_ThrowsArgumentException() { //Arrange ProductSize invalidProductSize = new ProductSize { Id = 1, ProductMetric = null, Size = "L", MetricXValue = 70, MetricYValue = 100, MetricZValue = 75 }; ProductSize nullProductSize = null; ProductMetric fetchedProductMetric = new ProductMetric { Id = 1, MetricX = "Length", MetricY = "Width", MetricZ = "Sleeve Length" }; ProductSize fetchedProductSize = new ProductSize { Id = 1, ProductMetric = fetchedProductMetric, Size = "XL", MetricXValue = 70, MetricYValue = 100, MetricZValue = 150 }; Mock <IProductSizeRepository> productSizeRepository = new Mock <IProductSizeRepository>(); productSizeRepository.Setup(repo => repo.Read(invalidProductSize.Id)). Returns(nullProductSize); productSizeRepository.Setup((repo => repo.ReadIncludeProductMetric(invalidProductSize.Id))) .Returns(fetchedProductSize); Mock <IProductMetricRepository> productMetricRepository = new Mock <IProductMetricRepository>(); IProductSizeService productSizeService = new ProductSizeService(productSizeRepository.Object, productMetricRepository.Object); //Act Action actual = () => productSizeService.Update(invalidProductSize); //Assert Assert.Throws <ArgumentException>(actual); }
public void Delete_IdNonExisting_ReturnsNull() { //Arrange int existingId = 12; ProductMetric expected = null; Mock <IProductMetricRepository> productMetricRepository = new Mock <IProductMetricRepository>(); productMetricRepository.Setup(repo => repo.Delete(existingId)). Returns(expected); IProductMetricService productMetricService = new ProductMetricService(productMetricRepository.Object); //Act ProductMetric actual = productMetricService.Delete(existingId); //Assert Assert.Equal(expected, actual); }
public void Update_ProductMetricNonExisting_ThrowsArgumentException() { //Arrange ProductModel invalidProductModel = new ProductModel { Id = 1, Name = "Rust In Peace Hoodie", ProductCategory = new ProductCategory { Id = 2 }, ProductMetric = new ProductMetric { Id = 4 }, Price = 120, Products = null }; ProductMetric nullProductMetric = null; Mock <IProductModelRepository> productModelRepository = new Mock <IProductModelRepository>(); productModelRepository.Setup(repo => repo.Read(invalidProductModel.Id)). Returns(invalidProductModel); Mock <IProductCategoryRepository> productCategoryRepository = new Mock <IProductCategoryRepository>(); productCategoryRepository.Setup(repo => repo.Read(invalidProductModel.ProductCategory.Id)). Returns(invalidProductModel.ProductCategory); Mock <IProductMetricRepository> productMetricRepository = new Mock <IProductMetricRepository>(); productMetricRepository.Setup(repo => repo.Read(invalidProductModel.ProductMetric.Id)). Returns(nullProductMetric); IProductModelService productModelService = new ProductModelService(productModelRepository.Object, productCategoryRepository.Object, productMetricRepository.Object); //Act Action actual = () => productModelService.Update(invalidProductModel); //Assert Assert.Throws <ArgumentException>(actual); }
public void Create_NameNull_ThrowsArgumentException() { //Arrange ProductMetric invalidProductMetric = new ProductMetric { Name = null, MetricX = "Width", MetricY = "Length", MetricZ = "Sleeve Length" }; Mock <IProductMetricRepository> productMetricRepository = new Mock <IProductMetricRepository>(); IProductMetricService productMetricService = new ProductMetricService(productMetricRepository.Object); //Act Action actual = () => productMetricService.Create(invalidProductMetric); //Assert Assert.Throws <ArgumentException>(actual); }
public void Update_MetricXEmpty_ThrowsArgumentException() { //Arrange ProductMetric invalidProductMetric = new ProductMetric { Id = 3, Name = "Oversized Hoodie", MetricX = "", MetricY = "Length", MetricZ = "Sleeve Length" }; Mock <IProductMetricRepository> productMetricRepository = new Mock <IProductMetricRepository>(); IProductMetricService productMetricService = new ProductMetricService(productMetricRepository.Object); //Act Action actual = () => productMetricService.Update(invalidProductMetric); //Assert Assert.Throws <ArgumentException>(actual); }
public void Create_MetricValueNegative_ThrowsArgumentException() { //Arrange ProductSize invalidProductSize = new ProductSize { ProductMetric = new ProductMetric { Id = 1 }, Size = "XL", MetricXValue = 70, MetricYValue = -13, MetricZValue = 20 }; ProductMetric fetchedProductMetric = new ProductMetric { Id = 1, Name = "Oversized Hoodie", MetricX = "Length", MetricY = "Width", MetricZ = "Sleeve Length" }; Mock <IProductSizeRepository> productSizeRepository = new Mock <IProductSizeRepository>(); Mock <IProductMetricRepository> productMetricRepository = new Mock <IProductMetricRepository>(); productMetricRepository.Setup(repo => repo.Read(invalidProductSize.ProductMetric.Id)). Returns(fetchedProductMetric); IProductSizeService productSizeService = new ProductSizeService(productSizeRepository.Object, productMetricRepository.Object); //Act Action actual = () => productSizeService.Create(invalidProductSize); //Assert Assert.Throws <ArgumentException>(actual); }
public ProductMetric Create(ProductMetric newProductMetric) { _ctx.ProductMetrics.Attach(newProductMetric).State = EntityState.Added; _ctx.SaveChanges(); return(newProductMetric); }
public ProductMetric Update(ProductMetric updatedProductMetric) { _ctx.ProductMetrics.Attach(updatedProductMetric).State = EntityState.Modified; _ctx.SaveChanges(); return(updatedProductMetric); }
public ProductMetric Create(ProductMetric newProductMetric) { ValidateCreate(newProductMetric); return(_productMetricRepository.Create(newProductMetric)); }
public ProductMetric Update(ProductMetric updatedProductMetric) { ValidateUpdate(updatedProductMetric); return(_productMetricRepository.Update(updatedProductMetric)); }