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

docs(samples): add quickstart sample #302

Merged
merged 4 commits into from Jan 22, 2021
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
@@ -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]
@@ -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:");
}
}