|
16 | 16 |
|
17 | 17 | public class WorkflowsQuickstartTest {
|
18 | 18 |
|
19 |
| - private static String projectId; |
20 |
| - private static final String LOCATION_ID = "us-central1"; |
21 |
| - private static final String WORKFLOW_ID = "myFirstWorkflow"; |
22 |
| - |
23 |
| - // Workflow source copied from: |
24 |
| - // https://github.com/GoogleCloudPlatform/workflows-samples/blob/main/src/myFirstWorkflow.workflows.yaml |
25 |
| - 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"; |
26 |
| - |
27 |
| - @BeforeClass |
28 |
| - public static void beforeClass() { |
29 |
| - final String ENV_GOOGLE_CLOUD_PROJECT = "GOOGLE_CLOUD_PROJECT"; |
30 |
| - projectId = System.getenv(ENV_GOOGLE_CLOUD_PROJECT); |
31 |
| - assertNotNull( |
32 |
| - String.format("Environment variable '%s' is required to perform these tests.", ENV_GOOGLE_CLOUD_PROJECT), |
33 |
| - projectId); |
| 19 | + private static String projectId; |
| 20 | + private static final String LOCATION_ID = "us-central1"; |
| 21 | + private static final String WORKFLOW_ID = "myFirstWorkflow"; |
| 22 | + |
| 23 | + // Workflow source copied from: |
| 24 | + // https://github.com/GoogleCloudPlatform/workflows-samples/blob/main/src/myFirstWorkflow.workflows.yaml |
| 25 | + 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"; |
| 26 | + |
| 27 | + @BeforeClass |
| 28 | + public static void beforeClass() { |
| 29 | + final String ENV_GOOGLE_CLOUD_PROJECT = "GOOGLE_CLOUD_PROJECT"; |
| 30 | + projectId = System.getenv(ENV_GOOGLE_CLOUD_PROJECT); |
| 31 | + assertNotNull( |
| 32 | + String.format("Environment variable '%s' is required to perform these tests.", ENV_GOOGLE_CLOUD_PROJECT), |
| 33 | + projectId); |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + public void testQuickstart() throws IOException, InterruptedException { |
| 38 | + System.out.println("testing!!"); |
| 39 | + |
| 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) throws IOException, InterruptedException { |
| 52 | + // Create a new workflow if it doesn't exist |
| 53 | + if (!workflowExists(projectId, location, workflowId)) { |
| 54 | + System.out.println("START DEPLOY"); |
| 55 | + 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; |
34 | 80 | }
|
35 |
| - |
36 |
| - @Test |
37 |
| - public void testQuickstart() throws IOException, InterruptedException { |
38 |
| - System.out.println("testing!!"); |
39 |
| - |
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) throws IOException, InterruptedException { |
52 |
| - // Create a new workflow if it doesn't exist |
53 |
| - if (!workflowExists(projectId, location, workflowId)) { |
54 |
| - System.out.println("START DEPLOY"); |
55 |
| - 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 |
| - } |
81 |
| - return false; |
82 |
| - } |
83 |
| - |
84 |
| - /** |
85 |
| - * Returns true if the workflow exists. |
86 |
| - * @return {boolean} True if the workflow exists already. |
87 |
| - */ |
88 |
| - private boolean workflowExists(String projectId, String location, String workflow) { |
89 |
| - try (WorkflowsClient workflowsClient = WorkflowsClient.create()) { |
90 |
| - WorkflowName parent = WorkflowName.of(projectId, location, workflow); |
91 |
| - return workflowsClient.getWorkflow(parent).getState() == Workflow.State.ACTIVE; |
92 |
| - } catch (Exception e) { |
93 |
| - return false; |
94 |
| - } |
| 81 | + return false; |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Returns true if the workflow exists. |
| 86 | + * @return {boolean} True if the workflow exists already. |
| 87 | + */ |
| 88 | + private boolean workflowExists(String projectId, String location, String workflow) { |
| 89 | + try (WorkflowsClient workflowsClient = WorkflowsClient.create()) { |
| 90 | + WorkflowName parent = WorkflowName.of(projectId, location, workflow); |
| 91 | + return workflowsClient.getWorkflow(parent).getState() == Workflow.State.ACTIVE; |
| 92 | + } catch (Exception e) { |
| 93 | + return false; |
95 | 94 | }
|
| 95 | + } |
96 | 96 | }
|
0 commit comments