public void AttachNullWorkflowApplicationShouldThrow()
        {
            using (var testdb = new SqlWorkflowInstanceStoreTest())
            {
                // Arrange
                var store = new SqlWorkflowInstanceStore(testdb.ConnectionString);

                // Act / Assert
                AssertHelper.Throws<ArgumentNullException>(
                    () => StateMachineStateTracker.Attach((WorkflowApplication)null, store));
            }
        }
        public void ConfigureConfiguresInstanceStore()
        {
            using (var testdb = new SqlWorkflowInstanceStoreTest())
            {
                // Arrange
                testdb.Create();

                // Act
                testdb.ConfigureInstanceStore();

                // Assert
                testdb.AssertDatabaseIsConfigured();
            }
        }
        public void CurrentStateIsReported()
        {
            // Arrange
            using (var testdb = new SqlWorkflowInstanceStoreTest())
            {
                var view = new TestWorkflowView(testdb.CreateInstanceStore());
                var model = view.Model;

                // Act
                model.New();

                // Assert
                Assert.AreEqual(StateMachineExample.State1, model.CurrentState);
            }
        }
        public void CtorShouldCreateTracker()
        {
            // Arrange
            using (var testdb = new SqlWorkflowInstanceStoreTest())
            {
                var view = new TestWorkflowView(testdb.CreateInstanceStore());
                var model = new WorkflowModel(view);

                // Act
                var wi = new WorkflowInstance(model);

                // Assert
                Assert.IsNotNull(wi.StateTracker);
            }
        }
        public void CurrentStateIsNullWhenNew()
        {
            // Arrange
            using (var testdb = new SqlWorkflowInstanceStoreTest())
            {
                var view = new TestWorkflowView(testdb.CreateInstanceStore());
                var model = view.Model;

                // Act
                var state = model.CurrentState;

                // Assert
                Assert.IsNull(state);
            }
        }
        public void HostIsNotNull()
        {
            // Arrange
            using (var testdb = new SqlWorkflowInstanceStoreTest())
            {
                var view = new TestWorkflowView(testdb.CreateInstanceStore());
                var model = new WorkflowModel(view);
                var wi = new WorkflowInstance(model);

                // Act
                wi.New();

                // Assert
                Assert.IsNotNull(wi.Host);
            }
        }
        public void CtorDatabaseNameArg()
        {
            const string Expected = "CtorDatabaseNameArg";
            using (var testdb = new SqlWorkflowInstanceStoreTest(Expected))
            {
                // Arrange

                // Act
                var actual = testdb.DatabaseName;
                var dataSource = testdb.DataSource;

                // Assert
                Assert.IsNotNull(actual);
                Assert.AreEqual(Expected, actual);
                Assert.AreEqual(Constants.LocalSqlExpress, dataSource);
            }
        }
        public void ConfigureInstanceStoreWithEvilDbShouldThrow()
        {
            using (var testdb = new SqlWorkflowInstanceStoreTest())
            {
                // Arrange

                // Create a plain database without running the scripts
                testdb.Create();

                // Act
                SqlWorkflowInstanceStoreManager.ConfigureInstanceStore(
                    EvilDatabase, testdb.ConnectionString);

                // Assert
                testdb.AssertDatabaseIsConfigured();
            }
        }
        public void CtorDatabaseNameDataSourceArg()
        {
            const string DatabaseName = "DatabaseName";
            const string DataSource = "ServerName";
            using (var testdb = new SqlWorkflowInstanceStoreTest(DatabaseName, DataSource))
            {
                // Arrange

                // Act
                var actual = testdb.DatabaseName;
                var dataSource = testdb.DataSource;

                // Assert
                Assert.IsNotNull(actual);
                Assert.AreEqual(DatabaseName, actual);
                Assert.AreEqual(DataSource, dataSource);
            }
        }
        public void ConfigureInstanceStoreShouldConfigure()
        {
            using (var testdb = new SqlWorkflowInstanceStoreTest())
            {
                // Arrange

                // Create a plain database without running the scripts
                testdb.Create();

                // Act
                // Configure the database with the scripts
                SqlWorkflowInstanceStoreManager.ConfigureInstanceStore(
                    testdb.DatabaseName,
                    testdb.ConnectionString);

                // Assert
                testdb.AssertDatabaseIsConfigured();
            }
        }
        public void ViewExistsReturnsTrueWhenViewDoesExist()
        {
            using (var testdb = new SqlWorkflowInstanceStoreTest())
            {
                // Arrange
                testdb.CreateInstanceStore();

                // Act
                var actual = testdb.ViewExists("Instances");

                // Assert
                Assert.IsTrue(actual);
            }
        }
        public void UnloadShouldUnloadWorkflowApp()
        {
            using (var testdb = new SqlWorkflowInstanceStoreTest())
            {
                // Arrange
                var view = new TestWorkflowView(testdb.CreateInstanceStore());
                var model = new WorkflowModel(view);

                // Create and run the workflow
                model.New();

                // Act
                // Load the instances from the datbase
                var workflowInstance = model.CurrentInstance;
                workflowInstance.Unload();
                var isLoaded = workflowInstance.IsLoaded;

                // Assert
                Assert.IsFalse(isLoaded);
            }
        }
        public void ResumeShouldResume()
        {
            // Arrange
            using (var testdb = new SqlWorkflowInstanceStoreTest())
            {
                var view = new TestWorkflowView(testdb.CreateInstanceStore());
                var model = new WorkflowModel(view);
                var wi = new WorkflowInstance(model);
                wi.New();

                // Act
                wi.Resume(StateTrigger.T1);

                // Assert
                Assert.AreEqual(StateMachineExample.State2, wi.StateTracker.CurrentState);
            }
        }
        public void IsLoadedReturnsTrueOnNew()
        {
            // Arrange
            using (var testdb = new SqlWorkflowInstanceStoreTest())
            {
                var view = new TestWorkflowView(testdb.CreateInstanceStore());
                var model = new WorkflowModel(view);
                var wi = new WorkflowInstance(model);
                wi.New();

                // Act
                var actual = wi.IsLoaded;

                // Assert
                Assert.IsTrue(actual);
            }
        }
        public void IsLoadedFiresPropertyChanged()
        {
            // Arrange
            using (var testdb = new SqlWorkflowInstanceStoreTest())
            {
                var view = new TestWorkflowView(testdb.CreateInstanceStore());
                var model = new WorkflowModel(view);
                var wi = new WorkflowInstance(model);
                var propChanged = false;

                wi.PropertyChanged += (sender, args) =>
                    {
                        if (!propChanged)
                        {
                            propChanged = args.PropertyName == "IsLoaded";
                        }
                    };

                // Act
                wi.New();

                // Assert
                Assert.IsTrue(propChanged);
            }
        }
        public void LoadShouldLoadWorkflowApp()
        {
            using (var testdb = new SqlWorkflowInstanceStoreTest())
            {
                // Arrange
                var view = new TestWorkflowView(testdb.CreateInstanceStore());
                var model = view.Model;

                // Create and run the workflow
                model.New();
                model.Unload();

                // Act
                model.Load();
                var isLoaded = model.IsLoaded;

                // Assert
                Assert.IsTrue(isLoaded);
            }
        }
        public void CtorNoArg()
        {
            // Arrange

            // Act
            var actual = new SqlWorkflowInstanceStoreTest();

            // Assert
            Assert.IsNotNull(actual);
        }
        /// <summary>
        ///   Runs the sample state machine through a number of triggers and leaves a persisted instance
        /// </summary>
        /// <param name="testdb"> The test database </param>
        /// <param name="triggers"> The triggers </param>
        /// <returns> The workflow instance ID </returns>
        private Guid RunSampleStateMachine(SqlWorkflowInstanceStoreTest testdb, params StateTrigger[] triggers)
        {
            var activity = new StateMachineExample();
            var host = WorkflowApplicationTest.Create(activity);
            var sqlWorkflowInstanceStore = new SqlWorkflowInstanceStore(testdb.ConnectionString);
            host.InstanceStore = sqlWorkflowInstanceStore;
            StateTracker.Attach(host.TestWorkflowApplication, sqlWorkflowInstanceStore);

            using (host)
            {
                // Start the workflow and run until it becomes idle with at least one bookmark
                host.TestWorkflowApplication.RunUntilAnyBookmark(Constants.Timeout);

                // Play each of the triggers
                foreach (var trigger in triggers)
                {
                    // Resume the workflow and run until any bookmark
                    host.TestWorkflowApplication.ResumeUntilBookmark(trigger, null, Constants.Timeout);
                }

                host.Unload(Constants.Timeout);
            }

            return host.Id;
        }
        public void IsLoadedFalseAfterCreate()
        {
            // Arrange
            using (var testdb = new SqlWorkflowInstanceStoreTest())
            {
                var view = new TestWorkflowView(testdb.CreateInstanceStore());
                var model = view.Model;

                // Act
                var actual = model.IsLoaded;

                // Assert
                Assert.IsFalse(actual);
            }
        }
        public void IsLoadedTrueWhenRun()
        {
            // Arrange
            using (var testdb = new SqlWorkflowInstanceStoreTest())
            {
                var view = new TestWorkflowView(testdb.CreateInstanceStore());
                var model = view.Model;

                model.New();

                // Act
                var actual = model.IsLoaded;

                // Assert
                Assert.IsTrue(actual);
            }
        }
        public void TransitionsReturnCollectionWhenRun()
        {
            // Arrange
            using (var testdb = new SqlWorkflowInstanceStoreTest())
            {
                var view = new TestWorkflowView(testdb.CreateInstanceStore());
                var model = view.Model;
                model.New();

                // Act
                var actual = model.Transitions;

                // Assert
                Assert.AreEqual(2, actual.Count);
                AssertHelper.OccursInOrder(actual, StateTrigger.T1.ToString(), StateTrigger.T2.ToString());
            }
        }
        public void OnCompleteRemovesCurrent()
        {
            // Arrange
            using (var testdb = new SqlWorkflowInstanceStoreTest())
            {
                var view = new TestWorkflowView(testdb.CreateInstanceStore());
                var model = view.Model;
                var completedEvent = new AutoResetEvent(false);
                model.Workflows.CollectionChanged += (sender, args) => completedEvent.Set();
                model.New();

                // Act
                model.ResumeBookmark(StateTrigger.T1);
                var index1 = model.SelectedIndex;
                var count1 = model.Workflows.Count;

                // Complete the workflow
                model.ResumeBookmark(StateTrigger.T5);
                completedEvent.WaitOne(Globals.Timeout);

                var index2 = model.SelectedIndex;
                var count2 = model.Workflows.Count;

                // Assert
                Assert.AreEqual(1, count1);
                Assert.AreEqual(0, index1);
                Assert.AreEqual(0, count2);
                Assert.AreEqual(-1, index2);
            }
        }
        public void ModelReturnsStateHistory()
        {
            // Arrange
            using (var testdb = new SqlWorkflowInstanceStoreTest())
            {
                var view = new TestWorkflowView(testdb.CreateInstanceStore());
                var model = view.Model;

                // Act
                model.New();

                // Assert
                Assert.AreEqual(1, model.StateHistory.Count);
            }
        }
        public void ProcedureExistsReturnsTrueWhenProcDoesExist()
        {
            using (var testdb = new SqlWorkflowInstanceStoreTest())
            {
                // Arrange
                testdb.CreateInstanceStore();

                // Act
                var actual = testdb.ProcedureExists("LoadInstance");

                // Assert
                Assert.IsTrue(actual);
            }
        }
        public void IdIsHostId()
        {
            // Arrange
            using (var testdb = new SqlWorkflowInstanceStoreTest())
            {
                var view = new TestWorkflowView(testdb.CreateInstanceStore());
                var model = new WorkflowModel(view);
                var wi = new WorkflowInstance(model);

                // Act
                wi.New();
                var hostid = wi.Host.Id;
                var id = wi.Id;

                // Assert
                Assert.AreEqual(hostid, id);
            }
        }
        public void IdIsLoadedTrackerId()
        {
            using (var testdb = new SqlWorkflowInstanceStoreTest())
            {
                // Arrange
                var view = new TestWorkflowView(testdb.CreateInstanceStore());
                var model = new WorkflowModel(view);

                // Create and run the workflow
                var id = model.New();

                // Act

                // Load the instances from the datbase
                model.LoadInstances(view);

                // Get the id from the state tracker
                var hostid = model.CurrentInstance.Id;

                // Assert
                Assert.AreEqual(hostid, id);
            }
        }
        public void IdIsNotEmptyGuid()
        {
            // Arrange
            using (var testdb = new SqlWorkflowInstanceStoreTest())
            {
                var view = new TestWorkflowView(testdb.CreateInstanceStore());
                var model = new WorkflowModel(view);
                var wi = new WorkflowInstance(model);

                // Act
                var id = wi.Id;

                // Assert
                Assert.AreEqual(Guid.Empty, id);
            }
        }
        public void LoadReturnsFalseWhenNoCurrentInstance()
        {
            // Arrange
            using (var testdb = new SqlWorkflowInstanceStoreTest())
            {
                var view = new TestWorkflowView(testdb.CreateInstanceStore());
                var model = view.Model;

                // Act
                var actual = model.Load();

                // Assert
                Assert.IsFalse(actual);
            }
        }
        public void CurrentStateMachineNameIsNullWhenNew()
        {
            // Arrange
            using (var testdb = new SqlWorkflowInstanceStoreTest())
            {
                var view = new TestWorkflowView(testdb.CreateInstanceStore());
                var model = view.Model;

                // Act
                var stateMachineName = model.CurrentStateMachineName;

                // Assert
                Assert.AreEqual(WorkflowModel.Unknown, stateMachineName);
            }
        }
        public void TrackerShouldLoadInstancesWithActivity()
        {
            using (var testdb = new SqlWorkflowInstanceStoreTest())
            {
                // Arrange
                testdb.CreateInstanceStore();
                var id = this.RunSampleStateMachine(testdb, StateTrigger.T1, StateTrigger.T3);

                // Act
                var instance = StateTracker.LoadInstance(id, testdb.ConnectionString);

                // Assert
                Assert.IsNotNull(instance);
                Assert.AreEqual(id, instance.InstanceId);
            }
        }