|
| 1 | +/* |
| 2 | + * Copyright 2021-Present The Serverless Workflow Specification Authors |
| 3 | + * <p> |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * <p> |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * <p> |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + */ |
| 17 | +import { |
| 18 | + ActionBuilder, |
| 19 | + ActionDataFilterBuilder, |
| 20 | + DatabasedSwitchBuilder, |
| 21 | + DefaultTransitionBuilder, |
| 22 | + DelayStateBuilder, |
| 23 | + FunctionDefBuilder, |
| 24 | + FunctionRefBuilder, |
| 25 | + OnErrorBuilder, |
| 26 | + OperationStateBuilder, |
| 27 | + StateDataFilterBuilder, |
| 28 | + SubFlowStateBuilder, |
| 29 | + TransitionDataConditionBuilder, |
| 30 | + WorkflowBuilder, |
| 31 | +} from "../../src"; |
| 32 | +import * as fs from "fs"; |
| 33 | + |
| 34 | + |
| 35 | +describe("jobmonitoring workflow example", () => { |
| 36 | + |
| 37 | + |
| 38 | + it('should generate Workflow object', function () { |
| 39 | + |
| 40 | + const workflow = new WorkflowBuilder() |
| 41 | + .withId("jobmonitoring") |
| 42 | + .withVersion("1.0") |
| 43 | + .withName("Job Monitoring") |
| 44 | + .withDescription("Monitor finished execution of a submitted job") |
| 45 | + .withStart("SubmitJob") |
| 46 | + .withFunctions([ |
| 47 | + new FunctionDefBuilder() |
| 48 | + .withName("submitJob") |
| 49 | + .withOperation("http://myapis.org/monitorapi.json#doSubmit") |
| 50 | + .build(), |
| 51 | + new FunctionDefBuilder() |
| 52 | + .withName("checkJobStatus") |
| 53 | + .withOperation("http://myapis.org/monitorapi.json#checkStatus") |
| 54 | + .build(), |
| 55 | + new FunctionDefBuilder() |
| 56 | + .withName("reportJobSuceeded") |
| 57 | + .withOperation("http://myapis.org/monitorapi.json#reportSucceeded") |
| 58 | + .build(), |
| 59 | + new FunctionDefBuilder() |
| 60 | + .withName("reportJobFailed") |
| 61 | + .withOperation("http://myapis.org/monitorapi.json#reportFailure") |
| 62 | + .build(), |
| 63 | + ]) |
| 64 | + .withStates([ |
| 65 | + new OperationStateBuilder() |
| 66 | + .withName("SubmitJob") |
| 67 | + .withActionMode('sequential') |
| 68 | + .withActions([ |
| 69 | + new ActionBuilder() |
| 70 | + .withFunctionRef( |
| 71 | + new FunctionRefBuilder() |
| 72 | + .withRefName("submitJob") |
| 73 | + .withArguments({ |
| 74 | + "name": "${ .job.name }", |
| 75 | + }) |
| 76 | + .build(), |
| 77 | + ) |
| 78 | + .withActionDataFilter( |
| 79 | + new ActionDataFilterBuilder() |
| 80 | + .withResults("${ .jobuid }") |
| 81 | + .build(), |
| 82 | + ) |
| 83 | + .build(), |
| 84 | + ]) |
| 85 | + .withOnErrors([ |
| 86 | + new OnErrorBuilder() |
| 87 | + .withError("*") |
| 88 | + .withTransition("SubmitError") |
| 89 | + .build(), |
| 90 | + ]) |
| 91 | + .withStateDataFilter( |
| 92 | + new StateDataFilterBuilder() |
| 93 | + .withOutput("${ .jobuid }") |
| 94 | + .build(), |
| 95 | + ) |
| 96 | + .withTransition("WaitForCompletion") |
| 97 | + .build(), |
| 98 | + new SubFlowStateBuilder() |
| 99 | + .withName("SubmitError") |
| 100 | + .withWorkflowId("handleJobSubmissionErrorWorkflow") |
| 101 | + .withEnd(true) |
| 102 | + .build(), |
| 103 | + new DelayStateBuilder() |
| 104 | + .withName("WaitForCompletion") |
| 105 | + .withTimeDelay("PT5S") |
| 106 | + .withTransition("GetJobStatus") |
| 107 | + .build(), |
| 108 | + new OperationStateBuilder() |
| 109 | + .withName("GetJobStatus") |
| 110 | + .withActionMode('sequential') |
| 111 | + .withActions([ |
| 112 | + new ActionBuilder() |
| 113 | + .withFunctionRef( |
| 114 | + new FunctionRefBuilder() |
| 115 | + .withRefName("checkJobStatus") |
| 116 | + .withArguments({ |
| 117 | + "name": "${ .jobuid }", |
| 118 | + }) |
| 119 | + .build(), |
| 120 | + ) |
| 121 | + .withActionDataFilter( |
| 122 | + new ActionDataFilterBuilder() |
| 123 | + .withResults("${ .jobstatus }") |
| 124 | + .build(), |
| 125 | + ) |
| 126 | + .build(), |
| 127 | + ]) |
| 128 | + .withStateDataFilter( |
| 129 | + new StateDataFilterBuilder() |
| 130 | + .withOutput("${ .jobstatus }") |
| 131 | + .build(), |
| 132 | + ) |
| 133 | + .withTransition("DetermineCompletion") |
| 134 | + .build(), |
| 135 | + new DatabasedSwitchBuilder() |
| 136 | + .withName("DetermineCompletion") |
| 137 | + .withDataConditions([ |
| 138 | + new TransitionDataConditionBuilder() |
| 139 | + .withCondition("${ .jobStatus == \"SUCCEEDED\" }") |
| 140 | + .withTransition("JobSucceeded") |
| 141 | + .build(), |
| 142 | + new TransitionDataConditionBuilder() |
| 143 | + .withCondition("${ .jobStatus == \"FAILED\" }") |
| 144 | + .withTransition("JobFailed") |
| 145 | + .build(), |
| 146 | + ]) |
| 147 | + .withDefault( |
| 148 | + new DefaultTransitionBuilder() |
| 149 | + .withTransition("WaitForCompletion") |
| 150 | + .build()) |
| 151 | + .build(), |
| 152 | + new OperationStateBuilder() |
| 153 | + .withName("JobSucceeded") |
| 154 | + .withActionMode('sequential') |
| 155 | + .withActions([ |
| 156 | + new ActionBuilder() |
| 157 | + .withFunctionRef( |
| 158 | + new FunctionRefBuilder() |
| 159 | + .withRefName("reportJobSuceeded") |
| 160 | + .withArguments({ |
| 161 | + "name": "${ .jobuid }", |
| 162 | + }) |
| 163 | + .build(), |
| 164 | + ) |
| 165 | + .build(), |
| 166 | + ]) |
| 167 | + .withEnd(true) |
| 168 | + .build(), |
| 169 | + new OperationStateBuilder() |
| 170 | + .withName("JobFailed") |
| 171 | + .withActionMode('sequential') |
| 172 | + .withActions([ |
| 173 | + new ActionBuilder() |
| 174 | + .withFunctionRef( |
| 175 | + new FunctionRefBuilder() |
| 176 | + .withRefName("reportJobFailed") |
| 177 | + .withArguments({ |
| 178 | + "name": "${ .jobuid }", |
| 179 | + }) |
| 180 | + .build(), |
| 181 | + ) |
| 182 | + .build(), |
| 183 | + ]) |
| 184 | + .withEnd(true) |
| 185 | + .build(), |
| 186 | + ]) |
| 187 | + .build(); |
| 188 | + |
| 189 | + |
| 190 | + const expected = JSON.parse(fs.readFileSync("./spec/examples/jobmonitoring.json") |
| 191 | + .toLocaleString()) as any; |
| 192 | + expect(workflow).toEqual(expected); |
| 193 | + |
| 194 | + }); |
| 195 | + |
| 196 | + |
| 197 | +}); |
0 commit comments