Skip to content
Permalink
Browse files
docs(samples): add quickstart sample (#302)
* docs(samples): add quickstart sample

* nit

* update license headers

* update license headers
  • Loading branch information
stephaniewang526 committed Jan 22, 2021
1 parent 8ec3351 commit 7199e0a2d7f7f8b5d03a21fbae96ee7f8828c750
@@ -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:");
}
}

0 comments on commit 7199e0a

Please sign in to comment.