Skip to content

Commit 9c0360f

Browse files
committed
ci: improve linting
Signed-off-by: Grant Timmerman <timmerman+devrel@google.com>
1 parent 8f523a6 commit 9c0360f

File tree

2 files changed

+122
-121
lines changed

2 files changed

+122
-121
lines changed

workflows/cloud-client/src/main/java/com/example/workflows/WorkflowsQuickstart.java

+47-46
Original file line numberDiff line numberDiff line change
@@ -31,59 +31,60 @@
3131
* mvn package exec:java -Dexec.mainClass='com.example.workflows.WorkflowsQuickstart'
3232
*/
3333
public class WorkflowsQuickstart {
34-
private static final String GOOGLE_CLOUD_PROJECT = System.getenv("GOOGLE_CLOUD_PROJECT");
35-
private static String LOCATION = System.getenv("LOCATION");
36-
private static String WORKFLOW = System.getenv("WORKFLOW");
34+
private static final String GOOGLE_CLOUD_PROJECT = System.getenv("GOOGLE_CLOUD_PROJECT");
35+
private static String LOCATION = System.getenv("LOCATION");
36+
private static String WORKFLOW = System.getenv("WORKFLOW");
3737

38-
public static String workflowsQuickstart(String projectId, String location, String workflow) {
39-
// Execute workflow
40-
try (ExecutionsClient workflowExecutionsClient = ExecutionsClient.create()) {
41-
WorkflowName parent = WorkflowName.of(projectId, location, workflow);
42-
Execution initialExecution = Execution.newBuilder().build();
43-
Execution createExecutionRes = workflowExecutionsClient.createExecution(parent, initialExecution);
38+
public static String workflowsQuickstart(String projectId, String location, String workflow) {
39+
// Execute workflow
40+
try (ExecutionsClient workflowExecutionsClient = ExecutionsClient.create()) {
41+
WorkflowName parent = WorkflowName.of(projectId, location, workflow);
42+
Execution initialExecution = Execution.newBuilder().build();
43+
Execution createExecutionRes = workflowExecutionsClient
44+
.createExecution(parent, initialExecution);
4445

45-
String executionName = createExecutionRes.getName();
46-
System.out.printf("Created execution: %s\n", executionName);
46+
String executionName = createExecutionRes.getName();
47+
System.out.printf("Created execution: %s%n", executionName);
4748

48-
// Wait for execution to finish, then print results.
49-
boolean executionFinished = false;
50-
long backoffDelay = 1_000; // Start wait with delay of 1,000 ms
51-
System.out.println("Poll for results...");
52-
while (!executionFinished) {
53-
Execution execution = workflowExecutionsClient.getExecution(executionName);
54-
executionFinished = execution.getState() != Execution.State.ACTIVE;
49+
// Wait for execution to finish, then print results.
50+
boolean executionFinished = false;
51+
long backoffDelay = 1_000; // Start wait with delay of 1,000 ms
52+
System.out.println("Poll for results...");
53+
while (!executionFinished) {
54+
Execution execution = workflowExecutionsClient.getExecution(executionName);
55+
executionFinished = execution.getState() != Execution.State.ACTIVE;
5556

56-
// If we haven't seen the results yet, wait.
57-
if (!executionFinished) {
58-
System.out.println("- Waiting for results");
59-
Thread.sleep(backoffDelay);
60-
backoffDelay *= 2; // Double the delay to provide exponential backoff.
61-
} else {
62-
System.out.printf("Execution finished with state: %s\n", execution.getState().name());
63-
System.out.println(execution.getResult());
64-
return execution.getResult();
65-
}
66-
}
67-
// This return is never reached.
68-
return "";
69-
} catch (Exception e) {
70-
System.out.printf("Error executing workflow: %s\n", e);
71-
return "";
57+
// If we haven't seen the results yet, wait.
58+
if (!executionFinished) {
59+
System.out.println("- Waiting for results");
60+
Thread.sleep(backoffDelay);
61+
backoffDelay *= 2; // Double the delay to provide exponential backoff.
62+
} else {
63+
System.out.printf("Execution finished with state: %s%n", execution.getState().name());
64+
System.out.println(execution.getResult());
65+
return execution.getResult();
7266
}
67+
}
68+
// This return is never reached.
69+
return "";
70+
} catch (Exception e) {
71+
System.out.printf("Error executing workflow: %s%n", e);
72+
return "";
7373
}
74+
}
7475

75-
/**
76-
* Demonstrates using the Workflows API.
77-
*/
78-
public static void main(String... args) {
79-
if (GOOGLE_CLOUD_PROJECT.isEmpty()) System.out.println("GOOGLE_CLOUD_PROJECT is empty");
80-
if (LOCATION == null || LOCATION.isEmpty()) {
81-
LOCATION = "us-central1";
82-
}
83-
if (WORKFLOW == null || WORKFLOW.isEmpty()) {
84-
WORKFLOW = "myFirstWorkflow";
85-
}
86-
workflowsQuickstart(GOOGLE_CLOUD_PROJECT, LOCATION, WORKFLOW);
76+
/**
77+
* Demonstrates using the Workflows API.
78+
*/
79+
public static void main(String... args) {
80+
if (GOOGLE_CLOUD_PROJECT.isEmpty()) System.out.println("GOOGLE_CLOUD_PROJECT is empty");
81+
if (LOCATION == null || LOCATION.isEmpty()) {
82+
LOCATION = "us-central1";
83+
}
84+
if (WORKFLOW == null || WORKFLOW.isEmpty()) {
85+
WORKFLOW = "myFirstWorkflow";
8786
}
87+
workflowsQuickstart(GOOGLE_CLOUD_PROJECT, LOCATION, WORKFLOW);
88+
}
8889
}
8990
// [END workflows_api_quickstart]

workflows/cloud-client/src/test/java/com/example/workflows/WorkflowsQuickstartTest.java

+75-75
Original file line numberDiff line numberDiff line change
@@ -16,81 +16,81 @@
1616

1717
public class WorkflowsQuickstartTest {
1818

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;
3480
}
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;
9594
}
95+
}
9696
}

0 commit comments

Comments
 (0)