Search in sources :

Example 1 with NodeDifference

use of com.google.api.ads.adwords.axis.utils.v201809.shopping.ProductPartitionNodeDiffer.NodeDifference in project googleads-java-lib by googleads.

the class ProductPartitionTreeImpl method addMutateOperations.

/**
 * Adds to the operations list all operations required to mutate {@code originalNode} to the state
 * of {@code newNode}.
 *
 * <p>The returned set of child product dimensions will only <em>potentially</em> be non-empty if
 * both {@code originalNode != null} and {@code newNode != null}.
 *
 * @param originalNode may be null
 * @param newNode may be null
 * @param ops the operations list to add to
 *
 * @return the set of child product dimensions that require further processing
 */
private Set<ProductDimension> addMutateOperations(@Nullable ProductPartitionNode originalNode, @Nullable ProductPartitionNode newNode, List<OperationPair> ops) {
    Set<ProductDimension> childDimensionsToProcess = Sets.newTreeSet(dimensionComparator);
    NodeDifference nodeDifference = ProductPartitionNodeDiffer.diff(originalNode, newNode, dimensionComparator);
    boolean isProcessChildren;
    switch(nodeDifference) {
        case NEW_NODE:
            ops.addAll(createAddOperations(newNode));
            // No need to further process children. The ADD operations above will include operations
            // for all children of newNode.
            isProcessChildren = false;
            break;
        case REMOVED_NODE:
            ops.add(createRemoveOperation(originalNode));
            // No need to further process children. The REMOVE operation above will perform a
            // cascading delete of all children of newNode.
            isProcessChildren = false;
            break;
        case PARTITION_TYPE_CHANGE:
        case EXCLUDED_UNIT_CHANGE:
            ops.add(createRemoveOperation(originalNode));
            ops.addAll(createAddOperations(newNode));
            // No need to further process children. The ADD operations above will include operations
            // for all children of newNode.
            isProcessChildren = false;
            break;
        case BIDDABLE_UNIT_CHANGE:
            // Ensure that the new node has the proper ID (this may have been lost if the node
            // was removed and then re-added).
            newNode = newNode.setProductPartitionId(originalNode.getProductPartitionId());
            ops.add(createSetBidOperation(newNode));
            // Process the children of newNode. The SET operation above will only handle changes
            // made to newNode, not its children.
            isProcessChildren = true;
            break;
        case NONE:
            // Ensure that the new node has the proper ID (this may have been lost if the node
            // was removed and then re-added).
            newNode = newNode.setProductPartitionId(originalNode.getProductPartitionId());
            // This node does not have changes, but its children may.
            isProcessChildren = true;
            break;
        default:
            throw new IllegalStateException("Unrecognized difference: " + nodeDifference);
    }
    if (isProcessChildren) {
        for (ProductPartitionNode childNode : Iterables.concat(originalNode.getChildren(), newNode.getChildren())) {
            childDimensionsToProcess.add(childNode.getDimension());
        }
    }
    return childDimensionsToProcess;
}
Also used : NodeDifference(com.google.api.ads.adwords.axis.utils.v201809.shopping.ProductPartitionNodeDiffer.NodeDifference) ProductDimension(com.google.api.ads.adwords.axis.v201809.cm.ProductDimension)

Example 2 with NodeDifference

use of com.google.api.ads.adwords.axis.utils.v201809.shopping.ProductPartitionNodeDiffer.NodeDifference in project googleads-java-lib by googleads.

the class ProductPartitionNodeDifferTest method testFindNodeDifference_origNull.

/**
 * Test for when the new node is not null and the original node is null.
 */
@Test
public void testFindNodeDifference_origNull() {
    ProductPartitionNode newNode = new ProductPartitionNode(null, null, -1L, dimensionComparator);
    NodeDifference diff = ProductPartitionNodeDiffer.diff(null, newNode, dimensionComparator);
    assertEquals(NodeDifference.NEW_NODE, diff);
}
Also used : NodeDifference(com.google.api.ads.adwords.axis.utils.v201809.shopping.ProductPartitionNodeDiffer.NodeDifference) Test(org.junit.Test)

Example 3 with NodeDifference

use of com.google.api.ads.adwords.axis.utils.v201809.shopping.ProductPartitionNodeDiffer.NodeDifference in project googleads-java-lib by googleads.

the class ProductPartitionNodeDifferTest method testFindNodeDifference_bothNull.

/**
 * Test for when both nodes are null.
 */
@Test
public void testFindNodeDifference_bothNull() {
    NodeDifference diff = ProductPartitionNodeDiffer.diff(null, null, dimensionComparator);
    assertEquals(NodeDifference.NONE, diff);
}
Also used : NodeDifference(com.google.api.ads.adwords.axis.utils.v201809.shopping.ProductPartitionNodeDiffer.NodeDifference) Test(org.junit.Test)

Example 4 with NodeDifference

use of com.google.api.ads.adwords.axis.utils.v201809.shopping.ProductPartitionNodeDiffer.NodeDifference in project googleads-java-lib by googleads.

the class ProductPartitionNodeDifferTest method testFindNodeDifference_neitherNull_logicallyEquivalent.

/**
 * Test for when both nodes are not null and are logically equivalent.
 */
@Test
public void testFindNodeDifference_neitherNull_logicallyEquivalent() {
    ProductPartitionNode origNode = new ProductPartitionNode(null, ProductDimensions.createOfferId("1234"), -1L, dimensionComparator).asSubdivision();
    ProductPartitionNode newNode = new ProductPartitionNode(null, ProductDimensions.createOfferId("1234"), -1L, dimensionComparator).asSubdivision();
    NodeDifference diff = ProductPartitionNodeDiffer.diff(origNode, newNode, dimensionComparator);
    assertEquals(NodeDifference.NONE, diff);
}
Also used : NodeDifference(com.google.api.ads.adwords.axis.utils.v201809.shopping.ProductPartitionNodeDiffer.NodeDifference) Test(org.junit.Test)

Example 5 with NodeDifference

use of com.google.api.ads.adwords.axis.utils.v201809.shopping.ProductPartitionNodeDiffer.NodeDifference in project googleads-java-lib by googleads.

the class ProductPartitionNodeDifferTest method testFindNodeDifference_newNull.

/**
 * Test for when the new node is null and the original node is not null.
 */
@Test
public void testFindNodeDifference_newNull() {
    ProductPartitionNode origNode = new ProductPartitionNode(null, null, -1L, dimensionComparator);
    NodeDifference diff = ProductPartitionNodeDiffer.diff(origNode, null, dimensionComparator);
    assertEquals(NodeDifference.REMOVED_NODE, diff);
}
Also used : NodeDifference(com.google.api.ads.adwords.axis.utils.v201809.shopping.ProductPartitionNodeDiffer.NodeDifference) Test(org.junit.Test)

Aggregations

NodeDifference (com.google.api.ads.adwords.axis.utils.v201809.shopping.ProductPartitionNodeDiffer.NodeDifference)5 Test (org.junit.Test)4 ProductDimension (com.google.api.ads.adwords.axis.v201809.cm.ProductDimension)1