|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Threading.Tasks; |
| 5 | +using Invictus.Testing.Model; |
| 6 | +using Invictus.Testing.Serialization; |
| 7 | +using Newtonsoft.Json.Linq; |
| 8 | +using Xunit; |
| 9 | +using Xunit.Abstractions; |
| 10 | + |
| 11 | +namespace Invictus.Testing.Tests.Integration |
| 12 | +{ |
| 13 | + public class LogicAppClientTests : IntegrationTest |
| 14 | + { |
| 15 | + /// <summary> |
| 16 | + /// Initializes a new instance of the <see cref="LogicAppClientTests"/> class. |
| 17 | + /// </summary> |
| 18 | + public LogicAppClientTests(ITestOutputHelper outputWriter) : base(outputWriter) |
| 19 | + { |
| 20 | + } |
| 21 | + |
| 22 | + [Fact] |
| 23 | + public async Task GetLogicAppTriggerUrl_NoTriggerNameSpecified_Success() |
| 24 | + { |
| 25 | + // Arrange |
| 26 | + using (var logicApp = await LogicAppClient.CreateAsync(ResourceGroup, LogicAppName, Authentication)) |
| 27 | + { |
| 28 | + // Act |
| 29 | + LogicAppTriggerUrl logicAppTriggerUrl = await logicApp.GetTriggerUrlAsync(); |
| 30 | + |
| 31 | + // Assert |
| 32 | + Assert.NotNull(logicAppTriggerUrl.Value); |
| 33 | + Assert.Equal("POST", logicAppTriggerUrl.Method); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + [Fact] |
| 38 | + public async Task GetLogicAppTriggerUrl_ByName_Success() |
| 39 | + { |
| 40 | + using (var logicApp = await LogicAppClient.CreateAsync(ResourceGroup, LogicAppName, Authentication)) |
| 41 | + { |
| 42 | + // Act |
| 43 | + LogicAppTriggerUrl logicAppTriggerUrl = await logicApp.GetTriggerUrlByNameAsync(triggerName: "manual"); |
| 44 | + |
| 45 | + // Assert |
| 46 | + Assert.NotNull(logicAppTriggerUrl.Value); |
| 47 | + Assert.Equal("POST", logicAppTriggerUrl.Method); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + [Fact] |
| 52 | + public async Task TemporaryEnableSuccessStaticResultForAction_WithoutConsumerStaticResult_Success() |
| 53 | + { |
| 54 | + // Arrange |
| 55 | + const string actionName = "HTTP"; |
| 56 | + string correlationId = $"correlationId-{Guid.NewGuid()}"; |
| 57 | + var headers = new Dictionary<string, string> |
| 58 | + { |
| 59 | + { "correlationId", correlationId }, |
| 60 | + }; |
| 61 | + |
| 62 | + // Act |
| 63 | + using (var logicApp = await LogicAppClient.CreateAsync(ResourceGroup, LogicAppMockingName, Authentication, Logger)) |
| 64 | + { |
| 65 | + await using (await logicApp.TemporaryEnableAsync()) |
| 66 | + { |
| 67 | + await using (await logicApp.TemporaryEnableSuccessStaticResultAsync(actionName)) |
| 68 | + { |
| 69 | + // Act |
| 70 | + await logicApp.TriggerAsync(headers); |
| 71 | + LogicAppAction enabledAction = await PollForLogicAppActionAsync(correlationId, actionName); |
| 72 | + |
| 73 | + Assert.Equal(actionName, enabledAction.Name); |
| 74 | + Assert.Equal("200", enabledAction.Outputs.statusCode.ToString()); |
| 75 | + Assert.Equal("Succeeded", enabledAction.Status); |
| 76 | + } |
| 77 | + |
| 78 | + await logicApp.TriggerAsync(headers); |
| 79 | + LogicAppAction disabledAction = await PollForLogicAppActionAsync(correlationId, actionName); |
| 80 | + |
| 81 | + Assert.NotEmpty(disabledAction.Outputs.headers); |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + [Fact] |
| 87 | + public async Task TemporaryEnableStaticResultsForAction_WithSuccessStaticResult_Success() |
| 88 | + { |
| 89 | + // Arrange |
| 90 | + const string actionName = "HTTP"; |
| 91 | + string correlationId = $"correlationId-{Guid.NewGuid()}"; |
| 92 | + var headers = new Dictionary<string, string> |
| 93 | + { |
| 94 | + { "correlationId", correlationId }, |
| 95 | + }; |
| 96 | + |
| 97 | + var definition = new StaticResultDefinition |
| 98 | + { |
| 99 | + Outputs = new Outputs |
| 100 | + { |
| 101 | + Headers = new Dictionary<string, string> { { "testheader", "testvalue" } }, |
| 102 | + StatusCode = "200", |
| 103 | + Body = JToken.Parse("{id : 12345, name : 'test body'}") |
| 104 | + }, |
| 105 | + Status = "Succeeded" |
| 106 | + }; |
| 107 | + |
| 108 | + // Act |
| 109 | + using (var logicApp = await LogicAppClient.CreateAsync(ResourceGroup, LogicAppMockingName, Authentication, Logger)) |
| 110 | + { |
| 111 | + await using (await logicApp.TemporaryEnableAsync()) |
| 112 | + { |
| 113 | + var definitions = new Dictionary<string, StaticResultDefinition> { [actionName] = definition }; |
| 114 | + await using (await logicApp.TemporaryEnableStaticResultsAsync(definitions)) |
| 115 | + { |
| 116 | + // Act |
| 117 | + await logicApp.TriggerAsync(headers); |
| 118 | + LogicAppAction enabledAction = await PollForLogicAppActionAsync(correlationId, actionName); |
| 119 | + |
| 120 | + Assert.Equal("200", enabledAction.Outputs.statusCode.ToString()); |
| 121 | + Assert.Equal("testvalue", enabledAction.Outputs.headers["testheader"].ToString()); |
| 122 | + Assert.Contains("test body", enabledAction.Outputs.body.ToString()); |
| 123 | + } |
| 124 | + |
| 125 | + await logicApp.TriggerAsync(headers); |
| 126 | + LogicAppAction disabledAction = await PollForLogicAppActionAsync(correlationId, actionName); |
| 127 | + |
| 128 | + Assert.DoesNotContain("test body", disabledAction.Outputs.body.ToString()); |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + [Fact] |
| 134 | + public async Task TemporaryEnableStaticResultForAction_WithSuccessStaticResult_Success() |
| 135 | + { |
| 136 | + // Arrange |
| 137 | + const string actionName = "HTTP"; |
| 138 | + string correlationId = $"correlationId-{Guid.NewGuid()}"; |
| 139 | + var headers = new Dictionary<string, string> |
| 140 | + { |
| 141 | + { "correlationId", correlationId }, |
| 142 | + }; |
| 143 | + |
| 144 | + var definition = new StaticResultDefinition |
| 145 | + { |
| 146 | + Outputs = new Outputs |
| 147 | + { |
| 148 | + Headers = new Dictionary<string, string> { { "testheader", "testvalue" } }, |
| 149 | + StatusCode = "200", |
| 150 | + Body = JToken.Parse("{id : 12345, name : 'test body'}") |
| 151 | + }, |
| 152 | + Status = "Succeeded" |
| 153 | + }; |
| 154 | + |
| 155 | + // Act |
| 156 | + using (var logicApp = await LogicAppClient.CreateAsync(ResourceGroup, LogicAppMockingName, Authentication)) |
| 157 | + { |
| 158 | + await using (await logicApp.TemporaryEnableAsync()) |
| 159 | + { |
| 160 | + await using (await logicApp.TemporaryEnableStaticResultAsync(actionName, definition)) |
| 161 | + { |
| 162 | + // Act |
| 163 | + await logicApp.TriggerAsync(headers); |
| 164 | + LogicAppAction enabledAction = await PollForLogicAppActionAsync(correlationId, actionName); |
| 165 | + |
| 166 | + Assert.Equal("200", enabledAction.Outputs.statusCode.ToString()); |
| 167 | + Assert.Equal("testvalue", enabledAction.Outputs.headers["testheader"].ToString()); |
| 168 | + Assert.Contains("test body", enabledAction.Outputs.body.ToString()); |
| 169 | + } |
| 170 | + |
| 171 | + await logicApp.TriggerAsync(headers); |
| 172 | + LogicAppAction disabledAction = await PollForLogicAppActionAsync(correlationId, actionName); |
| 173 | + |
| 174 | + Assert.DoesNotContain("test body", disabledAction.Outputs.body.ToString()); |
| 175 | + } |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + [Fact] |
| 180 | + public async Task TemporaryEnableLogicApp_Success() |
| 181 | + { |
| 182 | + // Act |
| 183 | + using (var logicApp = await LogicAppClient.CreateAsync(ResourceGroup, LogicAppMockingName, Authentication, Logger)) |
| 184 | + { |
| 185 | + await using (await logicApp.TemporaryEnableAsync()) |
| 186 | + { |
| 187 | + // Assert |
| 188 | + LogicApp metadata = await logicApp.GetMetadataAsync(); |
| 189 | + Assert.Equal("Enabled", metadata.State); |
| 190 | + } |
| 191 | + { |
| 192 | + LogicApp metadata = await logicApp.GetMetadataAsync(); |
| 193 | + Assert.Equal("Disabled", metadata.State); |
| 194 | + } |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + [Theory] |
| 199 | + [InlineData(null)] |
| 200 | + [InlineData("")] |
| 201 | + public async Task Constructor_WithBlankResourceGroup_Fails(string resourceGroup) |
| 202 | + { |
| 203 | + await Assert.ThrowsAsync<ArgumentException>( |
| 204 | + () => LogicAppClient.CreateAsync(resourceGroup, LogicAppName, Authentication)); |
| 205 | + } |
| 206 | + |
| 207 | + [Theory] |
| 208 | + [InlineData(null)] |
| 209 | + [InlineData("")] |
| 210 | + public async Task Constructor_WithBlankLogicApp_Fails(string logicApp) |
| 211 | + { |
| 212 | + await Assert.ThrowsAsync<ArgumentException>( |
| 213 | + () => LogicAppClient.CreateAsync(ResourceGroup, logicApp, Authentication)); |
| 214 | + } |
| 215 | + |
| 216 | + [Theory] |
| 217 | + [InlineData(null)] |
| 218 | + [InlineData("")] |
| 219 | + public async Task ConstructorWithLogger_WithBlankResourceGroup_Fails(string resourceGroup) |
| 220 | + { |
| 221 | + await Assert.ThrowsAsync<ArgumentException>( |
| 222 | + () => LogicAppClient.CreateAsync(resourceGroup, LogicAppName, Authentication, Logger)); |
| 223 | + } |
| 224 | + |
| 225 | + [Theory] |
| 226 | + [InlineData(null)] |
| 227 | + [InlineData("")] |
| 228 | + public async Task ConstructorWithLogger_WithBlankLogicApp_Fails(string logicApp) |
| 229 | + { |
| 230 | + await Assert.ThrowsAsync<ArgumentException>( |
| 231 | + () => LogicAppClient.CreateAsync(ResourceGroup, logicApp, Authentication, Logger)); |
| 232 | + } |
| 233 | + |
| 234 | + [Fact] |
| 235 | + public async Task Constructor_WithNullAuthentication_Fails() |
| 236 | + { |
| 237 | + await Assert.ThrowsAnyAsync<ArgumentException>( |
| 238 | + () => LogicAppClient.CreateAsync(ResourceGroup, LogicAppName, authentication: null)); |
| 239 | + } |
| 240 | + |
| 241 | + [Fact] |
| 242 | + public async Task ConstructorWithLogger_WithNullAuthentication_Fails() |
| 243 | + { |
| 244 | + await Assert.ThrowsAnyAsync<ArgumentException>( |
| 245 | + () => LogicAppClient.CreateAsync(ResourceGroup, LogicAppName, authentication: null, logger: Logger)); |
| 246 | + } |
| 247 | + |
| 248 | + private async Task<LogicAppAction> PollForLogicAppActionAsync(string correlationId, string actionName) |
| 249 | + { |
| 250 | + LogicAppRun logicAppRun = await LogicAppsProvider |
| 251 | + .LocatedAt(ResourceGroup, LogicAppMockingName, Authentication, Logger) |
| 252 | + .WithStartTime(DateTimeOffset.UtcNow.AddMinutes(-1)) |
| 253 | + .WithCorrelationId(correlationId) |
| 254 | + .PollForSingleLogicAppRunAsync(); |
| 255 | + |
| 256 | + Assert.True(logicAppRun.Actions.Count() != 0); |
| 257 | + LogicAppAction logicAppAction = logicAppRun.Actions.First(action => action.Name.Equals(actionName)); |
| 258 | + Assert.NotNull(logicAppAction); |
| 259 | + |
| 260 | + return logicAppAction; |
| 261 | + } |
| 262 | + } |
| 263 | +} |
0 commit comments