Skip to content

Commit 8910950

Browse files
authored
Merge pull request #77 from antmendoza/add-sendcloudevent-provisionorder-example
Add sendcloudevent and provisionorder example
2 parents bbb7591 + ff7d0da commit 8910950

17 files changed

+461
-44
lines changed

spec/examples/carauctionbids.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
{
1919
"name": "CarBidEvent",
2020
"type": "carBidMadeType",
21-
"source": "carBidEventSource"
21+
"source": "carBidEventSource",
22+
"kind": "consumed"
2223
}
2324
],
2425
"states": [

spec/examples/checkcarvitals.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,14 @@
3232
{
3333
"name": "CarTurnedOnEvent",
3434
"type": "car.events",
35-
"source": "my/car/start"
35+
"source": "my/car/start",
36+
"kind": "consumed"
3637
},
3738
{
3839
"name": "CarTurnedOffEvent",
3940
"type": "car.events",
40-
"source": "my/car/start"
41+
"source": "my/car/start",
42+
"kind": "consumed"
4143
}
4244
]
4345
}

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+
});

spec/examples/sendcloudevent.json

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{
2+
"id": "sendcloudeventonprovision",
3+
"version": "1.0",
4+
"name": "Send CloudEvent on provision completion",
5+
"start": "ProvisionOrdersState",
6+
"events": [
7+
{
8+
"name": "provisioningCompleteEvent",
9+
"type": "provisionCompleteType",
10+
"kind": "produced"
11+
}
12+
],
13+
"functions": [
14+
{
15+
"name": "provisionOrderFunction",
16+
"operation": "http://myapis.org/provisioning.json#doProvision",
17+
"type": "rest"
18+
}
19+
],
20+
"states": [
21+
{
22+
"name": "ProvisionOrdersState",
23+
"type": "foreach",
24+
"inputCollection": "${ .orders }",
25+
"iterationParam": "singleorder",
26+
"outputCollection": "${ .provisionedOrders }",
27+
"actions": [
28+
{
29+
"functionRef": {
30+
"refName": "provisionOrderFunction",
31+
"arguments": {
32+
"order": "${ .singleorder }"
33+
}
34+
}
35+
}
36+
],
37+
"end": {
38+
"produceEvents": [
39+
{
40+
"eventRef": "provisioningCompleteEvent",
41+
"data": "${ .provisionedOrders }"
42+
}
43+
]
44+
}
45+
}
46+
]
47+
}

spec/examples/sendcloudevent.spec.ts

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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 {WorkflowBuilder} from "../../src";
18+
import * as fs from "fs";
19+
import {
20+
ActionBuilder, EndBuilder,
21+
EventBuilder,
22+
EventsBuilder,
23+
ForEachStateBuilder,
24+
FunctionDefBuilder,
25+
FunctionRefBuilder,
26+
ProduceEventDefBuilder,
27+
} from '../../src';
28+
29+
30+
describe("sendcloudevent workflow example", () => {
31+
32+
33+
it('should generate Workflow object', function () {
34+
35+
const workflow = new WorkflowBuilder()
36+
.withId("sendcloudeventonprovision")
37+
.withVersion("1.0")
38+
.withName("Send CloudEvent on provision completion")
39+
.withStart("ProvisionOrdersState")
40+
.withEvents(
41+
new EventsBuilder()
42+
.withEvents([
43+
new EventBuilder()
44+
.withName("provisioningCompleteEvent")
45+
.withType("provisionCompleteType")
46+
.withKind("produced")
47+
.build(),
48+
49+
]).build(),
50+
)
51+
.withFunctions([
52+
new FunctionDefBuilder()
53+
.withName("provisionOrderFunction")
54+
.withOperation("http://myapis.org/provisioning.json#doProvision")
55+
.build(),
56+
])
57+
.withStates([
58+
new ForEachStateBuilder()
59+
.withName("ProvisionOrdersState")
60+
.withInputCollection("${ .orders }")
61+
.withIterationParam("singleorder")
62+
.withOutputCollection("${ .provisionedOrders }")
63+
.withActions([
64+
new ActionBuilder()
65+
.withFunctionRef(
66+
new FunctionRefBuilder()
67+
.withRefName("provisionOrderFunction")
68+
.withArguments({
69+
"order": "${ .singleorder }",
70+
})
71+
.build(),
72+
)
73+
.build(),
74+
])
75+
.withEnd(
76+
new EndBuilder()
77+
.withProduceEvents([
78+
new ProduceEventDefBuilder()
79+
.withEventRef("provisioningCompleteEvent")
80+
.withData("${ .provisionedOrders }")
81+
.build(),
82+
])
83+
.build(),
84+
)
85+
.build(),
86+
])
87+
.build();
88+
89+
90+
91+
92+
const expected = JSON.parse(fs.readFileSync("./spec/examples/sendcloudevent.json")
93+
.toLocaleString()) as any;
94+
expect(workflow).toEqual(expected);
95+
96+
});
97+
98+
99+
});

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export {CronDefBuilder} from "./model/cron-def.builder";
2323
export {DelayStateBuilder} from "./model/delay-state.builder";
2424
export {DatabasedSwitchBuilder} from "./model/databased-switch.builder";
2525
export {DefaultTransitionTypeBuilder} from "./model/default-transition-type.builder";
26+
export {EndBuilder} from "./model/end.builder";
2627
export {EventBuilder} from "./model/event.builder";
2728
export {EventBasedSwitchBuilder} from "./model/event-based-switch.builder";
2829
export {EventRefBuilder} from "./model/event-ref.builder";
@@ -35,6 +36,7 @@ export {FunctionsBuilder} from "./model/functions.builder";
3536
export {InjectStateBuilder} from "./model/inject-state.builder";
3637
export {MetadataBuilder} from "./model/metadata.builder";
3738
export {OnEventBuilder} from "./model/on-event.builder";
39+
export {OnErrorBuilder} from "./model/on-error.builder";
3840
export {OperationStateBuilder} from "./model/operation-state.builder";
3941
export {ProduceEventDefBuilder} from "./model/produce-event-def.builder";
4042
export {RepeatBuilder} from "./model/repeat.builder";

0 commit comments

Comments
 (0)