33import static org .assertj .core .api .Assertions .assertThat ;
44import static org .junit .jupiter .api .Assertions .assertDoesNotThrow ;
55import static org .junit .jupiter .api .Assertions .assertEquals ;
6- import static org .junit .jupiter .api .Assertions .assertFalse ;
76import static org .junit .jupiter .api .Assertions .assertNotNull ;
8- import static org .mockito .ArgumentMatchers .any ;
9- import static org .mockito .ArgumentMatchers .anyString ;
7+ import static org .junit .jupiter .api .Assertions .assertTrue ;
108import static org .mockito .Mockito .mock ;
11- import static org .mockito .Mockito .when ;
129
13- import com .optimizely .ab .Optimizely ;
14- import com .optimizely .ab .OptimizelyUserContext ;
15- import com .optimizely .ab .bucketing .UserProfileService ;
1610import com .optimizely .ab .config .ProjectConfigManager ;
17- import com .optimizely .ab .error .ErrorHandler ;
1811import com .optimizely .ab .event .EventProcessor ;
19- import com .optimizely .ab .odp .ODPManager ;
20- import com .optimizely .ab .optimizelydecision .OptimizelyDecision ;
21- import com .optimizely .ab .optimizelyjson .OptimizelyJSON ;
2212import dev .openfeature .sdk .EvaluationContext ;
2313import dev .openfeature .sdk .MutableContext ;
2414import dev .openfeature .sdk .ProviderEvaluation ;
2515import dev .openfeature .sdk .Value ;
26- import java .util .List ;
27- import java .util .Map ;
16+ import java .io .File ;
2817import lombok .SneakyThrows ;
18+ import org .junit .jupiter .api .BeforeAll ;
2919import org .junit .jupiter .api .Test ;
3020
3121public class OptimizelyProviderTest {
3222
23+ private static OptimizelyProvider provider ;
24+
25+ @ SneakyThrows
26+ @ BeforeAll
27+ static void setUp () {
28+ File dataFile = new File (
29+ OptimizelyProviderTest .class .getClassLoader ().getResource ("data.json" ).getFile ());
30+ String dataFileContent = new String (java .nio .file .Files .readAllBytes (dataFile .toPath ()));
31+
32+ OptimizelyProviderConfig config = OptimizelyProviderConfig .builder ()
33+ .eventProcessor (mock (EventProcessor .class ))
34+ .datafile (dataFileContent )
35+ .build ();
36+
37+ provider = new OptimizelyProvider (config );
38+ provider .initialize (new MutableContext ("test-targeting-key" ));
39+ }
40+
3341 @ Test
3442 public void test_constructor_initializes_provider_with_valid_config () {
3543 OptimizelyProviderConfig config = OptimizelyProviderConfig .builder ()
@@ -38,10 +46,10 @@ public void test_constructor_initializes_provider_with_valid_config() {
3846 .datafile ("test-datafile" )
3947 .build ();
4048
41- OptimizelyProvider provider = new OptimizelyProvider (config );
49+ OptimizelyProvider localProvider = new OptimizelyProvider (config );
4250
43- assertThat (provider ).isNotNull ();
44- assertEquals ("Optimizely" , provider .getMetadata ().getName ());
51+ assertThat (localProvider ).isNotNull ();
52+ assertEquals ("Optimizely" , localProvider .getMetadata ().getName ());
4553 }
4654
4755 @ Test
@@ -52,92 +60,30 @@ public void test_initialize_handles_null_configuration_parameters() {
5260 .datafile (null )
5361 .build ();
5462
55- OptimizelyProvider provider = new OptimizelyProvider (config );
63+ OptimizelyProvider localProvider = new OptimizelyProvider (config );
5664 EvaluationContext evaluationContext = mock (EvaluationContext .class );
5765
5866 assertDoesNotThrow (() -> {
59- provider .initialize (evaluationContext );
67+ localProvider .initialize (evaluationContext );
6068 });
6169 }
6270
63- @ Test
64- public void test_initialize_builds_optimizely_and_context_transformer () throws Exception {
65- OptimizelyProviderConfig config = mock (OptimizelyProviderConfig .class );
66- when (config .getProjectConfigManager ()).thenReturn (mock (ProjectConfigManager .class ));
67- OptimizelyProvider provider = new OptimizelyProvider (config );
68- provider .initialize (mock (EvaluationContext .class ));
69- }
70-
71- @ SneakyThrows
72- @ Test
73- public void test_get_string_evaluation_returns_correct_value () {
74- OptimizelyProviderConfig config = mock (OptimizelyProviderConfig .class );
75- ContextTransformer contextTransformer = mock (ContextTransformer .class );
76- OptimizelyUserContext userContext = mock (OptimizelyUserContext .class );
77- OptimizelyDecision decision = mock (OptimizelyDecision .class );
78-
79- when (contextTransformer .transform (any (EvaluationContext .class ))).thenReturn (userContext );
80- when (userContext .decide (anyString ())).thenReturn (decision );
81- when (decision .getVariationKey ()).thenReturn ("variationKey" );
82- when (decision .getEnabled ()).thenReturn (true );
83-
84- OptimizelyProvider provider = new OptimizelyProvider (config );
85- provider .initialize (new MutableContext ());
86-
87- EvaluationContext ctx = new MutableContext ("targetingKey" );
88- ProviderEvaluation <String > result = provider .getStringEvaluation ("featureKey" , "defaultValue" , ctx );
89-
90- assertEquals ("variationKey" , result .getValue ());
91-
92- when (decision .getEnabled ()).thenReturn (false );
93- result = provider .getStringEvaluation ("featureKey" , "defaultValue" , ctx );
94-
95- assertEquals ("defaultValue" , result .getValue ());
96- }
97-
9871 @ SneakyThrows
9972 @ Test
10073 public void test_get_object_evaluation_returns_transformed_variables () {
101- OptimizelyProviderConfig config = mock (OptimizelyProviderConfig .class );
102- ContextTransformer contextTransformer = mock (ContextTransformer .class );
103- OptimizelyUserContext userContext = mock (OptimizelyUserContext .class );
104- OptimizelyDecision decision = mock (OptimizelyDecision .class );
105- OptimizelyJSON optimizelyJSON = mock (OptimizelyJSON .class );
106-
107- when (contextTransformer .transform (any (EvaluationContext .class ))).thenReturn (userContext );
108- when (userContext .decide (anyString ())).thenReturn (decision );
109- when (decision .getEnabled ()).thenReturn (true );
110- when (decision .getVariables ()).thenReturn (optimizelyJSON );
111- when (optimizelyJSON .toMap ()).thenReturn (Map .of ("key" , "value" ));
112-
113- OptimizelyProvider provider = new OptimizelyProvider (config );
114- provider .initialize (mock (EvaluationContext .class ));
115-
11674 EvaluationContext ctx = new MutableContext ("targetingKey" );
117- ProviderEvaluation <Value > result = provider .getObjectEvaluation ("featureKey " , new Value (), ctx );
75+ ProviderEvaluation <Value > result = provider .getObjectEvaluation ("string-feature " , new Value (), ctx );
11876
11977 assertNotNull (result .getValue ());
120- assertNotNull (result .getValue ().asStructure ().getValue ("variables" ));
78+ assertEquals ("string_feature_variation" , result .getVariant ());
79+ assertEquals ("str1" , result .getValue ().asStructure ().getValue ("string_variable_1" ).asString ());
12180 }
12281
12382 @ Test
12483 public void test_get_boolean_evaluation_handles_null_variation_key () {
125- OptimizelyProviderConfig config = mock (OptimizelyProviderConfig .class );
126- ContextTransformer contextTransformerMock = mock (ContextTransformer .class );
127- OptimizelyUserContext userContextMock = mock (OptimizelyUserContext .class );
128- OptimizelyDecision decisionMock = mock (OptimizelyDecision .class );
129-
130- when (contextTransformerMock .transform (any ())).thenReturn (userContextMock );
131- when (userContextMock .decide (anyString ())).thenReturn (decisionMock );
132- when (decisionMock .getVariationKey ()).thenReturn (null );
133- when (decisionMock .getReasons ()).thenReturn (List .of ("reason1" , "reason2" ));
134- when (decisionMock .getEnabled ()).thenReturn (false );
135-
136- OptimizelyProvider provider = new OptimizelyProvider (config );
137-
138- ProviderEvaluation <Boolean > evaluation = provider .getBooleanEvaluation ("key" , false , mock (EvaluationContext .class ));
84+ EvaluationContext ctx = new MutableContext ("targetingKey" );
85+ ProviderEvaluation <Boolean > evaluation = provider .getBooleanEvaluation ("string-feature" , false , ctx );
13986
140- assertFalse (evaluation .getValue ());
141- assertEquals ("reason1, reason2" , evaluation .getReason ());
87+ assertTrue (evaluation .getValue ());
14288 }
14389}
0 commit comments