82 lines
2.9 KiB
Java
82 lines
2.9 KiB
Java
package dev.openfeature.sdk.testutils;
|
|
|
|
import com.google.common.collect.ImmutableMap;
|
|
import dev.openfeature.sdk.Value;
|
|
import dev.openfeature.sdk.providers.memory.Flag;
|
|
import lombok.experimental.UtilityClass;
|
|
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
|
|
import static dev.openfeature.sdk.Structure.mapToStructure;
|
|
|
|
/**
|
|
* Test flags utils.
|
|
*/
|
|
@UtilityClass
|
|
public class TestFlagsUtils {
|
|
|
|
public static final String BOOLEAN_FLAG_KEY = "boolean-flag";
|
|
public static final String STRING_FLAG_KEY = "string-flag";
|
|
public static final String INT_FLAG_KEY = "integer-flag";
|
|
public static final String FLOAT_FLAG_KEY = "float-flag";
|
|
public static final String OBJECT_FLAG_KEY = "object-flag";
|
|
public static final String CONTEXT_AWARE_FLAG_KEY = "context-aware";
|
|
public static final String WRONG_FLAG_KEY = "wrong-flag";
|
|
|
|
/**
|
|
* Building flags for testing purposes.
|
|
* @return map of flags
|
|
*/
|
|
public static Map<String, Flag<?>> buildFlags() {
|
|
Map<String, Flag<?>> flags = new HashMap<>();
|
|
flags.put(BOOLEAN_FLAG_KEY, Flag.builder()
|
|
.variant("on", true)
|
|
.variant("off", false)
|
|
.defaultVariant("on")
|
|
.build());
|
|
flags.put(STRING_FLAG_KEY, Flag.builder()
|
|
.variant("greeting", "hi")
|
|
.variant("parting", "bye")
|
|
.defaultVariant("greeting")
|
|
.build());
|
|
flags.put(INT_FLAG_KEY, Flag.builder()
|
|
.variant("one", 1)
|
|
.variant("ten", 10)
|
|
.defaultVariant("ten")
|
|
.build());
|
|
flags.put(FLOAT_FLAG_KEY, Flag.builder()
|
|
.variant("tenth", 0.1)
|
|
.variant("half", 0.5)
|
|
.defaultVariant("half")
|
|
.build());
|
|
flags.put(OBJECT_FLAG_KEY, Flag.builder()
|
|
.variant("empty", new HashMap<>())
|
|
.variant("template", new Value(mapToStructure(ImmutableMap.of(
|
|
"showImages", new Value(true),
|
|
"title", new Value("Check out these pics!"),
|
|
"imagesPerPage", new Value(100)
|
|
))))
|
|
.defaultVariant("template")
|
|
.build());
|
|
flags.put(CONTEXT_AWARE_FLAG_KEY, Flag.<String>builder()
|
|
.variant("internal", "INTERNAL")
|
|
.variant("external", "EXTERNAL")
|
|
.defaultVariant("external")
|
|
.contextEvaluator((flag, evaluationContext) -> {
|
|
if (new Value(false).equals(evaluationContext.getValue("customer"))) {
|
|
return (String) flag.getVariants().get("internal");
|
|
} else {
|
|
return (String) flag.getVariants().get(flag.getDefaultVariant());
|
|
}
|
|
})
|
|
.build());
|
|
flags.put(WRONG_FLAG_KEY, Flag.builder()
|
|
.variant("one", "uno")
|
|
.variant("two", "dos")
|
|
.defaultVariant("one")
|
|
.build());
|
|
return flags;
|
|
}
|
|
}
|