Skip to content

Commit ff7d0da

Browse files
committed
add provisionorder example
Signed-off-by: Antonio Mendoza Pérez <[email protected]>
1 parent 1482996 commit ff7d0da

File tree

5 files changed

+220
-2
lines changed

5 files changed

+220
-2
lines changed

spec/examples/provisionorder.json

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
{
2+
"id": "provisionorders",
3+
"version": "1.0",
4+
"name": "Provision Orders",
5+
"description": "Provision Orders and handle errors thrown",
6+
"start": "ProvisionOrder",
7+
"functions": [
8+
{
9+
"name": "provisionOrderFunction",
10+
"operation": "http://myapis.org/provisioningapi.json#doProvision",
11+
"type": "rest"
12+
}
13+
],
14+
"states": [
15+
{
16+
"name": "ProvisionOrder",
17+
"type": "operation",
18+
"actionMode": "sequential",
19+
"actions": [
20+
{
21+
"functionRef": {
22+
"refName": "provisionOrderFunction",
23+
"arguments": {
24+
"order": "${ .order }"
25+
}
26+
}
27+
}
28+
],
29+
"stateDataFilter": {
30+
"output": "${ .exceptions }"
31+
},
32+
"transition": "ApplyOrder",
33+
"onErrors": [
34+
{
35+
"error": "Missing order id",
36+
"transition": "MissingId"
37+
},
38+
{
39+
"error": "Missing order item",
40+
"transition": "MissingItem"
41+
},
42+
{
43+
"error": "Missing order quantity",
44+
"transition": "MissingQuantity"
45+
}
46+
]
47+
},
48+
{
49+
"name": "MissingId",
50+
"type": "subflow",
51+
"workflowId": "handleMissingIdExceptionWorkflow",
52+
"end": true
53+
},
54+
{
55+
"name": "MissingItem",
56+
"type": "subflow",
57+
"workflowId": "handleMissingItemExceptionWorkflow",
58+
"end": true
59+
},
60+
{
61+
"name": "MissingQuantity",
62+
"type": "subflow",
63+
"workflowId": "handleMissingQuantityExceptionWorkflow",
64+
"end": true
65+
},
66+
{
67+
"name": "ApplyOrder",
68+
"type": "subflow",
69+
"workflowId": "applyOrderWorkflowId",
70+
"end": true
71+
}
72+
]
73+
}

spec/examples/provisionorder.spec.ts

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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+
FunctionDefBuilder,
20+
FunctionRefBuilder,
21+
OnErrorBuilder,
22+
OperationStateBuilder,
23+
StateDataFilterBuilder,
24+
SubFlowStateBuilder,
25+
WorkflowBuilder,
26+
} from "../../src";
27+
import * as fs from "fs";
28+
29+
30+
describe("provisionorder workflow example", () => {
31+
32+
33+
it('should generate Workflow object', function () {
34+
35+
const workflow = new WorkflowBuilder()
36+
.withId("provisionorders")
37+
.withVersion("1.0")
38+
.withName("Provision Orders")
39+
.withDescription("Provision Orders and handle errors thrown")
40+
.withStart("ProvisionOrder")
41+
.withFunctions([
42+
new FunctionDefBuilder()
43+
.withName("provisionOrderFunction")
44+
.withOperation("http://myapis.org/provisioningapi.json#doProvision")
45+
.build(),
46+
])
47+
.withStates([
48+
new OperationStateBuilder()
49+
.withName("ProvisionOrder")
50+
.withActionMode("sequential")
51+
.withActions([
52+
new ActionBuilder()
53+
.withFunctionRef(
54+
new FunctionRefBuilder()
55+
.withRefName("provisionOrderFunction")
56+
.withArguments({
57+
"order": "${ .order }",
58+
})
59+
.build(),
60+
)
61+
.build(),
62+
])
63+
.withStateDataFilter(
64+
new StateDataFilterBuilder()
65+
.withOutput("${ .exceptions }")
66+
.build())
67+
.withTransition("ApplyOrder")
68+
.withOnErrors([
69+
new OnErrorBuilder()
70+
.withError("Missing order id")
71+
.withTransition("MissingId")
72+
.build(),
73+
new OnErrorBuilder()
74+
.withError("Missing order item")
75+
.withTransition("MissingItem")
76+
.build(),
77+
new OnErrorBuilder()
78+
.withError("Missing order quantity")
79+
.withTransition("MissingQuantity")
80+
.build(),
81+
])
82+
.build(),
83+
new SubFlowStateBuilder()
84+
.withName("MissingId")
85+
.withWorkflowId("handleMissingIdExceptionWorkflow")
86+
.withEnd(true)
87+
.build(),
88+
new SubFlowStateBuilder()
89+
.withName("MissingItem")
90+
.withWorkflowId("handleMissingItemExceptionWorkflow")
91+
.withEnd(true)
92+
.build(),
93+
new SubFlowStateBuilder()
94+
.withName("MissingQuantity")
95+
.withWorkflowId("handleMissingQuantityExceptionWorkflow")
96+
.withEnd(true)
97+
.build(),
98+
new SubFlowStateBuilder()
99+
.withName("ApplyOrder")
100+
.withWorkflowId("applyOrderWorkflowId")
101+
.withEnd(true)
102+
.build(),
103+
])
104+
.build();
105+
106+
107+
const expected = JSON.parse(fs.readFileSync("./spec/examples/provisionorder.json")
108+
.toLocaleString()) as any;
109+
expect(workflow).toEqual(expected);
110+
111+
});
112+
113+
114+
});

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export {FunctionsBuilder} from "./model/functions.builder";
3636
export {InjectStateBuilder} from "./model/inject-state.builder";
3737
export {MetadataBuilder} from "./model/metadata.builder";
3838
export {OnEventBuilder} from "./model/on-event.builder";
39+
export {OnErrorBuilder} from "./model/on-error.builder";
3940
export {OperationStateBuilder} from "./model/operation-state.builder";
4041
export {ProduceEventDefBuilder} from "./model/produce-event-def.builder";
4142
export {RepeatBuilder} from "./model/repeat.builder";

src/model/on-error.builder.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import {OnError, Transition} from '../index';
2+
3+
export class OnErrorBuilder {
4+
// @ts-ignore
5+
private model: OnError = {};
6+
7+
withError(value: string): OnErrorBuilder {
8+
this.model.error = value;
9+
return this;
10+
}
11+
12+
withTransition(value: Transition): OnErrorBuilder {
13+
this.model.transition = value;
14+
return this;
15+
}
16+
17+
build(): OnError {
18+
return this.model;
19+
}
20+
21+
}

src/model/operation-state.builder.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
* limitations under the License.
1515
*
1616
*/
17-
import {OperationState, Transition} from "./workflow";
18-
import {ActionModeType, Actions, End} from "./types";
17+
import {OnErrors, OperationState, Transition} from "./workflow";
18+
import {ActionModeType, Actions, End, StateDataFilter} from "./types";
1919

2020
export class OperationStateBuilder {
2121

@@ -51,6 +51,15 @@ export class OperationStateBuilder {
5151
return this;
5252

5353
}
54+
withStateDataFilter(value: StateDataFilter): OperationStateBuilder {
55+
this.model.stateDataFilter = value;
56+
return this;
57+
}
58+
withOnErrors(value: OnErrors): OperationStateBuilder {
59+
this.model.onErrors = value;
60+
return this;
61+
}
62+
5463
build(): OperationState {
5564
return this.model;
5665
}

0 commit comments

Comments
 (0)