|
| 1 | +package com.hindbiswas.jhp; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.*; |
| 4 | + |
| 5 | +import org.junit.jupiter.api.BeforeEach; |
| 6 | +import org.junit.jupiter.api.Test; |
| 7 | +import org.junit.jupiter.api.DisplayName; |
| 8 | + |
| 9 | +import java.util.*; |
| 10 | + |
| 11 | +@DisplayName("Context Tests") |
| 12 | +class ContextTest { |
| 13 | + |
| 14 | + private Context context; |
| 15 | + |
| 16 | + @BeforeEach |
| 17 | + void setUp() { |
| 18 | + context = new Context(); |
| 19 | + } |
| 20 | + |
| 21 | + @Test |
| 22 | + @DisplayName("Should add and retrieve string value") |
| 23 | + void testAddString() { |
| 24 | + context.add("name", "Alice"); |
| 25 | + Map<String, Object> ctx = context.getContext(); |
| 26 | + assertEquals("Alice", ctx.get("name")); |
| 27 | + } |
| 28 | + |
| 29 | + @Test |
| 30 | + @DisplayName("Should add and retrieve number value") |
| 31 | + void testAddNumber() { |
| 32 | + context.add("age", 25); |
| 33 | + Map<String, Object> ctx = context.getContext(); |
| 34 | + assertEquals(25, ctx.get("age")); |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + @DisplayName("Should add and retrieve boolean value") |
| 39 | + void testAddBoolean() { |
| 40 | + context.add("active", true); |
| 41 | + Map<String, Object> ctx = context.getContext(); |
| 42 | + assertEquals(true, ctx.get("active")); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + @DisplayName("Should handle null values") |
| 47 | + void testAddNull() { |
| 48 | + context.add("nullValue", null); |
| 49 | + Map<String, Object> ctx = context.getContext(); |
| 50 | + assertTrue(ctx.containsKey("nullValue")); |
| 51 | + assertNull(ctx.get("nullValue")); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + @DisplayName("Should convert POJO to Map") |
| 56 | + void testAddPOJO() { |
| 57 | + TestUser user = new TestUser("Bob", 30); |
| 58 | + context.add("user", user); |
| 59 | + |
| 60 | + Map<String, Object> ctx = context.getContext(); |
| 61 | + Object userObj = ctx.get("user"); |
| 62 | + |
| 63 | + assertInstanceOf(Map.class, userObj); |
| 64 | + @SuppressWarnings("unchecked") |
| 65 | + Map<String, Object> userMap = (Map<String, Object>) userObj; |
| 66 | + assertEquals("Bob", userMap.get("name")); |
| 67 | + assertEquals(30, userMap.get("age")); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + @DisplayName("Should convert array to Object array") |
| 72 | + void testAddArray() { |
| 73 | + int[] numbers = {1, 2, 3, 4, 5}; |
| 74 | + context.add("numbers", numbers); |
| 75 | + |
| 76 | + Map<String, Object> ctx = context.getContext(); |
| 77 | + Object numbersObj = ctx.get("numbers"); |
| 78 | + |
| 79 | + assertInstanceOf(Object[].class, numbersObj); |
| 80 | + Object[] arr = (Object[]) numbersObj; |
| 81 | + assertEquals(5, arr.length); |
| 82 | + assertEquals(1, arr[0]); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + @DisplayName("Should convert Collection to List") |
| 87 | + void testAddCollection() { |
| 88 | + List<String> items = Arrays.asList("apple", "banana", "cherry"); |
| 89 | + context.add("items", items); |
| 90 | + |
| 91 | + Map<String, Object> ctx = context.getContext(); |
| 92 | + Object itemsObj = ctx.get("items"); |
| 93 | + |
| 94 | + assertInstanceOf(List.class, itemsObj); |
| 95 | + @SuppressWarnings("unchecked") |
| 96 | + List<Object> list = (List<Object>) itemsObj; |
| 97 | + assertEquals(3, list.size()); |
| 98 | + assertEquals("apple", list.get(0)); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + @DisplayName("Should preserve Map as-is") |
| 103 | + void testAddMap() { |
| 104 | + Map<String, Object> data = new HashMap<>(); |
| 105 | + data.put("key1", "value1"); |
| 106 | + data.put("key2", 42); |
| 107 | + |
| 108 | + context.add("data", data); |
| 109 | + |
| 110 | + Map<String, Object> ctx = context.getContext(); |
| 111 | + Object dataObj = ctx.get("data"); |
| 112 | + |
| 113 | + assertInstanceOf(Map.class, dataObj); |
| 114 | + @SuppressWarnings("unchecked") |
| 115 | + Map<String, Object> map = (Map<String, Object>) dataObj; |
| 116 | + assertEquals("value1", map.get("key1")); |
| 117 | + assertEquals(42, map.get("key2")); |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + @DisplayName("Should handle multiple additions") |
| 122 | + void testMultipleAdditions() { |
| 123 | + context.add("name", "Alice"); |
| 124 | + context.add("age", 25); |
| 125 | + context.add("active", true); |
| 126 | + |
| 127 | + Map<String, Object> ctx = context.getContext(); |
| 128 | + assertEquals(3, ctx.size()); |
| 129 | + assertEquals("Alice", ctx.get("name")); |
| 130 | + assertEquals(25, ctx.get("age")); |
| 131 | + assertEquals(true, ctx.get("active")); |
| 132 | + } |
| 133 | + |
| 134 | + @Test |
| 135 | + @DisplayName("Should overwrite existing key") |
| 136 | + void testOverwriteKey() { |
| 137 | + context.add("name", "Alice"); |
| 138 | + context.add("name", "Bob"); |
| 139 | + |
| 140 | + Map<String, Object> ctx = context.getContext(); |
| 141 | + assertEquals("Bob", ctx.get("name")); |
| 142 | + } |
| 143 | + |
| 144 | + // Helper class for testing POJO conversion |
| 145 | + static class TestUser { |
| 146 | + public String name; |
| 147 | + public int age; |
| 148 | + |
| 149 | + public TestUser(String name, int age) { |
| 150 | + this.name = name; |
| 151 | + this.age = age; |
| 152 | + } |
| 153 | + } |
| 154 | +} |
0 commit comments