forked from open-feature/java-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHookDataTest.java
More file actions
111 lines (87 loc) · 3.25 KB
/
Copy pathHookDataTest.java
File metadata and controls
111 lines (87 loc) · 3.25 KB
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
101
102
103
104
105
106
107
108
109
110
111
package dev.openfeature.sdk;
import static org.junit.jupiter.api.Assertions.*;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import org.junit.jupiter.api.Test;
class HookDataTest {
@Test
void shouldStoreAndRetrieveValues() {
HookData hookData = HookData.create();
hookData.set("key1", "value1");
hookData.set("key2", 42);
hookData.set("key3", true);
assertEquals("value1", hookData.get("key1"));
assertEquals(42, hookData.get("key2"));
assertEquals(true, hookData.get("key3"));
}
@Test
void shouldReturnNullForMissingKeys() {
HookData hookData = HookData.create();
assertNull(hookData.get("nonexistent"));
}
@Test
void shouldSupportTypeSafeRetrieval() {
HookData hookData = HookData.create();
hookData.set("string", "hello");
hookData.set("integer", 123);
hookData.set("boolean", false);
assertEquals("hello", hookData.get("string", String.class));
assertEquals(Integer.valueOf(123), hookData.get("integer", Integer.class));
assertEquals(Boolean.FALSE, hookData.get("boolean", Boolean.class));
}
@Test
void shouldReturnNullForMissingKeysWithType() {
HookData hookData = HookData.create();
assertNull(hookData.get("missing", String.class));
}
@Test
void shouldThrowClassCastExceptionForWrongType() {
HookData hookData = HookData.create();
hookData.set("string", "not a number");
assertThrows(ClassCastException.class, () -> {
hookData.get("string", Integer.class);
});
}
@Test
void shouldOverwriteExistingValues() {
HookData hookData = HookData.create();
hookData.set("key", "original");
assertEquals("original", hookData.get("key"));
hookData.set("key", "updated");
assertEquals("updated", hookData.get("key"));
}
@Test
void shouldBeThreadSafe() throws InterruptedException {
HookData hookData = HookData.create();
int threadCount = 10;
int operationsPerThread = 100;
CountDownLatch latch = new CountDownLatch(threadCount);
ExecutorService executor = Executors.newFixedThreadPool(threadCount);
for (int i = 0; i < threadCount; i++) {
final int threadId = i;
executor.submit(() -> {
try {
for (int j = 0; j < operationsPerThread; j++) {
String key = "thread-" + threadId + "-key-" + j;
String value = "thread-" + threadId + "-value-" + j;
hookData.set(key, value);
assertEquals(value, hookData.get(key));
}
} finally {
latch.countDown();
}
});
}
assertTrue(latch.await(10, TimeUnit.SECONDS));
executor.shutdown();
}
@Test
void shouldSupportNullValues() {
HookData hookData = HookData.create();
hookData.set("nullKey", null);
assertNull(hookData.get("nullKey"));
assertNull(hookData.get("nullKey", String.class));
}
}