### Question:
ConnectToRandomCoreServerStrategy extends UpstreamDatabaseSelectionStrategy { @Override public Optional<MemberId> upstreamDatabase() throws UpstreamDatabaseSelectionException { final CoreTopology coreTopology = topologyService.localCoreServers(); if ( coreTopology.members().size() == 0 ) { throw new UpstreamDatabaseSelectionException( "No core servers available" ); } int skippedServers = random.nextInt( coreTopology.members().size() ); return coreTopology.members().keySet().stream().skip( skippedServers ).findFirst(); } ConnectToRandomCoreServerStrategy(); @Override Optional<MemberId> upstreamDatabase(); static final String IDENTITY; }### Answer:
@Test public void shouldConnectToRandomCoreServer() throws Exception { MemberId memberId1 = new MemberId( UUID.randomUUID() ); MemberId memberId2 = new MemberId( UUID.randomUUID() ); MemberId memberId3 = new MemberId( UUID.randomUUID() ); TopologyService topologyService = mock( TopologyService.class ); when( topologyService.localCoreServers() ).thenReturn( fakeCoreTopology( memberId1, memberId2, memberId3 ) ); ConnectToRandomCoreServerStrategy connectionStrategy = new ConnectToRandomCoreServerStrategy(); connectionStrategy.inject( topologyService, Config.defaults(), NullLogProvider.getInstance(), null ); Optional<MemberId> memberId = connectionStrategy.upstreamDatabase(); assertTrue( memberId.isPresent() ); assertThat( memberId.get(), anyOf( equalTo( memberId1 ), equalTo( memberId2 ), equalTo( memberId3 ) ) ); }
@Test public void filtersSelf() throws UpstreamDatabaseSelectionException { MemberId myself = new MemberId( new UUID( 1234, 5678 ) ); Config config = Config.defaults(); String groupName = "groupName"; ConnectToRandomCoreServerStrategy connectToRandomCoreServerStrategy = new ConnectToRandomCoreServerStrategy(); connectToRandomCoreServerStrategy.inject( new TopologyServiceThatPrioritisesItself( myself, groupName ), config, NullLogProvider.getInstance(), myself ); Optional<MemberId> found = connectToRandomCoreServerStrategy.upstreamDatabase(); Assert.assertTrue( found.isPresent() ); Assert.assertNotEquals( myself, found ); }