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 ShouldHostService() { var trackingProfile = new TrackingProfile { Queries = { new ActivityStateQuery { ActivityName = "ReceiveRequest", States = { "Executing" }, }, new ActivityStateQuery { ActivityName = "SendResponse", States = { "Executing" }, }, } }; var xamlInjector = new XamlInjector("TestSumService.xamlx"); // The first TestActivity1 will not be replaced - will add 1 to sum // Replace the second TestActivity1 with TestActivity2 - will add 2 to sum xamlInjector.ReplaceAt(1, typeof(TestActivity1), typeof(TestActivity2)); // Replace third TestActivity1 with TestActivity3 - will add 3 to sum xamlInjector.ReplaceAt(2, typeof(TestActivity1), typeof(TestActivity3)); // Replace all (2) TestActivity4 with TestActivity5 - will add 10 to sum xamlInjector.ReplaceAll(typeof(TestActivity4), typeof(TestActivity5)); // Response will be (data=1)+1+2+3+10 = 17 var serviceAddress = ServiceTest.GetUniqueEndpointAddress(); using (var testHost = new WorkflowServiceTestHost(xamlInjector.GetWorkflowService(), serviceAddress)) { testHost.Tracking.TrackingProfile = trackingProfile; testHost.Open(); var client = ChannelFactory<ITestService>.CreateChannel(ServiceTest.Pipe, serviceAddress); var response = client.GetData(1); Assert.AreEqual("17", response); testHost.Close(); // Find the tracking records for the ReceiveRequest and SendResponse // Activity <ReceiveRequest> state is Executing AssertTracking.ExistsAt(testHost.Tracking.Records, 0, "ReceiveRequest", ActivityInstanceState.Executing); // Activity <SendResponse> state is Executing AssertTracking.ExistsAt(testHost.Tracking.Records, 1, "SendResponse", ActivityInstanceState.Executing); } }
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 CustomStubShouldBeInvoked() { // Arrange var xamlInjector = new XamlInjector(Constants.ActivityWithTwoReceivesXaml); xamlInjector.ReplaceAll(typeof(Receive), typeof(ReceiveStub)); var sut = xamlInjector.GetActivity(); var host = WorkflowInvokerTest.Create(sut); var stubExtension = new TestMessagingStubExtensionImplementation(); host.Extensions.Add(stubExtension); stubExtension.EnqueueReceive(this.serviceContractName, "Operation1", null); stubExtension.EnqueueReceive(this.serviceContractName, "Operation2", null); try { // Act host.TestActivity(); // Assert Assert.IsTrue(stubExtension.Operation1Invoked); Assert.IsTrue(stubExtension.Operation2Invoked); } 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(); } }
public void CanMockReceiveAndSendReply() { // Arrange const int Expected = 123; var xamlInjector = new XamlInjector(Constants.DefaultServiceXamlx); // 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", 123); try { // Act host.TestActivity(); // Assert Assert.AreEqual(Expected.ToString(), stubExtension.Messages[1].Content); } finally { Trace.WriteLine("*** Messaging Stub Dump"); stubExtension.Trace(); Trace.WriteLine("\r\n*** Workflow Tracking Records"); host.Tracking.Trace(); } }
/// <summary> /// The verify injection. /// </summary> /// <param name="xamlInjector"> /// The xaml injector. /// </param> private void VerifyInjection(XamlInjector xamlInjector) { // The first TestActivity1 will not be replaced - will add 1 to sum // Replace the second TestActivity1 with TestActivity2 - will add 2 to sum xamlInjector.ReplaceAt(1, typeof(TestActivity1), typeof(TestActivity2)); // Replace third TestActivity1 with TestActivity3 - will add 3 to sum xamlInjector.ReplaceAt(2, typeof(TestActivity1), typeof(TestActivity3)); // Replace all (2) TestActivity4 with TestActivity5 - will add 10 to sum xamlInjector.ReplaceAll(typeof(TestActivity4), typeof(TestActivity5)); // Debug.WriteLine(string.Format("Invoking Injected XAML activity {0}", activity.GetType())); var host = new WorkflowInvokerTest(xamlInjector.GetActivity()); try { // Act host.TestActivity(); // Total should be 1+2+3+10=16 host.AssertOutArgument.AreEqual("sum", 16); } finally { host.Tracking.Trace(); } }
public void ShouldReplaceWriteLineActivity() { // Arrange var xamlInjector = new XamlInjector("TestInjectWriteLine.xaml", typeof(TestActivity1).Assembly); xamlInjector.ReplaceAll(typeof(TestActivity1), typeof(TestActivity2)); xamlInjector.ReplaceAll(typeof(WriteLine), typeof(MockWriteLine)); var activity = xamlInjector.GetActivity(); var invokerTest = new WorkflowInvokerTest(activity); try { // Act invokerTest.TestActivity(); // Assert invokerTest.Tracking.Assert.Exists("MockWriteLine", ActivityInstanceState.Closed); } finally { invokerTest.Tracking.Trace(); } }