diff --git a/README.md b/README.md index 31a90d7fb..ab6063e79 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) | | Avg Aggregation On Kind | [source code](https://github.com/googleapis/java-datastore/blob/main/samples/snippets/src/main/java/com/example/datastore/aggregation/AvgAggregationOnKind.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/AvgAggregationOnKind.java) | | Avg Aggregation With Limit | [source code](https://github.com/googleapis/java-datastore/blob/main/samples/snippets/src/main/java/com/example/datastore/aggregation/AvgAggregationWithLimit.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/AvgAggregationWithLimit.java) | | Avg Aggregation With Order By | [source code](https://github.com/googleapis/java-datastore/blob/main/samples/snippets/src/main/java/com/example/datastore/aggregation/AvgAggregationWithOrderBy.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/AvgAggregationWithOrderBy.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 new file mode 100644 index 000000000..984524630 --- /dev/null +++ b/samples/snippets/src/main/java/com/example/datastore/RegionalEndpoint.java @@ -0,0 +1,40 @@ +/* + * 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. + * 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; + +// Imports the Google Cloud client library +// [START datastore_regional_endpoint] + +import com.google.cloud.datastore.Datastore; +import com.google.cloud.datastore.DatastoreOptions; + +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(); + Datastore datastore = options.getService(); + 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 new file mode 100644 index 000000000..bf029dca6 --- /dev/null +++ b/samples/snippets/src/test/java/com/example/datastore/RegionalEndpointIT.java @@ -0,0 +1,75 @@ +/* + * 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. + * 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 static org.junit.Assert.assertEquals; + +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; +import org.junit.runners.JUnit4; + +/** + * Tests for quickstart sample. + */ +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class RegionalEndpointIT { + + private static RegionalEndpoint regionalEndpoint; + + 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 + // The kind for the test entity + String kind = "Task"; + // Use uuid to create key for the test entity + Key taskKey = datastoreWithEndpoint.newKeyFactory().setKind(kind).newKey(uuid.toString()); + + // Prepare the new entity + Entity task = Entity.newBuilder(taskKey).set("description", "Buy milk").build(); + + // Save the entity + datastoreWithEndpoint.put(task); + + // Retrieve the entity + Entity retrieved = datastoreWithEndpoint.get(taskKey); + assertEquals(task, retrieved); + // Remove the test entity + deleteTestEntity(datastoreWithEndpoint, taskKey); + } +}