public static ProductDraft DefaultProductDraftWithKey(ProductDraft draft, string key)
        {
            var productDraft = DefaultProductDraft(draft);

            productDraft.Key = key;
            return(productDraft);
        }
        public static ProductDraft DefaultProductDraftWithProductType(ProductDraft draft, ProductType productType)
        {
            var productDraft = DefaultProductDraft(draft);

            productDraft.ProductType = productType.ToIdResourceIdentifier();
            return(productDraft);
        }
        public static ProductDraft DefaultProductDraft(ProductDraft productDraft)
        {
            var randomInt = TestingUtility.RandomInt();

            productDraft.Name = new LocalizedString {
                { "en", $"Name_{randomInt}" }
            };
            productDraft.Description = new LocalizedString {
                { "en", $"Description_{randomInt}" }
            };
            productDraft.Slug = new LocalizedString {
                { "en", $"Slug_{randomInt}" }
            };
            productDraft.Key     = $"Key_{randomInt}";
            productDraft.Publish = true;

            var priceCentAmount = TestingUtility.RandomInt(1000, 5000);
            var productPrice    = TestingUtility.GetPriceDraft(priceCentAmount);

            var attributes = TestingUtility.GetListOfRandomAttributes();

            productDraft.MasterVariant = new ProductVariantDraft
            {
                Key    = $"MasterVariant_key_{randomInt}",
                Sku    = $"MasterVariant_Sku_{randomInt}",
                Prices = new List <PriceDraft> {
                    productPrice
                },
                Attributes = attributes
            };
            return(productDraft);
        }
        public static ProductDraft DefaultProductDraftWithImages(ProductDraft draft, List <Image> images)
        {
            var productDraft = DefaultProductDraft(draft);

            productDraft.MasterVariant.Images = images;
            return(productDraft);
        }
        public async Task ShouldCreateAndDeleteProductAsync()
        {
            ProductDraft productDraft = Helper.GetTestProductDraft(_project, _testProductType.Id, _testTaxCategory.Id);

            LocalizedString name = new LocalizedString();
            LocalizedString slug = new LocalizedString();

            foreach (string language in _project.Languages)
            {
                name.SetValue(language, string.Concat("Test Product", language, " ", Helper.GetRandomString(10)));
                slug.SetValue(language, string.Concat("test-product-", language, "-", Helper.GetRandomString(10)));
            }

            productDraft.Name = name;
            productDraft.Slug = slug;

            Response <Product> response = await _client.Products().CreateProductAsync(productDraft);

            Assert.True(response.Success);

            Product product = response.Result;

            Assert.NotNull(product.Id);

            string deletedProductId = product.Id;

            response = await _client.Products().DeleteProductAsync(product.Id, product.Version);

            Assert.True(response.Success);

            response = await _client.Products().GetProductByIdAsync(deletedProductId);

            Assert.False(response.Success);
        }
示例#6
0
        public static ProductDraft DefaultProductDraftWithSearchKeywords(ProductDraft draft)
        {
            var productDraft = DefaultProductDraft(draft);

            productDraft.SearchKeywords = GetSearchKeywords();
            return(productDraft);
        }
        public static ProductDraft DefaultProductDraftWithTaxCategory(ProductDraft draft, TaxCategory taxCategory)
        {
            var productDraft = DefaultProductDraft(draft);

            productDraft.TaxCategory = taxCategory.ToKeyResourceIdentifier();
            return(productDraft);
        }
示例#8
0
        public Product CreateProduct(ProductDraft productDraft)
        {
            IClient commerceToolsClient = this.GetService <IClient>();
            Product product             = commerceToolsClient.ExecuteAsync(new CreateCommand <Product>(productDraft)).Result;

            return(product);
        }
        public static ProductDraft DefaultProductDraftWithPublish(ProductDraft draft, bool published)
        {
            var productDraft = DefaultProductDraft(draft);

            productDraft.Publish = published;
            return(productDraft);
        }
        public static ProductDraft DefaultProductDraftWithAssets(ProductDraft draft, List <AssetDraft> assets)
        {
            var productDraft = DefaultProductDraft(draft);

            productDraft.MasterVariant.Assets = assets;
            return(productDraft);
        }
示例#11
0
        /// <summary>
        /// Gets a test product draft.
        /// </summary>
        /// <param name="project">Project</param>
        /// <param name="productTypeId">Product type ID</param>
        /// <param name="taxCategoryId">Tax category ID</param>
        /// <returns></returns>
        public static ProductDraft GetTestProductDraft(Project.Project project, string productTypeId, string taxCategoryId)
        {
            List <PriceDraft> priceDrafts = new List <PriceDraft>();

            foreach (string currency in project.Currencies)
            {
                Money value = new Money();
                value.CurrencyCode = currency;
                value.CentAmount   = Helper.GetRandomNumber(10, 999999);

                priceDrafts.Add(new PriceDraft(value));
            }

            string randomSku = Helper.GetRandomString(10);
            ProductVariantDraft productVariantDraft = new ProductVariantDraft();

            productVariantDraft.Sku    = randomSku;
            productVariantDraft.Prices = priceDrafts;

            ResourceIdentifier productType = new ResourceIdentifier();

            productType.Id     = productTypeId;
            productType.TypeId = Common.ReferenceType.ProductType;

            LocalizedString name            = new LocalizedString();
            LocalizedString slug            = new LocalizedString();
            LocalizedString description     = new LocalizedString();
            LocalizedString metaTitle       = new LocalizedString();
            LocalizedString metaDescription = new LocalizedString();
            LocalizedString metaKeywords    = new LocalizedString();

            foreach (string language in project.Languages)
            {
                name.SetValue(language, string.Concat("Test Product ", language, " ", randomSku));
                slug.SetValue(language, string.Concat("test-product-", language, "-", randomSku));
                description.SetValue(language, string.Concat("Created by commercetools.NET ", language));
                metaTitle.SetValue(language, string.Concat("Product Meta Title ", language));
                metaDescription.SetValue(language, string.Concat("Product Meta Description ", language));
                metaKeywords.SetValue(language, string.Concat("Product Meta Keywords ", language));
            }

            Reference taxCategory = new Reference();

            taxCategory.Id            = taxCategoryId;
            taxCategory.ReferenceType = Common.ReferenceType.TaxCategory;

            ProductDraft productDraft = new ProductDraft(name, productType, slug);

            productDraft.Key             = Helper.GetRandomString(15);
            productDraft.Description     = description;
            productDraft.MetaTitle       = metaTitle;
            productDraft.MetaDescription = metaDescription;
            productDraft.MetaKeywords    = metaKeywords;
            productDraft.TaxCategory     = taxCategory;
            productDraft.MasterVariant   = productVariantDraft;

            return(productDraft);
        }
        public void Init()
        {
            _client = new Client(Helper.GetConfiguration());

            Task <Response <Project.Project> > projectTask = _client.Project().GetProjectAsync();

            projectTask.Wait();
            Assert.IsTrue(projectTask.Result.Success);
            _project = projectTask.Result.Result;

            Assert.IsTrue(_project.Languages.Count > 0);
            Assert.IsTrue(_project.Currencies.Count > 0);

            ProductTypeDraft productTypeDraft = Helper.GetTestProductTypeDraft();
            Task <Response <ProductType> > productTypeTask = _client.ProductTypes().CreateProductTypeAsync(productTypeDraft);

            productTypeTask.Wait();
            Assert.IsTrue(productTypeTask.Result.Success);

            _testProductType = productTypeTask.Result.Result;
            Assert.NotNull(_testProductType.Id);

            TaxCategoryDraft taxCategoryDraft = Helper.GetTestTaxCategoryDraft(_project);
            Task <Response <TaxCategory> > taxCategoryTask = _client.TaxCategories().CreateTaxCategoryAsync(taxCategoryDraft);

            taxCategoryTask.Wait();
            Assert.IsTrue(taxCategoryTask.Result.Success);

            _testTaxCategory = taxCategoryTask.Result.Result;
            Assert.NotNull(_testTaxCategory.Id);

            _testProducts = new List <Product>();

            for (int i = 0; i < 5; i++)
            {
                ProductDraft productDraft = Helper.GetTestProductDraft(_project, _testProductType.Id, _testTaxCategory.Id);
                productDraft.Publish = true;

                LocalizedString name = new LocalizedString();

                foreach (string language in _project.Languages)
                {
                    name.SetValue(language, string.Concat("Test Product ", i, " ", language, " ", Helper.GetRandomString(10)));
                }

                productDraft.Name = name;

                Task <Response <Product> > productTask = _client.Products().CreateProductAsync(productDraft);
                productTask.Wait();
                Assert.IsTrue(productTask.Result.Success);

                Product product = productTask.Result.Result;
                Assert.NotNull(product.Id);

                _testProducts.Add(product);
            }
        }
示例#13
0
        public void CreateProduct()
        {
            IClient      commerceToolsClient = this.productFixture.GetService <IClient>();
            ProductDraft productDraft        = this.productFixture.GetProductDraft();
            Product      product             = commerceToolsClient.ExecuteAsync(new CreateCommand <Product>(productDraft)).Result;

            this.productFixture.ProductsToDelete.Add(product);
            Assert.Equal(productDraft.Name["en"], product.MasterData.Current.Name["en"]);
        }
        public static ProductDraft DefaultProductDraftWithCategory(ProductDraft draft, Category category)
        {
            var productDraft = DefaultProductDraft(draft);

            productDraft.Categories = new List <IReference <Category> >()
            {
                category.ToKeyResourceIdentifier()
            };
            return(productDraft);
        }
示例#15
0
        public async Task ShouldCreateAndDeleteProductAsync()
        {
            ProductDraft productDraft = Helper.GetTestProductDraft(_project, _testProductType.Id, _testTaxCategory.Id);
            Image        image        = new Image("http://via.placeholder.com/350x150", new ImageDimensions(350, 150))
            {
                Label = "TestLabel"
            };

            productDraft.MasterVariant.Images = new List <Image>()
            {
                image
            };

            LocalizedString name = new LocalizedString();
            LocalizedString slug = new LocalizedString();

            foreach (string language in _project.Languages)
            {
                name.SetValue(language, string.Concat("Test Product", language, " ", Helper.GetRandomString(10)));
                slug.SetValue(language, string.Concat("test-product-", language, "-", Helper.GetRandomString(10)));
            }

            productDraft.Name = name;
            productDraft.Slug = slug;

            Response <Product> response = await _client.Products().CreateProductAsync(productDraft);

            Assert.IsTrue(response.Success);

            Product product = response.Result;

            Assert.NotNull(product.Id);

            Assert.NotNull(product.MasterData);
            Assert.NotNull(product.MasterData.Current);
            Assert.NotNull(product.MasterData.Current.MasterVariant);
            Assert.NotNull(product.MasterData.Current.MasterVariant.Images);
            Assert.IsTrue(product.MasterData.Current.MasterVariant.Images.Count == 1);
            Image imageReturned = product.MasterData.Current.MasterVariant.Images[0];

            Assert.AreEqual(image.Label, imageReturned.Label);
            Assert.AreEqual(image.Url, imageReturned.Url);
            Assert.AreEqual(image.Dimensions.Width, imageReturned.Dimensions.Width);
            Assert.AreEqual(image.Dimensions.Height, imageReturned.Dimensions.Height);

            string deletedProductId = product.Id;

            response = await _client.Products().DeleteProductAsync(product.Id, product.Version);

            Assert.IsTrue(response.Success);

            response = await _client.Products().GetProductByIdAsync(deletedProductId);

            Assert.IsFalse(response.Success);
        }
 public static async Task WithUpdateableProduct(IClient client, Func <ProductDraft, ProductDraft> draftAction, Func <Product, Task <Product> > func)
 {
     await WithProductType(client, async productType =>
     {
         var productDraftWithProductType = new ProductDraft
         {
             ProductType = productType.ToKeyResourceIdentifier()
         };
         await WithUpdateableAsync(client, productDraftWithProductType, draftAction, func, null, DeleteProduct);
     });
 }
 public static async Task WithProduct(IClient client, Func <Product, Task> func)
 {
     await WithProductType(client, async productType =>
     {
         var productDraftWithProductType = new ProductDraft
         {
             ProductType = productType.ToKeyResourceIdentifier()
         };
         await WithAsync(client, productDraftWithProductType,
                         DefaultProductDraft, func, null, DeleteProduct);
     });
 }
        public void Init()
        {
            _client = Helper.GetClient();

            Task <Response <Project.Project> > projectTask = _client.Project().GetProjectAsync();

            projectTask.Wait();
            Assert.IsTrue(projectTask.Result.Success);
            _project = projectTask.Result.Result;

            ProductTypeDraft productTypeDraft = Helper.GetTestProductTypeDraft();
            Task <Response <ProductType> > productTypeTask = _client.ProductTypes().CreateProductTypeAsync(productTypeDraft);

            productTypeTask.Wait();
            Assert.IsTrue(productTypeTask.Result.Success);

            ProductType testProductType = productTypeTask.Result.Result;

            Assert.NotNull(testProductType.Id);

            TaxCategoryDraft testTaxCategoryDraft          = Helper.GetTestTaxCategoryDraft(_project);
            Task <Response <TaxCategory> > taxCategoryTask = _client.TaxCategories().CreateTaxCategoryAsync(testTaxCategoryDraft);

            taxCategoryTask.Wait();
            Assert.IsTrue(taxCategoryTask.Result.Success);

            ProductDraft productDraft = Helper.GetTestProductDraft(_project, testProductType.Id, taxCategoryTask.Result.Result.Id);
            Task <Response <Product> > productTask = _client.Products().CreateProductAsync(productDraft);

            productTask.Wait();
            Assert.IsTrue(productTask.Result.Success);

            Product testProduct = productTask.Result.Result;

            Assert.NotNull(testProduct.Id);
            _testProductId = testProduct.Id;

            var deleteProduct = _client.Products().DeleteProductAsync(testProduct);

            deleteProduct.Wait();
            Assert.IsTrue(deleteProduct.Result.Success);

            var deleteProductType = _client.ProductTypes().DeleteProductTypeAsync(testProductType);

            deleteProductType.Wait();
            Assert.IsTrue(deleteProductType.Result.Success);

            var deleteTaxCategory = _client.TaxCategories().DeleteTaxCategoryAsync(taxCategoryTask.Result.Result);

            deleteTaxCategory.Wait();
            Assert.IsTrue(deleteTaxCategory.Result.Success);
        }
        public static ProductDraft DefaultProductDraft(ProductDraft productDraft)
        {
            var randomInt = TestingUtility.RandomInt();

            productDraft.Name = new LocalizedString {
                { "en", $"Name_{randomInt}" }
            };
            productDraft.Slug = new LocalizedString {
                { "en", $"Slug_{randomInt}" }
            };
            productDraft.Key     = $"Key_{randomInt}";
            productDraft.Publish = true;
            return(productDraft);
        }
示例#20
0
        public void Init()
        {
            _client = Helper.GetClient();

            Task <Response <Project.Project> > projectTask = _client.Project().GetProjectAsync();

            projectTask.Wait();
            Assert.IsTrue(projectTask.Result.Success);
            _project = projectTask.Result.Result;

            ProductTypeDraft productTypeDraft = Helper.GetTestProductTypeDraft();
            Task <Response <ProductType> > productTypeTask = _client.ProductTypes().CreateProductTypeAsync(productTypeDraft);

            productTypeTask.Wait();
            Assert.IsTrue(productTypeTask.Result.Success);

            _testProductType = productTypeTask.Result.Result;
            Assert.NotNull(_testProductType.Id);

            TaxCategoryDraft taxCategoryDraft = Helper.GetTestTaxCategoryDraft(_project);
            Task <Response <TaxCategory> > taxCategoryTask = _client.TaxCategories().CreateTaxCategoryAsync(taxCategoryDraft);

            taxCategoryTask.Wait();
            Assert.IsTrue(taxCategoryTask.Result.Success);

            _testTaxCategory = taxCategoryTask.Result.Result;
            Assert.NotNull(_testTaxCategory.Id);

            _testProducts = new List <Product>();

            for (int i = 0; i < 5; i++) // Default Money
            {
                ProductDraft productDraft = Helper.GetTestProductDraft(_project, _testProductType.Id, _testTaxCategory.Id);

                Task <Response <Product> > productTask = _client.Products().CreateProductAsync(productDraft);
                productTask.Wait();
                Assert.IsTrue(productTask.Result.Success);

                Product product = productTask.Result.Result;
                Assert.NotNull(product.Id);

                _testProducts.Add(product);
            }
            //Add Product with Money Price => CentPrecision Money Type
            _productIdWithCentPrecisionPrice = AddProductWithSpecificMoneyType(MoneyTestTypes.CentPrecision);
            //Add Product with Money Price => HighPrecision Money Type
            _productIdWithHighPrecisionPrice = AddProductWithSpecificMoneyType(MoneyTestTypes.HighPrecision);
        }
示例#21
0
        private string AddProductWithSpecificMoneyType(MoneyTestTypes moneyTestTypes)
        {
            ProductDraft productDraft = Helper.GetTestProductDraft(_project, _testProductType.Id, _testTaxCategory.Id, moneyTestTypes);

            Task <Response <Product> > productTask = _client.Products().CreateProductAsync(productDraft);

            productTask.Wait();
            Assert.IsTrue(productTask.Result.Success);

            Product product = productTask.Result.Result;

            Assert.NotNull(product.Id);

            _testProducts.Add(product);
            return(product.Id);
        }
示例#22
0
        public ProductDraft GetProductDraft(Category category, ProductType productType, bool withVariants = false,
                                            bool publish = false)
        {
            ProductDraft productDraft = new ProductDraft();

            productDraft.Name = new LocalizedString()
            {
                { "en", this.RandomString(10) }
            };
            productDraft.Key  = this.RandomString(10);
            productDraft.Slug = new LocalizedString()
            {
                { "en", this.RandomString(10) }
            };
            productDraft.ProductType = new ResourceIdentifier <ProductType> {
                Key = productType.Key
            };
            ProductVariantDraft productMasterVariant =
                this.GetRandomProductVariantDraft(category.Id, ReferenceTypeId.Category);

            productDraft.MasterVariant = productMasterVariant;

            productDraft.Categories = new List <IReferenceable <Category> >
            {
                new ResourceIdentifier <Category> {
                    Key = category.Key
                }
            };
            productDraft.Publish = publish; //if true, the product is published immediately

            if (withVariants)               //then create variants for this product
            {
                productDraft.Variants = new List <ProductVariantDraft>();
                var productVariantDraft = this.GetRandomProductVariantDraft(category.Id, ReferenceTypeId.Category);
                productDraft.Variants.Add(productVariantDraft);
            }

            //Add taxCategory to product
            var taxCategory = CreateNewTaxCategory();

            productDraft.TaxCategory = new Reference <TaxCategory>()
            {
                Id = taxCategory.Id
            };

            return(productDraft);
        }
示例#23
0
        public void Init()
        {
            _client = new Client(Helper.GetConfiguration());

            Task <Response <Project.Project> > projectTask = _client.Project().GetProjectAsync();

            projectTask.Wait();
            Assert.IsTrue(projectTask.Result.Success);
            _project = projectTask.Result.Result;

            ProductTypeDraft productTypeDraft = Helper.GetTestProductTypeDraft();
            Task <Response <ProductType> > productTypeTask = _client.ProductTypes().CreateProductTypeAsync(productTypeDraft);

            productTypeTask.Wait();
            Assert.IsTrue(productTypeTask.Result.Success);

            _testProductType = productTypeTask.Result.Result;
            Assert.NotNull(_testProductType.Id);

            TaxCategoryDraft taxCategoryDraft = Helper.GetTestTaxCategoryDraft(_project);
            Task <Response <TaxCategory> > taxCategoryTask = _client.TaxCategories().CreateTaxCategoryAsync(taxCategoryDraft);

            taxCategoryTask.Wait();
            Assert.IsTrue(taxCategoryTask.Result.Success);

            _testTaxCategory = taxCategoryTask.Result.Result;
            Assert.NotNull(_testTaxCategory.Id);

            _testProducts = new List <Product>();

            for (int i = 0; i < 5; i++)
            {
                ProductDraft productDraft = Helper.GetTestProductDraft(_project, _testProductType.Id, _testTaxCategory.Id);

                Task <Response <Product> > productTask = _client.Products().CreateProductAsync(productDraft);
                productTask.Wait();
                Assert.IsTrue(productTask.Result.Success);

                Product product = productTask.Result.Result;
                Assert.NotNull(product.Id);

                _testProducts.Add(product);
            }
        }
        public void ProductDraftSerializationUsingReference()
        {
            ISerializerService serializerService = this.serializationFixture.SerializerService;

            var productDraft = new ProductDraft
            {
                Name = new LocalizedString()
                {
                    { "en", "name" },
                    { "en-US", "name" }
                },
                Slug = new LocalizedString()
                {
                    { "en", "slug" }
                },
                ProductType = new Reference <ProductType>
                {
                    Id = "ProductTypeId"
                },
                TaxCategory = new Reference <TaxCategory>
                {
                    Id = "TaxCategoryId"
                },
                Categories = new List <IReference <Category> >()
                {
                    new Reference <Category>
                    {
                        Id = "CategoryId"
                    }
                },
                State = new Reference <State>
                {
                    Id = "StateId"
                }
            };

            string result              = serializerService.Serialize(productDraft);
            JToken resultFormatted     = JValue.Parse(result);
            string serialized          = File.ReadAllText("Resources/Types/ProductDraftWithReference.json");
            JToken serializedFormatted = JValue.Parse(serialized);

            serializedFormatted.Should().BeEquivalentTo(resultFormatted);
        }
示例#25
0
        async Task CreateProductDraft(IMessageHandlerContext context)
        {
            var session = context.SynchronizedStorageSession.RavenSession();

            var draft = new ProductDraft()
            {
                StockItemId = Data.StockItemId,
            };

            await session.StoreAsync(draft).ConfigureAwait(false);

            Data.ProductDraftId = draft.Id;

            await context.Publish <IProductDraftCreatedEvent>(e =>
            {
                e.ProductDraftId = draft.Id;
                e.StockItemId    = draft.StockItemId;
            })
            .ConfigureAwait(false);
        }
        public static ProductDraft DefaultProductDraftWithMultipleVariants(ProductDraft draft, int variantsCount = 1)
        {
            var randomInt    = TestingUtility.RandomInt();
            var productDraft = DefaultProductDraft(draft);

            var variants = new List <ProductVariantDraft>();

            for (int i = 1; i <= variantsCount; i++)
            {
                var productVariant = new ProductVariantDraft
                {
                    Key = $"variant{i}_Key_{randomInt}",
                    Sku = $"variant{i}_Sku_{randomInt}"
                };
                variants.Add(productVariant);
            }

            productDraft.Variants = variants;
            return(productDraft);
        }
        public async void CheckErrorResponseException()
        {
            string productTypeId = Guid.NewGuid().ToString(); //references another resource that does not exist
            var    productDraft  = new ProductDraft
            {
                Name = new LocalizedString()
                {
                    { "en", TestingUtility.RandomString(10) }
                },
                Slug = new LocalizedString()
                {
                    { "en", TestingUtility.RandomString(10) }
                },
                ProductType = new ResourceIdentifier <ProductType> {
                    Id = productTypeId
                }
            };
            var exception = await Assert.ThrowsAsync <ErrorResponseException>(() =>
                                                                              client.ExecuteAsync(new CreateCommand <Product>(productDraft)));

            Assert.Equal(400, exception.StatusCode);
        }
示例#28
0
        public async void CheckErrorResponseException()
        {
            IClient      commerceToolsClient = this.errorsFixture.GetService <IClient>();
            string       productTypeId       = Guid.NewGuid().ToString(); //references another resource that does not exist
            ProductDraft productDraft        = new ProductDraft();

            productDraft.Name = new LocalizedString()
            {
                { "en", this.errorsFixture.RandomString(10) }
            };
            productDraft.Slug = new LocalizedString()
            {
                { "en", this.errorsFixture.RandomString(10) }
            };
            productDraft.ProductType = new ResourceIdentifier <ProductType> {
                Id = productTypeId
            };
            ErrorResponseException exception = await Assert.ThrowsAsync <ErrorResponseException>(() =>
                                                                                                 commerceToolsClient.ExecuteAsync(new CreateCommand <Product>(productDraft)));

            Assert.Equal(400, exception.StatusCode);
        }
示例#29
0
        public ProductDraft GetProductDraft(ProductType productType, Category category = null, bool withVariants = false,
                                            bool publish = false, bool withImages = false, bool withAssets = false, IReference <TaxCategory> taxCategoryReference = null, LocalizedString productName = null)
        {
            ProductDraft productDraft = new ProductDraft();

            productDraft.Key  = TestingUtility.RandomString(10);
            productDraft.Name = productName ?? new LocalizedString()
            {
                { "en", TestingUtility.RandomString(10) }
            };
            productDraft.Description = new LocalizedString()
            {
                { "en", TestingUtility.RandomString(20) }
            };
            productDraft.Slug = new LocalizedString()
            {
                { "en", TestingUtility.RandomString(10) }
            };
            productDraft.ProductType = new ResourceIdentifier <ProductType> {
                Key = productType.Key
            };

            var productMasterVariant =
                TestingUtility.GetRandomProductVariantDraft(productType.Id, ReferenceTypeId.ProductType);

            productDraft.MasterVariant = productMasterVariant;

            if (category != null)
            {
                productDraft.Categories = new List <IReference <Category> >
                {
                    new ResourceIdentifier <Category> {
                        Key = category.Key
                    }
                };
            }

            productDraft.Publish = publish; //if true, the product is published immediately

            if (withVariants)               //then create variants for this product
            {
                productDraft.Variants = new List <ProductVariantDraft>();
                for (var i = 0; i < 3; i++) // add 3 variants
                {
                    var productVariantDraft = TestingUtility.GetRandomProductVariantDraft(productType.Id, ReferenceTypeId.ProductType);
                    productDraft.Variants.Add(productVariantDraft);
                }
            }

            if (withImages)
            {
                productDraft.MasterVariant.Images = TestingUtility.GetListOfImages(3);
            }

            if (withAssets)
            {
                productDraft.MasterVariant.Assets = TestingUtility.GetListOfAssetsDrafts(3);
            }

            //Add taxCategory to product (if taxCategoryReference is null, then create new TaxCategory)
            var taxCategory = taxCategoryReference ?? CreateNewTaxCategory().ToReference();

            productDraft.TaxCategory = taxCategory;

            return(productDraft);
        }
        public void Init()
        {
            _client = new Client(Helper.GetConfiguration());

            Task <Response <Project.Project> > projectTask = _client.Project().GetProjectAsync();

            projectTask.Wait();
            Assert.IsTrue(projectTask.Result.Success);
            _project = projectTask.Result.Result;

            Assert.IsTrue(_project.Languages.Count > 0, "No Languages");
            Assert.IsTrue(_project.Currencies.Count > 0, "No Currencies");

            _testCustomers = new List <Customer>();
            _testCarts     = new List <Cart>();
            _testCartsExternalAmountTaxMode = new List <Cart>();
            CustomerDraft customerDraft;
            Task <Response <CustomerCreatedMessage> > customerTask;
            CustomerCreatedMessage customerCreatedMessage;
            CartDraft cartDraft;
            Cart      cart;
            Task <Response <Cart> > cartTask;

            for (int i = 0; i < 5; i++)
            {
                customerDraft = Helper.GetTestCustomerDraft();
                customerTask  = _client.Customers().CreateCustomerAsync(customerDraft);
                customerTask.Wait();
                Assert.IsTrue(customerTask.Result.Success);

                customerCreatedMessage = customerTask.Result.Result;
                Assert.NotNull(customerCreatedMessage.Customer);
                Assert.NotNull(customerCreatedMessage.Customer.Id);

                _testCustomers.Add(customerCreatedMessage.Customer);

                cartDraft = Helper.GetTestCartDraft(_project, customerCreatedMessage.Customer.Id);
                cartTask  = _client.Carts().CreateCartAsync(cartDraft);
                cartTask.Wait();
                Assert.NotNull(cartTask.Result);
                Assert.IsTrue(cartTask.Result.Success, "CreateCartAsync failed");
                cart = cartTask.Result.Result;
                Assert.NotNull(cart.Id);
                Console.Error.WriteLine(string.Format("CartManagerTest - Information Only - Init TestCartDraft TaxMode: {0}", cartDraft.TaxMode == null ? "(default)" : cart.TaxMode.ToString()));

                _testCarts.Add(cart);


                cartDraft         = Helper.GetTestCartDraft(_project, customerCreatedMessage.Customer.Id);
                cartDraft.TaxMode = TaxMode.ExternalAmount;
                cartTask          = _client.Carts().CreateCartAsync(cartDraft);
                cartTask.Wait();
                Assert.NotNull(cartTask.Result);
                Assert.IsTrue(cartTask.Result.Success, "CreateCartAsync failed");
                cart = cartTask.Result.Result;
                Assert.IsTrue(cart.TaxMode == TaxMode.ExternalAmount, string.Format("Cart created using ExternalAmount TaxMode failed - TaxMode: {0}", cart.TaxMode));
                Assert.NotNull(cart.Id);
                Console.Error.WriteLine(string.Format("CartManagerTest - Information Only - Init TestCartDraft TaxMode: {0}", cartDraft.TaxMode == null ? "(default)" : cart.TaxMode.ToString()));

                _testCartsExternalAmountTaxMode.Add(cart);
            }

            //customer/cart with external tax mode enabled

            customerDraft = Helper.GetTestCustomerDraft();
            customerTask  = _client.Customers().CreateCustomerAsync(customerDraft);
            customerTask.Wait();
            Assert.IsTrue(customerTask.Result.Success);
            customerCreatedMessage = customerTask.Result.Result;
            Assert.NotNull(customerCreatedMessage.Customer);
            Assert.NotNull(customerCreatedMessage.Customer.Id);

            _testCustomers.Add(customerCreatedMessage.Customer);

            cartDraft = Helper.GetTestCartDraftUsingExternalTaxMode(_project, customerCreatedMessage.Customer.Id);
            cartTask  = _client.Carts().CreateCartAsync(cartDraft);
            cartTask.Wait();
            Assert.NotNull(cartTask.Result);
            Assert.IsTrue(cartTask.Result.Success, "CreateCartAsync failed");
            cart = cartTask.Result.Result;
            Assert.NotNull(cart.Id);
            Console.Error.WriteLine(string.Format("CartManagerTest - Information Only - Init TestCartDraft TaxMode: {0}", cart.TaxMode));

            _testCarts.Add(cart);

            ProductTypeDraft productTypeDraft = Helper.GetTestProductTypeDraft();
            Task <Response <ProductType> > testProductTypeTask =
                _client.ProductTypes().CreateProductTypeAsync(productTypeDraft);

            testProductTypeTask.Wait();
            Assert.IsTrue(testProductTypeTask.Result.Success, "CreateProductType failed");
            _testProductType = testProductTypeTask.Result.Result;
            Assert.NotNull(_testProductType.Id);

            TaxCategoryDraft taxCategoryDraft = Helper.GetTestTaxCategoryDraft(_project);
            Task <Response <TaxCategory> > taxCategoryTask =
                _client.TaxCategories().CreateTaxCategoryAsync(taxCategoryDraft);

            taxCategoryTask.Wait();
            Assert.IsTrue(taxCategoryTask.Result.Success, "CreateTaxCategory failed");
            _testTaxCategory = taxCategoryTask.Result.Result;
            Assert.NotNull(_testTaxCategory.Id);

            Task <Response <ZoneQueryResult> > zoneQueryResultTask = _client.Zones().QueryZonesAsync();

            zoneQueryResultTask.Wait();
            Assert.IsTrue(zoneQueryResultTask.Result.Success);

            if (zoneQueryResultTask.Result.Result.Results.Count > 0)
            {
                _testZone        = zoneQueryResultTask.Result.Result.Results[0];
                _createdTestZone = false;
            }
            else
            {
                ZoneDraft zoneDraft = Helper.GetTestZoneDraft();
                Task <Response <Zone> > zoneTask = _client.Zones().CreateZoneAsync(zoneDraft);
                zoneTask.Wait();
                Assert.IsTrue(zoneTask.Result.Success, "CreateZone failed");
                _testZone        = zoneTask.Result.Result;
                _createdTestZone = true;
            }

            Assert.NotNull(_testZone.Id);

            foreach (string country in _project.Countries)
            {
                Location location =
                    _testZone.Locations
                    .Where(l => l.Country.Equals(country, StringComparison.OrdinalIgnoreCase))
                    .FirstOrDefault();

                if (location == null)
                {
                    location         = new Location();
                    location.Country = country;

                    AddLocationAction       addLocationAction = new AddLocationAction(location);
                    Task <Response <Zone> > updateZoneTask    = _client.Zones().UpdateZoneAsync(_testZone, addLocationAction);
                    updateZoneTask.Wait();
                    Assert.IsTrue(updateZoneTask.Result.Success, "UpdateZone failed");
                    _testZone = updateZoneTask.Result.Result;
                }
            }

            Assert.NotNull(_testZone.Locations.Count > 0);

            ShippingMethodDraft shippingMethodDraft =
                Helper.GetTestShippingMethodDraft(_project, _testTaxCategory, _testZone);
            Task <Response <ShippingMethod> > shippingMethodTask =
                _client.ShippingMethods().CreateShippingMethodAsync(shippingMethodDraft);

            shippingMethodTask.Wait();
            Assert.IsTrue(shippingMethodTask.Result.Success, "CreateShippingMethod failed");
            _testShippingMethod = shippingMethodTask.Result.Result;

            Assert.NotNull(_testShippingMethod.Id);

            ProductDraft productDraft = Helper.GetTestProductDraft(_project, _testProductType.Id, _testTaxCategory.Id);
            Task <Response <Product> > testProductTask = _client.Products().CreateProductAsync(productDraft);

            testProductTask.Wait();
            Assert.IsTrue(testProductTask.Result.Success, "CreateProduct failed");
            _testProduct = testProductTask.Result.Result;

            Assert.NotNull(_testProduct.Id);

            PaymentDraft paymentDraft = Helper.GetTestPaymentDraft(_project, _testCustomers[0].Id);
            Task <Response <Payment> > paymentTask = _client.Payments().CreatePaymentAsync(paymentDraft);

            paymentTask.Wait();
            Assert.IsTrue(paymentTask.Result.Success, "CreatePayment failed");
            _testPayment = paymentTask.Result.Result;

            Assert.NotNull(_testPayment.Id);

            TypeDraft typeDraft = Helper.GetTypeDraft(_project);
            Task <Response <Type> > typeTask = _client.Types().CreateTypeAsync(typeDraft);

            typeTask.Wait();
            Assert.IsTrue(typeTask.Result.Success, "CreateType failed");
            _testType = typeTask.Result.Result;
        }