Exemplo n.º 1
0
        public Task <ConfigurationSourceResult <TValue> > GetAppSettingAsync <TValue>(string key, CancellationToken cancellationToken = default(CancellationToken))
        {
            object temp;
            var    result = _config.TryGetValue(key, out temp);

            var sourceResult = result ? ConfigurationSourceResult <TValue> .SuccessResult((TValue)temp)
                            : ConfigurationSourceResult <TValue> .FailedResult();

            return(Task.FromResult(sourceResult));
        }
        protected override IConfigurationSource CreateConfigurationSource()
        {
            var innerSource = new Mock <IConfigurationSource>();

            innerSource.Setup(x => x.GetAppSettingAsync <TValue>(It.IsAny <string>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(ConfigurationSourceResult <TValue> .FailedResult());

            var source = new CachingConfigurationSourceDecorator(innerSource.Object, new CacheKeyCreator(Guid.NewGuid().ToString()), MemoryCache.Default, () => new CacheItemPolicy());

            return(source);
        }
Exemplo n.º 3
0
        protected override IConfigurationSource GivenConfigurationSourceWithExistingRKey(string key, TValue expectedValue)
        {
            var innerSource       = new Mock <IConfigurationSource>();
            var cancellationToken = new CancellationToken();

            innerSource.Setup(x => x.GetAppSettingAsync <TValue>(key, cancellationToken))
            .ReturnsAsync(ConfigurationSourceResult <TValue> .SuccessResult(expectedValue));

            var source = new CachingConfigurationSourceDecorator(innerSource.Object, new CacheKeyCreator(Guid.NewGuid().ToString()), MemoryCache.Default, () => new CacheItemPolicy());

            return(source);
        }
Exemplo n.º 4
0
        public async Task <ConfigurationSourceResult <TValue> > GetAppSettingAsync <TValue>(string key, CancellationToken cancellationToken = default(CancellationToken))
        {
            var document = await _collection.Find(new BsonDocument("_id", key))
                           .SingleOrDefaultAsync(cancellationToken)
                           .ConfigureAwait(false);

            if (document == null)
            {
                return(ConfigurationSourceResult <TValue> .FailedResult());
            }

            return
                (ConfigurationSourceResult <TValue> .SuccessResult(
                     (TValue)document.Value));
        }
        protected override IConfigurationSource CreateConfigurationSource()
        {
            var objectCache = new Mock <ObjectCache>();

            objectCache.Setup(x => x.Get(It.IsAny <string>(), It.IsAny <string>()))
            .Returns(ConfigurationSourceResult <TValue> .FailedResult());

            var innerSource = new Mock <IConfigurationSource>();

            innerSource.Setup(x => x.GetAppSettingAsync <TValue>(It.IsAny <string>(), It.IsAny <CancellationToken>()))
            .Throws(new Exception("Should never get here"));

            var source = new CachingConfigurationSourceDecorator(innerSource.Object, new CacheKeyCreator(Guid.NewGuid().ToString()), objectCache.Object, () => new CacheItemPolicy());

            return(source);
        }
Exemplo n.º 6
0
        public Task <ConfigurationSourceResult <TValue> > GetAppSettingAsync <TValue>(string key, CancellationToken cancellationToken = default(CancellationToken))
        {
            var configValue = System.Configuration.ConfigurationManager.AppSettings[key];

            if (configValue == null)
            {
                return(Task.FromResult(ConfigurationSourceResult <TValue> .FailedResult()));
            }
            else
            {
                var valueType = typeof(TValue);
                valueType = Nullable.GetUnderlyingType(valueType) ?? valueType;

                var value = (TValue)Convert.ChangeType(configValue, valueType);
                return(Task.FromResult(ConfigurationSourceResult <TValue> .SuccessResult(value)));
            }
        }
Exemplo n.º 7
0
        public Task <ConfigurationSourceResult <TValue> > GetAppSettingAsync <TValue>(string key, CancellationToken cancellationToken = default(CancellationToken))
        {
            var environmentValue = Environment.GetEnvironmentVariable(key);

            if (environmentValue == null)
            {
                return(Task.FromResult(ConfigurationSourceResult <TValue> .FailedResult()));
            }
            else
            {
                var valueType = typeof(TValue);
                valueType = Nullable.GetUnderlyingType(valueType) ?? valueType;

                var value = (TValue)Convert.ChangeType(environmentValue, valueType);

                return(Task.FromResult(ConfigurationSourceResult <TValue> .SuccessResult(value)));
            }
        }
Exemplo n.º 8
0
        public void GivenAnConfigurationWithACustomKeyFormatter()
        {
            _key          = _fixture.Create <string>();
            _formattedKey = _fixture.Create <string>();
            var value        = _fixture.Create <TValue>();
            var keyFormatter = new Mock <IKeyFormatter>();

            keyFormatter.Setup(x => x.FormatKey(_key))
            .Returns(_formattedKey);

            _source = new Mock <IConfigurationSource>();
            _source.Setup(x => x.GetAppSettingAsync <TValue>(_formattedKey, default(CancellationToken)))
            .ReturnsAsync(ConfigurationSourceResult <TValue> .SuccessResult(value));

            _configuration = new Configuration();
            _configuration.AddConfigurationSource(_source.Object);
            _configuration.SetKeyFormatter(keyFormatter.Object);
        }
Exemplo n.º 9
0
        public async Task WhenGettingValue_ThenConfigurationSourceIsCalledWithNewKeyAndCorrectValueReturned()
        {
            var key    = _fixture.Create("key");
            var newKey = _fixture.Create("newKey");

            _keyFormatter.Setup(x => x.FormatKey(key))
            .Returns(newKey);

            var cancellationToken = new CancellationToken();
            var value             = _fixture.Create("value");

            _configurationSource.Setup(x => x.GetAppSettingAsync <string>(newKey, cancellationToken))
            .ReturnsAsync(ConfigurationSourceResult <string> .SuccessResult(value));

            var actual = await _decorator.GetAppSettingAsync <string>(key, cancellationToken);

            Assert.IsTrue(actual.KeyExists);
            Assert.AreEqual(value, actual.Value);
        }
Exemplo n.º 10
0
        public async Task <ConfigurationSourceResult <TValue> > GetAppSettingAsync <TValue>(string key, CancellationToken cancellationToken = default(CancellationToken))
        {
            var credstashValue = await _credstash.GetSecretAsync(key)
                                 .ConfigureAwait(false);

            if (credstashValue.HasNoValue)
            {
                return(ConfigurationSourceResult <TValue> .FailedResult());
            }
            else
            {
                var valueType = typeof(TValue);
                valueType = Nullable.GetUnderlyingType(valueType) ?? valueType;

                var value = (TValue)Convert.ChangeType(credstashValue.Value, valueType);

                return(ConfigurationSourceResult <TValue> .SuccessResult(value));
            }
        }
 public void WhenTryingToGetTheAppSettings()
 {
     _result = _source.GetAppSettingAsync <TValue>(_key).Result;
 }
Exemplo n.º 12
0
        public void WhenTryingToGetTheAppSettings()
        {
            var key = new Fixture().Create <string>();

            _result = _source.GetAppSettingAsync <TValue>(key).Result;
        }
 private CacheItem CreateCacheItem <TValue>(string key, ConfigurationSourceResult <TValue> value)
 {
     return(new CacheItem(_cacheKeyCreator.CreateCacheKey(key), value));
 }