Пример #1
0
        public void ObjectsHaveNoPropertyToDeserializeSoIgnoringThemInsteadOfThrowing()
        {
            var functionResult = new FunctionResult()
            {
                PublicMember   = new JObject(),
                InternalMember = new JObject()
            };

            var sender           = new object();
            var context          = new object();
            var publicMember     = new object();
            var internalMember   = new object();
            var functionsManager = new FunctionsManager("component", "statemachine");

            functionsManager.AddTask(new object(),
                                     publicMember,
                                     internalMember,
                                     context,
                                     sender);

            Assert.DoesNotThrow(() =>
            {
                functionsManager.ApplyFunctionResult(functionResult,
                                                     publicMember,
                                                     internalMember,
                                                     context,
                                                     sender);
            });
        }
Пример #2
0
        public void AddTaskResultShouldThrowValidationExceptionWhenInvalidRequestId()
        {
            var sender = new object();

            var functionResult = new FunctionResult()
            {
                RequestId = "unknown request id"
            };

            var functionsManager = new FunctionsManager("component", "statemachine");

            functionsManager.AddTask(new object(), new object(), new object(), new object(), sender, "function");

            while (true)
            {
                var postedTask = functionsManager.GetTask();
                if (postedTask != null)
                {
                    break;
                }
                Thread.Sleep(TimeSpan.FromSeconds(1));
            }

            var exception = Assert.Throws <ValidationException>(()
                                                                => functionsManager.AddTaskResult(functionResult));

            Assert.IsTrue(exception.Message.Contains("unknown request id"),
                          "Exception message should show where is the problem");
        }
Пример #3
0
        public void ApplyPostedResultShouldWorkWithJObjects()
        {
            var publicMemberBefore = new PublicMember()
            {
                State = "before"
            };

            var publicMemberAfter = new JObject();

            publicMemberAfter.Add("State", new JValue("after"));

            var functionResult = new FunctionResult()
            {
                PublicMember = publicMemberAfter,
            };

            var sender           = new object();
            var context          = new object();
            var internalMember   = new object();
            var functionsManager = new FunctionsManager("component", "statemachine");

            functionsManager.AddTask(new object(),
                                     publicMemberBefore,
                                     internalMember,
                                     context,
                                     sender);

            functionsManager.ApplyFunctionResult(functionResult,
                                                 publicMemberBefore,
                                                 new object(),
                                                 context,
                                                 sender);

            Assert.AreEqual("after", publicMemberBefore.State);
        }
Пример #4
0
        public void ApplyFunctionResultShouldThrowValidationExceptionWhenDeserializationFails()
        {
            var sender             = new object();
            var publicMemberBefore = new PublicMember()
            {
                State = "before"
            };

            var publicMemberAfter = new JObject();

            publicMemberAfter.Add("State", new JValue("after"));

            var functionResult = new FunctionResult()
            {
                PublicMember = "Hello",
            };

            var functionsManager = new FunctionsManager("component", "statemachine");

            functionsManager.AddTask(new object(), new object(), new object(), new object(), sender, "function");

            while (true)
            {
                var postedTask = functionsManager.GetTask();
                if (postedTask != null)
                {
                    break;
                }
                Thread.Sleep(TimeSpan.FromSeconds(1));
            }

            var exception = Assert.Throws <ValidationException>(() => functionsManager.ApplyFunctionResult(functionResult,
                                                                                                           publicMemberBefore,
                                                                                                           new object(),
                                                                                                           new object(),
                                                                                                           sender));

            Assert.IsTrue(exception.Message.Contains("Hello"),
                          "Exception message should show where is the problem");
        }
Пример #5
0
        public void ApplyFunctionResultShouldThrowInvalidSenderValidationException()
        {
            var sender         = new object();
            var functionResult = new FunctionResult()
            {
                Senders = new List <SenderResult>()
                {
                    new SenderResult
                    {
                        SenderName      = "UnknownSender",
                        SenderParameter = "{ \"Value\": \"undo\" }",
                        UseContext      = false
                    },
                },
            };

            var functionsManager = new FunctionsManager("component", "statemachine");

            functionsManager.AddTask(new object(), new object(), new object(), new object(), sender, "function");

            while (true)
            {
                var postedTask = functionsManager.GetTask();
                if (postedTask != null)
                {
                    break;
                }
                Thread.Sleep(TimeSpan.FromSeconds(1));
            }

            var exception = Assert.Throws <ValidationException>(() => functionsManager.ApplyFunctionResult(functionResult,
                                                                                                           new object(),
                                                                                                           new object(),
                                                                                                           new object(),
                                                                                                           sender));

            Assert.IsTrue(exception.Message.Contains("UnknownSender"),
                          "Exception message should show where is the problem");
        }
Пример #6
0
        public void AddTaskShouldPutTaskOnQueue()
        {
            var functionsManager = new FunctionsManager("component", "statemachine");

            var xcEvent        = new object();
            var publicMember   = new object();
            var internalMember = new object();
            var context        = new object();
            var sender         = new object();

            functionsManager.AddTask(xcEvent, publicMember, internalMember, context, sender, "function");

            var enqueuedParameter = functionsManager.GetTask();

            Assert.AreEqual(xcEvent, enqueuedParameter.Event);
            Assert.AreEqual(publicMember, enqueuedParameter.PublicMember);
            Assert.AreEqual(internalMember, enqueuedParameter.InternalMember);
            Assert.AreEqual(context, enqueuedParameter.Context);
            Assert.AreEqual("component", enqueuedParameter.ComponentName);
            Assert.AreEqual("statemachine", enqueuedParameter.StateMachineName);
            Assert.AreEqual("function", enqueuedParameter.FunctionName);
            Assert.IsNotNull(enqueuedParameter.RequestId);
        }
Пример #7
0
        public async Task AddTaskShouldApplyPostedResultAndCallSenders()
        {
            var functionsManager = new FunctionsManager("component", "statemachine");

            var xcEvent      = new object();
            var publicMember = new PublicMember()
            {
                State = "before"
            };
            var internalMember = new object();
            var context        = new object();
            var sender         = Substitute.For <ISender>();

            var task = functionsManager.AddTask(xcEvent, publicMember, internalMember, context, sender, "function");

            Task.Run(() =>
            {
                while (true)
                {
                    var postedTask = functionsManager.GetTask();
                    if (postedTask != null)
                    {
                        var functionResult = new FunctionResult()
                        {
                            ComponentName    = "component",
                            StateMachineName = "statemachine",
                            PublicMember     = "{ \"State\": \"after\" }",
                            Senders          = new List <SenderResult>
                            {
                                new SenderResult
                                {
                                    SenderName      = "Do",
                                    SenderParameter = "{ \"Value\": \"do\" }",
                                    UseContext      = true
                                },
                                new SenderResult
                                {
                                    SenderName      = "Undo",
                                    SenderParameter = "{ \"Value\": \"undo\" }",
                                    UseContext      = false
                                },
                                new SenderResult
                                {
                                    SenderName = "Reply",
                                    UseContext = true
                                }
                            },
                            RequestId = postedTask.RequestId,
                        };
                        functionsManager.AddTaskResult(functionResult);

                        break;
                    }
                    Thread.Sleep(TimeSpan.FromSeconds(1));
                }
            });

            await task;

            Assert.AreEqual("after", publicMember.State);
            sender.Received().Do(context, Arg.Is <DoEvent>(evt => evt.Value == "do"), null);
            sender.Received().SendEvent(Arg.Is <UndoEvent>(evt => evt.Value == "undo"));
            sender.Received().Reply(context, Arg.Any <object>(), null);
        }