public void WillPauseAndSetErrorStateWhenMainTableFails()
        {
            var settings = new TestSettings();
            var db       = new TestDatabase(settings, new PopulatedTable(), new PopulatedEventTable());

            db.Setup();
            try
            {
                var connector = CreateConnector();

                connector.ResetConnector("WillPauseAndSetErrorStateWhenIncrementalFails");
                var configuration = GetTestConfiguration(
                    "WillPauseAndSetErrorStateWhenIncrementalFails",
                    settings.TestSqlDatabaseConnection.ToString(),
                    PopulatedTable.TableName,
                    new List <TableDetail>(),
                    new List <EventTable>()
                {
                    new EventTable()
                    {
                        EventSequenceColumnName = "Id",
                        MainTableIdColumnName   = "Name",
                        EventTypeColumnName     = PopulatedEventTable.EventTypeColumnName,
                        DeleteEventTypeValue    = "delete",
                        TableName = PopulatedEventTable.TableName
                    }
                });
                configuration.BatchSize = 1;
                Action execute = () => connector.ExecuteFetch(configuration);
                execute.Invoke(); //init should work fine
                var state = new ConnectorStateBase(_stateService.LoadState(configuration.JobName));
                state.State.Should().Be(JobState.IncrementalCrawling);

                //We change the name of the table to something that does not exist
                configuration.MainTable.TableName = "I_dont exist";
                execute.Should().Throw <SqlException>("The main table name does not exist");
                state = new ConnectorStateBase(_stateService.LoadState(configuration.JobName));
                state.State.Should().Be(JobState.Error, "Should be in error state since the Main table doesen't exist");

                //Invoking it a third time will make it revert to the last working state (IncrementalCrawling) just to
                //yet again fail to read the main table and go back to Error
                execute.Should().Throw <SqlException>("The main table name still does not exist");
                state = new ConnectorStateBase(_stateService.LoadState(configuration.JobName));
                state.State.Should().Be(JobState.Error);
            }
            finally
            {
                db.Destroy();
            }
        }
        public void Execute()
        {
            var jobStates = _stateService.LoadStates().ToArray();

            foreach (var state in jobStates)
            {
                var connectorState = new ConnectorStateBase(state);
                if (connectorState.State == JobState.Paused)
                {
                    Log.Information($"Resuming PAUSED job {state.Name} to its previous state {connectorState.LastWorkingState}");
                    connectorState.State = connectorState.LastWorkingState;
                    _stateService.SaveState(connectorState);
                }
            }
        }
        public void WillPauseAndSetErrorStateWhenInitializationError()
        {
            var settings = new TestSettings();
            var db       = new TestDatabase(settings, new PopulatedTable(), new PopulatedEventTable());

            db.Setup();
            try
            {
                var connector = CreateConnector();

                connector.ResetConnector("WillPauseAndSetErrorStateWhenInitializationError");
                var configuration = GetTestConfiguration(
                    "WillPauseAndSetErrorStateWhenInitializationError",
                    settings.TestSqlDatabaseConnection.ToString(),
                    PopulatedTable.TableName,
                    new List <TableDetail>(),
                    new List <EventTable>()
                {
                    new EventTable()
                    {
                        EventSequenceColumnName = "Id",
                        MainTableIdColumnName   = "Name",
                        EventTypeColumnName     = PopulatedEventTable.EventTypeColumnName,
                        DeleteEventTypeValue    = "delete",
                        TableName = PopulatedEventTable.TableName
                    }
                });
                configuration.ConnectionString = "ThisShouldNotWork";
                Action execute = () => connector.ExecuteFetch(configuration);
                execute.Should().Throw <ArgumentException>(
                    "The connection string is erroneous and should throw an exception");


                var state = new ConnectorStateBase(_stateService.LoadState(configuration.JobName));
                state.State.Should().Be(JobState.Paused);
            }
            finally
            {
                db.Destroy();
            }
        }