public void AnyActivityInstanceStateFindsClosed()
        {
            // Arrange
            var activity = new AddToNumOrThrow();
            dynamic args = new WorkflowArguments();
            args.Num = 2;
            var host = new WorkflowApplication(activity, args);
            var tracking = new ListTrackingParticipant();
            host.Extensions.Add(tracking);

            try
            {
                host.RunEpisode();

                // Act
                var any = tracking.Records.Any(ActivityInstanceState.Closed);

                // Assert
                Assert.IsTrue(any);
            }
            finally
            {
                tracking.Trace();
            }
        }
        public void FirstOrDefaultIdMatch()
        {
            // Arrange
            const string Id = "1";
            var activity = new AddToNumOrThrow();
            dynamic args = new WorkflowArguments();
            args.Num = 2;
            var host = new WorkflowApplication(activity, args);
            var tracking = new ListTrackingParticipant();
            host.Extensions.Add(tracking);

            try
            {
                host.RunEpisode();

                // Act
                var record = tracking.Records.FirstOrDefault(ActivityInstanceState.Executing, activityId: Id);

                // Assert
                Assert.IsNotNull(record);
                Assert.AreEqual(ActivityInstanceState.Executing, record.GetInstanceState());
            }
            finally
            {
                tracking.Trace();
            }
        }
        public void FirstOrDefaultDisplayNameNoMatchPastStartRecordNumber()
        {
            // Arrange
            const string DisplayName = "Assign";
            const long StartRecordNumber = 9;
            var activity = new AddToNumOrThrow();
            dynamic args = new WorkflowArguments();
            args.Num = 2;
            var host = new WorkflowApplication(activity, args);
            var tracking = new ListTrackingParticipant();
            host.Extensions.Add(tracking);

            try
            {
                host.RunEpisode();

                // Act
                var record = tracking.Records.FirstOrDefault(
                    ActivityInstanceState.Executing, DisplayName, startRecordNumber: StartRecordNumber);

                // Assert
                Assert.IsNull(record);
            }
            finally
            {
                tracking.Trace();
            }
        }
        public void FirstActivityInstanceStateFindsClosed()
        {
            // Arrange
            var activity = new AddToNumOrThrow();
            dynamic args = new WorkflowArguments();
            args.Num = 2;
            var host = new WorkflowApplication(activity, args);
            var tracking = new ListTrackingParticipant();
            host.Extensions.Add(tracking);

            try
            {
                host.RunEpisode();

                // Act
                var record = tracking.Records.First(ActivityInstanceState.Closed);

                // Assert
                Assert.IsNotNull(record);
                Assert.AreEqual(ActivityInstanceState.Closed, record.GetInstanceState());
            }
            finally
            {
                tracking.Trace();
            }
        }
        public void FirstOrDefaultActivityInstanceStateDoesNotFindFaulted()
        {
            // Arrange
            var activity = new AddToNumOrThrow();
            dynamic args = new WorkflowArguments();
            args.Num = 2;
            var host = new WorkflowApplication(activity, args);
            var tracking = new ListTrackingParticipant();
            host.Extensions.Add(tracking);

            host.RunEpisode();

            // Act
            var record = tracking.Records.FirstOrDefault(ActivityInstanceState.Faulted);

            // Assert
            Assert.IsNull(record);
        }
        public void SingleOrDefaultActivityInstanceStateThrowsOnClosed()
        {
            // Arrange
            var activity = new AddToNumOrThrow();
            dynamic args = new WorkflowArguments();
            args.Num = 2;
            var host = new WorkflowApplication(activity, args);
            var tracking = new ListTrackingParticipant();
            host.Extensions.Add(tracking);

            try
            {
                host.RunEpisode();

                // Act / Assert
                AssertHelper.Throws<InvalidOperationException>(
                    () => tracking.Records.SingleOrDefault(ActivityInstanceState.Closed));
            }
            finally
            {
                tracking.Trace();
            }
        }
        public void WithVariableValueThrowsNegStartRecordNumber()
        {
            // Arrange
            const long StartRecordNumber = -1;
            var activity = new AddToNumOrThrow();
            dynamic args = new WorkflowArguments();
            args.Num = 2;
            var host = new WorkflowApplication(activity, args);
            var tracking = new ListTrackingParticipant();
            host.Extensions.Add(tracking);

            try
            {
                host.RunEpisode();

                // Act / Assert
                AssertHelper.Throws<ArgumentOutOfRangeException>(
                    () => tracking.Records.OfType<ActivityStateRecord>().WithVariable("BAD", 1, StartRecordNumber));
            }
            finally
            {
                tracking.Trace();
            }
        }
        public void WhereIdNoMatchPastStartRecordNumber()
        {
            // Arrange
            const string Id = "Assign";
            const long StartRecordNumber = 9;
            var activity = new AddToNumOrThrow();
            dynamic args = new WorkflowArguments();
            args.Num = 2;
            var host = new WorkflowApplication(activity, args);
            var tracking = new ListTrackingParticipant();
            host.Extensions.Add(tracking);

            try
            {
                host.RunEpisode();

                // Act
                var query = tracking.Records.Where(
                    ActivityInstanceState.Executing, activityId: Id, startRecordNumber: StartRecordNumber);

                // Assert
                var list = query.ToList();
                Assert.AreEqual(0, list.Count());
            }
            finally
            {
                tracking.Trace();
            }
        }
        public void WithArgumentValueMatch()
        {
            // Arrange
            const string DisplayName = "AddToNumOrThrow";
            var activity = new AddToNumOrThrow();
            dynamic args = new WorkflowArguments();
            args.Num = 2;
            var host = new WorkflowApplication(activity, args);
            var tracking = new ListTrackingParticipant();
            host.Extensions.Add(tracking);

            try
            {
                host.RunEpisode();

                // Act
                var query = tracking.Records.OfType<ActivityStateRecord>().WithArgument("Num", 2);

                // Assert
                var list = query.ToList();
                Assert.AreEqual(2, list.Count());
                var first = list.First();
                Assert.IsNotNull(first);
                Assert.AreEqual(DisplayName, first.Activity.Name);
                Assert.AreEqual(ActivityInstanceState.Executing, first.GetInstanceState());
                var last = list.Last();
                Assert.IsNotNull(last);
                Assert.AreEqual(DisplayName, last.Activity.Name);
                Assert.AreEqual(ActivityInstanceState.Closed, last.GetInstanceState());
            }
            finally
            {
                tracking.Trace();
            }
        }
        public void WhereDisplayNameMatch()
        {
            // Arrange
            const string DisplayName = "AddToNumOrThrow";
            var activity = new AddToNumOrThrow();
            dynamic args = new WorkflowArguments();
            args.Num = 2;
            var host = new WorkflowApplication(activity, args);
            var tracking = new ListTrackingParticipant();
            host.Extensions.Add(tracking);

            try
            {
                host.RunEpisode();

                // Act
                var query = tracking.Records.Where(ActivityInstanceState.Executing, DisplayName);

                // Assert
                var list = query.ToList();
                Assert.AreEqual(1, list.Count());
                var activityStateRecord = list.FirstOrDefault();
                Assert.IsNotNull(activityStateRecord);
                Assert.AreEqual(DisplayName, activityStateRecord.Activity.Name);
                Assert.AreEqual(ActivityInstanceState.Executing, activityStateRecord.GetInstanceState());
            }
            finally
            {
                tracking.Trace();
            }
        }
        public void WhereIdAndStartRecordNumber()
        {
            // Arrange
            const string Id = "1.9";
            const long StartRecordNumber = 5;
            var activity = new AddToNumOrThrow();
            dynamic args = new WorkflowArguments();
            args.Num = 2;
            var host = new WorkflowApplication(activity, args);
            var tracking = new ListTrackingParticipant();
            host.Extensions.Add(tracking);

            try
            {
                host.RunEpisode();

                // Act
                var query = tracking.Records.Where(
                    ActivityInstanceState.Executing, activityId: Id, startRecordNumber: StartRecordNumber);

                // Assert
                var list = query.ToList();
                Assert.AreEqual(1, list.Count());
                var activityStateRecord = list.FirstOrDefault();
                Assert.IsNotNull(activityStateRecord);
                Assert.AreEqual(Id, activityStateRecord.Activity.Id);
                Assert.AreEqual(ActivityInstanceState.Executing, activityStateRecord.GetInstanceState());
            }
            finally
            {
                tracking.Trace();
            }
        }
        public void WhereActivityInstanceStateFindsClosed()
        {
            // Arrange
            var activity = new AddToNumOrThrow();
            dynamic args = new WorkflowArguments();
            args.Num = 2;
            var host = new WorkflowApplication(activity, args);
            var tracking = new ListTrackingParticipant();
            host.Extensions.Add(tracking);

            try
            {
                host.RunEpisode();

                // Act
                var query = tracking.Records.Where(ActivityInstanceState.Closed);

                // Assert
                var list = query.ToList();
                Assert.AreEqual(4, list.Count());
                Assert.IsFalse(
                    list.Any(record => record != null && record.GetInstanceState() != ActivityInstanceState.Closed),
                    "Found record where the state is not closed");
            }
            finally
            {
                tracking.Trace();
            }
        }
        public void WhereActivityInstanceStateDoesNotFindFaulted()
        {
            // Arrange
            var activity = new AddToNumOrThrow();
            dynamic args = new WorkflowArguments();
            args.Num = 2;
            var host = new WorkflowApplication(activity, args);
            var tracking = new ListTrackingParticipant();
            host.Extensions.Add(tracking);

            try
            {
                host.RunEpisode();

                // Act
                var query = tracking.Records.Where(ActivityInstanceState.Faulted);

                // Assert
                Assert.AreEqual(0, query.Count());
            }
            finally
            {
                tracking.Trace();
            }
        }
        public void AnyDisplayNameMatchWithStartRecordNumber()
        {
            // Arrange
            const string DisplayName = "Assign";
            const string Id = "1.9";
            const long StartRecordNumber = 5;
            var activity = new AddToNumOrThrow();
            dynamic args = new WorkflowArguments();
            args.Num = 2;
            var host = new WorkflowApplication(activity, args);
            var tracking = new ListTrackingParticipant();
            host.Extensions.Add(tracking);

            try
            {
                host.RunEpisode();

                // Act
                var any = tracking.Records.Any(ActivityInstanceState.Executing, DisplayName, Id, StartRecordNumber);

                // Assert
                Assert.IsTrue(any);
            }
            finally
            {
                tracking.Trace();
            }
        }
        public void FirstThrowsNegStartRecordNumber()
        {
            // Arrange
            const string DisplayName = "Assign";
            const long StartRecordNumber = -1;
            var activity = new AddToNumOrThrow();
            dynamic args = new WorkflowArguments();
            args.Num = 2;
            var host = new WorkflowApplication(activity, args);
            var tracking = new ListTrackingParticipant();
            host.Extensions.Add(tracking);

            try
            {
                host.RunEpisode();

                // Act / Assert
                AssertHelper.Throws<ArgumentOutOfRangeException>(
                    () =>
                    tracking.Records.First(
                        ActivityInstanceState.Executing, DisplayName, startRecordNumber: StartRecordNumber));
            }
            finally
            {
                tracking.Trace();
            }
        }
        public void WithVariableValueStartMatch()
        {
            // Arrange
            const string DisplayName = "Sequence";
            var activity = new AddToNumOrThrow();
            dynamic args = new WorkflowArguments();
            args.Num = 2;
            var host = new WorkflowApplication(activity, args);
            var tracking = new ListTrackingParticipant();
            host.Extensions.Add(tracking);

            try
            {
                host.RunEpisode();

                // Act
                // Find all ActivityStateRecords that have an variable named "Num" starting with record 7
                var query = tracking.Records.OfType<ActivityStateRecord>().WithVariable("VarNum", 1, 7);

                // Assert
                var list = query.ToList();
                Assert.AreEqual(1, list.Count());
                var first = list.First();
                Assert.IsNotNull(first);
                Assert.AreEqual(DisplayName, first.Activity.Name);
                Assert.AreEqual(ActivityInstanceState.Closed, first.GetInstanceState());
            }
            finally
            {
                tracking.Trace();
            }
        }
        public void LastActivityInstanceStateDoesNotFindFaulted()
        {
            // Arrange
            var activity = new AddToNumOrThrow();
            dynamic args = new WorkflowArguments();
            args.Num = 2;
            var host = new WorkflowApplication(activity, args);
            var tracking = new ListTrackingParticipant();
            host.Extensions.Add(tracking);

            host.RunEpisode();

            // Act / Assert
            AssertHelper.Throws<InvalidOperationException>(() => tracking.Records.Last(ActivityInstanceState.Faulted));
        }
        public void SingleDisplayNameMatchWithStartRecordNumber()
        {
            // Arrange
            const string DisplayName = "Assign";
            const long StartRecordNumber = 5;
            var activity = new AddToNumOrThrow();
            dynamic args = new WorkflowArguments();
            args.Num = 2;
            var host = new WorkflowApplication(activity, args);
            var tracking = new ListTrackingParticipant();
            host.Extensions.Add(tracking);

            try
            {
                host.RunEpisode();

                // Act
                var record = tracking.Records.Single(
                    ActivityInstanceState.Executing, DisplayName, startRecordNumber: StartRecordNumber);

                // Assert
                Assert.IsNotNull(record);
                Assert.AreEqual(ActivityInstanceState.Executing, record.GetInstanceState());
            }
            finally
            {
                tracking.Trace();
            }
        }