This repository has been archived by the owner on Sep 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(samples): add quickstart sample (#302)
* docs(samples): add quickstart sample * nit * update license headers * update license headers
- Loading branch information
1 parent
8ec3351
commit 7199e0a
Showing
2 changed files
with
116 additions
and
0 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
samples/snippets/src/main/java/com/example/bigqueryreservation/QuickstartSample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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] |
65 changes: 65 additions & 0 deletions
65
samples/snippets/src/test/java/com/example/bigqueryreservation/QuickstartSampleIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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:"); | ||
} | ||
} |