use of com.google.api.ads.adwords.axis.v201809.cm.AdGroupCriterion in project googleads-java-lib by googleads.
the class ProductPartitionTreeTest method testCreateMultiNodeTreeFromScratch.
/**
* Tests creating an empty tree and then adding several levels of nodes.
*/
@Test
public void testCreateMultiNodeTreeFromScratch() {
ProductPartitionTree tree = ProductPartitionTree.createAdGroupTree(-1L, biddingStrategyConfig, Collections.<AdGroupCriterion>emptyList());
ProductPartitionNode rootNode = tree.getRoot().asSubdivision();
ProductPartitionNode brand1 = rootNode.addChild(ProductDimensions.createBrand("google")).asSubdivision();
ProductPartitionNode brand1Offer1 = brand1.addChild(ProductDimensions.createOfferId("A")).asBiddableUnit().setBid(1000000L).putCustomParameter("param1", "value1").putCustomParameter("param2", "value2");
ProductPartitionNode brand1Offer2 = brand1.addChild(ProductDimensions.createOfferId(null)).asExcludedUnit();
ProductPartitionNode brand2 = rootNode.addChild(ProductDimensions.createBrand(null)).asExcludedUnit();
int expectedOpCount = 5;
List<AdGroupCriterionOperation> mutateOperations = tree.getMutateOperations();
assertEquals("Number of operations is incorrect", expectedOpCount, mutateOperations.size());
List<CriterionDescriptor> nodeDescriptors = Stream.of(rootNode, brand1, brand1Offer1, brand1Offer2, brand2).map(CriterionDescriptor::new).collect(Collectors.toList());
int opNum = 0;
List<CriterionDescriptor> opDescriptors = Lists.newArrayList();
Map<Long, CriterionDescriptor> opDescriptorsById = Maps.newHashMap();
for (AdGroupCriterionOperation op : mutateOperations) {
CriterionDescriptor opDescriptor = new CriterionDescriptor(op.getOperand(), opNum++);
opDescriptors.add(opDescriptor);
opDescriptorsById.put(opDescriptor.partitionId, opDescriptor);
}
Map<Long, Map<Long, CriterionDescriptor>> opDescriptorMap = buildDescriptorMap(opDescriptors);
for (CriterionDescriptor nodeDescriptor : nodeDescriptors) {
CriterionDescriptor opDescriptor = opDescriptorMap.get(nodeDescriptor.parentPartitionId).get(nodeDescriptor.partitionId);
nodeDescriptor.assertDescriptorEquals(opDescriptor);
AdGroupCriterionOperation op = mutateOperations.get(opDescriptor.operationNumber);
assertEquals("operator is incorrect", Operator.ADD, op.getOperator());
if (nodeDescriptor.parentPartitionId != null) {
CriterionDescriptor parentOpDescriptor = opDescriptorsById.get(nodeDescriptor.parentPartitionId);
assertNotNull("no operation found for parent", parentOpDescriptor);
assertThat("operation # for parent is > operation # for child", opDescriptor.operationNumber, Matchers.greaterThan(parentOpDescriptor.operationNumber));
}
}
assertThat("Tree toString does not contain the root's detailed toString", tree.toString(), Matchers.containsString(tree.getRoot().toDetailedString()));
assertThat("Tree toString does not contain the ad group ID", tree.toString(), Matchers.containsString(tree.getAdGroupId().toString()));
}
use of com.google.api.ads.adwords.axis.v201809.cm.AdGroupCriterion in project googleads-java-lib by googleads.
the class ProductPartitionTreeTest method testCreateTreeUsingService.
/**
* Tests that the factory method that retrieves the tree using API services builds
* the correct tree and passes the correct paging arguments.
*/
@Test
public void testCreateTreeUsingService() throws Exception {
AdWordsServicesInterface adWordsServices = AdWordsServices.getInstance();
AdWordsSession session = new AdWordsSession.Builder().withClientCustomerId("123-456-7890").withOAuth2Credential(new Credential(BearerToken.authorizationHeaderAccessMethod())).withDeveloperToken("devtoken").withUserAgent("test").withEndpoint(testHttpServer.getServerUrl()).build();
// Extract the API version from this test's package.
List<String> packageComponents = Lists.newArrayList(Splitter.on('.').split(getClass().getPackage().getName()));
final String apiVersion = packageComponents.get(packageComponents.size() - 2);
final int pageSize = 100;
final int numberOfCriteria = (pageSize * 5) + 1;
// Construct a list of CriterionDescriptors that will build a tree of the form:
// root
// OfferId = null EXCLUDED
// OfferId = 1 BIDDABLE
// OfferId = 2 BIDDABLE
// ...
// OfferId = numberOfCriteria BIDDABLE
List<CriterionDescriptor> descriptors = Lists.newArrayList();
long partitionId = 1L;
final long rootPartitionId = partitionId;
descriptors.add(new CriterionDescriptor(false, false, null, null, partitionId++, null));
descriptors.add(new CriterionDescriptor(true, true, ProductDimensions.createOfferId(null), null, partitionId++, rootPartitionId));
for (int i = 1; i <= (numberOfCriteria - 2); i++) {
CriterionDescriptor descriptor = new CriterionDescriptor(true, false, ProductDimensions.createOfferId(Integer.toString(i)), 10000000L, partitionId++, rootPartitionId, i == 2 ? "http://wwww.example.com/tracking?{lpurl}" : null);
descriptor.customParams.put("param1", "value1");
descriptor.customParams.put("param2", "value2");
descriptors.add(descriptor);
}
// Split the descriptor list into batches of size pageSize.
List<List<CriterionDescriptor>> descriptorBatches = Lists.partition(descriptors, pageSize);
List<String> responseBodies = Lists.newArrayList();
for (List<CriterionDescriptor> descriptorBatch : descriptorBatches) {
// For this batch of descriptors, manually construct the AdGroupCriterionPage
// to return. This is required because AdWordsServices is a final class, so this test
// cannot mock its behavior.
AdGroupCriterionPage mockPage = new AdGroupCriterionPage();
mockPage.setTotalNumEntries(numberOfCriteria);
mockPage.setEntries(new AdGroupCriterion[descriptorBatch.size()]);
int i = 0;
for (CriterionDescriptor descriptor : descriptorBatch) {
mockPage.setEntries(i++, descriptor.createCriterion());
}
// Serialize the page.
StringWriter writer = new StringWriter();
SerializationContext serializationContext = new SerializationContext(writer) {
/**
* Override the serialize method called by the Axis serializer and force it to
* pass {@code includeNull = false}.
*/
@SuppressWarnings("rawtypes")
@Override
public void serialize(QName elemQName, Attributes attributes, Object value, QName xmlType, Class javaType) throws IOException {
super.serialize(elemQName, attributes, value, xmlType, javaType, false, null);
}
};
serializationContext.setSendDecl(false);
new AxisSerializer().serialize(mockPage, serializationContext);
// Wrap the serialized page in a SOAP envelope.
StringBuilder response = new StringBuilder();
response.append("<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">" + "<soap:Header/><soap:Body>");
response.append(String.format("<getResponse xmlns=\"https://adwords.google.com/api/adwords/cm/%s\">", apiVersion));
// Replace the element name AdGroupCriterionPage with the expected name rval in the
// serialized page.
response.append(writer.toString().replaceAll("AdGroupCriterionPage", "rval"));
response.append("</getResponse></soap:Body></soap:Envelope>");
responseBodies.add(response.toString());
}
// Set the test server to return the response bodies constructed above.
testHttpServer.setMockResponseBodies(responseBodies);
// Build the tree.
ProductPartitionTree tree = ProductPartitionTree.createAdGroupTree(adWordsServices, session, 9999L);
// First, confirm that the paging elements were correct in each request's selector.
int requestNumber = 0;
for (String requestBody : testHttpServer.getAllRequestBodies()) {
int expectedOffset = requestNumber * pageSize;
assertThat("numberResults paging element is missing or incorrect in request", requestBody, Matchers.containsString("numberResults>" + pageSize + "</"));
if (requestNumber == 0) {
assertThat("startIndex paging element unexpectedly found in the first request", requestBody, Matchers.not(Matchers.containsString("startIndex>")));
} else {
assertThat("startIndex paging element is missing or incorrect in request", requestBody, Matchers.containsString("startIndex>" + expectedOffset + "</"));
}
requestNumber++;
}
// Confirm that the tree returned by the factory method matches the expected tree.
descriptors.get(0).assertDescriptorEquals(new CriterionDescriptor(tree.getRoot()));
// Get a map of all of the child descriptors for the root node.
Map<Long, CriterionDescriptor> descriptorMap = buildDescriptorMap(descriptors).get(rootPartitionId);
// Confirm each ProductPartitionNode under the root node has a matching entry in the descriptor
// map.
int childrenFound = 0;
for (ProductPartitionNode childNode : tree.getRoot().getChildren()) {
CriterionDescriptor nodeDescriptor = new CriterionDescriptor(childNode);
nodeDescriptor.assertDescriptorEquals(descriptorMap.get(nodeDescriptor.partitionId));
childrenFound++;
}
assertEquals("Did not find an entry in the response for every expected child node", descriptorMap.size(), childrenFound);
}
use of com.google.api.ads.adwords.axis.v201809.cm.AdGroupCriterion in project googleads-java-lib by googleads.
the class ProductPartitionTreeTest method testRemovedCriteriaIgnored.
/**
* Tests that the factory method ignores removed criteria.
*/
@Test
public void testRemovedCriteriaIgnored() {
CriterionDescriptor rootDescriptor = new CriterionDescriptor(true, false, null, 1000000L, 1L, null);
List<AdGroupCriterion> criteria = Lists.newArrayList();
criteria.add(rootDescriptor.createCriterion());
// Create a criteria for a child node and set its UserStatus to REMOVED.
ProductBrand brandGoogle = ProductDimensions.createBrand("google");
CriterionDescriptor removedDescriptor = new CriterionDescriptor(true, false, brandGoogle, null, 2L, 1L);
AdGroupCriterion removedCriterion = removedDescriptor.createCriterion();
((BiddableAdGroupCriterion) removedCriterion).setUserStatus(UserStatus.REMOVED);
criteria.add(removedCriterion);
ProductPartitionTree tree = ProductPartitionTree.createAdGroupTree(-1L, biddingStrategyConfig, criteria);
assertFalse("Brand = google criteria had status removed, but it is in the tree", tree.getRoot().hasChild(brandGoogle));
}
Aggregations