Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,7 @@ public static Builder newBuilder() {
clientBuilder
.setEndpoint(DEFAULT_ENDPOINT)
.setTransportChannelProvider(transportChannelProvider)
.setDefaultTransportChannelProvider(transportChannelProvider)
.setUseCloudOrgForApiAccess(false);
.setDefaultTransportChannelProvider(transportChannelProvider);
return clientBuilder;
}

Expand All @@ -124,12 +123,6 @@ public static Builder newBuilder() {
@Nullable
public abstract String getDeveloperToken();

/**
* Whether to use the Google Cloud Organization of your Google Cloud project instead of developer
* token to determine your Google Ads API access level.
*/
abstract boolean isUseCloudOrgForApiAccess();

/** Returns the endpoint to use. Defaults to DEFAULT_ENDPOINT. */
public abstract String getEndpoint();

Expand Down Expand Up @@ -407,10 +400,11 @@ abstract Builder setDefaultTransportChannelProvider(
TransportChannelProvider transportChannelProvider);

/** Returns the developer token currently configured. */
@Nullable
public abstract String getDeveloperToken();

/** Sets the developer token used to obtain access to the Google Ads API. */
public abstract Builder setDeveloperToken(String developerToken);
public abstract Builder setDeveloperToken(@Nullable String developerToken);

/**
* Sets the developer token from the specified {@code properties} if an entry for developer
Expand All @@ -423,18 +417,6 @@ private void setDeveloperToken(Properties properties) {
}
}

/**
* Gets whether to use the Google Cloud Organization of your Google Cloud project instead of
* developer token to determine your Google Ads API access level.
*/
public abstract boolean isUseCloudOrgForApiAccess();

/**
* Specifies whether to use the Google Cloud Organization of your Google Cloud project instead
* of developer token to determine your Google Ads API access level.
*/
public abstract Builder setUseCloudOrgForApiAccess(boolean useCloudOrgForApiAccess);

/** Returns the login customer ID currently configured. */
public abstract Long getLoginCustomerId();

Expand Down Expand Up @@ -657,24 +639,6 @@ public GoogleAdsClient build() {
// action is needed.
}

// Verifies that the client will use exactly one of dev token or Cloud org for API access.
final String errorSuffix =
"You must set either the developer token or set use Cloud org for API access to true, but"
+ " not both.";
if (getDeveloperToken() == null) {
// If dev token is null, verifies that the client is using Cloud org for API access.
Preconditions.checkState(
isUseCloudOrgForApiAccess(),
"Developer token is null but not using cloud org for API access. %s",
errorSuffix);
} else {
// If dev token is not null, verifies that the client is not using Cloud org for API access.
Preconditions.checkState(
!isUseCloudOrgForApiAccess(),
"Developer token is not null but using Cloud org for API access. %s",
errorSuffix);
}

// Provides the credentials to the primer to preemptively get these ready for usage.
Primer.getInstance().ifPresent(p -> p.primeCredentialsAsync(getCredentials()));
// Proceeds with creating the client library instance.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.google.auto.value.AutoValue;
import com.google.auto.value.extension.memoized.Memoized;
import com.google.common.base.Joiner;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableMap;
import com.google.protobuf.Message;
import java.io.IOException;
Expand Down Expand Up @@ -64,7 +65,7 @@ public static Builder newBuilder() {
@Memoized
public ImmutableMap<String, String> getHeaders() {
Map<String, String> headers = new HashMap<>();
if (getDeveloperToken() != null) {
if (!Strings.isNullOrEmpty(getDeveloperToken())) {
headers.put("developer-token", getDeveloperToken());
}
if (getLoginCustomerId() != null) {
Expand Down Expand Up @@ -134,7 +135,7 @@ private static String getProtobufVersion() {
public abstract static class Builder {

/** Sets the developer token. */
public abstract Builder setDeveloperToken(String developerToken);
public abstract Builder setDeveloperToken(@Nullable String developerToken);

/** Sets the login customer ID. */
public abstract Builder setLoginCustomerId(Long loginCustomerId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,7 @@ public void buildFromPropertiesFile_readsAllProperties() throws IOException {
* token property.
*/
@Test
public void testBuildFromPropertiesFile_withoutDeveloperToken_withUseCloudOrgForApiAccess()
throws IOException {
public void testBuildFromPropertiesFile_withoutDeveloperToken() throws IOException {
// Create a properties file in the temporary folder.
File propertiesFile = folder.newFile("ads.properties");
// Remove the developer token property.
Expand All @@ -190,41 +189,42 @@ public void testBuildFromPropertiesFile_withoutDeveloperToken_withUseCloudOrgFor
}

// Build a new client from the file.
GoogleAdsClient client =
GoogleAdsClient.newBuilder().fromPropertiesFile(propertiesFile).build();
assertGoogleAdsClient(client, LOGIN_CUSTOMER_ID, true);
assertNull(client.getDeveloperToken());
}

/** Tests building a client without a developer token. */
@Test
public void testBuild_withoutDeveloperToken() {
GoogleAdsClient client =
GoogleAdsClient.newBuilder()
.fromPropertiesFile(propertiesFile)
.setUseCloudOrgForApiAccess(true)
.fromProperties(testProperties)
.setDeveloperToken(null)
.build();
assertGoogleAdsClient(client, LOGIN_CUSTOMER_ID, true);
assertGoogleAdsClient(client, true);
assertNull(client.getDeveloperToken());
Comment thread
Raibaz marked this conversation as resolved.
}

/** Tests that clients can only use exactly one of dev token or Cloud org for API access. */
/** Tests that developer token is not included in headers when it is null. */
@Test
public void testDevTokenAndUseCloudOrgForApiAccessAreExclusive_failIfBothOrNeither() {
GoogleAdsClient.Builder builder = GoogleAdsClient.newBuilder().fromProperties(testProperties);
// Confirms developer token is set on the builder.
assertNotNull("dev token not set from test properties", builder.getDeveloperToken());
// Opts into using Cloud org for API access.
builder.setUseCloudOrgForApiAccess(true);
// Confirms build() fails.
Throwable exception =
assertThrows(
"Should fail when both dev token is set and using Cloud org for API access",
IllegalStateException.class,
() -> builder.build());
// Checks the exception message.
assertThat(exception.getMessage(), Matchers.containsString("not both"));

// Clears dev token and opts out of using Cloud org for API access.
builder.setDeveloperToken(null).setUseCloudOrgForApiAccess(false);
// Confirms build() fails.
exception =
assertThrows(
"Should fail when neither dev token is set nor using Cloud org for API access",
IllegalStateException.class,
() -> builder.build());
// Checks the exception message.
assertThat(exception.getMessage(), Matchers.containsString("not both"));
public void testNullDeveloperTokenNotAppearInHeaders() {
GoogleAdsClient.Builder builder =
GoogleAdsClient.newBuilder()
.fromProperties(testProperties)
.setDeveloperToken(null);
assertFalse(builder.getHeaders().containsKey("developer-token"));
}

/** Tests that developer token is not included in headers when it is empty. */
@Test
public void testEmptyDeveloperTokenNotAppearInHeaders() {
GoogleAdsClient.Builder builder =
GoogleAdsClient.newBuilder()
.fromProperties(testProperties)
.setDeveloperToken("");
assertFalse(builder.getHeaders().containsKey("developer-token"));
}

/**
Expand Down Expand Up @@ -1063,10 +1063,7 @@ private void assertGoogleAdsClient(
channelProvider.toBuilder().getMaxInboundMessageSize());
}

if (client.getDeveloperToken() == null) {
assertTrue(
"Developer token is null but use cloud org is false", client.isUseCloudOrgForApiAccess());
} else {
if (client.getDeveloperToken() != null) {
assertEquals("developer token", DEVELOPER_TOKEN, client.getDeveloperToken());
}
assertEquals("Login customer id", loginCustomerId, client.getLoginCustomerId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ public void developerTokenOptional() {
assertFalse(provider.getHeaders().containsKey("developer-token"));
}

/** Verifies that the developer token is not present in headers when empty. */
@Test
public void developerTokenEmpty_notIncluded() {
GoogleAdsHeaderProvider provider =
GoogleAdsHeaderProvider.newBuilder().setDeveloperToken("").build();
assertEquals("", provider.getDeveloperToken());
assertFalse(provider.getHeaders().containsKey("developer-token"));
}

/** Verifies that the developer token is set and present in the headers when provided. */
@Test
public void developerToken_includesIfSet() {
Expand Down