From 3b458dd8de617dd970f910d5fbf18abf1e11afc0 Mon Sep 17 00:00:00 2001 From: Prateek Jain Date: Mon, 21 Nov 2022 18:35:02 +0530 Subject: [PATCH 1/4] Running gcloud datastore emulator with --project argument --- .../google/cloud/datastore/testing/LocalDatastoreHelper.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/google-cloud-datastore/src/main/java/com/google/cloud/datastore/testing/LocalDatastoreHelper.java b/google-cloud-datastore/src/main/java/com/google/cloud/datastore/testing/LocalDatastoreHelper.java index e586f7e55..9bc700ef1 100644 --- a/google-cloud-datastore/src/main/java/com/google/cloud/datastore/testing/LocalDatastoreHelper.java +++ b/google-cloud-datastore/src/main/java/com/google/cloud/datastore/testing/LocalDatastoreHelper.java @@ -68,6 +68,7 @@ public class LocalDatastoreHelper extends BaseEmulatorHelper { // Common settings private static final String CONSISTENCY_FLAG = "--consistency="; + private static final String PROJECT_FLAG = "--project="; private static final double DEFAULT_CONSISTENCY = 0.9; private static final Logger LOGGER = Logger.getLogger(LocalDatastoreHelper.class.getName()); @@ -140,6 +141,7 @@ private LocalDatastoreHelper(Builder builder) { List gcloudCommand = new ArrayList<>(Arrays.asList(GCLOUD_CMD_TEXT.split(" "))); gcloudCommand.add(GCLOUD_CMD_PORT_FLAG + "localhost:" + getPort()); gcloudCommand.add(CONSISTENCY_FLAG + builder.consistency); + gcloudCommand.add(PROJECT_FLAG + getProjectId()); if (!builder.storeOnDisk) { gcloudCommand.add("--no-store-on-disk"); } From a17b9807d16b4f10ba738d826422d8c38c3b4d7c Mon Sep 17 00:00:00 2001 From: Prateek Jain Date: Mon, 21 Nov 2022 18:36:00 +0530 Subject: [PATCH 2/4] Allow user to pass a custom project id when creating a new LocalDatastoreHelper instance --- .../cloud/datastore/testing/LocalDatastoreHelper.java | 11 ++++++++++- .../datastore/testing/ITLocalDatastoreHelperTest.java | 10 ++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/google-cloud-datastore/src/main/java/com/google/cloud/datastore/testing/LocalDatastoreHelper.java b/google-cloud-datastore/src/main/java/com/google/cloud/datastore/testing/LocalDatastoreHelper.java index 9bc700ef1..151a107d5 100644 --- a/google-cloud-datastore/src/main/java/com/google/cloud/datastore/testing/LocalDatastoreHelper.java +++ b/google-cloud-datastore/src/main/java/com/google/cloud/datastore/testing/LocalDatastoreHelper.java @@ -16,6 +16,8 @@ package com.google.cloud.datastore.testing; +import static com.google.common.base.MoreObjects.firstNonNull; + import com.google.api.core.InternalApi; import com.google.cloud.NoCredentials; import com.google.cloud.ServiceOptions; @@ -70,6 +72,7 @@ public class LocalDatastoreHelper extends BaseEmulatorHelper { private static final String CONSISTENCY_FLAG = "--consistency="; private static final String PROJECT_FLAG = "--project="; private static final double DEFAULT_CONSISTENCY = 0.9; + private static final String DEFAULT_PROJECT_ID = PROJECT_ID_PREFIX + UUID.randomUUID(); private static final Logger LOGGER = Logger.getLogger(LocalDatastoreHelper.class.getName()); @@ -91,6 +94,7 @@ public static class Builder { private int port; private Path dataDir; private boolean storeOnDisk = true; + private String projectId; private Builder() {} @@ -110,6 +114,11 @@ public Builder setPort(int port) { return this; } + public Builder setProjectId(String projectId) { + this.projectId = projectId; + return this; + } + public Builder setDataDir(Path dataDir) { this.dataDir = dataDir; return this; @@ -130,7 +139,7 @@ private LocalDatastoreHelper(Builder builder) { super( "datastore", builder.port > 0 ? builder.port : BaseEmulatorHelper.findAvailablePort(DEFAULT_PORT), - PROJECT_ID_PREFIX + UUID.randomUUID().toString()); + firstNonNull(builder.projectId, DEFAULT_PROJECT_ID)); this.consistency = builder.consistency > 0 ? builder.consistency : DEFAULT_CONSISTENCY; this.gcdPath = builder.dataDir; this.storeOnDisk = builder.storeOnDisk; diff --git a/google-cloud-datastore/src/test/java/com/google/cloud/datastore/testing/ITLocalDatastoreHelperTest.java b/google-cloud-datastore/src/test/java/com/google/cloud/datastore/testing/ITLocalDatastoreHelperTest.java index 6cc236a8b..523026fd6 100644 --- a/google-cloud-datastore/src/test/java/com/google/cloud/datastore/testing/ITLocalDatastoreHelperTest.java +++ b/google-cloud-datastore/src/test/java/com/google/cloud/datastore/testing/ITLocalDatastoreHelperTest.java @@ -88,6 +88,16 @@ public void testCreateWithBuilder() { assertTrue(incompleteHelper.getProjectId().startsWith(PROJECT_ID_PREFIX)); } + @Test + public void testCreateWithCustomProjectId() { + String customProjectId = "custom-project-id"; + LocalDatastoreHelper helper = + LocalDatastoreHelper.newBuilder() + .setProjectId(customProjectId) + .build(); + assertEquals(customProjectId, helper.getProjectId()); + } + @Test public void testCreateWithToBuilder() throws IOException { LocalDatastoreHelper helper = From 82786203ee17701ce617b06be1478fc6f3af7c25 Mon Sep 17 00:00:00 2001 From: Prateek Jain Date: Tue, 29 Nov 2022 10:21:56 +0530 Subject: [PATCH 3/4] Creating a local variable to increase the readability and emphasizing on the fact that project id not nullable now --- .../google/cloud/datastore/testing/LocalDatastoreHelper.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/google-cloud-datastore/src/main/java/com/google/cloud/datastore/testing/LocalDatastoreHelper.java b/google-cloud-datastore/src/main/java/com/google/cloud/datastore/testing/LocalDatastoreHelper.java index 151a107d5..db4bd112e 100644 --- a/google-cloud-datastore/src/main/java/com/google/cloud/datastore/testing/LocalDatastoreHelper.java +++ b/google-cloud-datastore/src/main/java/com/google/cloud/datastore/testing/LocalDatastoreHelper.java @@ -140,6 +140,7 @@ private LocalDatastoreHelper(Builder builder) { "datastore", builder.port > 0 ? builder.port : BaseEmulatorHelper.findAvailablePort(DEFAULT_PORT), firstNonNull(builder.projectId, DEFAULT_PROJECT_ID)); + String projectId = firstNonNull(builder.projectId, DEFAULT_PROJECT_ID); this.consistency = builder.consistency > 0 ? builder.consistency : DEFAULT_CONSISTENCY; this.gcdPath = builder.dataDir; this.storeOnDisk = builder.storeOnDisk; @@ -150,7 +151,7 @@ private LocalDatastoreHelper(Builder builder) { List gcloudCommand = new ArrayList<>(Arrays.asList(GCLOUD_CMD_TEXT.split(" "))); gcloudCommand.add(GCLOUD_CMD_PORT_FLAG + "localhost:" + getPort()); gcloudCommand.add(CONSISTENCY_FLAG + builder.consistency); - gcloudCommand.add(PROJECT_FLAG + getProjectId()); + gcloudCommand.add(PROJECT_FLAG + projectId); if (!builder.storeOnDisk) { gcloudCommand.add("--no-store-on-disk"); } From 8d8c1079a6c2535afeabd4f70adb83ecd2168637 Mon Sep 17 00:00:00 2001 From: Owl Bot Date: Thu, 1 Dec 2022 04:13:49 +0000 Subject: [PATCH 4/4] =?UTF-8?q?=F0=9F=A6=89=20Updates=20from=20OwlBot=20po?= =?UTF-8?q?st-processor?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md --- .../cloud/datastore/testing/ITLocalDatastoreHelperTest.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/google-cloud-datastore/src/test/java/com/google/cloud/datastore/testing/ITLocalDatastoreHelperTest.java b/google-cloud-datastore/src/test/java/com/google/cloud/datastore/testing/ITLocalDatastoreHelperTest.java index 523026fd6..8a7fd88b0 100644 --- a/google-cloud-datastore/src/test/java/com/google/cloud/datastore/testing/ITLocalDatastoreHelperTest.java +++ b/google-cloud-datastore/src/test/java/com/google/cloud/datastore/testing/ITLocalDatastoreHelperTest.java @@ -92,9 +92,7 @@ public void testCreateWithBuilder() { public void testCreateWithCustomProjectId() { String customProjectId = "custom-project-id"; LocalDatastoreHelper helper = - LocalDatastoreHelper.newBuilder() - .setProjectId(customProjectId) - .build(); + LocalDatastoreHelper.newBuilder().setProjectId(customProjectId).build(); assertEquals(customProjectId, helper.getProjectId()); }