-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Add tests to Cloud Tasks Samples #1459
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
df91c0e
bb0666c
67b9cd3
f6fb8dd
960ae1e
40cb4cd
3143999
5cbb5c7
35479f9
f30d522
c0a4dca
4cfa03b
201881a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,24 +32,24 @@ public static void main(String[] args) throws Exception { | |
| String projectId = System.getenv("PROJECT_ID"); | ||
| String queueName = System.getenv("QUEUE_ID"); | ||
| String location = System.getenv("LOCATION_ID"); | ||
| String url = System.getenv("URL"); | ||
|
|
||
| // Instantiates a client. | ||
| try (CloudTasksClient client = CloudTasksClient.create()) { | ||
| // Variables provided by the system variables. | ||
| // projectId = "my-project-id"; | ||
| // queueName = "my-queue"; | ||
| // location = "us-central1"; | ||
| // url = "https://example.com/taskhandler"; | ||
| String payload = "hello"; | ||
| String email = args[0]; // Cloud IAM service account | ||
| String url = | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we suggest changing this?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since this is a snippet. I have updated with the sample without a main and highlighted the parameters. |
||
| "https://example.com/taskhandler"; // The full url path that the request will be sent to | ||
| String payload = "Hello, World!"; // The task HTTP request body | ||
|
|
||
| // Construct the fully qualified queue name. | ||
| String queuePath = QueueName.of(projectId, location, queueName).toString(); | ||
|
|
||
| // Add your service account email to construct the OIDC token. | ||
| // in order to add an authentication header to the request. | ||
| OidcToken.Builder oidcTokenBuilder = | ||
| OidcToken.newBuilder().setServiceAccountEmail("<SERVICE_ACCOUNT_EMAIL>"); | ||
| OidcToken.Builder oidcTokenBuilder = OidcToken.newBuilder().setServiceAccountEmail(email); | ||
|
|
||
| // Construct the task body. | ||
| Task.Builder taskBuilder = | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| /* | ||
| * Copyright 2019 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.example.task; | ||
|
|
||
| import static com.google.common.truth.Truth.assertThat; | ||
|
|
||
| import java.io.ByteArrayOutputStream; | ||
| import java.io.PrintStream; | ||
| import org.junit.After; | ||
| import org.junit.Before; | ||
| import org.junit.Rule; | ||
| import org.junit.Test; | ||
| import org.junit.rules.Timeout; | ||
| import org.junit.runner.RunWith; | ||
| import org.junit.runners.JUnit4; | ||
|
|
||
| /** Tests for creating Tasks with HTTP targets. */ | ||
| @RunWith(JUnit4.class) | ||
| public class CreateHttpTaskIT { | ||
|
|
||
| private ByteArrayOutputStream bout; | ||
| private PrintStream out; | ||
| // @Rule public Timeout globalTimeout = Timeout.seconds(300); // 5 minute timeout | ||
|
||
|
|
||
| @Before | ||
| public void setUp() { | ||
| bout = new ByteArrayOutputStream(); | ||
| out = new PrintStream(bout); | ||
| System.setOut(out); | ||
| } | ||
|
|
||
| @After | ||
| public void tearDown() { | ||
| System.setOut(null); | ||
| } | ||
|
|
||
| @Test | ||
| public void testCreateHttpTask() throws Exception { | ||
| CreateHttpTask.main(); | ||
| String got = bout.toString(); | ||
| assertThat(got).contains("Task created:"); | ||
| } | ||
|
|
||
| @Test | ||
| public void testCreateHttpTaskWithToken() throws Exception { | ||
| String[] args = {" tasks-test@java-docs-samples-tests.iam.gserviceaccount.com"}; | ||
| CreateHttpTaskWithToken.main(args); | ||
| String got = bout.toString(); | ||
| assertThat(got).contains("Task created:"); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you call this out in the README? (CHANGEME)