// 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.utils;
import com.google.api.adwords.lib.AdWordsUser;
import com.google.api.adwords.lib.ReportDate;
import junit.framework.TestCase;
import java.io.IOException;
import java.rmi.RemoteException;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.xml.rpc.ServiceException;
/**
* Tests the {@link UnitsUtils} utility class. For all tests, the credentials
* for this test will be pulled from "test_data/test.properties". The tests
* assume that the email field in the credentials is set to the root sandbox
* user "i.e. email@gmail.com."
*
* @author api.arogal@gmail.com (Adam Rogal)
*/
public class UnitsUtilsTest extends TestCase {
private AdWordsUser testUser;
private Date endDate;
private Date startDate;
private List<String[]> methods;
/**
* See {@link junit.framework.TestCase#setUp()}.
*/
@Override
protected void setUp() throws Exception {
super.setUp();
testUser = new AdWordsUser("test_data/test.properties");
endDate = new ReportDate().toCalendar().getTime();
startDate = new ReportDate(
new ReportDate().getYear(), new ReportDate().getMonth(), 1).toCalendar().getTime();
methods = CsvUtils.getCsvDataArray("data/ops_rates.csv", true);
}
/**
* Tests that all methods pulled from "data/ops_rates.csv" can be queried
* correctly. As this is using the sandbox, all results should be 0.
*/
public void testGetMethodUsage() throws Exception {
Map<String, Long> methodUsage = new UnitsUtils(testUser).getMethodUsage(startDate, endDate);
for (String[] method : methods) {
String key = method[1] + "." + method[2];
assertTrue("Could not find " + key + " in results.", methodUsage.containsKey(key));
}
}
/**
* Tests that all clients in the sandbox are included in the resulting
* usage map.
*/
public void testGetClientUnitsUsage() throws Exception {
testUser = testUser.generateClientAdWordsUser(null);
UnitsUsageMap usageMap = new UnitsUtils(testUser).getClientUnitsUsage(startDate, endDate);
assertEquals(testUser.getEffectiveUserId(), usageMap.getRootAccount());
assertEquals(Collections.EMPTY_LIST, usageMap.getDoubleCountedChildren());
for (int i = 1; i < 6; i++) {
String clientEmail = "client_" + i + "+" + testUser.getEmail();
assertTrue("Could not find " + clientEmail + " in results.",
usageMap.getUnitsUsageMap().containsKey(clientEmail));
}
}
}