Skip to content

Commit ee6af3c

Browse files
averikitschchingor13
authored andcommitted
samples: 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 601c672 commit ee6af3c

File tree

3 files changed

+97
-27
lines changed

3 files changed

+97
-27
lines changed

tasks/snippets/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/snippets/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)