forked from open-feature/java-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOpenFeatureClientTest.java
100 lines (79 loc) · 4.11 KB
/
OpenFeatureClientTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package dev.openfeature.sdk;
import java.util.*;
import dev.openfeature.sdk.fixtures.HookFixtures;
import org.junit.jupiter.api.*;
import org.mockito.Mockito;
import org.simplify4u.slf4jmock.LoggerMock;
import org.slf4j.Logger;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.*;
class OpenFeatureClientTest implements HookFixtures {
private Logger logger;
@BeforeEach void set_logger() {
logger = Mockito.mock(Logger.class);
LoggerMock.setMock(OpenFeatureClient.class, logger);
}
@AfterEach void reset_logs() {
LoggerMock.setMock(OpenFeatureClient.class, logger);
}
@Test
@DisplayName("should not throw exception if hook has different type argument than hookContext")
void shouldNotThrowExceptionIfHookHasDifferentTypeArgumentThanHookContext() {
OpenFeatureAPI api = mock(OpenFeatureAPI.class);
when(api.getProvider(any())).thenReturn(new DoSomethingProvider());
when(api.getHooks()).thenReturn(Arrays.asList(mockBooleanHook(), mockStringHook()));
OpenFeatureClient client = new OpenFeatureClient(api, "name", "version");
FlagEvaluationDetails<Boolean> actual = client.getBooleanDetails("feature key", Boolean.FALSE);
assertThat(actual.getValue()).isTrue();
// I dislike this, but given the mocking tools available, there's no way that I know of to say "no errors were logged"
Mockito.verify(logger, never()).error(any());
Mockito.verify(logger, never()).error(anyString(), any(Throwable.class));
Mockito.verify(logger, never()).error(anyString(), any(Object.class));
Mockito.verify(logger, never()).error(anyString(), any(), any());
Mockito.verify(logger, never()).error(anyString(), any(), any());
}
@Test
void mergeContextTest() {
String flag = "feature key";
boolean defaultValue = false;
String targetingKey = "targeting key";
EvaluationContext ctx = new ImmutableContext(targetingKey, new HashMap<>());
OpenFeatureAPI api = mock(OpenFeatureAPI.class);
FeatureProvider mockProvider = mock(FeatureProvider.class);
// this makes it so that true is returned only if the targeting key set at the client level is honored
when(mockProvider.getBooleanEvaluation(
eq(flag), eq(defaultValue), argThat(
context -> context.getTargetingKey().equals(targetingKey)))).thenReturn(ProviderEvaluation.<Boolean>builder()
.value(true).build());
when(api.getProvider()).thenReturn(mockProvider);
when(api.getProvider(any())).thenReturn(mockProvider);
OpenFeatureClient client = new OpenFeatureClient(api, "name", "version");
client.setEvaluationContext(ctx);
FlagEvaluationDetails<Boolean> result = client.getBooleanDetails(flag, defaultValue);
assertThat(result.getValue()).isTrue();
}
@Test
@DisplayName("addHooks should allow chaining by returning the same client instance")
void addHooksShouldAllowChaining() {
OpenFeatureAPI api = mock(OpenFeatureAPI.class);
OpenFeatureClient client = new OpenFeatureClient(api, "name", "version");
Hook<?> hook1 = Mockito.mock(Hook.class);
Hook<?> hook2 = Mockito.mock(Hook.class);
OpenFeatureClient result = client.addHooks(hook1, hook2);
assertEquals(client, result);
}
@Test
@DisplayName("setEvaluationContext should allow chaining by returning the same client instance")
void setEvaluationContextShouldAllowChaining() {
OpenFeatureAPI api = mock(OpenFeatureAPI.class);
OpenFeatureClient client = new OpenFeatureClient(api, "name", "version");
EvaluationContext ctx = new ImmutableContext("targeting key", new HashMap<>());
OpenFeatureClient result = client.setEvaluationContext(ctx);
assertEquals(client, result);
}
}