Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/Cli.Tests/ConfigGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ namespace Cli.Tests;
public class ConfigGeneratorTests
{
private IFileSystem? _fileSystem;
private RuntimeConfigLoader? _runtimeConfigLoader;
private FileSystemRuntimeConfigLoader? _runtimeConfigLoader;

[TestInitialize]
public void TestInitialize()
{
_fileSystem = FileSystemUtils.ProvisionMockFileSystem();

_runtimeConfigLoader = new RuntimeConfigLoader(_fileSystem);
_runtimeConfigLoader = new FileSystemRuntimeConfigLoader(_fileSystem);

ILoggerFactory loggerFactory = TestLoggerSupport.ProvisionLoggerFactory();

Expand Down Expand Up @@ -49,9 +49,9 @@ public void TryGenerateConfig_WithUserProvidedConfig(

// Mocking logger to assert on logs
Mock<ILogger<ConfigGenerator>> loggerMock = new();
ConfigGenerator.SetLoggerForCliConfigGenerator(loggerMock.Object);
SetLoggerForCliConfigGenerator(loggerMock.Object);

Assert.AreEqual(isConfigGenerationSuccessful, ConfigGenerator.TryGenerateConfig(options, _runtimeConfigLoader!, _fileSystem!));
Assert.AreEqual(isConfigGenerationSuccessful, TryGenerateConfig(options, _runtimeConfigLoader!, _fileSystem!));

if (!isConfigFilePresent)
{
Expand Down Expand Up @@ -96,9 +96,9 @@ public void TryGenerateConfig_UsingEnvironmentVariable(

// Mocking logger to assert on logs
Mock<ILogger<ConfigGenerator>> loggerMock = new();
ConfigGenerator.SetLoggerForCliConfigGenerator(loggerMock.Object);
SetLoggerForCliConfigGenerator(loggerMock.Object);

Assert.AreEqual(isConfigGenerationSuccessful, ConfigGenerator.TryGenerateConfig(options, _runtimeConfigLoader!, _fileSystem!));
Assert.AreEqual(isConfigGenerationSuccessful, TryGenerateConfig(options, _runtimeConfigLoader!, _fileSystem!));
if (!isConfigFilePresent)
{
Assert.AreEqual(isConfigGenerationSuccessful, _fileSystem!.File.Exists(configFileName));
Expand Down
4 changes: 2 additions & 2 deletions src/Cli.Tests/EndToEndTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class EndToEndTests
: VerifyBase
{
private IFileSystem? _fileSystem;
private RuntimeConfigLoader? _runtimeConfigLoader;
private FileSystemRuntimeConfigLoader? _runtimeConfigLoader;
private ILogger<Program>? _cliLogger;

[TestInitialize]
Expand All @@ -26,7 +26,7 @@ public void TestInitialize()

_fileSystem = fileSystem;

_runtimeConfigLoader = new RuntimeConfigLoader(_fileSystem);
_runtimeConfigLoader = new FileSystemRuntimeConfigLoader(_fileSystem);

ILoggerFactory loggerFactory = TestLoggerSupport.ProvisionLoggerFactory();

Expand Down
4 changes: 2 additions & 2 deletions src/Cli.Tests/InitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ public class InitTests
: VerifyBase
{
private IFileSystem? _fileSystem;
private RuntimeConfigLoader? _runtimeConfigLoader;
private FileSystemRuntimeConfigLoader? _runtimeConfigLoader;

[TestInitialize]
public void TestInitialize()
{
_fileSystem = FileSystemUtils.ProvisionMockFileSystem();

_runtimeConfigLoader = new RuntimeConfigLoader(_fileSystem);
_runtimeConfigLoader = new FileSystemRuntimeConfigLoader(_fileSystem);

ILoggerFactory loggerFactory = TestLoggerSupport.ProvisionLoggerFactory();

Expand Down
2 changes: 1 addition & 1 deletion src/Cli.Tests/Usings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
global using Microsoft.VisualStudio.TestTools.UnitTesting;
global using Moq;
global using Newtonsoft.Json.Linq;
global using static Azure.DataApiBuilder.Config.RuntimeConfigLoader;
global using static Azure.DataApiBuilder.Config.FileSystemRuntimeConfigLoader;
global using static Cli.ConfigGenerator;
global using static Cli.Tests.TestHelper;
global using static Cli.Utils;
26 changes: 13 additions & 13 deletions src/Cli.Tests/UtilsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ public void ConstructGraphQLOptionsWithSingularAndPluralWillSetSingularAndPlural
[DataTestMethod]
[DataRow("", "my-config.json", "my-config.json", DisplayName = "user provided the config file and environment variable was not set.")]
[DataRow("Test", "my-config.json", "my-config.json", DisplayName = "user provided the config file and environment variable was set.")]
[DataRow("Test", null, $"{RuntimeConfigLoader.CONFIGFILE_NAME}.Test{RuntimeConfigLoader.CONFIG_EXTENSION}", DisplayName = "config not provided, but environment variable was set.")]
[DataRow("", null, $"{RuntimeConfigLoader.CONFIGFILE_NAME}{RuntimeConfigLoader.CONFIG_EXTENSION}", DisplayName = "neither config was provided, nor environment variable was set.")]
[DataRow("Test", null, $"{CONFIGFILE_NAME}.Test{CONFIG_EXTENSION}", DisplayName = "config not provided, but environment variable was set.")]
[DataRow("", null, $"{CONFIGFILE_NAME}{CONFIG_EXTENSION}", DisplayName = "neither config was provided, nor environment variable was set.")]
public void TestConfigSelectionBasedOnCliPrecedence(
string? environmentValue,
string? userProvidedConfigFile,
Expand All @@ -117,13 +117,13 @@ public void TestConfigSelectionBasedOnCliPrecedence(
MockFileSystem fileSystem = new();
fileSystem.AddFile(expectedRuntimeConfigFile, new MockFileData(""));

RuntimeConfigLoader loader = new(fileSystem);
FileSystemRuntimeConfigLoader loader = new(fileSystem);

string? envValueBeforeTest = Environment.GetEnvironmentVariable(RuntimeConfigLoader.RUNTIME_ENVIRONMENT_VAR_NAME);
Environment.SetEnvironmentVariable(RuntimeConfigLoader.RUNTIME_ENVIRONMENT_VAR_NAME, environmentValue);
string? envValueBeforeTest = Environment.GetEnvironmentVariable(RUNTIME_ENVIRONMENT_VAR_NAME);
Environment.SetEnvironmentVariable(RUNTIME_ENVIRONMENT_VAR_NAME, environmentValue);
Assert.IsTrue(TryGetConfigFileBasedOnCliPrecedence(loader, userProvidedConfigFile, out string? actualRuntimeConfigFile));
Assert.AreEqual(expectedRuntimeConfigFile, actualRuntimeConfigFile);
Environment.SetEnvironmentVariable(RuntimeConfigLoader.RUNTIME_ENVIRONMENT_VAR_NAME, envValueBeforeTest);
Environment.SetEnvironmentVariable(RUNTIME_ENVIRONMENT_VAR_NAME, envValueBeforeTest);
}

/// <summary>
Expand Down Expand Up @@ -239,14 +239,14 @@ public void TestValidateAudienceAndIssuerForAuthenticationProvider(
public void TestMergeConfig()
{
MockFileSystem fileSystem = new();
fileSystem.AddFile(RuntimeConfigLoader.DEFAULT_CONFIG_FILE_NAME, new MockFileData(BASE_CONFIG));
fileSystem.AddFile(DEFAULT_CONFIG_FILE_NAME, new MockFileData(BASE_CONFIG));
fileSystem.AddFile("dab-config.Test.json", new MockFileData(ENV_BASED_CONFIG));

RuntimeConfigLoader loader = new(fileSystem);
FileSystemRuntimeConfigLoader loader = new(fileSystem);

Environment.SetEnvironmentVariable(RuntimeConfigLoader.RUNTIME_ENVIRONMENT_VAR_NAME, "Test");
Environment.SetEnvironmentVariable(RUNTIME_ENVIRONMENT_VAR_NAME, "Test");

Assert.IsTrue(Cli.ConfigMerger.TryMergeConfigsIfAvailable(fileSystem, loader, new StringLogger(), out string? mergedConfig), "Failed to merge config files");
Assert.IsTrue(ConfigMerger.TryMergeConfigsIfAvailable(fileSystem, loader, new StringLogger(), out string? mergedConfig), "Failed to merge config files");
Assert.AreEqual(mergedConfig, "dab-config.Test.merged.json");
Assert.IsTrue(fileSystem.File.Exists(mergedConfig));
Assert.IsTrue(JToken.DeepEquals(JObject.Parse(MERGED_CONFIG), JObject.Parse(fileSystem.File.ReadAllText(mergedConfig))));
Expand Down Expand Up @@ -282,7 +282,7 @@ public void TestMergeConfigAvailability(
MockFileSystem fileSystem = new();

// Setting up the test scenarios
Environment.SetEnvironmentVariable(RuntimeConfigLoader.RUNTIME_ENVIRONMENT_VAR_NAME, environmentValue);
Environment.SetEnvironmentVariable(RUNTIME_ENVIRONMENT_VAR_NAME, environmentValue);
string baseConfig = "dab-config.json";
string envBasedConfig = "dab-config.Test.json";

Expand All @@ -296,15 +296,15 @@ public void TestMergeConfigAvailability(
fileSystem.AddFile(envBasedConfig, new("{}"));
}

RuntimeConfigLoader loader = new(fileSystem);
FileSystemRuntimeConfigLoader loader = new(fileSystem);

Assert.AreEqual(
expectedIsMergedConfigAvailable,
ConfigMerger.TryMergeConfigsIfAvailable(fileSystem, loader, new StringLogger(), out string? mergedConfigFile),
"Availability of merge config should match");
Assert.AreEqual(expectedMergedConfigFileName, mergedConfigFile, "Merge config file name should match expected");

Environment.SetEnvironmentVariable(RuntimeConfigLoader.RUNTIME_ENVIRONMENT_VAR_NAME, null);
Environment.SetEnvironmentVariable(RUNTIME_ENVIRONMENT_VAR_NAME, null);
}
}

2 changes: 1 addition & 1 deletion src/Cli/Commands/AddOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public AddOptions(
[Option("permissions", Required = true, Separator = ':', HelpText = "Permissions required to access the source table or container.")]
public IEnumerable<string> Permissions { get; }

public void Handler(ILogger logger, RuntimeConfigLoader loader, IFileSystem fileSystem)
public void Handler(ILogger logger, FileSystemRuntimeConfigLoader loader, IFileSystem fileSystem)
{
logger.LogInformation($"{PRODUCT_NAME} {ProductInfo.GetProductVersion()}");
if (!IsEntityProvided(Entity, logger, command: "add"))
Expand Down
2 changes: 1 addition & 1 deletion src/Cli/Commands/InitOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public InitOptions(
[Option("graphql.disabled", Default = false, Required = false, HelpText = "Disables GraphQL endpoint for all entities.")]
public bool GraphQLDisabled { get; }

public void Handler(ILogger logger, RuntimeConfigLoader loader, IFileSystem fileSystem)
public void Handler(ILogger logger, FileSystemRuntimeConfigLoader loader, IFileSystem fileSystem)
{
logger.LogInformation($"{PRODUCT_NAME} {ProductInfo.GetProductVersion()}");
bool isSuccess = ConfigGenerator.TryGenerateConfig(this, loader, fileSystem);
Expand Down
2 changes: 1 addition & 1 deletion src/Cli/Commands/StartOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public StartOptions(bool verbose, LogLevel? logLevel, bool isHttpsRedirectionDis
[Option("no-https-redirect", Required = false, HelpText = "Disables automatic https redirects.")]
public bool IsHttpsRedirectionDisabled { get; }

public void Handler(ILogger logger, RuntimeConfigLoader loader, IFileSystem fileSystem)
public void Handler(ILogger logger, FileSystemRuntimeConfigLoader loader, IFileSystem fileSystem)
{
logger.LogInformation($"{PRODUCT_NAME} {ProductInfo.GetProductVersion()}");
bool isSuccess = ConfigGenerator.TryStartEngineWithOptions(this, loader, fileSystem);
Expand Down
2 changes: 1 addition & 1 deletion src/Cli/Commands/UpdateOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public UpdateOptions(
[Option('m', "map", Separator = ',', Required = false, HelpText = "Specify mappings between database fields and GraphQL and REST fields. format: --map \"backendName1:exposedName1,backendName2:exposedName2,...\".")]
public IEnumerable<string>? Map { get; }

public void Handler(ILogger logger, RuntimeConfigLoader loader, IFileSystem fileSystem)
public void Handler(ILogger logger, FileSystemRuntimeConfigLoader loader, IFileSystem fileSystem)
{
logger.LogInformation($"{PRODUCT_NAME} {ProductInfo.GetProductVersion()}");
if (!IsEntityProvided(Entity, logger, command: "update"))
Expand Down
18 changes: 9 additions & 9 deletions src/Cli/ConfigGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,21 @@ public static void SetLoggerForCliConfigGenerator(
/// <summary>
/// This method will generate the initial config with databaseType and connection-string.
/// </summary>
public static bool TryGenerateConfig(InitOptions options, RuntimeConfigLoader loader, IFileSystem fileSystem)
public static bool TryGenerateConfig(InitOptions options, FileSystemRuntimeConfigLoader loader, IFileSystem fileSystem)
{
string runtimeConfigFile = RuntimeConfigLoader.DEFAULT_CONFIG_FILE_NAME;
string runtimeConfigFile = FileSystemRuntimeConfigLoader.DEFAULT_CONFIG_FILE_NAME;
if (!string.IsNullOrWhiteSpace(options.Config))
{
_logger.LogInformation("Generating user provided config file with name: {configFileName}", options.Config);
runtimeConfigFile = options.Config;
}
else
{
string? environmentValue = Environment.GetEnvironmentVariable(RuntimeConfigLoader.RUNTIME_ENVIRONMENT_VAR_NAME);
string? environmentValue = Environment.GetEnvironmentVariable(FileSystemRuntimeConfigLoader.RUNTIME_ENVIRONMENT_VAR_NAME);
if (!string.IsNullOrWhiteSpace(environmentValue))
{
_logger.LogInformation("The environment variable {variableName} has a value of {variableValue}", RuntimeConfigLoader.RUNTIME_ENVIRONMENT_VAR_NAME, environmentValue);
runtimeConfigFile = RuntimeConfigLoader.GetEnvironmentFileName(RuntimeConfigLoader.CONFIGFILE_NAME, environmentValue);
_logger.LogInformation("The environment variable {variableName} has a value of {variableValue}", FileSystemRuntimeConfigLoader.RUNTIME_ENVIRONMENT_VAR_NAME, environmentValue);
runtimeConfigFile = FileSystemRuntimeConfigLoader.GetEnvironmentFileName(FileSystemRuntimeConfigLoader.CONFIGFILE_NAME, environmentValue);
_logger.LogInformation("Generating environment config file: {configPath}", fileSystem.Path.GetFullPath(runtimeConfigFile));
}
else
Expand Down Expand Up @@ -81,7 +81,7 @@ public static bool TryGenerateConfig(InitOptions options, RuntimeConfigLoader lo
/// <param name="options">Init options</param>
/// <param name="runtimeConfig">Output runtime config json.</param>
/// <returns>True on success. False otherwise.</returns>
public static bool TryCreateRuntimeConfig(InitOptions options, RuntimeConfigLoader loader, IFileSystem fileSystem, [NotNullWhen(true)] out RuntimeConfig? runtimeConfig)
public static bool TryCreateRuntimeConfig(InitOptions options, FileSystemRuntimeConfigLoader loader, IFileSystem fileSystem, [NotNullWhen(true)] out RuntimeConfig? runtimeConfig)
{
runtimeConfig = null;

Expand Down Expand Up @@ -225,7 +225,7 @@ public static bool TryCreateRuntimeConfig(InitOptions options, RuntimeConfigLoad
/// This method will add a new Entity with the given REST and GraphQL endpoints, source, and permissions.
/// It also supports fields that needs to be included or excluded for a given role and operation.
/// </summary>
public static bool TryAddEntityToConfigWithOptions(AddOptions options, RuntimeConfigLoader loader, IFileSystem fileSystem)
public static bool TryAddEntityToConfigWithOptions(AddOptions options, FileSystemRuntimeConfigLoader loader, IFileSystem fileSystem)
{
if (!TryGetConfigFileBasedOnCliPrecedence(loader, options.Config, out string runtimeConfigFile))
{
Expand Down Expand Up @@ -455,7 +455,7 @@ public static bool TryCreateSourceObjectForNewEntity(
/// This method will update an existing Entity with the given REST and GraphQL endpoints, source, and permissions.
/// It also supports updating fields that need to be included or excluded for a given role and operation.
/// </summary>
public static bool TryUpdateEntityWithOptions(UpdateOptions options, RuntimeConfigLoader loader, IFileSystem fileSystem)
public static bool TryUpdateEntityWithOptions(UpdateOptions options, FileSystemRuntimeConfigLoader loader, IFileSystem fileSystem)
{
if (!TryGetConfigFileBasedOnCliPrecedence(loader, options.Config, out string runtimeConfigFile))
{
Expand Down Expand Up @@ -951,7 +951,7 @@ public static bool VerifyCanUpdateRelationship(RuntimeConfig runtimeConfig, stri
/// overrides < environmentConfig < defaultConfig
/// Also preforms validation to check connection string is not null or empty.
/// </summary>
public static bool TryStartEngineWithOptions(StartOptions options, RuntimeConfigLoader loader, IFileSystem fileSystem)
public static bool TryStartEngineWithOptions(StartOptions options, FileSystemRuntimeConfigLoader loader, IFileSystem fileSystem)
{
string? configToBeUsed = options.Config;
if (string.IsNullOrEmpty(configToBeUsed) && ConfigMerger.TryMergeConfigsIfAvailable(fileSystem, loader, _logger, out configToBeUsed))
Expand Down
8 changes: 4 additions & 4 deletions src/Cli/ConfigMerger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ public static class ConfigMerger
/// and create a merged file called dab-config.{DAB_ENVIRONMENT}.merged.json
/// </summary>
/// <returns>Returns the name of the merged Config if successful.</returns>
public static bool TryMergeConfigsIfAvailable(IFileSystem fileSystem, RuntimeConfigLoader loader, ILogger logger, out string? mergedConfigFile)
public static bool TryMergeConfigsIfAvailable(IFileSystem fileSystem, FileSystemRuntimeConfigLoader loader, ILogger logger, out string? mergedConfigFile)
{
string? environmentValue = Environment.GetEnvironmentVariable(RuntimeConfigLoader.RUNTIME_ENVIRONMENT_VAR_NAME);
string? environmentValue = Environment.GetEnvironmentVariable(FileSystemRuntimeConfigLoader.RUNTIME_ENVIRONMENT_VAR_NAME);
mergedConfigFile = null;
if (!string.IsNullOrEmpty(environmentValue))
{
string baseConfigFile = RuntimeConfigLoader.DEFAULT_CONFIG_FILE_NAME;
string baseConfigFile = FileSystemRuntimeConfigLoader.DEFAULT_CONFIG_FILE_NAME;
string environmentBasedConfigFile = loader.GetFileName(environmentValue, considerOverrides: false);

if (loader.DoesFileExistInCurrentDirectory(baseConfigFile) && !string.IsNullOrEmpty(environmentBasedConfigFile))
Expand All @@ -34,7 +34,7 @@ public static bool TryMergeConfigsIfAvailable(IFileSystem fileSystem, RuntimeCon
string currentDir = fileSystem.Directory.GetCurrentDirectory();
logger.LogInformation("Merging {baseFilePath} and {envFilePath}", Path.Combine(currentDir, baseConfigFile), Path.Combine(currentDir, environmentBasedConfigFile));
string mergedConfigJson = MergeJsonProvider.Merge(baseConfigJson, overrideConfigJson);
mergedConfigFile = RuntimeConfigLoader.GetMergedFileNameForEnvironment(RuntimeConfigLoader.CONFIGFILE_NAME, environmentValue);
mergedConfigFile = FileSystemRuntimeConfigLoader.GetMergedFileNameForEnvironment(FileSystemRuntimeConfigLoader.CONFIGFILE_NAME, environmentValue);
fileSystem.File.WriteAllText(mergedConfigFile, mergedConfigJson);
logger.LogInformation("Generated merged config file: {mergedFile}", Path.Combine(currentDir, mergedConfigFile));
return true;
Expand Down
2 changes: 1 addition & 1 deletion src/Cli/Exporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Cli
{
internal static class Exporter
{
public static void Export(ExportOptions options, ILogger logger, RuntimeConfigLoader loader, IFileSystem fileSystem)
public static void Export(ExportOptions options, ILogger logger, FileSystemRuntimeConfigLoader loader, IFileSystem fileSystem)
{
StartOptions startOptions = new(false, LogLevel.None, false, options.Config!);

Expand Down
4 changes: 2 additions & 2 deletions src/Cli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ public static int Main(string[] args)
ConfigGenerator.SetLoggerForCliConfigGenerator(configGeneratorLogger);
Utils.SetCliUtilsLogger(cliUtilsLogger);
IFileSystem fileSystem = new FileSystem();
RuntimeConfigLoader loader = new(fileSystem);
FileSystemRuntimeConfigLoader loader = new(fileSystem);

return Execute(args, cliLogger, fileSystem, loader);
}

public static int Execute(string[] args, ILogger cliLogger, IFileSystem fileSystem, RuntimeConfigLoader loader)
public static int Execute(string[] args, ILogger cliLogger, IFileSystem fileSystem, FileSystemRuntimeConfigLoader loader)
{
// To know if `--help` or `--version` was requested.
bool isHelpOrVersionRequested = false;
Expand Down
Loading