Skip to content

Commit 5e0de55

Browse files
mziccardaozarov
authored andcommitted
Add options(namespace) method to LocalDatastoreHelper (googleapis#936)
Add options(namespace) method to LocalDatastoreHelper
1 parent d18e7bb commit 5e0de55

File tree

3 files changed

+30
-6
lines changed

3 files changed

+30
-6
lines changed

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

+6
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

+17-6
Original file line numberDiff line numberDiff line change
@@ -556,17 +556,28 @@ private static int findAvailablePort() {
556556
}
557557
}
558558

559+
private DatastoreOptions.Builder optionsBuilder() {
560+
return DatastoreOptions.builder()
561+
.projectId(projectId)
562+
.host("localhost:" + Integer.toString(port))
563+
.authCredentials(AuthCredentials.noAuth())
564+
.retryParams(RetryParams.noRetries());
565+
}
566+
559567
/**
560568
* Returns a {@link DatastoreOptions} instance that sets the host to use the Datastore emulator on
561569
* localhost.
562570
*/
563571
public DatastoreOptions options() {
564-
return DatastoreOptions.builder()
565-
.projectId(projectId)
566-
.host("localhost:" + Integer.toString(port))
567-
.authCredentials(AuthCredentials.noAuth())
568-
.retryParams(RetryParams.noRetries())
569-
.build();
572+
return optionsBuilder().build();
573+
}
574+
575+
/**
576+
* Returns a {@link DatastoreOptions} instance that sets the host to use the Datastore emulator on
577+
* localhost. The default namespace is set to {@code namespace}.
578+
*/
579+
public DatastoreOptions options(String namespace) {
580+
return optionsBuilder().namespace(namespace).build();
570581
}
571582

572583
/**

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

+7
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)