Skip to content

Commit 7edd544

Browse files
author
Ajay Kannan
committed
Renames LocalGcdHelper --> LocalDatastoreHelper and RemoteGcsHelper --> RemoteStorageHelper, also removes main()-related functions from LocalDatastoreHelper
1 parent e7994eb commit 7edd544

File tree

13 files changed

+236
-299
lines changed

13 files changed

+236
-299
lines changed

TESTING.md

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,56 +11,57 @@ This library provides tools to help write tests for code that uses the following
1111

1212
#### On your machine
1313

14-
You can test against a temporary local datastore by following these steps:
14+
You can test against a temporary local Datastore by following these steps:
1515

16-
1. Start the local datastore emulator using `LocalGcdHelper`. This can be done in two ways:
17-
- Run `LocalGcdHelper.java`'s `main` method with `START` provided as an argument, followed by optional arguments `--port=<port number>` and `--consistency=<float between 0 and 1, inclusive>`. This will create a temporary folder on your computer and bind `localhost:<port number>` for communication with the local datastore. If no port number is specified, port 8080 will be used. The consistency setting controls the fraction of Datastore writes that are immediately visible in global queries.
18-
- Use the `LocalGcdHelper.start(String projectId, int port, double consistency)` method before running your tests. For example, you can use the following code to start the local Datastore on any available port with the consistency set to 0.9:
19-
```java
20-
int port = LocalGcdHelper.findAvailablePort(LocalGcdHelper.DEFAULT_PORT);
21-
LocalGcdHelper helper = LocalGcdHelper.start("my-project-id", port, 0.9);
22-
```
16+
1. Start the local Datastore emulator before running your tests using `LocalDatastoreHelper`'s `start` method. This will create a temporary folder on your computer and bind a port for communication with the local datastore. There are two optional arguments for `start`: project ID and consistency. The consistency setting controls the fraction of Datastore writes that are immediately visible in global queries.
17+
```java
18+
// Use defaults for project ID and consistency
19+
LocalDatastoreHelper helper = LocalDatastoreHelper.start();
20+
// or explicitly provide them
21+
helper = LocalDatastoreHelper.start("my-project-id", 0.6);
22+
```
2323

24-
2. In your program, create and use a `Datastore` object with the options given by the `LocalGcdHelper` instance. For example:
24+
2. In your program, create and use a `Datastore` object with the options given by the `LocalDatastoreHelper` instance. For example:
2525
```java
26-
Datastore localDatastore = helper.options().service()
26+
Datastore localDatastore = helper.options().service();
2727
```
28+
2829
3. Run your tests.
2930

30-
4. Stop the local datastore emulator.
31-
- If you ran `LocalGcdHelper.java`'s `main` function to start the emulator, run `LocalGcdHelper.java`'s `main` method with arguments `STOP` and (optionally) `--port=<port number>`. If the port is not supplied, the program will attempt to close the last port started.
32-
- If you ran the `LocalGcdHelper.start` method to start the emulator, call the `stop()` method on the `LocalGcdHelper` object returned by `LocalGcdHelper.start`.
31+
4. Stop the local datastore emulator by calling the `stop()` method, like so:
32+
```java
33+
helper.stop();
34+
```
3335

3436
#### On a remote machine
3537

36-
You can test against a remote datastore emulator as well. To do this, set the `DatastoreOptions` project endpoint to the hostname of the remote machine, like the example below.
38+
You can test against a remote Datastore emulator as well. To do this, set the `DatastoreOptions` project endpoint to the hostname of the remote machine, like the example below.
3739

3840
```java
39-
DatastoreOptions options = DatastoreOptions.builder()
40-
.projectId(PROJECT_ID)
41+
DatastoreOptions options = LocalDatastoreHelper.options()
42+
.toBuilder()
4143
.host("http://<hostname of machine>:<port>")
42-
.authCredentials(AuthCredentials.noAuth())
4344
.build();
4445
Datastore localDatastore = options.service();
4546
```
4647

47-
Note that the remote datastore must be running before your tests are run.
48+
Note that the remote Datastore emulator must be running before your tests are run.
4849

4950
### Testing code that uses Storage
5051

51-
Currently, there isn't an emulator for Google Cloud Storage, so an alternative is to create a test project. `RemoteGcsHelper` contains convenience methods to make setting up and cleaning up the test project easier. To use this class, follow the steps below:
52+
Currently, there isn't an emulator for Google Cloud Storage, so an alternative is to create a test project. `RemoteStorageHelper` contains convenience methods to make setting up and cleaning up the test project easier. To use this class, follow the steps below:
5253

5354
1. Create a test Google Cloud project.
5455

5556
2. Download a JSON service account credentials file from the Google Developer's Console. See more about this on the [Google Cloud Platform Storage Authentication page][cloud-platform-storage-authentication].
5657

57-
3. Create a `RemoteGcsHelper` object using your project ID and JSON key.
58-
Here is an example that uses the `RemoteGcsHelper` to create a bucket.
58+
3. Create a `RemoteStorageHelper` object using your project ID and JSON key.
59+
Here is an example that uses the `RemoteStorageHelper` to create a bucket.
5960
```java
60-
RemoteGcsHelper gcsHelper =
61-
RemoteGcsHelper.create(PROJECT_ID, new FileInputStream("/path/to/my/JSON/key.json"));
62-
Storage storage = gcsHelper.options().service();
63-
String bucket = RemoteGcsHelper.generateBucketName();
61+
RemoteStorageHelper helper =
62+
RemoteStorageHelper.create(PROJECT_ID, new FileInputStream("/path/to/my/JSON/key.json"));
63+
Storage storage = helper.options().service();
64+
String bucket = RemoteStorageHelper.generateBucketName();
6465
storage.create(BucketInfo.of(bucket));
6566
```
6667

@@ -69,7 +70,7 @@ Here is an example that uses the `RemoteGcsHelper` to create a bucket.
6970
5. Clean up the test project by using `forceDelete` to clear any buckets used.
7071
Here is an example that clears the bucket created in Step 3 with a timeout of 5 seconds.
7172
```java
72-
RemoteGcsHelper.forceDelete(storage, bucket, 5, TimeUnit.SECONDS);
73+
RemoteStorageHelper.forceDelete(storage, bucket, 5, TimeUnit.SECONDS);
7374
```
7475

7576
### Testing code that uses Resource Manager

gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/it/ITBigQueryTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
import com.google.gcloud.storage.BlobInfo;
6868
import com.google.gcloud.storage.BucketInfo;
6969
import com.google.gcloud.storage.Storage;
70-
import com.google.gcloud.storage.testing.RemoteGcsHelper;
70+
import com.google.gcloud.storage.testing.RemoteStorageHelper;
7171

7272
import org.junit.AfterClass;
7373
import org.junit.BeforeClass;
@@ -134,7 +134,7 @@ public class ITBigQueryTest {
134134
private static final String LOAD_FILE = "load.csv";
135135
private static final String JSON_LOAD_FILE = "load.json";
136136
private static final String EXTRACT_FILE = "extract.csv";
137-
private static final String BUCKET = RemoteGcsHelper.generateBucketName();
137+
private static final String BUCKET = RemoteStorageHelper.generateBucketName();
138138
private static final TableId TABLE_ID = TableId.of(DATASET, "testing_table");
139139
private static final String CSV_CONTENT = "StringValue1\nStringValue2\n";
140140
private static final String JSON_CONTENT = "{"
@@ -171,7 +171,7 @@ public class ITBigQueryTest {
171171
@BeforeClass
172172
public static void beforeClass() throws InterruptedException {
173173
RemoteBigQueryHelper bigqueryHelper = RemoteBigQueryHelper.create();
174-
RemoteGcsHelper gcsHelper = RemoteGcsHelper.create();
174+
RemoteStorageHelper gcsHelper = RemoteStorageHelper.create();
175175
bigquery = bigqueryHelper.options().service();
176176
storage = gcsHelper.options().service();
177177
storage.create(BucketInfo.of(BUCKET));
@@ -199,7 +199,7 @@ public static void afterClass() throws ExecutionException, InterruptedException
199199
RemoteBigQueryHelper.forceDelete(bigquery, DATASET);
200200
}
201201
if (storage != null) {
202-
boolean wasDeleted = RemoteGcsHelper.forceDelete(storage, BUCKET, 10, TimeUnit.SECONDS);
202+
boolean wasDeleted = RemoteStorageHelper.forceDelete(storage, BUCKET, 10, TimeUnit.SECONDS);
203203
if (!wasDeleted && LOG.isLoggable(Level.WARNING)) {
204204
LOG.log(Level.WARNING, "Deletion of bucket {0} timed out, bucket is not empty", BUCKET);
205205
}

gcloud-java-core/src/main/java/com/google/gcloud/AuthCredentials.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ public RestorableState<AuthCredentials> capture() {
260260
/**
261261
* A placeholder for credentials to signify that requests sent to the server should not be
262262
* authenticated. This is typically useful when using the local service emulators, such as
263-
* {@code LocalGcdHelper} and {@code LocalResourceManagerHelper}.
263+
* {@code LocalDatastoreHelper} and {@code LocalResourceManagerHelper}.
264264
*/
265265
public static class NoAuthCredentials extends AuthCredentials {
266266

@@ -341,7 +341,7 @@ public static ServiceAccountAuthCredentials createFor(String account, PrivateKey
341341

342342
/**
343343
* Creates a placeholder denoting that no credentials should be used. This is typically useful
344-
* when using the local service emulators, such as {@code LocalGcdHelper} and
344+
* when using the local service emulators, such as {@code LocalDatastoreHelper} and
345345
* {@code LocalResourceManagerHelper}.
346346
*/
347347
public static AuthCredentials noAuth() {

0 commit comments

Comments
 (0)