diff --git a/src/Magnett.Automation.Core/Contexts/Context.cs b/src/Magnett.Automation.Core/Contexts/Context.cs index 4396a39..426306d 100644 --- a/src/Magnett.Automation.Core/Contexts/Context.cs +++ b/src/Magnett.Automation.Core/Contexts/Context.cs @@ -13,9 +13,11 @@ private Context(IContextVault contextVault) ?? throw new ArgumentNullException(nameof(contextVault)); } - public void Store(ContextField field, TValue value) + public Context Store(ContextField field, TValue value) { _contextVault.Set(field, value); + + return this; } public TValue Value(ContextField field) diff --git a/src/Magnett.Automation.Core/Magnett.Automation.Core.csproj b/src/Magnett.Automation.Core/Magnett.Automation.Core.csproj index b913a99..7832c5e 100644 --- a/src/Magnett.Automation.Core/Magnett.Automation.Core.csproj +++ b/src/Magnett.Automation.Core/Magnett.Automation.Core.csproj @@ -14,6 +14,11 @@ 0.50 10 net6.0 + Magnett.Automation + + + + diff --git a/src/Magnett.Automation.Core/StateMachines/IMachine.cs b/src/Magnett.Automation.Core/StateMachines/IMachine.cs index e5f9e97..d4cc4f6 100644 --- a/src/Magnett.Automation.Core/StateMachines/IMachine.cs +++ b/src/Magnett.Automation.Core/StateMachines/IMachine.cs @@ -8,5 +8,7 @@ public interface IMachine IMachine Dispatch(Enumeration action); IMachine Dispatch(string actionName); + public bool Equals(CommonNamedKey obj); + public bool Equals(Enumeration obj); } } \ No newline at end of file diff --git a/src/Magnett.Automation.Core/StateMachines/Implementations/Machine.cs b/src/Magnett.Automation.Core/StateMachines/Implementations/Machine.cs index efd5782..e34efd2 100644 --- a/src/Magnett.Automation.Core/StateMachines/Implementations/Machine.cs +++ b/src/Magnett.Automation.Core/StateMachines/Implementations/Machine.cs @@ -6,9 +6,9 @@ namespace Magnett.Automation.Core.StateMachines.Implementations { public class Machine : IMachine { - private readonly IMachineDefinition _definition; + protected readonly IMachineDefinition _definition; - private Machine(IMachineDefinition definition) + protected Machine(IMachineDefinition definition) { _definition = definition ?? throw new ArgumentNullException(nameof(definition)); @@ -47,9 +47,20 @@ public IMachine Dispatch(string actionName) } public IState State { get; private set; } + + public bool Equals(CommonNamedKey obj) + { + return State.Key.Equals(obj); + } + + public bool Equals(Enumeration obj) + { + return State.Key.Equals(obj); + } #endregion - + + public static IMachine Create(IMachineDefinition definition) { return new Machine(definition); diff --git a/src/Magnett.Automation.Core/StateMachines/Implementations/State.cs b/src/Magnett.Automation.Core/StateMachines/Implementations/State.cs index 38e0180..69e2ee0 100644 --- a/src/Magnett.Automation.Core/StateMachines/Implementations/State.cs +++ b/src/Magnett.Automation.Core/StateMachines/Implementations/State.cs @@ -7,7 +7,7 @@ [assembly: InternalsVisibleTo("Magnett.Automation.Core.UnitTest")] namespace Magnett.Automation.Core.StateMachines.Implementations { - internal class State : IState + internal class State : IState { private readonly TransitionList _transitionList; @@ -41,7 +41,12 @@ public bool IsFinalState() } #endregion - + + public override string ToString() + { + return Key.Name; + } + public static State Create(string name, TransitionList transitionList) { return new(name, transitionList); diff --git a/src/Magnett.Automation.Core/WorkFlows/IFlow.cs b/src/Magnett.Automation.Core/WorkFlows/IFlow.cs index f00ee2f..5104725 100644 --- a/src/Magnett.Automation.Core/WorkFlows/IFlow.cs +++ b/src/Magnett.Automation.Core/WorkFlows/IFlow.cs @@ -1,5 +1,6 @@ using System; using System.Threading.Tasks; +using Magnett.Automation.Core.Contexts; using Magnett.Automation.Core.WorkFlows.Implementations; namespace Magnett.Automation.Core.WorkFlows @@ -7,6 +8,8 @@ namespace Magnett.Automation.Core.WorkFlows public interface IFlow { Guid Id { get; } + + Context Context { get; } Task Run(); } diff --git a/src/Magnett.Automation.Core/WorkFlows/INode.cs b/src/Magnett.Automation.Core/WorkFlows/INode.cs index 417b58f..b0cc2f7 100644 --- a/src/Magnett.Automation.Core/WorkFlows/INode.cs +++ b/src/Magnett.Automation.Core/WorkFlows/INode.cs @@ -1,7 +1,9 @@ -namespace Magnett.Automation.Core.WorkFlows +using Magnett.Automation.Core.Contexts; + +namespace Magnett.Automation.Core.WorkFlows { public interface INode : INodeBase { - public NodeExit Execute(); + public NodeExit Execute(Context context); } } \ No newline at end of file diff --git a/src/Magnett.Automation.Core/WorkFlows/INodeAsync.cs b/src/Magnett.Automation.Core/WorkFlows/INodeAsync.cs index 5ad17d3..3c7729a 100644 --- a/src/Magnett.Automation.Core/WorkFlows/INodeAsync.cs +++ b/src/Magnett.Automation.Core/WorkFlows/INodeAsync.cs @@ -1,9 +1,10 @@ using System.Threading.Tasks; +using Magnett.Automation.Core.Contexts; namespace Magnett.Automation.Core.WorkFlows { public interface INodeAsync : INodeBase { - Task Execute(); + Task Execute(Context context); } } \ No newline at end of file diff --git a/src/Magnett.Automation.Core/WorkFlows/INodeBase.cs b/src/Magnett.Automation.Core/WorkFlows/INodeBase.cs index 626f80b..e8be1ab 100644 --- a/src/Magnett.Automation.Core/WorkFlows/INodeBase.cs +++ b/src/Magnett.Automation.Core/WorkFlows/INodeBase.cs @@ -6,8 +6,6 @@ namespace Magnett.Automation.Core.WorkFlows public interface INodeBase { public CommonNamedKey Key { get; } - public bool IsInit { get; } public string Name => Key?.Name; - void Init(Context flowContext); } } \ No newline at end of file diff --git a/src/Magnett.Automation.Core/WorkFlows/Implementations/Flow.cs b/src/Magnett.Automation.Core/WorkFlows/Implementations/Flow.cs index 1c7bfd3..6b8f7cc 100644 --- a/src/Magnett.Automation.Core/WorkFlows/Implementations/Flow.cs +++ b/src/Magnett.Automation.Core/WorkFlows/Implementations/Flow.cs @@ -1,5 +1,6 @@ using System; using System.Threading.Tasks; +using Magnett.Automation.Core.Contexts; namespace Magnett.Automation.Core.WorkFlows.Implementations { @@ -16,7 +17,8 @@ private Flow(IFlowRunner flowRunner) #region IFlow public Guid Id { get; } = Guid.NewGuid(); - + public Context Context => _flowRunner.FlowContext; + public async Task Run() { return await _flowRunner.Start(); diff --git a/src/Magnett.Automation.Core/WorkFlows/Implementations/FlowRunnerBase.cs b/src/Magnett.Automation.Core/WorkFlows/Implementations/FlowRunnerBase.cs index 28afa28..f94f2d4 100644 --- a/src/Magnett.Automation.Core/WorkFlows/Implementations/FlowRunnerBase.cs +++ b/src/Magnett.Automation.Core/WorkFlows/Implementations/FlowRunnerBase.cs @@ -24,12 +24,10 @@ protected FlowRunnerBase(IFlowDefinition definition, Context context) protected async Task ExecuteNode(INodeBase node) { - if (!node.IsInit) node.Init(FlowContext); - return node switch { - INodeAsync nodeAsync => await nodeAsync.Execute(), - INode nodeSync => await Task.Run(() => nodeSync.Execute()), + INodeAsync nodeAsync => await nodeAsync.Execute(FlowContext), + INode nodeSync => await Task.Run(() => nodeSync.Execute(FlowContext)), { } => throw new ArgumentException("Not a valid node"), null => throw new ArgumentNullException(nameof(node)) }; diff --git a/src/Magnett.Automation.Core/WorkFlows/Implementations/Node.cs b/src/Magnett.Automation.Core/WorkFlows/Implementations/Node.cs index e78259a..b4a6b54 100644 --- a/src/Magnett.Automation.Core/WorkFlows/Implementations/Node.cs +++ b/src/Magnett.Automation.Core/WorkFlows/Implementations/Node.cs @@ -1,4 +1,5 @@ using Magnett.Automation.Core.Commons; +using Magnett.Automation.Core.Contexts; namespace Magnett.Automation.Core.WorkFlows.Implementations { @@ -14,6 +15,6 @@ protected Node(CommonNamedKey key) : base(key) } - public abstract NodeExit Execute(); + public abstract NodeExit Execute(Context context); } } \ No newline at end of file diff --git a/src/Magnett.Automation.Core/WorkFlows/Implementations/NodeAsync.cs b/src/Magnett.Automation.Core/WorkFlows/Implementations/NodeAsync.cs index 8a6a5d7..0cb75f1 100644 --- a/src/Magnett.Automation.Core/WorkFlows/Implementations/NodeAsync.cs +++ b/src/Magnett.Automation.Core/WorkFlows/Implementations/NodeAsync.cs @@ -1,4 +1,5 @@ using System.Threading.Tasks; +using Magnett.Automation.Core.Contexts; namespace Magnett.Automation.Core.WorkFlows.Implementations { @@ -8,6 +9,6 @@ protected NodeAsync(string name) : base(name) { } - public abstract Task Execute(); + public abstract Task Execute(Context context); } } \ No newline at end of file diff --git a/src/Magnett.Automation.Core/WorkFlows/Implementations/NodeBase.cs b/src/Magnett.Automation.Core/WorkFlows/Implementations/NodeBase.cs index 1b7a599..fcaee63 100644 --- a/src/Magnett.Automation.Core/WorkFlows/Implementations/NodeBase.cs +++ b/src/Magnett.Automation.Core/WorkFlows/Implementations/NodeBase.cs @@ -6,9 +6,7 @@ namespace Magnett.Automation.Core.WorkFlows.Implementations { public abstract class NodeBase { - protected Context GlobalContext { get; private set; } public CommonNamedKey Key { get; } - public bool IsInit { get; private set; } protected NodeBase(string name) : this(CommonNamedKey.Create(name)) { @@ -20,13 +18,5 @@ protected NodeBase(CommonNamedKey key) Key = key ?? throw new ArgumentNullException(nameof(key)); } - - public virtual void Init(Context globalContext) - { - GlobalContext = globalContext - ?? throw new ArgumentNullException(nameof(globalContext)); - - IsInit = true; - } } } \ No newline at end of file diff --git a/src/Magnett.Automation.Core/WorkFlows/NodeExit.cs b/src/Magnett.Automation.Core/WorkFlows/NodeExit.cs index ff182b7..e3c7524 100644 --- a/src/Magnett.Automation.Core/WorkFlows/NodeExit.cs +++ b/src/Magnett.Automation.Core/WorkFlows/NodeExit.cs @@ -29,5 +29,13 @@ public static NodeExit Create( { return new NodeExit(code, isError, data); } + + public static NodeExit Create( + Enumeration code, + bool isError = false, + string data = null) + { + return new NodeExit(code?.Name, isError, data); + } } } \ No newline at end of file diff --git a/test/Magnett.Automation.Core.IntegrationTest/WorkFlows/SagaPattern/Definitions/Entities/Order.cs b/test/Magnett.Automation.Core.IntegrationTest/WorkFlows/SagaPattern/Definitions/Entities/Order.cs new file mode 100644 index 0000000..f7cbdc2 --- /dev/null +++ b/test/Magnett.Automation.Core.IntegrationTest/WorkFlows/SagaPattern/Definitions/Entities/Order.cs @@ -0,0 +1,26 @@ +using System; +using Magnett.Automation.Core.IntegrationTest.WorkFlows.SagaPattern.Definitions.States; + +namespace Magnett.Automation.Core.IntegrationTest.WorkFlows.SagaPattern.Definitions.Entities; + +public class Order +{ + public OrderState State { get; } + public Guid Id { get; } + public double Amount { get; } + public string Description { get; } + + private Order(double amount, string description) + { + Id = Guid.NewGuid(); + Amount = amount; + Description = description; + + State = OrderState.Create(); + } + + public static Order Create(double amount, string description) + { + return new Order(amount, description); + } +} \ No newline at end of file diff --git a/test/Magnett.Automation.Core.IntegrationTest/WorkFlows/SagaPattern/Definitions/Entities/Payment.cs b/test/Magnett.Automation.Core.IntegrationTest/WorkFlows/SagaPattern/Definitions/Entities/Payment.cs new file mode 100644 index 0000000..d9d39cd --- /dev/null +++ b/test/Magnett.Automation.Core.IntegrationTest/WorkFlows/SagaPattern/Definitions/Entities/Payment.cs @@ -0,0 +1,25 @@ +using System; +using Magnett.Automation.Core.IntegrationTest.WorkFlows.SagaPattern.Definitions.States; +using Magnett.Automation.Core.StateMachines; + +namespace Magnett.Automation.Core.IntegrationTest.WorkFlows.SagaPattern.Definitions.Entities; + +public class Payment +{ + public Guid Id { get; } + public double Amount { get; } + public PaymentState State { get; } + + private Payment(double amount) + { + Id = Guid.NewGuid(); + Amount = amount; + + State = PaymentState.Create(); + } + + public static Payment Create(double amount) + { + return new Payment(amount); + } +} \ No newline at end of file diff --git a/test/Magnett.Automation.Core.IntegrationTest/WorkFlows/SagaPattern/Definitions/NodeName.cs b/test/Magnett.Automation.Core.IntegrationTest/WorkFlows/SagaPattern/Definitions/NodeName.cs new file mode 100644 index 0000000..b08f892 --- /dev/null +++ b/test/Magnett.Automation.Core.IntegrationTest/WorkFlows/SagaPattern/Definitions/NodeName.cs @@ -0,0 +1,20 @@ +using Magnett.Automation.Core.Commons; + +namespace Magnett.Automation.Core.IntegrationTest.WorkFlows.SagaPattern.Definitions +{ + internal class NodeName : CommonNamedKey + { + public static readonly NodeName CreateOrder = new NodeName(nameof(CreateOrder)); + public static readonly NodeName ConfirmOrder = new NodeName(nameof(ConfirmOrder)); + public static readonly NodeName CancelOrder = new NodeName(nameof(CancelOrder)); + + public static readonly NodeName PreAuthorizePayment = new NodeName(nameof(PreAuthorizePayment)); + public static readonly NodeName ConfirmPayment = new NodeName(nameof(ConfirmPayment)); + public static readonly NodeName CancelPayment = new NodeName(nameof(CancelPayment)); + + private NodeName(string name) : base(name) + { + + } + } +} \ No newline at end of file diff --git a/test/Magnett.Automation.Core.IntegrationTest/WorkFlows/SagaPattern/Definitions/Nodes/CancelOrder.cs b/test/Magnett.Automation.Core.IntegrationTest/WorkFlows/SagaPattern/Definitions/Nodes/CancelOrder.cs new file mode 100644 index 0000000..2e1370c --- /dev/null +++ b/test/Magnett.Automation.Core.IntegrationTest/WorkFlows/SagaPattern/Definitions/Nodes/CancelOrder.cs @@ -0,0 +1,53 @@ +using System.Threading.Tasks; +using Magnett.Automation.Core.Commons; +using Magnett.Automation.Core.Contexts; +using Magnett.Automation.Core.IntegrationTest.WorkFlows.SagaPattern.Definitions.Entities; +using Magnett.Automation.Core.IntegrationTest.WorkFlows.SagaPattern.Definitions.States; +using Magnett.Automation.Core.WorkFlows; +using Magnett.Automation.Core.WorkFlows.Implementations; + +namespace Magnett.Automation.Core.IntegrationTest.WorkFlows.SagaPattern.Definitions.Nodes; + +/// +/// Rollback Order operation and cancel process +/// +public class CancelOrder : NodeAsync +{ + private CancelOrder(string name) : base(name) + { + } + + private readonly ContextField _orderField = ContextField.Create("Order"); + + #region ExitCodes + + public class ExitCode : Enumeration + { + public static readonly ExitCode Done = new ExitCode(1, nameof(Done)); + + private ExitCode(int id, string name) : base(id, name) + { + } + } + + #endregion + + public override async Task Execute(Context context) + { + var order = context.Value(_orderField); + + order.State.Dispatch(OrderStateDefinition.Action.Cancel); + + await Task.Delay(1000); + + return NodeExit.Create( + ExitCode.Done, + true, + $"Order cancelled id [{order.Id}] [{order.State}]"); + } + + public static CancelOrder Create(CommonNamedKey nodeName) + { + return new CancelOrder(nodeName?.Name); + } +} \ No newline at end of file diff --git a/test/Magnett.Automation.Core.IntegrationTest/WorkFlows/SagaPattern/Definitions/Nodes/CancelPayment.cs b/test/Magnett.Automation.Core.IntegrationTest/WorkFlows/SagaPattern/Definitions/Nodes/CancelPayment.cs new file mode 100644 index 0000000..fe7abad --- /dev/null +++ b/test/Magnett.Automation.Core.IntegrationTest/WorkFlows/SagaPattern/Definitions/Nodes/CancelPayment.cs @@ -0,0 +1,53 @@ +using System.Threading.Tasks; +using Magnett.Automation.Core.Commons; +using Magnett.Automation.Core.Contexts; +using Magnett.Automation.Core.IntegrationTest.WorkFlows.SagaPattern.Definitions.Entities; +using Magnett.Automation.Core.IntegrationTest.WorkFlows.SagaPattern.Definitions.States; +using Magnett.Automation.Core.WorkFlows; +using Magnett.Automation.Core.WorkFlows.Implementations; + +namespace Magnett.Automation.Core.IntegrationTest.WorkFlows.SagaPattern.Definitions.Nodes; + +/// +/// Cancel payment. and rollback pre-authorization +/// +public class CancelPayment : NodeAsync +{ + private CancelPayment(string name) : base(name) + { + } + + private readonly ContextField _paymentField = ContextField.Create("Payment"); + + #region ExitCodes + + public class ExitCode : Enumeration + { + public static readonly ExitCode Done = new ExitCode(1, nameof(Done)); + + private ExitCode(int id, string name) : base(id, name) + { + } + } + + #endregion + + public override async Task Execute(Context context) + { + var payment = context.Value(_paymentField); + + payment.State.Dispatch(PaymentStateDefinition.Action.Cancel); + + await Task.Delay(1000); + + return NodeExit.Create( + ExitCode.Done, + true, + $"Payment cancelled id [{payment.Id}] [{payment.State}]"); + } + + public static CancelPayment Create(CommonNamedKey nodeName) + { + return new CancelPayment(nodeName?.Name); + } +} \ No newline at end of file diff --git a/test/Magnett.Automation.Core.IntegrationTest/WorkFlows/SagaPattern/Definitions/Nodes/ConfirmOrder.cs b/test/Magnett.Automation.Core.IntegrationTest/WorkFlows/SagaPattern/Definitions/Nodes/ConfirmOrder.cs new file mode 100644 index 0000000..3cba30c --- /dev/null +++ b/test/Magnett.Automation.Core.IntegrationTest/WorkFlows/SagaPattern/Definitions/Nodes/ConfirmOrder.cs @@ -0,0 +1,53 @@ +using System.Threading.Tasks; +using Magnett.Automation.Core.Commons; +using Magnett.Automation.Core.Contexts; +using Magnett.Automation.Core.IntegrationTest.WorkFlows.SagaPattern.Definitions.Entities; +using Magnett.Automation.Core.IntegrationTest.WorkFlows.SagaPattern.Definitions.States; +using Magnett.Automation.Core.WorkFlows; +using Magnett.Automation.Core.WorkFlows.Implementations; + +namespace Magnett.Automation.Core.IntegrationTest.WorkFlows.SagaPattern.Definitions.Nodes; + +/// +/// Confirm Order after payment, close transaction +/// +public class ConfirmOrder : NodeAsync +{ + private ConfirmOrder(string name) : base(name) + { + } + + private readonly ContextField _orderField = ContextField.Create("Order"); + + #region ExitCodes + + public class ExitCode : Enumeration + { + public static readonly ExitCode Done = new ExitCode(1, nameof(Done)); + + private ExitCode(int id, string name) : base(id, name) + { + } + } + + #endregion + + public override async Task Execute(Context context) + { + var order = context.Value(_orderField); + + order.State.Dispatch(OrderStateDefinition.Action.Confirm); + + await Task.Delay(1000); + + return NodeExit.Create( + ExitCode.Done, + false, + $"Order confirmed id [{order.Id}] [{order.State}]"); + } + + public static ConfirmOrder Create(CommonNamedKey nodeName) + { + return new ConfirmOrder(nodeName?.Name); + } +} \ No newline at end of file diff --git a/test/Magnett.Automation.Core.IntegrationTest/WorkFlows/SagaPattern/Definitions/Nodes/ConfirmPayment.cs b/test/Magnett.Automation.Core.IntegrationTest/WorkFlows/SagaPattern/Definitions/Nodes/ConfirmPayment.cs new file mode 100644 index 0000000..6c9c787 --- /dev/null +++ b/test/Magnett.Automation.Core.IntegrationTest/WorkFlows/SagaPattern/Definitions/Nodes/ConfirmPayment.cs @@ -0,0 +1,75 @@ +using System; +using System.Threading.Tasks; + +using Magnett.Automation.Core.Commons; +using Magnett.Automation.Core.Contexts; +using Magnett.Automation.Core.IntegrationTest.WorkFlows.SagaPattern.Definitions.Entities; +using Magnett.Automation.Core.IntegrationTest.WorkFlows.SagaPattern.Definitions.States; +using Magnett.Automation.Core.WorkFlows; +using Magnett.Automation.Core.WorkFlows.Implementations; + +namespace Magnett.Automation.Core.IntegrationTest.WorkFlows.SagaPattern.Definitions.Nodes; + +/// +/// Try to make payment +/// +public class ConfirmPayment : NodeAsync +{ + private readonly ContextField _canMakePayment = ContextField.Create("CanMakePayment"); + private readonly ContextField _paymentField = ContextField.Create("Payment"); + + private ConfirmPayment(string name) : base(name) + { + } + + #region ExitCodes + + public class ExitCode : Enumeration + { + public static readonly ExitCode Done = new ExitCode(1, nameof(Done)); + public static readonly ExitCode Failed = new ExitCode(1, nameof(Failed)); + + private ExitCode(int id, string name) : base(id, name) + { + } + } + + #endregion + + private NodeExit Confirm(Payment payment) + { + payment.State.Dispatch(PaymentStateDefinition.Action.Confirm); + + return NodeExit.Create( + ExitCode.Done, + false, + $"Payment is Done"); + } + + private NodeExit Fail(Payment payment) + { + payment.State.Dispatch(PaymentStateDefinition.Action.Cancel); + + return NodeExit.Create( + ExitCode.Failed, + true, + $"Payment Failed"); + } + + + public override async Task Execute(Context context) + { + var payment = context.Value(_paymentField); + + await Task.Delay(1000); + + return context.Value(_canMakePayment) + ? Confirm(payment) + : Fail(payment); + } + + public static ConfirmPayment Create(CommonNamedKey nodeName) + { + return new ConfirmPayment(nodeName?.Name); + } +} \ No newline at end of file diff --git a/test/Magnett.Automation.Core.IntegrationTest/WorkFlows/SagaPattern/Definitions/Nodes/CreateOrder.cs b/test/Magnett.Automation.Core.IntegrationTest/WorkFlows/SagaPattern/Definitions/Nodes/CreateOrder.cs new file mode 100644 index 0000000..5b2b8b2 --- /dev/null +++ b/test/Magnett.Automation.Core.IntegrationTest/WorkFlows/SagaPattern/Definitions/Nodes/CreateOrder.cs @@ -0,0 +1,60 @@ +using System; +using System.Threading.Tasks; +using Magnett.Automation.Core.Commons; +using Magnett.Automation.Core.Contexts; +using Magnett.Automation.Core.IntegrationTest.WorkFlows.SagaPattern.Definitions.Entities; +using Magnett.Automation.Core.IntegrationTest.WorkFlows.SagaPattern.Definitions.States; +using Magnett.Automation.Core.WorkFlows; +using Magnett.Automation.Core.WorkFlows.Implementations; + +namespace Magnett.Automation.Core.IntegrationTest.WorkFlows.SagaPattern.Definitions.Nodes; + +/// +/// Create order record, begin transaction +/// +public class CreateOrder : NodeAsync +{ + private readonly ContextField _orderField = ContextField.Create("Order"); + private readonly ContextField _amountField = ContextField.Create("Amount"); + private readonly ContextField _descriptionField = ContextField.Create("Description"); + + #region ExitCodes + + public class ExitCode : Enumeration + { + public static readonly ExitCode Created = new ExitCode(1, nameof(Created)); + + private ExitCode(int id, string name) : base(id, name) + { + } + } + + #endregion + + private CreateOrder(string name) : base(name) + { + } + + public override async Task Execute(Context context) + { + var order = Order.Create( + context.Value(_amountField), + context.Value(_descriptionField)); + + order.State.Dispatch(OrderStateDefinition.Action.Validate); + + context.Store(_orderField, order); + + await Task.Delay(1000); + + return NodeExit.Create( + ExitCode.Created, + false, + $"Order id [{order.Id}] [{order.State.State.Key}]"); + } + + public static CreateOrder Create(CommonNamedKey nodeName) + { + return new CreateOrder(nodeName?.Name); + } +} \ No newline at end of file diff --git a/test/Magnett.Automation.Core.IntegrationTest/WorkFlows/SagaPattern/Definitions/Nodes/PreAuthorizePayment.cs b/test/Magnett.Automation.Core.IntegrationTest/WorkFlows/SagaPattern/Definitions/Nodes/PreAuthorizePayment.cs new file mode 100644 index 0000000..86706b2 --- /dev/null +++ b/test/Magnett.Automation.Core.IntegrationTest/WorkFlows/SagaPattern/Definitions/Nodes/PreAuthorizePayment.cs @@ -0,0 +1,85 @@ +using System; +using System.Threading.Tasks; + +using Magnett.Automation.Core.Commons; +using Magnett.Automation.Core.Contexts; +using Magnett.Automation.Core.WorkFlows; +using Magnett.Automation.Core.WorkFlows.Implementations; +using Magnett.Automation.Core.IntegrationTest.WorkFlows.SagaPattern.Definitions.Entities; +using Magnett.Automation.Core.IntegrationTest.WorkFlows.SagaPattern.Definitions.States; + +namespace Magnett.Automation.Core.IntegrationTest.WorkFlows.SagaPattern.Definitions.Nodes; + +/// +/// Check Limit Funds for payment +/// +public class PreAuthorizePayment : NodeAsync +{ + private readonly ContextField _orderField = ContextField.Create("Order"); + private readonly ContextField _creditField = ContextField.Create("CreditLimit"); + private readonly ContextField _paymentField = ContextField.Create("Payment"); + + private double _credit; + private Order _order; + private Payment _payment; + + private PreAuthorizePayment(string name) : base(name) + { + + } + + #region ExitCodes + + public class ExitCode : Enumeration + { + public static readonly ExitCode PreAuthorized = new ExitCode(1, nameof(PreAuthorized)); + public static readonly ExitCode Denied = new ExitCode(2, nameof(Denied)); + + private ExitCode(int id, string name) : base(id, name) + { + } + } + + #endregion + + private NodeExit PreAuthorize() + { + _payment.State.Dispatch(PaymentStateDefinition.Action.PreAuthorize); + + return NodeExit.Create( + ExitCode.PreAuthorized, + false, + $"Payment is pre-authorized, amount of [{_payment.Amount}] credit [{_credit}]"); + } + + private NodeExit Deny() + { + _payment.State.Dispatch(PaymentStateDefinition.Action.Deny); + + return NodeExit.Create( + ExitCode.Denied, + true, + $"Payment denied, amount of [{_payment.Amount}] credit [{_credit}]"); + } + + public override async Task Execute(Context context) + { + _order = context.Value(_orderField); + + _credit = context.Value(_creditField); + + _payment = Payment.Create(_order.Amount); + context.Store(_paymentField, _payment); + + await Task.Delay(1000); + + return _credit >= _order.Amount + ? PreAuthorize() + : Deny(); + } + + public static PreAuthorizePayment Create(CommonNamedKey nodeName) + { + return new PreAuthorizePayment(nodeName?.Name); + } +} \ No newline at end of file diff --git a/test/Magnett.Automation.Core.IntegrationTest/WorkFlows/SagaPattern/Definitions/SagaPatternDefinition.cs b/test/Magnett.Automation.Core.IntegrationTest/WorkFlows/SagaPattern/Definitions/SagaPatternDefinition.cs new file mode 100644 index 0000000..fa0df8c --- /dev/null +++ b/test/Magnett.Automation.Core.IntegrationTest/WorkFlows/SagaPattern/Definitions/SagaPatternDefinition.cs @@ -0,0 +1,38 @@ +using Magnett.Automation.Core.WorkFlows; +using Magnett.Automation.Core.WorkFlows.Builders; +using Magnett.Automation.Core.IntegrationTest.WorkFlows.SagaPattern.Definitions.Nodes; + +namespace Magnett.Automation.Core.IntegrationTest.WorkFlows.SagaPattern.Definitions; + +public static class SagaPatternDefinition +{ + public static IFlowDefinition Definition { get; } + + static SagaPatternDefinition() + { + Definition = FlowDefinitionBuilder.Create() + .WithInitialNode(CreateOrder.Create(NodeName.CreateOrder)) + .OnExitCode(CreateOrder.ExitCode.Created).GoTo(NodeName.PreAuthorizePayment) + .Build() + + .WithNode(PreAuthorizePayment.Create(NodeName.PreAuthorizePayment)) + .OnExitCode(PreAuthorizePayment.ExitCode.PreAuthorized).GoTo(NodeName.ConfirmPayment) + .OnExitCode(PreAuthorizePayment.ExitCode.Denied).GoTo(NodeName.CancelPayment) + .Build() + + .WithNode(ConfirmPayment.Create(NodeName.ConfirmPayment)) + .OnExitCode(ConfirmPayment.ExitCode.Done).GoTo(NodeName.ConfirmOrder) + .OnExitCode(ConfirmPayment.ExitCode.Failed).GoTo(NodeName.CancelPayment) + .Build() + + .WithNode(ConfirmOrder.Create(NodeName.ConfirmOrder)).Build() + + .WithNode(CancelPayment.Create(NodeName.CancelPayment)) + .OnExitCode(CancelPayment.ExitCode.Done).GoTo(NodeName.CancelOrder) + .Build() + + .WithNode(CancelOrder.Create(NodeName.CancelOrder)).Build() + + .BuildDefinition(); + } +} \ No newline at end of file diff --git a/test/Magnett.Automation.Core.IntegrationTest/WorkFlows/SagaPattern/Definitions/States/OrderState.cs b/test/Magnett.Automation.Core.IntegrationTest/WorkFlows/SagaPattern/Definitions/States/OrderState.cs new file mode 100644 index 0000000..c7e9e2c --- /dev/null +++ b/test/Magnett.Automation.Core.IntegrationTest/WorkFlows/SagaPattern/Definitions/States/OrderState.cs @@ -0,0 +1,22 @@ +using Magnett.Automation.Core.Commons; +using Magnett.Automation.Core.StateMachines; +using Magnett.Automation.Core.StateMachines.Implementations; + +namespace Magnett.Automation.Core.IntegrationTest.WorkFlows.SagaPattern.Definitions.States; + +public class OrderState : Machine +{ + private OrderState(IMachineDefinition definition) : base(definition) + { + } + + public static OrderState Create() + { + return new OrderState(OrderStateDefinition.Definition); + } + + public override string ToString() + { + return State.ToString(); + } +} \ No newline at end of file diff --git a/test/Magnett.Automation.Core.IntegrationTest/WorkFlows/SagaPattern/Definitions/States/OrderStateDefinition.cs b/test/Magnett.Automation.Core.IntegrationTest/WorkFlows/SagaPattern/Definitions/States/OrderStateDefinition.cs new file mode 100644 index 0000000..abce875 --- /dev/null +++ b/test/Magnett.Automation.Core.IntegrationTest/WorkFlows/SagaPattern/Definitions/States/OrderStateDefinition.cs @@ -0,0 +1,52 @@ +using Magnett.Automation.Core.Commons; +using Magnett.Automation.Core.StateMachines; +using Magnett.Automation.Core.StateMachines.Builders; + +namespace Magnett.Automation.Core.IntegrationTest.WorkFlows.SagaPattern.Definitions.States; + +internal static class OrderStateDefinition +{ + public static IMachineDefinition Definition { get; } + + static OrderStateDefinition() + { + Definition = MachineDefinitionBuilder.Create() + + .InitialState(State.New) + .OnAction(Action.Validate).ToState(State.Pending) + .Build() + + .AddState(State.Pending) + .OnAction(Action.Confirm).ToState(State.Confirmed) + .OnAction(Action.Cancel).ToState(State.Cancelled) + .Build() + + .AddState(State.Confirmed).Build() + + .AddState(State.Cancelled).Build() + + .BuildDefinition(); + } + public class State : Enumeration + { + public static readonly State New = new State(1, nameof(New)); + public static readonly State Pending = new State(2, nameof(Pending)); + public static readonly State Confirmed = new State(3, nameof(Confirmed)); + public static readonly State Cancelled = new State(4, nameof(Cancelled)); + + private State(int id, string name) : base(id, name) + { + } + } + + public class Action : Enumeration + { + public static readonly Action Validate = new Action(1, nameof(Validate)); + public static readonly Action Confirm = new Action(2, nameof(Confirm)); + public static readonly Action Cancel = new Action(3, nameof(Cancel)); + + private Action(int id, string name) : base(id, name) + { + } + } +} \ No newline at end of file diff --git a/test/Magnett.Automation.Core.IntegrationTest/WorkFlows/SagaPattern/Definitions/States/PaymentState.cs b/test/Magnett.Automation.Core.IntegrationTest/WorkFlows/SagaPattern/Definitions/States/PaymentState.cs new file mode 100644 index 0000000..d20cf12 --- /dev/null +++ b/test/Magnett.Automation.Core.IntegrationTest/WorkFlows/SagaPattern/Definitions/States/PaymentState.cs @@ -0,0 +1,16 @@ +using Magnett.Automation.Core.StateMachines; +using Magnett.Automation.Core.StateMachines.Implementations; + +namespace Magnett.Automation.Core.IntegrationTest.WorkFlows.SagaPattern.Definitions.States; + +public class PaymentState : Machine +{ + private PaymentState(IMachineDefinition definition) : base(definition) + { + } + + public static PaymentState Create() + { + return new PaymentState(PaymentStateDefinition.GetDefinition()); + } +} \ No newline at end of file diff --git a/test/Magnett.Automation.Core.IntegrationTest/WorkFlows/SagaPattern/Definitions/States/PaymetStateDefinition.cs b/test/Magnett.Automation.Core.IntegrationTest/WorkFlows/SagaPattern/Definitions/States/PaymetStateDefinition.cs new file mode 100644 index 0000000..c020f3e --- /dev/null +++ b/test/Magnett.Automation.Core.IntegrationTest/WorkFlows/SagaPattern/Definitions/States/PaymetStateDefinition.cs @@ -0,0 +1,69 @@ +using Magnett.Automation.Core.Commons; +using Magnett.Automation.Core.StateMachines; +using Magnett.Automation.Core.StateMachines.Builders; + +namespace Magnett.Automation.Core.IntegrationTest.WorkFlows.SagaPattern.Definitions.States; + +internal static class PaymentStateDefinition +{ + private static IMachineDefinition _definition; + + static PaymentStateDefinition() + { + CreateDefinition(); + } + public class State : Enumeration + { + public static readonly State New = new State(1, nameof(New)); + public static readonly State PreAuthorized = new State(2, nameof(PreAuthorized)); + public static readonly State Denied = new State(3, nameof(Denied)); + public static readonly State Confirmed = new State(4, nameof(Confirmed)); + public static readonly State Cancelled = new State(5, nameof(Cancelled)); + + private State(int id, string name) : base(id, name) + { + } + } + + public class Action : Enumeration + { + public static readonly Action PreAuthorize = new Action(1, nameof(PreAuthorize)); + public static readonly Action Deny = new Action(2, nameof(Deny)); + public static readonly Action Confirm = new Action(3, nameof(Confirm)); + public static readonly Action Cancel = new Action(4, nameof(Cancel)); + + private Action(int id, string name) : base(id, name) + { + } + } + + private static void CreateDefinition() + { + _definition = MachineDefinitionBuilder.Create() + + .InitialState(State.New) + .OnAction(Action.PreAuthorize).ToState(State.PreAuthorized) + .OnAction(Action.Deny).ToState(State.Denied) + .Build() + + .AddState(State.PreAuthorized) + .OnAction(Action.Confirm).ToState(State.Confirmed) + .OnAction(Action.Cancel).ToState(State.Cancelled) + .Build() + + .AddState(State.Denied) + .OnAction(Action.Cancel).ToState(State.Cancelled) + .Build() + + .AddState(State.Confirmed).Build() + + .AddState(State.Cancelled).Build() + + .BuildDefinition(); + } + + public static IMachineDefinition GetDefinition() + { + return _definition; + } +} \ No newline at end of file diff --git a/test/Magnett.Automation.Core.IntegrationTest/WorkFlows/SagaPattern/Saga.cs b/test/Magnett.Automation.Core.IntegrationTest/WorkFlows/SagaPattern/Saga.cs new file mode 100644 index 0000000..d54b449 --- /dev/null +++ b/test/Magnett.Automation.Core.IntegrationTest/WorkFlows/SagaPattern/Saga.cs @@ -0,0 +1,49 @@ +using System.Threading.Tasks; +using Magnett.Automation.Core.Contexts; +using Magnett.Automation.Core.IntegrationTest.WorkFlows.SagaPattern.Definitions; +using Magnett.Automation.Core.IntegrationTest.WorkFlows.SagaPattern.Definitions.Entities; +using Magnett.Automation.Core.WorkFlows; +using Magnett.Automation.Core.WorkFlows.Implementations; + +namespace Magnett.Automation.Core.IntegrationTest.WorkFlows.SagaPattern; + +public class Saga +{ + private readonly ContextField _amountField = ContextField.Create("Amount"); + private readonly ContextField _descriptionField = ContextField.Create("Description"); + private readonly ContextField _creditLimitField = ContextField.Create("CreditLimit"); + private readonly ContextField _canMakePayment = ContextField.Create("CanMakePayment"); + private readonly ContextField _orderField = ContextField.Create("Order"); + private readonly ContextField _paymentField = ContextField.Create("Payment"); + + private readonly IFlow _flow; + + private Saga(SagaRequest request) + { + var (amount, description, creditLimit, canMakePayment) = request; + + var context = Context.Create() + .Store(_amountField, amount) + .Store(_descriptionField, description) + .Store(_creditLimitField, creditLimit) + .Store(_canMakePayment, canMakePayment); + + _flow = Flow.Create( + FlowRunner.Create(SagaPatternDefinition.Definition, + context)); + } + + public async Task Execute() + { + var flowExit = await _flow.Run(); + + return new SagaResponse( + _flow.Context.Value(_orderField), + _flow.Context.Value(_paymentField)); + } + + public static Saga Create(SagaRequest request) + { + return new Saga(request); + } +} \ No newline at end of file diff --git a/test/Magnett.Automation.Core.IntegrationTest/WorkFlows/SagaPattern/SagaRequest.cs b/test/Magnett.Automation.Core.IntegrationTest/WorkFlows/SagaPattern/SagaRequest.cs new file mode 100644 index 0000000..48e7e1f --- /dev/null +++ b/test/Magnett.Automation.Core.IntegrationTest/WorkFlows/SagaPattern/SagaRequest.cs @@ -0,0 +1,3 @@ +namespace Magnett.Automation.Core.IntegrationTest.WorkFlows.SagaPattern; + +public record SagaRequest(double Amount, string Description, double CreditLimit, bool CanMakePayment); \ No newline at end of file diff --git a/test/Magnett.Automation.Core.IntegrationTest/WorkFlows/SagaPattern/SagaResponse.cs b/test/Magnett.Automation.Core.IntegrationTest/WorkFlows/SagaPattern/SagaResponse.cs new file mode 100644 index 0000000..70d25d3 --- /dev/null +++ b/test/Magnett.Automation.Core.IntegrationTest/WorkFlows/SagaPattern/SagaResponse.cs @@ -0,0 +1,5 @@ +using Magnett.Automation.Core.IntegrationTest.WorkFlows.SagaPattern.Definitions.Entities; + +namespace Magnett.Automation.Core.IntegrationTest.WorkFlows.SagaPattern; + +public record SagaResponse(Order Order, Payment Payment); \ No newline at end of file diff --git a/test/Magnett.Automation.Core.IntegrationTest/WorkFlows/SagaPatternTest.cs b/test/Magnett.Automation.Core.IntegrationTest/WorkFlows/SagaPatternTest.cs new file mode 100644 index 0000000..266636f --- /dev/null +++ b/test/Magnett.Automation.Core.IntegrationTest/WorkFlows/SagaPatternTest.cs @@ -0,0 +1,44 @@ +using System.Threading.Tasks; +using Xunit; + +using Magnett.Automation.Core.IntegrationTest.WorkFlows.SagaPattern; +using Magnett.Automation.Core.IntegrationTest.WorkFlows.SagaPattern.Definitions.States; + +namespace Magnett.Automation.Core.IntegrationTest.WorkFlows; + +public class SagaPatternTest +{ + [Fact] + public async Task SagaPattern_WhenInvokeWithCredit_ShouldReturnOrderConfirmedPaymentConfirmed() + { + var saga = Saga.Create( + new SagaRequest( + 10.0, + "Operation with credit", + 20.0, + true)); + + var result = await saga.Execute(); + + Assert.NotNull(result); + Assert.True(result.Order.State.Equals(OrderStateDefinition.State.Confirmed)); + Assert.True(result.Payment.State.Equals(PaymentStateDefinition.State.Confirmed)); + } + + [Fact] + public async Task SagaPattern_WhenInvokeWithOutCredit_ShouldReturnOrderCancelledPaymentCancelled() + { + var saga = Saga.Create( + new SagaRequest( + 50.0, + "Operation without credit", + 20.0, + true)); + + var result = await saga.Execute(); + + Assert.NotNull(result); + Assert.True(result.Order.State.Equals(OrderStateDefinition.State.Cancelled)); + Assert.True(result.Payment.State.Equals(PaymentStateDefinition.State.Cancelled)); + } +} \ No newline at end of file diff --git a/test/Magnett.Automation.Core.IntegrationTest/WorkFlows/SimpleFlow/Definitions/Node.cs b/test/Magnett.Automation.Core.IntegrationTest/WorkFlows/SimpleFlow/Definitions/Node.cs deleted file mode 100644 index 8508869..0000000 --- a/test/Magnett.Automation.Core.IntegrationTest/WorkFlows/SimpleFlow/Definitions/Node.cs +++ /dev/null @@ -1,16 +0,0 @@ -using Magnett.Automation.Core.Commons; - -namespace Magnett.Automation.Core.IntegrationTest.WorkFlows.SimpleFlow.Definitions -{ - internal class Node : CommonNamedKey - { - public static readonly Node Reset = new Node("Reset"); - public static readonly Node SetValue = new Node("SetValue"); - public static readonly Node SumValue = new Node("SumValue"); - - private Node(string name) : base(name) - { - - } - } -} \ No newline at end of file diff --git a/test/Magnett.Automation.Core.IntegrationTest/WorkFlows/SimpleFlow/Definitions/NodeName.cs b/test/Magnett.Automation.Core.IntegrationTest/WorkFlows/SimpleFlow/Definitions/NodeName.cs new file mode 100644 index 0000000..6535c0e --- /dev/null +++ b/test/Magnett.Automation.Core.IntegrationTest/WorkFlows/SimpleFlow/Definitions/NodeName.cs @@ -0,0 +1,16 @@ +using Magnett.Automation.Core.Commons; + +namespace Magnett.Automation.Core.IntegrationTest.WorkFlows.SimpleFlow.Definitions +{ + internal class NodeName : CommonNamedKey + { + public static readonly NodeName Reset = new NodeName("Reset"); + public static readonly NodeName SetValue = new NodeName("SetValue"); + public static readonly NodeName SumValue = new NodeName("SumValue"); + + private NodeName(string name) : base(name) + { + + } + } +} \ No newline at end of file diff --git a/test/Magnett.Automation.Core.IntegrationTest/WorkFlows/SimpleFlow/Definitions/Nodes/ResetValue.cs b/test/Magnett.Automation.Core.IntegrationTest/WorkFlows/SimpleFlow/Definitions/Nodes/ResetValue.cs index e6187fd..059656c 100644 --- a/test/Magnett.Automation.Core.IntegrationTest/WorkFlows/SimpleFlow/Definitions/Nodes/ResetValue.cs +++ b/test/Magnett.Automation.Core.IntegrationTest/WorkFlows/SimpleFlow/Definitions/Nodes/ResetValue.cs @@ -1,4 +1,5 @@ using Magnett.Automation.Core.Commons; +using Magnett.Automation.Core.Contexts; using Magnett.Automation.Core.WorkFlows; namespace Magnett.Automation.Core.IntegrationTest.WorkFlows.SimpleFlow.Definitions.Nodes @@ -24,11 +25,11 @@ private ResetValue(CommonNamedKey key, ContextDefinition contextDefinition) : } - public override NodeExit Execute() + public override NodeExit Execute(Context context) { - GlobalContext.Store(ContextDefinition.FirstDigit, 0); - GlobalContext.Store(ContextDefinition.SecondDigit, 0); - GlobalContext.Store(ContextDefinition.Result, 0); + context.Store(ContextDefinition.FirstDigit, 0); + context.Store(ContextDefinition.SecondDigit, 0); + context.Store(ContextDefinition.Result, 0); return NodeExit.Create(ExitCode.Ok.Name); } diff --git a/test/Magnett.Automation.Core.IntegrationTest/WorkFlows/SimpleFlow/Definitions/Nodes/SetValue.cs b/test/Magnett.Automation.Core.IntegrationTest/WorkFlows/SimpleFlow/Definitions/Nodes/SetValue.cs index 65a8341..359e98e 100644 --- a/test/Magnett.Automation.Core.IntegrationTest/WorkFlows/SimpleFlow/Definitions/Nodes/SetValue.cs +++ b/test/Magnett.Automation.Core.IntegrationTest/WorkFlows/SimpleFlow/Definitions/Nodes/SetValue.cs @@ -1,5 +1,6 @@ using System; using Magnett.Automation.Core.Commons; +using Magnett.Automation.Core.Contexts; using Magnett.Automation.Core.WorkFlows; namespace Magnett.Automation.Core.IntegrationTest.WorkFlows.SimpleFlow.Definitions.Nodes @@ -25,12 +26,12 @@ private SetValue(CommonNamedKey key, ContextDefinition contextDefinition) : } - public override NodeExit Execute() + public override NodeExit Execute(Context context) { var random = new Random(); - GlobalContext.Store(ContextDefinition.FirstDigit, random.Next(1000)); - GlobalContext.Store(ContextDefinition.SecondDigit, random.Next(1000)); + context.Store(ContextDefinition.FirstDigit, random.Next(1000)); + context.Store(ContextDefinition.SecondDigit, random.Next(1000)); return NodeExit.Create(ExitCode.Assigned.Name); } diff --git a/test/Magnett.Automation.Core.IntegrationTest/WorkFlows/SimpleFlow/Definitions/Nodes/SumValue.cs b/test/Magnett.Automation.Core.IntegrationTest/WorkFlows/SimpleFlow/Definitions/Nodes/SumValue.cs index af57042..042501d 100644 --- a/test/Magnett.Automation.Core.IntegrationTest/WorkFlows/SimpleFlow/Definitions/Nodes/SumValue.cs +++ b/test/Magnett.Automation.Core.IntegrationTest/WorkFlows/SimpleFlow/Definitions/Nodes/SumValue.cs @@ -1,4 +1,5 @@ using Magnett.Automation.Core.Commons; +using Magnett.Automation.Core.Contexts; using Magnett.Automation.Core.WorkFlows; namespace Magnett.Automation.Core.IntegrationTest.WorkFlows.SimpleFlow.Definitions.Nodes @@ -24,12 +25,12 @@ private SumValue(CommonNamedKey key, ContextDefinition contextDefinition) : } - public override NodeExit Execute() + public override NodeExit Execute(Context context) { - var firstDigit = GlobalContext.Value(ContextDefinition.FirstDigit); - var secondDigit = GlobalContext.Value(ContextDefinition.SecondDigit); + var firstDigit = context.Value(ContextDefinition.FirstDigit); + var secondDigit = context.Value(ContextDefinition.SecondDigit); - GlobalContext.Store(ContextDefinition.Result, firstDigit + secondDigit); + context.Store(ContextDefinition.Result, firstDigit + secondDigit); return NodeExit.Create(ExitCode.Done.Name); } diff --git a/test/Magnett.Automation.Core.IntegrationTest/WorkFlows/SimpleFlow/Definitions/SimpleFlowDefinition.cs b/test/Magnett.Automation.Core.IntegrationTest/WorkFlows/SimpleFlow/Definitions/SimpleFlowDefinition.cs index 0bf02a8..d6d2088 100644 --- a/test/Magnett.Automation.Core.IntegrationTest/WorkFlows/SimpleFlow/Definitions/SimpleFlowDefinition.cs +++ b/test/Magnett.Automation.Core.IntegrationTest/WorkFlows/SimpleFlow/Definitions/SimpleFlowDefinition.cs @@ -1,6 +1,6 @@ -using Magnett.Automation.Core.IntegrationTest.WorkFlows.SimpleFlow.Definitions.Nodes; -using Magnett.Automation.Core.WorkFlows; +using Magnett.Automation.Core.WorkFlows; using Magnett.Automation.Core.WorkFlows.Builders; +using Magnett.Automation.Core.IntegrationTest.WorkFlows.SimpleFlow.Definitions.Nodes; namespace Magnett.Automation.Core.IntegrationTest.WorkFlows.SimpleFlow.Definitions { @@ -19,15 +19,15 @@ private static void CreateDefinition() _contextDefinition = ContextDefinition.Create(); _definition = FlowDefinitionBuilder.Create() - .WithInitialNode(ResetValue.Create(Node.Reset, _contextDefinition)) - .OnExitCode(ResetValue.ExitCode.Ok).GoTo(Node.SetValue) + .WithInitialNode(ResetValue.Create(NodeName.Reset, _contextDefinition)) + .OnExitCode(ResetValue.ExitCode.Ok).GoTo(NodeName.SetValue) .Build() - .WithNode(SetValue.Create(Node.SetValue, _contextDefinition)) - .OnExitCode(SetValue.ExitCode.Assigned).GoTo(Node.SumValue) + .WithNode(SetValue.Create(NodeName.SetValue, _contextDefinition)) + .OnExitCode(SetValue.ExitCode.Assigned).GoTo(NodeName.SumValue) .Build() - .WithNode(SumValue.Create(Node.SumValue, _contextDefinition)).Build() + .WithNode(SumValue.Create(NodeName.SumValue, _contextDefinition)).Build() .BuildDefinition(); } diff --git a/test/Magnett.Automation.Core.UnitTest/WorkFlows/NodeExitTest.cs b/test/Magnett.Automation.Core.UnitTest/WorkFlows/NodeExitTest.cs index 1092bfa..6154080 100644 --- a/test/Magnett.Automation.Core.UnitTest/WorkFlows/NodeExitTest.cs +++ b/test/Magnett.Automation.Core.UnitTest/WorkFlows/NodeExitTest.cs @@ -1,4 +1,5 @@ using System; +using Magnett.Automation.Core.Commons; using Magnett.Automation.Core.WorkFlows; using Xunit; @@ -10,12 +11,23 @@ public class NodeExitTest private const string Data = "Data Info"; [Fact] - public void Create_WhenCodeIsNull_ThrowException() + public void Create_WhenEnumerationCodeIsNull_ThrowException() { + Enumeration parameter = null; + Assert.Throws(() => - _ = NodeExit.Create(null)); + _ = NodeExit.Create(parameter)); } + [Fact] + public void Create_WhenStringCodeIsNull_ThrowException() + { + string parameter = null; + + Assert.Throws(() => + _ = NodeExit.Create(parameter)); + } + [Fact] public void Create_WhenCodeNotIsNull_ReturnValidInstance() {