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 927a6cf23..4dcf2c953 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 @@ -1,5 +1,5 @@ /* - * Copyright 2015 Google LLC + * Copyright 2024 Google LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,6 +22,7 @@ import com.google.cloud.NoCredentials; import com.google.cloud.ServiceOptions; import com.google.cloud.datastore.DatastoreOptions; +import com.google.cloud.grpc.GrpcTransportOptions; import com.google.cloud.testing.BaseEmulatorHelper; import com.google.common.collect.ImmutableList; import java.io.IOException; @@ -215,6 +216,14 @@ public DatastoreOptions getOptions(String namespace) { return optionsBuilder.setNamespace(namespace).build(); } + /** + * Returns a {@link DatastoreOptions} instance that sets the host to use the Datastore emulator on + * localhost. The transportOptions is set to {@code grpcTransportOptions}. + */ + public DatastoreOptions getGrpcTransportOptions(GrpcTransportOptions grpcTransportOptions) { + return optionsBuilder.setTransportOptions(grpcTransportOptions).build(); + } + public DatastoreOptions.Builder setNamespace(String namespace) { return optionsBuilder.setNamespace(namespace); } diff --git a/google-cloud-datastore/src/main/java/com/google/cloud/datastore/testing/RemoteDatastoreHelper.java b/google-cloud-datastore/src/main/java/com/google/cloud/datastore/testing/RemoteDatastoreHelper.java index 1471328ff..647f54481 100644 --- a/google-cloud-datastore/src/main/java/com/google/cloud/datastore/testing/RemoteDatastoreHelper.java +++ b/google-cloud-datastore/src/main/java/com/google/cloud/datastore/testing/RemoteDatastoreHelper.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 Google LLC + * Copyright 2024 Google LLC * * 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/google-cloud-datastore/src/test/java/com/google/cloud/datastore/DatastoreTest.java b/google-cloud-datastore/src/test/java/com/google/cloud/datastore/AbstractDatastoreTest.java similarity index 98% rename from google-cloud-datastore/src/test/java/com/google/cloud/datastore/DatastoreTest.java rename to google-cloud-datastore/src/test/java/com/google/cloud/datastore/AbstractDatastoreTest.java index 184712443..540a5fed3 100644 --- a/google-cloud-datastore/src/test/java/com/google/cloud/datastore/DatastoreTest.java +++ b/google-cloud-datastore/src/test/java/com/google/cloud/datastore/AbstractDatastoreTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 Google LLC + * Copyright 2024 Google LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -67,7 +67,6 @@ import com.google.datastore.v1.RunQueryResponse; import com.google.datastore.v1.TransactionOptions; import com.google.protobuf.ByteString; -import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -79,21 +78,18 @@ import java.util.Set; import java.util.function.Predicate; import org.easymock.EasyMock; -import org.junit.AfterClass; import org.junit.Assert; import org.junit.Before; -import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; -import org.threeten.bp.Duration; @RunWith(JUnit4.class) -public class DatastoreTest { +public abstract class AbstractDatastoreTest { private static final LocalDatastoreHelper helper = LocalDatastoreHelper.create(1.0, 9090); - private static DatastoreOptions options = helper.getOptions(); - private static Datastore datastore; + protected static DatastoreOptions options = helper.getOptions(); + protected static Datastore datastore; private static final String PROJECT_ID = options.getProjectId(); private static final String KIND1 = "kind1"; private static final String KIND2 = "kind2"; @@ -166,11 +162,9 @@ public class DatastoreTest { private DatastoreRpcFactory rpcFactoryMock; private DatastoreRpc rpcMock; - @BeforeClass - public static void beforeClass() throws IOException, InterruptedException { - helper.start(); - options = helper.getOptions(); - datastore = options.getService(); + public AbstractDatastoreTest(DatastoreOptions options, Datastore datastore) { + this.options = options; + this.datastore = datastore; } @Before @@ -190,12 +184,6 @@ public void setUp() { datastore.add(ENTITY1, ENTITY2); } - @AfterClass - public static void afterClass() throws Exception { - datastore.close(); - helper.stop(Duration.ofMinutes(1)); - } - @Test public void testGetOptions() { assertSame(options, datastore.getOptions()); diff --git a/google-cloud-datastore/src/test/java/com/google/cloud/datastore/DatastoreOptionsTest.java b/google-cloud-datastore/src/test/java/com/google/cloud/datastore/DatastoreOptionsTest.java index 36e29defb..af9c82548 100644 --- a/google-cloud-datastore/src/test/java/com/google/cloud/datastore/DatastoreOptionsTest.java +++ b/google-cloud-datastore/src/test/java/com/google/cloud/datastore/DatastoreOptionsTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 Google LLC + * Copyright 2024 Google LLC * * 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/google-cloud-datastore/src/test/java/com/google/cloud/datastore/DatastoreTestGrpc.java b/google-cloud-datastore/src/test/java/com/google/cloud/datastore/DatastoreTestGrpc.java new file mode 100644 index 000000000..1f421d875 --- /dev/null +++ b/google-cloud-datastore/src/test/java/com/google/cloud/datastore/DatastoreTestGrpc.java @@ -0,0 +1,61 @@ +/* + * Copyright 2024 Google LLC + * + * 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.google.cloud.datastore; + +import com.google.cloud.datastore.testing.LocalDatastoreHelper; +import com.google.cloud.grpc.GrpcTransportOptions; +import com.google.common.truth.Truth; +import java.io.IOException; +import java.util.Arrays; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.threeten.bp.Duration; + +@RunWith(Parameterized.class) +public class DatastoreTestGrpc extends AbstractDatastoreTest { + + private static final LocalDatastoreHelper helper = LocalDatastoreHelper.create(1.0, 9090); + + private static DatastoreOptions options = + helper.getGrpcTransportOptions(GrpcTransportOptions.newBuilder().build()); + private static Datastore datastore = options.getService(); + + public DatastoreTestGrpc(DatastoreOptions options, Datastore datastore) { + super(options, datastore); + } + + @Parameterized.Parameters(name = "data options: {0}") + public static Iterable data() { + return Arrays.asList(new Object[][] {{options, datastore}}); + } + + @BeforeClass + public static void beforeClass() throws IOException, InterruptedException { + helper.start(); + options = helper.getGrpcTransportOptions(GrpcTransportOptions.newBuilder().build()); + datastore = options.getService(); + } + + @AfterClass + public static void afterClass() throws Exception { + datastore.close(); + Truth.assertThat(datastore.isClosed()).isTrue(); + helper.stop(Duration.ofMinutes(1)); + } +} diff --git a/google-cloud-datastore/src/test/java/com/google/cloud/datastore/DatastoreTestHttp.java b/google-cloud-datastore/src/test/java/com/google/cloud/datastore/DatastoreTestHttp.java new file mode 100644 index 000000000..245f68a1b --- /dev/null +++ b/google-cloud-datastore/src/test/java/com/google/cloud/datastore/DatastoreTestHttp.java @@ -0,0 +1,57 @@ +/* + * Copyright 2015 Google LLC + * + * 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.google.cloud.datastore; + +import com.google.cloud.datastore.testing.LocalDatastoreHelper; +import com.google.cloud.grpc.GrpcTransportOptions; +import java.io.IOException; +import java.util.Arrays; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.threeten.bp.Duration; + +@RunWith(Parameterized.class) +public class DatastoreTestHttp extends AbstractDatastoreTest { + + private static final LocalDatastoreHelper helper = LocalDatastoreHelper.create(1.0, 9090); + + private static DatastoreOptions options = helper.getOptions(); + private static Datastore datastore = options.getService(); + + public DatastoreTestHttp(DatastoreOptions options, Datastore datastore) { + super(options, datastore); + } + + @Parameterized.Parameters(name = "data options: {0}") + public static Iterable data() { + return Arrays.asList(new Object[][] {{options, datastore}}); + } + + @BeforeClass + public static void beforeClass() throws IOException, InterruptedException { + helper.start(); + options = helper.getGrpcTransportOptions(GrpcTransportOptions.newBuilder().build()); + datastore = options.getService(); + } + + @AfterClass + public static void afterClass() throws Exception { + helper.stop(Duration.ofMinutes(1)); + } +} 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 3ebd3ef4a..8359eb046 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 @@ -1,5 +1,5 @@ /* - * Copyright 2015 Google LLC + * Copyright 2024 Google LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,6 +16,7 @@ package com.google.cloud.datastore.testing; +import static com.google.common.truth.Truth.assertThat; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; @@ -23,12 +24,15 @@ import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; +import com.google.api.gax.grpc.InstantiatingGrpcChannelProvider; import com.google.cloud.NoCredentials; import com.google.cloud.datastore.Datastore; import com.google.cloud.datastore.DatastoreException; import com.google.cloud.datastore.DatastoreOptions; import com.google.cloud.datastore.Entity; import com.google.cloud.datastore.Key; +import com.google.cloud.grpc.GrpcTransportOptions; +import com.google.cloud.http.HttpTransportOptions; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; @@ -167,6 +171,23 @@ public void testOptions() { assertEquals(NAMESPACE, options.getNamespace()); } + @Test + public void testDefaultHttpTransportOptions() { + LocalDatastoreHelper helper = LocalDatastoreHelper.create(); + DatastoreOptions options = helper.getOptions(); + assertThat(options.getTransportOptions()).isInstanceOf(HttpTransportOptions.class); + } + + @Test + public void testSetGrpcTransportOptions() { + LocalDatastoreHelper helper = LocalDatastoreHelper.create(); + DatastoreOptions options = + helper.getGrpcTransportOptions(GrpcTransportOptions.newBuilder().build()); + assertThat(options.getTransportOptions()).isInstanceOf(GrpcTransportOptions.class); + assertThat(options.getTransportChannelProvider()) + .isInstanceOf(InstantiatingGrpcChannelProvider.class); + } + @Test public void testStartStopReset() throws IOException, InterruptedException, TimeoutException { try {