From 7199e0a2d7f7f8b5d03a21fbae96ee7f8828c750 Mon Sep 17 00:00:00 2001 From: Stephanie Wang Date: Fri, 22 Jan 2021 14:06:11 -0500 Subject: [PATCH] docs(samples): add quickstart sample (#302) * docs(samples): add quickstart sample * nit * update license headers * update license headers --- .../bigqueryreservation/QuickstartSample.java | 51 +++++++++++++++ .../QuickstartSampleIT.java | 65 +++++++++++++++++++ 2 files changed, 116 insertions(+) create mode 100644 samples/snippets/src/main/java/com/example/bigqueryreservation/QuickstartSample.java create mode 100644 samples/snippets/src/test/java/com/example/bigqueryreservation/QuickstartSampleIT.java diff --git a/samples/snippets/src/main/java/com/example/bigqueryreservation/QuickstartSample.java b/samples/snippets/src/main/java/com/example/bigqueryreservation/QuickstartSample.java new file mode 100644 index 00000000..d2b1df52 --- /dev/null +++ b/samples/snippets/src/main/java/com/example/bigqueryreservation/QuickstartSample.java @@ -0,0 +1,51 @@ +/* + * Copyright 2021 Google LLC + * + * 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.bigqueryreservation; + +// [START bigqueryreservation_quickstart] +import com.google.cloud.bigquery.reservation.v1.ReservationServiceClient; +import java.io.IOException; + +public class QuickstartSample { + + public static void main(String... args) throws Exception { + // TODO(developer): Replace these variables before running the sample. + String projectId = "YOUR_PROJECT_ID"; + String location = "LOCATION"; + quickStartSample(projectId, location); + } + + public static void quickStartSample(String projectId, String location) throws IOException { + try (ReservationServiceClient client = ReservationServiceClient.create()) { + // list reservations in the project + String parent = String.format("projects/%s/locations/%s", projectId, location); + client + .listReservations(parent) + .iterateAll() + .forEach(res -> System.out.println("Reservation resource name: " + res.getName())); + + // list capacity commitments in the project + client + .listCapacityCommitments(parent) + .iterateAll() + .forEach( + commitment -> + System.out.println("Capacity commitment resource name: " + commitment.getName())); + } + } +} +// [END bigqueryreservation_quickstart] diff --git a/samples/snippets/src/test/java/com/example/bigqueryreservation/QuickstartSampleIT.java b/samples/snippets/src/test/java/com/example/bigqueryreservation/QuickstartSampleIT.java new file mode 100644 index 00000000..605477ad --- /dev/null +++ b/samples/snippets/src/test/java/com/example/bigqueryreservation/QuickstartSampleIT.java @@ -0,0 +1,65 @@ +/* + * Copyright 2021 Google LLC + * + * 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.bigqueryreservation; + +import static com.google.common.truth.Truth.assertThat; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.util.logging.Level; +import java.util.logging.Logger; +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 Logger LOG = Logger.getLogger(QuickstartSampleIT.class.getName()); + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); + + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @After + public void tearDown() { + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + LOG.log(Level.INFO, bout.toString()); + } + + @Test + public void testQuickstart() throws Exception { + QuickstartSample.quickStartSample(PROJECT_ID, "US"); + String got = bout.toString(); + assertThat(got).contains("resource name:"); + } +}