Skip to content

Commit 0d0ddc0

Browse files
authored
Merge pull request #48 from antmendoza/support-creation-action-object
Support the creation of action object
2 parents b4e7f2b + e916e67 commit 0d0ddc0

File tree

4 files changed

+126
-21
lines changed

4 files changed

+126
-21
lines changed

spec/action-builder.spec.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
18+
import {ActionBuilder, ActionDataFilterType, EventRefType} from '../src';
19+
import {ActionDataFilterBuilder} from '../src/model/action-data-filter.builder';
20+
21+
22+
describe("ActionBuilder", () => {
23+
24+
it("should throws an error if mandatory fields are not set ", () => {
25+
26+
expect(() => new ActionBuilder().build()).toThrowError();
27+
28+
expect(() => new ActionBuilder()
29+
.withFunctionRef("").build())
30+
.toThrowError();
31+
});
32+
33+
34+
it("should throws an error if both functionRef and eventRef are set", () => {
35+
36+
expect(() => new ActionBuilder().build()).toThrowError();
37+
38+
expect(() => new ActionBuilder()
39+
.withFunctionRef("funcitionRef")
40+
.withEventRef(validEventRef()).build())
41+
.toThrowError();
42+
});
43+
44+
it("should generate a populated object", () => {
45+
expect(new ActionBuilder()
46+
.withName("actionName")
47+
.withEventRef(validEventRef())
48+
.withTimeOut("PT1H")
49+
.withActionDataFilter(new ActionDataFilterBuilder().build())
50+
.build()).toEqual(
51+
{
52+
name: "actionName",
53+
eventRef: validEventRef(),
54+
timeout: "PT1H",
55+
actionDataFilter: validActionDataFilter(),
56+
},
57+
);
58+
59+
60+
});
61+
62+
63+
});
64+
65+
66+
function validActionDataFilter(): ActionDataFilterType {
67+
return {};
68+
}
69+
70+
function validEventRef(): EventRefType {
71+
return {resultEventRef: "result"};
72+
}

src/model/action.builder.ts

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,52 @@
1414
* limitations under the License.
1515
*
1616
*/
17-
import {ActionType, FunctionRefType} from "./types";
17+
import {ActionDataFilterType, ActionType, EventRefType, FunctionRefType} from "./types";
1818

1919
export class ActionBuilder {
20-
// @ts-ignore
21-
private model: ActionType = {};
22-
23-
24-
withFunctionRef(value: FunctionRefType): ActionBuilder {
25-
this.model.functionRef = value;
26-
return this;
27-
}
28-
29-
build(): ActionType {
30-
//TODO validate either functionRef or eventRef
31-
return this.model;
32-
}
33-
34-
20+
21+
private model: ActionType = {};
22+
23+
24+
withFunctionRef(value: FunctionRefType): ActionBuilder {
25+
this.model.functionRef = value;
26+
return this;
27+
}
28+
29+
withEventRef(value: EventRefType): ActionBuilder {
30+
this.model.eventRef = value;
31+
return this;
32+
}
33+
34+
withName(value: string): ActionBuilder {
35+
this.model.name = value;
36+
return this;
37+
}
38+
39+
withTimeOut(value: string): ActionBuilder {
40+
this.model.timeout = value;
41+
return this;
42+
43+
}
44+
45+
withActionDataFilter(value: ActionDataFilterType): ActionBuilder {
46+
this.model.actionDataFilter = value;
47+
return this;
48+
49+
}
50+
51+
build(): ActionType {
52+
53+
if (!this.model.eventRef && !this.model.functionRef) {
54+
throw new Error("Either eventRef or functionRef have to be defined");
55+
}
56+
57+
if (this.model.eventRef && this.model.functionRef) {
58+
throw new Error("Only one eventRef or functionRef can be set");
59+
}
60+
61+
62+
return this.model;
63+
}
64+
3565
}

src/model/event-ref.builder.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import {EventRefType} from '../index';
1818

1919
export class EventRefBuilder {
2020

21-
// @ts-ignore
2221
private readonly model: EventRefType = {};
2322

2423

src/model/types.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,11 @@ export type EventRefType = {
5252
/**
5353
* Reference to the unique name of a 'produced' event definition
5454
*/
55-
triggerEventRef: string;
55+
triggerEventRef?: string;
5656
/**
5757
* Reference to the unique name of a 'consumed' event definition
5858
*/
59-
resultEventRef: string;
59+
resultEventRef?: string;
6060
/**
6161
* If string type, an expression which selects parts of the states data output to become the data (payload) of the event referenced by 'triggerEventRef'. If object type, a custom object to become the data (payload) of the event referenced by 'triggerEventRef'.
6262
*/
@@ -88,11 +88,15 @@ export type ActionType = {
8888
* Unique action definition name
8989
*/
9090
name?: string;
91-
functionRef: FunctionRefType;
91+
92+
/**
93+
* Referenced function
94+
*/
95+
functionRef?: FunctionRefType;
9296
/**
9397
* References a 'trigger' and 'result' reusable event definitions
9498
*/
95-
eventRef: EventRefType;
99+
eventRef?: EventRefType;
96100
/**
97101
* Time period to wait for function execution to complete
98102
*/

0 commit comments

Comments
 (0)