Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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;
Expand All @@ -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";
Expand Down Expand Up @@ -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
Expand All @@ -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());
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Object[]> 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));
}
}
Original file line number Diff line number Diff line change
@@ -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<Object[]> 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));
}
}
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -16,19 +16,23 @@

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;
import static org.junit.Assert.assertNull;
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;
Expand Down Expand Up @@ -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 {
Expand Down