Skip to content

Commit c9fae1b

Browse files
committed
Some tests around details
1 parent c56b390 commit c9fae1b

File tree

3 files changed

+70
-41
lines changed

3 files changed

+70
-41
lines changed

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ <T> FlagEvaluationDetails<T> evaluateFlag(FlagValueType type, String key, T defa
7070
} else if (type == FlagValueType.INTEGER) {
7171
providerEval = (ProviderEvaluation<T>) provider.getIntegerEvaluation(key, (Integer) defaultValue, invocationContext, options);
7272
} else {
73-
// TODO: Support other flag types.
7473
throw new GeneralError("Unknown flag type");
7574
}
7675

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package dev.openfeature.javasdk;
2+
3+
public class DoSomethingProvider implements FeatureProvider {
4+
@Override
5+
public String getName() {
6+
return "test";
7+
}
8+
9+
@Override
10+
public ProviderEvaluation<Boolean> getBooleanEvaluation(String key, Boolean defaultValue, EvaluationContext ctx, FlagEvaluationOptions options) {
11+
return ProviderEvaluation.<Boolean>builder()
12+
.value(!defaultValue).build();
13+
}
14+
15+
@Override
16+
public ProviderEvaluation<String> getStringEvaluation(String key, String defaultValue, EvaluationContext ctx, FlagEvaluationOptions options) {
17+
return ProviderEvaluation.<String>builder()
18+
.value(new StringBuilder(defaultValue).reverse().toString())
19+
.build();
20+
}
21+
22+
@Override
23+
public ProviderEvaluation<Integer> getIntegerEvaluation(String key, Integer defaultValue, EvaluationContext ctx, FlagEvaluationOptions options) {
24+
return ProviderEvaluation.<Integer>builder()
25+
.value(defaultValue * 100)
26+
.build();
27+
}
28+
}

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

Lines changed: 42 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -82,32 +82,7 @@ Client _client() {
8282
"evaluation, including boolean, numeric, string, and structure.")
8383
@Test void value_flags() {
8484
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-
});
85+
api.setProvider(new DoSomethingProvider());
11186
Client c = api.getClient();
11287
String key = "key";
11388

@@ -146,21 +121,37 @@ public ProviderEvaluation<Integer> getIntegerEvaluation(String key, Integer defa
146121
"field.")
147122
@Specification(spec="flag evaluation", number="1.12", text="The evaluation details structure's flag key field MUST " +
148123
"contain the flag key argument passed to the detailed flag evaluation method.")
149-
@Specification(spec="flag evaluation", number="1.13", text="In cases of normal execution, the evaluation details " +
150-
"structure's variant field MUST contain the value of the variant field in the flag resolution structure " +
151-
"returned by the configured provider, if the field is set.")
152-
@Specification(spec="flag evaluation", number="1.14", text="In cases of normal execution, the evaluation details " +
153-
"structure's reason field MUST contain the value of the reason field in the flag resolution structure " +
154-
"returned by the configured provider, if the field is set.")
155-
@Specification(spec="flag evaluation", number="1.15", text="In cases of abnormal execution, the evaluation details " +
156-
"structure's error code field MUST identify an error occurred during flag evaluation, having possible " +
157-
"values PROVIDER_NOT_READY, FLAG_NOT_FOUND, PARSE_ERROR, TYPE_MISMATCH, or GENERAL.")
158-
@Specification(spec="flag evaluation", number="1.16", text="In cases of abnormal execution (network failure, " +
159-
"unhandled error, etc) the reason field in the evaluation details SHOULD indicate an error.")
160-
@Disabled
161124
@Test void detail_flags() {
162-
// TODO: Add tests re: detail functions.
163-
throw new NotImplementedException();
125+
OpenFeatureAPI api = OpenFeatureAPI.getInstance();
126+
api.setProvider(new DoSomethingProvider());
127+
Client c = api.getClient();
128+
String key = "key";
129+
130+
FlagEvaluationDetails<Boolean> bd = FlagEvaluationDetails.<Boolean>builder()
131+
.flagKey(key)
132+
.value(false)
133+
.variant(null)
134+
.build();
135+
assertEquals(bd, c.getBooleanDetails(key, true));
136+
assertEquals(bd, c.getBooleanDetails(key, true, new EvaluationContext()));
137+
assertEquals(bd, c.getBooleanDetails(key, true, new EvaluationContext(), FlagEvaluationOptions.builder().build()));
138+
139+
FlagEvaluationDetails<String> sd = FlagEvaluationDetails.<String>builder()
140+
.flagKey(key)
141+
.value("tset")
142+
.variant(null)
143+
.build();
144+
assertEquals(sd, c.getStringDetails(key, "test"));
145+
assertEquals(sd, c.getStringDetails(key, "test", new EvaluationContext()));
146+
assertEquals(sd, c.getStringDetails(key, "test", new EvaluationContext(), FlagEvaluationOptions.builder().build()));
147+
148+
FlagEvaluationDetails<Integer> id = FlagEvaluationDetails.<Integer>builder()
149+
.flagKey(key)
150+
.value(400)
151+
.build();
152+
assertEquals(id, c.getIntegerDetails(key, 4));
153+
assertEquals(id, c.getIntegerDetails(key, 4, new EvaluationContext()));
154+
assertEquals(id, c.getIntegerDetails(key, 4, new EvaluationContext(), FlagEvaluationOptions.builder().build()));
164155
}
165156

166157
@Specification(spec="flag evaluation", number="1.17", text="The evaluation options structure's hooks field denotes " +
@@ -199,6 +190,17 @@ public ProviderEvaluation<Integer> getIntegerEvaluation(String key, Integer defa
199190
@Specification(spec="flag evaluation", number="1.21", text="The client MUST transform the evaluation context using " +
200191
"the provider's context transformer function, before passing the result of the transformation to the " +
201192
"provider's flag resolution functions.")
193+
@Specification(spec="flag evaluation", number="1.13", text="In cases of normal execution, the evaluation details " +
194+
"structure's variant field MUST contain the value of the variant field in the flag resolution structure " +
195+
"returned by the configured provider, if the field is set.")
196+
@Specification(spec="flag evaluation", number="1.14", text="In cases of normal execution, the evaluation details " +
197+
"structure's reason field MUST contain the value of the reason field in the flag resolution structure " +
198+
"returned by the configured provider, if the field is set.")
199+
@Specification(spec="flag evaluation", number="1.15", text="In cases of abnormal execution, the evaluation details " +
200+
"structure's error code field MUST identify an error occurred during flag evaluation, having possible " +
201+
"values PROVIDER_NOT_READY, FLAG_NOT_FOUND, PARSE_ERROR, TYPE_MISMATCH, or GENERAL.")
202+
@Specification(spec="flag evaluation", number="1.16", text="In cases of abnormal execution (network failure, " +
203+
"unhandled error, etc) the reason field in the evaluation details SHOULD indicate an error.")
202204
@Test @Disabled void todo() {}
203205

204206
@Specification(spec="flag evaluation", number="1.20", text="The client SHOULD provide asynchronous or non-blocking " +

0 commit comments

Comments
 (0)