Skip to content

Commit

Permalink
Add options(namespace) method to LocalDatastoreHelper
Browse files Browse the repository at this point in the history
  • Loading branch information
mziccard committed Apr 18, 2016
1 parent 9de36ed commit 91bd460
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ public DatastoreOptions build() {
return new DatastoreOptions(this);
}

/**
* Sets the default namespace to be used by the datastore service.
*/
public Builder namespace(String namespace) {
this.namespace = validateNamespace(namespace);
return this;
Expand Down Expand Up @@ -112,6 +115,9 @@ protected DatastoreRpcFactory defaultRpcFactory() {
return DefaultDatastoreRpcFactory.INSTANCE;
}

/**
* Returns the default namespace to be used by the datastore service.
*/
public String namespace() {
return namespace;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,20 @@ public DatastoreOptions options() {
.build();
}

/**
* Returns a {@link DatastoreOptions} instance that sets the host to use the Datastore emulator on
* localhost. The default namespace is set to {@code namespace}.
*/
public DatastoreOptions options(String namespace) {
return DatastoreOptions.builder()
.projectId(projectId)
.host("localhost:" + Integer.toString(port))
.authCredentials(AuthCredentials.noAuth())
.retryParams(RetryParams.noRetries())
.namespace(namespace)
.build();
}

/**
* Returns the project ID associated with this local Datastore emulator.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.google.cloud.datastore.testing;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;

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

private static final double TOLERANCE = 0.00001;
private static final String PROJECT_ID_PREFIX = "test-project-";
private static final String NAMESPACE = "namespace";

@Test
public void testCreate() {
Expand All @@ -49,5 +51,10 @@ public void testOptions() {
assertTrue(options.projectId().startsWith(PROJECT_ID_PREFIX));
assertTrue(options.host().startsWith("localhost:"));
assertSame(AuthCredentials.noAuth(), options.authCredentials());
options = helper.options(NAMESPACE);
assertTrue(options.projectId().startsWith(PROJECT_ID_PREFIX));
assertTrue(options.host().startsWith("localhost:"));
assertSame(AuthCredentials.noAuth(), options.authCredentials());
assertEquals(NAMESPACE, options.namespace());
}
}

0 comments on commit 91bd460

Please sign in to comment.