-
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(configuration): added configuration section-related functionality …
- Loading branch information
Showing
18 changed files
with
245 additions
and
3 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
10 changes: 10 additions & 0 deletions
10
Attest.Testing.Contracts/Configuration/IConfigurationSectionKeySplitter.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,10 @@ | ||
using System.Collections.Generic; | ||
|
||
// ReSharper disable once CheckNamespace | ||
namespace Attest.Testing.Configuration | ||
{ | ||
public interface IConfigurationSectionKeySplitter | ||
{ | ||
IEnumerable<string> Split(string key); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
Attest.Testing.Contracts/Configuration/IConfigurationSectionValueProvider.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,19 @@ | ||
using Microsoft.Extensions.Configuration; | ||
|
||
// ReSharper disable once CheckNamespace | ||
namespace Attest.Testing.Configuration | ||
{ | ||
/// <summary> | ||
/// Provides facilities for interacting with configuration sections. | ||
/// </summary> | ||
public interface IConfigurationSectionValueProvider | ||
{ | ||
/// <summary> | ||
/// Gets the value for the configuration section by its key. | ||
/// </summary> | ||
/// <param name="configuration">The configuration.</param> | ||
/// <param name="key">The section key.</param> | ||
/// <returns></returns> | ||
string GetValue(IConfiguration configuration, string key); | ||
} | ||
} |
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
16 changes: 16 additions & 0 deletions
16
Attest.Testing.Core.Specs/Configuration/Configuration.feature
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,16 @@ | ||
Feature: Configuration | ||
In order to support development with configuration | ||
As an app developer | ||
I want the framework to be able to resolve value by key | ||
|
||
Scenario: Resolving simple value by an existing key should return value | ||
Given The configuration uses environment variable compatible key splitter | ||
And The configuration contains the key "key" mapped to value "value" | ||
When I get the value by this key | ||
Then The value is resolved successfully | ||
|
||
Scenario: Resolving simple value by a non-existing key should return null | ||
Given The configuration uses environment variable compatible key splitter | ||
And The configuration does not contain the key "key" | ||
When I get the value by this key | ||
Then The value is returned as null |
50 changes: 50 additions & 0 deletions
50
Attest.Testing.Core.Specs/Configuration/ConfigurationScenarioDataStore.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,50 @@ | ||
using Attest.Fake.Core; | ||
using Attest.Testing.Configuration; | ||
using Attest.Testing.Context.SpecFlow; | ||
using Microsoft.Extensions.Configuration; | ||
using TechTalk.SpecFlow; | ||
|
||
namespace Attest.Testing.Core.Specs | ||
{ | ||
internal sealed class ConfigurationScenarioDataStore : ScenarioDataStoreBase | ||
{ | ||
public ConfigurationScenarioDataStore(ScenarioContext scenarioContext) : base(scenarioContext) | ||
{} | ||
|
||
public string Key | ||
{ | ||
get => GetValue<string>(); | ||
set => SetValue(value); | ||
} | ||
|
||
public string ExpectedValue | ||
{ | ||
get => GetValue<string>(); | ||
set => SetValue(value); | ||
} | ||
|
||
public string ActualValue | ||
{ | ||
get => GetValue<string>(); | ||
set => SetValue(value); | ||
} | ||
|
||
public IConfigurationSectionKeySplitter KeySplitter | ||
{ | ||
get => GetValue<IConfigurationSectionKeySplitter>(); | ||
set => SetValue(value); | ||
} | ||
|
||
public IFake<IConfiguration> Configuration | ||
{ | ||
get => GetValue<IFake<IConfiguration>>(); | ||
set => SetValue(value); | ||
} | ||
|
||
public IFake<IConfigurationSection> ConfigurationSection | ||
{ | ||
get => GetValue<IFake<IConfigurationSection>>(); | ||
set => SetValue(value); | ||
} | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
Attest.Testing.Core.Specs/Configuration/ConfigurationSteps.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,83 @@ | ||
using Attest.Fake.Core; | ||
using Attest.Fake.Moq; | ||
using Attest.Testing.Configuration; | ||
using FluentAssertions; | ||
using Microsoft.Extensions.Configuration; | ||
using TechTalk.SpecFlow; | ||
|
||
namespace Attest.Testing.Core.Specs | ||
{ | ||
[Binding] | ||
internal sealed class ConfigurationSteps | ||
{ | ||
private readonly ConfigurationScenarioDataStore _configurationScenarioDataStore; | ||
private readonly IFakeFactory _fakeFactory; | ||
|
||
public ConfigurationSteps(ScenarioContext scenarioContext) | ||
{ | ||
_configurationScenarioDataStore = new ConfigurationScenarioDataStore(scenarioContext); | ||
_fakeFactory = new FakeFactory(); | ||
} | ||
|
||
[Given(@"The configuration uses environment variable compatible key splitter")] | ||
public void GivenTheConfigurationUsesEnvironmentVariableCompatibleKeySplitter() | ||
{ | ||
_configurationScenarioDataStore.KeySplitter = new EnvironmentVariableKeySplitter(); | ||
} | ||
|
||
[Given(@"The configuration contains the key ""(.*)"" mapped to value ""(.*)""")] | ||
public void GivenTheConfigurationContainsTheKeyMappedToValue(string key, string value) | ||
{ | ||
_configurationScenarioDataStore.Key = key; | ||
_configurationScenarioDataStore.ExpectedValue = value; | ||
_configurationScenarioDataStore.Configuration = _fakeFactory.CreateFake<IConfiguration>(); | ||
_configurationScenarioDataStore.ConfigurationSection = _fakeFactory.CreateFake<IConfigurationSection>(); | ||
_configurationScenarioDataStore.ConfigurationSection | ||
.Setup(t => t.Value) | ||
.Callback(() => _configurationScenarioDataStore.ExpectedValue); | ||
_configurationScenarioDataStore.Configuration | ||
.Setup(t => t.GetSection(key)) | ||
.Callback(() => _configurationScenarioDataStore.ConfigurationSection.Object); | ||
} | ||
|
||
[Given(@"The configuration does not contain the key ""(.*)""")] | ||
public void GivenTheConfigurationDoesNotContainTheKeyMappedToValue(string key) | ||
{ | ||
_configurationScenarioDataStore.Key = key; | ||
_configurationScenarioDataStore.Configuration = _fakeFactory.CreateFake<IConfiguration>(); | ||
_configurationScenarioDataStore.Configuration | ||
.Setup(t => t.GetSection(key)) | ||
.Callback(() => null); | ||
} | ||
|
||
[When(@"I get the value by this key")] | ||
public void WhenIGetTheValueByThisKey() | ||
{ | ||
//TODO: Inject/Resolve | ||
var configurationSectionValueProvider = | ||
new ConfigurationSectionValueProvider(_configurationScenarioDataStore.KeySplitter); | ||
var value = configurationSectionValueProvider.GetValue( | ||
_configurationScenarioDataStore.Configuration.Object, | ||
_configurationScenarioDataStore.Key); | ||
_configurationScenarioDataStore.ActualValue = value; | ||
} | ||
|
||
[Then(@"The value is resolved successfully")] | ||
public void ThenTheValueIsResolvedSuccessfully() | ||
{ | ||
_configurationScenarioDataStore | ||
.ActualValue | ||
.Should() | ||
.Be(_configurationScenarioDataStore.ExpectedValue); | ||
} | ||
|
||
[Then(@"The value is returned as null")] | ||
public void ThenTheValueIsReturnedAsNull() | ||
{ | ||
_configurationScenarioDataStore | ||
.ActualValue | ||
.Should() | ||
.BeNull(); | ||
} | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
34 changes: 34 additions & 0 deletions
34
Attest.Testing.Core/Configuration/ConfigurationSectionValueProvider.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,34 @@ | ||
using System.Linq; | ||
using Microsoft.Extensions.Configuration; | ||
|
||
// ReSharper disable once CheckNamespace | ||
namespace Attest.Testing.Configuration | ||
{ | ||
/// <inheritdoc /> | ||
public class ConfigurationSectionValueProvider : IConfigurationSectionValueProvider | ||
{ | ||
private readonly IConfigurationSectionKeySplitter _configurationSectionKeySplitter; | ||
|
||
public ConfigurationSectionValueProvider(IConfigurationSectionKeySplitter configurationSectionKeySplitter) | ||
{ | ||
_configurationSectionKeySplitter = configurationSectionKeySplitter; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public string GetValue(IConfiguration configuration, string key) | ||
{ | ||
var sections = //key.Split(new[] {"__"}, StringSplitOptions.None) | ||
_configurationSectionKeySplitter.Split(key) | ||
.Where(x => !string.IsNullOrEmpty(x)) | ||
.ToArray(); | ||
IConfigurationSection section = null; | ||
foreach (var sectionName in sections) | ||
{ | ||
section = section == null ? configuration.GetSection(sectionName) : section.GetSection(sectionName); | ||
} | ||
|
||
var value = section?.Value; | ||
return value; | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
Attest.Testing.Core/Configuration/EnvironmentVariableKeySplitter.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,15 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
// ReSharper disable once CheckNamespace | ||
namespace Attest.Testing.Configuration | ||
{ | ||
public sealed class EnvironmentVariableKeySplitter : IConfigurationSectionKeySplitter | ||
{ | ||
/// <inheritdoc /> | ||
public IEnumerable<string> Split(string key) | ||
{ | ||
return key.Split(new[] {"__"}, StringSplitOptions.None); | ||
} | ||
} | ||
} |
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