private static Optional<ConfigValue> resolveStreamsConfig( final String maybePrefixedKey, final Object value) { final String key = maybePrefixedKey.startsWith(KSQL_STREAMS_PREFIX) ? maybePrefixedKey.substring(KSQL_STREAMS_PREFIX.length()) : maybePrefixedKey; if (key.startsWith(KsqlConfig.KSQL_CONFIG_PROPERTY_PREFIX)) { return Optional.empty(); } return new KsqlConfigResolver().resolve(maybePrefixedKey, false) .map(configItem -> new ConfigValue(configItem, key, configItem.parseValue(value))); }
@Override public Object parse(final String property, final Object value) { if (property.equalsIgnoreCase(DdlConfig.AVRO_SCHEMA) || property.equalsIgnoreCase(KsqlConstants.RUN_SCRIPT_STATEMENTS_CONTENT)) { validator.validate(property, value); return value; } final ConfigItem configItem = resolver.resolve(property, true) .orElseThrow(() -> new IllegalArgumentException(String.format( "Not recognizable as ksql, streams, consumer, or producer property: '%s'", property))); final Object parsedValue = configItem.parseValue(value); validator.validate(configItem.getPropertyName(), parsedValue); return parsedValue; } }
@Test(expected = ConfigException.class) public void shouldThrowIfCanNotCoerceParsedValue() { RESOLVED_WITH_VALIDATOR.parseValue("not a number"); }
@Test public void shouldPassThroughUnresolvedValueOnParse() { assertThat(UNRESOLVED.parseValue("anything"), is("anything")); assertThat(UNRESOLVED.parseValue(12345L), is(12345L)); }
@Test public void shouldParseResolvedValue() { assertThat(RESOLVED_NO_VALIDATOR.parseValue(101), is(101)); }
@Test public void shouldValidateParsedValue() { assertThat(RESOLVED_WITH_VALIDATOR.parseValue(101), is(101)); }
@Before public void setUp() { parser = new LocalPropertyParser(resolver, validator); when(configItem.parseValue(any(Object.class))) .thenReturn(PARSED_VALUE); when(configItem.getPropertyName()) .thenReturn(PARSED_PROP_NAME); when(resolver.resolve(anyString(), anyBoolean())) .thenReturn(Optional.of(configItem)); }
@Test public void shouldCoerceParsedValue() { assertThat(RESOLVED_NO_VALIDATOR.parseValue("101"), is(101)); }
@Test public void shouldResolvePasswordToPassword() { assertThat(RESOLVED_PASSWORD.parseValue("Sensitive"), is(new Password("Sensitive"))); }