Skip to content

Commit ca53a03

Browse files
author
Ajay Kannan
committed
Add tests for ResourceManagerImpl
1 parent c992427 commit ca53a03

File tree

7 files changed

+344
-16
lines changed

7 files changed

+344
-16
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ This client supports the following Google Cloud Platform services:
1414

1515
- [Google Cloud Datastore] (#google-cloud-datastore)
1616
- [Google Cloud Storage] (#google-cloud-storage)
17+
- [Google Cloud Resource Manager] (#google-cloud-resource-manager)
1718

1819
> Note: This client is a work-in-progress, and may occasionally
1920
> make backwards-incompatible changes.
@@ -190,7 +191,7 @@ Google Cloud Resource Manager
190191
191192
#### Preview
192193
193-
Here is a code snippet showing a simple usage example from within Compute/App Engine. Note that you must [supply credentials](#authentication) if running this snippet elsewhere.
194+
Here is a code snippet showing a simple usage example. Note that you must supply Google SDK credentials for this service, not other forms of authentication listed in the [Authentication section](#authentication).
194195
195196
```java
196197
import com.google.common.collect.ImmutableMap;

TESTING.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,12 @@ Here is an example that clears the bucket created in Step 3 with a timeout of 5
6464
```java
6565
RemoteGcsHelper.forceDelete(storage, bucket, 5, TimeUnit.SECONDS);
6666
```
67+
6768
### Testing code that uses Resource Manager
6869

6970
#### On your machine
7071

71-
You can test against a temporary local datastore by following these steps:
72+
You can test against a temporary local Resource Manager by following these steps:
7273

7374
1. Before running your testing code, start the Resource Manager emulator `LocalResourceManagerHelper`. This can be done as follows:
7475

@@ -79,9 +80,9 @@ You can test against a temporary local datastore by following these steps:
7980
helper.start();
8081
```
8182

82-
This will spawn a server thread that listens to localhost at an ephemeral port for Resource Manager requests.
83+
This will spawn a server thread that listens to `localhost` at an ephemeral port for Resource Manager requests.
8384

84-
2. In your program, create and use a Resource Manager service object whose host is set host to `localhost` at the appropriate port. For example,
85+
2. In your program, create and use a Resource Manager service object whose host is set host to `localhost` at the appropriate port. For example:
8586

8687
```java
8788
ResourceManager resourceManager = ResourceManagerOptions.builder()

gcloud-java-resourcemanager/README.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Example Application
2727
Authentication
2828
--------------
2929

30-
See the [Authentication](https://github.com/GoogleCloudPlatform/gcloud-java#authentication) section in the base directory's README.
30+
Unlike other `gcloud-java` service libraries, `gcloud-java-resourcemanager` only accepts Google Cloud SDK credentials at this time. If you are having trouble authenticating, it may be that you have other types of credentials that override your Google Cloud SDK credentials. See more about Google Cloud SDK credentials and credential precedence in the global README's [Authentication section](https://github.com/GoogleCloudPlatform/gcloud-java#authentication).
3131

3232
About Google Cloud Resource Manager
3333
-----------------------------------
@@ -67,8 +67,6 @@ import com.google.gcloud.resourcemanager.ResourceManagerOptions;
6767
ResourceManager resourceManager = ResourceManagerOptions.defaultInstance().service();
6868
```
6969

70-
> Note: Other `gcloud-java` service libraries allow you to authenticate using alternative methods. However, `gcloud-java-resourcemanager` only accepts Google Cloud SDK credentials at this time. If you are having trouble authenticating, it may be that you have other types of credentials that override your Google Cloud SDK credentials. See more about credential precedence in the [Authentication section](https://github.com/GoogleCloudPlatform/gcloud-java#authentication).
71-
7270
#### Creating a project
7371
All you need to create a project is a globally unique project ID. You can also optionally attach a non-unique name and labels to your project. Read more about naming guidelines for project IDs, names, and labels [here](https://cloud.google.com/resource-manager/reference/rest/v1beta1/projects). To create a project, add the following imports at the top of your file:
7472

@@ -108,7 +106,7 @@ ProjectInfo newProjectInfo = resourceManager.replace(projectFromServer.toBuilder
108106
.labels(ImmutableMap.of("launch-status", "in-development")).build());
109107
```
110108

111-
Note that the values of the project you pass in to `replace` overwrite the server's values for non-read-only fields (`projectName` and `labels`). For example, if you create a project with `projectName` "some-project-name", and then call replace using a `ProjectInfo` object that didn't set the `projectName`, then the server unsets the project's name. The server ignores any changes to the read-only fields `projectNumber`, `lifecycleState`, and `createTime`. The `projectId` cannot change.
109+
Note that the values of the project you pass in to `replace` overwrite the server's values for non-read-only fields, namely `projectName` and `labels`. For example, if you create a project with `projectName` "some-project-name" and subsequently call replace using a `ProjectInfo` object that didn't set the `projectName`, then the server will unset the project's name. The server ignores any attempted changes to the read-only fields `projectNumber`, `lifecycleState`, and `createTime`. The `projectId` cannot change.
112110

113111
#### Listing all projects
114112
Suppose that we want list of all projects for which we have read permissions. Add the following import:

gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/ProjectInfo.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131

3232
/**
3333
* A Google Cloud Resource Manager project metadata object.
34-
*
3534
* A Project is a high-level Google Cloud Platform entity. It is a container for ACLs, APIs,
3635
* AppEngine Apps, VMs, and other Google Cloud Platform resources.
3736
*/

gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/testing/LocalResourceManagerHelper.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,10 @@ private static Map<String, Object> parseListOptions(String query) {
236236
String[] argEntry = arg.split("=");
237237
switch (argEntry[0]) {
238238
case "fields":
239-
options.put("fields", argEntry[1].split(","));
239+
// List fields are in the form "projects(field1, field2, ...)"
240+
options.put(
241+
"fields",
242+
argEntry[1].substring("projects(".length(), argEntry[1].length() - 1).split(","));
240243
break;
241244
case "filter":
242245
options.put("filter", argEntry[1].split(" "));

gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/LocalResourceManagerHelperTest.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import org.junit.Test;
2020

2121
import java.util.HashMap;
22-
import java.util.Iterator;
2322
import java.util.Map;
2423

2524
public class LocalResourceManagerHelperTest {
@@ -295,16 +294,21 @@ public void testList() {
295294
COMPLETE_PROJECT.getProjectId(), "DELETE_REQUESTED");
296295
rpc.create(PROJECT_WITH_PARENT);
297296
projects = rpc.list(EMPTY_RPC_OPTIONS);
298-
Iterator<com.google.api.services.cloudresourcemanager.model.Project> it =
299-
projects.y().iterator();
300-
compareReadWriteFields(COMPLETE_PROJECT, it.next());
301-
compareReadWriteFields(PROJECT_WITH_PARENT, it.next());
297+
for (com.google.api.services.cloudresourcemanager.model.Project p : projects.y()) {
298+
if (p.getProjectId().equals(COMPLETE_PROJECT.getProjectId())) {
299+
compareReadWriteFields(COMPLETE_PROJECT, p);
300+
} else if (p.getProjectId().equals(PROJECT_WITH_PARENT.getProjectId())) {
301+
compareReadWriteFields(PROJECT_WITH_PARENT, p);
302+
} else {
303+
fail("Unexpected project in list.");
304+
}
305+
}
302306
}
303307

304308
@Test
305309
public void testListFieldOptions() {
306310
Map<ResourceManagerRpc.Option, Object> rpcOptions = new HashMap<>();
307-
rpcOptions.put(ResourceManagerRpc.Option.FIELDS, "projectId,name,labels");
311+
rpcOptions.put(ResourceManagerRpc.Option.FIELDS, "projects(projectId,name,labels)");
308312
rpcOptions.put(ResourceManagerRpc.Option.PAGE_TOKEN, "somePageToken");
309313
rpcOptions.put(ResourceManagerRpc.Option.PAGE_SIZE, 1);
310314
rpc.create(PROJECT_WITH_PARENT);

0 commit comments

Comments
 (0)