-
Notifications
You must be signed in to change notification settings - Fork 332
Expand file tree
/
Copy pathRuntimeConfigLoaderTests.cs
More file actions
136 lines (104 loc) · 6.03 KB
/
RuntimeConfigLoaderTests.cs
File metadata and controls
136 lines (104 loc) · 6.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Abstractions;
using System.IO.Abstractions.TestingHelpers;
using System.Linq;
using System.Threading.Tasks;
using Azure.DataApiBuilder.Config;
using Azure.DataApiBuilder.Config.ObjectModel;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json.Linq;
namespace Azure.DataApiBuilder.Service.Tests.Configuration;
[TestClass]
public class RuntimeConfigLoaderTests
{
[DataTestMethod]
[DataRow("dab-config.CosmosDb_NoSql.json")]
[DataRow("dab-config.MsSql.json")]
[DataRow("dab-config.MySql.json")]
[DataRow("dab-config.PostgreSql.json")]
public async Task CanLoadStandardConfig(string configPath)
{
string fileContents = await File.ReadAllTextAsync(configPath);
IFileSystem fs = new MockFileSystem(new Dictionary<string, MockFileData>() { { "dab-config.json", new MockFileData(fileContents) } });
FileSystemRuntimeConfigLoader loader = new(fs);
Assert.IsTrue(loader.TryLoadConfig("dab-config.json", out RuntimeConfig _), "Failed to load config");
}
/// <summary>
/// Test validates that when child files are present all datasources are loaded correctly.
/// </summary>
[DataTestMethod]
[DataRow("Multidab-config.CosmosDb_NoSql.json", new string[] { "Multidab-config.MsSql.json", "Multidab-config.MySql.json", "Multidab-config.PostgreSql.json" })]
public async Task CanLoadValidMultiSourceConfig(string configPath, IEnumerable<string> dataSourceFiles)
{
string fileContents = await File.ReadAllTextAsync(configPath);
// Parse the base JSON string
JObject baseJsonObject = JObject.Parse(fileContents);
// Create a new JArray to hold the values to be appended
JArray valuesToAppend = new(dataSourceFiles);
// Add or append the values to the base JSON
baseJsonObject.Add("data-source-files", valuesToAppend);
// Convert the modified JSON object back to a JSON string
string resultJson = baseJsonObject.ToString();
IFileSystem fs = new MockFileSystem(new Dictionary<string, MockFileData>() { { "dab-config.json", new MockFileData(resultJson) } });
FileSystemRuntimeConfigLoader loader = new(fs);
Assert.IsTrue(loader.TryLoadConfig("dab-config.json", out RuntimeConfig runtimeConfig), "Should successfully load config");
Assert.IsTrue(runtimeConfig.ListAllDataSources().Count() == 4, "Should have 4 data sources");
Assert.IsTrue(runtimeConfig.CosmosDataSourceUsed, "Should have CosmosDb data source");
Assert.IsTrue(runtimeConfig.SqlDataSourceUsed, "Should have Sql data source");
Assert.AreEqual(DatabaseType.CosmosDB_NoSQL, runtimeConfig.DataSource.DatabaseType, "Default datasource should be of root file database type.");
}
/// <summary>
/// Test validates that load fails when datasource files have duplicate entities.
/// Example: Publisher entity present in the 3 sql.json files.
/// </summary>
[DataTestMethod]
[DataRow("dab-config.CosmosDb_NoSql.json", new string[] { "dab-config.MsSql.json", "dab-config.MySql.json", "dab-config.PostgreSql.json" })]
public async Task FailLoadMultiDataSourceConfigDuplicateEntities(string configPath, IEnumerable<string> dataSourceFiles)
{
string fileContents = await File.ReadAllTextAsync(configPath);
// Parse the base JSON string
JObject baseJsonObject = JObject.Parse(fileContents);
// Create a new JArray to hold the values to be appended
JArray valuesToAppend = new(dataSourceFiles);
// Add or append the values to the base JSON
baseJsonObject.Add("data-source-files", valuesToAppend);
// Convert the modified JSON object back to a JSON string
string resultJson = baseJsonObject.ToString();
IFileSystem fs = new MockFileSystem(new Dictionary<string, MockFileData>() { { "dab-config.json", new MockFileData(resultJson) } });
FileSystemRuntimeConfigLoader loader = new(fs);
StringWriter sw = new();
Console.SetError(sw);
loader.TryLoadConfig("dab-config.json", out RuntimeConfig _);
string error = sw.ToString();
Assert.IsTrue(error.Contains("An item with the same key has already been added."),
"Error should contain the specific validation message.");
Assert.IsFalse(error.Contains("Stack Trace"),
"Stack trace should not be present in error output.");
}
/// <summary>
/// Test validates that when child files are present all autoentities are loaded correctly.
/// </summary>
[DataTestMethod]
[DataRow("Multidab-config.CosmosDb_NoSql.json", new string[] { "Multidab-config.MsSql.json", "Multidab-config.MySql.json", "Multidab-config.PostgreSql.json" }, 36)]
public async Task CanLoadValidMultiSourceConfigWithAutoentities(string configPath, IEnumerable<string> dataSourceFiles, int expectedEntities)
{
string fileContents = await File.ReadAllTextAsync(configPath);
// Parse the base JSON string
JObject baseJsonObject = JObject.Parse(fileContents);
// Create a new JArray to hold the values to be appended
JArray valuesToAppend = new(dataSourceFiles);
// Add or append the values to the base JSON
baseJsonObject.Add("data-source-files", valuesToAppend);
// Convert the modified JSON object back to a JSON string
string resultJson = baseJsonObject.ToString();
IFileSystem fs = new MockFileSystem(new Dictionary<string, MockFileData>() { { "dab-config.json", new MockFileData(resultJson) } });
FileSystemRuntimeConfigLoader loader = new(fs);
Assert.IsTrue(loader.TryLoadConfig("dab-config.json", out RuntimeConfig runtimeConfig), "Should successfully load config");
Assert.IsTrue(runtimeConfig.SqlDataSourceUsed, "Should have Sql data source");
Assert.AreEqual(expectedEntities, runtimeConfig.Entities.Entities.Count, "Number of entities is not what is expected.");
}
}