From f8f37af480976b7d4f041f2e9e544da7e832db1d Mon Sep 17 00:00:00 2001 From: Shweta Shetye Date: Mon, 10 Apr 2023 06:00:38 +0000 Subject: [PATCH 01/12] chore: added regional endpoint sample for datastore --- .../example/datastore/QuickstartSample.java | 4 +- .../example/datastore/RegionalEndpoint.java | 59 ++++++++++++++++++ .../example/datastore/RegionalEndpointIT.java | 62 +++++++++++++++++++ 3 files changed, 124 insertions(+), 1 deletion(-) create mode 100644 samples/snippets/src/main/java/com/example/datastore/RegionalEndpoint.java create mode 100644 samples/snippets/src/test/java/com/example/datastore/RegionalEndpointIT.java diff --git a/samples/snippets/src/main/java/com/example/datastore/QuickstartSample.java b/samples/snippets/src/main/java/com/example/datastore/QuickstartSample.java index 437ceb3a0..d509b777b 100644 --- a/samples/snippets/src/main/java/com/example/datastore/QuickstartSample.java +++ b/samples/snippets/src/main/java/com/example/datastore/QuickstartSample.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 Google Inc. + * Copyright 2023 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -24,6 +24,8 @@ import com.google.cloud.datastore.Key; public class QuickstartSample { + + public static void main(String... args) throws Exception { // Instantiates a client Datastore datastore = DatastoreOptions.getDefaultInstance().getService(); diff --git a/samples/snippets/src/main/java/com/example/datastore/RegionalEndpoint.java b/samples/snippets/src/main/java/com/example/datastore/RegionalEndpoint.java new file mode 100644 index 000000000..4454a1051 --- /dev/null +++ b/samples/snippets/src/main/java/com/example/datastore/RegionalEndpoint.java @@ -0,0 +1,59 @@ +/* + * Copyright 2016 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.datastore; + +// [START datastore_regional_endpoint] +// Imports the Google Cloud client library +import com.google.cloud.datastore.Datastore; +import com.google.cloud.datastore.DatastoreOptions; +import com.google.cloud.datastore.Entity; +import com.google.cloud.datastore.Key; + +public class RegionalEndpoint { + + + public static void main(String... args) throws Exception { + // Instantiates a client + DatastoreOptions options = DatastoreOptions.newBuilder() + .setProjectId("datastore-project-382616") + .setHost("https://nam5-firestore.googleapis.com") + .build(); + Datastore datastore = options.getService(); + + // The kind for the new entity + String kind = "Task"; + // The name/ID for the new entity + String name = "sampletask1"; + // The Cloud Datastore key for the new entity + Key taskKey = datastore.newKeyFactory().setKind(kind).newKey(name); + + // Prepares the new entity + Entity task = Entity.newBuilder(taskKey).set("description", "Buy milk").build(); + + // Saves the entity + datastore.put(task); + + System.out.printf("Saved %s: %s%n", task.getKey().getName(), task.getString("description")); + + // Retrieve entity + Entity retrieved = datastore.get(taskKey); + + System.out.printf("Retrieved %s: %s%n", taskKey.getName(), retrieved.getString("description")); + + } +} +// [END datastore_regional_endpoint] diff --git a/samples/snippets/src/test/java/com/example/datastore/RegionalEndpointIT.java b/samples/snippets/src/test/java/com/example/datastore/RegionalEndpointIT.java new file mode 100644 index 000000000..7e26b63ee --- /dev/null +++ b/samples/snippets/src/test/java/com/example/datastore/RegionalEndpointIT.java @@ -0,0 +1,62 @@ +/* + * Copyright 2016 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.datastore; + +import com.google.cloud.datastore.Datastore; +import com.google.cloud.datastore.DatastoreOptions; +import com.google.cloud.datastore.Key; +import com.rule.SystemsOutRule; +import org.junit.After; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Tests for quickstart sample. */ +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class RegionalEndpointIT { + + @Rule public final SystemsOutRule systemsOutRule = new SystemsOutRule(); + + private static final void deleteTestEntity() { + Datastore datastore = DatastoreOptions.getDefaultInstance().getService(); + String kind = "Task"; + String name = "sampletask1"; + Key taskKey = datastore.newKeyFactory().setKind(kind).newKey(name); + datastore.delete(taskKey); + } + + @Before + public void setUp() { + deleteTestEntity(); + } + + @After + public void tearDown() { + System.setOut(null); + deleteTestEntity(); + } + + @Test + public void testQuickstart() throws Exception { + QuickstartSample.main(); + systemsOutRule.assertContains("Saved sampletask1: Buy milk"); + systemsOutRule.assertContains("Retrieved sampletask1: Buy milk"); + } +} From 3c3b6550a2b55a15a75e47b7a0d2c9858b2b973e Mon Sep 17 00:00:00 2001 From: Shweta Shetye Date: Mon, 10 Apr 2023 06:04:34 +0000 Subject: [PATCH 02/12] chore: added regional endpoint sample for datastore --- .../src/main/java/com/example/datastore/QuickstartSample.java | 2 +- .../src/main/java/com/example/datastore/RegionalEndpoint.java | 2 +- .../src/test/java/com/example/datastore/RegionalEndpointIT.java | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/samples/snippets/src/main/java/com/example/datastore/QuickstartSample.java b/samples/snippets/src/main/java/com/example/datastore/QuickstartSample.java index d509b777b..9f2b5552f 100644 --- a/samples/snippets/src/main/java/com/example/datastore/QuickstartSample.java +++ b/samples/snippets/src/main/java/com/example/datastore/QuickstartSample.java @@ -1,5 +1,5 @@ /* - * Copyright 2023 Google Inc. + * Copyright 2016 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/samples/snippets/src/main/java/com/example/datastore/RegionalEndpoint.java b/samples/snippets/src/main/java/com/example/datastore/RegionalEndpoint.java index 4454a1051..12cbbe936 100644 --- a/samples/snippets/src/main/java/com/example/datastore/RegionalEndpoint.java +++ b/samples/snippets/src/main/java/com/example/datastore/RegionalEndpoint.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 Google Inc. + * Copyright 2023 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/samples/snippets/src/test/java/com/example/datastore/RegionalEndpointIT.java b/samples/snippets/src/test/java/com/example/datastore/RegionalEndpointIT.java index 7e26b63ee..5d68b0643 100644 --- a/samples/snippets/src/test/java/com/example/datastore/RegionalEndpointIT.java +++ b/samples/snippets/src/test/java/com/example/datastore/RegionalEndpointIT.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 Google Inc. + * Copyright 2023 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. From 117c621620cb5c51d12e18cccad6e5154ee7a037 Mon Sep 17 00:00:00 2001 From: Juan Lara Date: Tue, 11 Apr 2023 16:43:54 +0000 Subject: [PATCH 03/12] revert changes to QuickstartSample.java --- .../src/main/java/com/example/datastore/QuickstartSample.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/samples/snippets/src/main/java/com/example/datastore/QuickstartSample.java b/samples/snippets/src/main/java/com/example/datastore/QuickstartSample.java index 9f2b5552f..437ceb3a0 100644 --- a/samples/snippets/src/main/java/com/example/datastore/QuickstartSample.java +++ b/samples/snippets/src/main/java/com/example/datastore/QuickstartSample.java @@ -24,8 +24,6 @@ import com.google.cloud.datastore.Key; public class QuickstartSample { - - public static void main(String... args) throws Exception { // Instantiates a client Datastore datastore = DatastoreOptions.getDefaultInstance().getService(); From 0bcafad2bce674cca3f46628cc86d6fc249202f5 Mon Sep 17 00:00:00 2001 From: Juan Lara Date: Tue, 11 Apr 2023 17:29:24 +0000 Subject: [PATCH 04/12] Refactor test. Move validation operations to the test. --- .../example/datastore/RegionalEndpoint.java | 31 +++---------- .../example/datastore/RegionalEndpointIT.java | 43 +++++++++++++++---- 2 files changed, 41 insertions(+), 33 deletions(-) diff --git a/samples/snippets/src/main/java/com/example/datastore/RegionalEndpoint.java b/samples/snippets/src/main/java/com/example/datastore/RegionalEndpoint.java index 12cbbe936..eb02d1edc 100644 --- a/samples/snippets/src/main/java/com/example/datastore/RegionalEndpoint.java +++ b/samples/snippets/src/main/java/com/example/datastore/RegionalEndpoint.java @@ -16,8 +16,8 @@ package com.example.datastore; -// [START datastore_regional_endpoint] -// Imports the Google Cloud client library +// Imports the Google Cloud client library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 + import com.google.cloud.datastore.Datastore; import com.google.cloud.datastore.DatastoreOptions; import com.google.cloud.datastore.Entity; @@ -26,34 +26,15 @@ public class RegionalEndpoint { - public static void main(String... args) throws Exception { + public Datastore createClient() throws Exception { // Instantiates a client + // [START datastore_regional_endpoint] DatastoreOptions options = DatastoreOptions.newBuilder() - .setProjectId("datastore-project-382616") .setHost("https://nam5-firestore.googleapis.com") .build(); Datastore datastore = options.getService(); - - // The kind for the new entity - String kind = "Task"; - // The name/ID for the new entity - String name = "sampletask1"; - // The Cloud Datastore key for the new entity - Key taskKey = datastore.newKeyFactory().setKind(kind).newKey(name); - - // Prepares the new entity - Entity task = Entity.newBuilder(taskKey).set("description", "Buy milk").build(); - - // Saves the entity - datastore.put(task); - - System.out.printf("Saved %s: %s%n", task.getKey().getName(), task.getString("description")); - - // Retrieve entity - Entity retrieved = datastore.get(taskKey); - - System.out.printf("Retrieved %s: %s%n", taskKey.getName(), retrieved.getString("description")); + // [END datastore_regional_endpoint] + return datastore; } } -// [END datastore_regional_endpoint] diff --git a/samples/snippets/src/test/java/com/example/datastore/RegionalEndpointIT.java b/samples/snippets/src/test/java/com/example/datastore/RegionalEndpointIT.java index 5d68b0643..8a44d2eb2 100644 --- a/samples/snippets/src/test/java/com/example/datastore/RegionalEndpointIT.java +++ b/samples/snippets/src/test/java/com/example/datastore/RegionalEndpointIT.java @@ -18,6 +18,7 @@ import com.google.cloud.datastore.Datastore; import com.google.cloud.datastore.DatastoreOptions; +import com.google.cloud.datastore.Entity; import com.google.cloud.datastore.Key; import com.rule.SystemsOutRule; import org.junit.After; @@ -27,15 +28,18 @@ import org.junit.runner.RunWith; import org.junit.runners.JUnit4; -/** Tests for quickstart sample. */ +/** + * Tests for quickstart sample. + */ @RunWith(JUnit4.class) @SuppressWarnings("checkstyle:abbreviationaswordinname") public class RegionalEndpointIT { - @Rule public final SystemsOutRule systemsOutRule = new SystemsOutRule(); + private static RegionalEndpoint regionalEndpoint; + @Rule + public final SystemsOutRule systemsOutRule = new SystemsOutRule(); - private static final void deleteTestEntity() { - Datastore datastore = DatastoreOptions.getDefaultInstance().getService(); + private static final void deleteTestEntity(Datastore datastore) { String kind = "Task"; String name = "sampletask1"; Key taskKey = datastore.newKeyFactory().setKind(kind).newKey(name); @@ -44,19 +48,42 @@ private static final void deleteTestEntity() { @Before public void setUp() { - deleteTestEntity(); + regionalEndpoint = new RegionalEndpoint(); } @After public void tearDown() { System.setOut(null); - deleteTestEntity(); } @Test - public void testQuickstart() throws Exception { - QuickstartSample.main(); + public void testRegionalEndpoint() throws Exception { + Datastore datastoreWithEndpoint = regionalEndpoint.createClient(); + + // run a few operations with the client + deleteTestEntity(datastoreWithEndpoint); + // The kind for the new entity + String kind = "Task"; + // The name/ID for the new entity + String name = "sampletask1"; + // The Cloud Datastore key for the new entity + Key taskKey = datastoreWithEndpoint.newKeyFactory().setKind(kind).newKey(name); + + // Prepares the new entity + Entity task = Entity.newBuilder(taskKey).set("description", "Buy milk").build(); + + // Saves the entity + datastoreWithEndpoint.put(task); + + System.out.printf("Saved %s: %s%n", task.getKey().getName(), task.getString("description")); + + // Retrieve entity + Entity retrieved = datastoreWithEndpoint.get(taskKey); + + System.out.printf("Retrieved %s: %s%n", taskKey.getName(), retrieved.getString("description")); + systemsOutRule.assertContains("Saved sampletask1: Buy milk"); systemsOutRule.assertContains("Retrieved sampletask1: Buy milk"); + deleteTestEntity(datastoreWithEndpoint); } } From e0eda8e51ae76d8bf522773bcc5cffd1959aeb78 Mon Sep 17 00:00:00 2001 From: Juan Lara Date: Tue, 11 Apr 2023 20:02:58 +0000 Subject: [PATCH 05/12] Use correct api endpoint. --- .../src/main/java/com/example/datastore/RegionalEndpoint.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/snippets/src/main/java/com/example/datastore/RegionalEndpoint.java b/samples/snippets/src/main/java/com/example/datastore/RegionalEndpoint.java index eb02d1edc..51c74b372 100644 --- a/samples/snippets/src/main/java/com/example/datastore/RegionalEndpoint.java +++ b/samples/snippets/src/main/java/com/example/datastore/RegionalEndpoint.java @@ -30,7 +30,7 @@ public Datastore createClient() throws Exception { // Instantiates a client // [START datastore_regional_endpoint] DatastoreOptions options = DatastoreOptions.newBuilder() - .setHost("https://nam5-firestore.googleapis.com") + .setHost("https://nam5-datastore.googleapis.com") .build(); Datastore datastore = options.getService(); // [END datastore_regional_endpoint] From ec72ff5dc239b701468356b190b73c7c5c31ac2e Mon Sep 17 00:00:00 2001 From: Juan Lara Date: Wed, 12 Apr 2023 19:25:57 +0000 Subject: [PATCH 06/12] remove typo --- .../src/main/java/com/example/datastore/RegionalEndpoint.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/snippets/src/main/java/com/example/datastore/RegionalEndpoint.java b/samples/snippets/src/main/java/com/example/datastore/RegionalEndpoint.java index 51c74b372..d7b8a57af 100644 --- a/samples/snippets/src/main/java/com/example/datastore/RegionalEndpoint.java +++ b/samples/snippets/src/main/java/com/example/datastore/RegionalEndpoint.java @@ -16,7 +16,7 @@ package com.example.datastore; -// Imports the Google Cloud client library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 +// Imports the Google Cloud client library import com.google.cloud.datastore.Datastore; import com.google.cloud.datastore.DatastoreOptions; From 2cdaf5d4e2d15899efb9e9dbf27ac9b6e8731b75 Mon Sep 17 00:00:00 2001 From: Owl Bot Date: Wed, 12 Apr 2023 19:32:44 +0000 Subject: [PATCH 07/12] =?UTF-8?q?=F0=9F=A6=89=20Updates=20from=20OwlBot=20?= =?UTF-8?q?post-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 --- README.md | 1 + .../java/com/example/datastore/RegionalEndpoint.java | 9 ++------- .../java/com/example/datastore/RegionalEndpointIT.java | 8 ++------ 3 files changed, 5 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 8a3a3bbb2..0245bcab1 100644 --- a/README.md +++ b/README.md @@ -262,6 +262,7 @@ Samples are in the [`samples/`](https://github.com/googleapis/java-datastore/tre | --------------------------- | --------------------------------- | ------ | | Native Image Datastore Sample | [source code](https://github.com/googleapis/java-datastore/blob/main/samples/native-image-sample/src/main/java/com/example/datastore/NativeImageDatastoreSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-datastore&page=editor&open_in_editor=samples/native-image-sample/src/main/java/com/example/datastore/NativeImageDatastoreSample.java) | | Quickstart Sample | [source code](https://github.com/googleapis/java-datastore/blob/main/samples/snippets/src/main/java/com/example/datastore/QuickstartSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-datastore&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/datastore/QuickstartSample.java) | +| Regional Endpoint | [source code](https://github.com/googleapis/java-datastore/blob/main/samples/snippets/src/main/java/com/example/datastore/RegionalEndpoint.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-datastore&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/datastore/RegionalEndpoint.java) | | Count Aggregation In Transaction | [source code](https://github.com/googleapis/java-datastore/blob/main/samples/snippets/src/main/java/com/example/datastore/aggregation/CountAggregationInTransaction.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-datastore&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/datastore/aggregation/CountAggregationInTransaction.java) | | Count Aggregation On Kind | [source code](https://github.com/googleapis/java-datastore/blob/main/samples/snippets/src/main/java/com/example/datastore/aggregation/CountAggregationOnKind.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-datastore&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/datastore/aggregation/CountAggregationOnKind.java) | | Count Aggregation With Gql Query | [source code](https://github.com/googleapis/java-datastore/blob/main/samples/snippets/src/main/java/com/example/datastore/aggregation/CountAggregationWithGqlQuery.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-datastore&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/datastore/aggregation/CountAggregationWithGqlQuery.java) | diff --git a/samples/snippets/src/main/java/com/example/datastore/RegionalEndpoint.java b/samples/snippets/src/main/java/com/example/datastore/RegionalEndpoint.java index d7b8a57af..ce52eaf82 100644 --- a/samples/snippets/src/main/java/com/example/datastore/RegionalEndpoint.java +++ b/samples/snippets/src/main/java/com/example/datastore/RegionalEndpoint.java @@ -20,21 +20,16 @@ import com.google.cloud.datastore.Datastore; import com.google.cloud.datastore.DatastoreOptions; -import com.google.cloud.datastore.Entity; -import com.google.cloud.datastore.Key; public class RegionalEndpoint { - public Datastore createClient() throws Exception { // Instantiates a client // [START datastore_regional_endpoint] - DatastoreOptions options = DatastoreOptions.newBuilder() - .setHost("https://nam5-datastore.googleapis.com") - .build(); + DatastoreOptions options = + DatastoreOptions.newBuilder().setHost("https://nam5-datastore.googleapis.com").build(); Datastore datastore = options.getService(); // [END datastore_regional_endpoint] return datastore; - } } diff --git a/samples/snippets/src/test/java/com/example/datastore/RegionalEndpointIT.java b/samples/snippets/src/test/java/com/example/datastore/RegionalEndpointIT.java index 8a44d2eb2..ac7b374a2 100644 --- a/samples/snippets/src/test/java/com/example/datastore/RegionalEndpointIT.java +++ b/samples/snippets/src/test/java/com/example/datastore/RegionalEndpointIT.java @@ -17,7 +17,6 @@ package com.example.datastore; import com.google.cloud.datastore.Datastore; -import com.google.cloud.datastore.DatastoreOptions; import com.google.cloud.datastore.Entity; import com.google.cloud.datastore.Key; import com.rule.SystemsOutRule; @@ -28,16 +27,13 @@ import org.junit.runner.RunWith; import org.junit.runners.JUnit4; -/** - * Tests for quickstart sample. - */ +/** Tests for quickstart sample. */ @RunWith(JUnit4.class) @SuppressWarnings("checkstyle:abbreviationaswordinname") public class RegionalEndpointIT { private static RegionalEndpoint regionalEndpoint; - @Rule - public final SystemsOutRule systemsOutRule = new SystemsOutRule(); + @Rule public final SystemsOutRule systemsOutRule = new SystemsOutRule(); private static final void deleteTestEntity(Datastore datastore) { String kind = "Task"; From eeff0ae776ad3c03484db6925f3ecd77dea846e9 Mon Sep 17 00:00:00 2001 From: Juan Lara Date: Wed, 12 Apr 2023 21:34:53 +0000 Subject: [PATCH 08/12] Refactor test --- .../example/datastore/RegionalEndpoint.java | 4 ++-- .../example/datastore/RegionalEndpointIT.java | 22 +++++-------------- 2 files changed, 8 insertions(+), 18 deletions(-) diff --git a/samples/snippets/src/main/java/com/example/datastore/RegionalEndpoint.java b/samples/snippets/src/main/java/com/example/datastore/RegionalEndpoint.java index ce52eaf82..7fcfcc4a5 100644 --- a/samples/snippets/src/main/java/com/example/datastore/RegionalEndpoint.java +++ b/samples/snippets/src/main/java/com/example/datastore/RegionalEndpoint.java @@ -17,6 +17,7 @@ package com.example.datastore; // Imports the Google Cloud client library +// [START datastore_regional_endpoint] import com.google.cloud.datastore.Datastore; import com.google.cloud.datastore.DatastoreOptions; @@ -25,11 +26,10 @@ public class RegionalEndpoint { public Datastore createClient() throws Exception { // Instantiates a client - // [START datastore_regional_endpoint] DatastoreOptions options = DatastoreOptions.newBuilder().setHost("https://nam5-datastore.googleapis.com").build(); Datastore datastore = options.getService(); - // [END datastore_regional_endpoint] return datastore; } } +// [END datastore_regional_endpoint] \ No newline at end of file diff --git a/samples/snippets/src/test/java/com/example/datastore/RegionalEndpointIT.java b/samples/snippets/src/test/java/com/example/datastore/RegionalEndpointIT.java index ac7b374a2..26fcfebd4 100644 --- a/samples/snippets/src/test/java/com/example/datastore/RegionalEndpointIT.java +++ b/samples/snippets/src/test/java/com/example/datastore/RegionalEndpointIT.java @@ -16,24 +16,24 @@ package com.example.datastore; +import static org.junit.Assert.assertEquals; + import com.google.cloud.datastore.Datastore; import com.google.cloud.datastore.Entity; import com.google.cloud.datastore.Key; -import com.rule.SystemsOutRule; -import org.junit.After; import org.junit.Before; -import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; -/** Tests for quickstart sample. */ +/** + * Tests for quickstart sample. + */ @RunWith(JUnit4.class) @SuppressWarnings("checkstyle:abbreviationaswordinname") public class RegionalEndpointIT { private static RegionalEndpoint regionalEndpoint; - @Rule public final SystemsOutRule systemsOutRule = new SystemsOutRule(); private static final void deleteTestEntity(Datastore datastore) { String kind = "Task"; @@ -47,11 +47,6 @@ public void setUp() { regionalEndpoint = new RegionalEndpoint(); } - @After - public void tearDown() { - System.setOut(null); - } - @Test public void testRegionalEndpoint() throws Exception { Datastore datastoreWithEndpoint = regionalEndpoint.createClient(); @@ -71,15 +66,10 @@ public void testRegionalEndpoint() throws Exception { // Saves the entity datastoreWithEndpoint.put(task); - System.out.printf("Saved %s: %s%n", task.getKey().getName(), task.getString("description")); - // Retrieve entity Entity retrieved = datastoreWithEndpoint.get(taskKey); - System.out.printf("Retrieved %s: %s%n", taskKey.getName(), retrieved.getString("description")); - - systemsOutRule.assertContains("Saved sampletask1: Buy milk"); - systemsOutRule.assertContains("Retrieved sampletask1: Buy milk"); + assertEquals(task, retrieved); deleteTestEntity(datastoreWithEndpoint); } } From dc413258137d200c782083965a9ed39fa8908114 Mon Sep 17 00:00:00 2001 From: Juan Lara Date: Wed, 26 Apr 2023 22:58:10 +0000 Subject: [PATCH 09/12] Use a unique ID for the test entity Use an identifiable name for the test entity. --- .../src/test/java/com/example/datastore/RegionalEndpointIT.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/snippets/src/test/java/com/example/datastore/RegionalEndpointIT.java b/samples/snippets/src/test/java/com/example/datastore/RegionalEndpointIT.java index 26fcfebd4..23f00c71a 100644 --- a/samples/snippets/src/test/java/com/example/datastore/RegionalEndpointIT.java +++ b/samples/snippets/src/test/java/com/example/datastore/RegionalEndpointIT.java @@ -56,7 +56,7 @@ public void testRegionalEndpoint() throws Exception { // The kind for the new entity String kind = "Task"; // The name/ID for the new entity - String name = "sampletask1"; + String name = "regionalEndpointClient50720906"; // The Cloud Datastore key for the new entity Key taskKey = datastoreWithEndpoint.newKeyFactory().setKind(kind).newKey(name); From 52c15d5e74bb00be906b5c877b1d4d6b0cbfb933 Mon Sep 17 00:00:00 2001 From: Juan Lara Date: Mon, 1 May 2023 18:16:31 +0000 Subject: [PATCH 10/12] Use UUID for test entity. Fix linting errors. --- .../example/datastore/RegionalEndpoint.java | 10 ++++-- .../example/datastore/RegionalEndpointIT.java | 36 ++++++++++--------- 2 files changed, 26 insertions(+), 20 deletions(-) diff --git a/samples/snippets/src/main/java/com/example/datastore/RegionalEndpoint.java b/samples/snippets/src/main/java/com/example/datastore/RegionalEndpoint.java index 7fcfcc4a5..d509a8c38 100644 --- a/samples/snippets/src/main/java/com/example/datastore/RegionalEndpoint.java +++ b/samples/snippets/src/main/java/com/example/datastore/RegionalEndpoint.java @@ -24,12 +24,16 @@ public class RegionalEndpoint { + /** + * Create a client that uses a regional endpoint. + * @return Datastore client with regiona endpoint configured + */ public Datastore createClient() throws Exception { // Instantiates a client - DatastoreOptions options = - DatastoreOptions.newBuilder().setHost("https://nam5-datastore.googleapis.com").build(); + DatastoreOptions options = DatastoreOptions.newBuilder() + .setHost("https://nam5-datastore.googleapis.com").build(); Datastore datastore = options.getService(); return datastore; } } -// [END datastore_regional_endpoint] \ No newline at end of file +// [END datastore_regional_endpoint] diff --git a/samples/snippets/src/test/java/com/example/datastore/RegionalEndpointIT.java b/samples/snippets/src/test/java/com/example/datastore/RegionalEndpointIT.java index 23f00c71a..c54fc5a27 100644 --- a/samples/snippets/src/test/java/com/example/datastore/RegionalEndpointIT.java +++ b/samples/snippets/src/test/java/com/example/datastore/RegionalEndpointIT.java @@ -18,6 +18,9 @@ import static org.junit.Assert.assertEquals; +import java.util.UUID; + + import com.google.cloud.datastore.Datastore; import com.google.cloud.datastore.Entity; import com.google.cloud.datastore.Key; @@ -35,41 +38,40 @@ public class RegionalEndpointIT { private static RegionalEndpoint regionalEndpoint; - private static final void deleteTestEntity(Datastore datastore) { - String kind = "Task"; - String name = "sampletask1"; - Key taskKey = datastore.newKeyFactory().setKind(kind).newKey(name); - datastore.delete(taskKey); + private static UUID uuid; + + private static final void deleteTestEntity(Datastore datastore, Key key) { + datastore.delete(key); } @Before public void setUp() { + regionalEndpoint = new RegionalEndpoint(); + + uuid = UUID.randomUUID(); } @Test public void testRegionalEndpoint() throws Exception { Datastore datastoreWithEndpoint = regionalEndpoint.createClient(); - // run a few operations with the client - deleteTestEntity(datastoreWithEndpoint); - // The kind for the new entity + // Run a few operations with the client + // The kind for the test entity String kind = "Task"; - // The name/ID for the new entity - String name = "regionalEndpointClient50720906"; - // The Cloud Datastore key for the new entity - Key taskKey = datastoreWithEndpoint.newKeyFactory().setKind(kind).newKey(name); + // Use uuid to create key for the test entity + Key taskKey = datastoreWithEndpoint.newKeyFactory().setKind(kind).newKey(uuid.toString()); - // Prepares the new entity + // Prepare the new entity Entity task = Entity.newBuilder(taskKey).set("description", "Buy milk").build(); - // Saves the entity + // Save the entity datastoreWithEndpoint.put(task); - // Retrieve entity + // Retrieve the entity Entity retrieved = datastoreWithEndpoint.get(taskKey); - assertEquals(task, retrieved); - deleteTestEntity(datastoreWithEndpoint); + // Remove the test entity + deleteTestEntity(datastoreWithEndpoint, taskKey); } } From c446023ac5a48fa1e474386161acd6a4a2361343 Mon Sep 17 00:00:00 2001 From: Juan Lara Date: Mon, 1 May 2023 18:21:46 +0000 Subject: [PATCH 11/12] Fix linting error in javadoc @return comment --- .../src/main/java/com/example/datastore/RegionalEndpoint.java | 1 + 1 file changed, 1 insertion(+) diff --git a/samples/snippets/src/main/java/com/example/datastore/RegionalEndpoint.java b/samples/snippets/src/main/java/com/example/datastore/RegionalEndpoint.java index d509a8c38..984524630 100644 --- a/samples/snippets/src/main/java/com/example/datastore/RegionalEndpoint.java +++ b/samples/snippets/src/main/java/com/example/datastore/RegionalEndpoint.java @@ -26,6 +26,7 @@ public class RegionalEndpoint { /** * Create a client that uses a regional endpoint. + * * @return Datastore client with regiona endpoint configured */ public Datastore createClient() throws Exception { From 66fd56a5513a32c29cc27689bc1d0c07d3f5b7e8 Mon Sep 17 00:00:00 2001 From: Juan Lara Date: Mon, 1 May 2023 18:28:16 +0000 Subject: [PATCH 12/12] Fix order of imports in RegionalEndpointIT --- .../test/java/com/example/datastore/RegionalEndpointIT.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/samples/snippets/src/test/java/com/example/datastore/RegionalEndpointIT.java b/samples/snippets/src/test/java/com/example/datastore/RegionalEndpointIT.java index c54fc5a27..bf029dca6 100644 --- a/samples/snippets/src/test/java/com/example/datastore/RegionalEndpointIT.java +++ b/samples/snippets/src/test/java/com/example/datastore/RegionalEndpointIT.java @@ -18,12 +18,10 @@ import static org.junit.Assert.assertEquals; -import java.util.UUID; - - import com.google.cloud.datastore.Datastore; import com.google.cloud.datastore.Entity; import com.google.cloud.datastore.Key; +import java.util.UUID; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith;