|
2 | 2 |
|
3 | 3 | This library provides tools to help write tests for code that uses the following gcloud-java services: |
4 | 4 |
|
5 | | -- [BigQuery] (#testing-code-that-uses-bigquery) |
6 | | -- [Compute] (#testing-code-that-uses-compute) |
7 | 5 | - [Datastore] (#testing-code-that-uses-datastore) |
8 | | -- [DNS] (#testing-code-that-uses-dns) |
9 | | -- [PubSub] (#testing-code-that-uses-pubsub) |
10 | | -- [Resource Manager] (#testing-code-that-uses-resource-manager) |
11 | 6 | - [Storage] (#testing-code-that-uses-storage) |
12 | | - |
13 | | -### Testing code that uses BigQuery |
14 | | - |
15 | | -Currently, there isn't an emulator for Google BigQuery, so an alternative is to create a test |
16 | | -project. `RemoteBigQueryHelper` contains convenience methods to make setting up and cleaning up the |
17 | | -test project easier. To use this class, follow the steps below: |
18 | | - |
19 | | -1. Create a test Google Cloud project. |
20 | | - |
21 | | -2. Download a [JSON service account credentials file][create-service-account] from the Google |
22 | | -Developer's Console. |
23 | | - |
24 | | -3. Create a `RemoteBigQueryHelper` object using your project ID and JSON key. |
25 | | -Here is an example that uses the `RemoteBigQueryHelper` to create a dataset. |
26 | | - ```java |
27 | | - RemoteBigQueryHelper bigqueryHelper = |
28 | | - RemoteBigQueryHelper.create(PROJECT_ID, new FileInputStream("/path/to/my/JSON/key.json")); |
29 | | - BigQuery bigquery = bigqueryHelper.options().service(); |
30 | | - String dataset = RemoteBigQueryHelper.generateDatasetName(); |
31 | | - bigquery.create(DatasetInfo.builder(dataset).build()); |
32 | | - ``` |
33 | | - |
34 | | -4. Run your tests. |
35 | | - |
36 | | -5. Clean up the test project by using `forceDelete` to clear any datasets used. |
37 | | -Here is an example that clears the dataset created in Step 3. |
38 | | - ```java |
39 | | - RemoteBigQueryHelper.forceDelete(bigquery, dataset); |
40 | | - ``` |
41 | | - |
42 | | -### Testing code that uses Compute |
43 | | - |
44 | | -Currently, there isn't an emulator for Google Compute, so an alternative is to create a test |
45 | | -project. `RemoteComputeHelper` contains convenience methods to make setting up the test project |
46 | | -easier. To use this class, follow the steps below: |
47 | | - |
48 | | -1. Create a test Google Cloud project. |
49 | | - |
50 | | -2. Download a [JSON service account credentials file][create-service-account] from the Google |
51 | | -Developer's Console. |
52 | | - |
53 | | -3. Create a `RemoteComputeHelper` object using your project ID and JSON key. Here is an example that |
54 | | -uses the `RemoteComputeHelper` to create an address. |
55 | | - ```java |
56 | | - RemoteComputeHelper computeHelper = |
57 | | - RemoteBigQueryHelper.create(PROJECT_ID, new FileInputStream("/path/to/my/JSON/key.json")); |
58 | | - Compute compute = computeHelper.options().service(); |
59 | | - // Pick a name for the resource with low probability of clashing |
60 | | - String addressName = RemoteComputeHelper.baseResourceName() + "address"; |
61 | | - AddressId addressId = RegionAddressId.of(REGION, addressName); |
62 | | - AddressInfo addressInfo = AddressInfo.of(addressId); |
63 | | - Operation operation = compute.create(addressInfo); |
64 | | - ``` |
65 | | - |
66 | | -4. Run your tests. |
| 7 | +- [Resource Manager] (#testing-code-that-uses-resource-manager) |
| 8 | +- [BigQuery] (#testing-code-that-uses-bigquery) |
67 | 9 |
|
68 | 10 | ### Testing code that uses Datastore |
69 | 11 |
|
@@ -146,45 +88,30 @@ You can test against an in-memory local DNS by following these steps: |
146 | 88 |
|
147 | 89 | This method will block until the server thread has been terminated. |
148 | 90 |
|
149 | | -### Testing code that uses Pub/Sub |
150 | | - |
151 | | -#### On your machine |
152 | | - |
153 | | -You can test against a temporary local Pub/Sub by following these steps: |
154 | | - |
155 | | -1. Start the local Pub/Sub emulator before running your tests using `LocalPubSubHelper`'s `create` |
156 | | -and `start` methods. This will bind a port for communication with the local Pub/Sub emulator. |
157 | | - ```java |
158 | | - LocalPubSubHelper helper = LocalPubSubHelper.create(); |
| 91 | +### Testing code that uses Storage |
159 | 92 |
|
160 | | - helper.start(); // Starts the local Pub/Sub emulator in a separate process |
161 | | - ``` |
| 93 | +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: |
162 | 94 |
|
163 | | -2. Create and use a `PubSub` object with the options given by the `LocalPubSubHelper` instance. For |
164 | | -example: |
165 | | - ```java |
166 | | - PubSub localPubsub = helper.options().service(); |
167 | | - ``` |
| 95 | +1. Create a test Google Cloud project. |
168 | 96 |
|
169 | | -3. Run your tests. |
| 97 | +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]. |
170 | 98 |
|
171 | | -4. Stop the local Pub/Sub emulator by calling the `stop()` method, like so: |
| 99 | +3. Create a `RemoteStorageHelper` object using your project ID and JSON key. |
| 100 | +Here is an example that uses the `RemoteStorageHelper` to create a bucket. |
172 | 101 | ```java |
173 | | - helper.stop(); |
| 102 | + RemoteStorageHelper helper = |
| 103 | + RemoteStorageHelper.create(PROJECT_ID, new FileInputStream("/path/to/my/JSON/key.json")); |
| 104 | + Storage storage = helper.options().service(); |
| 105 | + String bucket = RemoteStorageHelper.generateBucketName(); |
| 106 | + storage.create(BucketInfo.of(bucket)); |
174 | 107 | ``` |
175 | 108 |
|
176 | | -#### On a remote machine |
177 | | - |
178 | | -You can test against a remote Pub/Sub emulator as well. To do this, set the `PubSubOptions` project |
179 | | -endpoint to the hostname of the remote machine, like the example below. |
| 109 | +4. Run your tests. |
180 | 110 |
|
| 111 | +5. Clean up the test project by using `forceDelete` to clear any buckets used. |
| 112 | +Here is an example that clears the bucket created in Step 3 with a timeout of 5 seconds. |
181 | 113 | ```java |
182 | | - PubSubOptions options = PubSubOptions.builder() |
183 | | - .projectId("my-project-id") // must match project ID specified on remote machine |
184 | | - .host("<hostname of machine>:<port>") |
185 | | - .authCredentials(AuthCredentials.noAuth()) |
186 | | - .build(); |
187 | | - PubSub localPubsub= options.service(); |
| 114 | + RemoteStorageHelper.forceDelete(storage, bucket, 5, TimeUnit.SECONDS); |
188 | 115 | ``` |
189 | 116 |
|
190 | 117 | ### Testing code that uses Resource Manager |
@@ -218,30 +145,74 @@ You can test against an in-memory local Resource Manager by following these step |
218 | 145 |
|
219 | 146 | This method will block until the server thread has been terminated. |
220 | 147 |
|
221 | | -### Testing code that uses Storage |
| 148 | +### Testing code that uses BigQuery |
222 | 149 |
|
223 | | -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: |
| 150 | +Currently, there isn't an emulator for Google BigQuery, so an alternative is to create a test |
| 151 | +project. `RemoteBigQueryHelper` contains convenience methods to make setting up and cleaning up the |
| 152 | +test project easier. To use this class, follow the steps below: |
224 | 153 |
|
225 | 154 | 1. Create a test Google Cloud project. |
226 | 155 |
|
227 | | -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]. |
| 156 | +2. Download a [JSON service account credentials file][create-service-account] from the Google |
| 157 | +Developer's Console. |
228 | 158 |
|
229 | | -3. Create a `RemoteStorageHelper` object using your project ID and JSON key. |
230 | | -Here is an example that uses the `RemoteStorageHelper` to create a bucket. |
| 159 | +3. Create a `RemoteBigQueryHelper` object using your project ID and JSON key. |
| 160 | +Here is an example that uses the `RemoteBigQueryHelper` to create a dataset. |
231 | 161 | ```java |
232 | | - RemoteStorageHelper helper = |
233 | | - RemoteStorageHelper.create(PROJECT_ID, new FileInputStream("/path/to/my/JSON/key.json")); |
234 | | - Storage storage = helper.options().service(); |
235 | | - String bucket = RemoteStorageHelper.generateBucketName(); |
236 | | - storage.create(BucketInfo.of(bucket)); |
| 162 | + RemoteBigQueryHelper bigqueryHelper = |
| 163 | + RemoteBigQueryHelper.create(PROJECT_ID, new FileInputStream("/path/to/my/JSON/key.json")); |
| 164 | + BigQuery bigquery = bigqueryHelper.options().service(); |
| 165 | + String dataset = RemoteBigQueryHelper.generateDatasetName(); |
| 166 | + bigquery.create(DatasetInfo.builder(dataset).build()); |
237 | 167 | ``` |
238 | 168 |
|
239 | 169 | 4. Run your tests. |
240 | 170 |
|
241 | | -5. Clean up the test project by using `forceDelete` to clear any buckets used. |
242 | | -Here is an example that clears the bucket created in Step 3 with a timeout of 5 seconds. |
| 171 | +5. Clean up the test project by using `forceDelete` to clear any datasets used. |
| 172 | +Here is an example that clears the dataset created in Step 3. |
243 | 173 | ```java |
244 | | - RemoteStorageHelper.forceDelete(storage, bucket, 5, TimeUnit.SECONDS); |
| 174 | + RemoteBigQueryHelper.forceDelete(bigquery, dataset); |
| 175 | + ``` |
| 176 | + |
| 177 | +### Testing code that uses Pub/Sub |
| 178 | + |
| 179 | +#### On your machine |
| 180 | + |
| 181 | +You can test against a temporary local Pub/Sub by following these steps: |
| 182 | + |
| 183 | +1. Start the local Pub/Sub emulator before running your tests using `LocalPubSubHelper`'s `create` |
| 184 | +and `start` methods. This will bind a port for communication with the local Pub/Sub emulator. |
| 185 | + ```java |
| 186 | + LocalPubSubHelper helper = LocalPubSubHelper.create(); |
| 187 | + |
| 188 | + helper.start(); // Starts the local Pub/Sub emulator in a separate process |
| 189 | + ``` |
| 190 | + |
| 191 | +2. Create and use a `PubSub` object with the options given by the `LocalPubSubHelper` instance. For |
| 192 | +example: |
| 193 | + ```java |
| 194 | + PubSub localPubsub = helper.options().service(); |
| 195 | + ``` |
| 196 | + |
| 197 | +3. Run your tests. |
| 198 | + |
| 199 | +4. Stop the local Pub/Sub emulator by calling the `stop()` method, like so: |
| 200 | + ```java |
| 201 | + helper.stop(); |
| 202 | + ``` |
| 203 | + |
| 204 | +#### On a remote machine |
| 205 | + |
| 206 | +You can test against a remote Pub/Sub emulator as well. To do this, set the `PubSubOptions` project |
| 207 | +endpoint to the hostname of the remote machine, like the example below. |
| 208 | + |
| 209 | + ```java |
| 210 | + PubSubOptions options = PubSubOptions.builder() |
| 211 | + .projectId("my-project-id") // must match project ID specified on remote machine |
| 212 | + .host("<hostname of machine>:<port>") |
| 213 | + .authCredentials(AuthCredentials.noAuth()) |
| 214 | + .build(); |
| 215 | + PubSub localPubsub= options.service(); |
245 | 216 | ``` |
246 | 217 |
|
247 | 218 | [cloud-platform-storage-authentication]:https://cloud.google.com/storage/docs/authentication?hl=en#service_accounts |
|
0 commit comments