public void EntityFrameworkEndpointMiddleware_PathAndVerbMatching_ReturnsExpected()
        {
            var opts      = new DbMigrationsEndpointOptions();
            var efContext = new MockDbContext();
            var container = Substitute.For <IServiceProvider>();

            container.GetService(typeof(MockDbContext)).Returns(efContext);
            var helper = Substitute.For <DbMigrationsEndpoint.DbMigrationsEndpointHelper>();

            helper.GetPendingMigrations(Arg.Any <DbContext>()).Returns(new[] { "pending" });
            helper.GetAppliedMigrations(Arg.Any <DbContext>()).Returns(new[] { "applied" });
            var ep = new DbMigrationsEndpoint(opts, container, helper);

            var mgmt = new CloudFoundryManagementOptions()
            {
                Path = "/"
            };

            mgmt.EndpointOptions.Add(opts);
            var middle = new DbMigrationsEndpointMiddleware(null, ep, new List <IManagementOptions> {
                mgmt
            });

            middle.RequestVerbAndPathMatch("GET", "/dbmigrations").Should().BeTrue();
            middle.RequestVerbAndPathMatch("PUT", "/dbmigrations").Should().BeFalse();
            middle.RequestVerbAndPathMatch("GET", "/badpath").Should().BeFalse();
        }
        public void RoutesByPathAndVerb()
        {
            var options = new DbMigrationsEndpointOptions();

            Assert.True(options.ExactMatch);
            Assert.Equal("/actuator/dbmigrations", options.GetContextPath(new ActuatorManagementOptions()));
            Assert.Equal("/cloudfoundryapplication/dbmigrations", options.GetContextPath(new CloudFoundryManagementOptions()));
            Assert.Null(options.AllowedVerbs);
        }
        public void Invoke_NonContainerRegistered_ReturnsExpected()
        {
            var container = Substitute.For <IServiceProvider>();
            var helper    = Substitute.For <DbMigrationsEndpoint.DbMigrationsEndpointHelper>();

            helper.ScanRootAssembly.Returns(typeof(MockDbContext).Assembly);
            var options = new DbMigrationsEndpointOptions();
            var logger  = GetLogger <DbMigrationsEndpoint>();

            var sut    = new DbMigrationsEndpoint(options, container, helper, logger);
            var result = sut.Invoke();

            result.Should().BeEmpty();
        }
        public async void HandleEntityFrameworkRequestAsync_ReturnsExpected()
        {
            var opts = new DbMigrationsEndpointOptions();

            ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();

            configurationBuilder.AddInMemoryCollection(appSettings);
            var mgmtOptions = TestHelper.GetManagementOptions(opts);
            var efContext   = new MockDbContext();
            var container   = Substitute.For <IServiceProvider>();

            container.GetService(typeof(MockDbContext)).Returns(efContext);
            var helper = Substitute.For <DbMigrationsEndpoint.DbMigrationsEndpointHelper>();

            helper.ScanRootAssembly.Returns(typeof(MockDbContext).Assembly);
            helper.GetPendingMigrations(Arg.Any <DbContext>()).Returns(new[] { "pending" });
            helper.GetAppliedMigrations(Arg.Any <DbContext>()).Returns(new[] { "applied" });
            var ep = new DbMigrationsEndpoint(opts, container, helper);

            var middle = new DbMigrationsEndpointMiddleware(null, ep, mgmtOptions);

            var context = CreateRequest("GET", "/dbmigrations");
            await middle.HandleEntityFrameworkRequestAsync(context);

            context.Response.Body.Seek(0, SeekOrigin.Begin);
            var reader = new StreamReader(context.Response.Body, Encoding.UTF8);
            var json   = await reader.ReadToEndAsync();

            var expected = JToken.FromObject(
                new Dictionary <string, DbMigrationsDescriptor>()
            {
                {
                    nameof(MockDbContext), new DbMigrationsDescriptor()
                    {
                        AppliedMigrations = new List <string> {
                            "applied"
                        },
                        PendingMigrations = new List <string> {
                            "pending"
                        }
                    }
                }
            },
                GetSerializer());
            var actual = JObject.Parse(json);

            actual.Should().BeEquivalentTo(expected);
        }
Beispiel #5
0
        public async Task HandleEntityFrameworkRequestAsync_ReturnsExpected()
        {
            var opts = new DbMigrationsEndpointOptions();

            var configurationBuilder = new ConfigurationBuilder();

            configurationBuilder.AddInMemoryCollection(AppSettings);
            var mgmtOptions = new ActuatorManagementOptions();

            mgmtOptions.EndpointOptions.Add(opts);
            var container = new ServiceCollection();

            container.AddScoped <MockDbContext>();
            var helper = Substitute.For <DbMigrationsEndpoint.DbMigrationsEndpointHelper>();

            helper.ScanRootAssembly.Returns(typeof(MockDbContext).Assembly);
            helper.GetPendingMigrations(Arg.Any <DbContext>()).Returns(new[] { "pending" });
            helper.GetAppliedMigrations(Arg.Any <DbContext>()).Returns(new[] { "applied" });
            var ep = new DbMigrationsEndpoint(opts, container.BuildServiceProvider(), helper);

            var middle = new DbMigrationsEndpointMiddleware(null, ep, mgmtOptions);

            var context = CreateRequest("GET", "/dbmigrations");
            await middle.HandleEntityFrameworkRequestAsync(context);

            context.Response.Body.Seek(0, SeekOrigin.Begin);
            var reader = new StreamReader(context.Response.Body, Encoding.UTF8);
            var json   = await reader.ReadToEndAsync();

            var expected = Serialize(
                new Dictionary <string, DbMigrationsDescriptor>()
            {
                {
                    nameof(MockDbContext), new DbMigrationsDescriptor()
                    {
                        AppliedMigrations = new List <string> {
                            "applied"
                        },
                        PendingMigrations = new List <string> {
                            "pending"
                        }
                    }
                }
            });

            Assert.Equal(expected, json);
        }
        /// <summary>
        /// Adds the services used by the DB Migrations actuator
        /// </summary>
        /// <param name="services">Reference to the service collection</param>
        /// <param name="configuration">Reference to the configuration system</param>
        /// <returns>A reference to the service collection</returns>
        public static IServiceCollection AddDbMigrationsActuatorServices(this IServiceCollection services, IConfiguration configuration)
        {
            if (services == null)
            {
                throw new ArgumentNullException(nameof(services));
            }

            if (configuration == null)
            {
                throw new ArgumentNullException(nameof(configuration));
            }

            var options = new DbMigrationsEndpointOptions(configuration);

            services.TryAddSingleton <IDbMigrationsOptions>(options);
            services.TryAddEnumerable(ServiceDescriptor.Singleton(typeof(IEndpointOptions), options));
            services.TryAddSingleton <DbMigrationsEndpoint>();

            return(services);
        }
        public void Invoke_NonExistingDatabase_ReturnsExpected()
        {
            var container = new ServiceCollection();

            container.AddScoped <MockDbContext>();
            var helper = Substitute.For <DbMigrationsEndpoint.DbMigrationsEndpointHelper>();

            helper.ScanRootAssembly.Returns(typeof(MockDbContext).Assembly);
            helper.GetPendingMigrations(Arg.Any <object>()).Throws(new SomeDbException("database doesn't exist"));
            helper.GetMigrations(Arg.Any <object>()).Returns(new[] { "migration" });
            var options = new DbMigrationsEndpointOptions();
            var logger  = GetLogger <DbMigrationsEndpoint>();

            var sut    = new DbMigrationsEndpoint(options, container.BuildServiceProvider(), helper, logger);
            var result = sut.Invoke();

            var contextName = nameof(MockDbContext);

            result.Should().ContainKey(contextName);
            result[contextName].AppliedMigrations.Should().BeEmpty();
            result[contextName].PendingMigrations.Should().Equal("migration");
        }
        public void Invoke_WhenExistingDatabase_ReturnsExpected()
        {
            var context   = new MockDbContext();
            var container = Substitute.For <IServiceProvider>();

            container.GetService(typeof(MockDbContext)).Returns(context);
            var helper = Substitute.For <DbMigrationsEndpoint.DbMigrationsEndpointHelper>();

            helper.ScanRootAssembly.Returns(typeof(MockDbContext).Assembly);
            helper.GetPendingMigrations(Arg.Any <object>()).Returns(new[] { "pending" });
            helper.GetAppliedMigrations(Arg.Any <object>()).Returns(new[] { "applied" });
            var options = new DbMigrationsEndpointOptions();
            var logger  = GetLogger <DbMigrationsEndpoint>();

            var sut    = new DbMigrationsEndpoint(options, container, helper, logger);
            var result = sut.Invoke();

            var contextName = nameof(MockDbContext);

            result.Should().ContainKey(contextName);
            result[contextName].AppliedMigrations.Should().Equal("applied");
            result[contextName].PendingMigrations.Should().Equal("pending");
        }