Skip to content

Commit c56b390

Browse files
committed
Tests which validate the backend providers are actually called.
Fixes #10
1 parent 3594044 commit c56b390

File tree

2 files changed

+42
-10
lines changed

2 files changed

+42
-10
lines changed

lib/src/main/java/dev/openfeature/javasdk/OpenFeatureClient.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ <T> FlagEvaluationDetails<T> evaluateFlag(FlagValueType type, String key, T defa
6565
if (type == FlagValueType.BOOLEAN) {
6666
// TODO: Can we guarantee that defaultValue is a boolean? If not, how to we handle that?
6767
providerEval = (ProviderEvaluation<T>) provider.getBooleanEvaluation(key, (Boolean) defaultValue, invocationContext, options);
68+
} else if(type == FlagValueType.STRING) {
69+
providerEval = (ProviderEvaluation<T>) provider.getStringEvaluation(key, (String) defaultValue, invocationContext, options);
70+
} else if (type == FlagValueType.INTEGER) {
71+
providerEval = (ProviderEvaluation<T>) provider.getIntegerEvaluation(key, (Integer) defaultValue, invocationContext, options);
6872
} else {
6973
// TODO: Support other flag types.
7074
throw new GeneralError("Unknown flag type");

lib/src/test/java/dev/openfeature/javasdk/FlagEvaluationSpecTests.java

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -81,20 +81,48 @@ Client _client() {
8181
@Specification(spec="flag evaluation", number="1.8.1",text="The client MUST provide methods for typed flag " +
8282
"evaluation, including boolean, numeric, string, and structure.")
8383
@Test void value_flags() {
84-
Client c = _client();
84+
OpenFeatureAPI api = OpenFeatureAPI.getInstance();
85+
api.setProvider(new FeatureProvider() {
86+
@Override
87+
public String getName() {
88+
return "test";
89+
}
90+
91+
@Override
92+
public ProviderEvaluation<Boolean> getBooleanEvaluation(String key, Boolean defaultValue, EvaluationContext ctx, FlagEvaluationOptions options) {
93+
return ProviderEvaluation.<Boolean>builder()
94+
.value(!defaultValue).build();
95+
}
96+
97+
@Override
98+
public ProviderEvaluation<String> getStringEvaluation(String key, String defaultValue, EvaluationContext ctx, FlagEvaluationOptions options) {
99+
return ProviderEvaluation.<String>builder()
100+
.value(new StringBuilder(defaultValue).reverse().toString())
101+
.build();
102+
}
103+
104+
@Override
105+
public ProviderEvaluation<Integer> getIntegerEvaluation(String key, Integer defaultValue, EvaluationContext ctx, FlagEvaluationOptions options) {
106+
return ProviderEvaluation.<Integer>builder()
107+
.value(defaultValue * 100)
108+
.build();
109+
}
110+
});
111+
Client c = api.getClient();
85112
String key = "key";
86-
assertFalse(c.getBooleanValue(key, false));
87-
assertFalse(c.getBooleanValue(key, false, new EvaluationContext()));
88-
assertFalse(c.getBooleanValue(key, false, new EvaluationContext(), FlagEvaluationOptions.builder().build()));
113+
114+
assertEquals(true, c.getBooleanValue(key, false));
115+
assertEquals(true, c.getBooleanValue(key, false, new EvaluationContext()));
116+
assertEquals(true, c.getBooleanValue(key, false, new EvaluationContext(), FlagEvaluationOptions.builder().build()));
89117

90118

91-
assertEquals("my-string", c.getStringValue(key, "my-string"));
92-
assertEquals("my-string", c.getStringValue(key, "my-string", new EvaluationContext()));
93-
assertEquals("my-string", c.getStringValue(key, "my-string", new EvaluationContext(), FlagEvaluationOptions.builder().build()));
119+
assertEquals("gnirts-ym", c.getStringValue(key, "my-string"));
120+
assertEquals("gnirts-ym", c.getStringValue(key, "my-string", new EvaluationContext()));
121+
assertEquals("gnirts-ym", c.getStringValue(key, "my-string", new EvaluationContext(), FlagEvaluationOptions.builder().build()));
94122

95-
assertEquals(4, c.getIntegerValue(key, 4));
96-
assertEquals(4, c.getIntegerValue(key, 4, new EvaluationContext()));
97-
assertEquals(4, c.getIntegerValue(key, 4, new EvaluationContext(), FlagEvaluationOptions.builder().build()));
123+
assertEquals(400, c.getIntegerValue(key, 4));
124+
assertEquals(400, c.getIntegerValue(key, 4, new EvaluationContext()));
125+
assertEquals(400, c.getIntegerValue(key, 4, new EvaluationContext(), FlagEvaluationOptions.builder().build()));
98126

99127
}
100128

0 commit comments

Comments
 (0)