Skip to content

Commit

Permalink
refactor: Refined action state
Browse files Browse the repository at this point in the history
  • Loading branch information
thygesteffensen committed Mar 3, 2021
1 parent c14f72c commit 9a73007
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 26 deletions.
17 changes: 1 addition & 16 deletions PowerAutomateMockUp/FlowParser/ActionState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,8 @@ public FlowResult()

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 ValueContainer? ActionInput { get; set; }

public ActionResult? ActionOutput { get; set; }
#nullable disable
Expand Down
7 changes: 5 additions & 2 deletions PowerAutomateMockUp/FlowParser/FlowRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,12 @@ private async Task RunFlow()

if (_flowRunnerSettings.LogActionsStates)
{
var jsonInputs = currentAd.First?.SelectToken("$.inputs");

_actionSates[currentAd.Name] = new ActionState
{
ActionInput = actionExecutor?.Inputs,
ActionInput = actionExecutor?.Inputs ??
(jsonInputs == null ? null : new ValueContainer(jsonInputs)),
ActionOutput = actionResult,
ActionOrder = _actionsExecuted++,
ActionName = actionExecutor?.ActionName
Expand Down Expand Up @@ -151,7 +154,7 @@ private async Task RunFlow()
if (currentAd == null && actionResultStatus == ActionStatus.Failed)
{
_logger.LogError(
"No succeeding action found after last action had status: Failed. Throwing error.");
"No succeeding action found after last action had status: Failed. Throwing error");
throw actionResult?.ActionExecutorException ??
new PowerAutomateMockUpException(
$"No exception recorded - {actionExecutor.ActionName} ended with status: Failed.");
Expand Down
10 changes: 9 additions & 1 deletion PowerAutomateMockUp/FlowRunnerDependencyExtension.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using Microsoft.Extensions.DependencyInjection;
using System.Diagnostics;
using Microsoft.Extensions.DependencyInjection;
using Parser.ExpressionParser;
using Parser.ExpressionParser.Functions.Base;
using Parser.ExpressionParser.Functions.Implementations.CollectionFunctions;
using Parser.ExpressionParser.Functions.Implementations.ConversionFunctions;
using Parser.ExpressionParser.Functions.Implementations.LogicalComparisonFunctions;
using Parser.ExpressionParser.Functions.Implementations.Miscellaneous;
using Parser.ExpressionParser.Functions.Implementations.StringFunctions;
using Parser.ExpressionParser.Functions.Storage;
using Parser.FlowParser;
Expand Down Expand Up @@ -33,6 +35,7 @@ public static void AddFlowRunner(this IServiceCollection services)
AddStringFunctions(services);
AddCollectionFunction(services);
AddConversionFunction(services);
AddMiscellaneousFunctions(services);

services.AddTransient<IFunction, VariablesFunction>();
services.AddTransient<IFunction, OutputsFunction>();
Expand All @@ -52,6 +55,11 @@ public static void AddFlowRunner(this IServiceCollection services)
services.AddLogging();
}

private static void AddMiscellaneousFunctions(IServiceCollection services)
{
services.AddTransient<IFunction, ParametersFunction>();
}

private static void AddStringFunctions(IServiceCollection services)
{
services.AddTransient<IFunction, ConcatFunction>();
Expand Down
29 changes: 22 additions & 7 deletions Test/FullFlowTestV2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ public async Task TestFlowFalse()
var services = new ServiceCollection();
services.AddFlowRunner();

const string authentication = "Custom value";
services.Configure<WorkflowParameters>(x => x.Parameters = new Dictionary<string, ValueContainer>
{
{"$authentication", new ValueContainer(authentication)}
});

services.AddFlowActionByName<UpdateAccountInvalidId>(UpdateAccountInvalidId.FlowActionName);
services.AddFlowActionByApiIdAndOperationsName<SendEmailNotification>(SendEmailNotification.ApiId,
SendEmailNotification.SupportedOperations);
Expand All @@ -47,16 +53,25 @@ public async Task TestFlowFalse()

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.");
Assert.NotNull(flowResult.ActionStates[actionName].ActionInput?["parameters"], "Action input is expected.");
var actionInput = flowResult.ActionStates[actionName].ActionInput?["parameters"].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.");
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.");

var getRecordValidIdActionAuth =
flowResult.ActionStates["Get_a_record_-_Valid_Id"].ActionInput?["authentication"];
Assert.IsNotNull(getRecordValidIdActionAuth);
Assert.AreEqual(ValueContainer.ValueType.String, getRecordValidIdActionAuth.Type());
Assert.AreEqual(authentication, getRecordValidIdActionAuth.GetValue<string>());
}

private class UpdateAccountInvalidId : OpenApiConnectionActionExecutorBase
private class UpdateAccountInvalidId : OpenApiConnectionActionExecutorBase
{
public const string FlowActionName = "Update_Account_-_Invalid_Id";

Expand Down

0 comments on commit 9a73007

Please sign in to comment.