Skip to content

Commit 91bd460

Browse files
committed
Add options(namespace) method to LocalDatastoreHelper
1 parent 9de36ed commit 91bd460

3 files changed

Lines changed: 27 additions & 0 deletions

File tree

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ public DatastoreOptions build() {
7575
return new DatastoreOptions(this);
7676
}
7777

78+
/**
79+
* Sets the default namespace to be used by the datastore service.
80+
*/
7881
public Builder namespace(String namespace) {
7982
this.namespace = validateNamespace(namespace);
8083
return this;
@@ -112,6 +115,9 @@ protected DatastoreRpcFactory defaultRpcFactory() {
112115
return DefaultDatastoreRpcFactory.INSTANCE;
113116
}
114117

118+
/**
119+
* Returns the default namespace to be used by the datastore service.
120+
*/
115121
public String namespace() {
116122
return namespace;
117123
}

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,20 @@ public DatastoreOptions options() {
569569
.build();
570570
}
571571

572+
/**
573+
* Returns a {@link DatastoreOptions} instance that sets the host to use the Datastore emulator on
574+
* localhost. The default namespace is set to {@code namespace}.
575+
*/
576+
public DatastoreOptions options(String namespace) {
577+
return DatastoreOptions.builder()
578+
.projectId(projectId)
579+
.host("localhost:" + Integer.toString(port))
580+
.authCredentials(AuthCredentials.noAuth())
581+
.retryParams(RetryParams.noRetries())
582+
.namespace(namespace)
583+
.build();
584+
}
585+
572586
/**
573587
* Returns the project ID associated with this local Datastore emulator.
574588
*/

gcloud-java-datastore/src/test/java/com/google/cloud/datastore/testing/LocalDatastoreHelperTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

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

19+
import static org.junit.Assert.assertEquals;
1920
import static org.junit.Assert.assertSame;
2021
import static org.junit.Assert.assertTrue;
2122

@@ -31,6 +32,7 @@ public class LocalDatastoreHelperTest {
3132

3233
private static final double TOLERANCE = 0.00001;
3334
private static final String PROJECT_ID_PREFIX = "test-project-";
35+
private static final String NAMESPACE = "namespace";
3436

3537
@Test
3638
public void testCreate() {
@@ -49,5 +51,10 @@ public void testOptions() {
4951
assertTrue(options.projectId().startsWith(PROJECT_ID_PREFIX));
5052
assertTrue(options.host().startsWith("localhost:"));
5153
assertSame(AuthCredentials.noAuth(), options.authCredentials());
54+
options = helper.options(NAMESPACE);
55+
assertTrue(options.projectId().startsWith(PROJECT_ID_PREFIX));
56+
assertTrue(options.host().startsWith("localhost:"));
57+
assertSame(AuthCredentials.noAuth(), options.authCredentials());
58+
assertEquals(NAMESPACE, options.namespace());
5259
}
5360
}

0 commit comments

Comments
 (0)