Skip to content

Commit 3947443

Browse files
committed
Support the creation of 'eventRef' object
Signed-off-by: Antonio Mendoza Pérez <[email protected]>
1 parent ef6071a commit 3947443

File tree

3 files changed

+128
-6
lines changed

3 files changed

+128
-6
lines changed

spec/event-ref-builder.spec.ts

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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 {EventRefBuilder} from '../src/model/event-ref.builder';
18+
19+
20+
describe("event ref builder", () => {
21+
22+
23+
it("should create an object with the required fields ", () => {
24+
25+
expect(new EventRefBuilder()
26+
.withResultEventRef("resultValue")
27+
.withTriggerEventRef("triggerValue")
28+
.build()).toEqual(
29+
{resultEventRef: "resultValue", triggerEventRef: 'triggerValue'},
30+
);
31+
32+
});
33+
34+
35+
it("should create an object with all possible fields ", () => {
36+
37+
expect(new EventRefBuilder()
38+
.withResultEventRef("resultValue")
39+
.withTriggerEventRef("triggerValue")
40+
.withContextAttributes({key: "valuect"})
41+
.withData({key: "valuedata"})
42+
.build()).toEqual(
43+
{
44+
resultEventRef: "resultValue",
45+
triggerEventRef: 'triggerValue',
46+
contextAttributes: {key: "valuect"},
47+
data: {key: "valuedata"},
48+
},
49+
);
50+
});
51+
52+
53+
it("should throws an error if mandatory fields are not set ", () => {
54+
55+
expect(() => new EventRefBuilder().build()).toThrowError();
56+
57+
expect(() => new EventRefBuilder()
58+
.withResultEventRef("").build())
59+
.toThrowError();
60+
61+
expect(() => new EventRefBuilder()
62+
.withTriggerEventRef("")
63+
.build()).toThrowError();
64+
65+
});
66+
67+
});

src/model/event-ref.builder.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<<<<<<< HEAD
2+
/*
3+
* Copyright 2021-Present The Serverless Workflow Specification Authors
4+
* <p>
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
* <p>
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
* <p>
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
import {EventRefType} from '../index';
19+
20+
export class EventRefBuilder {
21+
22+
// @ts-ignore
23+
private readonly model: EventRefType = {};
24+
25+
26+
withResultEventRef(value: string): EventRefBuilder {
27+
this.model.resultEventRef = value;
28+
return this;
29+
}
30+
31+
withTriggerEventRef(value: string): EventRefBuilder {
32+
this.model.triggerEventRef = value;
33+
return this;
34+
}
35+
36+
37+
withData(value: object): EventRefBuilder {
38+
this.model.data = value;
39+
return this;
40+
}
41+
42+
withContextAttributes(value: object): EventRefBuilder {
43+
this.model.contextAttributes = value;
44+
return this;
45+
}
46+
47+
build(): EventRefType {
48+
49+
if (!this.model.resultEventRef) {
50+
throw new Error("Field resultEventRef can not be undefined");
51+
}
52+
53+
if (!this.model.triggerEventRef) {
54+
throw new Error("Field triggerEventRef can not be undefined");
55+
}
56+
57+
return this.model;
58+
}
59+
}

src/model/types.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,11 @@ export type EventRefType = {
6262
*/
6363
data?:
6464
| string
65-
| {
66-
[k: string]: unknown;
67-
};
65+
| object;
6866
/**
6967
* Add additional extension context attributes to the produced event
7068
*/
71-
contextAttributes?: {
72-
[k: string]: string;
73-
};
69+
contextAttributes?: object;
7470
};
7571

7672
export type ActionType = {

0 commit comments

Comments
 (0)