|
| 1 | +package com.example.workflows; |
| 2 | + |
| 3 | +import com.google.api.gax.longrunning.OperationFuture; |
| 4 | +import com.google.cloud.workflows.v1.OperationMetadata; |
| 5 | +import com.google.cloud.workflows.v1.Workflow; |
| 6 | +import com.google.cloud.workflows.v1.WorkflowsClient; |
| 7 | +import org.junit.BeforeClass; |
| 8 | +import org.junit.Test; |
| 9 | + |
| 10 | +import com.google.cloud.workflows.v1.WorkflowName; |
| 11 | +import static com.example.workflows.WorkflowsQuickstart.workflowsQuickstart; |
| 12 | +import static org.junit.Assert.*; |
| 13 | + |
| 14 | +public class WorkflowsQuickstartTest { |
| 15 | + |
| 16 | + private static String projectId; |
| 17 | + private static final String LOCATION_ID = "us-central1"; |
| 18 | + private static final String WORKFLOW_ID = "myFirstWorkflow"; |
| 19 | + |
| 20 | + // Workflow source copied from: |
| 21 | + // https://github.com/GoogleCloudPlatform/workflows-samples/blob/main/src/myFirstWorkflow.workflows.yaml |
| 22 | + private static final String WORKFLOW_SOURCE = "# Copyright 2020 Google LLC\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n# http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\n# [START workflows_myfirstworkflow]\n- getCurrentTime:\n call: http.get\n args:\n url: https://us-central1-workflowsample.cloudfunctions.net/datetime\n result: currentTime\n- readWikipedia:\n call: http.get\n args:\n url: https://en.wikipedia.org/w/api.php\n query:\n action: opensearch\n search: ${currentTime.body.dayOfTheWeek}\n result: wikiResult\n- returnResult:\n return: ${wikiResult.body[1]}\n# [END workflows_myfirstworkflow]\n"; |
| 23 | + |
| 24 | + @BeforeClass |
| 25 | + public static void beforeClass() { |
| 26 | + final String ENV_GOOGLE_CLOUD_PROJECT = "GOOGLE_CLOUD_PROJECT"; |
| 27 | + projectId = System.getenv(ENV_GOOGLE_CLOUD_PROJECT); |
| 28 | + assertNotNull( |
| 29 | + String.format("Environment variable '%s' is required to perform these tests.", ENV_GOOGLE_CLOUD_PROJECT), |
| 30 | + projectId); |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + public void testQuickstart() { |
| 35 | + System.out.println("testing!!"); |
| 36 | + System.out.println("testing!!"); |
| 37 | + System.out.println("testing!!"); |
| 38 | + System.out.println("testing!!"); |
| 39 | + System.out.println("testing!!"); |
| 40 | + // Deploy the workflow |
| 41 | + deployWorkflow(projectId, LOCATION_ID, WORKFLOW_ID); |
| 42 | + |
| 43 | + // Run the workflow we deployed |
| 44 | +// String res = workflowsQuickstart(projectId, LOCATION_ID, WORKFLOW_ID); |
| 45 | +// |
| 46 | +// // A very basic assertion that we have some result. |
| 47 | +// assertNotNull("Result should not be null", res); |
| 48 | +// assertNotEquals("Result should not be empty", res, ""); |
| 49 | + } |
| 50 | + |
| 51 | + private boolean deployWorkflow(String projectId, String location, String workflowId) { |
| 52 | + // Create a new workflow if it doesn't exist |
| 53 | + if (!workflowExists(projectId, location, workflowId)) { |
| 54 | + System.out.println("START DEPLOY"); |
| 55 | + try (WorkflowsClient workflowsClient = WorkflowsClient.create()) { |
| 56 | + // Deploy workflow |
| 57 | + Workflow workflow = Workflow.newBuilder() |
| 58 | + .setName(workflowId) |
| 59 | + .setSourceContents(WORKFLOW_SOURCE) |
| 60 | + .build(); |
| 61 | + workflowsClient.createWorkflowAsync(location, workflow, workflowId); |
| 62 | + |
| 63 | + // Wait until workflow is active |
| 64 | + Workflow deployedWorkflow = null; |
| 65 | + |
| 66 | + System.out.println("DEPLOY START"); |
| 67 | + // Wait for the deployment to finish |
| 68 | + do { |
| 69 | + System.out.println("SLEEP"); |
| 70 | + deployedWorkflow = workflowsClient.getWorkflow(WorkflowName.newBuilder() |
| 71 | + .setProject(projectId) |
| 72 | + .setLocation(location) |
| 73 | + .setWorkflow(workflowId) |
| 74 | + .build()); |
| 75 | + Thread.sleep(2_000); |
| 76 | + } while (deployedWorkflow == null || deployedWorkflow.getState().equals(Workflow.State.ACTIVE)); |
| 77 | + |
| 78 | + // Return true if the workflow is now active |
| 79 | + return deployedWorkflow.getState() != Workflow.State.ACTIVE; |
| 80 | + } catch (Exception e) { |
| 81 | + System.out.printf("Error deploying workflow: %s\n", e); |
| 82 | + } |
| 83 | + } |
| 84 | + return false; |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Returns true if the workflow exists. |
| 89 | + * @return {boolean} True if the workflow exists already. |
| 90 | + */ |
| 91 | + private boolean workflowExists(String projectId, String location, String workflow) { |
| 92 | + try (WorkflowsClient workflowsClient = WorkflowsClient.create()) { |
| 93 | + WorkflowName parent = WorkflowName.of(projectId, location, workflow); |
| 94 | + return workflowsClient.getWorkflow(parent).getState() == Workflow.State.ACTIVE; |
| 95 | + } catch (Exception e) { |
| 96 | + return false; |
| 97 | + } |
| 98 | + } |
| 99 | +} |
0 commit comments