package com.ankurm.boot42up; import java.lang.reflect.Method; import java.util.Map; import java.util.Set; import java.util.TreeSet; import org.junit.jupiter.api.Test; import org.springframework.boot.context.properties.bind.BindContext; import org.springframework.boot.context.properties.bind.BindHandler; import org.springframework.boot.context.properties.bind.Bindable; import org.springframework.boot.context.properties.bind.Binder; import org.springframework.boot.context.properties.source.ConfigurationPropertyName; import org.springframework.boot.context.properties.source.MapConfigurationPropertySource; import org.springframework.boot.kafka.autoconfigure.KafkaProperties; import static org.assertj.core.api.Assertions.assertThat; /** * Binds the new Kafka admin properties and records which of the five keys the binder actually * consumed. On 4.2 all five bind. On 4.1.2 Boot does not complain about the unknown keys; they * are simply never read, which is the failure mode worth knowing about. */ class KafkaAdminBindingTest { private static final Map PROPS = Map.of( "spring.kafka.template.admin.client-id", "template-admin", "spring.kafka.template.admin.fail-fast", "true", "spring.kafka.listener.admin.client-id", "listener-admin", "spring.kafka.listener.admin.fail-fast", "true", "spring.kafka.listener.await-async-results-on-stop", "true"); @Test void adminPropertiesBind() throws Exception { Transcript t = new Transcript("02-kafka-admin-binding"); Set consumed = new TreeSet<>(); BindHandler recorder = new BindHandler() { @Override public Object onSuccess(ConfigurationPropertyName name, Bindable target, BindContext context, Object result) { if (context.getConfigurationProperty() != null) { consumed.add(name.toString()); } return result; } }; Binder binder = new Binder(new MapConfigurationPropertySource(PROPS)); KafkaProperties kp = binder.bind("spring.kafka", Bindable.of(KafkaProperties.class), recorder) .orElseGet(KafkaProperties::new); t.line("properties supplied: " + PROPS.size()); for (String key : new TreeSet<>(PROPS.keySet())) { t.line((consumed.contains(key.substring("spring.kafka.".length())) || consumed.contains(key) ? "bound " : "IGNORED ") + key); } long bound = PROPS.keySet().stream() .filter(k -> consumed.contains(k.substring("spring.kafka.".length())) || consumed.contains(k)) .count(); t.line("bound = " + bound + " of " + PROPS.size()); Object listener = call(kp, "getListener"); Object template = call(kp, "getTemplate"); t.line("listener.isAwaitAsyncResultsOnStop() = " + callOrAbsent(listener, "isAwaitAsyncResultsOnStop")); t.line("template.getAdmin() = " + callOrAbsent(template, "getAdmin").replaceAll("@[0-9a-f]+$", "")); if (Transcript.is42()) { assertThat(bound).isEqualTo(5); Object tplAdmin = call(template, "getAdmin"); Object lsnAdmin = call(listener, "getAdmin"); t.line("template admin client-id = " + call(tplAdmin, "getClientId")); t.line("template admin fail-fast = " + call(tplAdmin, "isFailFast")); t.line("listener admin client-id = " + call(lsnAdmin, "getClientId")); t.line("listener admin fail-fast = " + call(lsnAdmin, "isFailFast")); assertThat(call(tplAdmin, "getClientId")).isEqualTo("template-admin"); assertThat(call(lsnAdmin, "isFailFast")).isEqualTo(true); } else { assertThat(bound).isZero(); } t.write(); } private static Object call(Object target, String method) throws Exception { Method m = target.getClass().getMethod(method); return m.invoke(target); } private static String callOrAbsent(Object target, String method) { try { return String.valueOf(call(target, method)); } catch (NoSuchMethodException ex) { return "no such method"; } catch (Exception ex) { return ex.toString(); } } }