diff --git a/tasks/src/main/java/com/example/task/CreateHttpTask.java b/tasks/src/main/java/com/example/task/CreateHttpTask.java index 858b439ae58..e86636bbc6b 100644 --- a/tasks/src/main/java/com/example/task/CreateHttpTask.java +++ b/tasks/src/main/java/com/example/task/CreateHttpTask.java @@ -23,19 +23,22 @@ import com.google.cloud.tasks.v2.QueueName; import com.google.cloud.tasks.v2.Task; import com.google.protobuf.ByteString; +import java.io.IOException; import java.nio.charset.Charset; public class CreateHttpTask { - /** - * Create a task with a HTTP target 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. - * @throws Exception on Cloud Tasks Client errors. - */ + + public static void main(String[] args) throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "my-project-id"; + String locationId = "us-central1"; + String queueId = "my-queue"; + createTask(projectId, locationId, queueId); + } + + // Create a task with a HTTP target using the Cloud Tasks client. public static void createTask(String projectId, String locationId, String queueId) - throws Exception { + throws IOException { // Instantiates a client. try (CloudTasksClient client = CloudTasksClient.create()) { diff --git a/tasks/src/main/java/com/example/task/CreateHttpTaskWithToken.java b/tasks/src/main/java/com/example/task/CreateHttpTaskWithToken.java index 72c4768ef35..a341f680fea 100644 --- a/tasks/src/main/java/com/example/task/CreateHttpTaskWithToken.java +++ b/tasks/src/main/java/com/example/task/CreateHttpTaskWithToken.java @@ -24,21 +24,25 @@ import com.google.cloud.tasks.v2.QueueName; import com.google.cloud.tasks.v2.Task; import com.google.protobuf.ByteString; +import java.io.IOException; import java.nio.charset.Charset; public class CreateHttpTaskWithToken { - /** - * 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 main(String[] args) throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "my-project-id"; + String locationId = "us-central1"; + String queueId = "my-queue"; + String serviceAccountEmail = + "java-docs-samples-testing@java-docs-samples-testing.iam.gserviceaccount.com"; + createTask(projectId, locationId, queueId, serviceAccountEmail); + } + + // Create a task with a HTTP target and authorization token using the Cloud Tasks client. public static void createTask( String projectId, String locationId, String queueId, String serviceAccountEmail) - throws Exception { + throws IOException { // Instantiates a client. try (CloudTasksClient client = CloudTasksClient.create()) { diff --git a/tasks/src/main/java/com/example/task/CreateQueue.java b/tasks/src/main/java/com/example/task/CreateQueue.java new file mode 100644 index 00000000000..04aa81101ad --- /dev/null +++ b/tasks/src/main/java/com/example/task/CreateQueue.java @@ -0,0 +1,54 @@ +// Copyright 2020 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; + +// [START cloud_tasks_create_queue] +import com.google.cloud.tasks.v2.CloudTasksClient; +import com.google.cloud.tasks.v2.LocationName; +import com.google.cloud.tasks.v2.Queue; +import com.google.cloud.tasks.v2.QueueName; +import java.io.IOException; + +public class CreateQueue { + + public static void main(String[] args) throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "my-project-id"; + String locationId = "us-central1"; + String queueId = "my-queue"; + createQueue(projectId, locationId, queueId); + } + + // Create a queue using the Cloud Tasks client. + public static void createQueue(String projectId, String locationId, String queueId) + throws IOException { + + // Instantiates a client. + try (CloudTasksClient client = CloudTasksClient.create()) { + + // Construct the fully qualified location. + String parent = LocationName.of(projectId, locationId).toString(); + + // Construct the fully qualified queue path. + String queuePath = QueueName.of(projectId, locationId, queueId).toString(); + + // Send create queue request. + Queue queue = client.createQueue(parent, Queue.newBuilder().setName(queuePath).build()); + + System.out.println("Queue created: " + queue.getName()); + } + } +} +// [END cloud_tasks_create_queue] diff --git a/tasks/src/main/java/com/example/task/DeleteQueue.java b/tasks/src/main/java/com/example/task/DeleteQueue.java new file mode 100644 index 00000000000..6f5d9898b48 --- /dev/null +++ b/tasks/src/main/java/com/example/task/DeleteQueue.java @@ -0,0 +1,49 @@ +// Copyright 2020 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; + +// [START cloud_tasks_delete_queue] +import com.google.cloud.tasks.v2.CloudTasksClient; +import com.google.cloud.tasks.v2.QueueName; +import java.io.IOException; + +public class DeleteQueue { + + public static void main(String[] args) throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "my-project-id"; + String locationId = "us-central1"; + String queueId = "my-queue"; + deleteQueue(projectId, locationId, queueId); + } + + // Delete a queue using the Cloud Tasks client. + public static void deleteQueue(String projectId, String locationId, String queueId) + throws IOException { + + // Instantiates a client. + try (CloudTasksClient client = CloudTasksClient.create()) { + + // Construct the fully qualified queue path. + String queuePath = QueueName.of(projectId, locationId, queueId).toString(); + + // Send delete queue request. + client.deleteQueue(queuePath); + + System.out.println("Queue deleted: " + queueId); + } + } +} +// [END cloud_tasks_delete_queue] diff --git a/tasks/src/main/java/com/example/task/ListQueues.java b/tasks/src/main/java/com/example/task/ListQueues.java new file mode 100644 index 00000000000..85fd8c14cc6 --- /dev/null +++ b/tasks/src/main/java/com/example/task/ListQueues.java @@ -0,0 +1,58 @@ +// Copyright 2020 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; + +// [START cloud_tasks_list_queues] +import com.google.cloud.tasks.v2.CloudTasksClient; +import com.google.cloud.tasks.v2.LocationName; +import com.google.cloud.tasks.v2.Queue; +import java.io.IOException; + +public class ListQueues { + + public static void main(String[] args) throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "my-project-id"; + String locationId = "us-central1"; + listQueues(projectId, locationId); + } + + // List queues using the Cloud Tasks client. + public static void listQueues(String projectId, String locationId) + throws IOException { + + // Instantiates a client. + try (CloudTasksClient client = CloudTasksClient.create()) { + + // Construct the fully qualified location path. + String parent = LocationName.of(projectId, locationId).toString(); + + // Send list queues request. + CloudTasksClient.ListQueuesPagedResponse response = client.listQueues(parent); + + // Iterate over results and print queue names + int total = 0; + for (Queue queue : response.iterateAll()) { + System.out.println(queue.getName()); + total++; + } + + if (total == 0) { + System.out.println("No queues found!"); + } + } + } +} +// [END cloud_tasks_list_queues] diff --git a/tasks/src/test/java/com/example/task/CreateHttpTaskIT.java b/tasks/src/test/java/com/example/task/CreateHttpTaskIT.java index e03d907a677..11fd89f0b12 100644 --- a/tasks/src/test/java/com/example/task/CreateHttpTaskIT.java +++ b/tasks/src/test/java/com/example/task/CreateHttpTaskIT.java @@ -17,6 +17,7 @@ package com.example.task; import static com.google.common.truth.Truth.assertThat; +import static org.junit.Assert.assertNotNull; import com.google.cloud.tasks.v2.CloudTasksClient; import com.google.cloud.tasks.v2.QueueName; @@ -24,6 +25,7 @@ import java.io.PrintStream; import org.junit.After; import org.junit.Before; +import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -31,14 +33,26 @@ /** 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 PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); + private static final String LOCATION_ID = System.getenv("LOCATION_ID"); 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; + private static void requireEnvVar(String varName) { + assertNotNull( + String.format("Environment variable '%s' must be set to perform these tests.", varName), + System.getenv(varName)); + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_CLOUD_PROJECT"); + requireEnvVar("LOCATION_ID"); + } + @Before public void setUp() { bout = new ByteArrayOutputStream(); diff --git a/tasks/src/test/java/com/example/task/CreateQueueIT.java b/tasks/src/test/java/com/example/task/CreateQueueIT.java new file mode 100644 index 00000000000..8677ee30d09 --- /dev/null +++ b/tasks/src/test/java/com/example/task/CreateQueueIT.java @@ -0,0 +1,78 @@ +// Copyright 2020 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 static org.junit.Assert.assertNotNull; + +import com.google.cloud.tasks.v2.CloudTasksClient; +import com.google.cloud.tasks.v2.QueueName; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.util.UUID; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Tests for creating queues. */ +@RunWith(JUnit4.class) +public class CreateQueueIT { + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); + private static final String LOCATION_ID = System.getenv("LOCATION_ID"); + private static final String QUEUE_ID = "test-queue-" + UUID.randomUUID(); + + private ByteArrayOutputStream bout; + private PrintStream out; + + private static void requireEnvVar(String varName) { + assertNotNull( + String.format("Environment variable '%s' must be set to perform these tests.", varName), + System.getenv(varName)); + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_CLOUD_PROJECT"); + requireEnvVar("LOCATION_ID"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + try (CloudTasksClient client = CloudTasksClient.create()) { + String queuePath = QueueName.of(PROJECT_ID, LOCATION_ID, QUEUE_ID).toString(); + client.deleteQueue(queuePath); + } catch (Exception e) { + System.out.println("Error with queue deletion."); + } + System.setOut(null); + } + + @Test + public void testCreateQueue() throws Exception { + CreateQueue.createQueue(PROJECT_ID, LOCATION_ID, QUEUE_ID); + String got = bout.toString(); + assertThat(got).contains("Queue created:"); + } +} diff --git a/tasks/src/test/java/com/example/task/DeleteQueueIT.java b/tasks/src/test/java/com/example/task/DeleteQueueIT.java new file mode 100644 index 00000000000..90c3f74bf59 --- /dev/null +++ b/tasks/src/test/java/com/example/task/DeleteQueueIT.java @@ -0,0 +1,92 @@ +// Copyright 2020 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 static org.junit.Assert.assertNotNull; + +import com.google.api.gax.rpc.NotFoundException; +import com.google.cloud.tasks.v2.CloudTasksClient; +import com.google.cloud.tasks.v2.LocationName; +import com.google.cloud.tasks.v2.Queue; +import com.google.cloud.tasks.v2.QueueName; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.UUID; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Tests for deleting queues. */ +@RunWith(JUnit4.class) +public class DeleteQueueIT { + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); + private static final String LOCATION_ID = System.getenv("LOCATION_ID"); + private static final String QUEUE_ID = "test-queue-" + UUID.randomUUID(); + + private ByteArrayOutputStream bout; + private PrintStream out; + private Queue queue; + + private static void requireEnvVar(String varName) { + assertNotNull( + String.format("Environment variable '%s' must be set to perform these tests.", varName), + System.getenv(varName)); + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_CLOUD_PROJECT"); + requireEnvVar("LOCATION_ID"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + + try (CloudTasksClient client = CloudTasksClient.create()) { + String parent = LocationName.of(PROJECT_ID, LOCATION_ID).toString(); + String queuePath = QueueName.of(PROJECT_ID, LOCATION_ID, QUEUE_ID).toString(); + queue = client.createQueue(parent, Queue.newBuilder().setName(queuePath).build()); + } catch (Exception e) { + System.out.println("Error with queue creation."); + } + } + + @After + public void tearDown() { + try (CloudTasksClient client = CloudTasksClient.create()) { + client.deleteQueue(queue.getName()); + } catch (IOException e) { + System.out.println("Error with queue deletion."); + } catch (NotFoundException e) { + System.out.println("Queue already successfully deleted"); + } + System.setOut(null); + } + + @Test + public void testDeleteQueue() throws Exception { + DeleteQueue.deleteQueue(PROJECT_ID, LOCATION_ID, QUEUE_ID); + String got = bout.toString(); + assertThat(got).contains("Queue deleted:"); + } +} diff --git a/tasks/src/test/java/com/example/task/ListQueuesIT.java b/tasks/src/test/java/com/example/task/ListQueuesIT.java new file mode 100644 index 00000000000..11e68b8e58e --- /dev/null +++ b/tasks/src/test/java/com/example/task/ListQueuesIT.java @@ -0,0 +1,90 @@ +// Copyright 2020 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 static org.junit.Assert.assertNotNull; + +import com.google.api.gax.rpc.NotFoundException; +import com.google.cloud.tasks.v2.CloudTasksClient; +import com.google.cloud.tasks.v2.LocationName; +import com.google.cloud.tasks.v2.Queue; +import com.google.cloud.tasks.v2.QueueName; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.UUID; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Tests for listing queues. */ +@RunWith(JUnit4.class) +public class ListQueuesIT { + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); + private static final String LOCATION_ID = System.getenv("LOCATION_ID"); + private static final String QUEUE_ID = "test-queue-" + UUID.randomUUID(); + + private ByteArrayOutputStream bout; + private PrintStream out; + private Queue queue; + + private static void requireEnvVar(String varName) { + assertNotNull( + String.format("Environment variable '%s' must be set to perform these tests.", varName), + System.getenv(varName)); + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_CLOUD_PROJECT"); + requireEnvVar("LOCATION_ID"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + + try (CloudTasksClient client = CloudTasksClient.create()) { + String parent = LocationName.of(PROJECT_ID, LOCATION_ID).toString(); + String queuePath = QueueName.of(PROJECT_ID, LOCATION_ID, QUEUE_ID).toString(); + queue = client.createQueue(parent, Queue.newBuilder().setName(queuePath).build()); + } catch (Exception e) { + System.out.println("Error with queue creation."); + } + } + + @After + public void tearDown() { + try (CloudTasksClient client = CloudTasksClient.create()) { + client.deleteQueue(queue.getName()); + } catch (IOException e) { + System.out.println("Error with queue deletion."); + } + System.setOut(null); + } + + @Test + public void testListQueues() throws Exception { + ListQueues.listQueues(PROJECT_ID, LOCATION_ID); + String got = bout.toString(); + assertThat(got).contains(queue.getName()); + } +}