Example #1
0
        public void ConfigureServices_GetLoggers()
        {
            IHostingEnvironment hostenv = new MockHostingEnvironment();

            Mock <NciStartupBase> mockStartup = new Mock <NciStartupBase>(hostenv)
            {
                CallBase = true
            };

            mockStartup.Protected().Setup("AddAdditionalConfigurationMappings", Moq.Protected.ItExpr.IsAny <IServiceCollection>());

            NciStartupBase startup = mockStartup.Object;

            IServiceCollection svcColl         = new ServiceCollection();
            IServiceProvider   serviceProvider = startup.ConfigureServices(svcColl);

            Object svc;

            svc = serviceProvider.GetService(typeof(ILogger <DefaultController>));
            Assert.NotNull(svc);
            Assert.IsAssignableFrom <Logger <DefaultController> >(svc);

            svc = serviceProvider.GetService(typeof(ILogger <ElasticClient>));
            Assert.NotNull(svc);
            Assert.IsAssignableFrom <Logger <ElasticClient> >(svc);
        }
    public void Constructor()
    {
      IHostingEnvironment hostenv = new MockHostingEnvironment();

      Mock<NciStartupBase> startup = new Mock<NciStartupBase>(hostenv){ CallBase = true };

      NciStartupBase mock = startup.Object;

      Assert.NotNull(mock.Configuration);
    }
        public void Constructor()
        {
            Mock <IConfiguration> mockConfiguration = new Mock <IConfiguration>();
            IConfiguration        config            = mockConfiguration.Object;

            Mock <NciStartupBase> mockStartup = new Mock <NciStartupBase>(config)
            {
                CallBase = true
            };

            NciStartupBase startup = mockStartup.Object;

            Assert.NotNull(startup.Configuration);
        }
        public void ConfigureServices_GetLoggers()
        {
            string settings = @"
        {
          ""Elasticsearch"": {
            ""Servers"": ""http://localhost:9200"",
            ""Userid"": """",
            ""Password"":  """",
            ""MaximumRetries"": 5
          },
          ""Logging"": {
            ""LogLevel"": {
              ""Default"": ""Warning""
            }
          }
        }";

            ConfigurationBuilder builder = new ConfigurationBuilder();

            builder.AddJsonStream(new MemoryStream(Encoding.UTF8.GetBytes(settings)));
            IConfiguration config = builder.Build();

            Mock <NciStartupBase> mockStartup = new Mock <NciStartupBase>(config)
            {
                CallBase = true
            };

            mockStartup.Protected().Setup("AddAdditionalConfigurationMappings", Moq.Protected.ItExpr.IsAny <IServiceCollection>());

            NciStartupBase startup = mockStartup.Object;

            IServiceCollection svcColl = new ServiceCollection();

            startup.ConfigureServices(svcColl);

            IServiceProvider serviceProvider = svcColl.BuildServiceProvider();

            Object svc;

            svc = serviceProvider.GetService(typeof(ILogger <ElasticClient>));
            Assert.NotNull(svc);
            Assert.IsAssignableFrom <Logger <ElasticClient> >(svc);
        }
        public void ConfigureServices_SubclassSetupServices()
        {
            string settings = @"
        {
          ""Elasticsearch"": {
            ""Servers"": ""http://localhost:9200"",
            ""Userid"": """",
            ""Password"":  """",
            ""MaximumRetries"": 5
          },
          ""Logging"": {
            ""LogLevel"": {
              ""Default"": ""Warning""
            }
          }
        }";

            ConfigurationBuilder builder = new ConfigurationBuilder();

            builder.AddJsonStream(new MemoryStream(Encoding.UTF8.GetBytes(settings)));
            IConfiguration config = builder.Build();

            Mock <NciStartupBase> mockStartup = new Mock <NciStartupBase>(config)
            {
                CallBase = true
            };

            mockStartup.Protected().Setup("AddAdditionalConfigurationMappings", Moq.Protected.ItExpr.IsAny <IServiceCollection>());
            mockStartup.Protected().Setup("AddAppServices", Moq.Protected.ItExpr.IsAny <IServiceCollection>());

            NciStartupBase startup = mockStartup.Object;

            IServiceCollection svcColl = new ServiceCollection();

            startup.ConfigureServices(svcColl);

            IServiceProvider serviceProvider = svcColl.BuildServiceProvider();

            // Verify the subclass had a chance to add services.
            mockStartup.Protected().Verify("AddAdditionalConfigurationMappings", Times.Once(), Moq.Protected.ItExpr.IsAny <IServiceCollection>());
            mockStartup.Protected().Verify("AddAppServices", Times.Once(), Moq.Protected.ItExpr.IsAny <IServiceCollection>());
        }
Example #6
0
        public void ConfigureServices_ElasticsearchGoodConfiguration()
        {
            string appsettings = @"
{
    ""Elasticsearch"": {
        ""Servers"": ""http://localhost:9200"",
        ""Userid"": """",
        ""Password"": """",
        ""MaximumRetries"": 5
    }
  }
      ";

            // Create an appsettings.json in a location where only this test will see it.
            string        configLocation = Path.Join(Fixture.TestLocation, nameof(ConfigureServices_ElasticsearchGoodConfiguration));
            DirectoryInfo di             = Directory.CreateDirectory(configLocation);

            File.WriteAllText(Path.Join(configLocation, "appsettings.json"), appsettings);

            // Customize the hosting environment for this test.
            IHostingEnvironment hostenv = new MockHostingEnvironment();

            hostenv.ContentRootPath = configLocation;

            Mock <NciStartupBase> mockStartup = new Mock <NciStartupBase>(hostenv)
            {
                CallBase = true
            };

            NciStartupBase startup = mockStartup.Object;

            IServiceCollection svcColl         = new ServiceCollection();
            IServiceProvider   serviceProvider = startup.ConfigureServices(svcColl);

            Assert.NotNull(serviceProvider);

            Object svc = serviceProvider.GetService(typeof(IElasticClient));

            Assert.NotNull(svc);
            Assert.IsAssignableFrom <IElasticClient>(svc);
        }
Example #7
0
        public void ConfigureServices_SubclassSetupServices()
        {
            IHostingEnvironment hostenv = new MockHostingEnvironment();

            Mock <NciStartupBase> mockStartup = new Mock <NciStartupBase>(hostenv)
            {
                CallBase = true
            };

            mockStartup.Protected().Setup("AddAdditionalConfigurationMappings", Moq.Protected.ItExpr.IsAny <IServiceCollection>());
            mockStartup.Protected().Setup("AddAppServices", Moq.Protected.ItExpr.IsAny <IServiceCollection>());

            NciStartupBase startup = mockStartup.Object;

            IServiceCollection svcColl         = new ServiceCollection();
            IServiceProvider   serviceProvider = startup.ConfigureServices(svcColl);

            // Verify the subclass had a chance to add services.
            mockStartup.Protected().Verify("AddAdditionalConfigurationMappings", Times.Once(), Moq.Protected.ItExpr.IsAny <IServiceCollection>());
            mockStartup.Protected().Verify("AddAppServices", Times.Once(), Moq.Protected.ItExpr.IsAny <IServiceCollection>());
        }
Example #8
0
        public void ConfigureServices_ElasticsearchBadConfiguration()
        {
            IHostingEnvironment hostenv = new MockHostingEnvironment();

            Mock <NciStartupBase> mockStartup = new Mock <NciStartupBase>(hostenv)
            {
                CallBase = true
            };

            NciStartupBase startup = mockStartup.Object;

            IServiceCollection svcColl         = new ServiceCollection();
            IServiceProvider   serviceProvider = startup.ConfigureServices(svcColl);

            Assert.NotNull(serviceProvider);

            Exception ex = Assert.Throws <APIInternalException>(
                () => serviceProvider.GetService(typeof(IElasticClient))
                );

            Assert.Equal("No servers configured", ex.Message);
        }
        public void ConfigureServices_ElasticsearchBadConfiguration()
        {
            string appsettings = @"
      {
          ""Elasticsearch"": {
              ""Servers"": """",
              ""Userid"": """",
              ""Password"": """",
              ""MaximumRetries"": 5
          }
        }
      ";

            ConfigurationBuilder builder = new ConfigurationBuilder();

            builder.AddJsonStream(new MemoryStream(Encoding.UTF8.GetBytes(appsettings)));
            IConfiguration config = builder.Build();

            Mock <NciStartupBase> mockStartup = new Mock <NciStartupBase>(config)
            {
                CallBase = true
            };

            NciStartupBase startup = mockStartup.Object;

            IServiceCollection svcColl = new ServiceCollection();

            startup.ConfigureServices(svcColl);

            IServiceProvider serviceProvider = svcColl.BuildServiceProvider();

            Assert.NotNull(serviceProvider);

            Exception ex = Assert.Throws <APIInternalException>(
                () => serviceProvider.GetService(typeof(IElasticClient))
                );

            Assert.Equal("No servers configured", ex.Message);
        }
        public void ConfigureServices_ElasticsearchGoodConfiguration()
        {
            string appsettings = @"
      {
          ""Elasticsearch"": {
              ""Servers"": ""http://localhost:9200"",
              ""Userid"": """",
              ""Password"": """",
              ""MaximumRetries"": 5
          }
        }
      ";

            ConfigurationBuilder builder = new ConfigurationBuilder();

            builder.AddJsonStream(new MemoryStream(Encoding.UTF8.GetBytes(appsettings)));
            IConfiguration config = builder.Build();

            Mock <NciStartupBase> mockStartup = new Mock <NciStartupBase>(config)
            {
                CallBase = true
            };

            NciStartupBase startup = mockStartup.Object;

            IServiceCollection svcColl = new ServiceCollection();

            startup.ConfigureServices(svcColl);

            IServiceProvider serviceProvider = svcColl.BuildServiceProvider();

            Assert.NotNull(serviceProvider);

            Object svc = serviceProvider.GetService(typeof(IElasticClient));

            Assert.NotNull(svc);
            Assert.IsAssignableFrom <IElasticClient>(svc);
        }