-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: specific class for configured connection-url factory (#684)
* feat: read from appsettings.son * feat: add class ConfiguredConnectionUrlFactory
- Loading branch information
Showing
7 changed files
with
227 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
DubUrl.Extensions.Testing/Configuration/ConfiguredConnectionUrlFactoryTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Configuration; | ||
using NUnit.Framework; | ||
using DubUrl.Extensions.Configuration; | ||
using DubUrl.Mapping; | ||
|
||
namespace DubUrl.Extensions.Testing.Configuration | ||
{ | ||
public class ConfiguredConnectionUrlFactoryTest | ||
{ | ||
[Test()] | ||
public void FromConfiguration_ExistingConnectionString_ValueReturned() | ||
{ | ||
var connectionUrl = "mssql://localhost/Customers"; | ||
var connectionName = "Customers"; | ||
var connectionStrings = new Dictionary<string, string?> | ||
{ | ||
[$"ConnectionStrings:{connectionName}"] = connectionUrl | ||
}; | ||
var config = new ConfigurationBuilder() | ||
.AddInMemoryCollection(connectionStrings) | ||
.Build(); | ||
|
||
var factory = new ConfiguredConnectionUrlFactory(new SchemeMapperBuilder(), config); | ||
Assert.That(factory.InstantiateFromConnectionStrings(connectionName).Url, Is.EqualTo(connectionUrl)); | ||
} | ||
|
||
[Test()] | ||
public void FromConfiguration_ExistingKeys_ValueReturned() | ||
{ | ||
var connectionUrl = "mssql://localhost/Customers"; | ||
var key = "Databases:Customers:ConnectionUrl"; | ||
var databases = new Dictionary<string, string?> | ||
{ | ||
[key] = connectionUrl | ||
}; | ||
var config = new ConfigurationBuilder() | ||
.AddInMemoryCollection(databases) | ||
.Build(); | ||
|
||
var factory = new ConfiguredConnectionUrlFactory(new SchemeMapperBuilder(), config); | ||
Assert.That(factory.InstantiateFromConfiguration(key.Split(':')).Url, Is.EqualTo(connectionUrl)); | ||
} | ||
|
||
[Test()] | ||
[TestCase("Foo")] | ||
[TestCase("Databases:Foo")] | ||
[TestCase("Databases:Customers:Foo")] | ||
public void FromConfiguration_NotExistingKeys_Throws(string keys) | ||
{ | ||
var connectionUrl = "mssql://localhost/Customers"; | ||
var key = "Databases:Customers:ConnectionUrl"; | ||
var databases = new Dictionary<string, string?> | ||
{ | ||
[key] = connectionUrl | ||
}; | ||
var config = new ConfigurationBuilder() | ||
.AddInMemoryCollection(databases) | ||
.Build(); | ||
|
||
var factory = new ConfiguredConnectionUrlFactory(new SchemeMapperBuilder(), config); | ||
Assert.Throws<KeyNotFoundException>(() => factory.InstantiateFromConfiguration(keys.Split(':'))); | ||
} | ||
|
||
[Test()] | ||
public void BindFromConfiguration_ExistingKeyWithDetails_ReturnsValue() | ||
{ | ||
var connectionUrl = "mssql://foo:bar@localhost:1234/Customers?foo=1&bar=2"; | ||
var key = "Databases:Customers:ConnectionUrl"; | ||
var databases = new Dictionary<string, string?> | ||
{ | ||
[$"{key}:scheme"] = "mssql", | ||
[$"{key}:host"] = "localhost", | ||
[$"{key}:port"] = "1234", | ||
[$"{key}:username"] = "foo", | ||
[$"{key}:password"] = "bar", | ||
[$"{key}:segments:0"] = "Customers", | ||
[$"{key}:keys:0"] = "foo", | ||
[$"{key}:values:0"] = "1", | ||
[$"{key}:keys:1"] = "bar", | ||
[$"{key}:values:1"] = "2", | ||
}; | ||
var config = new ConfigurationBuilder() | ||
.AddInMemoryCollection(databases) | ||
.Build(); | ||
|
||
var factory = new ConfiguredConnectionUrlFactory(new SchemeMapperBuilder(), config); | ||
Assert.That(factory.InstantiateWithBind(key.Split(':')).Url, Is.EqualTo(connectionUrl)); | ||
} | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
DubUrl.Extensions/Configuration/ConfiguredConnectionUrlFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using DubUrl.Mapping; | ||
using Microsoft.Extensions.Configuration; | ||
|
||
namespace DubUrl.Extensions.Configuration | ||
{ | ||
public class ConfiguredConnectionUrlFactory : ConnectionUrlFactory | ||
{ | ||
private IConfigurationRoot Configuration { get; } | ||
|
||
public ConfiguredConnectionUrlFactory(SchemeMapperBuilder builder) | ||
: base(builder) | ||
{ | ||
Configuration = new ConfigurationBuilder() | ||
.AddJsonFile("appsettings.json") | ||
.AddEnvironmentVariables() | ||
.Build(); | ||
} | ||
|
||
public ConfiguredConnectionUrlFactory(SchemeMapperBuilder builder, IConfigurationRoot config) | ||
: base(builder) | ||
=> Configuration = config; | ||
|
||
public ConnectionUrl InstantiateFromConnectionStrings(string name) | ||
=> Instantiate(Configuration.GetConnectionString(name) | ||
?? throw new ArgumentOutOfRangeException(nameof(name), $"Cannot find a connection string named '{name}' in the section 'ConnectionStrings'")); | ||
|
||
public ConnectionUrl InstantiateFromConfiguration(string[] keys) | ||
=> Instantiate(GetSection(Configuration, keys).Value | ||
?? throw new NullReferenceException($"Value of the key '{string.Join('.', keys)}' is null.")); | ||
|
||
public ConnectionUrl InstantiateWithBind(string[] keys) | ||
=> Instantiate(GetSection(Configuration, keys).Get<ConnectionUrlSettings>().ToString()); | ||
|
||
private static IConfigurationSection GetSection(IConfigurationRoot config, string[] keys) | ||
{ | ||
IConfigurationSection? section = null; | ||
if (!keys.Any()) | ||
throw new ArgumentOutOfRangeException(nameof(keys), $"The provided keys cannot be an empty array."); | ||
for (int i = 0; i < keys.Length; i++) | ||
{ | ||
section = section is not null ? section.GetSection(keys[i]) : config.GetSection(keys[i]); | ||
if (!section.Exists()) | ||
if (i == 0) | ||
throw new KeyNotFoundException($"Cannot find the configuration key '{keys[i]}' at the root of configuration."); | ||
else | ||
throw new KeyNotFoundException($"Cannot find the configuration key '{keys[i]}' under the section '{string.Join('.', keys.Take(i))}'."); | ||
} | ||
return section!; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
[ConnectionStrings] | ||
Customers=duckdb://localhost/Customers | ||
|
||
[Databases] | ||
Customers=sqlite://localhost/Customers | ||
|
||
[Details] | ||
scheme=firebird | ||
host=remote.database.org | ||
port=1234 | ||
segment=myInstance/Customers | ||
|