Skip to content

Commit 87000b1

Browse files
authored
Add tests to Cloud Tasks Samples (#1459)
* Add tests * Update license * Update README * Update for testing purposes * Update queue location * Fix test styling * Library version * Update service account * Fix service account
1 parent 063ffe0 commit 87000b1

File tree

5 files changed

+136
-93
lines changed

5 files changed

+136
-93
lines changed

tasks/README.md

Lines changed: 13 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -19,68 +19,38 @@ pushes it to your queue.
1919

2020
## Creating a queue
2121

22-
To create a queue using the Cloud SDK, use the following gcloud command:
22+
To create a queue using the Cloud SDK, use the following `gcloud` command:
2323

2424
```
2525
gcloud tasks queues create <QUEUE_NAME>
2626
```
2727

28-
## Run the Sample Using the Command Line
28+
The location of your queue is the same as your Google Cloud Project. It can be discovered by using the following `gcloud` command:
2929

30-
Set environment variables:
31-
32-
First, your project ID:
33-
34-
```
35-
export GOOGLE_CLOUD_PROJECT=<YOUR_GOOGLE_CLOUD_PROJECT>
3630
```
37-
38-
Then the queue ID, as specified at queue creation time. Queue IDs already
39-
created can be listed with `gcloud tasks queues list`.
40-
31+
gcloud tasks queues describe <QUEUE_NAME>
4132
```
42-
export QUEUE_ID=<QUEUE_NAME>
43-
```
44-
45-
And finally the location ID, which can be discovered with
46-
`gcloud tasks queues describe $QUEUE_ID`, with the location embedded in
47-
the "name" value (for instance, if the name is
33+
the location embedded in the "name" value (for instance, if the name is
4834
"projects/my-project/locations/us-central1/queues/my-queue", then the
4935
location is "us-central1").
5036

51-
```
52-
export LOCATION_ID=<YOUR_ZONE>
53-
```
54-
55-
### Creating Tasks with HTTP Targets
37+
## Creating Tasks with HTTP Targets
5638

57-
Set an environment variable for the endpoint to your task handler. This is an
58-
example url:
59-
```
60-
export URL=https://example.com/taskshandler
61-
```
39+
Set an endpoint to your task handler by replacing the variable `url` with your
40+
HTTP target in `CreateHttpTask.java`.
6241

63-
Running the sample will create a task and add it to your queue. As the queue
64-
processes each task, it will send the task to the specific URL endpoint:
42+
The sample will create a task and add it to your queue. As the queue processes
43+
each task, it will send the task to the specific URL endpoint.
6544

66-
```
67-
mvn exec:java@HttpTask"
68-
```
45+
## Using HTTP Targets with Authentication Tokens
6946

70-
### Using HTTP Targets with Authentication Tokens
47+
Set an endpoint to your task handler by replacing the variable `url` with your
48+
HTTP target in `CreateHttpTaskWithToken.java`.
7149

7250
Your Cloud Tasks [service account][sa],
7351
(service-<project-number>@gcp-sa-cloudtasks.iam.gserviceaccount.com), must
7452
have the role of: `Service Account Token Creator` to generate a tokens.
7553

76-
Create or use an existing [service account][sa] to replace `<SERVICE_ACCOUNT_EMAIL>`
77-
in `CreateHttpTaskWithToken.java`. This service account will be used to
78-
authenticate the OIDC token.
79-
80-
Running the sample with command:
81-
```
82-
mvn exec:java@WithToken"
83-
```
84-
54+
Create or use an existing [service account][sa] to authenticate the OIDC token.
8555

8656
[sa]: https://cloud.google.com/iam/docs/service-accounts

tasks/pom.xml

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ Copyright 2018 Google LLC
3131
<groupId>com.google.cloud.samples</groupId>
3232
<artifactId>shared-configuration</artifactId>
3333
<version>1.0.11</version>
34-
<relativePath></relativePath>
3534
</parent>
3635

3736
<properties>
@@ -45,35 +44,39 @@ Copyright 2018 Google LLC
4544
<dependency>
4645
<groupId>com.google.cloud</groupId>
4746
<artifactId>google-cloud-tasks</artifactId>
48-
<version>1.4.0</version>
47+
<version>1.3.0</version>
48+
</dependency>
49+
<!-- Test dependencies -->
50+
<dependency>
51+
<groupId>junit</groupId>
52+
<artifactId>junit</artifactId>
53+
<version>4.13-beta-2</version>
54+
</dependency>
55+
<dependency>
56+
<groupId>com.google.truth</groupId>
57+
<artifactId>truth</artifactId>
58+
<version>0.44</version>
59+
<scope>test</scope>
4960
</dependency>
5061
</dependencies>
62+
5163
<build>
5264
<plugins>
5365
<plugin>
54-
<groupId>org.codehaus.mojo</groupId>
55-
<artifactId>exec-maven-plugin</artifactId>
56-
<version>1.4.0</version>
66+
<artifactId>maven-assembly-plugin</artifactId>
67+
<version>3.0.0</version>
68+
<configuration>
69+
<descriptorRefs>
70+
<descriptorRef>jar-with-dependencies</descriptorRef>
71+
</descriptorRefs>
72+
</configuration>
5773
<executions>
5874
<execution>
59-
<id>HttpTask</id>
60-
<goals>
61-
<goal>java</goal>
62-
</goals>
63-
<configuration>
64-
<mainClass>com.example.task.CreateHttpTask</mainClass>
65-
<cleanupDaemonThreads>false</cleanupDaemonThreads>
66-
</configuration>
67-
</execution>
68-
<execution>
69-
<id>WithToken</id>
70-
<goals>
71-
<goal>java</goal>
72-
</goals>
73-
<configuration>
74-
<mainClass>com.example.task.CreateHttpTaskWithToken</mainClass>
75-
<cleanupDaemonThreads>false</cleanupDaemonThreads>
76-
</configuration>
75+
<id>make-assembly</id>
76+
<phase>package</phase>
77+
<goals>
78+
<goal>single</goal>
79+
</goals>
7780
</execution>
7881
</executions>
7982
</plugin>

tasks/src/main/java/com/example/task/CreateHttpTask.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,24 @@
2626
import java.nio.charset.Charset;
2727

2828
public class CreateHttpTask {
29-
30-
public static void main(String[] args) throws Exception {
31-
String projectId = System.getenv("PROJECT_ID");
32-
String queueName = System.getenv("QUEUE_ID");
33-
String location = System.getenv("LOCATION_ID");
34-
String url = System.getenv("URL");
29+
/**
30+
* Create a task with a HTTP target using the Cloud Tasks client.
31+
*
32+
* @param projectId the Id of the project.
33+
* @param queueId the name of your Queue.
34+
* @param locationId the GCP region of your queue.
35+
* @throws Exception on Cloud Tasks Client errors.
36+
*/
37+
public static void createTask(String projectId, String locationId, String queueId)
38+
throws Exception {
3539

3640
// Instantiates a client.
3741
try (CloudTasksClient client = CloudTasksClient.create()) {
38-
// Variables provided by the system variables.
39-
// projectId = "my-project-id";
40-
// queueName = "my-queue";
41-
// location = "us-central1";
42-
// url = "https://example.com/taskhandler";
43-
String payload = "hello";
42+
String url = "https://example.com/taskhandler";
43+
String payload = "Hello, World!";
4444

4545
// Construct the fully qualified queue name.
46-
String queuePath = QueueName.of(projectId, location, queueName).toString();
46+
String queuePath = QueueName.of(projectId, locationId, queueId).toString();
4747

4848
// Construct the task body.
4949
Task.Builder taskBuilder =

tasks/src/main/java/com/example/task/CreateHttpTaskWithToken.java

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,29 +27,32 @@
2727
import java.nio.charset.Charset;
2828

2929
public class CreateHttpTaskWithToken {
30-
31-
public static void main(String[] args) throws Exception {
32-
String projectId = System.getenv("PROJECT_ID");
33-
String queueName = System.getenv("QUEUE_ID");
34-
String location = System.getenv("LOCATION_ID");
35-
String url = System.getenv("URL");
30+
/**
31+
* Create a task with a HTTP target and authorization token using the Cloud Tasks client.
32+
*
33+
* @param projectId the Id of the project.
34+
* @param queueId the name of your Queue.
35+
* @param locationId the GCP region of your queue.
36+
* @param serviceAccountEmail your Cloud IAM service account
37+
* @throws Exception on Cloud Tasks Client errors.
38+
*/
39+
public static void createTask(
40+
String projectId, String locationId, String queueId, String serviceAccountEmail)
41+
throws Exception {
3642

3743
// Instantiates a client.
3844
try (CloudTasksClient client = CloudTasksClient.create()) {
39-
// Variables provided by the system variables.
40-
// projectId = "my-project-id";
41-
// queueName = "my-queue";
42-
// location = "us-central1";
43-
// url = "https://example.com/taskhandler";
44-
String payload = "hello";
45+
String url =
46+
"https://example.com/taskhandler"; // The full url path that the request will be sent to
47+
String payload = "Hello, World!"; // The task HTTP request body
4548

4649
// Construct the fully qualified queue name.
47-
String queuePath = QueueName.of(projectId, location, queueName).toString();
50+
String queuePath = QueueName.of(projectId, locationId, queueId).toString();
4851

4952
// Add your service account email to construct the OIDC token.
5053
// in order to add an authentication header to the request.
5154
OidcToken.Builder oidcTokenBuilder =
52-
OidcToken.newBuilder().setServiceAccountEmail("<SERVICE_ACCOUNT_EMAIL>");
55+
OidcToken.newBuilder().setServiceAccountEmail(serviceAccountEmail);
5356

5457
// Construct the task body.
5558
Task.Builder taskBuilder =
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright 2019 Google LLC
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+
package com.example.task;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
21+
import java.io.ByteArrayOutputStream;
22+
import java.io.PrintStream;
23+
import org.junit.After;
24+
import org.junit.Before;
25+
import org.junit.Rule;
26+
import org.junit.Test;
27+
import org.junit.rules.Timeout;
28+
import org.junit.runner.RunWith;
29+
import org.junit.runners.JUnit4;
30+
31+
/** Tests for creating Tasks with HTTP targets. */
32+
@RunWith(JUnit4.class)
33+
public class CreateHttpTaskIT {
34+
private static final String PROJECT_ID = "java-docs-samples-testing";
35+
private static final String LOCATION_ID = "us-east1";
36+
private static final String QUEUE_ID = "default";
37+
private static final String EMAIL =
38+
"java-docs-samples-testing@java-docs-samples-testing.iam.gserviceaccount.com";
39+
private ByteArrayOutputStream bout;
40+
private PrintStream out;
41+
42+
@Before
43+
public void setUp() {
44+
bout = new ByteArrayOutputStream();
45+
out = new PrintStream(bout);
46+
System.setOut(out);
47+
}
48+
49+
@After
50+
public void tearDown() {
51+
System.setOut(null);
52+
}
53+
54+
@Test
55+
public void testCreateHttpTask() throws Exception {
56+
CreateHttpTask.createTask(PROJECT_ID, LOCATION_ID, QUEUE_ID);
57+
String got = bout.toString();
58+
assertThat(got).contains("Task created:");
59+
}
60+
61+
@Test
62+
public void testCreateHttpTaskWithToken() throws Exception {
63+
CreateHttpTaskWithToken.createTask(PROJECT_ID, LOCATION_ID, QUEUE_ID, EMAIL);
64+
String got = bout.toString();
65+
assertThat(got).contains("Task created:");
66+
}
67+
}

0 commit comments

Comments
 (0)