-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: liran2000 <[email protected]>
- Loading branch information
Showing
8 changed files
with
518 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package dev.openfeature.sdk.testutils; | ||
|
||
import io.cucumber.core.internal.com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.ToString; | ||
|
||
import java.util.Map; | ||
|
||
@ToString | ||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
@NoArgsConstructor | ||
@Getter | ||
public class Flag { | ||
private Flags.State state; | ||
private Map<String, Object> variants; | ||
private String defaultVariant; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package dev.openfeature.sdk.testutils; | ||
|
||
import io.cucumber.core.internal.com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
import io.cucumber.core.internal.com.fasterxml.jackson.core.JsonProcessingException; | ||
import io.cucumber.core.internal.com.fasterxml.jackson.databind.ObjectMapper; | ||
import lombok.Getter; | ||
import lombok.ToString; | ||
|
||
import java.util.Map; | ||
|
||
@ToString | ||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
@Getter | ||
public class Flags { | ||
|
||
public static class FlagsBuilder { | ||
|
||
private String configurationJson; | ||
|
||
private ObjectMapper objectMapper = new ObjectMapper(); | ||
|
||
private FlagsBuilder() { | ||
|
||
} | ||
|
||
public FlagsBuilder setConfigurationJson(String configurationJson) { | ||
this.configurationJson = configurationJson; | ||
return this; | ||
} | ||
|
||
public Flags build() throws JsonProcessingException { | ||
return objectMapper.readValue(configurationJson, Flags.class); | ||
} | ||
|
||
} | ||
|
||
public static FlagsBuilder builder() { | ||
return new FlagsBuilder(); | ||
} | ||
|
||
private Map<String, Flag> flags; | ||
|
||
public enum State { | ||
ENABLED, DISABLED | ||
} | ||
|
||
public enum Variant { | ||
on, off | ||
} | ||
|
||
@Getter | ||
public class Variants { | ||
private Map<String, Object> variants; | ||
} | ||
} |
181 changes: 181 additions & 0 deletions
181
src/test/java/dev/openfeature/sdk/testutils/InMemoryProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
package dev.openfeature.sdk.testutils; | ||
|
||
import dev.openfeature.sdk.*; | ||
import lombok.Getter; | ||
import lombok.SneakyThrows; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
/** | ||
* In-memory provider. | ||
* | ||
* Based on flagd configuration. | ||
*/ | ||
@Slf4j | ||
public class InMemoryProvider implements FeatureProvider { | ||
|
||
@Getter | ||
private final String name = "InMemoryProvider"; | ||
|
||
private Flags flags; | ||
|
||
private String jsonConfig; | ||
|
||
@Getter | ||
private ProviderState state = ProviderState.NOT_READY; | ||
|
||
@Override | ||
public Metadata getMetadata() { | ||
return new Metadata() { | ||
@Override | ||
public String getName() { | ||
return name; | ||
} | ||
}; | ||
} | ||
|
||
public InMemoryProvider(String jsonConfig) { | ||
this.jsonConfig = jsonConfig; | ||
} | ||
|
||
public void initialize(EvaluationContext evaluationContext) throws Exception { | ||
FeatureProvider.super.initialize(evaluationContext); | ||
this.flags = Flags.builder().setConfigurationJson(jsonConfig).build(); | ||
state = ProviderState.READY; | ||
log.info("finishing initializing provider, state: {}", state); | ||
} | ||
|
||
@Override | ||
public ProviderEvaluation<Boolean> getBooleanEvaluation(String key, Boolean defaultValue, EvaluationContext ctx) { | ||
Flag flag = flags.getFlags().get(key); | ||
if (flag == null) { | ||
return ProviderEvaluation.<Boolean>builder() | ||
.value(defaultValue) | ||
.reason(Reason.ERROR.toString()) | ||
.errorMessage(ErrorCode.FLAG_NOT_FOUND.name()) | ||
.errorCode(ErrorCode.FLAG_NOT_FOUND) | ||
.build(); | ||
} | ||
if (!(flag.getVariants().get(flag.getDefaultVariant()) instanceof Boolean)) { | ||
return ProviderEvaluation.<Boolean>builder() | ||
.value(defaultValue) | ||
.variant(flag.getDefaultVariant()) | ||
.reason(Reason.ERROR.toString()) | ||
.errorMessage(ErrorCode.TYPE_MISMATCH.name()) | ||
.errorCode(ErrorCode.TYPE_MISMATCH) | ||
.build(); | ||
} | ||
boolean value = (boolean) flag.getVariants().get(flag.getDefaultVariant()); | ||
return ProviderEvaluation.<Boolean>builder() | ||
.value(value) | ||
.variant(flag.getDefaultVariant()) | ||
.reason(Reason.STATIC.toString()) | ||
.build(); | ||
} | ||
|
||
@Override | ||
public ProviderEvaluation<String> getStringEvaluation(String key, String defaultValue, EvaluationContext ctx) { | ||
Flag flag = flags.getFlags().get(key); | ||
if (flag == null) { | ||
ProviderEvaluation<String> providerEvaluation = ProviderEvaluation.<String>builder() | ||
.value(defaultValue) | ||
.reason(Reason.ERROR.toString()) | ||
.errorMessage(ErrorCode.FLAG_NOT_FOUND.name()) | ||
.errorCode(ErrorCode.FLAG_NOT_FOUND) | ||
.build(); | ||
return providerEvaluation; | ||
} | ||
if (!(flag.getVariants().get(flag.getDefaultVariant()) instanceof String)) { | ||
return ProviderEvaluation.<String>builder() | ||
.value(defaultValue) | ||
.variant(flag.getDefaultVariant()) | ||
.reason(Reason.ERROR.toString()) | ||
.errorMessage(ErrorCode.TYPE_MISMATCH.name()) | ||
.errorCode(ErrorCode.TYPE_MISMATCH) | ||
.build(); | ||
} | ||
String value = (String) flag.getVariants().get(flag.getDefaultVariant()); | ||
return ProviderEvaluation.<String>builder() | ||
.value(value) | ||
.variant(flag.getDefaultVariant()) | ||
.reason(Reason.STATIC.toString()) | ||
.build(); | ||
} | ||
|
||
@Override | ||
public ProviderEvaluation<Integer> getIntegerEvaluation(String key, Integer defaultValue, EvaluationContext ctx) { | ||
Flag flag = flags.getFlags().get(key); | ||
if (flag == null) { | ||
return ProviderEvaluation.<Integer>builder() | ||
.value(defaultValue) | ||
.reason(Reason.ERROR.toString()) | ||
.errorMessage(ErrorCode.FLAG_NOT_FOUND.name()) | ||
.errorCode(ErrorCode.FLAG_NOT_FOUND) | ||
.build(); | ||
} | ||
if (!(flag.getVariants().get(flag.getDefaultVariant()) instanceof Integer)) { | ||
return ProviderEvaluation.<Integer>builder() | ||
.value(defaultValue) | ||
.variant(flag.getDefaultVariant()) | ||
.reason(Reason.ERROR.toString()) | ||
.errorMessage(ErrorCode.TYPE_MISMATCH.name()) | ||
.errorCode(ErrorCode.TYPE_MISMATCH) | ||
.build(); | ||
} | ||
Integer value = (Integer) flag.getVariants().get(flag.getDefaultVariant()); | ||
return ProviderEvaluation.<Integer>builder() | ||
.value(value) | ||
.variant(flag.getDefaultVariant()) | ||
.reason(Reason.STATIC.toString()) | ||
.build(); | ||
} | ||
|
||
@Override | ||
public ProviderEvaluation<Double> getDoubleEvaluation(String key, Double defaultValue, EvaluationContext ctx) { | ||
Flag flag = flags.getFlags().get(key); | ||
if (flag == null) { | ||
return ProviderEvaluation.<Double>builder() | ||
.value(defaultValue) | ||
.reason(Reason.ERROR.toString()) | ||
.errorMessage(ErrorCode.FLAG_NOT_FOUND.name()) | ||
.errorCode(ErrorCode.FLAG_NOT_FOUND) | ||
.build(); | ||
} | ||
if (!(flag.getVariants().get(flag.getDefaultVariant()) instanceof Double)) { | ||
return ProviderEvaluation.<Double>builder() | ||
.value(defaultValue) | ||
.variant(flag.getDefaultVariant()) | ||
.reason(Reason.ERROR.toString()) | ||
.errorMessage(ErrorCode.TYPE_MISMATCH.name()) | ||
.errorCode(ErrorCode.TYPE_MISMATCH) | ||
.build(); | ||
} | ||
Double value = (Double) flag.getVariants().get(flag.getDefaultVariant()); | ||
return ProviderEvaluation.<Double>builder() | ||
.value(value) | ||
.variant(flag.getDefaultVariant()) | ||
.reason(Reason.STATIC.toString()) | ||
.build(); | ||
} | ||
|
||
@SneakyThrows | ||
@Override | ||
public ProviderEvaluation<Value> getObjectEvaluation(String key, Value defaultValue, | ||
EvaluationContext invocationContext) { | ||
Flag flag = flags.getFlags().get(key); | ||
if (flag == null) { | ||
return ProviderEvaluation.<Value>builder() | ||
.value(defaultValue) | ||
.reason(Reason.ERROR.toString()) | ||
.errorMessage(ErrorCode.FLAG_NOT_FOUND.name()) | ||
.errorCode(ErrorCode.FLAG_NOT_FOUND) | ||
.build(); | ||
} | ||
Object object = flag.getVariants().get(flag.getDefaultVariant()); | ||
Value value = ValueUtils.convert(object); | ||
return ProviderEvaluation.<Value>builder() | ||
.value(value) | ||
.variant(flag.getDefaultVariant()) | ||
.reason(Reason.STATIC.toString()) | ||
.build(); | ||
} | ||
} |
Oops, something went wrong.