55import dev .openfeature .sdk .providers .memory .Flag ;
66import dev .openfeature .sdk .providers .memory .InMemoryProvider ;
77import org .apache .commons .lang3 .BooleanUtils ;
8- import org .junit .jupiter .api .extension .*;
8+ import org .junit .jupiter .api .extension .AfterEachCallback ;
9+ import org .junit .jupiter .api .extension .BeforeEachCallback ;
10+ import org .junit .jupiter .api .extension .ExtensionContext ;
911import org .junitpioneer .internal .PioneerAnnotationUtils ;
1012
1113import java .util .Arrays ;
1214import java .util .HashMap ;
1315import java .util .Map ;
1416
15- public class OpenFeatureExtension implements BeforeEachCallback , AfterEachCallback , ParameterResolver {
17+ /**
18+ * JUnit5 Extension for OpenFeature.
19+ */
20+ public class OpenFeatureExtension implements BeforeEachCallback , AfterEachCallback {
1621
1722 OpenFeatureAPI api = OpenFeatureAPI .getInstance ();
1823
19-
20- @ Override
21- public void afterEach (ExtensionContext extensionContext ) throws Exception {
22-
23- @ SuppressWarnings ("unchecked" ) Map <String , Map <String , Flag <?>>> configuration =
24- (Map <String , Map <String , Flag <?>>>) getStore (extensionContext ).get ("config" );
25- for (Map .Entry <String , Map <String , Flag <?>>> stringMapEntry : configuration .entrySet ()) {
26- InMemoryProvider inMemoryProvider = new InMemoryProvider (stringMapEntry .getValue ());
27- if (stringMapEntry .getKey ().isEmpty ()) {
28- api .setProvider (new NoOpProvider ());
29- } else {
30- api .setProvider (stringMapEntry .getKey (), new NoOpProvider ());
31- }
32- }
33- }
34-
35-
36- @ Override
37- public void beforeEach (ExtensionContext extensionContext ) throws Exception {
38- Map <String , Map <String , Flag <?>>> configuration = handleSimpleConfiguration (extensionContext );
39- configuration .putAll (handleExtendedConfiguration (extensionContext , configuration ));
40-
41- for (Map .Entry <String , Map <String , Flag <?>>> stringMapEntry : configuration .entrySet ()) {
42- InMemoryProvider inMemoryProvider = new InMemoryProvider (stringMapEntry .getValue ());
43- if (stringMapEntry .getKey ().isEmpty ()) {
44- api .setProvider (inMemoryProvider );
45- } else {
46- api .setProvider (stringMapEntry .getKey (), inMemoryProvider );
47- }
48- }
49-
50- getStore (extensionContext ).put ("config" , configuration );
51- }
52-
53- private static Map <String , Map <String , Flag <?>>> handleExtendedConfiguration (ExtensionContext extensionContext , Map <String , Map <String , Flag <?>>> configuration ) {
54- PioneerAnnotationUtils .findAllEnclosingRepeatableAnnotations (extensionContext , OpenFeature .class )
24+ private static Map <String , Map <String , Flag <?>>> handleExtendedConfiguration (
25+ ExtensionContext extensionContext ,
26+ Map <String , Map <String , Flag <?>>> configuration
27+ ) {
28+ PioneerAnnotationUtils
29+ .findAllEnclosingRepeatableAnnotations (extensionContext , OpenFeature .class )
5530 .forEachOrdered (annotation -> {
5631 Map <String , Flag <?>> domainFlags = configuration .getOrDefault (annotation .domain (), new HashMap <>());
5732
@@ -68,10 +43,13 @@ private static Map<String, Map<String, Flag<?>>> handleExtendedConfiguration(Ext
6843
6944 private static Map <String , Map <String , Flag <?>>> handleSimpleConfiguration (ExtensionContext extensionContext ) {
7045 Map <String , Map <String , Flag <?>>> configuration = new HashMap <>();
71- String defaultDomain = PioneerAnnotationUtils .findClosestEnclosingAnnotation (extensionContext , OpenFeatureDefaultDomain .class )
72- .map (OpenFeatureDefaultDomain ::value )
73- .orElse ("" );
74- PioneerAnnotationUtils .findAllEnclosingRepeatableAnnotations (extensionContext , dev .openfeature .contrib .tools .junitopenfeature .Flag .class )
46+ String defaultDomain = PioneerAnnotationUtils
47+ .findClosestEnclosingAnnotation (extensionContext , OpenFeatureDefaultDomain .class )
48+ .map (OpenFeatureDefaultDomain ::value ).orElse ("" );
49+ PioneerAnnotationUtils
50+ .findAllEnclosingRepeatableAnnotations (
51+ extensionContext ,
52+ dev .openfeature .contrib .tools .junitopenfeature .Flag .class )
7553 .forEachOrdered (flag -> {
7654 Map <String , Flag <?>> domainFlags = configuration .getOrDefault (defaultDomain , new HashMap <>());
7755 if (!domainFlags .containsKey (flag .name ())) {
@@ -103,13 +81,34 @@ private static Flag.FlagBuilder<?> generateFlagBuilder(dev.openfeature.contrib.t
10381 }
10482
10583 @ Override
106- public boolean supportsParameter (ParameterContext parameterContext , ExtensionContext extensionContext ) throws ParameterResolutionException {
107- return false ;
84+ public void afterEach (ExtensionContext extensionContext ) throws Exception {
85+
86+ @ SuppressWarnings ("unchecked" ) Map <String , Map <String , Flag <?>>> configuration =
87+ (Map <String , Map <String , Flag <?>>>) getStore (extensionContext ).get ("config" );
88+ for (Map .Entry <String , Map <String , Flag <?>>> stringMapEntry : configuration .entrySet ()) {
89+ if (stringMapEntry .getKey ().isEmpty ()) {
90+ api .setProvider (new NoOpProvider ());
91+ } else {
92+ api .setProvider (stringMapEntry .getKey (), new NoOpProvider ());
93+ }
94+ }
10895 }
10996
11097 @ Override
111- public Object resolveParameter (ParameterContext parameterContext , ExtensionContext extensionContext ) throws ParameterResolutionException {
112- return null ;
98+ public void beforeEach (ExtensionContext extensionContext ) throws Exception {
99+ Map <String , Map <String , Flag <?>>> configuration = handleSimpleConfiguration (extensionContext );
100+ configuration .putAll (handleExtendedConfiguration (extensionContext , configuration ));
101+
102+ for (Map .Entry <String , Map <String , Flag <?>>> stringMapEntry : configuration .entrySet ()) {
103+ InMemoryProvider inMemoryProvider = new InMemoryProvider (stringMapEntry .getValue ());
104+ if (stringMapEntry .getKey ().isEmpty ()) {
105+ api .setProvider (inMemoryProvider );
106+ } else {
107+ api .setProvider (stringMapEntry .getKey (), inMemoryProvider );
108+ }
109+ }
110+
111+ getStore (extensionContext ).put ("config" , configuration );
113112 }
114113
115114 private ExtensionContext .Store getStore (ExtensionContext context ) {
0 commit comments