44import com .launchdarkly .sdk .EvaluationReason ;
55import com .launchdarkly .sdk .LDContext ;
66import com .launchdarkly .sdk .LDValue ;
7+ import com .launchdarkly .sdk .server .interfaces .DataSourceStatusProvider ;
8+ import com .launchdarkly .sdk .server .interfaces .FlagTracker ;
79import com .launchdarkly .sdk .server .interfaces .LDClientInterface ;
810import dev .openfeature .sdk .*;
911import org .junit .jupiter .api .Test ;
1012
1113import static org .mockito .Mockito .*;
1214
13- import org .awaitility .Awaitility ;
14-
15- import java .time .Duration ;
16-
1715import static org .junit .jupiter .api .Assertions .*;
1816
19- /**
20- * Timeout for Awaitility waits. Provider initialization should complete quickly
21- * since these tests use a mock client, but we need a non-infinite bound to avoid
22- * hanging the build if a bug prevents initialization.
23- */
24-
2517public class ProviderTest {
2618 LDClientInterface mockedLdClient = mock (LDClientInterface .class );
2719
20+ {
21+ when (mockedLdClient .getFlagTracker ()).thenReturn (mock (FlagTracker .class ));
22+ DataSourceStatusProvider dsp = mock (DataSourceStatusProvider .class );
23+ when (dsp .getStatus ()).thenReturn (new DataSourceStatusProvider .Status (
24+ DataSourceStatusProvider .State .VALID , null , null ));
25+ when (mockedLdClient .getDataSourceStatusProvider ()).thenReturn (dsp );
26+ when (mockedLdClient .isInitialized ()).thenReturn (true );
27+ }
28+
2829 /**
2930 * This test uses the package private constructor, which means that it does not set
3031 * wrapper information.
@@ -38,18 +39,13 @@ public void itCanProvideMetadata() {
3839 }
3940
4041 @ Test
41- public void itCanDoABooleanEvaluation () {
42+ public void itCanDoABooleanEvaluation () throws Exception {
4243 EvaluationContext evaluationContext = new ImmutableContext ("user-key" );
4344
4445 when (mockedLdClient .boolVariationDetail ("the-key" , LDContext .create ("user-key" ), false ))
4546 .thenReturn (EvaluationDetail .fromValue (true , 12 , EvaluationReason .fallthrough ()));
4647
47- OpenFeatureAPI .getInstance ().setProvider (ldProvider );
48-
49- Awaitility .await ().atMost (Duration .ofSeconds (10 )).until (() -> OpenFeatureAPI
50- .getInstance ()
51- .getClient ()
52- .getBooleanValue ("the-key" , false , evaluationContext ));
48+ OpenFeatureAPI .getInstance ().setProviderAndWait (ldProvider );
5349
5450 assertTrue (OpenFeatureAPI
5551 .getInstance ()
@@ -67,19 +63,14 @@ public void itCanDoABooleanEvaluation() {
6763 }
6864
6965 @ Test
70- public void itCanDoAStringEvaluation () {
66+ public void itCanDoAStringEvaluation () throws Exception {
7167 EvaluationContext evaluationContext = new ImmutableContext ("user-key" );
7268
7369 when (mockedLdClient .stringVariationDetail ("the-key" , LDContext .create ("user-key" ), "default" ))
7470 .thenReturn (EvaluationDetail
7571 .fromValue ("evaluated" , 17 , EvaluationReason .off ()));
7672
77- OpenFeatureAPI .getInstance ().setProvider (ldProvider );
78-
79- Awaitility .await ().atMost (Duration .ofSeconds (10 )).until (() -> OpenFeatureAPI
80- .getInstance ()
81- .getClient ()
82- .getStringValue ("the-key" , "default" , evaluationContext ).equals ("evaluated" ));
73+ OpenFeatureAPI .getInstance ().setProviderAndWait (ldProvider );
8374
8475 assertEquals ("evaluated" , OpenFeatureAPI
8576 .getInstance ()
@@ -97,18 +88,13 @@ public void itCanDoAStringEvaluation() {
9788 }
9889
9990 @ Test
100- public void itCanDoADoubleEvaluation () {
91+ public void itCanDoADoubleEvaluation () throws Exception {
10192 EvaluationContext evaluationContext = new ImmutableContext ("user-key" );
10293
10394 when (mockedLdClient .doubleVariationDetail ("the-key" , LDContext .create ("user-key" ), 0.0 ))
10495 .thenReturn (EvaluationDetail .fromValue (1.0 , 42 , EvaluationReason .targetMatch ()));
10596
106- OpenFeatureAPI .getInstance ().setProvider (ldProvider );
107-
108- Awaitility .await ().atMost (Duration .ofSeconds (10 )).until (() -> OpenFeatureAPI
109- .getInstance ()
110- .getClient ()
111- .getDoubleValue ("the-key" , 0.0 , evaluationContext ) != 0.0 );
97+ OpenFeatureAPI .getInstance ().setProviderAndWait (ldProvider );
11298
11399 assertEquals (1.0 , OpenFeatureAPI
114100 .getInstance ()
@@ -126,7 +112,7 @@ public void itCanDoADoubleEvaluation() {
126112 }
127113
128114 @ Test
129- public void itCanDoAValueEvaluation () {
115+ public void itCanDoAValueEvaluation () throws Exception {
130116 EvaluationContext evaluationContext = new ImmutableContext ("user-key" );
131117
132118 EvaluationDetail <LDValue > evaluationDetail = EvaluationDetail
@@ -135,13 +121,7 @@ public void itCanDoAValueEvaluation() {
135121 when (mockedLdClient .jsonValueVariationDetail ("the-key" , LDContext .create ("user-key" ), LDValue .ofNull ()))
136122 .thenReturn (evaluationDetail );
137123
138- OpenFeatureAPI .getInstance ().setProvider (ldProvider );
139-
140- Awaitility .await ().atMost (Duration .ofSeconds (10 )).until (() -> {
141- Value val = OpenFeatureAPI .getInstance ().getClient ()
142- .getObjectValue ("the-key" , new Value (), evaluationContext );
143- return val != null && val .asStructure () != null ;
144- });
124+ OpenFeatureAPI .getInstance ().setProviderAndWait (ldProvider );
145125
146126 Value ofValue = OpenFeatureAPI
147127 .getInstance ()
0 commit comments