예제 #1
0
        public void AddDbContexts_WithVCAPs_AddsDbContexts()
        {
            // Arrange

            // Arrange
            IServiceCollection services = new ServiceCollection();

            Environment.SetEnvironmentVariable("VCAP_APPLICATION", TestHelpers.VCAP_APPLICATION);
            Environment.SetEnvironmentVariable("VCAP_SERVICES", MySqlTestHelpers.SingleServerVCAP);

            ConfigurationBuilder builder = new ConfigurationBuilder();

            builder.AddCloudFoundry();
            var config = builder.Build();

            // Act and Assert
            MySqlDbContextServiceCollectionExtensions.AddDbContext <GoodMySqlDbContext>(services, config);
            MySqlDbContextServiceCollectionExtensions.AddDbContext <Good2MySqlDbContext>(services, config);

            var built   = services.BuildServiceProvider();
            var service = built.GetService <GoodMySqlDbContext>();

            Assert.NotNull(service);

            var service2 = built.GetService <Good2MySqlDbContext>();

            Assert.NotNull(service2);
        }
        public void AddDbContext_WithServiceName_NoVCAPs_ThrowsConnectorException()
        {
            IServiceCollection services = new ServiceCollection();
            var config = new ConfigurationBuilder().Build();

            var ex = Assert.Throws <ConnectorException>(() => MySqlDbContextServiceCollectionExtensions.AddDbContext <GoodMySqlDbContext>(services, config, "foobar"));

            Assert.Contains("foobar", ex.Message);
        }
        public void AddDbContext_ThrowsIfServiceNameNull()
        {
            IServiceCollection services    = new ServiceCollection();
            IConfigurationRoot config      = null;
            string             serviceName = null;

            var ex = Assert.Throws <ArgumentNullException>(() => MySqlDbContextServiceCollectionExtensions.AddDbContext <GoodMySqlDbContext>(services, config, serviceName));

            Assert.Contains(nameof(serviceName), ex.Message);
        }
예제 #4
0
        public void AddDbContext_NoVCAPs_AddsDbContext()
        {
            // Arrange
            IServiceCollection services = new ServiceCollection();
            IConfigurationRoot config   = new ConfigurationBuilder().Build();

            // Act and Assert
            MySqlDbContextServiceCollectionExtensions.AddDbContext <GoodMySqlDbContext>(services, config);

            var service = services.BuildServiceProvider().GetService <GoodMySqlDbContext>();

            Assert.NotNull(service);
        }
        public void AddDbContext_ThrowsIfConfigurationNull()
        {
            IServiceCollection services = new ServiceCollection();
            IConfigurationRoot config   = null;

            var ex = Assert.Throws <ArgumentNullException>(() => MySqlDbContextServiceCollectionExtensions.AddDbContext <GoodMySqlDbContext>(services, config));

            Assert.Contains(nameof(config), ex.Message);

            var ex2 = Assert.Throws <ArgumentNullException>(() => MySqlDbContextServiceCollectionExtensions.AddDbContext <GoodMySqlDbContext>(services, config, "foobar"));

            Assert.Contains(nameof(config), ex2.Message);
        }
        public void AddDbContext_NoVCAPs_AddsDbContext()
        {
            IServiceCollection services = new ServiceCollection();
            var config = new ConfigurationBuilder().Build();

            MySqlDbContextServiceCollectionExtensions.AddDbContext <GoodMySqlDbContext>(services, config);

            var serviceProvider = services.BuildServiceProvider();
            var service         = serviceProvider.GetService <GoodMySqlDbContext>();
            var serviceHealth   = serviceProvider.GetService <IHealthContributor>();

            Assert.NotNull(service);
            Assert.NotNull(serviceHealth);
            Assert.IsAssignableFrom <RelationalDbHealthContributor>(serviceHealth);
        }
        public void AddDbContext_MultipleMySqlServices_ThrowsConnectorException()
        {
            IServiceCollection services = new ServiceCollection();

            Environment.SetEnvironmentVariable("VCAP_APPLICATION", TestHelpers.VCAP_APPLICATION);
            Environment.SetEnvironmentVariable("VCAP_SERVICES", MySqlTestHelpers.TwoServerVCAP);

            var builder = new ConfigurationBuilder();

            builder.AddCloudFoundry();
            var config = builder.Build();

            var ex = Assert.Throws <ConnectorException>(() => MySqlDbContextServiceCollectionExtensions.AddDbContext <GoodMySqlDbContext>(services, config));

            Assert.Contains("Multiple", ex.Message);
        }
        public void AddDbContexts_WithVCAPs_AddsDbContexts()
        {
            // Arrange
            var env1 = @"
{
      'limits': {
        'fds': 16384,
        'mem': 1024,
        'disk': 1024
      },
      'application_name': 'spring-cloud-broker',
      'application_uris': [
        'spring-cloud-broker.apps.testcloud.com'
      ],
      'name': 'spring-cloud-broker',
      'space_name': 'p-spring-cloud-services',
      'space_id': '65b73473-94cc-4640-b462-7ad52838b4ae',
      'uris': [
        'spring-cloud-broker.apps.testcloud.com'
      ],
      'users': null,
      'version': '07e112f7-2f71-4f5a-8a34-db51dbed30a3',
      'application_version': '07e112f7-2f71-4f5a-8a34-db51dbed30a3',
      'application_id': '798c2495-fe75-49b1-88da-b81197f2bf06'
    }
}";
            var env2 = @"
{
      'p-msql': [
        {
          'credentials': {
            'hostname': '192.168.0.90',
            'port': 3306,
            'name': 'cf_b4f8d2fa_a3ea_4e3a_a0e8_2cd040790355',
            'username': '******',
            'password': '******',
            'uri': 'mysql://*****:*****@192.168.0.90:3306/cf_b4f8d2fa_a3ea_4e3a_a0e8_2cd040790355?reconnect=true',
            'jdbcUrl': 'jdbc:mysql://192.168.0.90:3306/cf_b4f8d2fa_a3ea_4e3a_a0e8_2cd040790355?user=Dd6O1BPXUHdrmzbP&password=7E1LxXnlH2hhlPVt'
          },
          'syslog_drain_url': null,
          'label': 'p-mysql',
          'provider': null,
          'plan': '100mb-dev',
          'name': 'spring-cloud-broker-db',
          'tags': [
            'mysql',
            'relational'
          ]
        }
      ]
}
";
            // Arrange
            IServiceCollection services = new ServiceCollection();

            Environment.SetEnvironmentVariable("VCAP_APPLICATION", env1);
            Environment.SetEnvironmentVariable("VCAP_SERVICES", env2);

            ConfigurationBuilder builder = new ConfigurationBuilder();

            builder.AddCloudFoundry();
            var config = builder.Build();

            // Act and Assert
            MySqlDbContextServiceCollectionExtensions.AddDbContext <GoodDbContext>(services, config);
            MySqlDbContextServiceCollectionExtensions.AddDbContext <Good2DbContext>(services, config);

            var built   = services.BuildServiceProvider();
            var service = built.GetService <GoodDbContext>();

            Assert.NotNull(service);

            var service2 = built.GetService <Good2DbContext>();

            Assert.NotNull(service2);
        }