/// <summary> /// Create a domain product object from the given base product operations object. /// </summary> /// <param name="product"></param> /// <returns></returns> public ActionResult <Product> Create(ProductOperationObject product) { try { // map the product operation object to a domain object var productDomainObject = AutoMapper.Mapper.Map <ProductOperationObject, Product>(product); var newProduct = _unitOfWork.ProductRepository.Create(productDomainObject); _unitOfWork.Commit(); newProduct.Category = new Category { Id = product.Category.Id, Name = product.Category.Name }; newProduct.Manufacturer = new Manufacturer { Id = product.Manufacturer.Id, Name = product.Manufacturer.Name }; return(ActionResult <Product> .GetSuccess(newProduct)); } catch (Exception ex) { return(ActionResult <Product> .GetFailed($"[GetEditProduct] Exception/Failed to create product. Exception: {ex.Message}")); } }
public ProductAdminDto Post(ProductOperationObject product) { return(ActionVerbConfigService.WrapAction(() => { var createResult = _productLogic.Create(product); if (createResult.Success) { var createdProduct = createResult.Data; var adminDto = AutoMapper.Mapper.Map <ProductAdminDto>(createdProduct); return adminDto; } throw new HttpResponseException(ResponseMessageBuilder.BuildMessageFromActionResult(createResult)); })); }
public void CreateProduct_ShouldCreate_NewProduct() { // =========================================== // ARRANGE // =========================================== var productService = GetServiceInstance(); var name = "Test Product Name" + Guid.NewGuid(); var prodcutOperationObject = new ProductOperationObject() { Name = name, Category = new CategoryOperationObject() { Id = 1 }, Manufacturer = new ManufacturerOperationObject() { Id = 1 } }; // =========================================== // ACT // =========================================== var createProductResult = productService.Create(prodcutOperationObject); // =========================================== // ASSERT // =========================================== Assert.IsTrue(createProductResult.Success); var createdProduct = GetServiceInstance().GetEdit(createProductResult.Data.Id).Data; Assert.IsNotNull(createdProduct, "The product we queried from the database must not be null"); Assert.AreEqual(name, createdProduct.Name); Assert.IsTrue(!string.IsNullOrWhiteSpace(createdProduct.Category.Name)); Assert.IsTrue(!string.IsNullOrWhiteSpace(createdProduct.Manufacturer.Name)); }
private void RunFullAssertionSet( ProductEditObject readProduct, ProductOperationObject expectedProductInfo, Category expectedCategoryInfo, Manufacturer expectedManufacturerInfo, List <TagType> expectedTagList, bool runPropertyValueAsserts = false) { // product general asserts Assert.AreEqual(expectedProductInfo.Name, readProduct.Name, "The product name must match"); Assert.AreEqual(expectedProductInfo.PriceCurrent, readProduct.PriceCurrent, "The product price current must match"); Assert.AreEqual(expectedProductInfo.PriceRegular, readProduct.PriceRegular, "The product price regular must match"); // assert manufacturer Assert.AreEqual(expectedManufacturerInfo.Id, readProduct.ManufacturerId, "The product must have the correct manufacturer set"); Assert.IsNotNull(readProduct.Manufacturer); // assert product Assert.AreEqual(expectedCategoryInfo.Id, readProduct.CategoryId, "The Product must have the correct expected category"); Assert.IsNotNull(readProduct.Category, "The category information for the product must be present"); // assert tag types Assert.AreEqual(readProduct.Properties.Count, expectedTagList.Count, "The number of properties on the product must match the expected tag types present on the product"); foreach (var tagType in expectedTagList) { var propertyOnProduct = readProduct.Properties.FirstOrDefault(p => p.PropertyTypeId == tagType.Id); Assert.IsNotNull(propertyOnProduct, $"The tag type with id {tagType.Id} must be contained in the list of properties for the product"); Assert.IsTrue(propertyOnProduct.Name == tagType.Name, $"The name of the contained tag type {tagType.Name} must match the name of the property: {propertyOnProduct.Name}"); if (runPropertyValueAsserts) { Assert.IsTrue(!string.IsNullOrWhiteSpace(propertyOnProduct.Value), $"The value on the property with name {propertyOnProduct.Name} must be set"); } } }
public void ChangingProductCategory_WhichHasASpecificTagToAParentCategory_ShouldDisplayAllTagValuesFromChild() { // ===================== ARRANGE ===================== //==================================================== // we will have two categories // which will have a collection of tags var parentCategory = CreateCategory(GetUniqueString("Parent"), null); var childCategory = CreateCategory(GetUniqueString("Child"), parentCategory); // generate the manufacturer that will be used for the product testing var manufacturer = CreateManufacturer(GetUniqueString("ManufacturerName")); // generate tags for the categories var parentTagTypeList = GenerateTagTypesForCategory(parentCategory, 3); var childTagTypeList = GenerateTagTypesForCategory(childCategory, 1); var productOpObject = new ProductOperationObject() { Name = GetUniqueString("NewAdvancedUpdateProduct"), CategoryId = childCategory.Id, ManufacturerId = manufacturer.Id, PriceCurrent = 100.ToString(), PriceRegular = 150.ToString(), Category = new CategoryOperationObject() { Id = childCategory.Id }, Manufacturer = new ManufacturerOperationObject() { Id = manufacturer.Id } }; // ===================== ACT ========================= //==================================================== // PART 1 : // Create the product var createResult = GetServiceInstance().Create(productOpObject); // mid term assertion that the product is created and on read we have the specific number // of tag types based on the child category Assert.IsTrue(createResult.Success, "The product must successfully be created."); Assert.IsTrue(createResult.Data.Id != 0, "The product must be asigned a valid Id upon creation"); var newCreatedEditProduct = GetServiceInstance().GetEdit(createResult.Data.Id).Data; // Run assertion set for tag types var fullSetOfTagTypes = new List <TagType>(); fullSetOfTagTypes.AddRange(childTagTypeList); fullSetOfTagTypes.AddRange(parentTagTypeList); RunFullAssertionSet(newCreatedEditProduct, productOpObject, childCategory, manufacturer, fullSetOfTagTypes); // set values on all the properties for the product - both for the parent and child category // at this point the product is on the child category SetValuesOnAllPropertiesOnProduct(newCreatedEditProduct); var updateTagValuesResult = GetServiceInstance().Update(newCreatedEditProduct); var updatedPropertyValuesForAllPropsProduct = updateTagValuesResult.Data; // run full asserts for the updated properties but also check values RunFullAssertionSet(updatedPropertyValuesForAllPropsProduct, productOpObject, childCategory, manufacturer, fullSetOfTagTypes, true); // Update/ACT by Changing the Category of the product to the parent category // and setting tag type values updatedPropertyValuesForAllPropsProduct.CategoryId = parentCategory.Id; updatedPropertyValuesForAllPropsProduct.Category.Id = parentCategory.Id; var updatedProductToParentCategory = GetServiceInstance().Update(updatedPropertyValuesForAllPropsProduct).Data; // assert the information again but expecting only the parents tag types RunFullAssertionSet(updatedProductToParentCategory, productOpObject, parentCategory, manufacturer, parentTagTypeList, true); // RUN THE MAIN ACTION - CHANGING BACK TO THE CHILD CATEGORY updatedProductToParentCategory.Category.Id = childCategory.Id; updatedProductToParentCategory.CategoryId = childCategory.Id; var finalUpdateResult = GetServiceInstance().Update(updatedProductToParentCategory); var productUpdatedBackToChildCategory = finalUpdateResult.Data; // ===================== ASSERT ===================== // ================================================== RunFullAssertionSet(productUpdatedBackToChildCategory, productOpObject, childCategory, manufacturer, fullSetOfTagTypes, true); }