### Question:
UpstreamDatabaseStrategySelector { public MemberId bestUpstreamDatabase() throws UpstreamDatabaseSelectionException { MemberId result = null; for ( UpstreamDatabaseSelectionStrategy strategy : strategies ) { log.debug( "Trying selection strategy [%s]", strategy.toString() ); try { if ( strategy.upstreamDatabase().isPresent() ) { result = strategy.upstreamDatabase().get(); break; } } catch ( NoSuchElementException ex ) { } } if ( result == null ) { throw new UpstreamDatabaseSelectionException( "Could not find an upstream database with which to connect." ); } log.debug( "Selected upstream database [%s]", result ); return result; } UpstreamDatabaseStrategySelector( UpstreamDatabaseSelectionStrategy defaultStrategy );  UpstreamDatabaseStrategySelector( UpstreamDatabaseSelectionStrategy defaultStrategy, Iterable<UpstreamDatabaseSelectionStrategy> otherStrategies,
            LogProvider logProvider ); MemberId bestUpstreamDatabase();  }### Answer:
@Test public void shouldReturnTheMemberIdFromFirstSuccessfulStrategy() throws Exception { UpstreamDatabaseSelectionStrategy badOne = mock( UpstreamDatabaseSelectionStrategy.class ); when( badOne.upstreamDatabase() ).thenReturn( Optional.empty() ); UpstreamDatabaseSelectionStrategy anotherBadOne = mock( UpstreamDatabaseSelectionStrategy.class ); when( anotherBadOne.upstreamDatabase() ).thenReturn( Optional.empty() ); UpstreamDatabaseSelectionStrategy goodOne = mock( UpstreamDatabaseSelectionStrategy.class ); MemberId theMemberId = new MemberId( UUID.randomUUID() ); when( goodOne.upstreamDatabase() ).thenReturn( Optional.of( theMemberId ) ); UpstreamDatabaseStrategySelector selector = new UpstreamDatabaseStrategySelector( badOne, iterable( goodOne, anotherBadOne ), NullLogProvider.getInstance() ); MemberId result = selector.bestUpstreamDatabase(); assertEquals( theMemberId, result ); }
@Test public void shouldDefaultToRandomCoreServerIfNoOtherStrategySpecified() throws Exception { TopologyService topologyService = mock( TopologyService.class ); MemberId memberId = new MemberId( UUID.randomUUID() ); when( topologyService.localCoreServers() ).thenReturn( new CoreTopology( new ClusterId( UUID.randomUUID() ), false, mapOf( memberId, mock( CoreServerInfo.class ) ) ) ); ConnectToRandomCoreServerStrategy defaultStrategy = new ConnectToRandomCoreServerStrategy(); defaultStrategy.inject( topologyService, Config.defaults(), NullLogProvider.getInstance(), null ); UpstreamDatabaseStrategySelector selector = new UpstreamDatabaseStrategySelector( defaultStrategy ); MemberId instance = selector.bestUpstreamDatabase(); assertEquals( memberId, instance ); }
@Test public void shouldUseSpecifiedStrategyInPreferenceToDefault() throws Exception { TopologyService topologyService = mock( TopologyService.class ); MemberId memberId = new MemberId( UUID.randomUUID() ); when( topologyService.localCoreServers() ).thenReturn( new CoreTopology( new ClusterId( UUID.randomUUID() ), false, mapOf( memberId, mock( CoreServerInfo.class ) ) ) ); ConnectToRandomCoreServerStrategy shouldNotUse = mock( ConnectToRandomCoreServerStrategy.class ); UpstreamDatabaseSelectionStrategy mockStrategy = mock( UpstreamDatabaseSelectionStrategy.class ); when( mockStrategy.upstreamDatabase() ).thenReturn( Optional.of( new MemberId( UUID.randomUUID() ) ) ); UpstreamDatabaseStrategySelector selector = new UpstreamDatabaseStrategySelector( shouldNotUse, iterable( mockStrategy ), NullLogProvider.getInstance() ); selector.bestUpstreamDatabase(); verifyZeroInteractions( shouldNotUse ); }