From f327c58739ca4de39d8080f04c6cc237a415bc37 Mon Sep 17 00:00:00 2001 From: David Kornel Date: Mon, 6 May 2024 12:00:54 +0200 Subject: [PATCH] Read annotation value from all superclasses to set resoruce manager cleaner Signed-off-by: David Kornel --- .../ResourceManagerCleanerExtension.java | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/test-frame-common/src/main/java/io/skodjob/testframe/listeners/ResourceManagerCleanerExtension.java b/test-frame-common/src/main/java/io/skodjob/testframe/listeners/ResourceManagerCleanerExtension.java index ccb93d4..bce5d74 100644 --- a/test-frame-common/src/main/java/io/skodjob/testframe/listeners/ResourceManagerCleanerExtension.java +++ b/test-frame-common/src/main/java/io/skodjob/testframe/listeners/ResourceManagerCleanerExtension.java @@ -10,20 +10,25 @@ import org.junit.jupiter.api.extension.AfterEachCallback; import org.junit.jupiter.api.extension.ExtensionContext; +import java.util.Optional; + +import static org.junit.platform.commons.support.AnnotationSupport.findAnnotation; + /** * Enables cleaner extension based on cleanResources value */ public class ResourceManagerCleanerExtension implements AfterAllCallback, AfterEachCallback { /** - * Enables ResourceManagerCleanerExtension for afterAll callback + * Enables ResourceManagerCleanerExtension for after All callback * @param extensionContext context */ @Override public void afterAll(ExtensionContext extensionContext) { - Class testClass = extensionContext.getRequiredTestClass(); - ResourceManager annotation = testClass.getAnnotation(ResourceManager.class); - if (annotation != null && annotation.cleanResources()) { + Optional annotation = + findAnnotation(extensionContext.getRequiredTestClass(), ResourceManager.class); + + if (annotation.isPresent() && annotation.get().cleanResources()) { KubeResourceManager.setTestContext(extensionContext); KubeResourceManager.getInstance().deleteResources(); } @@ -35,9 +40,10 @@ public void afterAll(ExtensionContext extensionContext) { */ @Override public void afterEach(ExtensionContext extensionContext) { - Class testClass = extensionContext.getRequiredTestClass(); - ResourceManager annotation = testClass.getAnnotation(ResourceManager.class); - if (annotation != null && annotation.cleanResources()) { + Optional annotation = + findAnnotation(extensionContext.getRequiredTestClass(), ResourceManager.class); + + if (annotation.isPresent() && annotation.get().cleanResources()) { KubeResourceManager.setTestContext(extensionContext); KubeResourceManager.getInstance().deleteResources(); }