From 4f35dbf398bb0ba602efcf1e3b39398736f4d29e Mon Sep 17 00:00:00 2001 From: elisheva-qlogic Date: Tue, 18 Dec 2018 16:18:46 -0500 Subject: [PATCH] system property for tests set up --- .../examples/bigtable/HelloWorldTest.java | 32 ++++++++++++------- .../examples/bigtable/InstanceAdminTest.java | 25 +++++++++++---- .../examples/bigtable/TableAdminTest.java | 26 +++++++++++---- 3 files changed, 59 insertions(+), 24 deletions(-) diff --git a/google-cloud-examples/src/test/java/com/google/cloud/examples/bigtable/HelloWorldTest.java b/google-cloud-examples/src/test/java/com/google/cloud/examples/bigtable/HelloWorldTest.java index 8b7e489f3f7c..6b228d9d98fe 100644 --- a/google-cloud-examples/src/test/java/com/google/cloud/examples/bigtable/HelloWorldTest.java +++ b/google-cloud-examples/src/test/java/com/google/cloud/examples/bigtable/HelloWorldTest.java @@ -30,33 +30,35 @@ import com.google.cloud.bigtable.data.v2.models.RowMutation; import java.io.IOException; import org.junit.AfterClass; +import org.junit.AssumptionViolatedException; +import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; public class HelloWorldTest { - private static final String PROJECT_ID = "test-project"; // Replace with your project ID - private static final String INSTANCE_ID = "test-instance"; // Replace with your instance ID + private static final String INSTANCE_PROPERTY_NAME = "bigtable.instance"; private static final String TABLE_ID = "test-table" + System.currentTimeMillis(); private static final String COLUMN_FAMILY_ID = "test-cf"; private static final String COLUMN_QUALIFIER = "test-greeting"; private static final String ROW_KEY_PREFIX = "test-rowKey"; - private static BigtableDataSettings settings; private static BigtableDataClient dataClient; - private static BigtableTableAdminSettings adminSettings; private static BigtableTableAdminClient adminClient; @BeforeClass public static void beforeClass() throws IOException { - settings = - BigtableDataSettings.newBuilder() - .setInstanceName(InstanceName.of(PROJECT_ID, INSTANCE_ID)) + String targetInstance = System.getProperty(INSTANCE_PROPERTY_NAME); + if (targetInstance == null) { + dataClient = null; + adminClient = null; + return; + } + BigtableDataSettings settings = + BigtableDataSettings.newBuilder().setInstanceName(InstanceName.parse(targetInstance)) .build(); dataClient = BigtableDataClient.create(settings); - adminSettings = - BigtableTableAdminSettings.newBuilder() - .setInstanceName(com.google.bigtable.admin.v2.InstanceName.of(PROJECT_ID, INSTANCE_ID)) - .build(); + BigtableTableAdminSettings adminSettings = BigtableTableAdminSettings.newBuilder() + .setInstanceName(com.google.bigtable.admin.v2.InstanceName.parse(targetInstance)).build(); adminClient = BigtableTableAdminClient.create(adminSettings); if (!adminClient.exists(TABLE_ID)) { adminClient.createTable(CreateTableRequest.of(TABLE_ID).addFamily(COLUMN_FAMILY_ID)); @@ -70,6 +72,14 @@ public static void afterClass() throws Exception { adminClient.close(); } + @Before + public void setup() { + if (adminClient == null || dataClient == null) { + throw new AssumptionViolatedException( + INSTANCE_PROPERTY_NAME + " property is not set, skipping integration tests."); + } + } + @Test public void testCreateAndDeleteTable() { // Create table diff --git a/google-cloud-examples/src/test/java/com/google/cloud/examples/bigtable/InstanceAdminTest.java b/google-cloud-examples/src/test/java/com/google/cloud/examples/bigtable/InstanceAdminTest.java index f4f56c7a4320..e8d88c3effae 100644 --- a/google-cloud-examples/src/test/java/com/google/cloud/examples/bigtable/InstanceAdminTest.java +++ b/google-cloud-examples/src/test/java/com/google/cloud/examples/bigtable/InstanceAdminTest.java @@ -19,6 +19,7 @@ import static org.junit.Assert.assertTrue; import com.google.api.gax.rpc.NotFoundException; +import com.google.bigtable.admin.v2.InstanceName; import com.google.bigtable.admin.v2.ProjectName; import com.google.cloud.bigtable.admin.v2.BigtableInstanceAdminClient; import com.google.cloud.bigtable.admin.v2.BigtableInstanceAdminSettings; @@ -30,23 +31,27 @@ import java.io.IOException; import java.util.List; import org.junit.AfterClass; +import org.junit.AssumptionViolatedException; +import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; public class InstanceAdminTest { - private static final String GCLOUD_PROJECT = "test-project"; // Replace with your project ID + private static final String INSTANCE_PROPERTY_NAME = "bigtable.instance"; private static final String PRODUCTION_INSTANCE = "test-instance"; private static final String PRODUCTION_CLUSTER = "test-cluster" + System.currentTimeMillis(); - private static BigtableInstanceAdminSettings instanceAdminSettings; private static BigtableInstanceAdminClient adminClient; @BeforeClass public static void beforeClass() throws IOException { - instanceAdminSettings = - BigtableInstanceAdminSettings.newBuilder() - .setProjectName(ProjectName.of(GCLOUD_PROJECT)) - .build(); + String targetProject = System.getProperty(INSTANCE_PROPERTY_NAME); + ProjectName projectName = ProjectName.of(InstanceName.parse(targetProject).getProject()); + if (targetProject == null) { + adminClient = null; + } + BigtableInstanceAdminSettings instanceAdminSettings = + BigtableInstanceAdminSettings.newBuilder().setProjectName(projectName).build(); adminClient = BigtableInstanceAdminClient.create(instanceAdminSettings); if (!adminClient.exists(PRODUCTION_INSTANCE)) { adminClient.createInstance( @@ -63,6 +68,14 @@ public static void afterClass() { adminClient.close(); } + @Before + public void setup() { + if (adminClient == null) { + throw new AssumptionViolatedException( + INSTANCE_PROPERTY_NAME + " property is not set, skipping integration tests."); + } + } + @Test public void testCreateAndDeleteInstance() { // Create instance diff --git a/google-cloud-examples/src/test/java/com/google/cloud/examples/bigtable/TableAdminTest.java b/google-cloud-examples/src/test/java/com/google/cloud/examples/bigtable/TableAdminTest.java index 567c7eb26824..65f4f8ceba02 100644 --- a/google-cloud-examples/src/test/java/com/google/cloud/examples/bigtable/TableAdminTest.java +++ b/google-cloud-examples/src/test/java/com/google/cloud/examples/bigtable/TableAdminTest.java @@ -18,6 +18,7 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; +import com.google.bigtable.admin.v2.InstanceName; import com.google.cloud.bigtable.admin.v2.BigtableTableAdminClient; import com.google.cloud.bigtable.admin.v2.BigtableTableAdminSettings; import com.google.cloud.bigtable.admin.v2.models.ColumnFamily; @@ -27,23 +28,26 @@ import java.util.Collection; import java.util.List; import org.junit.AfterClass; +import org.junit.AssumptionViolatedException; +import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; public class TableAdminTest { - private static final String GCLOUD_PROJECT = "test-project"; // Replace with your project ID - private static final String INSTANCE_ID = "test-instance"; // Replace with your instance ID + private static final String INSTANCE_PROPERTY_NAME = "bigtable.instance"; private static final String TABLE_ID = "test-table" + System.currentTimeMillis(); - private static BigtableTableAdminSettings adminSettings; private static BigtableTableAdminClient adminClient; @BeforeClass public static void beforeClass() throws IOException { - adminSettings = - BigtableTableAdminSettings.newBuilder() - .setInstanceName( - com.google.bigtable.admin.v2.InstanceName.of(GCLOUD_PROJECT, INSTANCE_ID)) + String targetInstance = System.getProperty(INSTANCE_PROPERTY_NAME); + if (targetInstance == null) { + adminClient = null; + return; + } + BigtableTableAdminSettings adminSettings = + BigtableTableAdminSettings.newBuilder().setInstanceName(InstanceName.parse(targetInstance)) .build(); adminClient = BigtableTableAdminClient.create(adminSettings); if (!adminClient.exists(TABLE_ID)) { @@ -57,6 +61,14 @@ public static void afterClass() { adminClient.close(); } + @Before + public void setup() { + if (adminClient == null) { + throw new AssumptionViolatedException( + INSTANCE_PROPERTY_NAME + " property is not set, skipping integration tests."); + } + } + @Test public void testCreateAndDeleteTable() { // Create table