|
5 | 5 | using System.Data;
|
6 | 6 | using System.IO;
|
7 | 7 | using System.IO.Abstractions.TestingHelpers;
|
| 8 | +using System.Text; |
8 | 9 | using System.Text.Json;
|
9 | 10 | using Azure.DataApiBuilder.Config;
|
10 | 11 | using Azure.DataApiBuilder.Config.Converters;
|
|
15 | 16 | namespace Azure.DataApiBuilder.Service.Tests.UnitTests
|
16 | 17 | {
|
17 | 18 | /// <summary>
|
18 |
| - /// Unit tests for the environment variable |
19 |
| - /// parser for the runtime configuration. These |
| 19 | + /// Unit tests for deserializing the runtime configuration. These |
20 | 20 | /// tests verify that we parse the config correctly
|
21 | 21 | /// when replacing environment variables. Also verify
|
22 | 22 | /// we throw the right exception when environment
|
@@ -127,6 +127,34 @@ public void CheckCommentParsingInConfigFile()
|
127 | 127 | Assert.IsTrue(RuntimeConfigLoader.TryParseConfig(actualJson, out RuntimeConfig _), "Should not fail to parse with comments");
|
128 | 128 | }
|
129 | 129 |
|
| 130 | + /// <summary> |
| 131 | + /// Method to validate that comments are skipped in config file (and are ignored during deserialization). |
| 132 | + /// </summary> |
| 133 | + [TestMethod] |
| 134 | + public void TestDefaultsCreatedForOptionalProps() |
| 135 | + { |
| 136 | + // Test with no runtime property |
| 137 | + StringBuilder minJson = new(@" |
| 138 | + ""data-source"": { |
| 139 | + ""database-type"": ""mssql"", |
| 140 | + ""connection-string"": ""@env('test-connection-string')"" |
| 141 | + }, |
| 142 | + ""entities"": { }"); |
| 143 | + TryParseAndAssertOnDefaults("{" + minJson + "}"); |
| 144 | + |
| 145 | + // Test with an empty runtime property |
| 146 | + minJson.Append(@", ""runtime"": "); |
| 147 | + TryParseAndAssertOnDefaults("{" + minJson + "{ }}"); |
| 148 | + |
| 149 | + // Test with empty rest, graphql, host properties |
| 150 | + minJson.Append(@"{ ""rest"": { }, ""graphql"": { }, ""host"" : "); |
| 151 | + TryParseAndAssertOnDefaults("{" + minJson + "{ } } }", isHostSpecifiedButEmpty: true); |
| 152 | + |
| 153 | + // Test with empty rest, graphql, and empty host sub-properties |
| 154 | + minJson.Append(@"{ ""cors"": { }, ""authentication"": { } } }"); |
| 155 | + TryParseAndAssertOnDefaults("{" + minJson + "}", isHostSpecifiedButEmpty: false); |
| 156 | + } |
| 157 | + |
130 | 158 | #endregion Positive Tests
|
131 | 159 |
|
132 | 160 | #region Negative Tests
|
@@ -301,6 +329,31 @@ public static string GetModifiedJsonString(string[] reps, string enumString)
|
301 | 329 | ";
|
302 | 330 | }
|
303 | 331 |
|
| 332 | + private static void TryParseAndAssertOnDefaults(string json, bool isHostSpecifiedButEmpty = false) |
| 333 | + { |
| 334 | + Assert.IsTrue(RuntimeConfigLoader.TryParseConfig(json, out RuntimeConfig parsedConfig)); |
| 335 | + Assert.AreEqual(RuntimeConfig.DEFAULT_CONFIG_SCHEMA_LINK, parsedConfig.Schema); |
| 336 | + Assert.IsTrue(parsedConfig.Runtime.Rest.Enabled); |
| 337 | + Assert.AreEqual(RestRuntimeOptions.DEFAULT_PATH, parsedConfig.Runtime.Rest.Path); |
| 338 | + Assert.IsTrue(parsedConfig.Runtime.GraphQL.Enabled); |
| 339 | + Assert.AreEqual(GraphQLRuntimeOptions.DEFAULT_PATH, parsedConfig.Runtime.GraphQL.Path); |
| 340 | + Assert.IsTrue(parsedConfig.Runtime.GraphQL.AllowIntrospection); |
| 341 | + Assert.IsNull(parsedConfig.Runtime.BaseRoute); |
| 342 | + Assert.AreEqual(HostMode.Development, parsedConfig.Runtime.Host.Mode); |
| 343 | + if (isHostSpecifiedButEmpty) |
| 344 | + { |
| 345 | + Assert.IsNull(parsedConfig.Runtime.Host.Cors); |
| 346 | + Assert.IsNull(parsedConfig.Runtime.Host.Authentication); |
| 347 | + } |
| 348 | + else |
| 349 | + { |
| 350 | + Assert.AreEqual(0, parsedConfig.Runtime.Host.Cors.Origins.Length); |
| 351 | + Assert.IsFalse(parsedConfig.Runtime.Host.Cors.AllowCredentials); |
| 352 | + Assert.AreEqual(EasyAuthType.StaticWebApps.ToString(), parsedConfig.Runtime.Host.Authentication.Provider); |
| 353 | + Assert.IsNull(parsedConfig.Runtime.Host.Authentication.Jwt); |
| 354 | + } |
| 355 | + } |
| 356 | + |
304 | 357 | #endregion Helper Functions
|
305 | 358 |
|
306 | 359 | record StubJsonType(string Foo);
|
|
0 commit comments