Skip to content

Commit

Permalink
Add tests for ResourceManagerImpl
Browse files Browse the repository at this point in the history
  • Loading branch information
Ajay Kannan committed Dec 15, 2015
1 parent c992427 commit ca53a03
Show file tree
Hide file tree
Showing 7 changed files with 344 additions and 16 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ This client supports the following Google Cloud Platform services:

- [Google Cloud Datastore] (#google-cloud-datastore)
- [Google Cloud Storage] (#google-cloud-storage)
- [Google Cloud Resource Manager] (#google-cloud-resource-manager)

> Note: This client is a work-in-progress, and may occasionally
> make backwards-incompatible changes.
Expand Down Expand Up @@ -190,7 +191,7 @@ Google Cloud Resource Manager
#### Preview
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.
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).
```java
import com.google.common.collect.ImmutableMap;
Expand Down
7 changes: 4 additions & 3 deletions TESTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,12 @@ Here is an example that clears the bucket created in Step 3 with a timeout of 5
```java
RemoteGcsHelper.forceDelete(storage, bucket, 5, TimeUnit.SECONDS);
```

### Testing code that uses Resource Manager

#### On your machine

You can test against a temporary local datastore by following these steps:
You can test against a temporary local Resource Manager by following these steps:

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

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

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

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,
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:

```java
ResourceManager resourceManager = ResourceManagerOptions.builder()
Expand Down
6 changes: 2 additions & 4 deletions gcloud-java-resourcemanager/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Example Application
Authentication
--------------

See the [Authentication](https://github.com/GoogleCloudPlatform/gcloud-java#authentication) section in the base directory's README.
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).

About Google Cloud Resource Manager
-----------------------------------
Expand Down Expand Up @@ -67,8 +67,6 @@ import com.google.gcloud.resourcemanager.ResourceManagerOptions;
ResourceManager resourceManager = ResourceManagerOptions.defaultInstance().service();
```

> 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).
#### Creating a project
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:

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

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.
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.

#### Listing all projects
Suppose that we want list of all projects for which we have read permissions. Add the following import:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@

/**
* A Google Cloud Resource Manager project metadata object.
*
* A Project is a high-level Google Cloud Platform entity. It is a container for ACLs, APIs,
* AppEngine Apps, VMs, and other Google Cloud Platform resources.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,10 @@ private static Map<String, Object> parseListOptions(String query) {
String[] argEntry = arg.split("=");
switch (argEntry[0]) {
case "fields":
options.put("fields", argEntry[1].split(","));
// List fields are in the form "projects(field1, field2, ...)"
options.put(
"fields",
argEntry[1].substring("projects(".length(), argEntry[1].length() - 1).split(","));
break;
case "filter":
options.put("filter", argEntry[1].split(" "));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import org.junit.Test;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class LocalResourceManagerHelperTest {
Expand Down Expand Up @@ -295,16 +294,21 @@ public void testList() {
COMPLETE_PROJECT.getProjectId(), "DELETE_REQUESTED");
rpc.create(PROJECT_WITH_PARENT);
projects = rpc.list(EMPTY_RPC_OPTIONS);
Iterator<com.google.api.services.cloudresourcemanager.model.Project> it =
projects.y().iterator();
compareReadWriteFields(COMPLETE_PROJECT, it.next());
compareReadWriteFields(PROJECT_WITH_PARENT, it.next());
for (com.google.api.services.cloudresourcemanager.model.Project p : projects.y()) {
if (p.getProjectId().equals(COMPLETE_PROJECT.getProjectId())) {
compareReadWriteFields(COMPLETE_PROJECT, p);
} else if (p.getProjectId().equals(PROJECT_WITH_PARENT.getProjectId())) {
compareReadWriteFields(PROJECT_WITH_PARENT, p);
} else {
fail("Unexpected project in list.");
}
}
}

@Test
public void testListFieldOptions() {
Map<ResourceManagerRpc.Option, Object> rpcOptions = new HashMap<>();
rpcOptions.put(ResourceManagerRpc.Option.FIELDS, "projectId,name,labels");
rpcOptions.put(ResourceManagerRpc.Option.FIELDS, "projects(projectId,name,labels)");
rpcOptions.put(ResourceManagerRpc.Option.PAGE_TOKEN, "somePageToken");
rpcOptions.put(ResourceManagerRpc.Option.PAGE_SIZE, 1);
rpc.create(PROJECT_WITH_PARENT);
Expand Down
Loading

0 comments on commit ca53a03

Please sign in to comment.