Skip to content

feat: Add dedicated annotations for boolean, String, int and double flags in Junit5 extension #1478

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions tools/junit-openfeature/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@
<artifactId>junit-jupiter-api</artifactId>
</dependency>

<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-testkit</artifactId>
</dependency>

<dependency>
<groupId>org.junit-pioneer</groupId>
<artifactId>junit-pioneer</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package dev.openfeature.contrib.tools.junitopenfeature;

import java.lang.annotation.ElementType;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.junit.jupiter.api.extension.ExtendWith;

/**
* Repeatable annotation that allows you to define boolean feature flags for the default domain.
* Can be used as a standalone flag configuration but also within {@link OpenFeature}.
*/
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Repeatable(BooleanFlags.class)
@ExtendWith(OpenFeatureExtension.class)
public @interface BooleanFlag {
/**
* The key of the FeatureFlag.
*/
String name();

/**
* The value of the FeatureFlag.
*/
boolean value();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package dev.openfeature.contrib.tools.junitopenfeature;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.junit.jupiter.api.extension.ExtendWith;

/**
* Collection of {@link BooleanFlag} configurations.
*/
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@ExtendWith(OpenFeatureExtension.class)
public @interface BooleanFlags {
/**
* Collection of {@link BooleanFlag} configurations.
*/
BooleanFlag[] value() default {};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package dev.openfeature.contrib.tools.junitopenfeature;

import java.lang.annotation.ElementType;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.junit.jupiter.api.extension.ExtendWith;

/**
* Repeatable annotation that allows you to define double feature flags for the default domain.
* Can be used as a standalone flag configuration but also within {@link OpenFeature}.
*/
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Repeatable(DoubleFlags.class)
@ExtendWith(OpenFeatureExtension.class)
public @interface DoubleFlag {
/**
* The key of the FeatureFlag.
*/
String name();
/**
* The value of the FeatureFlag.
*/
double value();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package dev.openfeature.contrib.tools.junitopenfeature;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.junit.jupiter.api.extension.ExtendWith;

/**
* Collection of {@link DoubleFlag} configurations.
*/
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@ExtendWith(OpenFeatureExtension.class)
public @interface DoubleFlags {
/**
* Collection of {@link DoubleFlag} configurations.
*/
DoubleFlag[] value() default {};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package dev.openfeature.contrib.tools.junitopenfeature;

import java.lang.annotation.ElementType;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.junit.jupiter.api.extension.ExtendWith;

/**
* Repeatable annotation that allows you to define integer feature flags for the default domain.
* Can be used as a standalone flag configuration but also within {@link OpenFeature}.
*/
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Repeatable(IntegerFlags.class)
@ExtendWith(OpenFeatureExtension.class)
public @interface IntegerFlag {
/**
* The key of the FeatureFlag.
*/
String name();
/**
* The value of the FeatureFlag.
*/
int value();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package dev.openfeature.contrib.tools.junitopenfeature;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.junit.jupiter.api.extension.ExtendWith;

/**
* Collection of {@link IntegerFlag} configurations.
*/
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@ExtendWith(OpenFeatureExtension.class)
public @interface IntegerFlags {
/**
* Collection of {@link IntegerFlag} configurations.
*/
IntegerFlag[] value() default {};
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
/**
* Annotation for generating an extended configuration for OpenFeature.
* This annotation allows you to specify a list of flags for a specific domain.
* <p>
* Flags with duplicate names across different flag arrays
* (e.g., in {@link OpenFeature#value()} and {@link OpenFeature#booleanFlags()}
* or {@link OpenFeature#booleanFlags()} and {@link OpenFeature#stringFlags()})
* are not permitted and will result in an {@link IllegalArgumentException}.
*/
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
Expand All @@ -23,5 +28,21 @@
/**
* Collection of {@link Flag} configurations for this domain.
*/
Flag[] value();
Flag[] value() default {};
/**
* Collection of {@link BooleanFlag} configurations for this domain.
*/
BooleanFlag[] booleanFlags() default {};
/**
* Collection of {@link StringFlag} configurations for this domain.
*/
StringFlag[] stringFlags() default {};
/**
* Collection of {@link IntegerFlag} configurations for this domain.
*/
IntegerFlag[] integerFlags() default {};
/**
* Collection of {@link DoubleFlag} configurations for this domain.
*/
DoubleFlag[] doubleFlags() default {};
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@
import dev.openfeature.sdk.OpenFeatureAPI;
import dev.openfeature.sdk.providers.memory.Flag;
import java.lang.reflect.Method;
import java.util.AbstractMap;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.apache.commons.lang3.BooleanUtils;
import org.junit.jupiter.api.extension.AfterEachCallback;
import org.junit.jupiter.api.extension.BeforeEachCallback;
Expand All @@ -23,37 +29,127 @@ public class OpenFeatureExtension implements BeforeEachCallback, AfterEachCallba

private static Map<String, Map<String, Flag<?>>> handleExtendedConfiguration(
ExtensionContext extensionContext, Map<String, Map<String, Flag<?>>> configuration) {
PioneerAnnotationUtils.findAllEnclosingRepeatableAnnotations(extensionContext, OpenFeature.class)
.forEachOrdered(annotation -> {
Map<String, Flag<?>> domainFlags = configuration.getOrDefault(annotation.domain(), new HashMap<>());

Arrays.stream(annotation.value())
.filter(flag -> !domainFlags.containsKey(flag.name()))
.forEach(flag -> {
Flag.FlagBuilder<?> builder = generateFlagBuilder(flag);
domainFlags.put(flag.name(), builder.build());
});
configuration.put(annotation.domain(), domainFlags);
});
List<OpenFeature> openFeatureAnnotationList = PioneerAnnotationUtils.findAllEnclosingRepeatableAnnotations(
extensionContext, OpenFeature.class)
.collect(Collectors.toList());
Map<String, Set<String>> nonTypedFlagNamesByDomain = getFlagNamesByDomain(openFeatureAnnotationList);
openFeatureAnnotationList.forEach(annotation -> {
Map<String, Flag<?>> domainFlags = configuration.getOrDefault(annotation.domain(), new HashMap<>());

Arrays.stream(annotation.value())
.filter(flag -> !domainFlags.containsKey(flag.name()))
.forEach(flag -> {
Flag.FlagBuilder<?> builder = generateFlagBuilder(flag);
domainFlags.put(flag.name(), builder.build());
});
addTypedFlags(
annotation,
domainFlags,
nonTypedFlagNamesByDomain.getOrDefault(annotation.domain(), new HashSet<>()));
configuration.put(annotation.domain(), domainFlags);
});
return configuration;
}

private static Map<String, Set<String>> getFlagNamesByDomain(List<OpenFeature> openFeatureList) {
return openFeatureList.stream()
.map(o -> {
Set<String> flagNames = Arrays.stream(o.value())
.map(dev.openfeature.contrib.tools.junitopenfeature.Flag::name)
.collect(Collectors.toSet());
return new AbstractMap.SimpleEntry<>(o.domain(), flagNames);
})
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (t1, t2) -> {
t1.addAll(t2);
return t1;
}));
}

private static void addTypedFlags(OpenFeature annotation, Map<String, Flag<?>> domainFlags, Set<String> flagNames) {
addBooleanFlags(Arrays.stream(annotation.booleanFlags()), domainFlags, flagNames);
addStringFlags(Arrays.stream(annotation.stringFlags()), domainFlags, flagNames);
addIntegerFlags(Arrays.stream(annotation.integerFlags()), domainFlags, flagNames);
addDoubleFlags(Arrays.stream(annotation.doubleFlags()), domainFlags, flagNames);
}

private static void addBooleanFlags(
Stream<BooleanFlag> booleanFlags, Map<String, Flag<?>> domainFlags, Set<String> flagNames) {

booleanFlags.forEach(flag -> addFlag(domainFlags, flagNames, flag.name(), flag.value()));
}

private static void addStringFlags(
Stream<StringFlag> stringFlags, Map<String, Flag<?>> domainFlags, Set<String> flagNames) {
stringFlags.forEach(flag -> addFlag(domainFlags, flagNames, flag.name(), flag.value()));
}

private static void addIntegerFlags(
Stream<IntegerFlag> integerFlags, Map<String, Flag<?>> domainFlags, Set<String> flagNames) {
integerFlags.forEach(flag -> addFlag(domainFlags, flagNames, flag.name(), flag.value()));
}

private static void addDoubleFlags(
Stream<DoubleFlag> doubleFlags, Map<String, Flag<?>> domainFlags, Set<String> flagNames) {
doubleFlags.forEach(flag -> addFlag(domainFlags, flagNames, flag.name(), flag.value()));
}

private static <T> void addFlag(
Map<String, Flag<?>> domainFlags, Set<String> domainFlagNames, String flagName, T value) {
if (domainFlagNames.contains(flagName)) {
throw new IllegalArgumentException("Flag with name " + flagName + " already exists. "
+ "There shouldn't be @Flag and @" + value.getClass().getSimpleName() + "Flag with the same name!");
}

if (domainFlags.containsKey(flagName)) {
return;
}
Comment on lines +98 to +105
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One of these checks is redundant. I think we could even remove the domainFlagNames set entirely, as it contains the same information as the domainFlags map.

Flag.FlagBuilder<Object> builder =
Flag.builder().variant(String.valueOf(value), value).defaultVariant(String.valueOf(value));
domainFlags.put(flagName, builder.build());
}

private static Map<String, Map<String, Flag<?>>> handleSimpleConfiguration(ExtensionContext extensionContext) {
Map<String, Map<String, Flag<?>>> configuration = new HashMap<>();
String defaultDomain = PioneerAnnotationUtils.findClosestEnclosingAnnotation(
extensionContext, OpenFeatureDefaultDomain.class)
.map(OpenFeatureDefaultDomain::value)
.orElse("");
PioneerAnnotationUtils.findAllEnclosingRepeatableAnnotations(
extensionContext, dev.openfeature.contrib.tools.junitopenfeature.Flag.class)
.forEachOrdered(flag -> {
Map<String, Flag<?>> domainFlags = configuration.getOrDefault(defaultDomain, new HashMap<>());
if (!domainFlags.containsKey(flag.name())) {
Flag.FlagBuilder<?> builder = generateFlagBuilder(flag);
domainFlags.put(flag.name(), builder.build());
configuration.put(defaultDomain, domainFlags);
}
});
Map<String, Flag<?>> domainFlags = configuration.getOrDefault(defaultDomain, new HashMap<>());
List<dev.openfeature.contrib.tools.junitopenfeature.Flag> flagList =
PioneerAnnotationUtils.findAllEnclosingRepeatableAnnotations(
extensionContext, dev.openfeature.contrib.tools.junitopenfeature.Flag.class)
.collect(Collectors.toList());
Set<String> flagNames = flagList.stream()
.map(dev.openfeature.contrib.tools.junitopenfeature.Flag::name)
.collect(Collectors.toSet());

flagList.forEach(flag -> {
if (!domainFlags.containsKey(flag.name())) {
Flag.FlagBuilder<?> builder = generateFlagBuilder(flag);
domainFlags.put(flag.name(), builder.build());
configuration.put(defaultDomain, domainFlags);
}
});

Stream<BooleanFlag> booleanFlags =
PioneerAnnotationUtils.findAllEnclosingRepeatableAnnotations(extensionContext, BooleanFlag.class);
addBooleanFlags(booleanFlags, domainFlags, flagNames);

Stream<StringFlag> stringFlags =
PioneerAnnotationUtils.findAllEnclosingRepeatableAnnotations(extensionContext, StringFlag.class);
addStringFlags(stringFlags, domainFlags, flagNames);

Stream<IntegerFlag> integerFlags =
PioneerAnnotationUtils.findAllEnclosingRepeatableAnnotations(extensionContext, IntegerFlag.class);
addIntegerFlags(integerFlags, domainFlags, flagNames);

Stream<DoubleFlag> doubleFlags =
PioneerAnnotationUtils.findAllEnclosingRepeatableAnnotations(extensionContext, DoubleFlag.class);
addDoubleFlags(doubleFlags, domainFlags, flagNames);

if (!domainFlags.isEmpty()) {
configuration.put(defaultDomain, domainFlags);
}

return configuration;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package dev.openfeature.contrib.tools.junitopenfeature;

import java.lang.annotation.ElementType;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.junit.jupiter.api.extension.ExtendWith;

/**
* Repeatable annotation that allows you to define String feature flags for the default domain.
* Can be used as a standalone flag configuration but also within {@link OpenFeature}.
*/
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Repeatable(StringFlags.class)
@ExtendWith(OpenFeatureExtension.class)
public @interface StringFlag {
/**
* The key of the FeatureFlag.
*/
String name();

/**
* The value of the FeatureFlag.
*/
String value();
}
Loading
Loading