### Question:
LeaderOnlyStrategy extends UpstreamDatabaseSelectionStrategy { @Override public Optional<MemberId> upstreamDatabase() throws UpstreamDatabaseSelectionException { Map<MemberId,RoleInfo> memberRoles = topologyService.allCoreRoles(); if ( memberRoles.size() == 0 ) { throw new UpstreamDatabaseSelectionException( "No core servers available" ); } for ( Map.Entry<MemberId,RoleInfo> entry : memberRoles.entrySet() ) { RoleInfo role = entry.getValue(); if ( role == RoleInfo.LEADER && !Objects.equals( myself, entry.getKey() ) ) { return Optional.of( entry.getKey() ); } } return Optional.empty(); } LeaderOnlyStrategy(); @Override Optional<MemberId> upstreamDatabase(); static final String IDENTITY; }### Answer:
@Test public void ignoresSelf() throws UpstreamDatabaseSelectionException { MemberId myself = new MemberId( new UUID( 1234, 5678 ) ); String groupName = "groupName"; LeaderOnlyStrategy leaderOnlyStrategy = new LeaderOnlyStrategy(); TopologyServiceThatPrioritisesItself topologyServiceNoRetriesStrategy = new TopologyServiceThatPrioritisesItself( myself, groupName ) { @Override public Map<MemberId,RoleInfo> allCoreRoles() { Map<MemberId,RoleInfo> roles = new HashMap<>(); roles.put( myself, RoleInfo.LEADER ); roles.put( coreNotSelf, RoleInfo.LEADER ); return roles; } }; leaderOnlyStrategy.inject( topologyServiceNoRetriesStrategy, Config.defaults(), NullLogProvider.getInstance(), myself ); Optional<MemberId> resolved = leaderOnlyStrategy.upstreamDatabase(); Assert.assertTrue( resolved.isPresent() ); Assert.assertNotEquals( myself, resolved.get() ); }