public bool AddProduct(Product product) { logger.logInfo("Try to add a new product in db."); if (product == null) { ValidationException e = new ValidationException("Product is null"); logger.logError(e); throw e; } if (product.Categories == null) { ValidationException e = new ValidationException("Product's categories are null"); logger.logError(e); throw e; } CategoryService categoryService = new CategoryService(); foreach (Category categ in product.Categories) { if (categoryService.GetCategoryById(categ.IdCategory) == null) { EntityDoesNotExistException e = new EntityDoesNotExistException("One or more categories of the product do not exist"); logger.logError(e); throw e; } } var validationResults = Validation.Validate<Product>(product); if (product.Name != null) { if (!validationResults.IsValid) { ValidationException e = new ValidationException("Invalid product's name/description."); logger.logError(e); throw e; } else { try { DataMapperFactoryMethod.GetCurrentFactory().ProductFactory.AddProduct(product); } catch (DuplicateException duplicateException) { logger.logError(duplicateException); throw duplicateException; } } } else { ValidationException e = new ValidationException("Invalid product's name - null."); logger.logError(e); throw e; } logger.logInfo("Product added successfully!"); return true; }
public void AddProductExistent() { Product product = new Product(); product.Name = "thr"; product.Description = ""; CategoryService categoryService = new CategoryService(); Category category = categoryService.GetCategoryById(2); product.Categories.Add(category); ProductService productService = new ProductService(); Boolean result = false; try { result = productService.AddProduct(product); } catch (DuplicateException exc) { Assert.AreEqual("The same product already exists - same name, description and category", exc.Message); } Assert.AreEqual(result, false); }
public void UpdateProductDuplicateName() { Product product = new Product(); product.Name = "Product1"; product.Description = "Description"; CategoryService categoryService = new CategoryService(); Category category = categoryService.GetCategoryById(8); product.Categories.Add(category); ProductService productService = new ProductService(); productService.AddProduct(product); String name = "Product"; int id = 19; Product p = productService.GetProductById(id); Boolean result = false; try { result = productService.UpdateProduct(id, name); } catch (DuplicateException exc) { Assert.AreEqual("The same product already exists - same name, description and category", exc.Message); } Assert.AreEqual(false, result); }
public void GetProductsOfACategory() { CategoryService categoryService = new CategoryService(); ProductService productService = new ProductService(); Category category = categoryService.GetCategoryById(8); ICollection<Product> products = productService.GetAllProductsOfACategory(category); Assert.AreEqual(products.Count(), 2); }
public void AddProductZeroCharactesDescription() { Product product = new Product(); product.Name = "Product"; product.Description = ""; CategoryService categoryService = new CategoryService(); Category category = categoryService.GetCategoryById(2); product.Categories.Add(category); ProductService productService = new ProductService(); Boolean result = false; try { result = productService.AddProduct(product); } catch (ValidationException exc) { Assert.AreEqual("", exc.Message); } Assert.AreEqual(result, true); }