-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
When triggering a flow, the function returns and FlowResult instead of just a Task. It is now supposed to be easier to write general action executors and asserting a flow inside the unit test, instead of in the action executors. #43
- Loading branch information
1 parent
736a160
commit ed74956
Showing
4 changed files
with
217 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#nullable enable | ||
using System.Collections.Generic; | ||
using Parser.ExpressionParser; | ||
using Parser.FlowParser.ActionExecutors; | ||
|
||
namespace Parser.FlowParser | ||
{ | ||
public class FlowResult | ||
{ | ||
public FlowResult() | ||
{ | ||
ActionStates = new Dictionary<string, ActionState>(); | ||
} | ||
|
||
public Dictionary<string, ActionState> ActionStates { get; set; } | ||
public int NumberOfExecutedActions { get; set; } | ||
} | ||
|
||
public class ActionState | ||
{ | ||
private ValueContainer? _actionInput; | ||
#nullable enable | ||
public ValueContainer? ActionInput | ||
{ | ||
get => _actionInput; | ||
set | ||
{ | ||
_actionInput = value; | ||
if (_actionInput == null || _actionInput.Type() != ValueContainer.ValueType.Object) return; | ||
if (_actionInput.AsDict().ContainsKey("parameters")) | ||
{ | ||
ActionParameters = _actionInput.AsDict()["parameters"]; | ||
} | ||
} | ||
} | ||
|
||
public ValueContainer? ActionParameters { set; get; } | ||
|
||
public ActionResult? ActionOutput { get; set; } | ||
#nullable disable | ||
public string ActionName { get; set; } | ||
public string ActionType { get; set; } | ||
public int ActionOrder { get; set; } | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using NUnit.Framework; | ||
using Parser; | ||
using Parser.ExpressionParser; | ||
using Parser.FlowParser; | ||
using Parser.FlowParser.ActionExecutors; | ||
|
||
namespace Test | ||
{ | ||
[TestFixture] | ||
public class FullFlowTestV2 | ||
{ | ||
private static readonly string TestFlowPath = System.IO.Path.GetFullPath(@"FlowSamples"); | ||
|
||
[Test] | ||
public async Task TestFlowFalse() | ||
{ | ||
var path = @$"{TestFlowPath}\PowerAutomateMockUpSampleFlow.json"; | ||
|
||
var services = new ServiceCollection(); | ||
services.AddFlowRunner(); | ||
|
||
services.AddFlowActionByName<UpdateAccountInvalidId>(UpdateAccountInvalidId.FlowActionName); | ||
services.AddFlowActionByApiIdAndOperationsName<SendEmailNotification>(SendEmailNotification.ApiId, | ||
SendEmailNotification.SupportedOperations); | ||
services.AddFlowActionByName<GetRecordValidId>(GetRecordValidId.FlowActionName); | ||
services.AddFlowActionByName<UpdateAccountValidId>(UpdateAccountValidId.FlowActionName); | ||
services.AddFlowActionByName<SendOutWarning>(SendOutWarning.FlowActionName); | ||
|
||
|
||
var sp = services.BuildServiceProvider(); | ||
var flowRunner = sp.GetRequiredService<IFlowRunner>(); | ||
|
||
flowRunner.InitializeFlowRunner(path); | ||
|
||
var flowResult = await flowRunner.Trigger(new ValueContainer( | ||
new Dictionary<string, ValueContainer> | ||
{ | ||
{"body/name", new ValueContainer("Alice Bob")}, | ||
{"body/accountid", new ValueContainer(Guid.NewGuid().ToString())} | ||
})); | ||
|
||
Assert.AreEqual(7, flowResult.NumberOfExecutedActions); | ||
|
||
const string actionName = "Send_me_an_email_notification"; | ||
Assert.IsTrue(flowResult.ActionStates.ContainsKey(actionName), "Action is expected to be triggered."); | ||
Assert.NotNull(flowResult.ActionStates[actionName].ActionParameters, "Action input is expected."); | ||
var actionInput = flowResult.ActionStates[actionName].ActionParameters.AsDict(); | ||
Assert.IsTrue(actionInput.ContainsKey("NotificationEmailDefinition"), "Action input should contain this object."); | ||
var notification = actionInput["NotificationEmailDefinition"].AsDict(); | ||
Assert.AreEqual("A new Account have been added", notification["notificationSubject"].GetValue<string>(), "Asserting the input"); | ||
|
||
Assert.IsFalse(flowResult.ActionStates.ContainsKey(SendOutWarning.FlowActionName), "Action is not expected to be triggered."); | ||
} | ||
|
||
private class UpdateAccountInvalidId : OpenApiConnectionActionExecutorBase | ||
{ | ||
public const string FlowActionName = "Update_Account_-_Invalid_Id"; | ||
|
||
public UpdateAccountInvalidId(IExpressionEngine expressionEngine) : base(expressionEngine) | ||
{ | ||
} | ||
|
||
public override Task<ActionResult> Execute() | ||
{ | ||
return Task.FromResult(new ActionResult | ||
{ | ||
ActionStatus = ActionStatus.Failed, | ||
ActionOutput = new ValueContainer(true) | ||
}); | ||
} | ||
} | ||
|
||
private class SendEmailNotification : OpenApiConnectionActionExecutorBase | ||
{ | ||
public const string ApiId = "/providers/Microsoft.PowerApps/apis/shared_flowpush"; | ||
public static readonly string[] SupportedOperations = {"SendEmailNotification"}; | ||
|
||
public SendEmailNotification(IExpressionEngine expressionEngine) : base(expressionEngine) | ||
{ | ||
} | ||
|
||
public override Task<ActionResult> Execute() | ||
{ | ||
Console.WriteLine($"Email Title: {Parameters["NotificationEmailDefinition/notificationSubject"]}"); | ||
Console.WriteLine($"Email Content: {Parameters["NotificationEmailDefinition/notificationBody"]}"); | ||
|
||
return Task.FromResult(new ActionResult {ActionOutput = new ValueContainer(true)}); | ||
} | ||
} | ||
|
||
private class GetRecordValidId : OpenApiConnectionActionExecutorBase | ||
{ | ||
public const string FlowActionName = "Get_a_record_-_Valid_Id"; | ||
|
||
public GetRecordValidId(IExpressionEngine expressionEngine) : base(expressionEngine) | ||
{ | ||
} | ||
|
||
public override Task<ActionResult> Execute() | ||
{ | ||
return Task.FromResult(new ActionResult {ActionOutput = new ValueContainer(true)}); | ||
} | ||
} | ||
|
||
private class UpdateAccountValidId : OpenApiConnectionActionExecutorBase | ||
{ | ||
public const string FlowActionName = "Update_Account_-_Valid_Id"; | ||
|
||
public override Task<ActionResult> Execute() | ||
{ | ||
return Task.FromResult(new ActionResult()); | ||
} | ||
|
||
public UpdateAccountValidId(IExpressionEngine expressionEngine) : base(expressionEngine) | ||
{ | ||
} | ||
} | ||
|
||
private class SendOutWarning : OpenApiConnectionActionExecutorBase | ||
{ | ||
public const string FlowActionName = "Send_an_error_message_to_owner"; | ||
|
||
public SendOutWarning(IExpressionEngine expressionEngine) : base(expressionEngine) | ||
{ | ||
} | ||
|
||
public override Task<ActionResult> Execute() | ||
{ | ||
return Task.FromResult(new ActionResult()); | ||
} | ||
} | ||
} | ||
} |