Package com.google.api.adwords.v201008

Source Code of com.google.api.adwords.v201008.AdGroupAdServiceTest

// Copyright 2011 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.api.adwords.v201008;

import junit.framework.TestCase;

import com.google.api.adwords.lib.AdWordsService;
import com.google.api.adwords.lib.AdWordsUser;
import com.google.api.adwords.v201008.cm.Ad;
import com.google.api.adwords.v201008.cm.AdGroup;
import com.google.api.adwords.v201008.cm.AdGroupAd;
import com.google.api.adwords.v201008.cm.AdGroupAdPage;
import com.google.api.adwords.v201008.cm.AdGroupAdOperation;
import com.google.api.adwords.v201008.cm.AdGroupAdReturnValue;
import com.google.api.adwords.v201008.cm.AdGroupAdSelector;
import com.google.api.adwords.v201008.cm.AdGroupAdServiceInterface;
import com.google.api.adwords.v201008.cm.AdGroupAdStatus;
import com.google.api.adwords.v201008.cm.AdGroupOperation;
import com.google.api.adwords.v201008.cm.AdGroupPage;
import com.google.api.adwords.v201008.cm.AdGroupReturnValue;
import com.google.api.adwords.v201008.cm.AdGroupSelector;
import com.google.api.adwords.v201008.cm.AdGroupServiceInterface;
import com.google.api.adwords.v201008.cm.AdGroupStatus;
import com.google.api.adwords.v201008.cm.AdStatsSelector;
import com.google.api.adwords.v201008.cm.ApiException;
import com.google.api.adwords.v201008.cm.Bid;
import com.google.api.adwords.v201008.cm.DateRange;
import com.google.api.adwords.v201008.cm.ManualCPCAdGroupBids;
import com.google.api.adwords.v201008.cm.Money;
import com.google.api.adwords.v201008.cm.Operator;
import com.google.api.adwords.v201008.cm.StatsSelector;
import com.google.api.adwords.v201008.cm.TextAd;

/**
* Functional tests for AdGroupAdService.
*
* @author api.naoki.ishihara@gmail.com (Naoki Ishihara)
*/
public class AdGroupAdServiceTest extends TestCase {
  private AdWordsUser user = null;
  private long campaignId;
  private long adGroupId;
  private long adId;
  private AdGroupAdServiceInterface service = null;
  private TestUtils testUtils = null;

  /**
   * Set up the test fixtures.
   */
  @Override
  protected void setUp() throws Exception {
    super.setUp();
    user = new AdWordsUser("test_data/test.properties");
    service = user.getService(AdWordsService.V201008.ADGROUP_AD_SERVICE);
    testUtils = new TestUtils(user);
    campaignId = testUtils.createCampaign();
    adGroupId = testUtils.createAdGroup(campaignId);
    adId = testUtils.createTextAd(adGroupId);
  }

  /**
   * Test adding an ad group ad.
   */
  public void testAdd() throws Exception {
    // Create text ad.
    TextAd textAd = new TextAd();
    textAd.setHeadline("Luxury Cruise to Mars");
    textAd.setDescription1("Visit the Red Planet in style.");
    textAd.setDescription2("Low-gravity fun for everyone!");
    textAd.setDisplayUrl("www.example.com");
    textAd.setUrl("http://www.example.com");

    // Create ad group ad.
    AdGroupAd ad = new AdGroupAd();
    ad.setAdGroupId(adGroupId);
    ad.setAd(textAd);

    // Create operations.
    AdGroupAdOperation textAdGroupAdOperation = new AdGroupAdOperation();
    textAdGroupAdOperation.setOperand(ad);
    textAdGroupAdOperation.setOperator(Operator.ADD);
    AdGroupAdOperation[] operations = new AdGroupAdOperation[] {textAdGroupAdOperation};

    // Add ads.
    AdGroupAdReturnValue result = service.mutate(operations);
    AdGroupAd testAd = result.getValue()[0];

    // Set the generated fields.
    ad.getAd().setApprovalStatus(testAd.getAd().getApprovalStatus());
    ad.getAd().setAdType(testAd.getAd().getAdType());
    ad.getAd().setId(testAd.getAd().getId());
    ad.setStatus(testAd.getStatus());

    TestUtils.assertDeepReflectionEquals(ad, testAd);
  }

  /**
   * Test updating an ad group ad.
   */
  public void testUpdate() throws Exception {
    // Create ad with updated status.
    Ad ad = new Ad();
    ad.setId(adId);

    // Create ad with updated status
    AdGroupAd adGroupAd = new AdGroupAd();
    adGroupAd.setAdGroupId(adGroupId);
    adGroupAd.setAd(ad);
    adGroupAd.setStatus(AdGroupAdStatus.PAUSED);

    // Create operations.
    AdGroupAdOperation operation = new AdGroupAdOperation();
    operation.setOperand(adGroupAd);
    operation.setOperator(Operator.SET);

    AdGroupAdOperation[] operations = new AdGroupAdOperation[] {operation};

    // Update ad.
    AdGroupAdReturnValue result = service.mutate(operations);
    AdGroupAd testAdGroupAd = result.getValue()[0];

    assertEquals(adGroupAd.getStatus(), testAdGroupAd.getStatus());
  }

  /**
   * Test removing an ad group ad.
   */
  public void testRemove() throws Exception {
    // Create ad with updated status.
    Ad ad = new Ad();
    ad.setId(adId);

    // Create ad with updated status
    AdGroupAd adGroupAd = new AdGroupAd();
    adGroupAd.setAdGroupId(adGroupId);
    adGroupAd.setAd(ad);
    adGroupAd.setStatus(AdGroupAdStatus.PAUSED);

    // Create operations.
    AdGroupAdOperation operation = new AdGroupAdOperation();
    operation.setOperand(adGroupAd);
    operation.setOperator(Operator.REMOVE);

    AdGroupAdOperation[] operations = new AdGroupAdOperation[] {operation};

    // Remove ad.
    AdGroupAdReturnValue result = service.mutate(operations);
    AdGroupAd testAdGroupAd = result.getValue()[0];

    assertEquals(AdGroupAdStatus.DISABLED, testAdGroupAd.getStatus());
  }

  /**
   * Test getting an ad group ad.
   */
  public void testGet() throws Exception {
    // Create ad selector.
    AdGroupAdSelector selector = new AdGroupAdSelector();
    selector.setAdIds(new long[] {adId});

    // Create date range.
    DateRange range = new DateRange(TestUtils.firstDayOfMonth(), TestUtils.today());
    selector.setStatsSelector(new AdStatsSelector(range, null));

    // Get ads with the selector created above.
    AdGroupAdPage page = service.get(selector);

    assertNotNull(page);
    assertEquals(1, page.getEntries().length);
    assertNotNull(page.getEntries()[0]);
  }

  /**
   * Test getting all ad group ads in an ad group.
   */
  public void testGetAllInAdGroup() throws Exception {
    // Create ad selector.
    AdGroupAdSelector selector = new AdGroupAdSelector();
    selector.setAdGroupIds(new long[] {adGroupId});

    // Get ads with the selector created above.
    AdGroupAdPage page = service.get(selector);

    assertNotNull(page);
    assertNotNull(page.getEntries());
    assertTrue("Expected at least 1 entry", page.getTotalNumEntries() >= 1);
  }

  /**
   * Test getting all ad group ads in an ad group.
   */
  public void testGetAllInCampaign() throws Exception {
    // Create ad selector.
    AdGroupAdSelector selector = new AdGroupAdSelector();
    selector.setCampaignIds(new long[] {campaignId});

    // Get ads with the selector created above.
    AdGroupAdPage page = service.get(selector);

    assertNotNull(page);
    assertNotNull(page.getEntries());
    assertTrue("Expected at least 1 entry", page.getTotalNumEntries() >= 1);
  }

  /**
   * Test getting all ad group ads.
   */
  public void testGetAll() throws Exception {
    // Get ads with a created selector.
    AdGroupAdSelector selector = new AdGroupAdSelector();
    AdGroupAdPage page = service.get(selector);

    assertNotNull(page);
    assertNotNull(page.getEntries());
    assertTrue("Expected at least 1 entry", page.getTotalNumEntries() >= 1);
  }
}
TOP

Related Classes of com.google.api.adwords.v201008.AdGroupAdServiceTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.