1
1
package dev .openfeature .contrib .providers .flipt ;
2
2
3
- import com .flipt .api .FliptApiClient ;
4
- import com .flipt .api .FliptApiClientBuilder ;
5
- import com .flipt .api .core .Environment ;
3
+ import com .fasterxml .jackson .core .JsonProcessingException ;
4
+ import com .fasterxml .jackson .databind .ObjectMapper ;
5
+ import dev .openfeature .sdk .*;
6
+ import io .flipt .api .FliptClient ;
7
+ import io .flipt .api .FliptClient .FliptClientBuilder ;
6
8
import com .github .tomakehurst .wiremock .client .WireMock ;
7
9
import com .github .tomakehurst .wiremock .junit5 .WireMockRuntimeInfo ;
8
10
import com .github .tomakehurst .wiremock .junit5 .WireMockTest ;
9
- import dev .openfeature .sdk .Client ;
10
- import dev .openfeature .sdk .FlagEvaluationDetails ;
11
- import dev .openfeature .sdk .ImmutableContext ;
12
- import dev .openfeature .sdk .ImmutableMetadata ;
13
- import dev .openfeature .sdk .MutableContext ;
14
- import dev .openfeature .sdk .OpenFeatureAPI ;
15
- import dev .openfeature .sdk .ProviderEvaluation ;
16
- import dev .openfeature .sdk .ProviderEventDetails ;
17
- import dev .openfeature .sdk .ProviderState ;
18
11
import dev .openfeature .sdk .exceptions .GeneralError ;
19
12
import dev .openfeature .sdk .exceptions .ProviderNotReadyError ;
20
13
import lombok .SneakyThrows ;
26
19
import java .net .URL ;
27
20
import java .nio .file .Files ;
28
21
import java .nio .file .Paths ;
22
+ import java .util .HashMap ;
23
+ import java .util .Map ;
29
24
30
25
import static com .github .tomakehurst .wiremock .client .WireMock .aResponse ;
31
26
import static com .github .tomakehurst .wiremock .client .WireMock .equalTo ;
32
27
import static com .github .tomakehurst .wiremock .client .WireMock .post ;
33
28
import static com .github .tomakehurst .wiremock .client .WireMock .stubFor ;
34
29
import static com .github .tomakehurst .wiremock .client .WireMock .urlEqualTo ;
35
- import static org .junit .jupiter .api .Assertions .assertEquals ;
36
- import static org .junit .jupiter .api .Assertions .assertThrows ;
30
+ import static org .junit .jupiter .api .Assertions .*;
37
31
38
32
/**
39
33
* FliptProvider test, based on APIs mocking.
@@ -51,14 +45,16 @@ class FliptProviderTest {
51
45
public static final Double DOUBLE_FLAG_VALUE = 1.23 ;
52
46
public static final String USERS_FLAG_NAME = "users-flag" ;
53
47
public static final String TARGETING_KEY = "targeting_key" ;
48
+ public static final String OBJECT_FLAG_NAME = "object-flag" ;
49
+
54
50
private static FliptProvider fliptProvider ;
55
51
private static Client client ;
56
52
57
53
private String apiUrl ;
58
54
59
55
@ BeforeAll
60
56
void setUp (WireMockRuntimeInfo wmRuntimeInfo ) {
61
- apiUrl = "http://localhost:" + wmRuntimeInfo .getHttpPort () + "/" ;
57
+ apiUrl = "http://localhost:" + wmRuntimeInfo .getHttpPort ();
62
58
fliptProvider = buildFliptProvider ();
63
59
OpenFeatureAPI .getInstance ().setProviderAndWait ("sync" , fliptProvider );
64
60
client = OpenFeatureAPI .getInstance ().getClient ("sync" );
@@ -72,20 +68,20 @@ public void shutdown() {
72
68
private void mockFliptAPI (String url , String resourceName , String flagKey ) {
73
69
stubFor (
74
70
post (urlEqualTo (url ))
75
- .withHeader ("Content-Type" , equalTo ("application/json" ))
71
+ .withHeader ("Content-Type" , equalTo ("application/json; charset=UTF-8 " ))
76
72
.withRequestBody (WireMock .containing (flagKey ))
77
73
.willReturn (
78
74
aResponse ()
79
75
.withStatus (200 )
80
- .withHeader ("Content-Type" , "application/json" )
76
+ .withHeader ("Content-Type" , "application/json; charset=UTF-8 " )
81
77
.withBody (readResourceFileContent (resourceName ))));
82
78
}
83
79
84
80
@ SneakyThrows
85
81
private FliptProvider buildFliptProvider () {
86
- FliptApiClientBuilder fliptApiClientBuilder = FliptApiClient .builder ().url (apiUrl ). environment ( Environment . custom ( apiUrl ) );
82
+ FliptClientBuilder fliptClientBuilder = FliptClient .builder ().url (apiUrl );
87
83
FliptProviderConfig fliptProviderConfig = FliptProviderConfig .builder ()
88
- .fliptApiClientBuilder ( fliptApiClientBuilder )
84
+ .fliptClientBuilder ( fliptClientBuilder )
89
85
.build ();
90
86
return new FliptProvider (fliptProviderConfig );
91
87
}
@@ -101,7 +97,6 @@ void getBooleanEvaluation() {
101
97
mockFliptAPI ("/evaluate/v1/boolean" , "boolean.json" , FLAG_NAME );
102
98
MutableContext evaluationContext = new MutableContext ();
103
99
evaluationContext .setTargetingKey (TARGETING_KEY );
104
- assertEquals (true , fliptProvider .getBooleanEvaluation (FLAG_NAME , false , evaluationContext ).getValue ());
105
100
assertEquals (true , client .getBooleanValue (FLAG_NAME , false , evaluationContext ));
106
101
assertEquals (false , client .getBooleanValue ("non-existing" , false , evaluationContext ));
107
102
}
@@ -111,8 +106,6 @@ void getStringVariantEvaluation() {
111
106
mockFliptAPI ("/evaluate/v1/variant" , "variant.json" , VARIANT_FLAG_NAME );
112
107
MutableContext evaluationContext = new MutableContext ();
113
108
evaluationContext .setTargetingKey (TARGETING_KEY );
114
- assertEquals (VARIANT_FLAG_VALUE , fliptProvider .getStringEvaluation (VARIANT_FLAG_NAME , "" ,
115
- evaluationContext ).getValue ());
116
109
assertEquals (VARIANT_FLAG_VALUE , client .getStringValue (VARIANT_FLAG_NAME , "" , evaluationContext ));
117
110
assertEquals ("fallback_str" , client .getStringValue ("non-existing" , "fallback_str" , evaluationContext ));
118
111
}
@@ -123,8 +116,6 @@ void getIntegerEvaluation() {
123
116
MutableContext evaluationContext = new MutableContext ();
124
117
evaluationContext .setTargetingKey (TARGETING_KEY );
125
118
evaluationContext .add ("userId" , "int" );
126
- assertEquals (INT_FLAG_VALUE , fliptProvider .getIntegerEvaluation (INT_FLAG_NAME , 1 ,
127
- evaluationContext ).getValue ());
128
119
assertEquals (INT_FLAG_VALUE , client .getIntegerValue (INT_FLAG_NAME , 1 , evaluationContext ));
129
120
assertEquals (1 , client .getIntegerValue ("non-existing" , 1 , evaluationContext ));
130
121
@@ -138,8 +129,6 @@ void getDoubleEvaluation() {
138
129
MutableContext evaluationContext = new MutableContext ();
139
130
evaluationContext .setTargetingKey (TARGETING_KEY );
140
131
evaluationContext .add ("userId" , "double" );
141
- assertEquals (DOUBLE_FLAG_VALUE , fliptProvider .getDoubleEvaluation (DOUBLE_FLAG_NAME , 1.1 ,
142
- evaluationContext ).getValue ());
143
132
assertEquals (DOUBLE_FLAG_VALUE , client .getDoubleValue (DOUBLE_FLAG_NAME , 1.1 , evaluationContext ));
144
133
assertEquals (1.1 , client .getDoubleValue ("non-existing" , 1.1 , evaluationContext ));
145
134
@@ -153,7 +142,6 @@ void getStringEvaluationByUser() {
153
142
MutableContext evaluationContext = new MutableContext ();
154
143
evaluationContext .setTargetingKey (TARGETING_KEY );
155
144
evaluationContext .add ("userId" , "111" );
156
- assertEquals (VARIANT_FLAG_VALUE , fliptProvider .getStringEvaluation (USERS_FLAG_NAME , "" , evaluationContext ).getValue ());
157
145
assertEquals (VARIANT_FLAG_VALUE , client .getStringValue (USERS_FLAG_NAME , "" , evaluationContext ));
158
146
evaluationContext .add ("userId" , "2" );
159
147
assertEquals ("" , client .getStringValue (USERS_FLAG_NAME , "" , evaluationContext ));
@@ -164,12 +152,26 @@ void getEvaluationMetadataTest() {
164
152
mockFliptAPI ("/evaluate/v1/variant" , "variant.json" , VARIANT_FLAG_NAME );
165
153
MutableContext evaluationContext = new MutableContext ();
166
154
evaluationContext .setTargetingKey (TARGETING_KEY );
167
- ProviderEvaluation <String > stringEvaluation = fliptProvider .getStringEvaluation (VARIANT_FLAG_NAME , "" ,
168
- evaluationContext );
169
- ImmutableMetadata flagMetadata = stringEvaluation .getFlagMetadata ();
170
- assertEquals ("attachment-1" , flagMetadata .getString ("variant-attachment" ));
171
155
FlagEvaluationDetails <String > nonExistingFlagEvaluation = client .getStringDetails ("non-existing" , "" , evaluationContext );
172
- assertEquals (null , nonExistingFlagEvaluation .getFlagMetadata ().getBoolean ("variant-attachment" ));
156
+ assertNull (nonExistingFlagEvaluation .getFlagMetadata ().getBoolean ("variant-attachment" ));
157
+ }
158
+
159
+ @ SneakyThrows
160
+ @ Test
161
+ void getObjectEvaluationTest () {
162
+ mockFliptAPI ("/evaluate/v1/variant" , "variant-object.json" , OBJECT_FLAG_NAME );
163
+ MutableContext evaluationContext = new MutableContext ();
164
+ evaluationContext .setTargetingKey (TARGETING_KEY );
165
+ evaluationContext .add ("userId" , "object" );
166
+
167
+ Value expectedValue = new Value ("{\" key1\" :\" value1\" ,\" key2\" :42,\" key3\" :true}" );
168
+ Value emptyValue = new Value ();
169
+
170
+ assertEquals (expectedValue , client .getObjectValue (OBJECT_FLAG_NAME , emptyValue , evaluationContext ));
171
+ assertEquals (emptyValue , client .getObjectValue ("non-existing" , emptyValue , evaluationContext ));
172
+
173
+ // non-object flag value
174
+ assertEquals (emptyValue , client .getObjectValue (VARIANT_FLAG_NAME , emptyValue , evaluationContext ));
173
175
}
174
176
175
177
@ SneakyThrows
0 commit comments