Skip to content
This repository has been archived by the owner on Sep 27, 2023. It is now read-only.

docs(samples): migrate samples into client #177

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 10 additions & 1 deletion pom.xml
Expand Up @@ -184,10 +184,19 @@
<link>https://developers.google.com/protocol-buffers/docs/reference/java/</link>
<link>https://googleapis.dev/java/google-auth-library/latest/</link>
<link>https://googleapis.dev/java/gax/latest/</link>
<link>https://googleapis.github.io/api-common-java/${google.api-common.version}/apidocs/</link>
<link>https://googleapis.github.io/api-common-java/</link>
</links>
</configuration>
</plugin>
</plugins>
</reporting>

<profiles>
<profile>
<id>include-samples</id>
<modules>
<module>samples</module>
</modules>
</profile>
</profiles>
</project>
1 change: 1 addition & 0 deletions samples/snippets/pom.xml
Expand Up @@ -44,6 +44,7 @@
</dependency>
<!-- [END bigquerydatatransfer_install_with_bom] -->

<!-- Test dependencies -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
@@ -0,0 +1,59 @@
/*
* Copyright 2018 Google Inc.
*
* 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.example.bigquerydatatransfer;

// [START bigquerydatatransfer_quickstart]
// Imports the Google Cloud client library

import com.google.cloud.bigquery.datatransfer.v1.DataSource;
import com.google.cloud.bigquery.datatransfer.v1.DataTransferServiceClient;
import com.google.cloud.bigquery.datatransfer.v1.DataTransferServiceClient.ListDataSourcesPagedResponse;
import com.google.cloud.bigquery.datatransfer.v1.ListDataSourcesRequest;

public class QuickstartSample {
/**
* List available data sources for the BigQuery Data Transfer service.
*/
public static void main(String... args) throws Exception {
// Sets your Google Cloud Platform project ID.
// String projectId = "YOUR_PROJECT_ID";
String projectId = args[0];

// Instantiate a client. If you don't specify credentials when constructing a client, the
// client library will look for credentials in the environment, such as the
// GOOGLE_APPLICATION_CREDENTIALS environment variable.
try (DataTransferServiceClient client = DataTransferServiceClient.create()) {
// Request the list of available data sources.
String parent = String.format("projects/%s", projectId);
ListDataSourcesRequest request =
ListDataSourcesRequest.newBuilder()
.setParent(parent)
.build();
ListDataSourcesPagedResponse response = client.listDataSources(request);

// Print the results.
System.out.println("Supported Data Sources:");
for (DataSource dataSource : response.iterateAll()) {
System.out.println(dataSource.getDisplayName());
System.out.printf("\tID: %s%n", dataSource.getDataSourceId());
System.out.printf("\tFull path: %s%n", dataSource.getName());
System.out.printf("\tDescription: %s%n", dataSource.getDescription());
}
}
}
}
// [END bigquerydatatransfer_quickstart]
@@ -0,0 +1,56 @@
/*
* Copyright 2018 Google Inc.
*
* 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.example.bigquerydatatransfer;

import static com.google.common.truth.Truth.assertThat;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/** Tests for quickstart sample. */
@RunWith(JUnit4.class)
@SuppressWarnings("checkstyle:abbreviationaswordinname")
public class QuickstartSampleIT {
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");

private ByteArrayOutputStream bout;
private PrintStream out;

@Before
public void setUp() {
bout = new ByteArrayOutputStream();
out = new PrintStream(bout);
System.setOut(out);
}

@After
public void tearDown() {
System.setOut(null);
}

@Test
public void testQuickstart() throws Exception {
QuickstartSample.main(PROJECT_ID);
String got = bout.toString();
assertThat(got).contains("Supported Data Sources:");
}
}