Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NET6 + ProtoBuf #28

Merged
merged 13 commits into from
Feb 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

# Serverless Workflow Specification - .NET SDK

Provides .NET 5.0 API/SPI and Model Validation for the [Serverless Workflow Specification](https://github.com/serverlessworkflow/specification)
Provides .NET 6.0 API/SPI and Model Validation for the [Serverless Workflow Specification](https://github.com/serverlessworkflow/specification)

With the SDK, you can:

Expand Down Expand Up @@ -46,10 +46,15 @@ var workflow = WorkflowDefinition.Create("MyWorkflow", "MyWorkflow", "1.0")
})
.Execute("fakeEventTrigger", action =>
{
action.Consume(e =>
e.WithName("fakeEvent")
.WithSource(new Uri("https://fakesource.com"))
.WithType("fakeType"));
action
.Consume(e =>
e.WithName("fakeEvent")
.WithSource(new Uri("https://fakesource.com"))
.WithType("fakeType"))
.ThenProduce(e =>
e.WithName("otherEvent")
.WithSource(new Uri("https://fakesource.com"))
.WithType("fakeType"));
}))
.End()
.Build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.31019.35
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ServerlessWorkflow.Sdk", "ServerlessWorkflow.Sdk\ServerlessWorkflow.Sdk.csproj", "{E174F5CC-F3DC-4370-AAC1-AC1D7724C792}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ServerlessWorkflow.Sdk", "src\ServerlessWorkflow.Sdk\ServerlessWorkflow.Sdk.csproj", "{E174F5CC-F3DC-4370-AAC1-AC1D7724C792}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ServerlessWorkflow.Sdk.UnitTests", "ServerlessWorkflow.Sdk.UnitTests\ServerlessWorkflow.Sdk.UnitTests.csproj", "{70AD35E0-0C14-4EBF-A841-B0D084392753}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ServerlessWorkflow.Sdk.UnitTests", "src\ServerlessWorkflow.Sdk.UnitTests\ServerlessWorkflow.Sdk.UnitTests.csproj", "{70AD35E0-0C14-4EBF-A841-B0D084392753}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,32 +39,70 @@ public class WorkflowReaderTests

protected IWorkflowReader Reader { get; } = WorkflowReader.Create();

[Fact(Skip = "Does not work on GIT")]
public async Task Read_LocalExamples()
[Fact]
public async Task Read_Yaml_ShouldWork()
{
//arrange
var yaml = File.ReadAllText(Path.Combine("Resources", "Workflows", "operation.yaml"));

//act
var workflow = await this.Reader.ParseAsync(yaml);
var parsedWorkflow = await this.Reader.ParseAsync(yaml);

//assert
workflow
parsedWorkflow
.Should()
.NotBeNull();
workflow.Events
parsedWorkflow.Events
.Should()
.NotBeEmpty();
workflow.Functions
parsedWorkflow.Functions
.Should()
.NotBeEmpty();
workflow.States
parsedWorkflow.States
.Should()
.NotBeEmpty();
parsedWorkflow.Metadata
.Should()
.NotBeNull();
parsedWorkflow.Metadata
.Get("podSize")
.Should()
.Be("small");
}

[Fact]
public async Task Read_OfficialExamples()
public async Task Read_Json_ShouldWork()
{
//arrange
var yaml = File.ReadAllText(Path.Combine("Resources", "Workflows", "operation.json"));

//act
var parsedWorkflow = await this.Reader.ParseAsync(yaml);

//assert
parsedWorkflow
.Should()
.NotBeNull();
parsedWorkflow.Events
.Should()
.NotBeEmpty();
parsedWorkflow.Functions
.Should()
.NotBeEmpty();
parsedWorkflow.States
.Should()
.NotBeEmpty();
parsedWorkflow.Metadata
.Should()
.NotBeNull();
parsedWorkflow.Metadata
.Get("podSize")
.Should()
.Be("small");
}

[Fact]
public async Task Read_OfficialExamples_ShouldWork()
{
IDictionary<string, string> errors = new Dictionary<string, string>();
await foreach(Example example in GetOfficialExamplesAsync())
Expand All @@ -81,8 +119,8 @@ public async Task Read_OfficialExamples()
}
}

[Fact(Skip = "Does not work on GIT")]
public async Task Read_ExternalDefinitions()
[Fact]
public async Task Read_ExternalDefinitions_ShouldWork()
{
//arrange
var yaml = File.ReadAllText(Path.Combine("Resources", "Workflows", "externalref.yaml"));
Expand All @@ -96,7 +134,7 @@ public async Task Read_ExternalDefinitions()
.NotBeNull();
workflow.Constants
.Should()
.NotBeEmpty();
.NotBeNull();
workflow.Secrets
.Should()
.NotBeEmpty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*
*/
using ProtoBuf;
using ServerlessWorkflow.Sdk.Models;
using ServerlessWorkflow.Sdk.Services.IO;
using System;
Expand All @@ -37,7 +38,7 @@ protected static WorkflowDefinition BuildWorkflow()
.WithExecutionTimeout(timeout =>
timeout.After(new TimeSpan(30, 2, 0, 0)))
.StartsWith("inject", flow =>
flow.Inject(new { username = "test", password = "123456", scopes = new string[] { "api", "test" } }))
flow.Inject(new { username = "test", password = "123456"/*, scopes = new string[] { "api", "test" }*/ }))
.Then("operation", flow =>
flow.Execute("fakeApiFunctionCall", action =>
{
Expand All @@ -49,17 +50,22 @@ protected static WorkflowDefinition BuildWorkflow()
})
.Execute("fakeEventTrigger", action =>
{
action.Consume(e =>
e.WithName("fakeEvent")
.WithSource(new Uri("https://fakesource.com"))
.WithType("fakeType"));
action
.Consume(e =>
e.WithName("fakeEvent")
.WithSource(new Uri("https://fakesource.com"))
.WithType("fakeType"))
.ThenProduce(e =>
e.WithName("otherEvent")
.WithSource(new Uri("https://fakesource.com"))
.WithType("fakeType"));
}))
.End()
.Build();
}

[Fact]
public async Task WriteYaml()
public async Task Write_Yaml_ShouldWork()
{
var workflow = BuildWorkflow();
using Stream stream = new MemoryStream();
Expand All @@ -74,18 +80,29 @@ public async Task WriteYaml()
}

[Fact]
public async Task WriteJson()
public async Task Write_Json_ShoudlWork()
{
var workflow = BuildWorkflow();
var toSerialize = BuildWorkflow();
using Stream stream = new MemoryStream();
this.Writer.Write(workflow, stream, WorkflowDefinitionFormat.Json);
this.Writer.Write(toSerialize, stream, WorkflowDefinitionFormat.Json);
stream.Flush();
stream.Position = 0;
using StreamReader reader = new(stream);
string json = reader.ReadToEnd();
stream.Position = 0;
workflow = await this.Reader.ReadAsync(stream);
Assert.NotNull(workflow);
var deserialized = await this.Reader.ReadAsync(stream);
Assert.NotNull(deserialized);
}

[Fact]
public void Write_Proto_ShouldWork()
{
var toSerialize = BuildWorkflow();
using var stream = new MemoryStream();
Serializer.Serialize(stream, toSerialize);
stream.Position = 0;
var deserialized = Serializer.Deserialize<WorkflowDefinition>(stream);
Assert.NotNull(deserialized);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void Validate_CallbackState_ActionNull_ShouldFail()
.NotBeNull();
result.Errors.Should()
.NotBeNullOrEmpty()
.And.Contain(e => e.ErrorCode == "States[0]" && e.ErrorMessage.StartsWith($"{nameof(CallbackStateDefinition)}.{nameof(CallbackStateDefinition.Action)}"));
.And.Contain(e => e.ErrorCode.StartsWith($"{nameof(CallbackStateDefinition)}.{nameof(CallbackStateDefinition.Action)}"));
}

[Fact]
Expand All @@ -60,7 +60,7 @@ public void Validate_CallbackState_EventNull_ShouldFail()
.NotBeNull();
result.Errors.Should()
.NotBeNullOrEmpty()
.And.Contain(e => e.ErrorCode == "States[0]" && e.ErrorMessage.Contains($"{nameof(CallbackStateDefinition)}.{nameof(CallbackStateDefinition.Event)}"));
.And.Contain(e => e.ErrorCode.Contains($"{nameof(CallbackStateDefinition)}.{nameof(CallbackStateDefinition.Event)}"));
}

[Fact]
Expand All @@ -83,7 +83,7 @@ public void Validate_CallbackState_EventNotFound_ShouldFail()
.NotBeNull();
result.Errors.Should()
.NotBeNullOrEmpty()
.And.Contain(e => e.ErrorCode == "States[0]" && e.ErrorMessage.Contains($"{nameof(CallbackStateDefinition)}.{nameof(CallbackStateDefinition.Event)}"));
.And.Contain(e => e.ErrorCode.Contains($"{nameof(CallbackStateDefinition)}.{nameof(CallbackStateDefinition.Event)}"));
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void Validate_EventState_NoTriggers_ShouldFail()
.NotBeNull();
result.Errors.Should()
.NotBeNullOrEmpty()
.And.Contain(e => e.ErrorCode == "States[0]" && e.ErrorMessage.StartsWith($"{nameof(EventStateDefinition)}.{nameof(EventStateDefinition.Triggers)}"));
.And.Contain(e => e.ErrorCode.StartsWith($"{nameof(EventStateDefinition)}.{nameof(EventStateDefinition.Triggers)}"));
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void Validate_InjectState_NoData_ShouldFail()
.NotBeNull();
result.Errors.Should()
.NotBeNullOrEmpty()
.And.Contain(e => e.ErrorCode == "States[0]" && e.ErrorMessage.StartsWith($"{nameof(InjectStateDefinition)}.{nameof(InjectStateDefinition.Data)}"));
.And.Contain(e => e.ErrorCode.StartsWith($"{nameof(InjectStateDefinition)}.{nameof(InjectStateDefinition.Data)}"));
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ public void Validate_SwitchState_NoDataOrEventConditions_ShouldFail()
.NotBeNull();
result.Errors.Should()
.NotBeNullOrEmpty()
.And.Contain(e => e.ErrorCode == "States[0]" && e.ErrorMessage.Contains($"{nameof(SwitchStateDefinition)}.{nameof(SwitchStateDefinition.DataConditions)}"))
.And.Contain(e => e.ErrorCode == "States[0]" && e.ErrorMessage.Contains($"{nameof(SwitchStateDefinition)}.{nameof(SwitchStateDefinition.EventConditions)}"));
.And.Contain(e => e.ErrorCode.Contains($"{nameof(SwitchStateDefinition)}.{nameof(SwitchStateDefinition.DataConditions)}"))
.And.Contain(e => e.ErrorCode.Contains($"{nameof(SwitchStateDefinition)}.{nameof(SwitchStateDefinition.EventConditions)}"));
}

[Fact]
Expand All @@ -60,7 +60,7 @@ public void Validate_SwitchState_NoDefaultCondition_ShouldFail()
.NotBeNull();
result.Errors.Should()
.NotBeNullOrEmpty()
.And.Contain(e => e.ErrorCode == "States[0]" && e.ErrorMessage.Contains($"{nameof(SwitchStateDefinition)}.{nameof(SwitchStateDefinition.DefaultCondition)}"));
.And.Contain(e => e.ErrorCode.Contains($"{nameof(SwitchStateDefinition)}.{nameof(SwitchStateDefinition.DefaultCondition)}"));
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,9 @@ public void Validate_Workflow_DuplicateEventNames_ShouldFail()
Id = "fake",
Name = "fake",
Version = "fake",
Events = new() { new() { Name = "fake", Source = "fake", Type = "fake" }, new() { Name = "fake", Source = "fake", Type = "fake" } }
Events = new() { new() { Name = "fake", Source = "fake", Type = "fake" }, new() { Name = "fake", Source = "fake", Type = "fake" } },
Start = "sleep",
States = new() { new SleepStateDefinition() {Name = "sleep", Delay = TimeSpan.FromSeconds(2), End = true } }
};

//act
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"id": "operation",
"name": "Operation",
"version": "0.1.0",
"events": [
{
"name": "ProducedEvent",
"kind": "produced",
"type": "producedEvent",
"source": "workflow"
},
{
"name": "ConsumedEvent",
"kind": "consumed",
"type": "consumedEvent",
"source": "workflow"
}
],
"functions": [
{
"name": "Function",
"operation": "http://fake.address"
}
],
"metadata": {
"podSize": "small"
},
"start": "Operation",
"states": [
{
"name": "Operation",
"type": "operation",
"actions": [
{
"name": "Function",
"functionRef": {
"refName": "Function",
"arguments": {
"message": "Hello world!"
}
}
},
{
"name": "Function",
"eventRef": {
"triggerEventRef": "ProducedEvent",
"resultEventRef": "ConsumedEvent"
}
}
],
"end": true
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ events:
functions:
- name: Function
operation: http://fake.address
metadata:
podSize: small
start: Operation
states:
- name: Operation
type: operation
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net6.0</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="5.10.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.1" />
<PackageReference Include="FluentAssertions" Version="6.4.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0" />
<PackageReference Include="Moq" Version="4.16.1" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.0.3">
<PackageReference Include="coverlet.collector" Version="3.1.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand All @@ -41,6 +41,9 @@
<None Update="Resources\secrets\default.yaml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Resources\workflows\operation.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Resources\workflows\operation.yaml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
Expand Down
Loading