public void Simple()
 {
     var wf = new StatefulWorkflow<StatefulObject>(2, gateway.Object)
         .Yield(1)
         .Do(x => x.Feedback("middle"))
         .Yield(2)
         ;
     gateway.SetReturnsDefault<bool>(true);
     gateway.Setup(x => x.IsTransitionAllowed(
         It.Is<ITransition>(y => (int?)y.From == 1 && (int?)y.To == 2)))
         .Returns(false);
     wf.Start(obj.Object);
     Assert.Throws<UnallowedTransitionException>(() => wf.Start(obj.Object));
     obj.Verify(x => x.Feedback("middle"), Times.Never());
 }
Exemplo n.º 2
0
        public void Simple()
        {
            var wf = new StatefulWorkflow <StatefulObject>(2, gateway.Object)
                     .Yield(1)
                     .Do(x => x.Feedback("middle"))
                     .Yield(2)
            ;

            gateway.SetReturnsDefault <bool>(true);
            gateway.Setup(x => x.IsTransitionAllowed(
                              It.Is <ITransition>(y => (int?)y.From == 1 && (int?)y.To == 2)))
            .Returns(false);
            wf.Start(obj.Object);
            Assert.Throws <UnallowedTransitionException>(() => wf.Start(obj.Object));
            obj.Verify(x => x.Feedback("middle"), Times.Never());
        }
Exemplo n.º 3
0
        public void it_throws_a_WorkflowActionFailedException()
        {
            var wf = new StatefulWorkflow<Entity>()
                .When(x => true).Fail()
                .Yield("end");

            Assert.Throws<WorkflowActionFailedException>(() => wf.Start(new Entity()));
        }
Exemplo n.º 4
0
        public void it_throws_a_WorkflowActionFailedException()
        {
            var wf = new StatefulWorkflow <Entity>()
                     .When(x => true).Fail()
                     .Yield("end");

            Assert.Throws <WorkflowActionFailedException>(() => wf.Start(new Entity()));
        }
Exemplo n.º 5
0
        public void BranchingIntoUnallowedState()
        {
            var branch1 = Declare.Step();
            var wf      = new StatefulWorkflow <StatefulObject>(2, gateway.Object)
                          .Yield(1)
                          .Define(branch1)
                          .Do(x => x.Feedback("middle"))
                          .Yield(2)
                          .When(x => true).BranchTo(branch1)
            ;

            gateway.SetReturnsDefault <bool>(true);
            gateway.Setup(x => x.IsTransitionAllowed(
                              It.Is <ITransition>(y => object.Equals(y.From, 2) && object.Equals(y.To, 2))))
            .Returns(false);
            wf.Start(obj.Object);
            wf.Start(obj.Object);

            Assert.Throws <UnallowedTransitionException>(() => wf.Start(obj.Object));
            obj.Verify(x => x.Feedback("middle"), Times.Once()); // not twice
        }
        public void BranchingIntoUnallowedState()
        {
            var branch1 = Declare.Step();
            var wf = new StatefulWorkflow<StatefulObject>(2, gateway.Object)
                .Yield(1)
                .Define(branch1)
                .Do(x => x.Feedback("middle"))
                .Yield(2)
                .When(x => false, otherwise: branch1)
                ;

            gateway.SetReturnsDefault<bool>(true);
            gateway.Setup(x => x.IsTransitionAllowed(
                It.Is<ITransition>(y => object.Equals(y.From, 2) && object.Equals(y.To, 2))))
                .Returns(false);
            wf.Start(obj.Object);
            wf.Start(obj.Object);

            Assert.Throws<UnallowedTransitionException>(() => wf.Start(obj.Object));
            obj.Verify(x => x.Feedback("middle"), Times.Once()); // not twice
        }
Exemplo n.º 7
0
        public void it_can_branch_using_negative_When()
        {
            var point = Declare.Step();

            var wf = new StatefulWorkflow<Entity>()
                .Define(point)
                .Yield("branched")
                .Yield("start")
                .When(x => false).BranchTo(point)
                .Yield("didn't branch");

            var obj = new Entity() { state = "start" };
            wf.Start(obj);
            Assert.That(obj.GetStateId(null), Is.EqualTo("didn't branch"));
        }
        public void it_will_let_you_break_with_a_nonexistent_state()
        {
            bool branched = false;
            var wf = new StatefulWorkflow<Entity>()
                .Do(x => branched = true)
                .Yield("broke")
                .Yield("begin")
                .When(x => true).BreakWithStatus("nonexistent")
                .Yield("didn't break");

            var obj = new Entity() { state = "begin" };
            wf.Start(obj);
            obj.state.ShouldBe("nonexistent");
            Assert.That(!branched);
        }
Exemplo n.º 9
0
        public void it_throws_a_WorkflowActionFailedException_with_a_formatted_message()
        {
            var wf = new StatefulWorkflow <Entity>()
                     .When(x => true).Fail(x => x.With("{0} message", 1))
                     .Yield("end");

            try
            {
                wf.Start(new Entity());
                Assert.Fail("an exception should have been thrown");
            }
            catch (WorkflowActionFailedException e)
            {
                e.Message.ShouldBe("1 message");
            }
        }
Exemplo n.º 10
0
        public void it_throws_a_WorkflowActionFailedException_with_a_formatted_message()
        {
            var wf = new StatefulWorkflow<Entity>()
                .When(x => true).Fail(x => x.With("{0} message", 1))
                .Yield("end");

            try
            {
                wf.Start(new Entity());
                Assert.Fail("an exception should have been thrown");
            }
            catch (WorkflowActionFailedException e)
            {
                e.Message.ShouldBe("1 message");
            }
        }
Exemplo n.º 11
0
        public void it_throws_the_exception_created_by_the_user()
        {
            var ex = new UserDefinedException();
            var wf = new StatefulWorkflow <Entity>()
                     .When(x => true).Fail(f => f.With(x => ex))
                     .Yield("end");

            try
            {
                wf.Start(new Entity());
                Assert.Fail("an exception should have been thrown");
            }
            catch (UserDefinedException e)
            {
                Assert.That(e, Is.SameAs(ex));
            }
        }
Exemplo n.º 12
0
        public void it_can_access_info_from_lambda()
        {
            bool lambdaCalled = false;
            var wf = new StatefulWorkflow<Entity>()
                .Yield("start")
                .When(x => true).Fail(f => f.With(o =>
                {
                    lambdaCalled = true;
                    Assert.That(o, Is.Not.Null);
                    Assert.That(o.GetStateId(null), Is.EqualTo("start"));
                    return "failed successfully";
                }))
                .Yield("end");

            Assert.Throws<WorkflowActionFailedException>(() => wf.Start(new Entity() { state = "start" }));
            Assert.That(lambdaCalled, "lambda builder function should be called");
        }
        public int multiple_workflow_steps_are_executed_conditionally(bool branch)
        {
            int hitInnerBranch = 0, hitOutterBranch = 0;
            var workflow = new StatefulWorkflow<Entity>()
                .When(x => branch).Do(wf =>
                {
                    wf.Do(x => hitInnerBranch++);
                    wf.Do(x => hitInnerBranch++);
                })
                .Do(x => hitOutterBranch++)
                .Yield("end");

            var obj = new Entity();
            workflow.Start(obj);
            hitOutterBranch.ShouldBe(1);
            return hitInnerBranch;
        }
Exemplo n.º 14
0
        public int multiple_workflow_steps_are_executed_conditionally(bool branch)
        {
            int hitInnerBranch = 0, hitOutterBranch = 0;
            var workflow = new StatefulWorkflow <Entity>()
                           .When(x => branch).Do(wf =>
            {
                wf.Do(x => hitInnerBranch++);
                wf.Do(x => hitInnerBranch++);
            })
                           .Do(x => hitOutterBranch++)
                           .Yield("end");

            var obj = new Entity();

            workflow.Start(obj);
            hitOutterBranch.ShouldBe(1);
            return(hitInnerBranch);
        }
Exemplo n.º 15
0
        public void it_will_let_you_break_with_a_nonexistent_state()
        {
            bool branched = false;
            var  wf       = new StatefulWorkflow <Entity>()
                            .Do(x => branched = true)
                            .Yield("broke")
                            .Yield("begin")
                            .When(x => true).BreakWithStatus("nonexistent")
                            .Yield("didn't break");

            var obj = new Entity()
            {
                state = "begin"
            };

            wf.Start(obj);
            obj.state.ShouldBe("nonexistent");
            Assert.That(!branched);
        }
        public void it_observes_security_rules()
        {
            var security = new Mock<ITransitionGateway>();
            security.Setup(x => x.AllowTransitions(It.IsAny<IList<ITransition>>()))
                .Returns<IList<ITransition>>(x => x.Where(y => (string)y.From == "begin" && (string)y.To == "didn't break"));

            bool branched = false;
            var wf = new StatefulWorkflow<Entity>(null, security.Object)
                .Do(x => branched = true)
                .Yield("broke")
                .Yield("begin")
                .When(x => true).BreakWithStatus("broke")
                .Yield("didn't break");

            var obj = new Entity() { state = "begin" };
            Assert.Throws<UnallowedTransitionException>(() => wf.Start(obj));
            obj.state.ShouldBe("begin");
            Assert.That(!branched);
        }
Exemplo n.º 17
0
        public void it_can_branch_using_positive_Unless()
        {
            var point = Declare.Step();

            var wf = new StatefulWorkflow <Entity>()
                     .Define(point)
                     .Yield("branched")
                     .Yield("start")
                     .Unless(x => true).BranchTo(point)
                     .Yield("didn't branch");

            var obj = new Entity()
            {
                state = "start"
            };

            wf.Start(obj);
            Assert.That(obj.GetStateId(null), Is.EqualTo("didn't branch"));
        }
        public void ItUsesTheOriginalStateWhenCheckingSecurity()
        {
            var transitions = new[] { new Transition(null, "start", "end") };
            var security    = new DefaultTransitionGateway(transitions);

            var end = Declare.Step();
            var wf  = new StatefulWorkflow <T>(null, security)
                      .Define(end)
                      .Yield("end")
                      .Yield("start")
                      .Do(x => x.SetStateId(null, "end"))
                      .When(x => true).BranchTo(end);

            var t = new T();

            wf.Start(t);

            // Main assertion is really that it didn't throw a security exception
            Assert.That(t.GetStateId(null), Is.EqualTo("end"));
        }
Exemplo n.º 19
0
        public void it_can_access_info_from_lambda()
        {
            bool lambdaCalled = false;
            var  wf           = new StatefulWorkflow <Entity>()
                                .Yield("start")
                                .When(x => true).Fail(f => f.With(o =>
            {
                lambdaCalled = true;
                Assert.That(o, Is.Not.Null);
                Assert.That(o.GetStateId(null), Is.EqualTo("start"));
                return("failed successfully");
            }))
                                .Yield("end");

            Assert.Throws <WorkflowActionFailedException>(() => wf.Start(new Entity()
            {
                state = "start"
            }));
            Assert.That(lambdaCalled, "lambda builder function should be called");
        }
Exemplo n.º 20
0
        public void it_observes_security_rules()
        {
            var security = new Mock <ITransitionGateway>();

            security.Setup(x => x.AllowTransitions(It.IsAny <IList <ITransition> >()))
            .Returns <IList <ITransition> >(x => x.Where(y => (string)y.From == "begin" && (string)y.To == "didn't break"));

            bool branched = false;
            var  wf       = new StatefulWorkflow <Entity>(null, security.Object)
                            .Do(x => branched = true)
                            .Yield("broke")
                            .Yield("begin")
                            .When(x => true).BreakWithStatus("broke")
                            .Yield("didn't break");

            var obj = new Entity()
            {
                state = "begin"
            };

            Assert.Throws <UnallowedTransitionException>(() => wf.Start(obj));
            obj.state.ShouldBe("begin");
            Assert.That(!branched);
        }
Exemplo n.º 21
0
        public void it_throws_the_exception_created_by_the_user()
        {
            var ex = new UserDefinedException();
            var wf = new StatefulWorkflow<Entity>()
                .When(x => true).Fail(f => f.With(x => ex))
                .Yield("end");

            try
            {
                wf.Start(new Entity());
                Assert.Fail("an exception should have been thrown");
            }
            catch (UserDefinedException e)
            {
                Assert.That(e, Is.SameAs(ex));
            }
        }