-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
df91c0e
Add tests
averikitsch bb0666c
Update license
averikitsch 67b9cd3
Update README
averikitsch f6fb8dd
Merge branch 'master' into tasks-tests
averikitsch 960ae1e
Update for testing purposes
averikitsch 40cb4cd
Merge branch 'tasks-tests' of https://github.com/GoogleCloudPlatform/…
averikitsch 3143999
Update queue location
averikitsch 5cbb5c7
Merge branch 'master' into tasks-tests
averikitsch 35479f9
Fix test styling
averikitsch f30d522
Merge branch 'tasks-tests' of https://github.com/GoogleCloudPlatform/…
averikitsch c0a4dca
Library version
averikitsch 4cfa03b
Update service account
averikitsch 201881a
Fix service account
averikitsch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,29 +27,32 @@ | |
import java.nio.charset.Charset; | ||
|
||
public class CreateHttpTaskWithToken { | ||
|
||
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"); | ||
/** | ||
* Create a task with a HTTP target and authorization token using the Cloud Tasks client. | ||
* | ||
* @param projectId the Id of the project. | ||
* @param queueId the name of your Queue. | ||
* @param locationId the GCP region of your queue. | ||
* @param serviceAccountEmail your Cloud IAM service account | ||
* @throws Exception on Cloud Tasks Client errors. | ||
*/ | ||
public static void createTask( | ||
String projectId, String locationId, String queueId, String serviceAccountEmail) | ||
throws Exception { | ||
|
||
// 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 url = | ||
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? 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(); | ||
String queuePath = QueueName.of(projectId, locationId, queueId).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.newBuilder().setServiceAccountEmail(serviceAccountEmail); | ||
|
||
// Construct the task body. | ||
Task.Builder taskBuilder = | ||
|
67 changes: 67 additions & 0 deletions
67
tasks/src/test/java/com/example/task/CreateHttpTaskIT.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* 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 static final String PROJECT_ID = "java-docs-samples-testing"; | ||
private static final String LOCATION_ID = "us-east1"; | ||
private static final String QUEUE_ID = "default"; | ||
private static final String EMAIL = | ||
"java-docs-samples-testing@java-docs-samples-testing.iam.gserviceaccount.com"; | ||
private ByteArrayOutputStream bout; | ||
private PrintStream out; | ||
|
||
@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.createTask(PROJECT_ID, LOCATION_ID, QUEUE_ID); | ||
String got = bout.toString(); | ||
assertThat(got).contains("Task created:"); | ||
} | ||
|
||
@Test | ||
public void testCreateHttpTaskWithToken() throws Exception { | ||
CreateHttpTaskWithToken.createTask(PROJECT_ID, LOCATION_ID, QUEUE_ID, EMAIL); | ||
String got = bout.toString(); | ||
assertThat(got).contains("Task created:"); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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)