-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
41bc78a
commit 79e6f04
Showing
10 changed files
with
201 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
ActionFlow.Tests/Providers/WorkflowProviderBuiltinExtensionsTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
using ActionFlow.Domain.Engine; | ||
using ActionFlow.Engine.Providers; | ||
using ActionFlow.Engine.Providers.Extensions; | ||
using FluentAssertions; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using System.Text.Json; | ||
|
||
namespace ActionFlow.Tests.Providers; | ||
|
||
[TestClass] | ||
public class WorkflowProviderBuiltinExtensionsTests | ||
{ | ||
[TestMethod] | ||
public void When_using_from_json_it_should_add_workflow() | ||
{ | ||
//Arrange | ||
var workflows = new List<Workflow> | ||
{ | ||
new("Test", | ||
[ | ||
new("initialize", "Variable", new Dictionary<string, object> | ||
{ | ||
{ "age", "1" }, | ||
{ "canWalk", "true" }, | ||
}), | ||
new("test variable value", "Variable", [], "age == 1 && canWalk == true") | ||
]) | ||
}; | ||
|
||
var workflowJson = JsonSerializer.SerializeToNode(workflows); | ||
var services = new ServiceCollection(); | ||
services.AddJsonWorkflowProvider(workflowJson!.AsArray()); | ||
|
||
//Act | ||
var provider = services.BuildServiceProvider(); | ||
|
||
//Assert | ||
var workflowProvider = provider.GetRequiredService<IWorkflowProvider>(); | ||
var result = workflowProvider.GetAllWorkflows(); | ||
result.Should().BeEquivalentTo(workflows); | ||
} | ||
|
||
[TestMethod] | ||
public void When_using_from_json_file_it_should_add_workflow() | ||
{ | ||
//Arrange | ||
var expected = new List<Workflow> | ||
{ | ||
new("Test", | ||
[ | ||
new("initialize", "Variable", new Dictionary<string, object> | ||
{ | ||
{ "age", "1" }, | ||
{ "canWalk", "true" }, | ||
}), | ||
new("test variable value", "Variable", [], "age == 1 && canWalk == true") | ||
]) | ||
}; | ||
|
||
var services = new ServiceCollection(); | ||
var path = Path.Combine(Environment.CurrentDirectory, @"Providers\workflows.json"); | ||
services.AddJsonWorkflowProvider(path); | ||
|
||
//Act | ||
var provider = services.BuildServiceProvider(); | ||
|
||
//Assert | ||
var workflowProvider = provider.GetRequiredService<IWorkflowProvider>(); | ||
var result = workflowProvider.GetAllWorkflows(); | ||
result.Should().BeEquivalentTo(expected); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using ActionFlow.Domain.Engine; | ||
using ActionFlow.Engine.Providers; | ||
|
||
namespace ActionFlow.Tests.Providers; | ||
|
||
[TestClass] | ||
public class WorkflowProviderTests | ||
{ | ||
[TestMethod] | ||
public void When_getting_all_workflows_it_should_return_a_list() | ||
{ | ||
//Arrange | ||
var testWorkflows = new List<Workflow> | ||
{ | ||
new("test", []) | ||
}; | ||
var sut = new WorkflowProvider(testWorkflows); | ||
|
||
//Act | ||
var result = sut.GetAllWorkflows(); | ||
|
||
//Assert | ||
Assert.IsInstanceOfType(result, typeof(List<Workflow>)); | ||
Assert.IsTrue(result.Contains(testWorkflows[0])); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
[ | ||
{ | ||
"WorkflowName": "Test", | ||
"Steps": [ | ||
{ | ||
"Name": "initialize", | ||
"ActionType": "Variable", | ||
"ConditionExpression": null, | ||
"Properties": { | ||
"age": "1", | ||
"canWalk": "true" | ||
} | ||
}, | ||
{ | ||
"Name": "test variable value", | ||
"ActionType": "Variable", | ||
"ConditionExpression": "age == 1 \u0026\u0026 canWalk == true", | ||
"Properties": {} | ||
} | ||
], | ||
"OutputParameters": null | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,19 @@ | ||
namespace ActionFlow.Domain.Engine | ||
{ | ||
public class Step | ||
{ | ||
public Step(string name, string actionType, Dictionary<string, object>? properties = null, string? conditionExpression = null) | ||
{ | ||
Name = name; | ||
ActionType = actionType; | ||
Properties = properties ?? []; | ||
ConditionExpression = conditionExpression; | ||
} | ||
public class Step | ||
{ | ||
public Step(string name, string actionType, Dictionary<string, object>? properties = null, string? conditionExpression = null) | ||
{ | ||
Name = name; | ||
ActionType = actionType; | ||
Properties = properties ?? []; | ||
ConditionExpression = conditionExpression; | ||
} | ||
|
||
public string Name { get; } | ||
public string ActionType { get; } | ||
public string? ConditionExpression { get; } | ||
public Dictionary<string, object>? Properties { get; } | ||
} | ||
public string Name { get; } | ||
public string ActionType { get; } | ||
public string? ConditionExpression { get; } | ||
|
||
public Dictionary<string, object>? Properties { get; } | ||
} | ||
} |
49 changes: 40 additions & 9 deletions
49
ActionFlow/Engine/Providers/Extensions/WorkflowProviderBuiltinExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,59 @@ | ||
using ActionFlow.Domain.Engine; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using System.Text.Json; | ||
using System.Text.Json.Nodes; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace ActionFlow.Engine.Providers.Extensions; | ||
public static class WorkflowProviderBuiltinExtensions | ||
{ | ||
public static void UseFromJson(this IWorkflowProvider workflowProvider, JsonArray workflowsJson) | ||
public static void AddJsonWorkflowProvider(this IServiceCollection services, string jsonPath) | ||
{ | ||
var workflows = workflowsJson.Deserialize<Workflow[]>(); | ||
var jsonString = File.ReadAllText(jsonPath); | ||
var workflowsJson = JsonArray.Parse(jsonString)?.AsArray(); | ||
|
||
services.AddJsonWorkflowProvider(workflowsJson!); | ||
} | ||
|
||
public static void AddJsonWorkflowProvider(this IServiceCollection services, JsonArray workflowsJson) | ||
{ | ||
var options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true }; | ||
options.Converters.Add(new ObjectToStringConverter()); | ||
|
||
var workflows = workflowsJson.Deserialize<List<Workflow>>(options); | ||
|
||
if (workflows != null) | ||
{ | ||
foreach (var workflow in workflows) | ||
services.AddScoped<IWorkflowProvider>(x => | ||
{ | ||
workflowProvider.AddWorkflow(workflow); | ||
} | ||
return new WorkflowProvider([.. workflows]); | ||
}); | ||
} | ||
} | ||
|
||
public static void UseFromJson(this IWorkflowProvider workflowProvider, string jsonPath) | ||
public class ObjectToStringConverter : JsonConverter<object> | ||
{ | ||
var jsonBytes = File.ReadAllBytes(jsonPath); | ||
var workflowsJson = JsonArray.Parse(jsonBytes)?.AsArray(); | ||
public override bool CanConvert(Type typeToConvert) => typeof(object) == typeToConvert; | ||
|
||
workflowProvider.UseFromJson(workflowsJson!); | ||
public override object Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
if (reader.TokenType == JsonTokenType.Number) | ||
{ | ||
return reader.TryGetInt64(out long l) ? l.ToString() : reader.GetDouble().ToString(); | ||
} | ||
if (reader.TokenType == JsonTokenType.String) | ||
{ | ||
return reader.GetString(); | ||
} | ||
using (JsonDocument document = JsonDocument.ParseValue(ref reader)) | ||
{ | ||
return document.RootElement.Clone().ToString(); | ||
} | ||
} | ||
|
||
public override void Write(Utf8JsonWriter writer, object value, JsonSerializerOptions options) | ||
{ | ||
writer.WriteStringValue(value.ToString()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters