### Question:
IndexSamplingController { public void sampleIndexes( IndexSamplingMode mode ) { IndexMap indexMap = indexMapSnapshotProvider.indexMapSnapshot(); jobQueue.addAll( !mode.sampleOnlyIfUpdated, PrimitiveLongCollections.toIterator( indexMap.indexIds() ) ); scheduleSampling( mode, indexMap ); } IndexSamplingController( IndexSamplingConfig config,
                             IndexSamplingJobFactory jobFactory,
                             IndexSamplingJobQueue<Long> jobQueue,
                             IndexSamplingJobTracker jobTracker,
                             IndexMapSnapshotProvider indexMapSnapshotProvider,
                             JobScheduler scheduler,
                             RecoveryCondition indexRecoveryCondition,
                             LogProvider logProvider ); void sampleIndexes( IndexSamplingMode mode ); void sampleIndex( long indexId, IndexSamplingMode mode ); void recoverIndexSamples(); void start(); void stop();  }### Answer:
@Test public void shouldStartASamplingJobForEachIndexInTheDB() { IndexSamplingController controller = newSamplingController( always( false ), logProvider); when( tracker.canExecuteMoreSamplingJobs() ).thenReturn( true ); when( indexProxy.getState() ).thenReturn( ONLINE ); controller.sampleIndexes( BACKGROUND_REBUILD_UPDATED ); verify( jobFactory ).create( indexId, indexProxy ); verify( tracker ).scheduleSamplingJob( job ); verify( tracker, times( 2 ) ).canExecuteMoreSamplingJobs(); verifyNoMoreInteractions( jobFactory, tracker ); }
@Test public void shouldNotStartAJobIfTheIndexIsNotOnline() { IndexSamplingController controller = newSamplingController( always( false ), logProvider); when( tracker.canExecuteMoreSamplingJobs() ).thenReturn( true ); when( indexProxy.getState() ).thenReturn( POPULATING ); controller.sampleIndexes( BACKGROUND_REBUILD_UPDATED ); verify( tracker, times( 2 ) ).canExecuteMoreSamplingJobs(); verifyNoMoreInteractions( jobFactory, tracker ); }
@Test public void shouldNotStartAJobIfTheTrackerCannotHandleIt() { IndexSamplingController controller = newSamplingController( always( false ), logProvider); when( tracker.canExecuteMoreSamplingJobs() ).thenReturn( false ); when( indexProxy.getState() ).thenReturn( ONLINE ); controller.sampleIndexes( BACKGROUND_REBUILD_UPDATED ); verify( tracker, times( 1 ) ).canExecuteMoreSamplingJobs(); verifyNoMoreInteractions( jobFactory, tracker ); }
@Test public void shouldSampleAllTheIndexes() { IndexSamplingController controller = newSamplingController( always( false ), logProvider); when( tracker.canExecuteMoreSamplingJobs() ).thenReturn( true ); when( indexProxy.getState() ).thenReturn( ONLINE ); when( anotherIndexProxy.getState() ).thenReturn( ONLINE ); indexMap.putIndexProxy( anotherIndexProxy ); controller.sampleIndexes( TRIGGER_REBUILD_UPDATED ); verify( jobFactory ).create( indexId, indexProxy ); verify( tracker ).scheduleSamplingJob( job ); verify( jobFactory ).create( anotherIndexId, anotherIndexProxy ); verify( tracker ).scheduleSamplingJob( anotherJob ); verify( tracker, times( 2 ) ).waitUntilCanExecuteMoreSamplingJobs(); verifyNoMoreInteractions( jobFactory, tracker ); }
@Test public void shouldSampleAllTheOnlineIndexes() { IndexSamplingController controller = newSamplingController( always( false ), logProvider); when( tracker.canExecuteMoreSamplingJobs() ).thenReturn( true ); when( indexProxy.getState() ).thenReturn( ONLINE ); when( anotherIndexProxy.getState() ).thenReturn( POPULATING ); indexMap.putIndexProxy( anotherIndexProxy ); controller.sampleIndexes( TRIGGER_REBUILD_UPDATED ); verify( jobFactory ).create( indexId, indexProxy ); verify( tracker ).scheduleSamplingJob( job ); verify( tracker, times( 2 ) ).waitUntilCanExecuteMoreSamplingJobs(); verifyNoMoreInteractions( jobFactory, tracker ); }