예제 #1
0
        public void Should_Return_Collection_Of_Responses_From_All_Applicable_Handlers()
        {
            var sut = IocHelper.GetRequestBus(sc =>
            {
                sc.AddTransient <IResponseHandler <TestRequest, TestResult>, DependentResponseHandler>();
                sc.AddTransient <IResponseHandler <TestRequest, TestResult>, IndependentResponseHandler>();
                sc.AddTransient <IResponseHandler <TestRequest, TestResult>, MainResponseHandler>();
                sc.AddTransient <IResponseHandler <TestRequest, TestResult>, DependentNonApplicableResponseHandler>();
            });

            var request = new TestRequest
            {
                Name = "Ruhul"
            };

            var rsp = sut.Responses <TestRequest, TestResult>(request);

            rsp.MainResponse().IsSucceed.ShouldBe(true);
            rsp.MainResponse().Value.ShouldNotBeNull();
            rsp.MainResponse().Value.Message.ShouldBe($"{request.Name} : handled by {nameof(MainResponseHandler)}");
            rsp.OtherResponses().Count().ShouldBe(2);
            rsp.OtherResponses().ShouldContain(x => x.Value.Message == $"{request.Name} : handled by {nameof(DependentResponseHandler)}");
            rsp.OtherResponses().ShouldContain(x => x.Value.Message == $"{request.Name} : handled by {nameof(IndependentResponseHandler)}");
            rsp.OtherResponses().ShouldNotContain(x => x.Value.Message == $"{request.Name} : handled by {nameof(DependentNonApplicableResponseHandler)}");
        }
예제 #2
0
        public async Task Should_Return_Collection_Of_Responses_From_All_Applicable_Handlers()
        {
            var sut = IocHelper.GetRequestBus(sc =>
            {
                sc.AddTransient <IResponseHandlerAsync <TestRequest, TestResult>, DependentResponseHandler>();
                sc.AddTransient <IResponseHandlerAsync <TestRequest, TestResult>, IndependentResponseHandler>();
                sc.AddTransient <IResponseHandlerAsync <TestRequest, TestResult>, MainResponseHandler>();
                sc.AddTransient <IResponseHandlerAsync <TestRequest, TestResult>, DependentNonApplicableResponseHandler>();
            });

            var request = new TestRequest
            {
                Name = "Ruhul"
            };

            var rsp = await sut.ResponsesAsync <TestRequest, TestResult>(request);

            var mainRsp = rsp.MainResponse();
            var others  = rsp.OtherResponses().ToArray();

            mainRsp.ShouldNotBeNull();
            others.Length.ShouldBe(2);
            mainRsp.IsSucceed.ShouldBe(true);
            others.ShouldAllBe(x => x.IsSucceed);
            others.ShouldAllBe(x => x.Value != null);
            mainRsp.Value.Message.ShouldBe($"{request.Name} : handled by {nameof(MainResponseHandler)}");
            others.ShouldContain(x =>
                                 x.Value.Message == $"{request.Name} : handled by {nameof(DependentResponseHandler)}");
            others.ShouldContain(x =>
                                 x.Value.Message == $"{request.Name} : handled by {nameof(IndependentResponseHandler)}");
            others.ShouldNotContain(x =>
                                    x.Value.Message == $"{request.Name} : handled by {nameof(DependentNonApplicableResponseHandler)}");
        }
예제 #3
0
        public async Task Filters_Should_Change_Final_Response_After_All_Handlers_Executed()
        {
            var sut = IocHelper.GetRequestBus(sc =>
            {
                sc.AddTransient <IResponseHandlerAsync <TestRequest, TestResult>, MainResponseHandler>();
                sc.AddTransient <IResponseHandlerAsync <TestRequest, TestResult>, DependentResponseHandler>();
                sc.AddTransient <IResponseFilterAsync <TestRequest, TestResult>, ResponseFilter>();
                sc.AddTransient <IResponseFilterAsync <TestRequest, TestResult>, ResponseFilterNotApplicable>();
            });

            var request = new TestRequest
            {
                Name = "Ruhul"
            };

            var rsp = await sut.ResponsesAsync <TestRequest, TestResult>(request);

            var mainRsp = rsp.MainResponse();
            var others  = rsp.OtherResponses().ToArray();

            mainRsp.ShouldNotBeNull();
            mainRsp.IsSucceed.ShouldBe(true);
            others.Length.ShouldBe(2);
            others.ShouldContain(x => x.Value.Message == $"{request.Name} filtered by {nameof(ResponseFilter)}");
        }
예제 #4
0
        public void Should_Throw_Exception_When_No_Handler_Available()
        {
            var sut = IocHelper.GetRequestBus(sc =>
            {
                sc.AddTransient <IResponseHandler <None, TestResult>, ResponseHandlerNotApplicable>();
            });

            Should.Throw <NoHandlerAvailable>(() => sut.Response <TestResult>());
        }
예제 #5
0
        public void Should_Throw_Exception_When_Handler_Not_Available()
        {
            var sut = IocHelper.GetRequestBus(sc =>
            {
                sc.AddTransient <IRequestHandler <TestRequest, TestResponse>, TestRequestNotApplicableHandler>();
            });

            Should.Throw <NoHandlerAvailable>(() =>
                                              sut.Send <TestRequest, TestResponse>(new TestRequest()));
        }
예제 #6
0
        public void Should_Return_BadRequest_With_Error_When_Validation_Failed()
        {
            var sut = IocHelper.GetRequestBus(sc =>
            {
                sc.AddTransient <IRequestValidator <TestRequest>, TestRequestValidator>();
            });

            var rsp = sut.WidgetResponse(new TestRequest());

            rsp.StatusCode.ShouldBe(400);
            rsp.Errors.Count().ShouldBe(1);
            rsp.Errors.ShouldContain(x => x.Message == "Name is required." && x.PropertyName == "Name");
        }
예제 #7
0
        public void Should_Return_Empty_Response_When_No_Handler_Registered()
        {
            var sut = IocHelper.GetRequestBus(sc =>
            {
            });

            var rsp = sut.WidgetResponse(new TestRequest());

            rsp.ShouldNotBeNull();
            rsp.StatusCode.ShouldBe((int)HttpStatusCode.OK);
            rsp.Errors.ShouldBeEmpty();
            rsp.Widgets.ShouldBeEmpty();
        }
예제 #8
0
        public void Should_Make_RequestBus_Context_Available_To_Handler()
        {
            var sut = IocHelper.GetRequestBus(sc =>
            {
                sc.AddTransient <IRequestBusContextWriter, RequestContextWriter>();
                sc.AddTransient <IRequestHandler <TestRequestContext, None>, TestRequestContextHandler>();
            });

            var request = new TestRequestContext();

            sut.Send(request);
            request.MsgInContext.ShouldBe("Hello World!");
        }
예제 #9
0
        public void Should_Return_Result_By_First_Applicable_Handler()
        {
            var sut = IocHelper.GetRequestBus(sc =>
            {
                sc.AddTransient <IRequestHandler <TestRequest, TestResponse>, TestRequestNotApplicableHandler>();
                sc.AddTransient <IRequestHandler <TestRequest, TestResponse>, TestRequestHandler>();
            });

            var rsp = sut.Send <TestRequest, TestResponse>(new TestRequest());

            rsp.IsSucceed.ShouldBe(true);
            rsp.Value.HandlerExecuted.ShouldBe(nameof(TestRequestHandler));
        }
예제 #10
0
        public void Should_Validate_Request()
        {
            var sut = IocHelper.GetRequestBus(sc =>
            {
                sc.AddTransient <IRequestValidator <TestRequest>, TestRequestValidator>();
            });

            var rsp = sut.Responses <TestRequest, TestResult>(new TestRequest());

            rsp.MainResponse().ShouldNotBeNull();
            rsp.MainResponse().IsSucceed.ShouldBeFalse();
            rsp.MainResponse().Errors.ShouldContain(x => x.Message == "Name is required.");
        }
예제 #11
0
        public void Should_Execute_Applicable_Handler()
        {
            var sut = IocHelper.GetRequestBus(sc =>
            {
                sc.AddTransient <IResponseHandler <None, TestResult>, ResponseHandler>();
                sc.AddTransient <IResponseHandler <None, TestResult>, ResponseHandlerNotApplicable>();
            });

            var rsp = sut.Response <TestResult>();

            rsp.IsSucceed.ShouldBe(true);
            rsp.Value.ShouldNotBeNull();
            rsp.Value.Message.ShouldBe("Hello World!");
        }
예제 #12
0
        public void Should_Return_Response_From_First_Applicable_Handler()
        {
            var sut = IocHelper.GetRequestBus(sc =>
            {
                sc.AddTransient <IRequestHandler <TestRequest, None>, TestNoApplicableRequestHandler>();
                sc.AddTransient <IRequestHandler <TestRequest, None>, TestRequestHandler>();
            });

            var request = new TestRequest();

            sut.Send(request);

            request.HandlersExecuted.ShouldContain(nameof(TestRequestHandler));
        }
예제 #13
0
        public async Task Should_Run_First_Applicable_Handler()
        {
            var sut = IocHelper.GetRequestBus(sc =>
            {
                sc.AddTransient <IRequestHandlerAsync <TestRequest, None>, TestNoApplicableRequestHandler>();
                sc.AddTransient <IRequestHandlerAsync <TestRequest, None>, TestRequestHandler>();
            });

            var request = new TestRequest();

            await sut.SendAsync(request);

            request.HandlersExecuted.ShouldContain(nameof(TestRequestHandler));
        }
예제 #14
0
        public void Should_Populate_And_Make_RequestContext_Available_To_Handler()
        {
            var sut = IocHelper.GetRequestBus(sc =>
            {
                sc.AddTransient <IRequestHandler <TestRequest, TestResponse>, TestContextHandler>();
                sc.AddTransient <IRequestBusContextWriter, TestTenantContextWriter>();
                sc.AddTransient <IRequestBusContextWriter, TestUserContextWriter>();
            });

            var rsp = sut.Send <TestRequest, TestResponse>(new TestRequest());

            rsp.IsSucceed.ShouldBe(true);
            rsp.Value.HandlerExecuted.ShouldBe(nameof(TestContextHandler));
            rsp.Value.Message.ShouldBe("tenant:bookworm-au user:ruhul");
        }
예제 #15
0
        public async Task Should_Validate_Request()
        {
            var sut = IocHelper.GetRequestBus(sc =>
            {
                sc.AddTransient <IRequestValidatorAsync <TestRequest>, TestRequestValidator>();
            });

            var rsp = await sut.ResponsesAsync <TestRequest, TestResult>(new TestRequest());

            var mainRsp = rsp.MainResponse();

            mainRsp.ShouldNotBeNull();
            mainRsp.IsSucceed.ShouldBe(false);
            mainRsp.Errors.ShouldContain(x => x.Message == "Name is required.");
        }
예제 #16
0
        public void Should_Execute_All_Applicable_EventHandlers()
        {
            var sut = IocHelper.GetRequestBus(sc =>
            {
                sc.AddTransient <IEventHandler <OrderPlaced>, OrderPlacedTest1EventHandler>();
                sc.AddTransient <IEventHandler <OrderPlaced>, OrderPlacedTest2EventHandler>();
                sc.AddTransient <IEventHandler <OrderPlaced>, OrderPlacedTest3EventHandler>();
            });

            var @event = new OrderPlaced();

            sut.Publish(@event);

            @event.HandlersProcessed.ShouldContain(nameof(OrderPlacedTest1EventHandler));
            @event.HandlersProcessed.ShouldContain(nameof(OrderPlacedTest2EventHandler));
            @event.HandlersProcessed.ShouldNotContain(nameof(OrderPlacedTest3EventHandler));
        }
예제 #17
0
        public void Should_Return_All_Widget_Responses()
        {
            var sut = IocHelper.GetRequestBus(sc =>
            {
                sc.AddTransient <IResponseHandler <TestRequest, WidgetResponse>, MainTestWidget>();
                sc.AddTransient <IResponseHandler <TestRequest, WidgetResponse>, TestWidget3Rd>();
                sc.AddTransient <IResponseHandler <TestRequest, WidgetResponse>, TestWidget2Nd>();
            });

            var rsp = sut.WidgetResponse(new TestRequest());

            rsp.StatusCode.ShouldBe(200);
            rsp.RedirectAction.ShouldBeNull();
            rsp.Widgets.ShouldContain(x => x.Name == "main" && x.DisplayOrder == 0);
            rsp.Widgets.ShouldContain(x => x.Name == "recently-viewed" && x.DisplayOrder == 2);
            rsp.Widgets.ShouldContain(x => x.Name == "latest-editorials" && x.DisplayOrder == 1);
        }
예제 #18
0
        public async Task Should_Validate_Request()
        {
            var sut = IocHelper.GetRequestBus(sc =>
            {
                sc.AddTransient <IRequestHandlerAsync <TestValidationRequest, string>, TestValidationRequestHandler>();
                sc.AddTransient <IRequestValidatorAsync <TestValidationRequest>,
                                 TestNotApplicableRequestValidator>();
                sc.AddTransient <IRequestValidatorAsync <TestValidationRequest>, TestRequestValidator>();
            });

            var rsp = await sut.SendAsync <TestValidationRequest, string>(new TestValidationRequest());

            rsp.IsSucceed.ShouldBe(false);
            rsp.Value.ShouldBe(null);
            rsp.Errors.ShouldContain(err =>
                                     err.Message == "Name is required." &&
                                     err.PropertyName == "Name");
        }
예제 #19
0
        public void Should_Not_Run_Other_Handlers_When_Main_Handler_Failed()
        {
            var sut = IocHelper.GetRequestBus(sc =>
            {
                sc.AddTransient <IResponseHandler <TestRequest, TestResult>, DependentResponseHandler>();
                sc.AddTransient <IResponseHandler <TestRequest, TestResult>, IndependentResponseHandler>();
                sc.AddTransient <IResponseHandler <TestRequest, TestResult>, MainFailedResponseHandler>();
                sc.AddTransient <IResponseHandler <TestRequest, TestResult>, DependentNonApplicableResponseHandler>();
            });

            var request = new TestRequest
            {
                Name = "Ruhul"
            };

            var rsp = sut.Responses <TestRequest, TestResult>(request);

            rsp.MainResponse().IsSucceed.ShouldBe(false);
            rsp.MainResponse().Errors.Count().ShouldBe(0);
        }
예제 #20
0
        public void Exception_In_NonMain_Handler_Should_Not_Stop_Other_Handlers_Execution()
        {
            var sut = IocHelper.GetRequestBus(sc =>
            {
                sc.AddTransient <IResponseHandler <TestRequest, TestResult>, InDependentHandlerWithException>();
                sc.AddTransient <IResponseHandler <TestRequest, TestResult>, DependentHandlerWithException>();
                sc.AddTransient <IResponseHandler <TestRequest, TestResult>, MainResponseHandler>();
            });

            var request = new TestRequest
            {
                Name = "Ruhul"
            };

            var rsp = sut.Responses <TestRequest, TestResult>(request);

            rsp.MainResponse().IsSucceed.ShouldBe(true);
            rsp.MainResponse().Value.Message.ShouldBe($"{request.Name} : handled by {nameof(MainResponseHandler)}");
            rsp.MainResponse().Errors.Count().ShouldBe(0);
        }
예제 #21
0
        public async Task Should_Validate_Request()
        {
            var sut = IocHelper.GetRequestBus(sc =>
            {
                sc.AddTransient <IRequestHandlerAsync <RequestForShouldValidateRequest, None>,
                                 RequestHandlerForShouldValidateRequest>();

                sc.AddTransient <IRequestValidatorAsync <RequestForShouldValidateRequest>,
                                 RequestValidatorNotApplicableForShouldValidateRequest>();

                sc.AddTransient <IRequestValidatorAsync <RequestForShouldValidateRequest>,
                                 RequestValidatorForShouldValidateRequest>();
            });

            var rsp = await sut.SendAsync <RequestForShouldValidateRequest>(new RequestForShouldValidateRequest());

            rsp.IsSucceed.ShouldBe(false);
            rsp.Errors.ShouldContain(x =>
                                     x.Message == "Name is required." &&
                                     x.PropertyName == "Name");
        }
예제 #22
0
        public void Filters_Should_Change_Final_Response_After_All_Handlers_Executed()
        {
            var sut = IocHelper.GetRequestBus(sc =>
            {
                sc.AddTransient <IResponseHandler <TestRequest, TestResult>, MainResponseHandler>();
                sc.AddTransient <IResponseHandler <TestRequest, TestResult>, DependentResponseHandler>();
                sc.AddTransient <IResponseFilter <TestRequest, TestResult>, ResponseFilter>();
                sc.AddTransient <IResponseFilter <TestRequest, TestResult>, ResponseFilterNotApplicable>();
            });

            var request = new TestRequest
            {
                Name = "Ruhul"
            };

            var rsp = sut.Responses <TestRequest, TestResult>(request);

            rsp.MainResponse().IsSucceed.ShouldBe(true);
            rsp.OtherResponses().Count().ShouldBe(2);
            rsp.OtherResponses().ShouldContain(x =>
                                               x.Value.Message == $"{request.Name} filtered by {nameof(ResponseFilter)}");
        }
예제 #23
0
        public async Task Should_Not_Run_Other_Dependent_Handlers_When_Main_Handler_Failed()
        {
            var sut = IocHelper.GetRequestBus(sc =>
            {
                sc.AddTransient <IResponseHandlerAsync <TestRequest, TestResult>, DependentResponseHandler>();
                sc.AddTransient <IResponseHandlerAsync <TestRequest, TestResult>, IndependentResponseHandler>();
                sc.AddTransient <IResponseHandlerAsync <TestRequest, TestResult>, MainFailedResponseHandler>();
                sc.AddTransient <IResponseHandlerAsync <TestRequest, TestResult>, DependentNonApplicableResponseHandler>();
            });

            var request = new TestRequest
            {
                Name = "Ruhul"
            };

            var rsp = await sut.ResponsesAsync <TestRequest, TestResult>(request);

            var mainRsp = rsp.MainResponse();

            mainRsp.ShouldNotBeNull();
            mainRsp.IsSucceed.ShouldBe(false);
            mainRsp.Errors.Count().ShouldBe(0);
            rsp.OtherResponses().Count().ShouldBe(1);
        }
예제 #24
0
        public void Should_Throw_Exception_When_No_Handler_Available()
        {
            var sut = IocHelper.GetRequestBus();

            Should.Throw <NoHandlerAvailable>(() => sut.Send <TestRequest>(new TestRequest()));
        }
예제 #25
0
        public async Task Should_Throw_Exception_When_No_Handler_Available()
        {
            var sut = IocHelper.GetRequestBus();

            await Should.ThrowAsync <NoHandlerAvailable>(() => sut.SendAsync(new TestRequest()));
        }