### Question:
Policies { Policy selectFor( Map<String,String> context ) throws ProcedureException { String policyName = context.get( POLICY_KEY ); if ( policyName == null ) { return defaultPolicy(); } else { Policy selectedPolicy = policies.get( policyName ); if ( selectedPolicy == null ) { throw new ProcedureException( Status.Procedure.ProcedureCallFailed, format( "Policy definition for '%s' could not be found.", policyName ) ); } return selectedPolicy; } } Policies( Log log );  static final String POLICY_KEY; }### Answer:
@Test public void shouldSupplyDefaultUnfilteredPolicyForEmptyContext() throws Exception { Policies policies = new Policies( log ); Policy policy = policies.selectFor( emptyMap() ); Set<ServerInfo> input = asSet( new ServerInfo( new AdvertisedSocketAddress( "bolt", 1 ), new MemberId( UUID.randomUUID() ), asSet( "groupA" ) ), new ServerInfo( new AdvertisedSocketAddress( "bolt", 2 ), new MemberId( UUID.randomUUID() ), asSet( "groupB" ) ) ); Set<ServerInfo> output = policy.apply( input ); assertEquals( input, output ); assertEquals( Policies.DEFAULT_POLICY, policy ); }
@Test public void shouldThrowExceptionOnUnknownPolicyName() { Policies policies = new Policies( log ); try { policies.selectFor( stringMap( Policies.POLICY_KEY, "unknown-policy" ) ); fail(); } catch ( ProcedureException e ) { assertEquals( Status.Procedure.ProcedureCallFailed, e.status() ); } }
@Test public void shouldThrowExceptionOnSelectionOfUnregisteredDefault() { Policies policies = new Policies( log ); try { policies.selectFor( stringMap( Policies.POLICY_KEY, Policies.DEFAULT_POLICY_NAME ) ); fail(); } catch ( ProcedureException e ) { assertEquals( Status.Procedure.ProcedureCallFailed, e.status() ); } }