Package com.google.api.adwords.lib

Source Code of com.google.api.adwords.lib.ServiceAccountantTest

// Copyright 2010 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.lib;

import com.google.api.adwords.v13.ApiException;
import com.google.api.adwords.v200909.cm.CampaignSelector;
import com.google.api.adwords.v200909.cm.Paging;

import junit.framework.TestCase;

import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.List;

/**
* A functional test between {@link ServiceAccountant} and
* {@link ServiceAccountantManager} the classes that use it. Credentials are
* taken from "test_data/test.properties".
*
* @author api.arogal@gmail.com (Adam Rogal)
*/
// TODO(api.arogal): Add tests for faults
public class ServiceAccountantTest extends TestCase {
  private AdWordsUser testUser;

  private com.google.api.adwords.v200909.cm.AdGroupAdServiceInterface adGroupAdV200909Service;
  private com.google.api.adwords.v200909.cm.AdGroupCriterionServiceInterface
      adGroupCriterionV200909Service;
  private com.google.api.adwords.v200909.cm.AdGroupServiceInterface adGroupV200909Service;
  private com.google.api.adwords.v200909.cm.CampaignServiceInterface campaignV200909Service;

  List<ServiceAccountant> v200909ServiceAccoutants;

  private ServiceAccountant adV200909ServiceAccountant;
  private ServiceAccountant adGroupV200909ServiceAccountant;
  private ServiceAccountant campaignV200909ServiceAccountant;
  private ServiceAccountant criterionV200909ServiceAccountant;

  private ServiceAccountantManager serviceAccountantManager;

  @Override
  protected void setUp() throws Exception {
    super.setUp();
    testUser = new AdWordsUser("test_data/test.properties");
    serviceAccountantManager = ServiceAccountantManager.getInstance();
    serviceAccountantManager.setAutoCreateAccountant(true);

    adGroupAdV200909Service = testUser.getService(AdWordsService.V200909.ADGROUP_AD_SERVICE);
    adGroupCriterionV200909Service =
        testUser.getService(AdWordsService.V200909.ADGROUP_CRITERION_SERVICE);
    adGroupV200909Service = testUser.getService(AdWordsService.V200909.ADGROUP_SERVICE);
    campaignV200909Service = testUser.getService(AdWordsService.V200909.CAMPAIGN_SERVICE);

    v200909ServiceAccoutants = new ArrayList<ServiceAccountant>();
    serviceAccountantManager.clearCounters();

    adV200909ServiceAccountant =
        ServiceAccountantManager.createServiceAccountant(adGroupAdV200909Service);
    v200909ServiceAccoutants.add(adV200909ServiceAccountant);

    adGroupV200909ServiceAccountant =
        ServiceAccountantManager.createServiceAccountant(adGroupV200909Service);
    v200909ServiceAccoutants.add(adGroupV200909ServiceAccountant);

    campaignV200909ServiceAccountant =
        ServiceAccountantManager.createServiceAccountant(campaignV200909Service);
    v200909ServiceAccoutants.add(campaignV200909ServiceAccountant);

    criterionV200909ServiceAccountant =
        ServiceAccountantManager.createServiceAccountant(adGroupCriterionV200909Service);
    v200909ServiceAccoutants.add(criterionV200909ServiceAccountant);
  }

  /**
   * Tests that all v200909 service accountants begin with correct values.
   */
  public void testV200909ServiceAccountant_startValues() {
    for (ServiceAccountant serviceAccountant : v200909ServiceAccoutants) {
      assertEquals("Total unit count", 0, serviceAccountant.getTotalUnitCount());
      assertEquals("Total response time", 0, serviceAccountant.getTotalResponseTime());
      assertEquals("Total operation count", 0, serviceAccountant.getTotalOperationCount());

      assertTrue("lastRequestId was empty.", "".equals(serviceAccountant.getLastRequestId()));
      assertEquals("Last operation count", 0, serviceAccountant.getLastOperationCount());
      assertEquals("Last unit count", 0, serviceAccountant.getLastUnitCount());

    }

    assertEquals("Total operation count", 0,
        serviceAccountantManager.getTotalOperationCount());
    assertEquals("Total unit count", 0,
        serviceAccountantManager.getTotalUnitCount());
    assertEquals("Total response time", 0,
        serviceAccountantManager.getTotalResponseTime());
  }

  /**
   * Tests that all v200909 service accountants can record metrics correctly.
   * For now all v200909 services return 0 for all fields.
   */
  public void testV200909ServiceAccountant() throws RemoteException {
    performV200909Operations();

    long totalUnits = 0;
    long totalOperations = 0;
    long totalTime = 0;

    for (ServiceAccountant serviceAccountant : v200909ServiceAccoutants) {
      assertTrue("Total unit count", serviceAccountant.getTotalUnitCount() > 0);
      assertEquals("Total operation count", 1, serviceAccountant.getTotalOperationCount());
      assertTrue("Total response time", serviceAccountant.getTotalResponseTime() > 0);

      assertFalse("lastRequestId was empty.", "".equals(serviceAccountant.getLastRequestId()));
      assertEquals("Last operation count", 1, serviceAccountant.getLastOperationCount());
      assertTrue("Last unit count", serviceAccountant.getLastUnitCount() > 0);

      totalUnits += serviceAccountant.getLastUnitCount();
      totalOperations += serviceAccountant.getLastOperationCount();
      totalTime += serviceAccountant.getLastResponseTime();
    }

    assertEquals("Sum total operation count", totalOperations,
        serviceAccountantManager.getTotalOperationCount());
    assertEquals("Sum total unit count", totalUnits,
        serviceAccountantManager.getTotalUnitCount());
    assertEquals("Sum total time", totalTime,
        serviceAccountantManager.getTotalResponseTime());

    performV200909Operations();

    for (ServiceAccountant serviceAccountant : v200909ServiceAccoutants) {
      assertTrue("Total unit count", serviceAccountant.getTotalUnitCount() > 0);
      assertEquals("Total operation count", 2, serviceAccountant.getTotalOperationCount());
      assertTrue("Total response time", serviceAccountant.getTotalResponseTime() > 0);

      assertFalse("lastRequestId was empty.", "".equals(serviceAccountant.getLastRequestId()));
      assertEquals("Last operation count", 1, serviceAccountant.getLastOperationCount());
      assertTrue("Last unit count", serviceAccountant.getLastUnitCount() > 0);

      totalUnits += serviceAccountant.getLastUnitCount();
      totalOperations += serviceAccountant.getLastOperationCount();
      totalTime += serviceAccountant.getLastResponseTime();
    }

    assertEquals("Sum total operation count", totalOperations,
        serviceAccountantManager.getTotalOperationCount());
    assertEquals("Sum total unit count", totalUnits,
        serviceAccountantManager.getTotalUnitCount());
    assertEquals("Sum total time", totalTime,
        serviceAccountantManager.getTotalResponseTime());
  }

  /**
   * Tests that removing a service from the service account manager correctly
   * stops accounting it.
   */
  public void testRemoveService() throws com.google.api.adwords.v200909.cm.ApiException,
      RemoteException {
    long oldUnitCount = serviceAccountantManager.getTotalUnitCount();
    serviceAccountantManager.setAutoCreateAccountant(true);
    campaignV200909Service.get(new CampaignSelector(null, null, null, new Paging(0, 100)));
    assertTrue("Service was incorrectly accounted.",
        oldUnitCount < serviceAccountantManager.getTotalUnitCount());

    serviceAccountantManager.removeService(campaignV200909Service);
    assertNull(serviceAccountantManager.getServiceAccountant(campaignV200909Service));

    oldUnitCount = serviceAccountantManager.getTotalUnitCount();
    serviceAccountantManager.setAutoCreateAccountant(false);
    campaignV200909Service.get(new CampaignSelector(null, null, null, new Paging(0, 100)));
    assertEquals("Service was incorrectly accounted.",
        oldUnitCount, serviceAccountantManager.getTotalUnitCount());
  }

  /**
   * Performs a set of operations using the supplied services that should be
   * considered a black box for the purpose of this demo. This should cost
   * 4 units and 4 operations.
   *
   * @throws RemoteException if one of the API operations fails
   * @throws ApiException if one of the API operations fails
   */
  private void performV200909Operations() throws RemoteException, ApiException {
    // Create new campaign structure.
    com.google.api.adwords.v200909.cm.Campaign campaign =
        new com.google.api.adwords.v200909.cm.Campaign();
    campaign.setName("Campaign #" + System.currentTimeMillis());
    campaign.setStatus(com.google.api.adwords.v200909.cm.CampaignStatus.PAUSED);
    campaign.setBiddingStrategy(new com.google.api.adwords.v200909.cm.ManualCPC());

    com.google.api.adwords.v200909.cm.Budget budget =
        new com.google.api.adwords.v200909.cm.Budget(
            com.google.api.adwords.v200909.cm.BudgetBudgetPeriod.DAILY,
            new com.google.api.adwords.v200909.cm.Money("USD", 50000000L),
            com.google.api.adwords.v200909.cm.BudgetBudgetDeliveryMethod.STANDARD);
    campaign.setBudget(budget);

    com.google.api.adwords.v200909.cm.CampaignOperation campaignOperation =
          new com.google.api.adwords.v200909.cm.CampaignOperation();
    campaignOperation.setOperand(campaign);
    campaignOperation.setOperator(com.google.api.adwords.v200909.cm.Operator.ADD);

    // Add campaign.
    com.google.api.adwords.v200909.cm.CampaignReturnValue campaignResult =
        campaignV200909Service.mutate(
              new com.google.api.adwords.v200909.cm.CampaignOperation[] {campaignOperation});

    campaign = campaignResult.getValue()[0];

    com.google.api.adwords.v200909.cm.AdGroup adGroup =
        new com.google.api.adwords.v200909.cm.AdGroup();
    adGroup.setCampaignId(campaign.getId());
    adGroup.setName("Sample Ad Group");

    com.google.api.adwords.v200909.cm.ManualCPCAdGroupBids adGroupBids =
        new com.google.api.adwords.v200909.cm.ManualCPCAdGroupBids();
    adGroupBids.setKeywordMaxCpc(new com.google.api.adwords.v200909.cm.Bid(
        new com.google.api.adwords.v200909.cm.Money("USD", 10000000L)));

    adGroup.setBids(adGroupBids);
    adGroup.setStatus(com.google.api.adwords.v200909.cm.AdGroupStatus.ENABLED);

    com.google.api.adwords.v200909.cm.AdGroupOperation adGroupOperation =
        new com.google.api.adwords.v200909.cm.AdGroupOperation();
    adGroupOperation.setOperator(com.google.api.adwords.v200909.cm.Operator.ADD);
    adGroupOperation.setOperand(adGroup);

    // Add ad group.
    com.google.api.adwords.v200909.cm.AdGroupReturnValue adGroupResult =
        adGroupV200909Service.mutate(
            new com.google.api.adwords.v200909.cm.AdGroupOperation[]{adGroupOperation});

    adGroup = adGroupResult.getValue()[0];

    com.google.api.adwords.v200909.cm.TextAd textAd =
        new com.google.api.adwords.v200909.cm.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");

    com.google.api.adwords.v200909.cm.AdGroupAd adGroupAd =
        new com.google.api.adwords.v200909.cm.AdGroupAd();
    adGroupAd.setAd(textAd);
    adGroupAd.setAdGroupId(adGroup.getId());
    adGroupAd.setStatus(com.google.api.adwords.v200909.cm.AdGroupAdStatus.ENABLED);

    com.google.api.adwords.v200909.cm.AdGroupAdOperation adGroupAdOperation =
        new com.google.api.adwords.v200909.cm.AdGroupAdOperation();
    adGroupAdOperation.setOperator(com.google.api.adwords.v200909.cm.Operator.ADD);
    adGroupAdOperation.setOperand(adGroupAd);

    // Add text ads.
    com.google.api.adwords.v200909.cm.AdGroupAdReturnValue result =
        adGroupAdV200909Service.mutate(
            new com.google.api.adwords.v200909.cm.AdGroupAdOperation[] {adGroupAdOperation});

    com.google.api.adwords.v200909.cm.BiddableAdGroupCriterion adGroupCriterion =
        new com.google.api.adwords.v200909.cm.BiddableAdGroupCriterion();
    adGroupCriterion.setCriterion(new com.google.api.adwords.v200909.cm.Keyword(
        null, null, "mars cruise", com.google.api.adwords.v200909.cm.KeywordMatchType.BROAD));
    adGroupCriterion.setAdGroupId(adGroup.getId());

    com.google.api.adwords.v200909.cm.AdGroupCriterionOperation operation =
        new com.google.api.adwords.v200909.cm.AdGroupCriterionOperation();
    operation.setOperator(com.google.api.adwords.v200909.cm.Operator.ADD);
    operation.setOperand(adGroupCriterion);

    // Add keyword.
    com.google.api.adwords.v200909.cm.AdGroupCriterionReturnValue results =
        adGroupCriterionV200909Service.mutate(
            new com.google.api.adwords.v200909.cm.AdGroupCriterionOperation[] {operation});
  }
}
TOP

Related Classes of com.google.api.adwords.lib.ServiceAccountantTest

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.