|
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) |
5 | 7 | - [Datastore] (#testing-code-that-uses-datastore)
|
6 |
| -- [Storage] (#testing-code-that-uses-storage) |
| 8 | +- [DNS] (#testing-code-that-uses-dns) |
| 9 | +- [PubSub] (#testing-code-that-uses-pubsub) |
7 | 10 | - [Resource Manager] (#testing-code-that-uses-resource-manager)
|
8 |
| -- [BigQuery] (#testing-code-that-uses-bigquery) |
| 11 | +- [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. |
9 | 67 |
|
10 | 68 | ### Testing code that uses Datastore
|
11 | 69 |
|
@@ -88,30 +146,45 @@ You can test against an in-memory local DNS by following these steps:
|
88 | 146 |
|
89 | 147 | This method will block until the server thread has been terminated.
|
90 | 148 |
|
91 |
| -### Testing code that uses Storage |
| 149 | +### Testing code that uses Pub/Sub |
92 | 150 |
|
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: |
| 151 | +#### On your machine |
94 | 152 |
|
95 |
| -1. Create a test Google Cloud project. |
| 153 | +You can test against a temporary local Pub/Sub by following these steps: |
96 | 154 |
|
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]. |
| 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(); |
98 | 159 |
|
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. |
| 160 | + helper.start(); // Starts the local Pub/Sub emulator in a separate process |
| 161 | + ``` |
| 162 | + |
| 163 | +2. Create and use a `PubSub` object with the options given by the `LocalPubSubHelper` instance. For |
| 164 | +example: |
101 | 165 | ```java
|
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)); |
| 166 | + PubSub localPubsub = helper.options().service(); |
107 | 167 | ```
|
108 | 168 |
|
109 |
| -4. Run your tests. |
| 169 | +3. Run your tests. |
110 | 170 |
|
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. |
| 171 | +4. Stop the local Pub/Sub emulator by calling the `stop()` method, like so: |
113 | 172 | ```java
|
114 |
| - RemoteStorageHelper.forceDelete(storage, bucket, 5, TimeUnit.SECONDS); |
| 173 | + helper.stop(); |
| 174 | + ``` |
| 175 | + |
| 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. |
| 180 | + |
| 181 | + ```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(); |
115 | 188 | ```
|
116 | 189 |
|
117 | 190 | ### Testing code that uses Resource Manager
|
@@ -145,74 +218,30 @@ You can test against an in-memory local Resource Manager by following these step
|
145 | 218 |
|
146 | 219 | This method will block until the server thread has been terminated.
|
147 | 220 |
|
148 |
| -### Testing code that uses BigQuery |
| 221 | +### Testing code that uses Storage |
149 | 222 |
|
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: |
| 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: |
153 | 224 |
|
154 | 225 | 1. Create a test Google Cloud project.
|
155 | 226 |
|
156 |
| -2. Download a [JSON service account credentials file][create-service-account] from the Google |
157 |
| -Developer's Console. |
| 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]. |
158 | 228 |
|
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. |
| 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. |
161 | 231 | ```java
|
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()); |
| 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)); |
167 | 237 | ```
|
168 | 238 |
|
169 | 239 | 4. Run your tests.
|
170 | 240 |
|
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. |
173 |
| - ```java |
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 |
| - |
| 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. |
209 | 243 | ```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(); |
| 244 | + RemoteStorageHelper.forceDelete(storage, bucket, 5, TimeUnit.SECONDS); |
216 | 245 | ```
|
217 | 246 |
|
218 | 247 | [cloud-platform-storage-authentication]:https://cloud.google.com/storage/docs/authentication?hl=en#service_accounts
|
|
0 commit comments