Skip to content

Commit d992958

Browse files
committed
Add the SubflowReference property to the ActionDefinition
Signed-off-by: charles.davernas <[email protected]>
1 parent 960b205 commit d992958

17 files changed

+534
-111
lines changed

src/ServerlessWorkflow.Sdk.UnitTests/Cases/Validation/ActionValidationTests.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@ public void Validate_Action_NoFunctionNorEvent_ShouldFail()
2727
.NotBeNull();
2828
result.Errors.Should()
2929
.NotBeNullOrEmpty()
30-
.And.HaveCount(2)
30+
.And.HaveCount(3)
3131
.And.Contain(e => e.PropertyName == nameof(ActionDefinition.Event))
32-
.And.Contain(e => e.PropertyName == nameof(ActionDefinition.Function));
32+
.And.Contain(e => e.PropertyName == nameof(ActionDefinition.Function))
33+
.And.Contain(e => e.PropertyName == nameof(ActionDefinition.Subflow));
3334
}
3435

3536
}

src/ServerlessWorkflow.Sdk.UnitTests/Cases/Validation/EventReferenceValidationTests.cs

+18-18
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ public void Validate_EventReference_TriggerEventNotSet_ShouldFail()
2121
//act
2222
var result = new EventReferenceValidator(workflow).Validate(eventRef);
2323

24-
//asset
24+
//assert
2525
result.Should()
26-
.NotBeNull();
26+
.NotBeNull();
2727
result.Errors.Should()
2828
.NotBeNullOrEmpty()
2929
.And.Contain(e => e.PropertyName == nameof(EventReference.TriggerEvent));
@@ -42,9 +42,9 @@ public void Validate_EventReference_TriggerEventNotFound_ShouldFail()
4242
//act
4343
var result = new EventReferenceValidator(workflow).Validate(eventRef);
4444

45-
//asset
45+
//assert
4646
result.Should()
47-
.NotBeNull();
47+
.NotBeNull();
4848
result.Errors.Should()
4949
.NotBeNullOrEmpty()
5050
.And.Contain(e => e.PropertyName == nameof(EventReference.TriggerEvent));
@@ -64,9 +64,9 @@ public void Validate_EventReference_TriggerEventConsumed_ShouldFail()
6464
//act
6565
var result = new EventReferenceValidator(workflow).Validate(eventRef);
6666

67-
//asset
67+
//assert
6868
result.Should()
69-
.NotBeNull();
69+
.NotBeNull();
7070
result.Errors.Should()
7171
.NotBeNullOrEmpty()
7272
.And.Contain(e => e.PropertyName == nameof(EventReference.TriggerEvent));
@@ -77,17 +77,17 @@ public void Validate_EventReference_ResultEventNotSet_ShouldFail()
7777
{
7878
//arrange
7979
var workflow = WorkflowDefinition.Create("fake", "fake", "fake")
80-
.StartsWith("fake", flow => flow.Callback())
81-
.End()
82-
.Build();
80+
.StartsWith("fake", flow => flow.Callback())
81+
.End()
82+
.Build();
8383
var eventRef = new EventReference() { TriggerEvent = "fake" };
8484

8585
//act
8686
var result = new EventReferenceValidator(workflow).Validate(eventRef);
8787

88-
//asset
88+
//assert
8989
result.Should()
90-
.NotBeNull();
90+
.NotBeNull();
9191
result.Errors.Should()
9292
.NotBeNullOrEmpty()
9393
.And.Contain(e => e.PropertyName == nameof(EventReference.ResultEvent));
@@ -98,17 +98,17 @@ public void Validate_EventReference_ResultEventNotFound_ShouldFail()
9898
{
9999
//arrange
100100
var workflow = WorkflowDefinition.Create("fake", "fake", "fake")
101-
.StartsWith("fake", flow => flow.Callback())
102-
.End()
103-
.Build();
101+
.StartsWith("fake", flow => flow.Callback())
102+
.End()
103+
.Build();
104104
var eventRef = new EventReference() { TriggerEvent = "fakeTrigger", ResultEvent = "fakeResult" };
105105

106106
//act
107107
var result = new EventReferenceValidator(workflow).Validate(eventRef);
108108

109-
//asset
109+
//assert
110110
result.Should()
111-
.NotBeNull();
111+
.NotBeNull();
112112
result.Errors.Should()
113113
.NotBeNullOrEmpty()
114114
.And.Contain(e => e.PropertyName == nameof(EventReference.ResultEvent));
@@ -128,9 +128,9 @@ public void Validate_EventReference_ResultEventProduced_ShouldFail()
128128
//act
129129
var result = new EventReferenceValidator(workflow).Validate(eventRef);
130130

131-
//asset
131+
//assert
132132
result.Should()
133-
.NotBeNull();
133+
.NotBeNull();
134134
result.Errors.Should()
135135
.NotBeNullOrEmpty()
136136
.And.Contain(e => e.PropertyName == nameof(EventReference.ResultEvent));

src/ServerlessWorkflow.Sdk.UnitTests/Cases/Validation/FunctionReferenceValidationTests.cs

+7-6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
namespace ServerlessWorkflow.Sdk.UnitTests.Cases.Validation
77
{
8+
89
public class FunctionReferenceValidationTests
910
{
1011

@@ -13,17 +14,17 @@ public void Validate_FunctionReference_NameNotSet_ShouldFail()
1314
{
1415
//arrange
1516
var workflow = WorkflowDefinition.Create("fake", "fake", "fake")
16-
.StartsWith("fake", flow => flow.Callback())
17-
.End()
18-
.Build();
17+
.StartsWith("fake", flow => flow.Callback())
18+
.End()
19+
.Build();
1920
var functionRef = new FunctionReference();
2021

2122
//act
2223
var result = new FunctionReferenceValidator(workflow).Validate(functionRef);
2324

24-
//asset
25+
//assert
2526
result.Should()
26-
.NotBeNull();
27+
.NotBeNull();
2728
result.Errors.Should()
2829
.NotBeNullOrEmpty()
2930
.And.Contain(e => e.PropertyName == nameof(FunctionReference.Name));
@@ -42,7 +43,7 @@ public void Validate_FunctionReference_FunctionNotFound_ShouldFail()
4243
//act
4344
var result = new FunctionReferenceValidator(workflow).Validate(functionRef);
4445

45-
//asset
46+
//assert
4647
result.Should()
4748
.NotBeNull();
4849
result.Errors.Should()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using FluentAssertions;
2+
using ServerlessWorkflow.Sdk.Models;
3+
using ServerlessWorkflow.Sdk.Services.Validation;
4+
using Xunit;
5+
6+
namespace ServerlessWorkflow.Sdk.UnitTests.Cases.Validation
7+
{
8+
public class SubflowReferenceValidationTests
9+
{
10+
11+
[Fact]
12+
public void Validate_SubflowReference_WorkflowIdNotSet_ShouldFail()
13+
{
14+
//arrange
15+
var subflowRef = new SubflowReference();
16+
17+
//act
18+
var result = new SubflowReferenceValidator(new()).Validate(subflowRef);
19+
20+
//assert
21+
result.Should()
22+
.NotBeNull();
23+
result.Errors.Should()
24+
.NotBeNullOrEmpty()
25+
.And.Contain(e => e.PropertyName == nameof(SubflowReference.WorkflowId));
26+
}
27+
28+
}
29+
30+
}

src/ServerlessWorkflow.Sdk/ActionType.cs

+7-2
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,17 @@ public enum ActionType
3030
/// Indicates an action that invokes a function
3131
/// </summary>
3232
[EnumMember(Value = "function")]
33-
FunctionCall,
33+
Function,
3434
/// <summary>
3535
/// Indicates an action that executes a cloud event trigger
3636
/// </summary>
3737
[EnumMember(Value = "trigger")]
38-
EventTrigger
38+
Trigger,
39+
/// <summary>
40+
/// Indicates an action that executes a subflow
41+
/// </summary>
42+
[EnumMember(Value = "subflow")]
43+
Subflow
3944
}
4045

4146
}

src/ServerlessWorkflow.Sdk/Models/ActionDefinition.cs

+48-29
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ public ActionType Type
3838
get
3939
{
4040
if (this.Function != null)
41-
return ActionType.FunctionCall;
41+
return ActionType.Function;
4242
else
43-
return ActionType.EventTrigger;
43+
return ActionType.Trigger;
4444
}
4545
}
4646

@@ -75,31 +75,11 @@ public FunctionReference Function
7575
}
7676
set
7777
{
78-
if(value != null)
79-
{
80-
this._Function = value;
81-
this.FunctionToken = JToken.FromObject(value);
82-
}
83-
}
84-
}
85-
86-
/// <summary>
87-
/// Gets the reference of the function to invoke
88-
/// </summary>
89-
[Newtonsoft.Json.JsonIgnore]
90-
[System.Text.Json.Serialization.JsonIgnore]
91-
[YamlIgnore]
92-
public string FunctionReference
93-
{
94-
get
95-
{
96-
if (this.Function != null)
97-
return this.Function.Name;
98-
else if (this.FunctionReference != null
99-
&& this.FunctionToken.Type == JTokenType.String)
100-
return this.FunctionToken.ToObject<string>();
78+
this._Function = value;
79+
if (this._Function == null)
80+
this.FunctionToken = null;
10181
else
102-
return null;
82+
this.FunctionToken = JToken.FromObject(this._Function);
10383
}
10484
}
10585

@@ -129,11 +109,50 @@ public EventReference Event
129109
}
130110
set
131111
{
132-
if (value != null)
112+
this._Event = value;
113+
if (this._Event == null)
114+
this.EventToken = null;
115+
else
116+
this.EventToken = JToken.FromObject(this._Event);
117+
}
118+
}
119+
120+
/// <summary>
121+
/// Gets/sets a <see cref="JToken"/> that references a subflow to run
122+
/// </summary>
123+
[Newtonsoft.Json.JsonProperty(PropertyName = "subflowRef")]
124+
[System.Text.Json.Serialization.JsonPropertyName("subflowRef")]
125+
[YamlMember(Alias = "subflowRef")]
126+
protected virtual JToken SubflowToken { get; set; }
127+
128+
private SubflowReference _Subflow;
129+
/// <summary>
130+
/// Gets the object used to configure the reference of the subflow to run
131+
/// </summary>
132+
[Newtonsoft.Json.JsonIgnore]
133+
[System.Text.Json.Serialization.JsonIgnore]
134+
[YamlIgnore]
135+
public SubflowReference Subflow
136+
{
137+
get
138+
{
139+
if (this._Subflow == null
140+
&& this.SubflowToken != null)
133141
{
134-
this._Event = value;
135-
this.EventToken = JToken.FromObject(value);
142+
if (this.SubflowToken?.Type == JTokenType.Object)
143+
this._Subflow = this.SubflowToken.ToObject<SubflowReference>();
144+
else if (this.SubflowToken?.Type == JTokenType.String)
145+
this._Subflow = SubflowReference.Parse(this.SubflowToken.ToObject<string>());
136146
}
147+
return this._Subflow;
148+
}
149+
set
150+
{
151+
this._Subflow = value;
152+
if (this._Subflow == null)
153+
this.SubflowToken = null;
154+
else
155+
this.SubflowToken = JToken.FromObject(this._Subflow);
137156
}
138157
}
139158

0 commit comments

Comments
 (0)