From 7809e1dc6476769f9edd5e4c35b43651f276ca51 Mon Sep 17 00:00:00 2001 From: Maitri Mangal Date: Fri, 13 Oct 2023 15:41:13 +0000 Subject: [PATCH] adding test case --- samples/install-without-bom/pom.xml | 5 +++ samples/snapshot/pom.xml | 5 +++ samples/snippets/pom.xml | 4 ++ .../src/test/java/pubsub/AdminIT.java | 41 +++++++++++++++++++ 4 files changed, 55 insertions(+) diff --git a/samples/install-without-bom/pom.xml b/samples/install-without-bom/pom.xml index e76fdeee0..3513cecf1 100644 --- a/samples/install-without-bom/pom.xml +++ b/samples/install-without-bom/pom.xml @@ -88,6 +88,11 @@ google-cloud-bigquery 2.33.1 + + com.google.cloud + google-cloud-storage + 2.28.0 + diff --git a/samples/snapshot/pom.xml b/samples/snapshot/pom.xml index 4ff513287..87d443cf2 100644 --- a/samples/snapshot/pom.xml +++ b/samples/snapshot/pom.xml @@ -87,6 +87,11 @@ google-cloud-bigquery 2.33.1 + + com.google.cloud + google-cloud-storage + 2.28.0 + diff --git a/samples/snippets/pom.xml b/samples/snippets/pom.xml index 7ffe9b828..fafda4479 100644 --- a/samples/snippets/pom.xml +++ b/samples/snippets/pom.xml @@ -63,6 +63,10 @@ com.google.cloud google-cloud-bigquery + + com.google.cloud + google-cloud-storage + org.apache.avro avro diff --git a/samples/snippets/src/test/java/pubsub/AdminIT.java b/samples/snippets/src/test/java/pubsub/AdminIT.java index e42c81290..82ea8a39d 100644 --- a/samples/snippets/src/test/java/pubsub/AdminIT.java +++ b/samples/snippets/src/test/java/pubsub/AdminIT.java @@ -44,6 +44,11 @@ import org.junit.Rule; import org.junit.Test; import org.junit.rules.Timeout; +import com.google.cloud.storage.Bucket; +import com.google.cloud.storage.BucketInfo; +import com.google.cloud.storage.Storage; +import com.google.cloud.storage.StorageClass; +import com.google.cloud.storage.StorageOptions; public class AdminIT { private ByteArrayOutputStream bout; @@ -64,6 +69,11 @@ public class AdminIT { private static final String bigquerySubscriptionId = "iam-bigquery-subscription-" + _suffix; private static final String bigqueryTableId = "java_samples_table_" + _suffix; + private static final String cloudStorageSubscriptionId = "iam-cloudstorage-subscription-" + _suffix; + private static final String cloudStorageBucketName = "java_samples_gcs_bucket_" + _suffix; + private static final String cloudStorageFilenamePrefix = "log_events_"; + private static final String cloudStorageFilenameSuffix = ".text"; + private static final TopicName topicName = TopicName.of(projectId, topicId); private static final SubscriptionName pullSubscriptionName = SubscriptionName.of(projectId, pullSubscriptionId); @@ -97,6 +107,9 @@ public void setUp() throws Exception { // Create table for BigQuery subscription. createBigQueryTable(); + + // Create bucket for CloudStorage subscription + createCloudStorageBucket(); } @After @@ -124,6 +137,9 @@ public void tearDown() throws Exception { // Delete BigQuery table. deleteBigQueryTable(); + //Delete GCS Bucket + deleteCloudStorageBucket(); + System.setOut(null); } @@ -153,6 +169,22 @@ private void deleteBigQueryTable() throws Exception { bigquery.delete(datasetId, BigQuery.DatasetDeleteOption.deleteContents()); } + private void createCloudStorageBucket() throws Exception { + Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService(); + String location = "ASIA"; + Bucket bucket = + storage.create( + BucketInfo.newBuilder(cloudStorageBucketName) + .setLocation(location) + .build()); + } + + private void deleteCloudStorageBucket() throws Exception { + Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService(); + Bucket bucket = storage.get(cloudStorageBucketName); + bucket.delete(); + } + @Test public void testAdmin() throws Exception { // Test create topic. @@ -264,6 +296,14 @@ public void testAdmin() throws Exception { assertThat(bout.toString()).contains("Created a BigQuery subscription:"); assertThat(bout.toString()).contains(bigqueryTablePath); + bout.reset(); + // Test create a CloudStorage susbscription + CreateCloudStorageSubscriptionExample.createCloudStorageSubscription( + projectId, topicId, cloudStorageSubscriptionId, cloudStorageBucketName, + cloudStorageFilenamePrefix, cloudStorageFilenameSuffix); + assertThat(bout.toString()).contains("Created a CloudStorage subscription:"); + assertThat(bout.toString()).contains(cloudStorageBucketName); + bout.reset(); // Test delete subscription. DeleteSubscriptionExample.deleteSubscriptionExample(projectId, pullSubscriptionId); @@ -271,6 +311,7 @@ public void testAdmin() throws Exception { DeleteSubscriptionExample.deleteSubscriptionExample(projectId, orderedSubscriptionId); DeleteSubscriptionExample.deleteSubscriptionExample(projectId, exactlyOnceSubscriptionId); DeleteSubscriptionExample.deleteSubscriptionExample(projectId, bigquerySubscriptionId); + DeleteSubscriptionExample.deleteSubscriptionExample(projectId, cloudStorageSubscriptionId); assertThat(bout.toString()).contains("Deleted subscription."); bout.reset();