GoogleAdsClient extends AbstractGoogleAdsClient { public static Builder newBuilder() { AutoValue_GoogleAdsClient.Builder clientBuilder = new AutoValue_GoogleAdsClient.Builder(); clientBuilder.setEnableGeneratedCatalog(true); InstantiatingGrpcChannelProvider transportChannelProvider = InstantiatingGrpcChannelProvider.newBuilder() .setInterceptorProvider( () -> ImmutableList.of( new LoggingInterceptor( new RequestLogger(), clientBuilder.getHeaders(), clientBuilder.getEndpoint()))) .setMaxInboundMetadataSize(16 * 1024 * 1024) .setMaxInboundMessageSize(64 * 1024 * 1024) .build(); clientBuilder .setEndpoint(DEFAULT_ENDPOINT) .setTransportChannelProvider(transportChannelProvider); return clientBuilder; }  static Builder newBuilder(); abstract Credentials getCredentials(); abstract String getDeveloperToken(); abstract String getEndpoint(); @Nullable abstract Long getLoginCustomerId(); @Nullable abstract Long getLinkedCustomerId(); abstract Builder toBuilder();  }
@Test public void buildFromPropertiesFile_readsFromPropertiesFile() throws IOException { File propertiesFile = folder.newFile("ads.properties"); try (FileWriter propertiesFileWriter = new FileWriter(propertiesFile)) { testProperties.store(propertiesFileWriter, null); } Builder builder = GoogleAdsClient.newBuilder(); builder.setConfigurationFileSupplier(() -> propertiesFile); GoogleAdsClient client = builder.fromPropertiesFile().build(); assertGoogleAdsClient(client); }
@Test public void buildFromPropertiesFile_readsAllProperties() throws IOException { File propertiesFile = folder.newFile("ads.properties"); try (FileWriter propertiesFileWriter = new FileWriter(propertiesFile)) { testProperties.store(propertiesFileWriter, null); } GoogleAdsClient client = GoogleAdsClient.newBuilder().fromPropertiesFile(propertiesFile).build(); assertGoogleAdsClient(client); }
@Test public void testBuildFromPropertiesFile_withoutLoginCustomerId() throws IOException { File propertiesFile = folder.newFile("ads.properties"); testProperties.remove(ConfigPropertyKey.LOGIN_CUSTOMER_ID.getPropertyKey()); try (FileWriter propertiesFileWriter = new FileWriter(propertiesFile)) { testProperties.store(propertiesFileWriter, null); } GoogleAdsClient client = GoogleAdsClient.newBuilder() .fromPropertiesFile(propertiesFile) .setTransportChannelProvider(localChannelProvider) .build(); assertGoogleAdsClient(client, null, enabledGeneratedCatalog); }
@Test public void buildFromPropertiesFile_invalidFilePath_throwsException() throws IOException { File nonExistentFile = new File(folder.getRoot(), "I_dont_exist.properties"); thrown.expect(FileNotFoundException.class); thrown.expectMessage(nonExistentFile.getName()); GoogleAdsClient.newBuilder().fromPropertiesFile(nonExistentFile); }
@Test public void buildWithoutPropertiesFile_supportsAllFields() throws IOException { Credentials credentials = UserCredentials.newBuilder() .setClientId(CLIENT_ID) .setClientSecret(CLIENT_SECRET) .setRefreshToken(REFRESH_TOKEN) .build(); GoogleAdsClient client = GoogleAdsClient.newBuilder() .setCredentials(credentials) .setDeveloperToken(DEVELOPER_TOKEN) .setLoginCustomerId(LOGIN_CUSTOMER_ID) .setEnableGeneratedCatalog(enabledGeneratedCatalog) .setTransportChannelProvider(localChannelProvider) .build(); assertGoogleAdsClient(client); }
@Test public void build_enableGeneratedCatalog_not_required() throws IOException { Credentials credentials = UserCredentials.newBuilder() .setClientId(CLIENT_ID) .setClientSecret(CLIENT_SECRET) .setRefreshToken(REFRESH_TOKEN) .build(); GoogleAdsClient client = GoogleAdsClient.newBuilder() .setCredentials(credentials) .setDeveloperToken(DEVELOPER_TOKEN) .setLoginCustomerId(LOGIN_CUSTOMER_ID) .build(); assertGoogleAdsClient(client, LOGIN_CUSTOMER_ID, true); }
@Test public void x_goog_api_client_header_isSent() { GoogleAdsClient client = GoogleAdsClient.newBuilder() .setCredentials(fakeCredentials) .setDeveloperToken(DEVELOPER_TOKEN) .setLoginCustomerId(LOGIN_CUSTOMER_ID) .setEnableGeneratedCatalog(enabledGeneratedCatalog) .setTransportChannelProvider(localChannelProvider) .setEndpoint("fake-address") .build(); mockService.addResponse(SearchGoogleAdsResponse.newBuilder().build()); try (GoogleAdsServiceClient googleAdsClient = client.getLatestVersion().createGoogleAdsServiceClient()) { googleAdsClient.search("123", "select blah"); } assertTrue( "GAX/GRPC/Java platform headers missing", localChannelProvider.isHeaderSent( ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern())); }
@Test public void loginCustomerId_sentIfSpecified() { GoogleAdsClient client = GoogleAdsClient.newBuilder() .setCredentials(fakeCredentials) .setDeveloperToken(DEVELOPER_TOKEN) .setLoginCustomerId(LOGIN_CUSTOMER_ID) .setEnableGeneratedCatalog(enabledGeneratedCatalog) .setTransportChannelProvider(localChannelProvider) .setEndpoint("fake-address") .build(); mockService.addResponse(SearchGoogleAdsResponse.newBuilder().build()); try (GoogleAdsServiceClient googleAdsServiceClient = client.getLatestVersion().createGoogleAdsServiceClient()) { googleAdsServiceClient.search("123", "select blah"); } assertTrue( "login customer ID not found", localChannelProvider.isHeaderSent( "login-customer-id", Pattern.compile(String.valueOf(LOGIN_CUSTOMER_ID)))); }
@Test public void loginCustomerId_notSentIfExcluded() { GoogleAdsClient client = GoogleAdsClient.newBuilder() .setCredentials(fakeCredentials) .setDeveloperToken(DEVELOPER_TOKEN) .setTransportChannelProvider(localChannelProvider) .setEnableGeneratedCatalog(enabledGeneratedCatalog) .build(); mockService.addResponse(SearchGoogleAdsResponse.newBuilder().build()); try (GoogleAdsServiceClient googleAdsServiceClient = client.getLatestVersion().createGoogleAdsServiceClient()) { googleAdsServiceClient.search("123", "select blah"); } assertFalse( "login customer ID header should be excluded if not configured", localChannelProvider.isHeaderSent("login-customer-id", Pattern.compile(".*"))); }
@Test public void linkedCustomerId_sentIfSpecified() { GoogleAdsClient client = GoogleAdsClient.newBuilder() .setCredentials(fakeCredentials) .setDeveloperToken(DEVELOPER_TOKEN) .setLinkedCustomerId(LINKED_CUSTOMER_ID) .setEnableGeneratedCatalog(enabledGeneratedCatalog) .setTransportChannelProvider(localChannelProvider) .setEndpoint("fake-address") .build(); mockService.addResponse(SearchGoogleAdsResponse.newBuilder().build()); try (GoogleAdsServiceClient googleAdsServiceClient = client.getLatestVersion().createGoogleAdsServiceClient()) { googleAdsServiceClient.search("123", "select blah"); } assertTrue( "linked customer ID not found", localChannelProvider.isHeaderSent( "linked-customer-id", Pattern.compile(String.valueOf(LINKED_CUSTOMER_ID)))); }
@Test public void exceptionTransformedToGoogleAdsException() { GoogleAdsClient client = GoogleAdsClient.newBuilder() .setCredentials(fakeCredentials) .setDeveloperToken(DEVELOPER_TOKEN) .setTransportChannelProvider(localChannelProvider) .setEnableGeneratedCatalog(enabledGeneratedCatalog) .build(); Metadata.Key trailerKey = ApiCatalog.getDefault().getLatestVersion().getExceptionFactory().getTrailerKey(); Metadata trailers = new Metadata(); GoogleAdsFailure.Builder failure = GoogleAdsFailure.newBuilder(); failure.addErrors(GoogleAdsError.newBuilder().setMessage("Test error message")); trailers.put(trailerKey, failure.build().toByteArray()); StatusException rootCause = new StatusException(Status.UNKNOWN, trailers); mockService.addException(new ApiException(rootCause, GrpcStatusCode.of(Code.UNKNOWN), false)); try (GoogleAdsServiceClient googleAdsServiceClient = client.getLatestVersion().createGoogleAdsServiceClient()) { googleAdsServiceClient.search("123", "select blah"); } catch (GoogleAdsException ex) { } }
@Test public void hashCode_doesNotCollide() { GoogleAdsClient clientA = GoogleAdsClient.newBuilder() .setCredentials(fakeCredentials) .setDeveloperToken(DEVELOPER_TOKEN) .setEnableGeneratedCatalog(enabledGeneratedCatalog) .build(); GoogleAdsClient clientB = GoogleAdsClient.newBuilder() .setCredentials(fakeCredentials) .setDeveloperToken(DEVELOPER_TOKEN + "asdf") .setEnableGeneratedCatalog(enabledGeneratedCatalog) .build(); assertNotEquals( "Clients with distinct params should have distinct hashCodes", clientA.hashCode(), clientB.hashCode()); }
@Test public void equals_equalIfSameInstance() { GoogleAdsClient client = GoogleAdsClient.newBuilder() .setCredentials(fakeCredentials) .setDeveloperToken(DEVELOPER_TOKEN) .setEnableGeneratedCatalog(enabledGeneratedCatalog) .build(); assertEquals("same instance should be equal", client, client); }
@Test public void equals_notEqualIfDifferentInstance() { GoogleAdsClient clientA = GoogleAdsClient.newBuilder() .setCredentials(fakeCredentials) .setDeveloperToken(DEVELOPER_TOKEN) .setEnableGeneratedCatalog(enabledGeneratedCatalog) .build(); GoogleAdsClient clientB = GoogleAdsClient.newBuilder() .setCredentials(fakeCredentials) .setDeveloperToken(DEVELOPER_TOKEN) .setEnableGeneratedCatalog(enabledGeneratedCatalog) .build(); assertNotEquals("different instances should not be equal", clientA, clientB); }
@Test public void toString_returnsNotNull() { GoogleAdsClient client = GoogleAdsClient.newBuilder() .setCredentials(fakeCredentials) .setDeveloperToken(DEVELOPER_TOKEN) .setEnableGeneratedCatalog(enabledGeneratedCatalog) .build(); String toString = client.toString(); assertNotNull("toString should return a non-null string", toString); assertFalse("toString should return a non-empty string", toString.isEmpty()); }