From 510e5e3a11351b03db6d8810da6f19f7637a360b Mon Sep 17 00:00:00 2001 From: Jeff Ching Date: Wed, 26 Jun 2019 09:07:54 -0700 Subject: [PATCH 1/7] Add smoke test extracted from java-docs-samples repo --- .../tasks/v2beta3/CloudTasksSmokeTest.java | 84 +++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 google-cloud-clients/google-cloud-tasks/src/test/java/com/google/cloud/tasks/v2beta3/CloudTasksSmokeTest.java diff --git a/google-cloud-clients/google-cloud-tasks/src/test/java/com/google/cloud/tasks/v2beta3/CloudTasksSmokeTest.java b/google-cloud-clients/google-cloud-tasks/src/test/java/com/google/cloud/tasks/v2beta3/CloudTasksSmokeTest.java new file mode 100644 index 000000000000..28778cefb9dc --- /dev/null +++ b/google-cloud-clients/google-cloud-tasks/src/test/java/com/google/cloud/tasks/v2beta3/CloudTasksSmokeTest.java @@ -0,0 +1,84 @@ +/* + * 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 + * + * https://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.google.cloud.tasks.v2beta3; + +import com.google.common.base.Preconditions; +import com.google.protobuf.ByteString; +import org.junit.Test; + +import java.io.IOException; +import java.nio.charset.Charset; +import java.util.logging.Level; +import java.util.logging.Logger; + +public class CloudTasksSmokeTest { + private static final String PROJECT_ENV_NAME = "GOOGLE_CLOUD_PROJECT"; + private static final String LEGACY_PROJECT_ENV_NAME = "GCLOUD_PROJECT"; + private static final String LOCATION_ID = "us-east1"; + private static final String QUEUE_ID = "default"; + + public static void main(String args[]) { + Logger.getLogger("").setLevel(Level.WARNING); + try { + createTask(); + System.out.println("OK"); + } catch (IOException e) { + System.err.println("Failed with exception:"); + e.printStackTrace(System.err); + System.exit(1); + } + } + + @Test + public void run() { + main(null); + } + + public static void createTask() throws IOException { + try (CloudTasksClient client = CloudTasksClient.create()) { + String url = "https://example.com/taskhandler"; + String payload = "Hello, World!"; + + // Construct the fully qualified queue name. + String queuePath = QueueName.of(getProjectId(), LOCATION_ID, QUEUE_ID).toString(); + + // Construct the task body. + Task.Builder taskBuilder = + Task.newBuilder() + .setHttpRequest( + HttpRequest.newBuilder() + .setBody(ByteString.copyFrom(payload, Charset.defaultCharset())) + .setUrl(url) + .setHttpMethod(HttpMethod.POST) + .build()); + + // Send create task request. + Task task = client.createTask(queuePath, taskBuilder.build()); + System.out.println("Task created: " + task.getName()); + } + } + + + private static String getProjectId() { + String projectId = System.getProperty(PROJECT_ENV_NAME, System.getenv(PROJECT_ENV_NAME)); + if (projectId == null) { + projectId = + System.getProperty(LEGACY_PROJECT_ENV_NAME, System.getenv(LEGACY_PROJECT_ENV_NAME)); + } + Preconditions.checkArgument(projectId != null, "A project ID is required."); + return projectId; + } +} From 4380d1fecdd9a574e05e6d492061ade7d5ccc575 Mon Sep 17 00:00:00 2001 From: Jeff Ching Date: Wed, 26 Jun 2019 13:01:35 -0700 Subject: [PATCH 2/7] Create task queue if not already exists --- .../tasks/v2beta3/CloudTasksSmokeTest.java | 41 ++++++++++++++++--- 1 file changed, 36 insertions(+), 5 deletions(-) diff --git a/google-cloud-clients/google-cloud-tasks/src/test/java/com/google/cloud/tasks/v2beta3/CloudTasksSmokeTest.java b/google-cloud-clients/google-cloud-tasks/src/test/java/com/google/cloud/tasks/v2beta3/CloudTasksSmokeTest.java index 28778cefb9dc..90640f08225b 100644 --- a/google-cloud-clients/google-cloud-tasks/src/test/java/com/google/cloud/tasks/v2beta3/CloudTasksSmokeTest.java +++ b/google-cloud-clients/google-cloud-tasks/src/test/java/com/google/cloud/tasks/v2beta3/CloudTasksSmokeTest.java @@ -15,25 +15,30 @@ */ package com.google.cloud.tasks.v2beta3; +import com.google.api.gax.rpc.AlreadyExistsException; import com.google.common.base.Preconditions; import com.google.protobuf.ByteString; -import org.junit.Test; +import org.junit.*; import java.io.IOException; import java.nio.charset.Charset; import java.util.logging.Level; import java.util.logging.Logger; +import static org.junit.Assert.fail; + public class CloudTasksSmokeTest { private static final String PROJECT_ENV_NAME = "GOOGLE_CLOUD_PROJECT"; private static final String LEGACY_PROJECT_ENV_NAME = "GCLOUD_PROJECT"; - private static final String LOCATION_ID = "us-east1"; + private static final String LOCATION_ENV_NAME = "GOOGLE_CLOUD_LOCATION"; + private static final String DEFAULT_LOCATION_ID = "us-central1"; private static final String QUEUE_ID = "default"; + static Queue queue; public static void main(String args[]) { Logger.getLogger("").setLevel(Level.WARNING); try { - createTask(); + createTask(getProjectId(), getLocationId(), QUEUE_ID); System.out.println("OK"); } catch (IOException e) { System.err.println("Failed with exception:"); @@ -42,18 +47,36 @@ public static void main(String args[]) { } } + @BeforeClass + public static void setupQueue() { + String queueName = QueueName.of(getProjectId(), getLocationId(), QUEUE_ID).toString(); + try (CloudTasksClient client = CloudTasksClient.create()) { + Queue q = Queue.newBuilder() + .setName(queueName) + .build(); + try { + + queue = client.createQueue(LocationName.of(getProjectId(), getLocationId()), q); + } catch (AlreadyExistsException e) { + queue = client.getQueue(queueName); + } + } catch (IOException e) { + fail("Failed to create queue: " + e.getMessage()); + } + } + @Test public void run() { main(null); } - public static void createTask() throws IOException { + public static void createTask(String project, String location, String queue) throws IOException { try (CloudTasksClient client = CloudTasksClient.create()) { String url = "https://example.com/taskhandler"; String payload = "Hello, World!"; // Construct the fully qualified queue name. - String queuePath = QueueName.of(getProjectId(), LOCATION_ID, QUEUE_ID).toString(); + String queuePath = QueueName.of(project, location, queue).toString(); // Construct the task body. Task.Builder taskBuilder = @@ -81,4 +104,12 @@ private static String getProjectId() { Preconditions.checkArgument(projectId != null, "A project ID is required."); return projectId; } + + private static String getLocationId() { + String locationId = System.getProperty(LOCATION_ENV_NAME, System.getenv(LOCATION_ENV_NAME)); + if (locationId == null) { + return DEFAULT_LOCATION_ID; + } + return locationId; + } } From 1afadf4dc10cca0b56e878b2d5a870a4c1969445 Mon Sep 17 00:00:00 2001 From: Jeff Ching Date: Wed, 26 Jun 2019 13:04:57 -0700 Subject: [PATCH 3/7] Run smoke tests as part of integration tests --- google-cloud-clients/pom.xml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/google-cloud-clients/pom.xml b/google-cloud-clients/pom.xml index 6cc908ac5651..2c82db62439e 100644 --- a/google-cloud-clients/pom.xml +++ b/google-cloud-clients/pom.xml @@ -600,6 +600,9 @@ + + **/*SmokeTest.java + 1200 sponge_log From 2ab1ced91aaa55e04dbf544ed42844441b291048 Mon Sep 17 00:00:00 2001 From: Jeff Ching Date: Mon, 1 Jul 2019 13:08:55 -0700 Subject: [PATCH 4/7] Fix mismerge --- google-cloud-clients/pom.xml | 3 --- 1 file changed, 3 deletions(-) diff --git a/google-cloud-clients/pom.xml b/google-cloud-clients/pom.xml index 2c82db62439e..6cc908ac5651 100644 --- a/google-cloud-clients/pom.xml +++ b/google-cloud-clients/pom.xml @@ -600,9 +600,6 @@ - - **/*SmokeTest.java - 1200 sponge_log From f97e4556e7e804938e15debf6dc6c38f09a5586b Mon Sep 17 00:00:00 2001 From: Jeff Ching Date: Mon, 1 Jul 2019 13:23:25 -0700 Subject: [PATCH 5/7] Fix code format --- .../tasks/v2beta3/CloudTasksSmokeTest.java | 28 ++++++++----------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/google-cloud-clients/google-cloud-tasks/src/test/java/com/google/cloud/tasks/v2beta3/CloudTasksSmokeTest.java b/google-cloud-clients/google-cloud-tasks/src/test/java/com/google/cloud/tasks/v2beta3/CloudTasksSmokeTest.java index 90640f08225b..65d6119bc9e4 100644 --- a/google-cloud-clients/google-cloud-tasks/src/test/java/com/google/cloud/tasks/v2beta3/CloudTasksSmokeTest.java +++ b/google-cloud-clients/google-cloud-tasks/src/test/java/com/google/cloud/tasks/v2beta3/CloudTasksSmokeTest.java @@ -15,17 +15,16 @@ */ package com.google.cloud.tasks.v2beta3; +import static org.junit.Assert.fail; + import com.google.api.gax.rpc.AlreadyExistsException; import com.google.common.base.Preconditions; import com.google.protobuf.ByteString; -import org.junit.*; - import java.io.IOException; import java.nio.charset.Charset; import java.util.logging.Level; import java.util.logging.Logger; - -import static org.junit.Assert.fail; +import org.junit.*; public class CloudTasksSmokeTest { private static final String PROJECT_ENV_NAME = "GOOGLE_CLOUD_PROJECT"; @@ -51,9 +50,7 @@ public static void main(String args[]) { public static void setupQueue() { String queueName = QueueName.of(getProjectId(), getLocationId(), QUEUE_ID).toString(); try (CloudTasksClient client = CloudTasksClient.create()) { - Queue q = Queue.newBuilder() - .setName(queueName) - .build(); + Queue q = Queue.newBuilder().setName(queueName).build(); try { queue = client.createQueue(LocationName.of(getProjectId(), getLocationId()), q); @@ -80,13 +77,13 @@ public static void createTask(String project, String location, String queue) thr // Construct the task body. Task.Builder taskBuilder = - Task.newBuilder() - .setHttpRequest( - HttpRequest.newBuilder() - .setBody(ByteString.copyFrom(payload, Charset.defaultCharset())) - .setUrl(url) - .setHttpMethod(HttpMethod.POST) - .build()); + Task.newBuilder() + .setHttpRequest( + HttpRequest.newBuilder() + .setBody(ByteString.copyFrom(payload, Charset.defaultCharset())) + .setUrl(url) + .setHttpMethod(HttpMethod.POST) + .build()); // Send create task request. Task task = client.createTask(queuePath, taskBuilder.build()); @@ -94,12 +91,11 @@ public static void createTask(String project, String location, String queue) thr } } - private static String getProjectId() { String projectId = System.getProperty(PROJECT_ENV_NAME, System.getenv(PROJECT_ENV_NAME)); if (projectId == null) { projectId = - System.getProperty(LEGACY_PROJECT_ENV_NAME, System.getenv(LEGACY_PROJECT_ENV_NAME)); + System.getProperty(LEGACY_PROJECT_ENV_NAME, System.getenv(LEGACY_PROJECT_ENV_NAME)); } Preconditions.checkArgument(projectId != null, "A project ID is required."); return projectId; From 12ec94b95f3a082b988f4d3ab19bc226fbe3c5ef Mon Sep 17 00:00:00 2001 From: Jeff Ching Date: Mon, 1 Jul 2019 13:55:30 -0700 Subject: [PATCH 6/7] Fix imports --- .../com/google/cloud/tasks/v2beta3/CloudTasksSmokeTest.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/google-cloud-clients/google-cloud-tasks/src/test/java/com/google/cloud/tasks/v2beta3/CloudTasksSmokeTest.java b/google-cloud-clients/google-cloud-tasks/src/test/java/com/google/cloud/tasks/v2beta3/CloudTasksSmokeTest.java index 65d6119bc9e4..0de6e55fed59 100644 --- a/google-cloud-clients/google-cloud-tasks/src/test/java/com/google/cloud/tasks/v2beta3/CloudTasksSmokeTest.java +++ b/google-cloud-clients/google-cloud-tasks/src/test/java/com/google/cloud/tasks/v2beta3/CloudTasksSmokeTest.java @@ -20,11 +20,13 @@ import com.google.api.gax.rpc.AlreadyExistsException; import com.google.common.base.Preconditions; import com.google.protobuf.ByteString; +import org.junit.BeforeClass; +import org.junit.Test; + import java.io.IOException; import java.nio.charset.Charset; import java.util.logging.Level; import java.util.logging.Logger; -import org.junit.*; public class CloudTasksSmokeTest { private static final String PROJECT_ENV_NAME = "GOOGLE_CLOUD_PROJECT"; From e6fdc64d919be4f4aa36e70a768467a47d5a63b9 Mon Sep 17 00:00:00 2001 From: Jeff Ching Date: Mon, 1 Jul 2019 14:14:21 -0700 Subject: [PATCH 7/7] Fix format --- .../com/google/cloud/tasks/v2beta3/CloudTasksSmokeTest.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/google-cloud-clients/google-cloud-tasks/src/test/java/com/google/cloud/tasks/v2beta3/CloudTasksSmokeTest.java b/google-cloud-clients/google-cloud-tasks/src/test/java/com/google/cloud/tasks/v2beta3/CloudTasksSmokeTest.java index 0de6e55fed59..3faeb1d39989 100644 --- a/google-cloud-clients/google-cloud-tasks/src/test/java/com/google/cloud/tasks/v2beta3/CloudTasksSmokeTest.java +++ b/google-cloud-clients/google-cloud-tasks/src/test/java/com/google/cloud/tasks/v2beta3/CloudTasksSmokeTest.java @@ -20,13 +20,12 @@ import com.google.api.gax.rpc.AlreadyExistsException; import com.google.common.base.Preconditions; import com.google.protobuf.ByteString; -import org.junit.BeforeClass; -import org.junit.Test; - import java.io.IOException; import java.nio.charset.Charset; import java.util.logging.Level; import java.util.logging.Logger; +import org.junit.BeforeClass; +import org.junit.Test; public class CloudTasksSmokeTest { private static final String PROJECT_ENV_NAME = "GOOGLE_CLOUD_PROJECT";