From a1846b0eb54fbef3a4d3339f799fb2b13f26ec62 Mon Sep 17 00:00:00 2001 From: Shubha Rajan Date: Thu, 19 Mar 2020 13:32:40 -0700 Subject: [PATCH 1/3] modified tests to cancel job --- .../test/java/dlp/snippets/InspectTests.java | 28 +++++++++++++++---- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/dlp/src/test/java/dlp/snippets/InspectTests.java b/dlp/src/test/java/dlp/snippets/InspectTests.java index a767ed72dbc..0c73718fb16 100644 --- a/dlp/src/test/java/dlp/snippets/InspectTests.java +++ b/dlp/src/test/java/dlp/snippets/InspectTests.java @@ -20,6 +20,8 @@ import static org.hamcrest.core.StringContains.containsString; import static org.junit.Assert.assertNotNull; +import com.google.cloud.dlp.v2.DlpServiceClient; +import com.google.privacy.dlp.v2.CancelDlpJobRequest; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.PrintStream; @@ -103,8 +105,13 @@ public void testInspectGcsFile() throws InterruptedException, ExecutionException InspectGcsFile.inspectGcsFile(PROJECT_ID, GCS_PATH, TOPIC_ID, SUBSCRIPTION_ID); String output = bout.toString(); - assertThat(output, containsString("Info type: PHONE_NUMBER")); - assertThat(output, containsString("Info type: EMAIL_ADDRESS")); + assertThat(output, containsString("Job created: ")); + + String jobId = output.split("Job created: ")[1].split("\n")[0]; + CancelDlpJobRequest request = CancelDlpJobRequest.newBuilder().setName(jobId).build(); + try (DlpServiceClient client = DlpServiceClient.create()) { + client.cancelDlpJob(request); + } } @Test @@ -114,8 +121,13 @@ public void testInspectDatastoreEntity() PROJECT_ID, datastoreNamespace, datastoreKind, TOPIC_ID, SUBSCRIPTION_ID); String output = bout.toString(); - assertThat(output, containsString("Info type: PHONE_NUMBER")); - assertThat(output, containsString("Info type: EMAIL_ADDRESS")); + assertThat(output, containsString("Job created: ")); + + String jobId = output.split("Job created: ")[1].split("\n")[0]; + CancelDlpJobRequest request = CancelDlpJobRequest.newBuilder().setName(jobId).build(); + try (DlpServiceClient client = DlpServiceClient.create()) { + client.cancelDlpJob(request); + } } @Test @@ -125,6 +137,12 @@ public void testInspectBigQueryTable() PROJECT_ID, DATASET_ID, TABLE_ID, TOPIC_ID, SUBSCRIPTION_ID); String output = bout.toString(); - assertThat(output, containsString("Info type: PHONE_NUMBER")); + assertThat(output, containsString("Job created: ")); + + String jobId = output.split("Job created: ")[1].split("\n")[0]; + CancelDlpJobRequest request = CancelDlpJobRequest.newBuilder().setName(jobId).build(); + try (DlpServiceClient client = DlpServiceClient.create()) { + client.cancelDlpJob(request); + } } } From 1d9a26e61663ba5c5f517b1c700cd445916fb754 Mon Sep 17 00:00:00 2001 From: Shubha Rajan Date: Thu, 19 Mar 2020 16:40:19 -0700 Subject: [PATCH 2/3] added delay to tests --- dlp/src/test/java/dlp/snippets/InspectTests.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/dlp/src/test/java/dlp/snippets/InspectTests.java b/dlp/src/test/java/dlp/snippets/InspectTests.java index 0c73718fb16..7267992e6f8 100644 --- a/dlp/src/test/java/dlp/snippets/InspectTests.java +++ b/dlp/src/test/java/dlp/snippets/InspectTests.java @@ -26,6 +26,7 @@ import java.io.IOException; import java.io.PrintStream; import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -102,7 +103,9 @@ public void testInspectImageFile() { @Test public void testInspectGcsFile() throws InterruptedException, ExecutionException, IOException { + InspectGcsFile.inspectGcsFile(PROJECT_ID, GCS_PATH, TOPIC_ID, SUBSCRIPTION_ID); + TimeUnit.SECONDS.sleep(3); String output = bout.toString(); assertThat(output, containsString("Job created: ")); @@ -117,8 +120,10 @@ public void testInspectGcsFile() throws InterruptedException, ExecutionException @Test public void testInspectDatastoreEntity() throws InterruptedException, ExecutionException, IOException { + InspectDatastoreEntity.insepctDatastoreEntity( PROJECT_ID, datastoreNamespace, datastoreKind, TOPIC_ID, SUBSCRIPTION_ID); + TimeUnit.SECONDS.sleep(3); String output = bout.toString(); assertThat(output, containsString("Job created: ")); @@ -133,8 +138,10 @@ public void testInspectDatastoreEntity() @Test public void testInspectBigQueryTable() throws InterruptedException, ExecutionException, IOException { + InspectBigQueryTable.inspectBigQueryTable( PROJECT_ID, DATASET_ID, TABLE_ID, TOPIC_ID, SUBSCRIPTION_ID); + TimeUnit.SECONDS.sleep(3); String output = bout.toString(); assertThat(output, containsString("Job created: ")); From ff5af0ac9159aa93278e5e040d571c913dd557ab Mon Sep 17 00:00:00 2001 From: Shubha Rajan Date: Thu, 19 Mar 2020 23:40:40 -0700 Subject: [PATCH 3/3] added comment --- dlp/src/test/java/dlp/snippets/InspectTests.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/dlp/src/test/java/dlp/snippets/InspectTests.java b/dlp/src/test/java/dlp/snippets/InspectTests.java index 7267992e6f8..d42ea7c758a 100644 --- a/dlp/src/test/java/dlp/snippets/InspectTests.java +++ b/dlp/src/test/java/dlp/snippets/InspectTests.java @@ -105,11 +105,13 @@ public void testInspectImageFile() { public void testInspectGcsFile() throws InterruptedException, ExecutionException, IOException { InspectGcsFile.inspectGcsFile(PROJECT_ID, GCS_PATH, TOPIC_ID, SUBSCRIPTION_ID); + // Await job creation TimeUnit.SECONDS.sleep(3); String output = bout.toString(); assertThat(output, containsString("Job created: ")); + // Cancelling the job early to conserve quota String jobId = output.split("Job created: ")[1].split("\n")[0]; CancelDlpJobRequest request = CancelDlpJobRequest.newBuilder().setName(jobId).build(); try (DlpServiceClient client = DlpServiceClient.create()) { @@ -123,11 +125,13 @@ public void testInspectDatastoreEntity() InspectDatastoreEntity.insepctDatastoreEntity( PROJECT_ID, datastoreNamespace, datastoreKind, TOPIC_ID, SUBSCRIPTION_ID); + // Await job creation TimeUnit.SECONDS.sleep(3); String output = bout.toString(); assertThat(output, containsString("Job created: ")); + // Cancelling the job early to conserve quota String jobId = output.split("Job created: ")[1].split("\n")[0]; CancelDlpJobRequest request = CancelDlpJobRequest.newBuilder().setName(jobId).build(); try (DlpServiceClient client = DlpServiceClient.create()) { @@ -141,11 +145,13 @@ public void testInspectBigQueryTable() InspectBigQueryTable.inspectBigQueryTable( PROJECT_ID, DATASET_ID, TABLE_ID, TOPIC_ID, SUBSCRIPTION_ID); + // Await job creation TimeUnit.SECONDS.sleep(3); String output = bout.toString(); assertThat(output, containsString("Job created: ")); + // Cancelling the job early to conserve quota String jobId = output.split("Job created: ")[1].split("\n")[0]; CancelDlpJobRequest request = CancelDlpJobRequest.newBuilder().setName(jobId).build(); try (DlpServiceClient client = DlpServiceClient.create()) {