/// <summary> /// Builds a factory that knows how to create a Config Provider that acts like the real thing, but doesn't use real caches or service calls. /// </summary> /// <param name="applicationConfig">The canned application configuration that the provider should use</param> /// <param name="environmentConfig">The canned environment configuration that the provider should use</param> /// <returns>A mock factory</returns> private Mock <IConfigProviderFactory> CreateFactory(ConfigRoot applicationConfig, ConfigRoot environmentConfig) { var mockConfigServiceProviderForApplication = new Mock <IConfigServiceProvider>(MockBehavior.Strict); var cannedApplicationResponse = new ConfigServiceResponse { Config = applicationConfig, StatusCode = HttpStatusCode.OK }; mockConfigServiceProviderForApplication.Setup(x => x.GetConfig()).Returns(cannedApplicationResponse); var mockApplicationConfig = new Mock <IJsonEntity <ConfigRoot> >(MockBehavior.Strict); mockApplicationConfig.SetupProperty(x => x.Contents); mockApplicationConfig.SetupProperty(x => x.JsonData); mockApplicationConfig.Object.Contents = applicationConfig; var mockConfigServiceProviderForEnvironment = new Mock <IConfigServiceProvider>(MockBehavior.Strict); var cannedEnvironmentResponse = new ConfigServiceResponse { Config = environmentConfig, StatusCode = HttpStatusCode.OK }; mockConfigServiceProviderForEnvironment.Setup(x => x.GetConfig()).Returns(cannedEnvironmentResponse); var mockEnvironmentConfig = new Mock <IJsonEntity <ConfigRoot> >(MockBehavior.Strict); mockEnvironmentConfig.SetupProperty(x => x.Contents); mockEnvironmentConfig.SetupProperty(x => x.JsonData); mockEnvironmentConfig.Object.Contents = environmentConfig; var mockFactory = new Mock <IConfigProviderFactory>(MockBehavior.Strict); var cacheProvider = new Mock <IRepository <IJsonEntity <ConfigRoot> > >(MockBehavior.Strict); cacheProvider.SetupAllProperties(); cacheProvider.SetupGet(x => x.Entities).Returns(new[] { mockApplicationConfig.Object, mockEnvironmentConfig.Object }.AsQueryable()); cacheProvider.Setup(x => x.Save()); cacheProvider.Setup(x => x.Update(It.IsAny <IJsonEntity <ConfigRoot> >())); mockFactory.Setup( x => x.Create(_applicationConfigProvider.ApplicationComponentName, It.IsAny <Dictionary <string, Func <string, bool> > >())) .Returns <string, Dictionary <string, Func <string, bool> > >((name, validators) => { return(new ConfigProvider(name, validators, mockConfigServiceProviderForApplication.Object, cacheProvider.Object)); }); mockFactory.Setup( x => x.Create("Environment", It.IsAny <Dictionary <string, Func <string, bool> > >())) .Returns <string, Dictionary <string, Func <string, bool> > >((name, validators) => { return(new ConfigProvider(name, validators, mockConfigServiceProviderForEnvironment.Object, cacheProvider.Object)); }); return(mockFactory); }
public void Test_provider_behavior_when_config_service_returns_invalid_data() { var provider = new ConfigServiceProvider(ResourceUrlTemplate, HttpClientTestHelper.CreateClientThatAlwaysReturnsGibberish()); ConfigServiceResponse response = provider.GetConfig(); Assert.IsNotNull(response); Assert.IsNotNull(response.Config); Assert.AreEqual(ResourceUrlTemplate, response.Config.ComponentName); Assert.AreEqual(default(DateTime), response.Config.LastModified); Assert.IsNull(response.Config.Data); Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); Assert.IsNotNull(response.Exception); Assert.AreEqual(typeof(UnableToAccessConfigurationException), response.Exception.GetType()); Assert.AreEqual(typeof(AggregateException), response.Exception.InnerException.GetType()); }
public void Test_provider_behavior_when_config_service_address_does_not_resolve() { var provider = new ConfigServiceProvider(ResourceUrlTemplate, HttpClientTestHelper.CreateClientSimulatingABadAddress()); ConfigServiceResponse response = provider.GetConfig(); Assert.IsNotNull(response); Assert.IsNotNull(response.Config); Assert.AreEqual(ResourceUrlTemplate, response.Config.ComponentName); Assert.AreEqual(default(DateTime), response.Config.LastModified); Assert.IsNull(response.Config.Data); Assert.AreEqual(default(HttpStatusCode), response.StatusCode); Assert.IsNotNull(response.Exception); Assert.AreEqual(typeof(UnableToAccessConfigurationException), response.Exception.GetType()); Assert.AreEqual(typeof(AggregateException), response.Exception.InnerException.GetType()); }
public void Test_provider_behavior_when_config_service_returns_404() { HttpClient mockClient = HttpClientTestHelper.CreateStandardFakeClient(new MockHabitatServer()); var provider = new ConfigServiceProvider("foo2", mockClient); ConfigServiceResponse response = provider.GetConfig(); Assert.IsNotNull(response); Assert.IsNotNull(response.Config); Assert.AreEqual("foo2", response.Config.ComponentName); Assert.AreEqual(default(DateTime), response.Config.LastModified); Assert.IsNull(response.Config.Data); Assert.AreEqual(HttpStatusCode.NotFound, response.StatusCode); Assert.IsNotNull(response.Exception); Assert.AreEqual(typeof(UnableToAccessConfigurationException), response.Exception.GetType()); Assert.IsNull(response.Exception.InnerException); }
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); }
/// <summary> /// Builds a factory that knows how to create a Config Provider that acts like the real thing, but doesn't use real caches or service calls. /// </summary> /// <param name="applicationConfig">The canned application configuration that the provider should use</param> /// <param name="environmentConfig">The canned environment configuration that the provider should use</param> /// <returns>A mock factory</returns> private Mock<IConfigProviderFactory> CreateFactory(ConfigRoot applicationConfig, ConfigRoot environmentConfig) { var mockConfigServiceProviderForApplication = new Mock<IConfigServiceProvider>(MockBehavior.Strict); var cannedApplicationResponse = new ConfigServiceResponse { Config = applicationConfig, StatusCode = HttpStatusCode.OK }; mockConfigServiceProviderForApplication.Setup(x => x.GetConfig()).Returns(cannedApplicationResponse); var mockApplicationConfig = new Mock<IJsonEntity<ConfigRoot>>(MockBehavior.Strict); mockApplicationConfig.SetupProperty(x => x.Contents); mockApplicationConfig.SetupProperty(x => x.JsonData); mockApplicationConfig.Object.Contents = applicationConfig; var mockConfigServiceProviderForEnvironment = new Mock<IConfigServiceProvider>(MockBehavior.Strict); var cannedEnvironmentResponse = new ConfigServiceResponse { Config = environmentConfig, StatusCode = HttpStatusCode.OK }; mockConfigServiceProviderForEnvironment.Setup(x => x.GetConfig()).Returns(cannedEnvironmentResponse); var mockEnvironmentConfig = new Mock<IJsonEntity<ConfigRoot>>(MockBehavior.Strict); mockEnvironmentConfig.SetupProperty(x => x.Contents); mockEnvironmentConfig.SetupProperty(x => x.JsonData); mockEnvironmentConfig.Object.Contents = environmentConfig; var mockFactory = new Mock<IConfigProviderFactory>(MockBehavior.Strict); var cacheProvider = new Mock<IRepository<IJsonEntity<ConfigRoot>>>(MockBehavior.Strict); cacheProvider.SetupAllProperties(); cacheProvider.SetupGet(x => x.Entities).Returns(new[] {mockApplicationConfig.Object, mockEnvironmentConfig.Object}.AsQueryable()); cacheProvider.Setup(x => x.Save()); cacheProvider.Setup(x => x.Update(It.IsAny<IJsonEntity<ConfigRoot>>())); mockFactory.Setup( x => x.Create(_applicationConfigProvider.ApplicationComponentName, It.IsAny<Dictionary<string, Func<string, bool>>>())) .Returns<string, Dictionary<string, Func<string, bool>>>((name, validators) => { return new ConfigProvider(name, validators, mockConfigServiceProviderForApplication.Object, cacheProvider.Object); }); mockFactory.Setup( x => x.Create("Environment", It.IsAny<Dictionary<string, Func<string, bool>>>())) .Returns<string, Dictionary<string, Func<string, bool>>>((name, validators) => { return new ConfigProvider(name, validators, mockConfigServiceProviderForEnvironment.Object, cacheProvider.Object); }); return mockFactory; }