Skip to content

Commit b40bf53

Browse files
author
Ajay Kannan
committed
Merge to fix conflict from recent README.md updates
2 parents 8cca008 + 76bf376 commit b40bf53

20 files changed

Lines changed: 194 additions & 18 deletions

File tree

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Add this to your pom.xml file
3030
<dependency>
3131
<groupId>com.google.gcloud</groupId>
3232
<artifactId>gcloud-java</artifactId>
33-
<version>0.0.7</version>
33+
<version>0.0.9</version>
3434
</dependency>
3535
```
3636

@@ -127,6 +127,13 @@ if (!blob.exists()) {
127127
}
128128
```
129129

130+
Testing
131+
-------
132+
133+
This library provides tools to help write tests for code that uses gcloud-java services.
134+
135+
See [TESTING] to read more about using our testing helpers.
136+
130137
Contributing
131138
------------
132139

@@ -159,6 +166,7 @@ Apache 2.0 - See [LICENSE] for more information.
159166
[CONTRIBUTING]:https://github.com/GoogleCloudPlatform/gcloud-java/blob/master/CONTRIBUTING.md
160167
[code-of-conduct]:https://github.com/GoogleCloudPlatform/gcloud-java/blob/master/CODE_OF_CONDUCT.md
161168
[LICENSE]: https://github.com/GoogleCloudPlatform/gcloud-java/blob/master/LICENSE
169+
[TESTING]: https://github.com/GoogleCloudPlatform/gcloud-java/blob/master/TESTING.md
162170
[cloud-platform]: https://cloud.google.com/
163171
[cloud-datastore]: https://cloud.google.com/datastore/docs
164172
[cloud-datastore-docs]: https://cloud.google.com/datastore/docs

TESTING.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
## gcloud-java tools for testing
2+
3+
This library provides tools to help write tests for code that uses gcloud-java services.
4+
5+
### Testing code that uses Datastore
6+
7+
#### On your machine
8+
9+
You can test against a temporary local datastore by following these steps:
10+
11+
1. Start the local datastore emulator using `LocalGcdHelper`. This can be done in two ways:
12+
- Run `LocalGcdHelper.java`'s `main` method with arguments `START` and (optionally) `--port=<port number>`. This will create a temporary folder on your computer and bind `localhost:<port number>` for communication with the local datastore. The port number is an optional argument. If no port number is specified, port 8080 will be used.
13+
- Call `LocalGcdHelper.start(<project ID>, <port number>)` before running your tests. Save the `LocalGcdHelper` object returned so that you can stop the emulator later.
14+
15+
2. In your program, create and use a datastore whose host is set host to `localhost:<port number>`. For example,
16+
```java
17+
DatastoreOptions options = DatastoreOptions.builder()
18+
.projectId(PROJECT_ID)
19+
.host("http://localhost:8080")
20+
.build();
21+
Datastore localDatastore = DatastoreFactory.instance().get(options);
22+
```
23+
3. Run your tests.
24+
25+
4. Stop the local datastore emulator.
26+
- 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.
27+
- If you ran `LocalGcdHelper.start()` to start the emulator, call the `stop()` method on the `LocalGcdHelper` object returned by `LocalGcdHelper.start()`.
28+
29+
#### On a remote machine
30+
31+
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.
32+
33+
```java
34+
DatastoreOptions options = DatastoreOptions.builder()
35+
.projectId(PROJECT_ID)
36+
.host("http://<hostname of machine>:<port>")
37+
.build();
38+
Datastore localDatastore = DatastoreFactory.instance().get(options);
39+
```
40+
41+
Note that the remote datastore must be running before your tests are run.
42+
43+
### Testing code that uses Storage
44+
45+
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:
46+
47+
1. Create a test Google Cloud project.
48+
49+
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].
50+
51+
3. Create a `RemoteGcsHelper` object using your project ID and JSON key.
52+
Here is an example that uses the `RemoteGcsHelper` to create a bucket.
53+
```java
54+
RemoteGcsHelper gcsHelper = RemoteGcsHelper.create(PROJECT_ID, "/path/to/my/JSON/key.json");
55+
Storage storage = StorageFactory.instance().get(gcsHelper.options());
56+
String bucket = RemoteGcsHelper.generateBucketName();
57+
storage.create(BucketInfo.of(bucket));
58+
```
59+
60+
4. Run your tests.
61+
62+
5. Clean up the test project by using `forceDelete` to clear any buckets used.
63+
Here is an example that clears the bucket created in Step 3 with a timeout of 5 seconds.
64+
```java
65+
RemoteGcsHelper.forceDelete(storage, bucket, 5, TimeUnit.SECONDS);
66+
```
67+
68+
69+
[cloud-platform-storage-authentication]:https://cloud.google.com/storage/docs/authentication?hl=en#service_accounts

gcloud-java-core/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Add this to your pom.xml file
1717
<dependency>
1818
<groupId>com.google.gcloud</groupId>
1919
<artifactId>gcloud-java-core</artifactId>
20-
<version>0.0.7</version>
20+
<version>0.0.9</version>
2121
</dependency>
2222
```
2323

gcloud-java-core/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<parent>
1212
<groupId>com.google.gcloud</groupId>
1313
<artifactId>gcloud-java-pom</artifactId>
14-
<version>0.0.8-SNAPSHOT</version>
14+
<version>0.0.9</version>
1515
</parent>
1616
<dependencies>
1717
<dependency>

gcloud-java-datastore/README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Add this to your pom.xml file
2020
<dependency>
2121
<groupId>com.google.gcloud</groupId>
2222
<artifactId>gcloud-java-datastore</artifactId>
23-
<version>0.0.7</version>
23+
<version>0.0.9</version>
2424
</dependency>
2525
```
2626

@@ -66,6 +66,13 @@ if (entity == null) {
6666
}
6767
```
6868

69+
Testing
70+
-------
71+
72+
This library has tools to help write tests for code that uses the Datastore.
73+
74+
See [TESTING] to read more about testing.
75+
6976
Contributing
7077
------------
7178

@@ -95,6 +102,7 @@ Apache 2.0 - See [LICENSE] for more information.
95102

96103
[CONTRIBUTING]:https://github.com/GoogleCloudPlatform/gcloud-java/blob/master/CONTRIBUTING.md
97104
[LICENSE]: https://github.com/GoogleCloudPlatform/gcloud-java/blob/master/LICENSE
105+
[TESTING]: https://github.com/GoogleCloudPlatform/gcloud-java/blob/master/TESTING.md#testing-code-that-uses-datastore
98106
[cloud-platform]: https://cloud.google.com/
99107
[cloud-datastore]: https://cloud.google.com/datastore/docs
100108
[cloud-datastore-docs]: https://cloud.google.com/datastore/docs

gcloud-java-datastore/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<parent>
1212
<groupId>com.google.gcloud</groupId>
1313
<artifactId>gcloud-java-pom</artifactId>
14-
<version>0.0.8-SNAPSHOT</version>
14+
<version>0.0.9</version>
1515
</parent>
1616
<dependencies>
1717
<dependency>

gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/LocalGcdHelper.java renamed to gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/testing/LocalGcdHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package com.google.gcloud.datastore;
17+
package com.google.gcloud.datastore.testing;
1818

1919
import static java.nio.charset.StandardCharsets.UTF_8;
2020

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2015 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/**
18+
* A testing helper for Google Cloud Datastore.
19+
*
20+
* <p>A simple usage example:
21+
* <p>Before the test:
22+
* <pre> {@code
23+
* LocalGcdHelper gcdHelper = LocalGcdHelper.start(PROJECT_ID, PORT_NUMBER);
24+
* DatastoreOptions options = DatastoreOptions.builder()
25+
* .projectId(PROJECT_ID)
26+
* .host("localhost:8080")
27+
* .build();
28+
* Datastore localDatastore = DatastoreFactory.instance().get(options);
29+
* } </pre>
30+
*
31+
* <p>After the test:
32+
* <pre> {@code
33+
* gcdHelper.stop();
34+
* } </pre>
35+
*
36+
* @see <a href="https://github.com/GoogleCloudPlatform/gcloud-java/blob/master/TESTING.md#testing-code-that-uses-datastore">
37+
* gcloud-java tools for testing</a>
38+
*/
39+
package com.google.gcloud.datastore.testing;

gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreOptionsTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import static org.junit.Assert.assertSame;
2323
import static org.junit.Assert.assertTrue;
2424

25+
import com.google.gcloud.datastore.testing.LocalGcdHelper;
2526
import com.google.gcloud.spi.DatastoreRpc;
2627
import com.google.gcloud.spi.DatastoreRpcFactory;
2728

gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import com.google.gcloud.datastore.StructuredQuery.OrderBy;
3434
import com.google.gcloud.datastore.StructuredQuery.Projection;
3535
import com.google.gcloud.datastore.StructuredQuery.PropertyFilter;
36+
import com.google.gcloud.datastore.testing.LocalGcdHelper;
3637
import com.google.gcloud.spi.DatastoreRpc;
3738
import com.google.gcloud.spi.DatastoreRpc.DatastoreRpcException.Reason;
3839
import com.google.gcloud.spi.DatastoreRpcFactory;

0 commit comments

Comments
 (0)