public Builder add( String key, URI uri, Precedence precedence ) { if ( entries.stream().anyMatch( e -> e.key.equals( key ) && e.precedence == precedence ) ) { throw new InvalidSettingException( String.format( "Unable to add two entries with the same precedence using key '%s' and precedence '%s'", key, precedence ) ); } entries.add( new URIEntry( key, uri, precedence ) ); return this; }
@Test void badDurationMissingNumber() { Setting<Duration> setting = buildSetting( "foo.bar", DURATION ).build(); InvalidSettingException exception = assertThrows( InvalidSettingException.class, () -> setting.apply( map( stringMap( "foo.bar", "ms" ) ) ) ); assertThat( exception.getMessage(), containsString( "Missing numeric value" ) ); }
@Test void badDurationInvalidUnit() { Setting<Duration> setting = buildSetting( "foo.bar", DURATION ).build(); InvalidSettingException exception = assertThrows( InvalidSettingException.class, () -> setting.apply( map( stringMap( "foo.bar", "2gigaseconds" ) ) ) ); assertThat( exception.getMessage(), containsString( "Unrecognized unit 'gigaseconds'" ) ); }
public Builder add( String key, String scheme, String hostname, int port, Precedence precedence ) { try { return add( key, new URI( scheme, null, hostname, port, null, null, null ), precedence ); } catch ( URISyntaxException e ) { throw new InvalidSettingException( String.format( "Unable to construct bolt discoverable URI using '%s' as hostname: " + "%s", hostname, e.getMessage() ), e ); } }
@Test public void shouldThrowOnUnknownPolicySetting() { // given SslPolicyConfigValidator validator = new SslPolicyConfigValidator(); Map<String,String> originalParams = params( "dbms.ssl.policy.default.color", "blue" ); // when try { validator.validate( originalParams, warnings ); fail(); } catch ( InvalidSettingException e ) { assertTrue( e.getMessage().contains( "Invalid setting name" ) ); } }
public Builder add( String key, String uri, Precedence precedence ) { try { return add( key, new URI( uri ), precedence ); } catch ( URISyntaxException e ) { throw new InvalidSettingException( String.format( "Unable to construct bolt discoverable URI using '%s' as uri: " + "%s", uri, e.getMessage() ), e ); } }
@Test public void shouldThrowOnDirectPolicySetting() { // given SslPolicyConfigValidator validator = new SslPolicyConfigValidator(); Map<String,String> originalParams = params( "dbms.ssl.policy.base_directory", "path" ); // when try { validator.validate( originalParams, warnings ); fail(); } catch ( InvalidSettingException e ) { assertTrue( e.getMessage().contains( "Invalid setting name" ) ); } }
@Override @Nonnull public Map<String,Object> values( @Nonnull Map<String,String> params ) { final HashMap<String,Object> result = new HashMap<>(); ownedEntries( params ).forEach( s -> result.putAll( getSettingFor( s.getKey(), params ) .orElseThrow( () -> new InvalidSettingException( format( "Invalid connector setting: %s", s.getKey() ) ) ) .values( params ) ) ); return result; }
@Test public void shouldComplainWhenMissingMandatoryBaseDirectory() { // given SslPolicyConfigValidator validator = new SslPolicyConfigValidator(); Map<String,String> originalParams = params( "dbms.ssl.policy.default.private_key", "private.key", "dbms.ssl.policy.default.public_certificate", "public.crt" ); // when try { validator.validate( originalParams, warnings ); fail(); } catch ( InvalidSettingException e ) { assertTrue( e.getMessage().contains( "Missing mandatory setting" ) ); } }
@Override @Nonnull public Map<String,String> validate( @Nonnull Map<String,String> rawConfig, @Nonnull Consumer<String> warningConsumer ) throws InvalidSettingException { final HashMap<String,String> result = new HashMap<>(); ownedEntries( rawConfig ).forEach( s -> result.putAll( getSettingFor( s.getKey(), rawConfig ) .orElseThrow( () -> new InvalidSettingException( format( "Invalid connector setting: %s", s.getKey() ) ) ) .validate( rawConfig, warningConsumer ) ) ); warnAboutDeprecatedConnectors( result, warningConsumer ); return result; }
@Test void exceptDoesNotAllowForbiddenValues() { Setting<String> restrictedSetting = buildSetting( "foo", STRING, "test" ).constraint( except( "a", "b", "c" ) ).build(); assertEquals( "test", restrictedSetting.apply( map( stringMap() ) ) ); assertEquals( "d", restrictedSetting.apply( map( stringMap( "foo", "d" ) ) ) ); assertThrows( InvalidSettingException.class, () -> restrictedSetting.apply( map( stringMap( "foo", "a" ) ) ) ); assertThrows( InvalidSettingException.class, () -> restrictedSetting.apply( map( stringMap( "foo", "b" ) ) ) ); InvalidSettingException exception = assertThrows( InvalidSettingException.class, () -> restrictedSetting.apply( map( stringMap( "foo", "c" ) ) ) ); assertThat( exception.getMessage(), containsString( "not allowed value is: c" ) ); }
@Override public Map<String,String> validate( Map<String,String> rawConfig, Consumer<String> warningConsumer ) throws InvalidSettingException { // Validate setting, if present or default value otherwise try { apply( rawConfig::get ); // only return if it was present though return rawConfig.entrySet().stream().filter( entry -> entry.getKey().startsWith( name() ) ).collect( CollectorsUtil.entriesToMap() ); } catch ( RuntimeException e ) { throw new InvalidSettingException( e.getMessage(), e ); } } };
throw new InvalidSettingException( "Invalid setting name: " + settingName ); throw new InvalidSettingException( "Missing mandatory setting: " + policy.base_directory.name() );
@Nonnull private static Map<String,String> assertEncryption( @Nonnull String name, @Nonnull Setting<?> setting, @Nonnull Map<String,String> rawConfig ) throws InvalidSettingException { Map<String,String> result = setting.validate( rawConfig, nullConsumer ); Optional<?> encryption = Optional.ofNullable( setting.apply( rawConfig::get ) ); if ( "https".equalsIgnoreCase( name ) ) { if ( encryption.isPresent() && encryption.get() != TLS ) { throw new InvalidSettingException( format( "'%s' is only allowed to be '%s'; not '%s'", setting.name(), TLS.name(), encryption.get() ) ); } } else if ( "http".equalsIgnoreCase( name ) ) { if ( encryption.isPresent() && encryption.get() != NONE ) { throw new InvalidSettingException( format( "'%s' is only allowed to be '%s'; not '%s'", setting.name(), NONE.name(), encryption.get() ) ); } } return result; }
throw new InvalidSettingException( format( "Invalid connector setting: %s", key ) ); throw new InvalidSettingException( format( "Missing mandatory value for '%s'", typeKey ) ); throw new InvalidSettingException( format( "'%s' must be one of %s; not '%s'", typeKey, String.join( ", ", validTypes ), typeValue ) );
@Override public Map<String,String> validate( @Nonnull Config config, @Nonnull Log log ) throws InvalidSettingException { if ( !config.get( MySettingsWithDefaults.hello ).equals( "neo4j" ) ) { throw new InvalidSettingException( "Setting hello has to set to neo4j" ); } return Collections.emptyMap(); } }
throw new InvalidSettingException( name(), value, e.getMessage() );
@Override default Map<String,String> validate( Map<String,String> rawConfig, Consumer<String> warningConsumer ) throws InvalidSettingException { // Validate setting, if present or default value otherwise try { apply( rawConfig::get ); // only return if it was present though if ( rawConfig.containsKey( name() ) ) { return stringMap( name(), rawConfig.get( name() ) ); } else { return emptyMap(); } } catch ( RuntimeException e ) { throw new InvalidSettingException( e.getMessage(), e ); } }
throw new InvalidSettingException( String.format( "Unknown config option '%s'. To resolve either remove it from your configuration " + "or set '%s' to false.", key, strict_config_validation.name() ) );
public Builder add( String key, URI uri, Precedence precedence ) { if ( entries.stream().anyMatch( e -> e.key.equals( key ) && e.precedence == precedence ) ) { throw new InvalidSettingException( String.format( "Unable to add two entries with the same precedence using key '%s' and precedence '%s'", key, precedence ) ); } entries.add( new URIEntry( key, uri, precedence ) ); return this; }