public void Config_validation_exception_contains_missing_values_when_server_and_cache_config_is_invalid()
        {
            ConfigRoot configFromMockCache = MockHabitatServer.GetConfigRoot(TestComponentName);

            configFromMockCache.Data.Children[0].Value = "fromcache";
            configFromMockCache.Data.Children.Add(new ConfigNode {
                Name = "N4", Value = "V4"
            });
            CreateMockDurableCacheEntry(configFromMockCache);

            var testFactory = new ConfigProviderFactory(TestAssemblyName, _mockConfigServiceHttpClient, _mockFileSystem.Object);
            var validators  = new Dictionary <string, Func <string, bool> > {
                { "foo.N1", x => true }, { "foo.N2", x => true }, { "foo.N3", x => true }, { "foo.N4", x => true }
            };

            IConfigProvider configProvider = testFactory.Create(TestComponentName, validators);

            try
            {
                configProvider.GetAndValidateConfiguration();
                Assert.Fail("Invalid configuration passed validation");
            }
            catch (UnableToAccessConfigurationException e)
            {
                Assert.IsInstanceOfType(e.InnerException, typeof(ConfigValidationException), "Expected a ConfigValidationException to be thrown");
                ConfigValidationException configValidationEx = (ConfigValidationException)e.InnerException;
                Assert.IsTrue(Regex.IsMatch(configValidationEx.Message, "foo.N3, foo.N4"), "Missing invalid parameters from validation error message");
            }
        }
        public void Config_should_always_come_from_server_if_it_returns_valid_data()
        {
            ConfigRoot configFromMockCache = MockHabitatServer.GetConfigRoot(TestComponentName);

            configFromMockCache.Data.Children[0].Value = "fromcache";
            CreateMockDurableCacheEntry(configFromMockCache);

            var testFactory = new ConfigProviderFactory(TestAssemblyName, _mockConfigServiceHttpClient, _mockFileSystem.Object);

            IConfigProvider             configProvider = testFactory.Create(TestComponentName, new Dictionary <string, Func <string, bool> >());
            ConfigRoot                  config         = configProvider.GetAndValidateConfiguration();
            Dictionary <string, string> dictionary     = config.Data.ToDictionary();

            Assert.AreEqual("V1", dictionary[string.Format("{0}.N1", TestComponentName)]);
        }
        public void Cache_should_always_be_updated_when_valid_data_is_retrieved_from_server()
        {
            ConfigRoot originalConfigFromMockCache = MockHabitatServer.GetConfigRoot(TestComponentName);

            originalConfigFromMockCache.Data.Children[0].Value = "fromcache";
            CreateMockDurableCacheEntry(originalConfigFromMockCache);

            var testFactory = new ConfigProviderFactory(TestAssemblyName, _mockConfigServiceHttpClient, _mockFileSystem.Object);

            IConfigProvider configProvider     = testFactory.Create(TestComponentName, new Dictionary <string, Func <string, bool> >());
            ConfigRoot      configFromProvider = configProvider.GetAndValidateConfiguration();

            ConfigRoot updatedConfigFromMockCache = ReadMockDurableCacheEntry();

            Assert.IsFalse(_objectComparer.Compare(originalConfigFromMockCache, updatedConfigFromMockCache).AreEqual);
            Assert.IsTrue(_objectComparer.Compare(configFromProvider, updatedConfigFromMockCache).AreEqual);
        }
        public void Config_data_in_cache_should_always_be_validated()
        {
            ConfigRoot configFromMockCache = MockHabitatServer.GetConfigRoot(TestComponentName);

            configFromMockCache.Data.Children[0].Value = "fromcache";
            CreateMockDurableCacheEntry(configFromMockCache);

            // In this case, we have to bypass validation of the service data because the service is offline
            _mockConfigServiceHttpClient = HttpClientTestHelper.CreateClientSimulatingABadAddress();
            var testFactory = new ConfigProviderFactory(TestAssemblyName, _mockConfigServiceHttpClient, _mockFileSystem.Object);

            var validators = new Dictionary <string, Func <string, bool> > {
                { "N1.N1", x => false }, { "N1.N2", x => false }
            };
            IConfigProvider configProvider = testFactory.Create(TestComponentName, validators);

            configProvider.GetAndValidateConfiguration();
        }
Exemple #5
0
        public void Test_provider_behavior_when_config_service_returns_valid_data()
        {
            ConfigRoot testConfig   = MockHabitatServer.GetConfigRoot(ResourceName);
            DateTime   expectedDate = testConfig.LastModified;

            HttpClient mockClient = HttpClientTestHelper.CreateStandardFakeClient(new MockHabitatServer());
            var        provider   = new ConfigServiceProvider(ResourceName, mockClient);

            ConfigServiceResponse response = provider.GetConfig();

            Assert.IsNotNull(response);
            Assert.IsNotNull(response.Config);
            Assert.AreEqual(ResourceName, response.Config.ComponentName);
            Assert.AreEqual(expectedDate, response.Config.LastModified);
            Assert.IsNotNull(response.Config.Data);
            Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
            Assert.IsNull(response.Exception);
            Assert.IsNotNull(response.Config);
            Assert.IsTrue(_objectComparer.Compare(testConfig, response.Config).AreEqual);
        }