Skip to content

Commit a7890f7

Browse files
committed
feat(UnitTests): Added validation unit tests
Signed-off-by: Charles d'Avernas <[email protected]>
1 parent 213e696 commit a7890f7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1038
-95
lines changed

src/ServerlessWorkflow.Sdk.Builders/DoTaskDefinitionBuilder.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ public virtual IDoTaskDefinitionBuilder Do(Action<ITaskDefinitionMapBuilder> set
3939
public override DoTaskDefinition Build()
4040
{
4141
if (this.Tasks == null || this.Tasks.Count < 2) throw new NullReferenceException("The execution strategy must define at least two subtasks");
42-
return new()
42+
return this.Configure(new()
4343
{
4444
Do = this.Tasks
45-
};
45+
});
4646
}
4747

4848
}

src/ServerlessWorkflow.Sdk.Builders/EmitTaskDefinitionBuilder.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,13 @@ public virtual IEmitTaskDefinitionBuilder Event(Action<IEventDefinitionBuilder>
4848
public override EmitTaskDefinition Build()
4949
{
5050
if (this.EventDefinition == null) throw new NullReferenceException("The event to emit must be defined");
51-
return new()
51+
return this.Configure(new()
5252
{
5353
Emit = new()
5454
{
5555
Event = this.EventDefinition
5656
}
57-
};
57+
});
5858
}
5959

6060
}

src/ServerlessWorkflow.Sdk.Builders/ForTaskDefinitionBuilder.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public override ForTaskDefinition Build()
8080
if (string.IsNullOrWhiteSpace(this.EachVariableName)) throw new NullReferenceException("The variable name used to store the iterated items must be set");
8181
if (string.IsNullOrWhiteSpace(this.InExpression)) throw new NullReferenceException("The runtime expression used to resolve the collection to iterate must be set");
8282
if (this.Tasks == null || this.Tasks.Count < 1) throw new NullReferenceException("The task to perform at each iteration must be set");
83-
return new()
83+
return this.Configure(new()
8484
{
8585
For = new()
8686
{
@@ -89,7 +89,7 @@ public override ForTaskDefinition Build()
8989
At = this.AtVariableName
9090
},
9191
Do = this.Tasks
92-
};
92+
});
9393
}
9494

9595
}

src/ServerlessWorkflow.Sdk.Builders/ForkTaskDefinitionBuilder.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,14 @@ public virtual IForkTaskDefinitionBuilder Compete()
5151
public override ForkTaskDefinition Build()
5252
{
5353
if (this.Tasks == null || this.Tasks.Count < 2) throw new NullReferenceException("The execution strategy must define at least two subtasks");
54-
return new()
54+
return this.Configure(new()
5555
{
5656
Fork = new()
5757
{
5858
Branches = this.Tasks,
5959
Compete = this.ShouldCompete
6060
}
61-
};
61+
});
6262
}
6363

6464
}

src/ServerlessWorkflow.Sdk.Builders/Interfaces/ITaskDefinitionBuilder.cs

+21
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,27 @@ public interface ITaskDefinitionBuilder<TBuilder>
4343
/// <returns>The configured <see cref="ITaskDefinitionBuilder{TBuilder}"/></returns>
4444
TBuilder If(string condition);
4545

46+
/// <summary>
47+
/// Sets the workflow's timeout
48+
/// </summary>
49+
/// <param name="name">The name of the workflow's timeout</param>
50+
/// <returns>The configured <see cref="ITaskDefinitionBuilder{TBuilder}"/></returns>
51+
TBuilder WithTimeout(string name);
52+
53+
/// <summary>
54+
/// Sets the workflow's timeout
55+
/// </summary>
56+
/// <param name="timeout">The workflow's timeout</param>
57+
/// <returns>The configured <see cref="ITaskDefinitionBuilder{TBuilder}"/></returns>
58+
TBuilder WithTimeout(TimeoutDefinition timeout);
59+
60+
/// <summary>
61+
/// Sets the workflow's timeout
62+
/// </summary>
63+
/// <param name="setup">An <see cref="Action{T}"/> used to setup the workflow's timeout</param>
64+
/// <returns>The configured <see cref="ITaskDefinitionBuilder{TBuilder}"/></returns>
65+
TBuilder WithTimeout(Action<ITimeoutDefinitionBuilder> setup);
66+
4667
/// <summary>
4768
/// Configures the task to build to then execute the specified flow directive
4869
/// </summary>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright © 2024-Present The Serverless Workflow Specification Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License"),
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
namespace ServerlessWorkflow.Sdk.Builders;
15+
16+
/// <summary>
17+
/// Defines the fundamentals of a service used to build <see cref="TimeoutDefinition"/>s
18+
/// </summary>
19+
public interface ITimeoutDefinitionBuilder
20+
{
21+
22+
/// <summary>
23+
/// Configures the <see cref="TimeoutDefinition"/> to timeout after the specified duration
24+
/// </summary>
25+
/// <param name="duration">The ISO 8601 duration after which to timeout</param>
26+
/// <returns>The configured <see cref="ITimeoutDefinitionBuilder"/></returns>
27+
ITimeoutDefinitionBuilder After(string duration);
28+
29+
/// <summary>
30+
/// Configures the <see cref="TimeoutDefinition"/> to timeout after the specified duration
31+
/// </summary>
32+
/// <param name="duration">The duration after which to timeout</param>
33+
/// <returns>The configured <see cref="ITimeoutDefinitionBuilder"/></returns>
34+
ITimeoutDefinitionBuilder After(Duration duration);
35+
36+
/// <summary>
37+
/// Builds the configured <see cref="TimeoutDefinition"/>
38+
/// </summary>
39+
/// <returns>A new <see cref="TimeoutDefinition"/></returns>
40+
TimeoutDefinition Build();
41+
42+
}

src/ServerlessWorkflow.Sdk.Builders/Interfaces/IWorkflowDefinitionBuilder.cs

+30-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ public interface IWorkflowDefinitionBuilder
2020
: ITaskDefinitionMapBuilder<IWorkflowDefinitionBuilder>
2121
{
2222

23+
/// <summary>
24+
/// Sets the semantic version of the Serverless Workflow DSL used to define the workflow
25+
/// </summary>
26+
/// <param name="version">The semantic version of the Serverless Workflow DSL used to define the workflow</param>
27+
/// <returns>The configured <see cref="IWorkflowDefinitionBuilder"/></returns>
28+
IWorkflowDefinitionBuilder UseDsl(string version);
29+
2330
/// <summary>
2431
/// Sets the workflow's namespace
2532
/// </summary>
@@ -60,16 +67,37 @@ public interface IWorkflowDefinitionBuilder
6067
/// </summary>
6168
/// <param name="name">The tag's name</param>
6269
/// <param name="value">The tag's value</param>
63-
/// <returns>The configured <see cref="ICallTaskDefinitionBuilder"/></returns>
70+
/// <returns>The configured <see cref="IWorkflowDefinitionBuilder"/></returns>
6471
IWorkflowDefinitionBuilder WithTag(string name, string value);
6572

6673
/// <summary>
6774
/// Sets the tags of the workflow
6875
/// </summary>
6976
/// <param name="arguments">A name/value mapping of the workflow's tags</param>
70-
/// <returns>The configured <see cref="ICallTaskDefinitionBuilder"/></returns>
77+
/// <returns>The configured <see cref="IWorkflowDefinitionBuilder"/></returns>
7178
IWorkflowDefinitionBuilder WithTag(IDictionary<string, string> arguments);
7279

80+
/// <summary>
81+
/// Sets the workflow's timeout
82+
/// </summary>
83+
/// <param name="name">The name of the workflow's timeout</param>
84+
/// <returns>The configured <see cref="IWorkflowDefinitionBuilder"/></returns>
85+
IWorkflowDefinitionBuilder WithTimeout(string name);
86+
87+
/// <summary>
88+
/// Sets the workflow's timeout
89+
/// </summary>
90+
/// <param name="timeout">The workflow's timeout</param>
91+
/// <returns>The configured <see cref="IWorkflowDefinitionBuilder"/></returns>
92+
IWorkflowDefinitionBuilder WithTimeout(TimeoutDefinition timeout);
93+
94+
/// <summary>
95+
/// Sets the workflow's timeout
96+
/// </summary>
97+
/// <param name="setup">An <see cref="Action{T}"/> used to setup the workflow's timeout</param>
98+
/// <returns>The configured <see cref="IWorkflowDefinitionBuilder"/></returns>
99+
IWorkflowDefinitionBuilder WithTimeout(Action<ITimeoutDefinitionBuilder> setup);
100+
73101
/// <summary>
74102
/// Uses the specified authentication policy
75103
/// </summary>

src/ServerlessWorkflow.Sdk.Builders/ListenTaskDefinitionBuilder.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ public virtual IListenTaskDefinitionBuilder To(Action<IListenerTargetDefinitionB
4242
public override ListenTaskDefinition Build()
4343
{
4444
if (this.Listener == null) throw new NullReferenceException("The listener must be set");
45-
return new()
45+
return this.Configure(new()
4646
{
4747
Listen = this.Listener
48-
};
48+
});
4949
}
5050

5151
}

src/ServerlessWorkflow.Sdk.Builders/RaiseTaskDefinitionBuilder.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,13 @@ public virtual IRaiseTaskDefinitionBuilder Error(Action<IErrorDefinitionBuilder>
4747
public override RaiseTaskDefinition Build()
4848
{
4949
if (this.ErrorDefinition == null) throw new NullReferenceException("The error to raise must be set");
50-
return new()
50+
return this.Configure(new()
5151
{
5252
Raise = new()
5353
{
5454
Error = this.ErrorDefinition
5555
}
56-
};
56+
});
5757
}
5858

5959
}

src/ServerlessWorkflow.Sdk.Builders/RunTaskDefinitionBuilder.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public override RunTaskDefinition Build()
6464
{
6565
if (this.ProcessBuilder == null) throw new NullReferenceException("The process to run must be set");
6666
var process = this.ProcessBuilder.Build();
67-
return new()
67+
return this.Configure(new()
6868
{
6969
Run = new()
7070
{
@@ -73,7 +73,7 @@ public override RunTaskDefinition Build()
7373
Shell = process is ShellProcessDefinition shell ? shell : null,
7474
Workflow = process is WorkflowProcessDefinition workflow ? workflow : null
7575
}
76-
};
76+
});
7777
}
7878

7979
}

src/ServerlessWorkflow.Sdk.Builders/SetTaskDefinitionBuilder.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ public virtual ISetTaskDefinitionBuilder Set(IDictionary<string, object> variabl
4545
}
4646

4747
/// <inheritdoc/>
48-
public override SetTaskDefinition Build() => new()
48+
public override SetTaskDefinition Build() => this.Configure(new()
4949
{
5050
Set = this.Variables
51-
};
51+
});
5252

5353
}

src/ServerlessWorkflow.Sdk.Builders/SwitchTaskDefinitionBuilder.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ public ISwitchTaskDefinitionBuilder Case(string name, Action<ISwitchCaseDefiniti
3838
}
3939

4040
/// <inheritdoc/>
41-
public override SwitchTaskDefinition Build() => new()
41+
public override SwitchTaskDefinition Build() => this.Configure(new()
4242
{
4343
Switch = this.Cases
44-
};
44+
});
4545

4646
}

src/ServerlessWorkflow.Sdk.Builders/TaskDefinitionBuilder.cs

+36
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ public abstract class TaskDefinitionBuilder<TBuilder, TDefinition>
2929
/// </summary>
3030
protected string? IfExpression { get; set; }
3131

32+
/// <summary>
33+
/// Gets/sets the task's timeout, if any
34+
/// </summary>
35+
protected OneOf<TimeoutDefinition, string>? Timeout { get; set; }
36+
3237
/// <summary>
3338
/// Gets/sets the flow directive, if any, used to then execute
3439
/// </summary>
@@ -42,6 +47,32 @@ public virtual TBuilder If(string condition)
4247
return (TBuilder)(object)this;
4348
}
4449

50+
/// <inheritdoc/>
51+
public virtual TBuilder WithTimeout(string name)
52+
{
53+
ArgumentException.ThrowIfNullOrWhiteSpace(name);
54+
this.Timeout = name;
55+
return (TBuilder)(object)this;
56+
}
57+
58+
/// <inheritdoc/>
59+
public virtual TBuilder WithTimeout(TimeoutDefinition timeout)
60+
{
61+
ArgumentNullException.ThrowIfNull(timeout);
62+
this.Timeout = timeout;
63+
return (TBuilder)(object)this;
64+
}
65+
66+
/// <inheritdoc/>
67+
public virtual TBuilder WithTimeout(Action<ITimeoutDefinitionBuilder> setup)
68+
{
69+
ArgumentNullException.ThrowIfNull(setup);
70+
var builder = new TimeoutDefinitionBuilder();
71+
setup(builder);
72+
this.Timeout = builder.Build();
73+
return (TBuilder)(object)this;
74+
}
75+
4576
/// <inheritdoc/>
4677
public virtual TBuilder Then(string directive)
4778
{
@@ -58,6 +89,11 @@ public virtual TBuilder Then(string directive)
5889
protected virtual TDefinition Configure(TDefinition definition)
5990
{
6091
definition.If = this.IfExpression;
92+
if (this.Timeout != null)
93+
{
94+
if (this.Timeout.T1Value != null) definition.Timeout = this.Timeout.T1Value;
95+
else definition.TimeoutReference = this.Timeout.T2Value;
96+
}
6197
definition.Then = this.ThenDirective;
6298
return definition;
6399
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright © 2024-Present The Serverless Workflow Specification Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License"),
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
using System.Xml;
15+
16+
namespace ServerlessWorkflow.Sdk.Builders;
17+
18+
/// <summary>
19+
/// Represents the default implementation of the <see cref="ITimeoutDefinitionBuilder"/> interface
20+
/// </summary>
21+
public class TimeoutDefinitionBuilder
22+
: ITimeoutDefinitionBuilder
23+
{
24+
25+
/// <summary>
26+
/// Gets/sets the duration after which to timeout
27+
/// </summary>
28+
protected Duration? AfterValue { get; set; }
29+
30+
/// <inheritdoc/>
31+
public virtual ITimeoutDefinitionBuilder After(string duration)
32+
{
33+
ArgumentException.ThrowIfNullOrWhiteSpace(duration);
34+
this.AfterValue = XmlConvert.ToTimeSpan(duration);
35+
return this;
36+
}
37+
38+
/// <inheritdoc/>
39+
public virtual ITimeoutDefinitionBuilder After(Duration duration)
40+
{
41+
ArgumentNullException.ThrowIfNull(duration);
42+
this.AfterValue = duration;
43+
return this;
44+
}
45+
46+
/// <inheritdoc/>
47+
public virtual TimeoutDefinition Build()
48+
{
49+
if (this.AfterValue == null) throw new NullReferenceException("The duration after which to timeout must be set");
50+
return new()
51+
{
52+
After = this.AfterValue
53+
};
54+
}
55+
56+
}

src/ServerlessWorkflow.Sdk.Builders/TryTaskDefinitionBuilder.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@ public override TryTaskDefinition Build()
5454
{
5555
if (this.TryTasks == null || this.TryTasks.Count < 1) throw new NullReferenceException("The task to try must be set");
5656
if (this.ErrorCatcher == null) throw new NullReferenceException("The catch clause must be set");
57-
return new()
57+
return this.Configure(new()
5858
{
5959
Try = this.TryTasks,
6060
Catch = this.ErrorCatcher
61-
};
61+
});
6262
}
6363

6464
}

0 commit comments

Comments
 (0)