|
| 1 | +package dev.openfeature.sdk; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.*; |
| 4 | + |
| 5 | +import java.util.concurrent.CountDownLatch; |
| 6 | +import java.util.concurrent.ExecutorService; |
| 7 | +import java.util.concurrent.Executors; |
| 8 | +import java.util.concurrent.TimeUnit; |
| 9 | +import org.junit.jupiter.api.Test; |
| 10 | + |
| 11 | +class HookDataTest { |
| 12 | + |
| 13 | + @Test |
| 14 | + void shouldStoreAndRetrieveValues() { |
| 15 | + HookData hookData = HookData.create(); |
| 16 | + |
| 17 | + hookData.set("key1", "value1"); |
| 18 | + hookData.set("key2", 42); |
| 19 | + hookData.set("key3", true); |
| 20 | + |
| 21 | + assertEquals("value1", hookData.get("key1")); |
| 22 | + assertEquals(42, hookData.get("key2")); |
| 23 | + assertEquals(true, hookData.get("key3")); |
| 24 | + } |
| 25 | + |
| 26 | + @Test |
| 27 | + void shouldReturnNullForMissingKeys() { |
| 28 | + HookData hookData = HookData.create(); |
| 29 | + |
| 30 | + assertNull(hookData.get("nonexistent")); |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + void shouldSupportTypeSafeRetrieval() { |
| 35 | + HookData hookData = HookData.create(); |
| 36 | + |
| 37 | + hookData.set("string", "hello"); |
| 38 | + hookData.set("integer", 123); |
| 39 | + hookData.set("boolean", false); |
| 40 | + |
| 41 | + assertEquals("hello", hookData.get("string", String.class)); |
| 42 | + assertEquals(Integer.valueOf(123), hookData.get("integer", Integer.class)); |
| 43 | + assertEquals(Boolean.FALSE, hookData.get("boolean", Boolean.class)); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + void shouldReturnNullForMissingKeysWithType() { |
| 48 | + HookData hookData = HookData.create(); |
| 49 | + |
| 50 | + assertNull(hookData.get("missing", String.class)); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + void shouldThrowClassCastExceptionForWrongType() { |
| 55 | + HookData hookData = HookData.create(); |
| 56 | + |
| 57 | + hookData.set("string", "not a number"); |
| 58 | + |
| 59 | + assertThrows(ClassCastException.class, () -> { |
| 60 | + hookData.get("string", Integer.class); |
| 61 | + }); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + void shouldOverwriteExistingValues() { |
| 66 | + HookData hookData = HookData.create(); |
| 67 | + |
| 68 | + hookData.set("key", "original"); |
| 69 | + assertEquals("original", hookData.get("key")); |
| 70 | + |
| 71 | + hookData.set("key", "updated"); |
| 72 | + assertEquals("updated", hookData.get("key")); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + void shouldBeThreadSafe() throws InterruptedException { |
| 77 | + HookData hookData = HookData.create(); |
| 78 | + int threadCount = 10; |
| 79 | + int operationsPerThread = 100; |
| 80 | + CountDownLatch latch = new CountDownLatch(threadCount); |
| 81 | + ExecutorService executor = Executors.newFixedThreadPool(threadCount); |
| 82 | + |
| 83 | + for (int i = 0; i < threadCount; i++) { |
| 84 | + final int threadId = i; |
| 85 | + executor.submit(() -> { |
| 86 | + try { |
| 87 | + for (int j = 0; j < operationsPerThread; j++) { |
| 88 | + String key = "thread-" + threadId + "-key-" + j; |
| 89 | + String value = "thread-" + threadId + "-value-" + j; |
| 90 | + hookData.set(key, value); |
| 91 | + assertEquals(value, hookData.get(key)); |
| 92 | + } |
| 93 | + } finally { |
| 94 | + latch.countDown(); |
| 95 | + } |
| 96 | + }); |
| 97 | + } |
| 98 | + |
| 99 | + assertTrue(latch.await(10, TimeUnit.SECONDS)); |
| 100 | + executor.shutdown(); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + void shouldSupportNullValues() { |
| 105 | + HookData hookData = HookData.create(); |
| 106 | + |
| 107 | + hookData.set("nullKey", null); |
| 108 | + assertNull(hookData.get("nullKey")); |
| 109 | + assertNull(hookData.get("nullKey", String.class)); |
| 110 | + } |
| 111 | +} |
| 112 | + |
0 commit comments