1
1
package dev .openfeature .sdk ;
2
2
3
- import dev .openfeature .sdk .fixtures .HookFixtures ;
4
- import lombok .SneakyThrows ;
3
+ import static org .assertj .core .api .Assertions .assertThat ;
4
+ import static org .junit .jupiter .api .Assertions .assertEquals ;
5
+ import static org .junit .jupiter .api .Assertions .assertThrows ;
6
+ import static org .mockito .ArgumentMatchers .any ;
7
+ import static org .mockito .ArgumentMatchers .anyString ;
8
+ import static org .mockito .Mockito .mock ;
9
+ import static org .mockito .Mockito .never ;
10
+
11
+ import java .util .HashMap ;
12
+ import java .util .concurrent .atomic .AtomicBoolean ;
13
+
5
14
import org .junit .jupiter .api .AfterEach ;
6
15
import org .junit .jupiter .api .BeforeEach ;
7
16
import org .junit .jupiter .api .DisplayName ;
10
19
import org .simplify4u .slf4jmock .LoggerMock ;
11
20
import org .slf4j .Logger ;
12
21
13
- import java .util .Arrays ;
14
- import java .util .HashMap ;
15
- import java .util .concurrent .atomic .AtomicBoolean ;
16
-
17
- import static org .assertj .core .api .Assertions .assertThat ;
18
- import static org .junit .jupiter .api .Assertions .assertEquals ;
19
- import static org .mockito .ArgumentMatchers .*;
20
- import static org .mockito .Mockito .*;
22
+ import dev .openfeature .sdk .exceptions .FatalError ;
23
+ import dev .openfeature .sdk .fixtures .HookFixtures ;
24
+ import dev .openfeature .sdk .testutils .TestEventsProvider ;
21
25
22
26
class OpenFeatureClientTest implements HookFixtures {
23
27
@@ -37,14 +41,10 @@ void reset_logs() {
37
41
@ Test
38
42
@ DisplayName ("should not throw exception if hook has different type argument than hookContext" )
39
43
void shouldNotThrowExceptionIfHookHasDifferentTypeArgumentThanHookContext () {
40
- DoSomethingProvider provider = new DoSomethingProvider ();
41
- OpenFeatureAPI api = mock (OpenFeatureAPI .class );
42
- when (api .getProvider (any ())).thenReturn (provider );
43
- when (api .getHooks ()).thenReturn (Arrays .asList (mockBooleanHook (), mockStringHook ()));
44
-
45
- MockProviderRepository mockProviderRepository = new MockProviderRepository (provider , true );
46
- OpenFeatureClient client = new OpenFeatureClient (mockProviderRepository , api , "name" , "version" );
47
-
44
+ OpenFeatureAPI api = OpenFeatureAPI .getInstance ();
45
+ api .setProvider ("shouldNotThrowExceptionIfHookHasDifferentTypeArgumentThanHookContext" , new DoSomethingProvider ());
46
+ Client client = api .getClient ("shouldNotThrowExceptionIfHookHasDifferentTypeArgumentThanHookContext" );
47
+ client .addHooks (mockBooleanHook (), mockStringHook ());
48
48
FlagEvaluationDetails <Boolean > actual = client .getBooleanDetails ("feature key" , Boolean .FALSE );
49
49
50
50
assertThat (actual .getValue ()).isTrue ();
@@ -56,36 +56,11 @@ void shouldNotThrowExceptionIfHookHasDifferentTypeArgumentThanHookContext() {
56
56
Mockito .verify (logger , never ()).error (anyString (), any (), any ());
57
57
}
58
58
59
- @ Test
60
- void mergeContextTest () {
61
- String flag = "feature key" ;
62
- boolean defaultValue = false ;
63
- String targetingKey = "targeting key" ;
64
- EvaluationContext ctx = new ImmutableContext (targetingKey , new HashMap <>());
65
- OpenFeatureAPI api = mock (OpenFeatureAPI .class );
66
- FeatureProvider mockProvider = mock (FeatureProvider .class );
67
- // this makes it so that true is returned only if the targeting key set at the client level is honored
68
- when (mockProvider .getBooleanEvaluation (
69
- eq (flag ), eq (defaultValue ), argThat (
70
- context -> context .getTargetingKey ().equals (targetingKey )))).thenReturn (ProviderEvaluation .<Boolean >builder ()
71
- .value (true ).build ());
72
- when (api .getProvider ()).thenReturn (mockProvider );
73
- when (api .getProvider (any ())).thenReturn (mockProvider );
74
-
75
- MockProviderRepository mockProviderRepository = new MockProviderRepository (mockProvider , true );
76
- OpenFeatureClient client = new OpenFeatureClient (mockProviderRepository , api , "name" , "version" );
77
- client .setEvaluationContext (ctx );
78
-
79
- FlagEvaluationDetails <Boolean > result = client .getBooleanDetails (flag , defaultValue );
80
-
81
- assertThat (result .getValue ()).isTrue ();
82
- }
83
-
84
59
@ Test
85
60
@ DisplayName ("addHooks should allow chaining by returning the same client instance" )
86
61
void addHooksShouldAllowChaining () {
87
62
OpenFeatureAPI api = mock (OpenFeatureAPI .class );
88
- OpenFeatureClient client = new OpenFeatureClient (() -> null , api , "name" , "version" );
63
+ OpenFeatureClient client = new OpenFeatureClient (api , "name" , "version" );
89
64
Hook <?> hook1 = Mockito .mock (Hook .class );
90
65
Hook <?> hook2 = Mockito .mock (Hook .class );
91
66
@@ -97,7 +72,7 @@ void addHooksShouldAllowChaining() {
97
72
@ DisplayName ("setEvaluationContext should allow chaining by returning the same client instance" )
98
73
void setEvaluationContextShouldAllowChaining () {
99
74
OpenFeatureAPI api = mock (OpenFeatureAPI .class );
100
- OpenFeatureClient client = new OpenFeatureClient (() -> null , api , "name" , "version" );
75
+ OpenFeatureClient client = new OpenFeatureClient (api , "name" , "version" );
101
76
EvaluationContext ctx = new ImmutableContext ("targeting key" , new HashMap <>());
102
77
103
78
OpenFeatureClient result = client .setEvaluationContext (ctx );
@@ -107,49 +82,27 @@ void setEvaluationContextShouldAllowChaining() {
107
82
@ Test
108
83
@ DisplayName ("Should not call evaluation methods when the provider has state FATAL" )
109
84
void shouldNotCallEvaluationMethodsWhenProviderIsInFatalErrorState () {
110
- MockProvider mockProvider = new MockProvider (ProviderState .FATAL );
111
- OpenFeatureAPI api = mock (OpenFeatureAPI .class );
112
- MockProviderRepository mockProviderRepository = new MockProviderRepository (mockProvider , true );
113
- OpenFeatureClient client = new OpenFeatureClient (mockProviderRepository , api , "name" , "version" );
114
- mockProviderRepository .featureProviderStateManager .onEmit (
115
- ProviderEvent .PROVIDER_ERROR ,
116
- ProviderEventDetails .builder ().errorCode (ErrorCode .PROVIDER_FATAL ).build ()
117
- );
118
- FlagEvaluationDetails <Boolean > details = client .getBooleanDetails ("key" , true );
85
+ FeatureProvider provider = new TestEventsProvider (100 , true , "fake fatal" , true );
86
+ OpenFeatureAPI api = OpenFeatureAPI .getInstance ();
87
+ Client client = api .getClient ("shouldNotCallEvaluationMethodsWhenProviderIsInFatalErrorState" );
119
88
120
- assertThat (mockProvider .isEvaluationCalled ()).isFalse ();
89
+ assertThrows (FatalError .class , () -> api .setProviderAndWait ("shouldNotCallEvaluationMethodsWhenProviderIsInFatalErrorState" , provider ));
90
+ FlagEvaluationDetails <Boolean > details = client .getBooleanDetails ("key" , true );
121
91
assertThat (details .getErrorCode ()).isEqualTo (ErrorCode .PROVIDER_FATAL );
122
92
}
123
93
124
94
@ Test
125
95
@ DisplayName ("Should not call evaluation methods when the provider has state NOT_READY" )
126
96
void shouldNotCallEvaluationMethodsWhenProviderIsInNotReadyState () {
127
- MockProvider mockProvider = new MockProvider (ProviderState .NOT_READY );
128
- OpenFeatureAPI api = mock (OpenFeatureAPI .class );
129
- OpenFeatureClient client = new OpenFeatureClient (new MockProviderRepository (mockProvider , false ), api , "name" , "version" );
97
+ FeatureProvider provider = new TestEventsProvider (5000 );
98
+ OpenFeatureAPI api = OpenFeatureAPI .getInstance ();
99
+ api .setProvider ("shouldNotCallEvaluationMethodsWhenProviderIsInNotReadyState" , provider );
100
+ Client client = api .getClient ("shouldNotCallEvaluationMethodsWhenProviderIsInNotReadyState" );
130
101
FlagEvaluationDetails <Boolean > details = client .getBooleanDetails ("key" , true );
131
102
132
- assertThat (mockProvider .isEvaluationCalled ()).isFalse ();
133
103
assertThat (details .getErrorCode ()).isEqualTo (ErrorCode .PROVIDER_NOT_READY );
134
104
}
135
105
136
- private static class MockProviderRepository implements ProviderAccessor {
137
- private final FeatureProviderStateManager featureProviderStateManager ;
138
-
139
- @ SneakyThrows
140
- public MockProviderRepository (FeatureProvider featureProvider , boolean init ) {
141
- this .featureProviderStateManager = new FeatureProviderStateManager (featureProvider );
142
- if (init ) {
143
- this .featureProviderStateManager .initialize (null );
144
- }
145
- }
146
-
147
- @ Override
148
- public FeatureProviderStateManager getProviderStateManager () {
149
- return featureProviderStateManager ;
150
- }
151
- }
152
-
153
106
private static class MockProvider implements FeatureProvider {
154
107
private final AtomicBoolean evaluationCalled = new AtomicBoolean ();
155
108
private final ProviderState providerState ;
0 commit comments