public void TestNavigation() {
      rootNode = rootNode.AsSubdivision();
      ProductBrand brandGoogle = ProductDimensions.CreateBrand("google");
      ProductBrand brandOther = ProductDimensions.CreateBrand(null);
      ProductCanonicalCondition conditionNew =
          ProductDimensions.CreateCanonicalCondition(ProductCanonicalConditionCondition.NEW);
      ProductCanonicalCondition conditionUsed =
          ProductDimensions.CreateCanonicalCondition(ProductCanonicalConditionCondition.USED);
      ProductCanonicalCondition conditionOther = ProductDimensions.CreateCanonicalCondition();

      // Build up the brand = Google node under the root.
      ProductPartitionNode brandGoogleNode = rootNode.AddChild(brandGoogle).AsSubdivision();
      brandGoogleNode.AddChild(conditionNew);
      brandGoogleNode.AddChild(conditionUsed);
      brandGoogleNode.AddChild(conditionOther);

      Assert.True(brandGoogleNode.HasChild(conditionNew),
          "HasChild should return true for existing child dimension.");
      Assert.AreSame(brandGoogleNode, brandGoogleNode.GetChild(conditionNew).Parent,
          "parent->GetChild->getParent should return parent.");
      Assert.True(brandGoogleNode.HasChild(conditionUsed),
          "HasChild should return true for existing child dimension.");
      Assert.AreSame(brandGoogleNode, brandGoogleNode.GetChild(conditionUsed).Parent,
          "parent->GetChild->getParent should return parent.");
      Assert.True(brandGoogleNode.HasChild(conditionOther),
          "HasChild should return true for existing child dimension.");
      Assert.AreSame(brandGoogleNode, brandGoogleNode.GetChild(conditionOther).Parent,
          "parent->GetChild->getParent should return parent.");

      // Build up the brand = null (other) node under the root.
      ProductPartitionNode brandOtherNode = rootNode.AddChild(brandOther).AsSubdivision();
      brandOtherNode.AddChild(conditionNew);
      Assert.True(brandOtherNode.HasChild(conditionNew),
          "HasChild should return true for existing child dimension.");
      Assert.AreSame(brandOtherNode, brandOtherNode.GetChild(conditionNew).Parent,
          "parent->GetChild->getParent should return parent.");
      Assert.False(brandOtherNode.HasChild(conditionUsed),
          "HasChild should return false for nonexistent child dimension.");
      Assert.False(brandOtherNode.HasChild(conditionOther),
          "HasChild should return false for nonexistent child dimension.");
      brandOtherNode.AddChild(conditionOther);
      Assert.True(brandOtherNode.HasChild(conditionOther),
          "HasChild should return true for existing child dimension.");
      Assert.AreSame(brandOtherNode, brandOtherNode.GetChild(conditionOther).Parent,
          "parent->GetChild->getParent should return parent.");

      // Remove one of the children of brand = null.
      brandOtherNode.RemoveChild(conditionOther);
      Assert.False(brandOtherNode.HasChild(conditionOther),
          "HasChild should return false for a removed child dimension.");

      // Remove the rest of the children of brand = null.
      brandOtherNode.RemoveAllChildren();
      Assert.False(brandOtherNode.HasChild(conditionNew),
          "HasChild should return false for any removed child dimension.");
      Assert.False(brandOtherNode.HasChild(conditionUsed),
          "HasChild should return false for any removed child dimension.");
    }
        /// <summary>
        /// Creates a product partition tree.
        /// </summary>
        /// <param name="user">The AdWords user for which the product partition is created.</param>
        /// <param name="adGroupId">Ad group ID.</param>
        /// <returns>The product partition.</returns>
        private static ProductPartitionTree CreateProductPartition(AdWordsUser user, long adGroupId)
        {
            using (AdGroupCriterionService adGroupCriterionService =
                       (AdGroupCriterionService)user.GetService(AdWordsService.v201809
                                                                .AdGroupCriterionService))
            {
                // Build a new ProductPartitionTree using the ad group's current set of criteria.
                ProductPartitionTree partitionTree =
                    ProductPartitionTree.DownloadAdGroupTree(user, adGroupId);

                Console.WriteLine("Original tree: {0}", partitionTree);

                // Clear out any existing criteria.
                ProductPartitionNode rootNode = partitionTree.Root.RemoveAllChildren();

                // Make the root node a subdivision.
                rootNode = rootNode.AsSubdivision();

                // Add a unit node for condition = NEW to include it.
                rootNode.AddChild(
                    ProductDimensions.CreateCanonicalCondition(ProductCanonicalConditionCondition
                                                               .NEW));

                // Add a unit node for condition = USED to include it.
                rootNode.AddChild(
                    ProductDimensions.CreateCanonicalCondition(ProductCanonicalConditionCondition
                                                               .USED));

                // Exclude everything else.
                rootNode.AddChild(ProductDimensions.CreateCanonicalCondition()).AsExcludedUnit();

                // Make the mutate request, using the operations returned by the
                // ProductPartitionTree.
                AdGroupCriterionOperation[] mutateOperations = partitionTree.GetMutateOperations();

                if (mutateOperations.Length == 0)
                {
                    Console.WriteLine(
                        "Skipping the mutate call because the original tree and the current " +
                        "tree are logically identical.");
                }
                else
                {
                    adGroupCriterionService.mutate(mutateOperations);
                }

                // The request was successful, so create a new ProductPartitionTree based on the
                // updated state of the ad group.
                partitionTree = ProductPartitionTree.DownloadAdGroupTree(user, adGroupId);
                return(partitionTree);
            }
        }
    public void TestCreateCanonicalCondition() {
      ProductCanonicalCondition conditionA = new ProductCanonicalCondition() {
        condition = ProductCanonicalConditionCondition.NEW
      };

      ProductCanonicalCondition conditionB =
          ProductDimensions.CreateCanonicalCondition(ProductCanonicalConditionCondition.NEW);

      Assert.True(comparer.Equals(conditionA, conditionB));

      ProductCanonicalCondition conditionC = new ProductCanonicalCondition() {
      };

      ProductCanonicalCondition conditionD = ProductDimensions.CreateCanonicalCondition();

      Assert.True(comparer.Equals(conditionC, conditionD));
    }
        /// <summary>
        /// Creates a tree of the form:
        ///
        /// <pre>
        /// ROOT
        ///   ProductType Level1 shoes
        ///     ProductType Level2 athletic shoes
        ///       Condition new $2.50
        ///       Condition used $1.00
        ///       Other - exclude from bidding
        ///     ProductType Level2 walking shoes
        ///       Condition new $3.50
        ///       Condition used $1.25
        ///       Other $1.00
        ///     ProductType Level2 null (everything else) - exclude from bidding
        ///   ProductType Level1 clothing
        ///     ProductType Level2 winter clothing
        ///       Condition new $1.00
        ///       Condition used $1.25
        ///       Other $1.50
        ///     ProductType Level2 summer clothing
        ///       Condition new $1.10
        ///       Condition used $1.00
        ///       Other $1.25
        ///     ProductType Level2 null (everything else)
        ///       Condition new $0.90
        ///       Condition used $0.85
        ///       Other $0.75
        ///   ProductType Level1 null (everything else) - exclude from bidding
        /// </pre>
        /// </summary>
        private void RebuildComplexTree()
        {
            // Clear out the tree.
            ProductPartitionNode rootNode = tree.Root.RemoveAllChildren().AsSubdivision();

            ProductPartitionNode shoesLevel1 = rootNode.AddChild(ProductDimensions.CreateType(
                                                                     ProductDimensionType.PRODUCT_TYPE_L1, "shoes")).AsSubdivision();

            ProductPartitionNode athleticShoesLevel2 = shoesLevel1.AddChild(
                ProductDimensions.CreateType(ProductDimensionType.PRODUCT_TYPE_L2, "athletic shoes"))
                                                       .AsSubdivision();

            athleticShoesLevel2.AddChild(ProductDimensions.CreateCanonicalCondition(
                                             ProductCanonicalConditionCondition.NEW)).AsBiddableUnit().CpcBid = 2500000L;
            athleticShoesLevel2.AddChild(ProductDimensions.CreateCanonicalCondition(
                                             ProductCanonicalConditionCondition.USED)).AsBiddableUnit().CpcBid = 1000000L;
            athleticShoesLevel2.AddChild(ProductDimensions.CreateCanonicalCondition()).AsExcludedUnit();

            ProductPartitionNode walkingShoesLevel2 = shoesLevel1.AddChild(
                ProductDimensions.CreateType(ProductDimensionType.PRODUCT_TYPE_L2, "walking shoes"))
                                                      .AsSubdivision();

            walkingShoesLevel2.AddChild(ProductDimensions.CreateCanonicalCondition(
                                            ProductCanonicalConditionCondition.NEW)).AsBiddableUnit().CpcBid = 3500000L;
            walkingShoesLevel2.AddChild(ProductDimensions.CreateCanonicalCondition(
                                            ProductCanonicalConditionCondition.USED)).AsBiddableUnit().CpcBid = 1250000L;
            walkingShoesLevel2.AddChild(ProductDimensions.CreateCanonicalCondition()).AsBiddableUnit()
            .CpcBid = 1000000L;

            shoesLevel1.AddChild(ProductDimensions.CreateType(ProductDimensionType.PRODUCT_TYPE_L2))
            .AsExcludedUnit();

            ProductPartitionNode clothingLevel1 = rootNode.AddChild(ProductDimensions.CreateType(
                                                                        ProductDimensionType.PRODUCT_TYPE_L1, "clothing")).AsSubdivision();

            ProductPartitionNode winterClothingLevel2 = clothingLevel1.AddChild(
                ProductDimensions.CreateType(ProductDimensionType.PRODUCT_TYPE_L2, "winter clothing"))
                                                        .AsSubdivision();

            winterClothingLevel2.AddChild(ProductDimensions.CreateCanonicalCondition(
                                              ProductCanonicalConditionCondition.NEW)).AsBiddableUnit().CpcBid = 1000000L;
            winterClothingLevel2.AddChild(ProductDimensions.CreateCanonicalCondition(
                                              ProductCanonicalConditionCondition.USED)).AsBiddableUnit().CpcBid = 1250000L;
            winterClothingLevel2.AddChild(ProductDimensions.CreateCanonicalCondition()).AsBiddableUnit()
            .CpcBid = 1500000L;

            ProductPartitionNode summerClothingLevel2 = clothingLevel1.AddChild(
                ProductDimensions.CreateType(ProductDimensionType.PRODUCT_TYPE_L2, "summer clothing"))
                                                        .AsSubdivision();

            summerClothingLevel2.AddChild(ProductDimensions.CreateCanonicalCondition(
                                              ProductCanonicalConditionCondition.NEW)).AsBiddableUnit().CpcBid = 1100000L;
            summerClothingLevel2.AddChild(ProductDimensions.CreateCanonicalCondition(
                                              ProductCanonicalConditionCondition.USED)).AsBiddableUnit().CpcBid = 1000000L;
            summerClothingLevel2.AddChild(ProductDimensions.CreateCanonicalCondition()).AsBiddableUnit()
            .CpcBid = 1250000L;

            ProductPartitionNode otherClothingLevel2 = clothingLevel1.AddChild(
                ProductDimensions.CreateType(ProductDimensionType.PRODUCT_TYPE_L2, null))
                                                       .AsSubdivision();

            otherClothingLevel2.AddChild(ProductDimensions.CreateCanonicalCondition(
                                             ProductCanonicalConditionCondition.NEW)).AsBiddableUnit().CpcBid = 900000L;
            otherClothingLevel2.AddChild(ProductDimensions.CreateCanonicalCondition(
                                             ProductCanonicalConditionCondition.USED)).AsBiddableUnit().CpcBid = 850000L;
            otherClothingLevel2.AddChild(ProductDimensions.CreateCanonicalCondition()).AsBiddableUnit()
            .CpcBid = 750000L;

            rootNode.AddChild(ProductDimensions.CreateType(ProductDimensionType.PRODUCT_TYPE_L1, null))
            .AsExcludedUnit();

            Assert.DoesNotThrow(delegate() {
                tree = ExecuteTreeOperations();
            });
        }
        /// <summary>
        /// Runs the code example.
        /// </summary>
        /// <param name="user">The AdWords user.</param>
        /// <param name="adGroupId">The ad group to which product partition is
        /// added.</param>
        public void Run(AdWordsUser user, long adGroupId)
        {
            using (AdGroupCriterionService adGroupCriterionService =
                       (AdGroupCriterionService)user.GetService(
                           AdWordsService.v201802.AdGroupCriterionService)) {
                // Build a new ProductPartitionTree using the ad group's current set of criteria.
                ProductPartitionTree partitionTree =
                    ProductPartitionTree.DownloadAdGroupTree(user, adGroupId);

                Console.WriteLine("Original tree: {0}", partitionTree);

                // Clear out any existing criteria.
                ProductPartitionNode rootNode = partitionTree.Root.RemoveAllChildren();

                // Make the root node a subdivision.
                rootNode = rootNode.AsSubdivision();

                // Add a unit node for condition = NEW.
                ProductPartitionNode newConditionNode = rootNode.AddChild(
                    ProductDimensions.CreateCanonicalCondition(ProductCanonicalConditionCondition.NEW));
                newConditionNode.AsBiddableUnit().CpcBid = 200000;

                ProductPartitionNode usedConditionNode = rootNode.AddChild(
                    ProductDimensions.CreateCanonicalCondition(ProductCanonicalConditionCondition.USED));
                usedConditionNode.AsBiddableUnit().CpcBid = 100000;

                // Add a subdivision node for condition = null (everything else).
                ProductPartitionNode otherConditionNode =
                    rootNode.AddChild(ProductDimensions.CreateCanonicalCondition()).AsSubdivision();

                // Add a unit node under condition = null for brand = "CoolBrand".
                ProductPartitionNode coolBrandNode = otherConditionNode.AddChild(
                    ProductDimensions.CreateBrand("CoolBrand"));
                coolBrandNode.AsBiddableUnit().CpcBid = 900000L;

                // Add a unit node under condition = null for brand = "CheapBrand".
                ProductPartitionNode cheapBrandNode = otherConditionNode.AddChild(
                    ProductDimensions.CreateBrand("CheapBrand"));
                cheapBrandNode.AsBiddableUnit().CpcBid = 10000L;

                // Add a subdivision node under condition = null for brand = null (everything else).
                ProductPartitionNode otherBrandNode = otherConditionNode.AddChild(
                    ProductDimensions.CreateBrand(null)).AsSubdivision();

                // Add unit nodes under condition = null/brand = null.
                // The value for each bidding category is a fixed ID for a specific
                // category. You can retrieve IDs for categories from the ConstantDataService.
                // See the 'GetProductCategoryTaxonomy' example for more details.

                // Add a unit node under condition = null/brand = null for product type
                // level 1 = 'Luggage & Bags'.
                ProductPartitionNode luggageAndBagNode = otherBrandNode.AddChild(
                    ProductDimensions.CreateBiddingCategory(ProductDimensionType.BIDDING_CATEGORY_L1,
                                                            -5914235892932915235L));
                luggageAndBagNode.AsBiddableUnit().CpcBid = 750000L;

                // Add a unit node under condition = null/brand = null for product type
                // level 1 = null (everything else).
                ProductPartitionNode everythingElseNode = otherBrandNode.AddChild(
                    ProductDimensions.CreateBiddingCategory(ProductDimensionType.BIDDING_CATEGORY_L1));
                everythingElseNode.AsBiddableUnit().CpcBid = 110000L;

                try {
                    // Make the mutate request, using the operations returned by the ProductPartitionTree.
                    AdGroupCriterionOperation[] mutateOperations = partitionTree.GetMutateOperations();

                    if (mutateOperations.Length == 0)
                    {
                        Console.WriteLine("Skipping the mutate call because the original tree and the " +
                                          "current tree are logically identical.");
                    }
                    else
                    {
                        adGroupCriterionService.mutate(mutateOperations);
                    }

                    // The request was successful, so create a new ProductPartitionTree based on the updated
                    // state of the ad group.
                    partitionTree = ProductPartitionTree.DownloadAdGroupTree(user, adGroupId);

                    Console.WriteLine("Final tree: {0}", partitionTree);
                } catch (Exception e) {
                    throw new System.ApplicationException("Failed to set shopping product partition.", e);
                }
            }
        }