Example #1
0
        public void CanMockReceiveAndSendReply()
        {
            // Arrange
            const int Expected = 123;
            var xamlInjector = new XamlInjector(MockingMessagingActivities + "\\" + Service1Xamlx);

            // Setup the XamlInjector to replace the receive / send activities
            xamlInjector.ReplaceAll(typeof(Receive), typeof(ReceiveStub));
            xamlInjector.ReplaceAll(typeof(SendReply), typeof(SendReplyStub));

            // Access the workflow service Body activity for testing with WorkflowInvoker
            var host = WorkflowInvokerTest.Create(xamlInjector.GetWorkflowService().Body);

            // Setup the extension
            var stubExtension = new MessagingStubExtension();
            stubExtension.EnqueueReceive(this.serviceContractName, "GetData", Expected);
            host.Extensions.Add(stubExtension);

            try
            {
                host.TestActivity();

                // The reply should be "123"
                Assert.AreEqual(Expected.ToString(CultureInfo.InvariantCulture), stubExtension.Messages[1].Content);
            }
            finally
            {
                Trace.WriteLine("*** Messaging Stub Dump");
                stubExtension.Trace();

                Trace.WriteLine("\r\n*** Workflow Tracking Records");
                host.Tracking.Trace();
            }
        }
        public void CanImplementMessagingProtocol()
        {
            // Arrange
            const int Expected = 123;
            const int Increment = 3;
            var xamlInjector = new XamlInjector(Constants.ServicewithtworeceivesandincrementXamlx);

            // Setup the XamlInjector to replace the receive / send activities
            xamlInjector.ReplaceAll(typeof(Receive), typeof(ReceiveStub));
            xamlInjector.ReplaceAll(typeof(SendReply), typeof(SendReplyStub));

            // Access the workflow service Body activity for testing with WorkflowInvoker
            var host = WorkflowInvokerTest.Create(xamlInjector.GetWorkflowService().Body);

            // Setup the extension
            var stubExtension = new MessagingStubExtension();
            host.Extensions.Add(stubExtension);

            stubExtension.EnqueueReceive(this.serviceContractName, "GetData", Expected);
            stubExtension.OnIdle = s =>
                {
                    // If processing the reply to the initial message
                    if (s.Messages.Count == 2 && s.QueueCount == 0)
                    {
                        // Access the content and add the increment
                        s.EnqueueReceive(
                            this.serviceContractName, "IncrementData", ((int)s.Messages[1].Content) + Increment);
                    }
                };

            try
            {
                // Act
                host.TestActivity();

                // Assert
                Assert.AreEqual(Expected + Expected + Increment, stubExtension.Messages[3].Content);
                Assert.AreEqual(0, stubExtension.QueueCount);
            }
            finally
            {
                Trace.WriteLine("*** Messaging Stub Dump");
                stubExtension.Trace();

                Trace.WriteLine("\r\n*** Workflow Tracking Records");
                host.Tracking.Trace();
            }
        }
        public void WhenSendWithParametersContentStubInvoked()
        {
            var xamlInjector = new XamlInjector(Constants.ActivityWithSendParametersContentXaml);
            xamlInjector.ReplaceAll(typeof(Send), typeof(SendStub));
            var sut = xamlInjector.GetActivity();
            var host = new WorkflowInvokerTest(sut);
            var stubExtension = new MessagingStubExtension();
            host.Invoker.Extensions.Add(stubExtension);

            try
            {
                host.TestActivity();

                // Shows that the SendStub activity was used
                host.Tracking.Assert.Exists("SendStub", ActivityInstanceState.Closed);
                host.Tracking.Assert.DoesNotExist("Send", ActivityInstanceState.Closed);

                // There should be two send activities
                Assert.AreEqual(2, stubExtension.Messages.Count);

                // Verify the parameters content from the first send
                Assert.AreEqual(1, stubExtension.Messages[0].Parameter("num"));
                Assert.AreEqual("test1", stubExtension.Messages[0].Parameter("test"));

                // Verify the parameters content from the second send
                Assert.AreEqual(2, stubExtension.Messages[1].Parameter("num"));
                Assert.AreEqual("test2", stubExtension.Messages[1].Parameter("test"));
            }
            finally
            {
                Trace.WriteLine("*** Messaging Stub Dump");
                stubExtension.Trace();

                Trace.WriteLine("\r\n*** Workflow Tracking Records");
                host.Tracking.Trace();
            }
        }
        public void WhenSendWithNoContentStubInvoked()
        {
            var xamlInjector = new XamlInjector(Constants.ActivityWithSendXaml);
            xamlInjector.ReplaceAll(typeof(Send), typeof(SendStub));
            var sut = xamlInjector.GetActivity();
            var host = new WorkflowInvokerTest(sut);
            var stubExtension = new MessagingStubExtension();
            host.Invoker.Extensions.Add(stubExtension);
            try
            {
                host.TestActivity();

                // Shows that the SendStub activity was used
                host.Tracking.Assert.Exists("SendStub", ActivityInstanceState.Closed);
                host.Tracking.Assert.DoesNotExist("Send", ActivityInstanceState.Closed);
                Assert.AreEqual(1, stubExtension.Messages.Count);
                Assert.AreEqual(1, stubExtension.Messages[0].Content);
            }
            finally
            {
                Trace.WriteLine("*** Messaging Stub Dump");
                stubExtension.Trace();

                Trace.WriteLine("\r\n*** Workflow Tracking Records");
                host.Tracking.Trace();
            }
        }
        public void WhenSendStubInvokedSimulatesCommunicationException()
        {
            // Arrange
            var xamlInjector = new XamlInjector(Constants.ActivityWithSendCatchCommExceptionXaml);
            xamlInjector.ReplaceAll(typeof(Send), typeof(SendStub));
            var sut = xamlInjector.GetActivity();
            var host = new WorkflowInvokerTest(sut);

            var stubExtension = new MessagingStubExtension();

            // Add an implementation that will simulate a bad URI for the activity
            // Note: If the DisplayName is not set, the name will be the name of the SendStub activity
            stubExtension.SetImplementation("SendStub", () => { throw new EndpointNotFoundException(); });
            host.Extensions.Add(stubExtension);

            try
            {
                // Act
                host.TestActivity();

                // Assert
                // Shows that the SendStub activity was used
                host.Tracking.Assert.Exists("SendStub", ActivityInstanceState.Executing);
                host.Tracking.Assert.DoesNotExist("Send", ActivityInstanceState.Closed);
                host.AssertOutArgument.IsTrue("CatchHandled");
                host.AssertOutArgument.IsInstanceOfType("CaughtException", typeof(EndpointNotFoundException));
            }
            finally
            {
                Trace.WriteLine("*** Messaging Stub Dump");
                stubExtension.Trace();

                Trace.WriteLine("\r\n*** Workflow Tracking Records");
                host.Tracking.Trace();
            }
        }
        public void StubCorrelationActivities()
        {
            // Arrange
            const int Expected = 12;
            var xamlInjector = new XamlInjector(Constants.ActivityWithReceiveAndSendReplyAndCorrXaml);
            xamlInjector.ReplaceAll(typeof(Receive), typeof(ReceiveStub));
            xamlInjector.ReplaceAll(typeof(SendReply), typeof(SendReplyStub));
            xamlInjector.ReplaceAll(typeof(InitializeCorrelation), typeof(InitializeCorrelationStub));
            var host = WorkflowInvokerTest.Create(xamlInjector.GetActivity());

            var stubExtension = new MessagingStubExtension();
            stubExtension.EnqueueReceive(
                this.serviceContractName, "Sum", new Dictionary<string, object> { { "x", 5 }, { "y", 7 } });
            host.Extensions.Add(stubExtension);

            try
            {
                // Act
                host.TestActivity();

                // Assert
                host.Tracking.Assert.Exists("ReceiveStub", ActivityInstanceState.Closed);
                host.Tracking.Assert.Exists("SendReplyToReceive", ActivityInstanceState.Closed);
                host.Tracking.Assert.Exists(WorkflowInstanceRecordState.Idle);
                host.Tracking.Assert.Exists(WorkflowInstanceRecordState.Completed);
                host.AssertOutArgument.AreEqual("sum", Expected);
            }
            finally
            {
                Trace.WriteLine("*** Messaging Stub Dump");
                stubExtension.Trace();

                Trace.WriteLine("\r\n*** Workflow Tracking Records");
                host.Tracking.Trace();
            }
        }
        public void SendAndReceiveReplyStubMessageImplementationShouldSetSum()
        {
            // Arrange
            const int Expected = 12;
            var xamlInjector = new XamlInjector(Constants.ActivityWithSendAndReceiveReplyXaml);

            // Replace the messaging activities with stubs
            xamlInjector.ReplaceAll(typeof(ReceiveReply), typeof(ReceiveReplyStub));
            xamlInjector.ReplaceAll(typeof(Send), typeof(SendStub));

            var host = WorkflowInvokerTest.Create(xamlInjector.GetActivity());

            // Setup the extension
            var stubExtension = new MessagingStubExtension();

            // For the reply use the name of the contract / operation from the matching Send activity
            stubExtension.EnqueueReceiveReply(
                this.serviceContractName, "Sum", new Dictionary<string, object> { { "SumResult", Expected } });
            host.Extensions.Add(stubExtension);

            try
            {
                // Act
                host.TestActivity(Constants.Timeout);

                // Assert
                host.Tracking.Assert.Exists("SendStub", ActivityInstanceState.Closed);
                host.Tracking.Assert.Exists("ReceiveReplyForSend", ActivityInstanceState.Closed);
                host.Tracking.Assert.Exists(WorkflowInstanceRecordState.Idle);
                host.Tracking.Assert.Exists(WorkflowInstanceRecordState.Completed);
                host.AssertOutArgument.AreEqual("sum", Expected);

                Assert.AreEqual(2, stubExtension.Messages.Count);
                Assert.AreEqual(5, stubExtension.Messages[0].Parameter("x"));
                Assert.AreEqual(7, stubExtension.Messages[0].Parameter("y"));
            }
            finally
            {
                Trace.WriteLine("*** Messaging Stub Dump");
                stubExtension.Trace();

                Trace.WriteLine("\r\n*** Workflow Tracking Records");
                host.Tracking.Trace();
            }
        }
        public void ReceiveStubShouldGoIdleWithBookmark()
        {
            // Arrange
            var xamlInjector = new XamlInjector(Constants.ActivityWithReceiveXaml);
            xamlInjector.ReplaceAll(typeof(Receive), typeof(ReceiveStub));
            var host = WorkflowApplicationTest.Create(xamlInjector.GetActivity());
            var stubExtension = new MessagingStubExtension();
            host.Extensions.Add(stubExtension);

            try
            {
                // Act
                // Run until idle with this bookmark
                host.TestWorkflowApplication.RunEpisode("{http://tempuri.org/}IService|Sum");

                // Assert
                host.Tracking.Assert.Exists("ReceiveStub", ActivityInstanceState.Executing);
                host.Tracking.Assert.Exists(WorkflowInstanceRecordState.Idle);
            }
            finally
            {
                Trace.WriteLine("*** Messaging Stub Dump");
                stubExtension.Trace();

                Trace.WriteLine("\r\n*** Workflow Tracking Records");
                host.Tracking.Trace();
            }
        }
        public void MessagingStubImplementationShouldSupplyMessageResults()
        {
            // Arrange
            const int Expected = 12;
            var xamlInjector = new XamlInjector(Constants.ActivityWithReceiveMessageXaml);
            xamlInjector.ReplaceAll(typeof(Receive), typeof(ReceiveStub));
            var host = WorkflowInvokerTest.Create(xamlInjector.GetActivity());

            var stubExtension = new MessagingStubExtension();
            stubExtension.EnqueueReceive(this.serviceContractName, "Sum", 5);
            host.Extensions.Add(stubExtension);

            try
            {
                // Act
                host.TestActivity();

                // Assert
                host.Tracking.Assert.Exists("ReceiveStub", ActivityInstanceState.Closed);
                host.Tracking.Assert.Exists(WorkflowInstanceRecordState.Idle);
                host.AssertOutArgument.AreEqual("sum", Expected);
            }
            finally
            {
                Trace.WriteLine("*** Messaging Stub Dump");
                stubExtension.Trace();

                Trace.WriteLine("\r\n*** Workflow Tracking Records");
                host.Tracking.Trace();
            }
        }
        public void CanSendToAnyParallelBranch()
        {
            var xamlInjector = new XamlInjector(Constants.ServiceWithParallelThreeReceivesXamlx);
            xamlInjector.ReplaceAll(typeof(Receive), typeof(ReceiveStub));
            xamlInjector.ReplaceAll(typeof(SendReply), typeof(SendReplyStub));
            var host = new WorkflowInvokerTest(xamlInjector.GetWorkflowService().Body);
            var stubExtension = new MessagingStubExtension();
            host.Invoker.Extensions.Add(stubExtension);

            stubExtension.EnqueueReceive(this.serviceContractName, "GetData3", 3);
            stubExtension.EnqueueReceive(this.serviceContractName, "GetData1", 1);
            stubExtension.EnqueueReceive(this.serviceContractName, "GetData2", 2);

            try
            {
                host.TestActivity();

                Assert.AreEqual(6, stubExtension.Messages.Count);
                Assert.AreEqual("3", stubExtension.Messages[1].Content);
                Assert.AreEqual("1", stubExtension.Messages[3].Content);
                Assert.AreEqual("2", stubExtension.Messages[5].Content);
            }
            finally
            {
                Trace.WriteLine("*** Messaging Stub Dump");
                stubExtension.Trace();

                Trace.WriteLine("\r\n*** Workflow Tracking Records");
                host.Tracking.Trace();
            }
        }
Example #11
0
        public void CanSimulateSendFailure()
        {
            // Arrange
            var xamlInjector = new XamlInjector(@"MockingMessagingActivities\ServiceWithSend.xamlx");

            // Setup the XamlInjector to replace the receive / send activities
            xamlInjector.ReplaceAll(typeof(Receive), typeof(ReceiveStub));
            xamlInjector.ReplaceAll(typeof(SendReply), typeof(SendReplyStub));
            xamlInjector.ReplaceAll(typeof(Send), typeof(SendStub));
            xamlInjector.ReplaceAll(typeof(ReceiveReply), typeof(ReceiveReplyStub));

            // Access the workflow service Body activity for testing with WorkflowInvoker
            var host = WorkflowInvokerTest.Create(xamlInjector.GetWorkflowService().Body);

            // Setup the extension
            var stubExtension = new MessagingStubExtension();

            // The first receive will start the process
            stubExtension.EnqueueReceive(this.serviceContractName, "GetData", 123);

            // The first send will result in an exception
            stubExtension.SetImplementation("Send Primary", () => { throw new EndpointNotFoundException(); });

            // The second send should succeed
            // The next receive reply should simulate data from the backup service
            stubExtension.EnqueueReceiveReply(this.serviceContractName, "GetData", BackupResponse);
            host.Extensions.Add(stubExtension);

            try
            {
                host.TestActivity();

                // Assert
                // The final reply is "Backup 123"
                Assert.AreEqual(BackupResponse, stubExtension.Messages[4].Content);
            }
            finally
            {
                Trace.WriteLine("*** Messaging Stub Dump");
                stubExtension.Trace();

                Trace.WriteLine("\r\n*** Workflow Tracking Records");
                host.Tracking.Trace();
            }
        }
Example #12
0
        public void CanMockTwoReceiveAndSendReplyWithParameters()
        {
            // Arrange
            var xamlInjector = new XamlInjector(@"MockingMessagingActivities\Service2Parameters.xamlx");

            // Setup the XamlInjector to replace the receive / send activities
            xamlInjector.ReplaceAll(typeof(Receive), typeof(ReceiveStub));
            xamlInjector.ReplaceAll(typeof(SendReply), typeof(SendReplyStub));

            // Access the workflow service Body activity for testing with WorkflowInvoker
            var host = WorkflowInvokerTest.Create(xamlInjector.GetWorkflowService().Body);

            // Setup the extension
            var stubExtension = new MessagingStubExtension();

            // Setup the first message
            dynamic parameterValues1 = new WorkflowArguments();
            parameterValues1.value1 = 5;
            parameterValues1.value2 = 4;
            stubExtension.EnqueueReceive(this.serviceContractName, "GetData", parameterValues1);

            // Setup the second message
            dynamic parameterValues2 = new WorkflowArguments();
            parameterValues2.value1 = 6;
            parameterValues2.value2 = 3;
            stubExtension.EnqueueReceive(this.serviceContractName, "GetData2", parameterValues2);

            host.Extensions.Add(stubExtension);

            try
            {
                host.TestActivity();

                // Assert
                // The first reply parameter "data1" is "5"
                Assert.AreEqual("5", stubExtension.Messages[1].Parameter("data1"));

                // The first reply parameter "data1" is "4"
                Assert.AreEqual("4", stubExtension.Messages[1].Parameter("data2"));

                // The second reply parameter "data1" is "7"
                Assert.AreEqual("7", stubExtension.Messages[3].Parameter("data1"));

                // The second reply parameter "data1" is "5"
                Assert.AreEqual("5", stubExtension.Messages[3].Parameter("data2"));
            }
            finally
            {
                Trace.WriteLine("*** Messaging Stub Dump");
                stubExtension.Trace();

                Trace.WriteLine("\r\n*** Workflow Tracking Records");
                host.Tracking.Trace();
            }
        }
Example #13
0
        public void CanMockTwoReceiveAndSendReply()
        {
            // Arrange
            var xamlInjector = new XamlInjector(@"MockingMessagingActivities\Service2.xamlx");

            // Setup the XamlInjector to replace the receive / send activities
            xamlInjector.ReplaceAll(typeof(Receive), typeof(ReceiveStub));
            xamlInjector.ReplaceAll(typeof(SendReply), typeof(SendReplyStub));

            // Access the workflow service Body activity for testing with WorkflowInvoker
            var host = WorkflowInvokerTest.Create(xamlInjector.GetWorkflowService().Body);

            // Setup the extension
            var stubExtension = new MessagingStubExtension();
            stubExtension.EnqueueReceive(this.serviceContractName, "GetData", 5);
            stubExtension.EnqueueReceive(this.serviceContractName, "GetData2", 6);
            host.Extensions.Add(stubExtension);

            try
            {
                host.TestActivity();

                // Assert
                // The first reply is "5"
                Assert.AreEqual("5", stubExtension.Messages[1].Content);

                // The second reply is "8"
                Assert.AreEqual("8", stubExtension.Messages[3].Content);
            }
            finally
            {
                Trace.WriteLine("*** Messaging Stub Dump");
                stubExtension.Trace();

                Trace.WriteLine("\r\n*** Workflow Tracking Records");
                host.Tracking.Trace();
            }
        }
Example #14
0
        public void CanMockReceiveAndSendReplyWithParameters()
        {
            // Arrange
            const int Value1 = 123;
            const int Value2 = 456;
            var xamlInjector = new XamlInjector(@"MockingMessagingActivities\Service1Parameters.xamlx");

            // Setup the XamlInjector to replace the receive / send activities
            xamlInjector.ReplaceAll(typeof(Receive), typeof(ReceiveStub));
            xamlInjector.ReplaceAll(typeof(SendReply), typeof(SendReplyStub));

            // Access the workflow service Body activity for testing with WorkflowInvoker
            var host = WorkflowInvokerTest.Create(xamlInjector.GetWorkflowService().Body);

            // Setup the extension
            dynamic arguments = new WorkflowArguments();
            arguments.value1 = Value1;
            arguments.value2 = Value2;

            var stubExtension = new MessagingStubExtension();
            stubExtension.EnqueueReceive(this.serviceContractName, "GetData", arguments);
            host.Extensions.Add(stubExtension);

            try
            {
                host.TestActivity();

                // The first reply message parameter data1 should be "123"
                Assert.AreEqual(Value1.ToString(CultureInfo.InvariantCulture), stubExtension.Messages[1].Parameter("data1"));

                // The first reply message parameter data2 should be "456"
                Assert.AreEqual(Value2.ToString(CultureInfo.InvariantCulture), stubExtension.Messages[1].Parameter("data2"));
            }
            finally
            {
                Trace.WriteLine("*** Messaging Stub Dump");
                stubExtension.Trace();

                Trace.WriteLine("\r\n*** Workflow Tracking Records");
                host.Tracking.Trace();
            }
        }