5
5
import dev .openfeature .sdk .providers .memory .Flag ;
6
6
import dev .openfeature .sdk .providers .memory .InMemoryProvider ;
7
7
import 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 ;
9
11
import org .junitpioneer .internal .PioneerAnnotationUtils ;
10
12
11
13
import java .util .Arrays ;
12
14
import java .util .HashMap ;
13
15
import java .util .Map ;
14
16
15
- public class OpenFeatureExtension implements BeforeEachCallback , AfterEachCallback , ParameterResolver {
17
+ /**
18
+ * JUnit5 Extension for OpenFeature.
19
+ */
20
+ public class OpenFeatureExtension implements BeforeEachCallback , AfterEachCallback {
16
21
17
22
OpenFeatureAPI api = OpenFeatureAPI .getInstance ();
18
23
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 )
55
30
.forEachOrdered (annotation -> {
56
31
Map <String , Flag <?>> domainFlags = configuration .getOrDefault (annotation .domain (), new HashMap <>());
57
32
@@ -68,10 +43,13 @@ private static Map<String, Map<String, Flag<?>>> handleExtendedConfiguration(Ext
68
43
69
44
private static Map <String , Map <String , Flag <?>>> handleSimpleConfiguration (ExtensionContext extensionContext ) {
70
45
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 )
75
53
.forEachOrdered (flag -> {
76
54
Map <String , Flag <?>> domainFlags = configuration .getOrDefault (defaultDomain , new HashMap <>());
77
55
if (!domainFlags .containsKey (flag .name ())) {
@@ -103,13 +81,34 @@ private static Flag.FlagBuilder<?> generateFlagBuilder(dev.openfeature.contrib.t
103
81
}
104
82
105
83
@ 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
+ }
108
95
}
109
96
110
97
@ 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 );
113
112
}
114
113
115
114
private ExtensionContext .Store getStore (ExtensionContext context ) {
0 commit comments