public async Task CanAddAnICommandMiddleware()
        {
            var provider = new ServiceCollection()
                           .BuildServiceProvider(true);

            var app = new CommandApplicationBuilder(new Dictionary <string, object>(), provider);

            app.UseMiddleware <TestMiddleware>();

            var cmdDelegate = app.Build();

            var context = new CommandContext()
            {
                Result              = new Mock <IResult>().Object,
                Command             = new Mock <ICommand>().Object,
                ServiceScopeFactory = provider.GetRequiredService <IServiceScopeFactory>(),
            };

            await cmdDelegate(context);

            var item = context.Items[TestMiddleware.ItemKey] as string;

            Assert.False(string.IsNullOrWhiteSpace(item));
            Assert.Equal(TestMiddleware.ItemValue, item);
        }
        private async Task ExecuteMiddlewareAsync(CommandContext context, OnLog onLog = null, bool addFilter = false)
        {
            var services = new ServiceCollection()
                           .AddLogging(logging =>
            {
                if (onLog.HasContent())
                {
                    logging.AddProvider(new TestLoggerProvider(onLog));
                }
            })
                           .AddSingleton <IFilterProvider, FilterProvider>();

            if (addFilter)
            {
                services.AddFilterFactory <ExceptionFilterFactory, ICommandExceptionFilter, CommandExceptionDelegate>();
                services.AddSingleton <SingletonDependency>();
                services.AddOptions <MariCommandsOptions>();
                services.Configure <MariCommandsOptions>(options =>
                {
                    options.Filters.Add(typeof(TestExceptionFilter));
                });
            }

            var provider = services.BuildServiceProvider(true);

            context.ServiceScopeFactory = provider.GetRequiredService <IServiceScopeFactory>();

            var app = new CommandApplicationBuilder(new Dictionary <string, object>(), provider);

            app.UseDefaultExceptionMiddleware();

            var commandDelegate = app.Build();

            await commandDelegate(context);
        }
        public async Task CanExecuteBuildedApplication()
        {
            var services = new ServiceCollection();

            services.AddLogging();
            services.AddOptions <MariCommandsOptions>();
            services.AddSingleton <IModuleCache, ModuleCache>();


            var provider = services.BuildServiceProvider(true);


            var app = new CommandApplicationBuilder(new Dictionary <string, object>(), provider);

            app.UseDefaultMiddlewares();

            var cmdDelegate = app.Build();

            var context = new CommandContext()
            {
                RawArgs             = "someRandomText",
                ServiceScopeFactory = provider.GetRequiredService <IServiceScopeFactory>(),
            };

            await cmdDelegate(context);

            Assert.NotNull(context.Result);
            Assert.IsType <CommandNotFoundResult>(context.Result);
        }
        public async Task MiddlewareCanShortCircuit()
        {
            var provider = new ServiceCollection()
                           .BuildServiceProvider(true);

            var app = new CommandApplicationBuilder(new Dictionary <string, object>(), provider);

            app.Use((next) =>
            {
                return(context =>
                {
                    return Task.CompletedTask;
                });
            });

            var cmdDelegate = app.Build();

            var context = new CommandContext()
            {
                ServiceScopeFactory = provider.GetRequiredService <IServiceScopeFactory>(),
            };

            await cmdDelegate(context);

            Assert.Null(context.Result);
            Assert.Null(context.Command);
        }
        public void CanBuildApplication()
        {
            var provider = new ServiceCollection()
                           .BuildServiceProvider(true);

            var app = new CommandApplicationBuilder(new Dictionary <string, object>(), provider);

            app.UseDefaultMiddlewares();

            var cmdDelegate = app.Build();
        }
        public async Task NotThrowExceptionForCommandAndResult()
        {
            var provider = new ServiceCollection()
                           .BuildServiceProvider(true);

            var app = new CommandApplicationBuilder(new Dictionary <string, object>(), provider);

            var cmdDelegate = app.Build();

            var context = new CommandContext()
            {
                Result              = new Mock <IResult>().Object,
                Command             = new Mock <ICommand>().Object,
                ServiceScopeFactory = provider.GetRequiredService <IServiceScopeFactory>(),
            };

            await cmdDelegate(context);
        }
        public async Task ThrowsExceptionForNoResultAndNoCommand()
        {
            var provider = new ServiceCollection()
                           .BuildServiceProvider(true);

            var app = new CommandApplicationBuilder(new Dictionary <string, object>(), provider);

            var cmdDelegate = app.Build();

            var context = new CommandContext()
            {
                ServiceScopeFactory = provider.GetRequiredService <IServiceScopeFactory>(),
            };

            await Assert.ThrowsAsync <InvalidOperationException>(() =>
            {
                return(cmdDelegate(context));
            });
        }
Exemple #8
0
        public async Task AlwaysInitializedWhenRunContext()
        {
            var services = new ServiceCollection();

            services.AddScoped <ICommandContextAccessor, ScopedCommandContextAccessor>();
            services.AddScoped <TestDependency>();

            using var provider = services.BuildServiceProvider(true);

            var scopeFactory = provider.GetRequiredService <IServiceScopeFactory>();

            using var scope = scopeFactory.CreateScope();

            var context = new CommandContext();

            context.ServiceScopeFactory = scopeFactory;

            var app = new CommandApplicationBuilder(new Dictionary <string, object>(), provider);

            app.Use((next) =>
            {
                return((context) =>
                {
                    return Task.CompletedTask;
                });
            });

            var commandDelegate = app.Build();

            await commandDelegate(context);

            var commandContextAccessor = context.CommandServices.GetRequiredService <ICommandContextAccessor>();
            var testDependency         = context.CommandServices.GetRequiredService <TestDependency>();

            Assert.NotNull(commandContextAccessor);
            Assert.NotNull(commandContextAccessor.CommandContext);
            Assert.NotNull(testDependency);
            Assert.NotNull(testDependency.CommandContext);
            Assert.Equal(context, commandContextAccessor.CommandContext);
            Assert.Equal(context, testDependency.CommandContext);
        }
        public async Task MiddlewaresWillBeBuildedInCorrectOrder()
        {
            var provider = new ServiceCollection()
                           .BuildServiceProvider(true);

            var app = new CommandApplicationBuilder(new Dictionary <string, object>(), provider);

            var logKey = "LOG";

            var log1 = "Start middleware 1";
            var log2 = "Finish middleware 1";
            var log3 = "Start middleware 2";
            var log4 = "Finish middleware 2";
            var log5 = "Start middleware 3";
            var log6 = "Finish middleware 3";

            app.Use((next) =>
            {
                return(async context =>
                {
                    var logs = context.Items[logKey] as List <string>;

                    logs.Add(log1);

                    await next(context);

                    logs.Add(log2);
                });
            });

            app.Use((next) =>
            {
                return(async context =>
                {
                    var logs = context.Items[logKey] as List <string>;

                    logs.Add(log3);

                    await next(context);

                    logs.Add(log4);
                });
            });

            app.Use((next) =>
            {
                return(async context =>
                {
                    var logs = context.Items[logKey] as List <string>;

                    logs.Add(log5);

                    await next(context);

                    logs.Add(log6);
                });
            });


            var cmdDelegate = app.Build();

            var context = new CommandContext()
            {
                Result              = new Mock <IResult>().Object,
                Command             = new Mock <ICommand>().Object,
                ServiceScopeFactory = provider.GetRequiredService <IServiceScopeFactory>(),
            };

            context.Items[logKey] = new List <string>();

            await cmdDelegate(context);

            var logs = context.Items[logKey] as List <string>;

            Assert.NotNull(context.Result);
            Assert.NotNull(context.Command);
            Assert.Equal(logs[0], log1);
            Assert.Equal(logs[1], log3);
            Assert.Equal(logs[2], log5);
            Assert.Equal(logs[3], log6);
            Assert.Equal(logs[4], log4);
            Assert.Equal(logs[5], log2);
        }