int numAdGrp = generator.nextInt(6) + 1;
for (int j = 0; j < numAdGrp; j++) {
AdGroupPage adGroupPage =
adGroupService.get(new AdGroupSelector(campaign.getId(), null, null, null));
AdGroup[] adGroups = adGroupPage.getEntries();
AdGroup adGroup = null;
if (adGroups != null && j < adGroups.length) {
adGroup = adGroups[j];
} else {
adGroup = new AdGroup();
adGroup.setName("AdGroup " + (j + 1));
adGroup.setBids(new ManualCPCAdGroupBids(null, new Bid(new Money("USD",
generator.nextInt(10) * 50000L + 50000L)), null, null));
adGroup.setCampaignId(campaign.getId());
AdGroupReturnValue adGroupReturnValue =
adGroupService.mutate(new AdGroupOperation[] {new AdGroupOperation(Operator.ADD,
null, adGroup)});
adGroup = adGroupReturnValue.getValue()[0];
}
int numAds = generator.nextInt(10) + 1;
for (int k = 0; k < numAds; k++) {
TextAd ad = new TextAd();
ad.setDescription1("This is the description");
ad.setDescription2("Second line of the description");
ad.setHeadline("This is the headline " + generator.nextInt(5000));
ad.setDisplayUrl("http://google.com/");
ad.setUrl("http://example.com");
AdGroupAd adGroupAd = new AdGroupAd();
adGroupAd.setAd(ad);
adGroupAd.setAdGroupId(adGroup.getId());
adGroupAdService.mutate(new AdGroupAdOperation[] {new AdGroupAdOperation(Operator.ADD,
"ADD", adGroupAd, null)});
}
numAds = generator.nextInt(10) + 1;
for (int k = 0; k < numAds; k++) {
ImageAd ad = new ImageAd();
Image image = new Image();
image.setData(DataUtils.getTestImage());
image.setName("name.jpg");
ad.setImage(image);
ad.setDisplayUrl("http://google.com/");
ad.setUrl("http://example.com");
ad.setName("name.jpg");
AdGroupAd adGroupAd = new AdGroupAd();
adGroupAd.setAd(ad);
adGroupAd.setAdGroupId(adGroup.getId());
adGroupAdService.mutate(new AdGroupAdOperation[] {new AdGroupAdOperation(Operator.ADD,
"ADD", adGroupAd, null)});
}
ArrayList<AdGroupCriterionOperation> criteriaOps =
new ArrayList<AdGroupCriterionOperation>();
int numKeywords = generator.nextInt(10) + 1;
for (int k = 0; k < numKeywords; k++) {
Keyword keyword = new Keyword();
keyword.setText("test" + generator.nextInt(5000));
keyword.setMatchType(KeywordMatchType.PHRASE);
BiddableAdGroupCriterion criterion = new BiddableAdGroupCriterion();
criterion.setAdGroupId(adGroup.getId());
criterion.setCriterion(keyword);
criteriaOps.add(new AdGroupCriterionOperation(Operator.ADD, "ADD", criterion, null));
}