Skip to content

Commit 2306cfa

Browse files
authored
test: Add GRPC DatastoreTest and parameterize DatastoreTest for both gRPC and HTTP transports (#1478)
* test: Parameterize DatastoreTest to run for both gRPC and HTTP transports * test: Parameterize DatastoreTest to run for both gRPC and HTTP transports * fix linting * update copy right
1 parent 37256a4 commit 2306cfa

File tree

7 files changed

+159
-23
lines changed

7 files changed

+159
-23
lines changed

google-cloud-datastore/src/main/java/com/google/cloud/datastore/testing/LocalDatastoreHelper.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015 Google LLC
2+
* Copyright 2024 Google LLC
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,6 +22,7 @@
2222
import com.google.cloud.NoCredentials;
2323
import com.google.cloud.ServiceOptions;
2424
import com.google.cloud.datastore.DatastoreOptions;
25+
import com.google.cloud.grpc.GrpcTransportOptions;
2526
import com.google.cloud.testing.BaseEmulatorHelper;
2627
import com.google.common.collect.ImmutableList;
2728
import java.io.IOException;
@@ -215,6 +216,14 @@ public DatastoreOptions getOptions(String namespace) {
215216
return optionsBuilder.setNamespace(namespace).build();
216217
}
217218

219+
/**
220+
* Returns a {@link DatastoreOptions} instance that sets the host to use the Datastore emulator on
221+
* localhost. The transportOptions is set to {@code grpcTransportOptions}.
222+
*/
223+
public DatastoreOptions getGrpcTransportOptions(GrpcTransportOptions grpcTransportOptions) {
224+
return optionsBuilder.setTransportOptions(grpcTransportOptions).build();
225+
}
226+
218227
public DatastoreOptions.Builder setNamespace(String namespace) {
219228
return optionsBuilder.setNamespace(namespace);
220229
}

google-cloud-datastore/src/main/java/com/google/cloud/datastore/testing/RemoteDatastoreHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016 Google LLC
2+
* Copyright 2024 Google LLC
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

google-cloud-datastore/src/test/java/com/google/cloud/datastore/DatastoreTest.java renamed to google-cloud-datastore/src/test/java/com/google/cloud/datastore/AbstractDatastoreTest.java

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015 Google LLC
2+
* Copyright 2024 Google LLC
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -67,7 +67,6 @@
6767
import com.google.datastore.v1.RunQueryResponse;
6868
import com.google.datastore.v1.TransactionOptions;
6969
import com.google.protobuf.ByteString;
70-
import java.io.IOException;
7170
import java.util.ArrayList;
7271
import java.util.Arrays;
7372
import java.util.Collections;
@@ -79,21 +78,18 @@
7978
import java.util.Set;
8079
import java.util.function.Predicate;
8180
import org.easymock.EasyMock;
82-
import org.junit.AfterClass;
8381
import org.junit.Assert;
8482
import org.junit.Before;
85-
import org.junit.BeforeClass;
8683
import org.junit.Test;
8784
import org.junit.runner.RunWith;
8885
import org.junit.runners.JUnit4;
89-
import org.threeten.bp.Duration;
9086

9187
@RunWith(JUnit4.class)
92-
public class DatastoreTest {
88+
public abstract class AbstractDatastoreTest {
9389

9490
private static final LocalDatastoreHelper helper = LocalDatastoreHelper.create(1.0, 9090);
95-
private static DatastoreOptions options = helper.getOptions();
96-
private static Datastore datastore;
91+
protected static DatastoreOptions options = helper.getOptions();
92+
protected static Datastore datastore;
9793
private static final String PROJECT_ID = options.getProjectId();
9894
private static final String KIND1 = "kind1";
9995
private static final String KIND2 = "kind2";
@@ -166,11 +162,9 @@ public class DatastoreTest {
166162
private DatastoreRpcFactory rpcFactoryMock;
167163
private DatastoreRpc rpcMock;
168164

169-
@BeforeClass
170-
public static void beforeClass() throws IOException, InterruptedException {
171-
helper.start();
172-
options = helper.getOptions();
173-
datastore = options.getService();
165+
public AbstractDatastoreTest(DatastoreOptions options, Datastore datastore) {
166+
this.options = options;
167+
this.datastore = datastore;
174168
}
175169

176170
@Before
@@ -190,12 +184,6 @@ public void setUp() {
190184
datastore.add(ENTITY1, ENTITY2);
191185
}
192186

193-
@AfterClass
194-
public static void afterClass() throws Exception {
195-
datastore.close();
196-
helper.stop(Duration.ofMinutes(1));
197-
}
198-
199187
@Test
200188
public void testGetOptions() {
201189
assertSame(options, datastore.getOptions());

google-cloud-datastore/src/test/java/com/google/cloud/datastore/DatastoreOptionsTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015 Google LLC
2+
* Copyright 2024 Google LLC
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.cloud.datastore;
18+
19+
import com.google.cloud.datastore.testing.LocalDatastoreHelper;
20+
import com.google.cloud.grpc.GrpcTransportOptions;
21+
import com.google.common.truth.Truth;
22+
import java.io.IOException;
23+
import java.util.Arrays;
24+
import org.junit.AfterClass;
25+
import org.junit.BeforeClass;
26+
import org.junit.runner.RunWith;
27+
import org.junit.runners.Parameterized;
28+
import org.threeten.bp.Duration;
29+
30+
@RunWith(Parameterized.class)
31+
public class DatastoreTestGrpc extends AbstractDatastoreTest {
32+
33+
private static final LocalDatastoreHelper helper = LocalDatastoreHelper.create(1.0, 9090);
34+
35+
private static DatastoreOptions options =
36+
helper.getGrpcTransportOptions(GrpcTransportOptions.newBuilder().build());
37+
private static Datastore datastore = options.getService();
38+
39+
public DatastoreTestGrpc(DatastoreOptions options, Datastore datastore) {
40+
super(options, datastore);
41+
}
42+
43+
@Parameterized.Parameters(name = "data options: {0}")
44+
public static Iterable<Object[]> data() {
45+
return Arrays.asList(new Object[][] {{options, datastore}});
46+
}
47+
48+
@BeforeClass
49+
public static void beforeClass() throws IOException, InterruptedException {
50+
helper.start();
51+
options = helper.getGrpcTransportOptions(GrpcTransportOptions.newBuilder().build());
52+
datastore = options.getService();
53+
}
54+
55+
@AfterClass
56+
public static void afterClass() throws Exception {
57+
datastore.close();
58+
Truth.assertThat(datastore.isClosed()).isTrue();
59+
helper.stop(Duration.ofMinutes(1));
60+
}
61+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2015 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.cloud.datastore;
18+
19+
import com.google.cloud.datastore.testing.LocalDatastoreHelper;
20+
import com.google.cloud.grpc.GrpcTransportOptions;
21+
import java.io.IOException;
22+
import java.util.Arrays;
23+
import org.junit.AfterClass;
24+
import org.junit.BeforeClass;
25+
import org.junit.runner.RunWith;
26+
import org.junit.runners.Parameterized;
27+
import org.threeten.bp.Duration;
28+
29+
@RunWith(Parameterized.class)
30+
public class DatastoreTestHttp extends AbstractDatastoreTest {
31+
32+
private static final LocalDatastoreHelper helper = LocalDatastoreHelper.create(1.0, 9090);
33+
34+
private static DatastoreOptions options = helper.getOptions();
35+
private static Datastore datastore = options.getService();
36+
37+
public DatastoreTestHttp(DatastoreOptions options, Datastore datastore) {
38+
super(options, datastore);
39+
}
40+
41+
@Parameterized.Parameters(name = "data options: {0}")
42+
public static Iterable<Object[]> data() {
43+
return Arrays.asList(new Object[][] {{options, datastore}});
44+
}
45+
46+
@BeforeClass
47+
public static void beforeClass() throws IOException, InterruptedException {
48+
helper.start();
49+
options = helper.getGrpcTransportOptions(GrpcTransportOptions.newBuilder().build());
50+
datastore = options.getService();
51+
}
52+
53+
@AfterClass
54+
public static void afterClass() throws Exception {
55+
helper.stop(Duration.ofMinutes(1));
56+
}
57+
}

google-cloud-datastore/src/test/java/com/google/cloud/datastore/testing/ITLocalDatastoreHelperTest.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015 Google LLC
2+
* Copyright 2024 Google LLC
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,19 +16,23 @@
1616

1717
package com.google.cloud.datastore.testing;
1818

19+
import static com.google.common.truth.Truth.assertThat;
1920
import static org.junit.Assert.assertEquals;
2021
import static org.junit.Assert.assertFalse;
2122
import static org.junit.Assert.assertNotNull;
2223
import static org.junit.Assert.assertNull;
2324
import static org.junit.Assert.assertSame;
2425
import static org.junit.Assert.assertTrue;
2526

27+
import com.google.api.gax.grpc.InstantiatingGrpcChannelProvider;
2628
import com.google.cloud.NoCredentials;
2729
import com.google.cloud.datastore.Datastore;
2830
import com.google.cloud.datastore.DatastoreException;
2931
import com.google.cloud.datastore.DatastoreOptions;
3032
import com.google.cloud.datastore.Entity;
3133
import com.google.cloud.datastore.Key;
34+
import com.google.cloud.grpc.GrpcTransportOptions;
35+
import com.google.cloud.http.HttpTransportOptions;
3236
import java.io.IOException;
3337
import java.nio.file.Files;
3438
import java.nio.file.Path;
@@ -167,6 +171,23 @@ public void testOptions() {
167171
assertEquals(NAMESPACE, options.getNamespace());
168172
}
169173

174+
@Test
175+
public void testDefaultHttpTransportOptions() {
176+
LocalDatastoreHelper helper = LocalDatastoreHelper.create();
177+
DatastoreOptions options = helper.getOptions();
178+
assertThat(options.getTransportOptions()).isInstanceOf(HttpTransportOptions.class);
179+
}
180+
181+
@Test
182+
public void testSetGrpcTransportOptions() {
183+
LocalDatastoreHelper helper = LocalDatastoreHelper.create();
184+
DatastoreOptions options =
185+
helper.getGrpcTransportOptions(GrpcTransportOptions.newBuilder().build());
186+
assertThat(options.getTransportOptions()).isInstanceOf(GrpcTransportOptions.class);
187+
assertThat(options.getTransportChannelProvider())
188+
.isInstanceOf(InstantiatingGrpcChannelProvider.class);
189+
}
190+
170191
@Test
171192
public void testStartStopReset() throws IOException, InterruptedException, TimeoutException {
172193
try {

0 commit comments

Comments
 (0)