### Question:
UserDefinedConfigurationStrategy extends UpstreamDatabaseSelectionStrategy { @Override public Optional<MemberId> upstreamDatabase() { return filters.flatMap( filters -> { Set<ServerInfo> possibleServers = possibleServers(); return filters.apply( possibleServers ).stream().map( ServerInfo::memberId ).filter( memberId -> !Objects.equals( myself, memberId ) ).findFirst(); } ); } UserDefinedConfigurationStrategy(); @Override void init(); @Override Optional<MemberId> upstreamDatabase(); static final String IDENTITY; }### Answer:
@Test public void shouldPickTheFirstMatchingServerIfCore() { MemberId theCoreMemberId = new MemberId( UUID.randomUUID() ); TopologyService topologyService = fakeTopologyService( fakeCoreTopology( theCoreMemberId ), fakeReadReplicaTopology( memberIDs( 100 ), this::noEastGroupGenerator ) ); UserDefinedConfigurationStrategy strategy = new UserDefinedConfigurationStrategy(); Config config = Config.defaults( CausalClusteringSettings.user_defined_upstream_selection_strategy, "groups(east); groups(core); halt()" ); strategy.inject( topologyService, config, NullLogProvider.getInstance(), null ); Optional<MemberId> memberId = strategy.upstreamDatabase(); assertThat( memberId, contains( theCoreMemberId ) ); }
@Test public void shouldPickTheFirstMatchingServerIfReadReplica() { MemberId[] readReplicaIds = memberIDs( 100 ); TopologyService topologyService = fakeTopologyService( fakeCoreTopology( new MemberId( UUID.randomUUID() ) ), fakeReadReplicaTopology( readReplicaIds, this::noEastGroupGenerator ) ); UserDefinedConfigurationStrategy strategy = new UserDefinedConfigurationStrategy(); String wantedGroup = noEastGroup.get( 1 ); Config config = configWithFilter( "groups(" + wantedGroup + "); halt()" ); strategy.inject( topologyService, config, NullLogProvider.getInstance(), null ); Optional<MemberId> memberId = strategy.upstreamDatabase(); assertThat( memberId, contains( isIn( readReplicaIds ) ) ); assertThat( memberId.map( this::noEastGroupGenerator ), contains( equalTo( asSet( wantedGroup ) ) ) ); }
@Test public void shouldReturnEmptyIfNoMatchingServers() { MemberId[] readReplicaIds = memberIDs( 100 ); TopologyService topologyService = fakeTopologyService( fakeCoreTopology( new MemberId( UUID.randomUUID() ) ), fakeReadReplicaTopology( readReplicaIds, this::noEastGroupGenerator ) ); UserDefinedConfigurationStrategy strategy = new UserDefinedConfigurationStrategy(); String wantedGroup = eastGroup; Config config = configWithFilter( "groups(" + wantedGroup + "); halt()" ); strategy.inject( topologyService, config, NullLogProvider.getInstance(), null ); Optional<MemberId> memberId = strategy.upstreamDatabase(); assertThat( memberId, empty() ); }
@Test public void shouldReturnEmptyIfInvalidFilterSpecification() { TopologyService topologyService = fakeTopologyService( fakeCoreTopology( new MemberId( UUID.randomUUID() ) ), fakeReadReplicaTopology( memberIDs( 100 ), this::noEastGroupGenerator ) ); UserDefinedConfigurationStrategy strategy = new UserDefinedConfigurationStrategy(); Config config = configWithFilter( "invalid filter specification" ); strategy.inject( topologyService, config, NullLogProvider.getInstance(), null ); Optional<MemberId> memberId = strategy.upstreamDatabase(); assertThat( memberId, empty() ); }
@Test public void shouldNotReturnSelf() { String wantedGroup = eastGroup; MemberId[] readReplicaIds = memberIDs( 1 ); TopologyService topologyService = fakeTopologyService( fakeCoreTopology( new MemberId( UUID.randomUUID() ) ), fakeReadReplicaTopology( readReplicaIds, memberId -> asSet( wantedGroup ) ) ); UserDefinedConfigurationStrategy strategy = new UserDefinedConfigurationStrategy(); Config config = configWithFilter( "groups(" + wantedGroup + "); halt()" ); strategy.inject( topologyService, config, NullLogProvider.getInstance(), readReplicaIds[0] ); Optional<MemberId> memberId = strategy.upstreamDatabase(); assertThat( memberId, empty() ); }