Skip to content

Commit 81e68f9

Browse files
committed
test fixes and unit tests
Signed-off-by: Kavindu Dodanduwa <[email protected]>
1 parent d24ba8d commit 81e68f9

File tree

3 files changed

+219
-4
lines changed

3 files changed

+219
-4
lines changed

providers/flagd/src/main/java/dev/openfeature/contrib/providers/flagd/resolver/process/targeting/SemVer.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public Object evaluate(List arguments, Object data) throws JsonLogicEvaluationEx
6767
final String arg2Parsed = (String) arguments.get(1);
6868

6969
if (!OPS.contains(arg2Parsed)) {
70-
log.log(Level.FINE, String.format("Not valid operator in argument 2. Received: %a", arg2Parsed));
70+
log.log(Level.FINE, String.format("Not valid operator in argument 2. Received: %s", arg2Parsed));
7171
return null;
7272
}
7373

providers/flagd/src/test/java/dev/openfeature/contrib/providers/flagd/resolver/process/targeting/FractionalTest.java

+46-3
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@
1616
class FractionalTest {
1717

1818
@Test
19-
void selfContainedFractional() throws JsonLogicEvaluationException {
19+
void selfContainedFractionalA() throws JsonLogicEvaluationException {
2020
// given
2121
Fractional fractional = new Fractional();
2222

2323
/* Rule
2424
* [
25-
* "bucketKey",
25+
* "bucketKeyA", // this is resolved value of an expression
2626
* [
2727
* "red",
2828
* 50
@@ -35,7 +35,7 @@ void selfContainedFractional() throws JsonLogicEvaluationException {
3535
* */
3636

3737
final List<Object> rule = new ArrayList<>();
38-
rule.add("bucketKey");
38+
rule.add("bucketKeyA");
3939

4040
final List<Object> bucket1 = new ArrayList<>();
4141
bucket1.add("red");
@@ -58,6 +58,49 @@ void selfContainedFractional() throws JsonLogicEvaluationException {
5858
assertEquals("green", evaluate);
5959
}
6060

61+
@Test
62+
void selfContainedFractionalB() throws JsonLogicEvaluationException {
63+
// given
64+
Fractional fractional = new Fractional();
65+
66+
/* Rule
67+
* [
68+
* "bucketKeyB", // this is resolved value of an expression
69+
* [
70+
* "red",
71+
* 50
72+
* ],
73+
* [
74+
* "blue",
75+
* 50
76+
* ]
77+
* ]
78+
* */
79+
80+
final List<Object> rule = new ArrayList<>();
81+
rule.add("bucketKeyB");
82+
83+
final List<Object> bucket1 = new ArrayList<>();
84+
bucket1.add("red");
85+
bucket1.add(50);
86+
87+
final List<Object> bucket2 = new ArrayList<>();
88+
bucket2.add("green");
89+
bucket2.add(50);
90+
91+
rule.add(bucket1);
92+
rule.add(bucket2);
93+
94+
Map<String, String> data = new HashMap<>();
95+
data.put(FLAG_KEY, "flagA");
96+
97+
// when
98+
Object evaluate = fractional.evaluate(rule, data);
99+
100+
// then
101+
assertEquals("red", evaluate);
102+
}
103+
61104
@Test
62105
void targetingBackedFractional() throws JsonLogicEvaluationException {
63106
// given
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
package dev.openfeature.contrib.providers.flagd.resolver.process.targeting;
2+
3+
import dev.openfeature.sdk.ImmutableContext;
4+
import dev.openfeature.sdk.Value;
5+
import org.junit.jupiter.api.BeforeAll;
6+
import org.junit.jupiter.api.Test;
7+
8+
import java.util.HashMap;
9+
import java.util.Map;
10+
11+
import static org.junit.jupiter.api.Assertions.assertEquals;
12+
13+
class OperatorTest {
14+
private static Operator OPERATOR;
15+
16+
@BeforeAll
17+
static void setUp() {
18+
OPERATOR = new Operator();
19+
}
20+
21+
@Test
22+
void fractionalTestA() throws TargetingRuleException {
23+
// given
24+
25+
// fractional rule with email as expression key
26+
final String targetingRule = "" +
27+
"{\n" +
28+
" \"fractional\": [\n" +
29+
" {\"var\": \"email\"},\n" +
30+
" [\n" +
31+
" \"red\",\n" +
32+
" 25\n" +
33+
" ],\n" +
34+
" [\n" +
35+
" \"blue\",\n" +
36+
" 25\n" +
37+
" ],\n" +
38+
" [\n" +
39+
" \"green\",\n" +
40+
" 25\n" +
41+
" ],\n" +
42+
" [\n" +
43+
" \"yellow\",\n" +
44+
" 25\n" +
45+
" ]\n" +
46+
" ]\n" +
47+
"}";
48+
49+
Map<String, Value> ctxData = new HashMap<>();
50+
ctxData.put("email", new Value("[email protected]"));
51+
52+
53+
// when
54+
Object evalVariant = OPERATOR.apply("headerColor", targetingRule, new ImmutableContext(ctxData));
55+
56+
// then
57+
assertEquals("red", evalVariant);
58+
}
59+
60+
@Test
61+
void fractionalTestB() throws TargetingRuleException {
62+
// given
63+
64+
// fractional rule with email as expression key
65+
final String targetingRule = "" +
66+
"{\n" +
67+
" \"fractional\": [\n" +
68+
" {\"var\": \"email\"},\n" +
69+
" [\n" +
70+
" \"red\",\n" +
71+
" 25\n" +
72+
" ],\n" +
73+
" [\n" +
74+
" \"blue\",\n" +
75+
" 25\n" +
76+
" ],\n" +
77+
" [\n" +
78+
" \"green\",\n" +
79+
" 25\n" +
80+
" ],\n" +
81+
" [\n" +
82+
" \"yellow\",\n" +
83+
" 25\n" +
84+
" ]\n" +
85+
" ]\n" +
86+
"}";
87+
88+
Map<String, Value> ctxData = new HashMap<>();
89+
ctxData.put("email", new Value("[email protected]"));
90+
91+
92+
// when
93+
Object evalVariant = OPERATOR.apply("headerColor", targetingRule, new ImmutableContext(ctxData));
94+
95+
// then
96+
assertEquals("yellow", evalVariant);
97+
}
98+
99+
@Test
100+
void stringCompStartsWith() throws TargetingRuleException {
101+
// given
102+
103+
// starts with rule with email as expression key
104+
final String targetingRule = "" +
105+
"{\n" +
106+
" \"starts_with\": [\n" +
107+
" {\"var\": \"email\"},\n" +
108+
" \"admin\"\n" +
109+
" ]\n" +
110+
"}";
111+
112+
Map<String, Value> ctxData = new HashMap<>();
113+
ctxData.put("email", new Value("[email protected]"));
114+
115+
116+
// when
117+
Object evalVariant = OPERATOR.apply("adminRule", targetingRule, new ImmutableContext(ctxData));
118+
119+
// then
120+
assertEquals(true, evalVariant);
121+
}
122+
123+
@Test
124+
void stringCompEndsWith() throws TargetingRuleException {
125+
// given
126+
127+
// ends with rule with email as expression key
128+
final String targetingRule = "" +
129+
"{\n" +
130+
" \"ends_with\": [\n" +
131+
" {\"var\": \"email\"},\n" +
132+
" \"@faas.com\"\n" +
133+
" ]\n" +
134+
"}";
135+
136+
Map<String, Value> ctxData = new HashMap<>();
137+
ctxData.put("email", new Value("[email protected]"));
138+
139+
140+
// when
141+
Object evalVariant = OPERATOR.apply("isFaas", targetingRule, new ImmutableContext(ctxData));
142+
143+
// then
144+
assertEquals(true, evalVariant);
145+
}
146+
147+
@Test
148+
void semVerA() throws TargetingRuleException {
149+
// given
150+
151+
// sem_ver rule with version as expression key
152+
final String targetingRule = "{\n" +
153+
" \"if\": [\n" +
154+
" {\n" +
155+
" \"sem_ver\": [{\"var\": \"version\"}, \">=\", \"1.0.0\"]\n" +
156+
" },\n" +
157+
" \"red\", null\n" +
158+
" ]\n" +
159+
"}";
160+
161+
Map<String, Value> ctxData = new HashMap<>();
162+
ctxData.put("version", new Value("1.1.0"));
163+
164+
165+
// when
166+
Object evalVariant = OPERATOR.apply("versionFlag", targetingRule, new ImmutableContext(ctxData));
167+
168+
// then
169+
assertEquals("red", evalVariant);
170+
}
171+
172+
}

0 commit comments

Comments
 (0)