Skip to content

Commit

Permalink
Add options(namespace) method to LocalDatastoreHelper (#936)
Browse files Browse the repository at this point in the history
Add options(namespace) method to LocalDatastoreHelper
  • Loading branch information
mziccard authored and aozarov committed Apr 18, 2016
1 parent d18e7bb commit 5e0de55
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 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 @@ -556,17 +556,28 @@ private static int findAvailablePort() {
}
}

private DatastoreOptions.Builder optionsBuilder() {
return DatastoreOptions.builder()
.projectId(projectId)
.host("localhost:" + Integer.toString(port))
.authCredentials(AuthCredentials.noAuth())
.retryParams(RetryParams.noRetries());
}

/**
* Returns a {@link DatastoreOptions} instance that sets the host to use the Datastore emulator on
* localhost.
*/
public DatastoreOptions options() {
return DatastoreOptions.builder()
.projectId(projectId)
.host("localhost:" + Integer.toString(port))
.authCredentials(AuthCredentials.noAuth())
.retryParams(RetryParams.noRetries())
.build();
return optionsBuilder().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 optionsBuilder().namespace(namespace).build();
}

/**
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 5e0de55

Please sign in to comment.