8000 Writing quickstart + test for Bigtable (#1431) · rschoultz/java-docs-samples@1a491c8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1a491c8

Browse files
Writing quickstart + test for Bigtable (GoogleCloudPlatform#1431)
* Setup mvn package and copy in existing samples and tests * Fix style issues and ran tests * Writing quickstart + test for Bigtable * Updating pom with current versions * Adding default values for the system propertiesˆ * surefire instead of failsafe * setup table for testing using cbt tool, retriggering test * Removing unnecessary code from quickstart test that was causing failures * cleaning up quickstart * Changing test variables to use GOOGLE_CLOUD_PROJECT and the instance environment variable
1 parent 01edb36 commit 1a491c8

File tree

7 files changed

+171
-39
lines changed

7 files changed

+171
-39
lines changed

.kokoro/tests/run_tests.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ if [[ "$SCRIPT_DEBUG" != "true" ]]; then
5656
source "${KOKORO_GFILE_DIR}/aws-secrets.sh"
5757
source "${KOKORO_GFILE_DIR}/storage-hmac-credentials.sh"
5858
source "${KOKORO_GFILE_DIR}/dlp_secrets.txt"
59+
source "${KOKORO_GFILE_DIR}/bigtable_secrets.txt"
5960
# Activate service account
6061
gcloud auth activate-service-account \
6162
--key-file="$GOOGLE_APPLICATION_CREDENTIALS" \

bigtable/pom.xml

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -98,17 +98,6 @@
9898
<artifactId>maven-project-info-reports-plugin</artifactId>
9999
<version>3.0.0</version>
100100
</plugin>
101-
<plugin>
102-
<groupId>org.apache.maven.plugins</groupId>
103-
<artifactId>maven-surefire-plugin</artifactId>
104-
<version>3.0.0-M3</version>
105-
<configuration>
106-
<systemPropertyVariables>
107-
<bigtable.project>java-docs-samples-testing</bigtable.project>
108-
<bigtable.instance>instance</bigtable.instance>
109-
</systemPropertyVariables>
110-
</configuration>
111-
</plugin>
112101
</plugins>
113102
</pluginManagement>
114103
</build>
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright 2019 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.m.examples.bigtable;
18+
19+
// START [bigtable_quickstart_veneer]
20+
21+
import com.google.api.gax.rpc.NotFoundException;
22+
import com.google.cloud.bigtable.data.v2.BigtableDataClient;
23+
import com.google.cloud.bigtable.data.v2.BigtableDataSettings;
24+
import com.google.cloud.bigtable.data.v2.models.Row;
25+
import com.google.cloud.bigtable.data.v2.models.RowCell;
26+
27+
public class Quickstart {
28+
29+
public static void quickstart(String projectId, String instanceId, String tableId) {
30+
// String projectId = "my-project-id";
31+
// String instanceId = "my-instance-id";
32+
// String tableId = "my-table-id";
33+
34+
BigtableDataSettings settings =
35+
BigtableDataSettings.newBuilder().setProjectId(projectId).setInstanceId(instanceId).build();
36+
37+
// Initialize client that will be used to send requests. This client only needs to be created
38+
// once, and can be reused for multiple requests. After completing all of your requests, call
39+
// the "close" method on the client to safely clean up any remaining background resources.
40+
try (BigtableDataClient dataClient = BigtableDataClient.create(settings)) {
41+
try {
42+
System.out.println("\nReading a single row by row key");
43+
Row row = dataClient.readRow(tableId, "r1");
44+
System.out.println("Row: " + row.getKey().toStringUtf8());
45+
for (RowCell cell : row.getCells()) {
46+
System.out.printf(
47+
"Family: %s Qualifier: %s Value: %s%n",
48+
cell.getFamily(), cell.getQualifier().toStringUtf8(), cell.getValue().toStringUtf8());
49+
}
50+
} catch (NotFoundException e) {
51+
System.err.println("Failed to read from a non-existent table: " + e.getMessage());
52+
}
53+
54+
} catch (Exception e) {
55+
System.out.println("Error during functionName: \n" + e.toString());
56+
}
57+
}
58+
}
59+
// END [bigtable_quickstart_veneer]

bigtable/src/test/java/com/m/examples/bigtable/HelloWorldTest.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@
4040
/** Integration tests for {@link HelloWorld} */
4141
public class HelloWorldTest {
4242

43-
private static final String PROJECT_PROPERTY_NAME = "bigtable.project";
44-
private static final String INSTANCE_PROPERTY_NAME = "bigtable.instance";
43+
private static final String INSTANCE_PROPERTY_NAME = "BIGTABLE_TESTING_INSTANCE";
4544
private static final String TABLE_PREFIX = "table";
4645
private static String tableId;
4746
private static BigtableDataClient dataClient;
@@ -50,10 +49,17 @@ public class HelloWorldTest {
5049
private static String instanceId;
5150
private HelloWorld helloWorld;
5251

52+
private static String requireEnv(String varName) {
53+
assertNotNull(
54+
System.getenv(varName),
55+
"System property '%s' is required to perform these tests.".format(varName));
56+
return System.getenv(varName);
57+
}
58+
5359
@BeforeClass
5460
public static void beforeClass() throws IOException {
55-
projectId = System.getProperty(PROJECT_PROPERTY_NAME);
56-
instanceId = System.getProperty(INSTANCE_PROPERTY_NAME);
61+
projectId = requireEnv("GOOGLE_CLOUD_PROJECT");
62+
instanceId = requireEnv(INSTANCE_PROPERTY_NAME);
5763
if (projectId == null || instanceId == null) {
5864
dataClient = null;
5965
adminClient = null;
@@ -79,13 +85,6 @@ public static void afterClass() throws Exception {
7985

8086
@Before
8187
public void setup() throws IOException {
82-
if (adminClient == null || dataClient == null) {
83-
throw new AssumptionViolatedException(
84-
PROJECT_PROPERTY_NAME
85-
+ " or "
86-
+ INSTANCE_PROPERTY_NAME
87-
+ " property is not set, skipping integration tests.");
88-
}
8988
tableId = generateTableId();
9089
helloWorld = new HelloWorld(projectId, instanceId, tableId);
9190
adminClient.createTable(CreateTableRequest.of(tableId).addFamily("cf1"));

bigtable/src/test/java/com/m/examples/bigtable/InstanceAdminExampleTest.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
/** Integration tests for {@link InstanceAdminExample} */
4343
public class InstanceAdminExampleTest {
4444

45-
private static final String PROJECT_PROPERTY_NAME = "bigtable.project";
4645
private static final String ID_PREFIX = "instanceadmin";
4746
private static final String CLUSTER = "cluster";
4847
private static String projectId;
@@ -51,9 +50,16 @@ public class InstanceAdminExampleTest {
5150
private String instanceId;
5251
private InstanceAdminExample instanceAdmin;
5352

53+
private static String requireEnv(String varName) {
54+
assertNotNull(
55+
System.getenv(varName),
56+
"System property '%s' is required to perform these tests.".format(varName));
57+
return System.getenv(varName);
58+
}
59+
5460
@BeforeClass
5561
public static void beforeClass() throws IOException {
56-
projectId = System.getProperty(PROJECT_PROPERTY_NAME);
62+
projectId = requireEnv("GOOGLE_CLOUD_PROJECT");
5763
if (projectId == null) {
5864
adminClient = null;
5965
return;
@@ -71,10 +77,6 @@ public static void afterClass() {
7177

7278
@Before
7379
public void setup() throws IOException {
74-
if (adminClient == null) {
75-
throw new AssumptionViolatedException(
76-
PROJECT_PROPERTY_NAME + " property is not set, skipping integration tests.");
77-
}
7880
instanceId = generateId();
7981
clusterId = generateId();
8082
instanceAdmin = new InstanceAdminExample(projectId, instanceId, clusterId);
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright 2019 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.m.examples.bigtable;
18+
19+
import static org.junit.Assert.assertNotNull;
20+
import static org.junit.Assert.assertThat;
21+
22+
import com.google.cloud.bigtable.admin.v2.BigtableTableAdminClient;
23+
import com.google.cloud.bigtable.admin.v2.BigtableTableAdminSettings;
24+
import com.google.cloud.bigtable.data.v2.BigtableDataClient;
25+
import com.google.cloud.bigtable.data.v2.BigtableDataSettings;
26+
import java.io.ByteArrayOutputStream;
27+
import java.io.IOException;
28+
import java.io.PrintStream;
29+
import org.hamcrest.CoreMatchers;
30+
import org.junit.After;
31+
import org.junit.AfterClass;
32+
import org.junit.AssumptionViolatedException;
33+
import org.junit.Before;
34+
import org.junit.BeforeClass;
35+
import org.junit.Test;
36+
37+
/**
38+
* Integration tests for {@link Quickstart}
39+
*/
40+
public class QuickstartTest {
41+
42+
private static final String INSTANCE_PROPERTY_NAME = "BIGTABLE_TESTING_INSTANCE";
43+
private static final String TABLE_ID = "quickstart-table";
44+
private static String projectId;
45+
private static String instanceId;
46+
private ByteArrayOutputStream bout;
47+
48+
private static String requireEnv(String varName) {
49+
assertNotNull(
50+
System.getenv(varName),
51+
"System property '%s' is required to perform these tests.".format(varName));
52+
return System.getenv(varName);
53+
}
54+
55+
@BeforeClass
56+
public static void beforeClass() {
57+
projectId = requireEnv("GOOGLE_CLOUD_PROJECT");
58+
instanceId = requireEnv(INSTANCE_PROPERTY_NAME);
59+
}
60+
61+
@Before
62+
public void setupStream() {
63+
bout = new ByteArrayOutputStream();
64+
System.setOut(new PrintStream(bout));
65+
}
66+
67+
@Test
68+
public void testQuickstart() {
69+
try {
70+
Quickstart.quickstart(projectId, instanceId, TABLE_ID);
71+
} catch (Exception e) {
72+
System.out.println("Failed to run quickstart.");
73+
System.out.println(e);
74+
}
75+
76+
String output = bout.toString();
77+
assertThat(output, CoreMatchers.containsString("Reading a single row by row key"));
78+
assertThat(output, CoreMatchers.containsString("Row: r1"));
79+
assertThat(
80+
output, CoreMatchers.containsString("Family: cf1 Qualifier: c1 Value: quickstart"));
81+
}
82+
}

bigtable/src/test/java/com/m/examples/bigtable/TableAdminExampleTest.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import static com.google.cloud.bigtable.admin.v2.models.GCRules.GCRULES;
2020
import static org.junit.Assert.assertFalse;
21+
import static org.junit.Assert.assertNotNull;
2122
import static org.junit.Assert.assertTrue;
2223

2324
import com.google.cloud.bigtable.admin.v2.BigtableTableAdminClient;
@@ -45,19 +46,25 @@
4546
/** Integration tests for {@link TableAdminExample} */
4647
public class TableAdminExampleTest {
4748

48-
private static final String PROJECT_PROPERTY_NAME = "bigtable.project";
49-
private static final String INSTANCE_PROPERTY_NAME = "bigtable.instance";
49+
private static final String INSTANCE_PROPERTY_NAME = "BIGTABLE_TESTING_INSTANCE";
5050
private static final String TABLE_PREFIX = "table";
5151
private static BigtableTableAdminClient adminClient;
5252
private static String instanceId;
5353
private static String projectId;
5454
private String tableId;
5555
private TableAdminExample tableAdmin;
5656

57+
private static String requireEnv(String varName) {
58+
assertNotNull(
59+
System.getenv(varName),
60+
"System property '%s' is required to perform these tests.".format(varName));
61+
return System.getenv(varName);
62+
}
63+
5764
@BeforeClass
5865
public static void beforeClass() throws IOException {
59-
projectId = System.getProperty(PROJECT_PROPERTY_NAME);
60-
instanceId = System.getProperty(INSTANCE_PROPERTY_NAME);
66+
projectId = requireEnv("GOOGLE_CLOUD_PROJECT");
67+
instanceId = requireEnv(INSTANCE_PROPERTY_NAME);
6168
if (projectId == null || instanceId == null) {
6269
adminClient = null;
6370
return;
@@ -78,13 +85,6 @@ public static void afterClass() {
7885

7986
@Before
8087
public void setup() throws IOException {
81-
if (adminClient == null) {
82-
throw new AssumptionViolatedException(
83-
INSTANCE_PROPERTY_NAME
84-
+ " or "
85-
+ PROJECT_PROPERTY_NAME
86-
+ " property is not set, skipping integration tests.");
87-
}
8888
tableId = generateTableId();
8989
tableAdmin = new TableAdminExample(projectId, instanceId, tableId);
9090
adminClient.createTable(CreateTableRequest.of(tableId).addFamily("cf"));

0 commit comments

Comments
 (0)
0