public void TestBuilderByComponentType()
        {
            // Arrange
            const string name = "test-name";

            PipelineComponentResolver.Add(new FooComponent());
            PipelineComponentResolver.Add(new BarComponent());

            var pipeline = NonAsyncPipelineBuilder <TestPayload>
                           .Initialize(null)
                           .WithComponent <FooComponent>()
                           .WithComponent <BarComponent>()
                           .WithComponentResolver(PipelineComponentResolver)
                           .WithoutSettings()
                           .Build(name);

            var payload = new TestPayload();

            // Act
            payload.FooWasCalled.Should().BeFalse();
            payload.BarWasCalled.Should().BeFalse();
            var result = pipeline.Execute(payload);

            // Assert
            pipeline.Name.Should().Be(name);
            result.Count.Should().Be(2);
            result.Count.Should().Be(2);
            result.FooWasCalled.Should().BeTrue();
            result.BarWasCalled.Should().BeTrue();
        }
        public void TestBuilderWithSettings()
        {
            // Arrange
            PipelineComponentResolver.Add(new ConfigurableComponent());

            var pipeline = NonAsyncPipelineBuilder <TestPayload>
                           .Initialize(null)
                           .WithComponent <ConfigurableComponent>()
                           .WithComponentResolver(PipelineComponentResolver)
                           .WithSettings(new Dictionary <string, IDictionary <string, string> >
            {
                {
                    "ConfigurableComponent", new Dictionary <string, string>
                    {
                        { "UseFoo", "false" },
                        { "TestValue", "MyBarTestValue" }
                    }
                }
            })
                           .Build();

            var payload = new TestPayload();

            // Act
            var result = pipeline.Execute(payload);

            // Assert
            pipeline.Name.StartsWith("Pipeline").Should().BeTrue();
            payload.FooStatus.Should().BeNull();
            result.BarStatus.Should().Be("MyBarTestValue");
        }
        public void Pipeline_Execution_Test()
        {
            //Arrange
            PipelineComponentResolver.Add(new FooComponent(), new BarComponent());

            var types = new List <Type> {
                typeof(FooComponent), typeof(BarComponent)
            };
            var config = types.ToDictionary <Type, string, IDictionary <string, string> >(
                t => t.Name,
                t => new Dictionary <string, string> {
                { "test", "value" }
            });

            var target = new Pipeline <TestPayload>(PipelineComponentResolver, types, config, null);

            //Act
            var result = target.Execute(new TestPayload());

            //Assert
            result.Should().NotBeNull();
            result.Count.Should().Be(2);
            result.FooStatus.Should().Be($"{nameof(FooComponent)} executed!");
            result.BarStatus.Should().Be($"{nameof(BarComponent)} executed!");
        }
        public void Pipeline_Execution_ComponentNames_EmptySettings_Test()
        {
            //Arrange
            PipelineComponentResolver.Add(new FooComponent(), new BarComponent());
            var types = new List <string> {
                nameof(FooComponent), nameof(BarComponent)
            };
            var target = new Pipeline <TestPayload>(PipelineComponentResolver, types, new Dictionary <string, IDictionary <string, string> >(), null);

            //Act
            var result = target.Execute(new TestPayload());

            //Assert
            result.Should().NotBeNull();
            result.Count.Should().Be(2);
            result.FooStatus.Should().Be($"{nameof(FooComponent)} executed!");
            result.BarStatus.Should().Be($"{nameof(BarComponent)} executed!");
        }
        public void Pipeline_ExecutionStatusNotification_Test()
        {
            //Arrange
            PipelineComponentResolver.Add(new FooComponent(), new BarExceptionComponent());

            var receiver = Substitute.For <IPipelineComponentExecutionStatusReceiver>();

            var sut = PipelineBuilder <TestPayload> .InitializePipeline(receiver)
                      .WithComponent <FooComponent>()
                      .WithComponent <BarExceptionComponent>()
                      .WithComponentResolver(PipelineComponentResolver)
                      .WithoutSettings()
                      .Build();

            //Act
            Action act = () => sut.Execute(new TestPayload());

            //Assert
            act.Should()
            .ThrowExactly <PipelineExecutionException>()
            .WithInnerExceptionExactly <NotImplementedException>();

            receiver.Received(2)
            .ReceiveExecutionStarting(Arg.Is <PipelineComponentExecutionStartingInfo>(info =>
                                                                                      info.PipelineComponentName == nameof(FooComponent) ||
                                                                                      info.PipelineComponentName == nameof(BarExceptionComponent)));

            receiver.Received()
            .ReceiveExecutionCompleted(
                Arg.Is <PipelineComponentExecutionCompletedInfo>(info =>
                                                                 info.PipelineComponentName == nameof(FooComponent) &&
                                                                 info.ExecutionTime != TimeSpan.Zero &&
                                                                 info.Exception == null));

            receiver.Received()
            .ReceiveExecutionCompleted(
                Arg.Is <PipelineComponentExecutionCompletedInfo>(info =>
                                                                 info.PipelineComponentName == nameof(BarExceptionComponent) &&
                                                                 info.ExecutionTime != TimeSpan.Zero &&
                                                                 info.Exception is NotImplementedException));
        }
        public void PipelineComponent_SettingNotFoundException_Test()
        {
            //Arrange
            PipelineComponentResolver.Add(new FooSettingNotFoundComponent());
            var types = new List <Type> {
                typeof(FooSettingNotFoundComponent)
            };
            var config = types.ToDictionary <Type, string, IDictionary <string, string> >(
                t => t.Name,
                t => new Dictionary <string, string> {
                { "test", "value" }
            });

            var target = new Pipeline <TestPayload>(PipelineComponentResolver, types, config, null);

            //Act
            Action act = () => target.Execute(new TestPayload());

            //Assert
            act.Should().ThrowExactly <PipelineExecutionException>()
            .WithInnerExceptionExactly <PipelineComponentSettingNotFoundException>();
        }
        public void Pipeline_Execution_Cancellation_Test()
        {
            //Arrange
            PipelineComponentResolver.Add(new DelayComponent(), new BarComponent());

            var types = new List <Type> {
                typeof(DelayComponent), typeof(BarComponent)
            };
            var config = types.ToDictionary <Type, string, IDictionary <string, string> >(
                t => t.Name,
                t => new Dictionary <string, string> {
                { "test", "value" }
            });

            var cts = new CancellationTokenSource(TimeSpan.FromSeconds(2));

            var target = new Pipeline <TestPayload>(PipelineComponentResolver, types, config, null);

            //Act
            Action act = () => target.Execute(new TestPayload(), cts.Token);

            //Assert
            act.Should().Throw <OperationCanceledException>();
        }