From 0821d12198392a8b35e6bdc53597aa8254bc5e4b Mon Sep 17 00:00:00 2001 From: Sam Barker Date: Mon, 2 Oct 2023 15:21:29 +1300 Subject: [PATCH] Try to reduce duplication and thus sonar moaning when injecting fields Signed-off-by: Sam Barker --- .../junit5ext/KafkaClusterExtension.java | 83 ++++++++++--------- 1 file changed, 42 insertions(+), 41 deletions(-) diff --git a/junit5-extension/src/main/java/io/kroxylicious/testing/kafka/junit5ext/KafkaClusterExtension.java b/junit5-extension/src/main/java/io/kroxylicious/testing/kafka/junit5ext/KafkaClusterExtension.java index 9bc446ae..fd554d19 100644 --- a/junit5-extension/src/main/java/io/kroxylicious/testing/kafka/junit5ext/KafkaClusterExtension.java +++ b/junit5-extension/src/main/java/io/kroxylicious/testing/kafka/junit5ext/KafkaClusterExtension.java @@ -20,6 +20,7 @@ import java.util.Comparator; import java.util.Iterator; import java.util.List; +import java.util.Map; import java.util.Objects; import java.util.ServiceLoader; import java.util.UUID; @@ -570,52 +571,36 @@ private void injectFields(ExtensionContext context, Object testInstance, Class predicate.test(field) && Admin.class.isAssignableFrom(field.getType()) && isCandidate(field), + final Map, List> fieldsByType = findFields(testClass, + field -> predicate.test(field) && isCandidate(field), HierarchyTraversalMode.BOTTOM_UP) - .forEach(field -> { - try { - makeAccessible(field).set(testInstance, getAdmin( - "field " + field.getName(), - field, - field.getType().asSubclass(Admin.class), - context)); - } - catch (Throwable t) { - ExceptionUtils.throwAsUncheckedException(t); - } - }); + .stream() + .collect(Collectors.groupingBy(Field::getType)); - findFields(testClass, - field -> predicate.test(field) && Producer.class.isAssignableFrom(field.getType()) && isCandidate(field), - HierarchyTraversalMode.BOTTOM_UP) - .forEach(field -> { - try { - makeAccessible(field).set(testInstance, getProducer( - "field " + field.getName(), - field, - (Class) field.getType().asSubclass(Producer.class), - field.getGenericType(), - context)); - } - catch (Throwable t) { - ExceptionUtils.throwAsUncheckedException(t); - } - }); - findFields(testClass, - field -> predicate.test(field) && Consumer.class.isAssignableFrom(field.getType()) && isCandidate(field), - HierarchyTraversalMode.BOTTOM_UP) + injectClient(Admin.class, KafkaClusterExtension::getAdmin, context, testInstance, fieldsByType); + injectClient(Producer.class, KafkaClusterExtension::getProducer, context, testInstance, fieldsByType); + injectClient(Consumer.class, KafkaClusterExtension::getConsumer, context, testInstance, fieldsByType); + } + + @SuppressWarnings("unchecked") + private static void injectClient(Class clientType, ClientFactory clientFactory, ExtensionContext context, Object testInstance, + Map, List> fieldsByType) { + fieldsByType.entrySet().stream() + .filter(entry -> clientType.isAssignableFrom(entry.getKey())) + .map(Map.Entry::getValue) + .flatMap(List::stream) .forEach(field -> { try { - makeAccessible(field).set(testInstance, getConsumer( - "field " + field.getName(), - field, - (Class) field.getType().asSubclass(Consumer.class), - field.getGenericType(), - context)); + makeAccessible(field).set(testInstance, + clientFactory.getClient( + "field " + field.getName(), + field, + (Class) field.getType().asSubclass(clientType), + field.getGenericType(), + context)); } - catch (Throwable t) { - ExceptionUtils.throwAsUncheckedException(t); + catch (Exception e) { + ExceptionUtils.throwAsUncheckedException(e); } }); } @@ -880,6 +865,14 @@ private static Admin getAdmin(String description, AnnotatedElement sourceElement, Class type, ExtensionContext extensionContext) { + return getAdmin(description, sourceElement, type, Void.class, extensionContext); + } + + private static Admin getAdmin(String description, + AnnotatedElement sourceElement, + Class type, + Type genericType, + ExtensionContext extensionContext) { KafkaCluster cluster = findClusterFromContext(sourceElement, extensionContext, type, description); @@ -1084,4 +1077,12 @@ private void assertSupportedType(String target, Class type) { } } + @FunctionalInterface + private interface ClientFactory { + X getClient(String description, + AnnotatedElement sourceElement, + Class type, + Type genericType, + ExtensionContext extensionContext); + } }