Migrate TestNG to Junit5
[sdc/sdc-tosca.git] / sdc-tosca / src / test / java / org / onap / sdc / impl / BaseSetupExtension.java
diff --git a/sdc-tosca/src/test/java/org/onap/sdc/impl/BaseSetupExtension.java b/sdc-tosca/src/test/java/org/onap/sdc/impl/BaseSetupExtension.java
new file mode 100644 (file)
index 0000000..da26995
--- /dev/null
@@ -0,0 +1,31 @@
+package org.onap.sdc.impl;
+
+import static org.junit.jupiter.api.extension.ExtensionContext.Namespace.GLOBAL;
+
+import org.junit.jupiter.api.extension.BeforeAllCallback;
+import org.junit.jupiter.api.extension.ExtensionContext;
+import org.onap.sdc.tosca.parser.exceptions.SdcToscaParserException;
+
+public abstract class BaseSetupExtension implements BeforeAllCallback, ExtensionContext.Store.CloseableResource {
+
+    @Override
+    public void beforeAll(ExtensionContext context) throws Exception {
+        // We need to use a unique key here, across all usages of this particular extension.
+        String uniqueKey = this.getClass().getName();
+        Object value = context.getRoot().getStore(GLOBAL).get(uniqueKey);
+        if (value == null) {
+            // First test container invocation.
+            context.getRoot().getStore(GLOBAL).put(uniqueKey, this);
+            setup();
+        }
+    }
+
+    // Callback that is invoked <em>exactly once</em>
+    // before the start of <em>all</em> test containers.
+    abstract void setup() throws SdcToscaParserException;
+
+    // Callback that is invoked <em>exactly once</em>
+    // after the end of <em>all</em> test containers.
+    // Inherited from {@code CloseableResource}
+    public abstract void close() throws Throwable;
+}