Skip to content

Commit a6bd1a2

Browse files
committed
fix(datastore): update tests after major version upgrade and remove deprecated methods
TAG=agy CONV=293474b3-0d10-47f5-89ba-79a4d13bf1bb
1 parent 147d7e9 commit a6bd1a2

12 files changed

Lines changed: 32 additions & 298 deletions

File tree

java-datastore/datastore-v1-proto-client/src/main/java/com/google/datastore/v1/client/DatastoreEmulator.java

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
package com.google.datastore.v1.client;
1717

18-
import static com.google.api.client.util.Preconditions.checkNotNull;
1918
import static com.google.api.client.util.Preconditions.checkState;
2019

2120
import java.io.BufferedReader;
@@ -102,37 +101,6 @@ public void clear() throws DatastoreEmulatorException {
102101
sendEmptyRequest("/reset", "POST");
103102
}
104103

105-
/**
106-
* Starts the emulator. It is the caller's responsibility to call {@link #stop}. Note that
107-
* receiving an exception does not indicate that the server did not start. We recommend calling
108-
* {@link #stop} to ensure the server is not running regardless of the result of this method.
109-
*
110-
* @param emulatorDir The path to the emulator directory, e.g. /usr/local/cloud-datastore-emulator
111-
* @param projectId The project ID
112-
* @param commandLineOptions Command line options to pass to the emulator on startup
113-
* @throws DatastoreEmulatorException If {@link #start} has already been called or the server does
114-
* not start successfully.
115-
* @deprecated prefer setting options in the emulator options and calling {#start()}.
116-
*/
117-
@Deprecated
118-
public synchronized void start(String emulatorDir, String projectId, String... commandLineOptions)
119-
throws DatastoreEmulatorException {
120-
checkNotNull(emulatorDir, "emulatorDir cannot be null");
121-
checkNotNull(projectId, "projectId cannot be null");
122-
checkState(state == State.NEW, "Cannot call start() more than once.");
123-
try {
124-
startEmulatorInternal(
125-
emulatorDir + "/cloud_datastore_emulator", projectId, Arrays.asList(commandLineOptions));
126-
state = State.STARTED;
127-
} finally {
128-
if (state != State.STARTED) {
129-
// If we're not able to start the server we don't want people trying again. Just move it
130-
// straight to the STOPPED state.
131-
state = State.STOPPED;
132-
}
133-
}
134-
}
135-
136104
public synchronized void start() throws DatastoreEmulatorException {
137105
checkState(state == State.NEW, "Cannot call start() more than once.");
138106
try {

java-datastore/datastore-v1-proto-client/src/main/java/com/google/datastore/v1/client/DatastoreHelper.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,8 +323,23 @@ private static void setProjectEndpointFromEnv(DatastoreOptions.Builder options)
323323
}
324324
String projectId = getProjectIdFromEnv();
325325
if (System.getenv(URL_OVERRIDE_ENV_VAR) != null) {
326-
options.projectEndpoint(
327-
String.format("%s/projects/%s", System.getenv(URL_OVERRIDE_ENV_VAR), projectId));
326+
String urlOverride = System.getenv(URL_OVERRIDE_ENV_VAR);
327+
options.projectId(projectId);
328+
// The projectEndpoint builder method was removed. Use host or localHost instead.
329+
// Since host and localHost methods don't accept a scheme, we strip it if present.
330+
// We then check if it's a local host to use options.localHost(...) for the emulator,
331+
// or options.host(...) for actual calls.
332+
String host = urlOverride;
333+
if (host.startsWith("http://")) {
334+
host = host.substring("http://".length());
335+
} else if (host.startsWith("https://")) {
336+
host = host.substring("https://".length());
337+
}
338+
if (host.startsWith("localhost") || host.startsWith("127.0.0.1")) {
339+
options.localHost(host);
340+
} else {
341+
options.host(host);
342+
}
328343
return;
329344
}
330345
if (System.getenv(LOCAL_HOST_ENV_VAR) != null) {

java-datastore/datastore-v1-proto-client/src/main/java/com/google/datastore/v1/client/DatastoreOptions.java

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -132,24 +132,6 @@ public Builder localHost(String localHost) {
132132
return this;
133133
}
134134

135-
/**
136-
* Sets the project endpoint used to access Cloud Datastore. Prefer using {@link #projectId}
137-
* and/or {@link #host}/{@link #localHost} when possible.
138-
*
139-
* @deprecated Use {@link #projectId} and/or {@link #host}/{@link #localHost} instead.
140-
*/
141-
@Deprecated
142-
public Builder projectEndpoint(String projectEndpoint) {
143-
checkArgument(projectId == null, PROJECT_ENDPOINT_AND_PROJECT_ID_ERROR);
144-
checkArgument(localHost == null && host == null, PROJECT_ENDPOINT_AND_HOST_ERROR);
145-
if (!includesScheme(projectEndpoint)) {
146-
throw new IllegalArgumentException(
147-
String.format("Project endpoint \"%s\" must include scheme.", projectEndpoint));
148-
}
149-
this.projectEndpoint = projectEndpoint;
150-
return this;
151-
}
152-
153135
/** Sets the (optional) initializer to run on HTTP requests to Cloud Datastore. */
154136
public Builder initializer(HttpRequestInitializer initializer) {
155137
this.initializer = initializer;

java-datastore/datastore-v1-proto-client/src/test/java/com/google/datastore/v1/client/DatastoreClientTest.java

Lines changed: 0 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -76,51 +76,6 @@ public void options_NoProjectIdOrProjectEndpoint() {
7676
factory.create(options.build());
7777
}
7878

79-
@Test
80-
public void options_ProjectIdAndProjectEndpoint() throws Exception {
81-
IllegalArgumentException exception =
82-
assertThrows(
83-
IllegalArgumentException.class,
84-
() ->
85-
new DatastoreOptions.Builder()
86-
.projectId(PROJECT_ID)
87-
.projectEndpoint(
88-
"http://localhost:1234/datastore/v1beta42/projects/project-id"));
89-
assertThat(exception)
90-
.hasMessageThat()
91-
.contains("Cannot set both project endpoint and project ID");
92-
}
93-
94-
@Test
95-
public void options_LocalHostAndProjectEndpoint() throws Exception {
96-
IllegalArgumentException exception =
97-
assertThrows(
98-
IllegalArgumentException.class,
99-
() ->
100-
new DatastoreOptions.Builder()
101-
.localHost("localhost:8080")
102-
.projectEndpoint(
103-
"http://localhost:1234/datastore/v1beta42/projects/project-id"));
104-
assertThat(exception)
105-
.hasMessageThat()
106-
.contains("Can set at most one of project endpoint, host, and local host");
107-
}
108-
109-
@Test
110-
public void options_HostAndProjectEndpoint() throws Exception {
111-
IllegalArgumentException exception =
112-
assertThrows(
113-
IllegalArgumentException.class,
114-
() ->
115-
new DatastoreOptions.Builder()
116-
.host("foo-datastore.googleapis.com")
117-
.projectEndpoint(
118-
"http://localhost:1234/datastore/v1beta42/projects/project-id"));
119-
assertThat(exception)
120-
.hasMessageThat()
121-
.contains("Can set at most one of project endpoint, host, and local host");
122-
}
123-
12479
@Test
12580
public void options_HostAndLocalHost() throws Exception {
12681
IllegalArgumentException exception =
@@ -235,34 +190,6 @@ public void create_DefaultHost() {
235190
.isEqualTo("https://datastore.googleapis.com/v1/projects/project-id");
236191
}
237192

238-
@Test
239-
public void create_ProjectEndpoint() {
240-
Datastore datastore =
241-
factory.create(
242-
new DatastoreOptions.Builder()
243-
.projectEndpoint("http://prom-qa/datastore/v1beta42/projects/project-id")
244-
.build());
245-
assertThat(datastore.remoteRpc.getUrl())
246-
.isEqualTo("http://prom-qa/datastore/v1beta42/projects/project-id");
247-
}
248-
249-
@Test
250-
public void create_ProjectEndpointNoScheme() {
251-
IllegalArgumentException exception =
252-
assertThrows(
253-
IllegalArgumentException.class,
254-
() ->
255-
factory.create(
256-
new DatastoreOptions.Builder()
257-
.projectEndpoint("localhost:1234/datastore/v1beta42/projects/project-id")
258-
.build()));
259-
assertThat(exception)
260-
.hasMessageThat()
261-
.contains(
262-
"Project endpoint \"localhost:1234/datastore/v1beta42/projects/project-id\" must"
263-
+ " include scheme.");
264-
}
265-
266193
@Test
267194
public void initializer() throws Exception {
268195
options.initializer(

java-datastore/datastore-v1-proto-client/src/test/java/com/google/datastore/v1/client/DatastoreEmulatorTest.java

Lines changed: 6 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -26,39 +26,11 @@
2626
@RunWith(JUnit4.class)
2727
public class DatastoreEmulatorTest {
2828

29-
private static final DatastoreEmulatorOptions options =
30-
new DatastoreEmulatorOptions.Builder().build();
31-
32-
@Test
33-
public void testArgs() throws DatastoreEmulatorException {
34-
DatastoreEmulator datastore =
35-
new DatastoreEmulator(null, "blar", options) {
36-
@Override
37-
void startEmulatorInternal(
38-
String emulatorDir, String projectId, List<String> cmdLineOpts) {
39-
// no-op for testing
40-
}
41-
};
42-
43-
try {
44-
datastore.start(null, "projectId");
45-
fail("expected exception");
46-
} catch (NullPointerException npe) {
47-
// good
48-
}
49-
50-
try {
51-
datastore.start("path/to/emulator", null);
52-
fail("expected exception");
53-
} catch (NullPointerException npe) {
54-
// good
55-
}
56-
57-
datastore.start("path/to/emulator", "projectId");
58-
}
59-
6029
@Test
6130
public void testLifecycle() throws DatastoreEmulatorException {
31+
DatastoreEmulatorOptions options =
32+
new DatastoreEmulatorOptions.Builder().setCommand("/yar").setProjectId("myproject").build();
33+
6234
DatastoreEmulator datastore =
6335
new DatastoreEmulator(null, "blar", options) {
6436
@Override
@@ -73,12 +45,9 @@ protected void stopEmulatorInternal() {
7345
}
7446
};
7547

76-
String emulatorDir = "/yar";
77-
String myProject = "myproject";
78-
79-
datastore.start(emulatorDir, myProject);
48+
datastore.start();
8049
try {
81-
datastore.start(emulatorDir, myProject);
50+
datastore.start();
8251
fail("expected exception");
8352
} catch (IllegalStateException e) {
8453
// good
@@ -90,7 +59,7 @@ protected void stopEmulatorInternal() {
9059

9160
// Once we've stopped we can't start again.
9261
try {
93-
datastore.start(emulatorDir, myProject);
62+
datastore.start();
9463
fail("expected exception");
9564
} catch (IllegalStateException e) {
9665
// good

java-datastore/google-cloud-datastore-utils/src/main/java/com/google/datastore/utils/DatastoreHelper.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -325,8 +325,7 @@ private static void setProjectEndpointFromEnv(DatastoreOptions.Builder options)
325325
}
326326
String projectId = getProjectIdFromEnv();
327327
if (System.getenv(URL_OVERRIDE_ENV_VAR) != null) {
328-
options.projectEndpoint(
329-
String.format("%s/projects/%s", System.getenv(URL_OVERRIDE_ENV_VAR), projectId));
328+
options.host(String.format("%s/projects/%s", System.getenv(URL_OVERRIDE_ENV_VAR), projectId));
330329
return;
331330
}
332331
if (System.getenv(LOCAL_HOST_ENV_VAR) != null) {
@@ -335,7 +334,6 @@ private static void setProjectEndpointFromEnv(DatastoreOptions.Builder options)
335334
return;
336335
}
337336
options.projectId(projectId);
338-
return;
339337
}
340338

341339
/**

java-datastore/google-cloud-datastore-utils/src/main/java/com/google/datastore/utils/DatastoreOptions.java

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -133,24 +133,6 @@ public Builder localHost(String localHost) {
133133
return this;
134134
}
135135

136-
/**
137-
* Sets the project endpoint used to access Cloud Datastore. Prefer using {@link #projectId}
138-
* and/or {@link #host}/{@link #localHost} when possible.
139-
*
140-
* @deprecated Use {@link #projectId} and/or {@link #host}/{@link #localHost} instead.
141-
*/
142-
@Deprecated
143-
public Builder projectEndpoint(String projectEndpoint) {
144-
checkArgument(projectId == null, PROJECT_ENDPOINT_AND_PROJECT_ID_ERROR);
145-
checkArgument(localHost == null && host == null, PROJECT_ENDPOINT_AND_HOST_ERROR);
146-
if (!includesScheme(projectEndpoint)) {
147-
throw new IllegalArgumentException(
148-
String.format("Project endpoint \"%s\" must include scheme.", projectEndpoint));
149-
}
150-
this.projectEndpoint = projectEndpoint;
151-
return this;
152-
}
153-
154136
/** Sets the (optional) initializer to run on HTTP requests to Cloud Datastore. */
155137
public Builder initializer(HttpRequestInitializer initializer) {
156138
this.initializer = initializer;

java-datastore/google-cloud-datastore-utils/src/test/java/com/google/datastore/utils/DatastoreClientTest.java

Lines changed: 2 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@
6060
public class DatastoreClientTest {
6161
private static final String PROJECT_ID = "project-id";
6262

63-
private DatastoreFactory factory = new MockDatastoreFactory();
64-
private DatastoreOptions.Builder options =
63+
private final DatastoreFactory factory = new MockDatastoreFactory();
64+
private final DatastoreOptions.Builder options =
6565
new DatastoreOptions.Builder().projectId(PROJECT_ID).credential(new MockCredential());
6666

6767
@Test
@@ -76,51 +76,6 @@ public void options_NoProjectIdOrProjectEndpoint() {
7676
factory.create(options.build());
7777
}
7878

79-
@Test
80-
public void options_ProjectIdAndProjectEndpoint() throws Exception {
81-
IllegalArgumentException exception =
82-
assertThrows(
83-
IllegalArgumentException.class,
84-
() ->
85-
new DatastoreOptions.Builder()
86-
.projectId(PROJECT_ID)
87-
.projectEndpoint(
88-
"http://localhost:1234/datastore/v1beta42/projects/project-id"));
89-
assertThat(exception)
90-
.hasMessageThat()
91-
.contains("Cannot set both project endpoint and project ID");
92-
}
93-
94-
@Test
95-
public void options_LocalHostAndProjectEndpoint() throws Exception {
96-
IllegalArgumentException exception =
97-
assertThrows(
98-
IllegalArgumentException.class,
99-
() ->
100-
new DatastoreOptions.Builder()
101-
.localHost("localhost:8080")
102-
.projectEndpoint(
103-
"http://localhost:1234/datastore/v1beta42/projects/project-id"));
104-
assertThat(exception)
105-
.hasMessageThat()
106-
.contains("Can set at most one of project endpoint, host, and local host");
107-
}
108-
109-
@Test
110-
public void options_HostAndProjectEndpoint() throws Exception {
111-
IllegalArgumentException exception =
112-
assertThrows(
113-
IllegalArgumentException.class,
114-
() ->
115-
new DatastoreOptions.Builder()
116-
.host("foo-datastore.googleapis.com")
117-
.projectEndpoint(
118-
"http://localhost:1234/datastore/v1beta42/projects/project-id"));
119-
assertThat(exception)
120-
.hasMessageThat()
121-
.contains("Can set at most one of project endpoint, host, and local host");
122-
}
123-
12479
@Test
12580
public void options_HostAndLocalHost() throws Exception {
12681
IllegalArgumentException exception =
@@ -235,34 +190,6 @@ public void create_DefaultHost() {
235190
.isEqualTo("https://datastore.googleapis.com/v1/projects/project-id");
236191
}
237192

238-
@Test
239-
public void create_ProjectEndpoint() {
240-
Datastore datastore =
241-
factory.create(
242-
new DatastoreOptions.Builder()
243-
.projectEndpoint("http://prom-qa/datastore/v1beta42/projects/project-id")
244-
.build());
245-
assertThat(datastore.remoteRpc.getUrl())
246-
.isEqualTo("http://prom-qa/datastore/v1beta42/projects/project-id");
247-
}
248-
249-
@Test
250-
public void create_ProjectEndpointNoScheme() {
251-
IllegalArgumentException exception =
252-
assertThrows(
253-
IllegalArgumentException.class,
254-
() ->
255-
factory.create(
256-
new DatastoreOptions.Builder()
257-
.projectEndpoint("localhost:1234/datastore/v1beta42/projects/project-id")
258-
.build()));
259-
assertThat(exception)
260-
.hasMessageThat()
261-
.contains(
262-
"Project endpoint \"localhost:1234/datastore/v1beta42/projects/project-id\" must"
263-
+ " include scheme.");
264-
}
265-
266193
@Test
267194
public void initializer() throws Exception {
268195
options.initializer(

0 commit comments

Comments
 (0)