Skip to content
This repository was archived by the owner on Nov 14, 2024. It is now read-only.

Commit 96b506b

Browse files
authored
Feature - revisit custom Logic App models (#37)
* Feature - revisit custom Logic App models * pr-sug: rename 'Value' to 'Url' * pr-sug: rename 'Value' to 'Url' * pr-fix: remove 'null' generation from required properties
1 parent f4f624b commit 96b506b

File tree

13 files changed

+498
-127
lines changed

13 files changed

+498
-127
lines changed

src/Invictus.Testing.Tests.Integration/LogicAppClientTests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public async Task GetLogicAppTriggerUrl_NoTriggerNameSpecified_Success()
2929
LogicAppTriggerUrl logicAppTriggerUrl = await logicApp.GetTriggerUrlAsync();
3030

3131
// Assert
32-
Assert.NotNull(logicAppTriggerUrl.Value);
32+
Assert.NotNull(logicAppTriggerUrl.Url);
3333
Assert.Equal("POST", logicAppTriggerUrl.Method);
3434
}
3535
}
@@ -43,7 +43,7 @@ public async Task GetLogicAppTriggerUrl_ByName_Success()
4343
LogicAppTriggerUrl logicAppTriggerUrl = await logicApp.GetTriggerUrlByNameAsync(triggerName: "manual");
4444

4545
// Assert
46-
Assert.NotNull(logicAppTriggerUrl.Value);
46+
Assert.NotNull(logicAppTriggerUrl.Url);
4747
Assert.Equal("POST", logicAppTriggerUrl.Method);
4848
}
4949
}
@@ -72,7 +72,7 @@ public async Task TemporaryEnableSuccessStaticResultForAction_WithoutConsumerSta
7272

7373
Assert.Equal(actionName, enabledAction.Name);
7474
Assert.Equal("200", enabledAction.Outputs.statusCode.ToString());
75-
Assert.Equal("Succeeded", enabledAction.Status);
75+
Assert.Equal(LogicAppActionStatus.Succeeded, enabledAction.Status);
7676
}
7777

7878
await logicApp.TriggerAsync(headers);
@@ -186,11 +186,11 @@ public async Task TemporaryEnableLogicApp_Success()
186186
{
187187
// Assert
188188
LogicApp metadata = await logicApp.GetMetadataAsync();
189-
Assert.Equal("Enabled", metadata.State);
189+
Assert.Equal(LogicAppState.Enabled, metadata.State);
190190
}
191191
{
192192
LogicApp metadata = await logicApp.GetMetadataAsync();
193-
Assert.Equal("Disabled", metadata.State);
193+
Assert.Equal(LogicAppState.Disabled, metadata.State);
194194
}
195195
}
196196
}

src/Invictus.Testing.Tests.Integration/LogicAppConverterTests.cs

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ public class LogicAppConverterTests
1818
public void ToLogicApp_WithWorkflow_CreatesAlternative()
1919
{
2020
// Arrange
21-
string state = BogusGenerator.PickRandom("NotSpecified", "Completed", "Enabled", "Disabled", "Deleted", "Suspended");
21+
var state = BogusGenerator.PickRandom<LogicAppState>();
2222
var workflow = new Workflow(
23-
name: BogusGenerator.Internet.DomainName().OrNull(BogusGenerator),
23+
name: BogusGenerator.Internet.DomainName(),
2424
createdTime: BogusGenerator.Date.Recent(),
2525
changedTime: BogusGenerator.Date.Recent(),
26-
state: state.OrNull(BogusGenerator),
26+
state: state.ToString(),
2727
version: BogusGenerator.System.Version().ToString().OrNull(BogusGenerator),
2828
accessEndpoint: BogusGenerator.Internet.IpAddress().ToString().OrNull(BogusGenerator),
2929
definition: BogusGenerator.Random.String().OrNull(BogusGenerator));
@@ -36,7 +36,7 @@ public void ToLogicApp_WithWorkflow_CreatesAlternative()
3636
Assert.Equal(workflow.Name, actual.Name);
3737
Assert.Equal(workflow.CreatedTime, actual.CreatedTime);
3838
Assert.Equal(workflow.ChangedTime, actual.ChangedTime);
39-
Assert.Equal(workflow.State, actual.State);
39+
Assert.Equal(workflow.State, actual.State.ToString());
4040
Assert.Equal(workflow.Version, actual.Version);
4141
Assert.Equal(workflow.AccessEndpoint, actual.AccessEndpoint);
4242
Assert.Equal(workflow.Definition, actual.Definition);
@@ -54,7 +54,7 @@ public void ToLogicAppAction_WithInputOutput_CreatesAlternative()
5454
string trackedPropertiesJson = JsonConvert.SerializeObject(trackedProperties).OrNull(BogusGenerator);
5555

5656
var workflowAction = new WorkflowRunAction(
57-
name: BogusGenerator.Internet.DomainName().OrNull(BogusGenerator),
57+
name: BogusGenerator.Internet.DomainName(),
5858
startTime: BogusGenerator.Date.Past(),
5959
endTime: BogusGenerator.Date.Past(),
6060
status: GenerateStatus(),
@@ -71,7 +71,7 @@ public void ToLogicAppAction_WithInputOutput_CreatesAlternative()
7171
Assert.Equal(workflowAction.Name, actual.Name);
7272
Assert.Equal(workflowAction.StartTime, actual.StartTime);
7373
Assert.Equal(workflowAction.EndTime, actual.EndTime);
74-
Assert.Equal(workflowAction.Status, actual.Status);
74+
Assert.Equal(workflowAction.Status, actual.Status.ToString());
7575
Assert.Equal(workflowAction.Error, actual.Error);
7676
Assert.Equal(inputs, actual.Inputs);
7777
Assert.Equal(outputs, actual.Outputs);
@@ -92,7 +92,7 @@ public void ToLogicAppRun_WithWorkflowRunAndActions_CreatesCombinedModel()
9292
// Assert
9393
Assert.NotNull(actual);
9494
Assert.Equal(workflowRun.Name, actual.Id);
95-
Assert.Equal(workflowRun.Status, actual.Status);
95+
Assert.Equal(workflowRun.Status, actual.Status.ToString());
9696
Assert.Equal(workflowRun.StartTime, actual.StartTime);
9797
Assert.Equal(workflowRun.EndTime, actual.EndTime);
9898
Assert.Equal(workflowRun.Error, actual.Error);
@@ -104,7 +104,7 @@ public void ToLogicAppRun_WithWorkflowRunAndActions_CreatesCombinedModel()
104104
Assert.Equal(trigger.Outputs, actual.Trigger.Outputs);
105105
Assert.Equal(trigger.StartTime, actual.Trigger.StartTime);
106106
Assert.Equal(trigger.EndTime, actual.Trigger.EndTime);
107-
Assert.Equal(trigger.Status, actual.Trigger.Status);
107+
Assert.Equal(trigger.Status, actual.Trigger.Status.ToString());
108108
Assert.Equal(trigger.Error, actual.Trigger.Error);
109109

110110
Assert.All(actions.Where(action => action.TrackedProperties != null), action =>
@@ -119,7 +119,7 @@ public void ToLogicAppRun_WithWorkflowRunAndActions_CreatesCombinedModel()
119119
private static WorkflowRunTrigger CreateWorkflowRunTrigger()
120120
{
121121
var trigger = new WorkflowRunTrigger(
122-
name: BogusGenerator.Internet.DomainName().OrNull(BogusGenerator),
122+
name: BogusGenerator.Internet.DomainName(),
123123
inputs: BogusGenerator.Random.Word().OrNull(BogusGenerator),
124124
outputs: BogusGenerator.Random.Word().OrNull(BogusGenerator),
125125
startTime: BogusGenerator.Date.Past(),
@@ -133,7 +133,7 @@ private static WorkflowRun CreateWorkflowRun(WorkflowRunTrigger trigger)
133133
{
134134
var correlation = new Correlation(BogusGenerator.Random.String().OrNull(BogusGenerator)).OrNull(BogusGenerator);
135135
var workflowRun = new WorkflowRun(
136-
name: BogusGenerator.Internet.DomainWord().OrNull(BogusGenerator),
136+
name: BogusGenerator.Internet.DomainWord(),
137137
startTime: BogusGenerator.Date.Recent(),
138138
status: GenerateStatus(),
139139
error: BogusGenerator.Random.Bytes(10).OrNull(BogusGenerator),
@@ -153,27 +153,23 @@ private static IEnumerable<LogicAppAction> CreateLogicAppActions()
153153

154154
IList<LogicAppAction> actions = BogusGenerator.Make(actionCount, () =>
155155
{
156-
return new LogicAppAction
157-
{
158-
Name = BogusGenerator.Internet.DomainWord().OrNull(BogusGenerator),
159-
Inputs = BogusGenerator.Random.Words().OrNull(BogusGenerator),
160-
Outputs = BogusGenerator.Random.Words().OrNull(BogusGenerator),
161-
Status = GenerateStatus(),
162-
StartTime = BogusGenerator.Date.Past(),
163-
EndTime = BogusGenerator.Date.Recent(),
164-
Error = BogusGenerator.Random.Byte(10).OrNull(BogusGenerator),
165-
TrackedProperties = trackedProperties
166-
};
156+
return new LogicAppAction(
157+
BogusGenerator.Internet.DomainWord(),
158+
BogusGenerator.PickRandom<LogicAppActionStatus>(),
159+
BogusGenerator.Random.Words().OrNull(BogusGenerator),
160+
BogusGenerator.Random.Words().OrNull(BogusGenerator),
161+
BogusGenerator.Random.Byte(10).OrNull(BogusGenerator),
162+
trackedProperties,
163+
BogusGenerator.Date.Past(),
164+
BogusGenerator.Date.Recent());
167165
});
168166

169167
return actions;
170168
}
171169

172170
private static string GenerateStatus()
173171
{
174-
return BogusGenerator.PickRandom("NotSpecified", "Paused", "Running",
175-
"Waiting", "Succeeded", "Skipped", "Suspended", "Cancelled", "Failed", "Faulted",
176-
"TimedOut", "Aborted", "Ignored");
172+
return BogusGenerator.PickRandom<LogicAppActionStatus>().ToString();
177173
}
178174
}
179175
}

src/Invictus.Testing/Invictus.Testing.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<PackageReference Include="Guard.NET" Version="1.2.0" />
2222
<PackageReference Include="Microsoft.Azure.Management.Logic" Version="4.1.0" />
2323
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="1.1.1" />
24+
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
2425
<PackageReference Include="Microsoft.Rest.ClientRuntime.Azure.Authentication" Version="2.4.0" />
2526
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
2627
<PackageReference Include="Polly-Signed" Version="5.9.0" />

src/Invictus.Testing/LogicAppClient.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ public async Task TriggerAsync(IDictionary<string, string> headers)
215215
LogicAppTriggerUrl triggerUrl = await GetTriggerUrlAsync();
216216

217217
_logger.LogTrace("Trigger the workflow of logic app '{LogicAppName}' in resource group '{ResourceGroup}'", _logicAppName, _resourceGroup);
218-
using (var request = new HttpRequestMessage(HttpMethod.Post, triggerUrl.Value))
218+
using (var request = new HttpRequestMessage(HttpMethod.Post, triggerUrl.Url))
219219
{
220220
foreach (KeyValuePair<string, string> header in headers)
221221
{
@@ -250,11 +250,7 @@ public async Task<LogicAppTriggerUrl> GetTriggerUrlByNameAsync(string triggerNam
250250
WorkflowTriggerCallbackUrl callbackUrl =
251251
await _logicManagementClient.WorkflowTriggers.ListCallbackUrlAsync(_resourceGroup, _logicAppName, triggerName);
252252

253-
return new LogicAppTriggerUrl
254-
{
255-
Value = callbackUrl.Value,
256-
Method = callbackUrl.Method
257-
};
253+
return new LogicAppTriggerUrl(callbackUrl.Value, callbackUrl.Method);
258254
}
259255

260256
private async Task<string> GetTriggerNameAsync()
Lines changed: 62 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using System.Collections.ObjectModel;
34
using System.Linq;
45
using GuardNet;
@@ -20,59 +21,72 @@ public static LogicAppRun ToLogicAppRun(WorkflowRun workFlowRun, IEnumerable<Log
2021
{
2122
Guard.NotNull(workFlowRun, nameof(workFlowRun));
2223
Guard.NotNull(actions, nameof(actions));
24+
Enum.TryParse(workFlowRun.Status, out LogicAppActionStatus status);
2325

24-
return new LogicAppRun
25-
{
26-
Id = workFlowRun.Name,
27-
StartTime = workFlowRun.StartTime,
28-
EndTime = workFlowRun.EndTime,
29-
Status = workFlowRun.Status,
30-
Error = workFlowRun.Error,
31-
CorrelationId = workFlowRun.Correlation?.ClientTrackingId,
32-
Trigger = CreateLogicAppTriggerFrom(workFlowRun.Trigger),
33-
Actions = actions,
34-
TrackedProperties = new ReadOnlyDictionary<string, string>(GetAllTrackedProperties(actions))
35-
};
26+
LogicAppTrigger trigger = CreateLogicAppTriggerFrom(workFlowRun.Trigger);
27+
IDictionary<string, string> trackedProperties = GetAllTrackedProperties(actions);
28+
29+
return new LogicAppRun(
30+
workFlowRun.Name,
31+
status,
32+
workFlowRun.Error,
33+
workFlowRun.Correlation?.ClientTrackingId,
34+
trigger,
35+
actions,
36+
trackedProperties,
37+
workFlowRun.StartTime,
38+
workFlowRun.EndTime);
3639
}
3740

3841
private static LogicAppTrigger CreateLogicAppTriggerFrom(WorkflowRunTrigger workflowRunTrigger)
3942
{
40-
return new LogicAppTrigger
41-
{
42-
Name = workflowRunTrigger.Name,
43-
Inputs = workflowRunTrigger.Inputs,
44-
Outputs = workflowRunTrigger.Outputs,
45-
StartTime = workflowRunTrigger.StartTime,
46-
EndTime = workflowRunTrigger.EndTime,
47-
Status = workflowRunTrigger.Status,
48-
Error = workflowRunTrigger.Error
49-
};
43+
Enum.TryParse(workflowRunTrigger.Status, out LogicAppActionStatus status);
44+
45+
return new LogicAppTrigger(
46+
workflowRunTrigger.Name,
47+
status,
48+
workflowRunTrigger.Inputs,
49+
workflowRunTrigger.Outputs,
50+
workflowRunTrigger.Error,
51+
workflowRunTrigger.StartTime,
52+
workflowRunTrigger.EndTime);
5053
}
5154

5255
/// <summary>
5356
/// Convert to <see cref="LogicAppAction"/>.
5457
/// </summary>
5558
public static LogicAppAction ToLogicAppAction(WorkflowRunAction workflowRunAction, dynamic input, dynamic output)
5659
{
57-
var logicAppAction = new LogicAppAction
58-
{
59-
Name = workflowRunAction.Name,
60-
StartTime = workflowRunAction.StartTime,
61-
EndTime = workflowRunAction.EndTime,
62-
Status = workflowRunAction.Status,
63-
Error = workflowRunAction.Error,
64-
Inputs = input,
65-
Outputs = output
66-
};
67-
68-
if (workflowRunAction.TrackedProperties != null)
60+
Guard.NotNull(param: workflowRunAction, paramName: nameof(workflowRunAction));
61+
Enum.TryParse(value: workflowRunAction.Status, result: out LogicAppActionStatus status);
62+
63+
IDictionary<string, string> trackedProperties = DeserializeTrackedProperties(workflowRunAction: workflowRunAction);
64+
65+
var logicAppAction = new LogicAppAction(
66+
name: workflowRunAction.Name,
67+
status: status,
68+
error: workflowRunAction.Error,
69+
inputs: input,
70+
outputs: output,
71+
trackedProperties: trackedProperties,
72+
startTime: workflowRunAction.StartTime,
73+
endTime: workflowRunAction.EndTime);
74+
75+
return logicAppAction;
76+
}
77+
78+
private static IDictionary<string, string> DeserializeTrackedProperties(WorkflowRunAction workflowRunAction)
79+
{
80+
if (workflowRunAction.TrackedProperties is null)
6981
{
70-
logicAppAction.TrackedProperties =
71-
JsonConvert.DeserializeObject<Dictionary<string, string>>(
72-
workflowRunAction.TrackedProperties.ToString());
82+
return new Dictionary<string, string>();
7383
}
7484

75-
return logicAppAction;
85+
var trackedPropertiesJson = workflowRunAction.TrackedProperties.ToString();
86+
var trackedProperties = JsonConvert.DeserializeObject<Dictionary<string, string>>(trackedPropertiesJson);
87+
88+
return trackedProperties;
89+
7690
}
7791

7892
private static IDictionary<string, string> GetAllTrackedProperties(IEnumerable<LogicAppAction> actions)
@@ -92,17 +106,16 @@ private static IDictionary<string, string> GetAllTrackedProperties(IEnumerable<L
92106
public static LogicApp ToLogicApp(Workflow workflow)
93107
{
94108
Guard.NotNull(workflow, nameof(workflow));
109+
Enum.TryParse(workflow.State, out LogicAppState state);
95110

96-
return new LogicApp
97-
{
98-
Name = workflow.Name,
99-
CreatedTime = workflow.CreatedTime,
100-
ChangedTime = workflow.ChangedTime,
101-
State = workflow.State,
102-
Version = workflow.Version,
103-
AccessEndpoint = workflow.AccessEndpoint,
104-
Definition = workflow.Definition
105-
};
111+
return new LogicApp(
112+
workflow.Name,
113+
state,
114+
workflow.Version,
115+
workflow.AccessEndpoint,
116+
workflow.Definition,
117+
workflow.CreatedTime,
118+
workflow.ChangedTime);
106119
}
107120
}
108121
}

src/Invictus.Testing/LogicAppsProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ private static async Task<JToken> GetHttpJsonStringAsync(string uri)
319319
return null;
320320
}
321321

322-
private bool HasTrackedProperty(IDictionary<string, string> properties)
322+
private bool HasTrackedProperty(IReadOnlyDictionary<string, string> properties)
323323
{
324324
if (properties is null || properties.Count <= 0)
325325
{

0 commit comments

Comments
 (0)