public void ParentTest()
        {
            var category = "stocks";

            var categoryName = new ProductCategoryName(category, category);

            var helper = new CategoryLeafHelper(categoryName.Nodes);

            Assert.Single(helper.LeafNodes);
            Assert.Equal("stocks", helper.LeafNodes.First().Id);
        }
        public async Task <List <string> > Validate(List <ProductAndCategoryPair> pairs)
        {
            var categoryNames = pairs.Select(x => x.Category).Distinct();

            var categoryNameValidations =
                new Dictionary <string, Result <ProductCategoryName, ProductCategoriesErrorCodes> >();

            foreach (var category in categoryNames)
            {
                var validationResult = await _productCategoryStringService.Create(category);

                categoryNameValidations.Add(category, validationResult);
            }

            // product category name validation failed
            if (categoryNameValidations.Any(kvp => kvp.Value.IsFailed))
            {
                return(categoryNameValidations
                       .Where(kvp => kvp.Value.IsFailed)
                       .Select((kvp) => $"Category \"{kvp.Key}\" is not a valid category")
                       .ToList());
            }

            var allNodes = categoryNameValidations
                           .SelectMany(kvp => kvp.Value.Value.Nodes)
                           .ToList();

            var leafHelper = new CategoryLeafHelper(allNodes);

            var errorMessages = new List <string>();

            // product must be in a leaf category validation
            foreach (var pair in pairs)
            {
                var categoryName           = pair.Category;
                var normalizedCategoryName = categoryNameValidations[categoryName].Value.NormalizedName;
                if (!leafHelper.IsLeaf(normalizedCategoryName))
                {
                    errorMessages.Add(
                        $"Product {pair.ProductId} cannot be attached to a category {categoryName} because it is not a leaf category");
                }
            }

            return(errorMessages);
        }
        public void TwoParentsTest()
        {
            var category1 = "stocks";
            var category2 = "futures";

            var categoryName1 = new ProductCategoryName(category1, category1);
            var categoryName2 = new ProductCategoryName(category2, category2);


            var allNodes = new List <ProductCategory>();

            allNodes.AddRange(categoryName1.Nodes);
            allNodes.AddRange(categoryName2.Nodes);

            var helper = new CategoryLeafHelper(allNodes);

            Assert.Equal(2, helper.LeafNodes.Count);
        }
        public void SubcategoryTest()
        {
            var category1 = "stocks";
            var category2 = "stocks.germany";

            var categoryName1 = new ProductCategoryName(category1, category1);
            var categoryName2 = new ProductCategoryName(category2, category2);

            var allNodes = new List <ProductCategory>();

            allNodes.AddRange(categoryName1.Nodes);
            allNodes.AddRange(categoryName2.Nodes);

            var helper = new CategoryLeafHelper(allNodes);

            Assert.Single(helper.LeafNodes);
            Assert.Equal("stocks.germany", helper.LeafNodes.First().Id);
        }
        public void TwoParentsOneSubcategoryTest()
        {
            var category1 = "stocks";
            var category2 = "stocks.germany";
            var category3 = "futures";

            var categoryName1 = new ProductCategoryName(category1, category1);
            var categoryName2 = new ProductCategoryName(category2, category2);
            var categoryName3 = new ProductCategoryName(category3, category3);

            var allNodes = new List <ProductCategory>();

            allNodes.AddRange(categoryName1.Nodes);
            allNodes.AddRange(categoryName2.Nodes);
            allNodes.AddRange(categoryName3.Nodes);

            var helper = new CategoryLeafHelper(allNodes);

            Assert.Equal(2, helper.LeafNodes.Count);

            Assert.Contains(helper.LeafNodes, node => node.Id == "stocks.germany");
            Assert.Contains(helper.LeafNodes, node => node.Id == "futures");
        }