Ejemplo n.º 1
0
        public async Task when_handling_the_command()
        {
            CommandName = "FakeCommand";
            Json        = JObject.Parse("{}");

            async Task should_invoke_the_command_processor()
            {
                FakeCommandProcessor.Setup(x => x.ProcessWithOrWithoutResultAsync(CommandName, Json)).Returns(Task.FromResult(CommandResult.None));

                var result = await Subject.Handle(CommandName, Json) as OkResult;

                result.StatusCode.Should().Be(200);
            }

            async Task should_handle_CommandValidationException()
            {
                FakeCommandProcessor.Setup(x => x.ProcessWithOrWithoutResultAsync(CommandName, Json)).Throws(new CommandValidationException("invalid"));

                var result = await Subject.Handle(CommandName, Json) as BadRequestObjectResult;

                result.ShouldBeError("invalid");
            }

            async Task should_handle_Exception()
            {
                FakeCommandProcessor.Setup(x => x.ProcessWithOrWithoutResultAsync(CommandName, Json)).Throws(new Exception("fail"));

                var result = await Subject.Handle(CommandName, Json) as ObjectResult;

                result.StatusCode.Should().Be(500);
                result.ShouldBeError("fail");
            }
        }
Ejemplo n.º 2
0
        public async Task when_handling_the_command()
        {
            CommandName = "FakeCommand";
            FakeCommandProcessor.Setup(x => x.GetCommandType(CommandName)).Returns(typeof(FakeCommand));

            async Task should_invoke_the_command_processor()
            {
                FakeCommandProcessor.Setup(x => x.ProcessAsync(It.IsAny <FakeCommand>())).Returns(Task.CompletedTask);

                var result = await Subject.Handle(CommandName, Json);

                (await result.ExecuteAsync(CancellationToken.None)).StatusCode.Should().Be(200);
            }

            async Task should_handle_CommandProcessorException()
            {
                FakeCommandProcessor.Setup(x => x.ProcessAsync(It.IsAny <FakeCommand>())).Throws(new CommandProcessorException("fail"));

                var result = await Subject.Handle(CommandName, Json);

                await result.ShouldBeErrorAsync("fail", 400);
            }

            async Task should_handle_CommandException()
            {
                FakeCommandProcessor.Setup(x => x.ProcessAsync(It.IsAny <FakeCommand>())).Throws(new CommandException("invalid"));

                var result = await Subject.Handle(CommandName, Json);

                await result.ShouldBeErrorAsync("invalid", 400);
            }

            async Task should_handle_Exception()
            {
                FakeCommandProcessor.Setup(x => x.ProcessAsync(It.IsAny <FakeCommand>())).Throws(new Exception("fail"));

                var result = await Subject.Handle(CommandName, Json);

                await result.ShouldBeErrorAsync("fail", 500);
            }

            async Task should_log_errors()
            {
                var fakeTraceWriter = new Mock <ITraceWriter>();
                var subject         = new FakeCommandController(FakeCommandProcessor.Object, fakeTraceWriter.Object)
                {
                    Request       = new HttpRequestMessage(),
                    Configuration = new HttpConfiguration()
                };

                FakeCommandProcessor.Setup(x => x.ProcessAsync(It.IsAny <FakeCommand>())).Throws(new Exception("fail"));

                await subject.Handle(CommandName, Json);

                fakeTraceWriter.Verify(x => x.Trace(It.IsAny <HttpRequestMessage>(), "UnhandledCommandException", TraceLevel.Error, It.IsAny <Action <TraceRecord> >()));
            }
        }
Ejemplo n.º 3
0
        public async Task when_handling_the_command_with_result()
        {
            CommandName = "FakeResultCommand";
            Json        = JObject.Parse("{}");

            async Task should_invoke_the_command_processor()
            {
                FakeCommandProcessor.Setup(x => x.ProcessWithOrWithoutResultAsync(CommandName, Json)).Returns(Task.FromResult(new CommandResult(new FakeResult())));

                var result = await Subject.Handle(CommandName, Json);

                (await result.ExecuteAsync(CancellationToken.None)).Content.Should().NotBeNull();
            }
        }
Ejemplo n.º 4
0
        public async Task when_handling_the_command_with_result()
        {
            CommandName = "FakeResultCommand";
            Json        = JObject.Parse("{}");

            async Task should_invoke_the_command_processor()
            {
                FakeCommandProcessor.Setup(x => x.ProcessWithOrWithoutResultAsync(CommandName, Json)).Returns(Task.FromResult(new CommandResult(new FakeResult())));

                var result = await Subject.Handle(CommandName, Json) as OkObjectResult;

                result.StatusCode.Should().Be(200);
                result.Value.Should().NotBeNull();
            }
        }
Ejemplo n.º 5
0
        public async Task when_handling_the_command_with_result()
        {
            CommandName = "FakeResultCommand";
            FakeCommandProcessor.Setup(x => x.GetCommandType(CommandName)).Returns(typeof(FakeResultCommand));

            async Task should_return_the_result_from_the_command_processor()
            {
                var expected = new FakeResult();

                FakeCommandProcessor.Setup(x => x.ProcessWithResultAsync(It.IsAny <FakeResultCommand>())).Returns(Task.FromResult(expected));

                var result = await Subject.Handle(CommandName, Json) as OkNegotiatedContentResult <object>;

                (await result.ExecuteAsync(CancellationToken.None)).StatusCode.Should().Be(200);
                result.Content.Should().Be(expected);
            }
        }
Ejemplo n.º 6
0
        public async Task when_handling_the_command()
        {
            async Task should_return_the_result_from_the_command_processor()
            {
                var expected = new FakeResult();

                FakeCommandProcessor.Setup(x => x.ProcessWithResultAsync(It.IsAny <FakeResultCommand>())).Returns(Task.FromResult(expected));

                var result = await Subject.Handle(new FakeResultCommand()) as OkObjectResult;

                result.StatusCode.Should().Be(200);
                result.Value.Should().Be(expected);
            }

            async Task should_handle_CommandProcessorException()
            {
                FakeCommandProcessor.Setup(x => x.ProcessWithResultAsync(It.IsAny <FakeResultCommand>())).Throws(new CommandProcessorException("fail"));

                var result = await Subject.Handle(new FakeResultCommand());

                result.ShouldBeError("fail", 400);
            }

            async Task should_handle_CommandException()
            {
                FakeCommandProcessor.Setup(x => x.ProcessWithResultAsync(It.IsAny <FakeResultCommand>())).Throws(new CommandException("invalid"));

                var result = await Subject.Handle(new FakeResultCommand());

                result.ShouldBeError("invalid", 400);
            }

            async Task should_handle_Exception()
            {
                FakeCommandProcessor.Setup(x => x.ProcessWithResultAsync(It.IsAny <FakeResultCommand>())).Throws(new Exception("fail"));

                var result = await Subject.Handle(new FakeResultCommand());

                result.ShouldBeError("fail", 500);
            }
        }
Ejemplo n.º 7
0
        public async Task when_handling_the_command()
        {
            CommandName = "FakeCommand";
            Json        = JObject.Parse("{}");

            async Task should_invoke_the_command_processor()
            {
                FakeCommandProcessor.Setup(x => x.ProcessWithOrWithoutResultAsync(CommandName, Json)).Returns(Task.FromResult(CommandResult.None));

                var result = await Subject.Handle(CommandName, Json);

                (await result.ExecuteAsync(CancellationToken.None)).Content.Should().BeNull();
            }

            async Task should_handle_Exception()
            {
                FakeCommandProcessor.Setup(x => x.ProcessWithOrWithoutResultAsync(CommandName, Json)).Throws(new Exception("fail"));

                var result = await Subject.Handle(CommandName, Json) as ExceptionResult;

                await result.ShouldBeErrorAsync("fail");
            }
        }
        public async Task when_handling_the_command()
        {
            async Task should_invoke_the_command_processor()
            {
                var result = await Subject.Handle(new FakeCommand()) as OkResult;

                result.StatusCode.Should().Be(200);
            }

            async Task should_handle_CommandProcessorException()
            {
                FakeCommandProcessor.Setup(x => x.ProcessAsync(It.IsAny <FakeCommand>())).Throws(new CommandProcessorException("fail"));

                var result = await Subject.Handle(new FakeCommand());

                result.ShouldBeError("fail", 400);
            }

            async Task should_handle_CommandException()
            {
                FakeCommandProcessor.Setup(x => x.ProcessAsync(It.IsAny <FakeCommand>())).Throws(new CommandException("invalid"));

                var result = await Subject.Handle(new FakeCommand());

                result.ShouldBeError("invalid", 400);
            }

            async Task should_handle_Exception()
            {
                FakeCommandProcessor.Setup(x => x.ProcessAsync(It.IsAny <FakeCommand>())).Throws(new Exception("fail"));

                var result = await Subject.Handle(new FakeCommand());

                result.ShouldBeError("fail", 500);
            }
        }