|
| 1 | +package dev.openfeature.api; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertNotEquals; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 6 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 7 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 8 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 9 | + |
| 10 | +import java.util.Map; |
| 11 | +import org.junit.jupiter.api.Test; |
| 12 | + |
| 13 | +class EnhancedImmutableMetadataTest { |
| 14 | + |
| 15 | + @Test |
| 16 | + void builder_shouldCreateEmptyMetadata() { |
| 17 | + ImmutableMetadata metadata = ImmutableMetadata.builder().build(); |
| 18 | + |
| 19 | + assertNotNull(metadata); |
| 20 | + assertTrue(metadata.asUnmodifiableObjectMap().isEmpty()); |
| 21 | + } |
| 22 | + |
| 23 | + @Test |
| 24 | + void builder_addString_shouldAddStringValue() { |
| 25 | + String key = "stringKey"; |
| 26 | + String value = "stringValue"; |
| 27 | + |
| 28 | + ImmutableMetadata metadata = |
| 29 | + ImmutableMetadata.builder().addString(key, value).build(); |
| 30 | + |
| 31 | + assertEquals(1, metadata.asUnmodifiableObjectMap().size()); |
| 32 | + assertEquals(value, metadata.asUnmodifiableObjectMap().get(key)); |
| 33 | + assertEquals(value, metadata.getString(key)); |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + void builder_addInteger_shouldAddIntegerValue() { |
| 38 | + String key = "intKey"; |
| 39 | + Integer value = 42; |
| 40 | + |
| 41 | + ImmutableMetadata metadata = |
| 42 | + ImmutableMetadata.builder().addInteger(key, value).build(); |
| 43 | + |
| 44 | + assertEquals(1, metadata.asUnmodifiableObjectMap().size()); |
| 45 | + assertEquals(value, metadata.getInteger(key)); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + void builder_addLong_shouldAddLongValue() { |
| 50 | + String key = "longKey"; |
| 51 | + Long value = 1234567890L; |
| 52 | + |
| 53 | + ImmutableMetadata metadata = |
| 54 | + ImmutableMetadata.builder().addLong(key, value).build(); |
| 55 | + |
| 56 | + assertEquals(1, metadata.asUnmodifiableObjectMap().size()); |
| 57 | + assertEquals(value, metadata.getLong(key)); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + void builder_addFloat_shouldAddFloatValue() { |
| 62 | + String key = "floatKey"; |
| 63 | + Float value = 3.14f; |
| 64 | + |
| 65 | + ImmutableMetadata metadata = |
| 66 | + ImmutableMetadata.builder().addFloat(key, value).build(); |
| 67 | + |
| 68 | + assertEquals(1, metadata.asUnmodifiableObjectMap().size()); |
| 69 | + assertEquals(value, metadata.getFloat(key)); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + void builder_addDouble_shouldAddDoubleValue() { |
| 74 | + String key = "doubleKey"; |
| 75 | + Double value = 3.141592653589793; |
| 76 | + |
| 77 | + ImmutableMetadata metadata = |
| 78 | + ImmutableMetadata.builder().addDouble(key, value).build(); |
| 79 | + |
| 80 | + assertEquals(1, metadata.asUnmodifiableObjectMap().size()); |
| 81 | + assertEquals(value, metadata.getDouble(key)); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + void builder_addBoolean_shouldAddBooleanValue() { |
| 86 | + String key = "boolKey"; |
| 87 | + Boolean value = true; |
| 88 | + |
| 89 | + ImmutableMetadata metadata = |
| 90 | + ImmutableMetadata.builder().addBoolean(key, value).build(); |
| 91 | + |
| 92 | + assertEquals(1, metadata.asUnmodifiableObjectMap().size()); |
| 93 | + assertEquals(value, metadata.getBoolean(key)); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + void builder_shouldAddMultipleValuesOfDifferentTypes() { |
| 98 | + ImmutableMetadata metadata = ImmutableMetadata.builder() |
| 99 | + .addString("stringKey", "stringValue") |
| 100 | + .addInteger("intKey", 42) |
| 101 | + .addLong("longKey", 1234567890L) |
| 102 | + .addFloat("floatKey", 3.14f) |
| 103 | + .addDouble("doubleKey", 3.141592653589793) |
| 104 | + .addBoolean("boolKey", true) |
| 105 | + .build(); |
| 106 | + |
| 107 | + assertEquals(6, metadata.asUnmodifiableObjectMap().size()); |
| 108 | + assertEquals("stringValue", metadata.getString("stringKey")); |
| 109 | + assertEquals(Integer.valueOf(42), metadata.getInteger("intKey")); |
| 110 | + assertEquals(Long.valueOf(1234567890L), metadata.getLong("longKey")); |
| 111 | + assertEquals(Float.valueOf(3.14f), metadata.getFloat("floatKey")); |
| 112 | + assertEquals(Double.valueOf(3.141592653589793), metadata.getDouble("doubleKey")); |
| 113 | + assertEquals(Boolean.TRUE, metadata.getBoolean("boolKey")); |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + void builder_shouldHandleNullValues() { |
| 118 | + ImmutableMetadata metadata = ImmutableMetadata.builder() |
| 119 | + .addString("stringKey", null) |
| 120 | + .addInteger("intKey", null) |
| 121 | + .addLong("longKey", null) |
| 122 | + .addFloat("floatKey", null) |
| 123 | + .addDouble("doubleKey", null) |
| 124 | + .addBoolean("boolKey", null) |
| 125 | + .build(); |
| 126 | + |
| 127 | + assertEquals(6, metadata.asUnmodifiableObjectMap().size()); |
| 128 | + assertNull(metadata.getString("stringKey")); |
| 129 | + assertNull(metadata.getInteger("intKey")); |
| 130 | + assertNull(metadata.getLong("longKey")); |
| 131 | + assertNull(metadata.getFloat("floatKey")); |
| 132 | + assertNull(metadata.getDouble("doubleKey")); |
| 133 | + assertNull(metadata.getBoolean("boolKey")); |
| 134 | + } |
| 135 | + |
| 136 | + @Test |
| 137 | + void builder_shouldOverwriteExistingKeys() { |
| 138 | + ImmutableMetadata metadata = ImmutableMetadata.builder() |
| 139 | + .addString("key", "firstValue") |
| 140 | + .addString("key", "secondValue") |
| 141 | + .build(); |
| 142 | + |
| 143 | + assertEquals(1, metadata.asUnmodifiableObjectMap().size()); |
| 144 | + assertEquals("secondValue", metadata.getString("key")); |
| 145 | + } |
| 146 | + |
| 147 | + @Test |
| 148 | + void builder_shouldAllowChaining() { |
| 149 | + ImmutableMetadata metadata = ImmutableMetadata.builder() |
| 150 | + .addString("key1", "value1") |
| 151 | + .addInteger("key2", 42) |
| 152 | + .addBoolean("key3", true) |
| 153 | + .build(); |
| 154 | + |
| 155 | + assertEquals(3, metadata.asUnmodifiableObjectMap().size()); |
| 156 | + assertEquals("value1", metadata.getString("key1")); |
| 157 | + assertEquals(Integer.valueOf(42), metadata.getInteger("key2")); |
| 158 | + assertEquals(Boolean.TRUE, metadata.getBoolean("key3")); |
| 159 | + } |
| 160 | + |
| 161 | + @Test |
| 162 | + void getters_shouldReturnNullForMissingKeys() { |
| 163 | + ImmutableMetadata metadata = ImmutableMetadata.builder().build(); |
| 164 | + |
| 165 | + assertNull(metadata.getString("missing")); |
| 166 | + assertNull(metadata.getInteger("missing")); |
| 167 | + assertNull(metadata.getLong("missing")); |
| 168 | + assertNull(metadata.getFloat("missing")); |
| 169 | + assertNull(metadata.getDouble("missing")); |
| 170 | + assertNull(metadata.getBoolean("missing")); |
| 171 | + } |
| 172 | + |
| 173 | + @Test |
| 174 | + void getters_shouldReturnNullForWrongType() { |
| 175 | + ImmutableMetadata metadata = |
| 176 | + ImmutableMetadata.builder().addString("key", "stringValue").build(); |
| 177 | + |
| 178 | + assertEquals("stringValue", metadata.getString("key")); |
| 179 | + assertNull(metadata.getInteger("key")); // Wrong type should return null |
| 180 | + assertNull(metadata.getLong("key")); |
| 181 | + assertNull(metadata.getFloat("key")); |
| 182 | + assertNull(metadata.getDouble("key")); |
| 183 | + assertNull(metadata.getBoolean("key")); |
| 184 | + } |
| 185 | + |
| 186 | + @Test |
| 187 | + void asUnmodifiableObjectMap_shouldReturnUnmodifiableMap() { |
| 188 | + ImmutableMetadata metadata = |
| 189 | + ImmutableMetadata.builder().addString("key", "value").build(); |
| 190 | + |
| 191 | + Map<String, Object> map = metadata.asUnmodifiableObjectMap(); |
| 192 | + assertEquals(1, map.size()); |
| 193 | + assertEquals("value", map.get("key")); |
| 194 | + |
| 195 | + // Should be unmodifiable |
| 196 | + assertThrows(UnsupportedOperationException.class, () -> { |
| 197 | + map.put("newKey", "newValue"); |
| 198 | + }); |
| 199 | + |
| 200 | + assertThrows(UnsupportedOperationException.class, () -> { |
| 201 | + map.remove("key"); |
| 202 | + }); |
| 203 | + |
| 204 | + assertThrows(UnsupportedOperationException.class, () -> { |
| 205 | + map.clear(); |
| 206 | + }); |
| 207 | + } |
| 208 | + |
| 209 | + @Test |
| 210 | + void equals_shouldWorkCorrectly() { |
| 211 | + ImmutableMetadata metadata1 = ImmutableMetadata.builder() |
| 212 | + .addString("key1", "value1") |
| 213 | + .addInteger("key2", 42) |
| 214 | + .build(); |
| 215 | + |
| 216 | + ImmutableMetadata metadata2 = ImmutableMetadata.builder() |
| 217 | + .addString("key1", "value1") |
| 218 | + .addInteger("key2", 42) |
| 219 | + .build(); |
| 220 | + |
| 221 | + ImmutableMetadata metadata3 = ImmutableMetadata.builder() |
| 222 | + .addString("key1", "different") |
| 223 | + .addInteger("key2", 42) |
| 224 | + .build(); |
| 225 | + |
| 226 | + // Same content should be equal |
| 227 | + assertEquals(metadata1, metadata2); |
| 228 | + assertEquals(metadata2, metadata1); |
| 229 | + |
| 230 | + // Different content should not be equal |
| 231 | + assertNotEquals(metadata1, metadata3); |
| 232 | + |
| 233 | + // Self-equality |
| 234 | + assertEquals(metadata1, metadata1); |
| 235 | + |
| 236 | + // Null comparison |
| 237 | + assertNotEquals(metadata1, null); |
| 238 | + |
| 239 | + // Different class comparison |
| 240 | + assertNotEquals(metadata1, "not metadata"); |
| 241 | + } |
| 242 | + |
| 243 | + @Test |
| 244 | + void hashCode_shouldBeConsistent() { |
| 245 | + ImmutableMetadata metadata1 = ImmutableMetadata.builder() |
| 246 | + .addString("key1", "value1") |
| 247 | + .addInteger("key2", 42) |
| 248 | + .build(); |
| 249 | + |
| 250 | + ImmutableMetadata metadata2 = ImmutableMetadata.builder() |
| 251 | + .addString("key1", "value1") |
| 252 | + .addInteger("key2", 42) |
| 253 | + .build(); |
| 254 | + |
| 255 | + assertEquals(metadata1.hashCode(), metadata2.hashCode()); |
| 256 | + } |
| 257 | + |
| 258 | + @Test |
| 259 | + void toString_shouldIncludeContent() { |
| 260 | + ImmutableMetadata metadata = ImmutableMetadata.builder() |
| 261 | + .addString("stringKey", "stringValue") |
| 262 | + .addInteger("intKey", 42) |
| 263 | + .build(); |
| 264 | + |
| 265 | + String toString = metadata.toString(); |
| 266 | + assertTrue(toString.contains("ImmutableMetadata")); |
| 267 | + // Note: toString uses default Object.toString, content not directly included |
| 268 | + assertNotNull(toString); |
| 269 | + } |
| 270 | + |
| 271 | + @Test |
| 272 | + void builder_shouldCreateIndependentInstances() { |
| 273 | + ImmutableMetadata.Builder builder = ImmutableMetadata.builder().addString("key1", "value1"); |
| 274 | + |
| 275 | + ImmutableMetadata metadata1 = builder.build(); |
| 276 | + |
| 277 | + // Adding to builder after first build should not affect first instance |
| 278 | + builder.addString("key2", "value2"); |
| 279 | + ImmutableMetadata metadata2 = builder.build(); |
| 280 | + |
| 281 | + assertEquals(1, metadata1.asUnmodifiableObjectMap().size()); |
| 282 | + assertEquals(2, metadata2.asUnmodifiableObjectMap().size()); |
| 283 | + assertNull(metadata1.getString("key2")); |
| 284 | + assertEquals("value2", metadata2.getString("key2")); |
| 285 | + } |
| 286 | + |
| 287 | + @Test |
| 288 | + void numberTypes_shouldBeStoredCorrectly() { |
| 289 | + // Test edge cases for numeric types |
| 290 | + ImmutableMetadata metadata = ImmutableMetadata.builder() |
| 291 | + .addInteger("maxInt", Integer.MAX_VALUE) |
| 292 | + .addInteger("minInt", Integer.MIN_VALUE) |
| 293 | + .addLong("maxLong", Long.MAX_VALUE) |
| 294 | + .addLong("minLong", Long.MIN_VALUE) |
| 295 | + .addFloat("maxFloat", Float.MAX_VALUE) |
| 296 | + .addFloat("minFloat", Float.MIN_VALUE) |
| 297 | + .addDouble("maxDouble", Double.MAX_VALUE) |
| 298 | + .addDouble("minDouble", Double.MIN_VALUE) |
| 299 | + .build(); |
| 300 | + |
| 301 | + assertEquals(Integer.MAX_VALUE, metadata.getInteger("maxInt")); |
| 302 | + assertEquals(Integer.MIN_VALUE, metadata.getInteger("minInt")); |
| 303 | + assertEquals(Long.MAX_VALUE, metadata.getLong("maxLong")); |
| 304 | + assertEquals(Long.MIN_VALUE, metadata.getLong("minLong")); |
| 305 | + assertEquals(Float.MAX_VALUE, metadata.getFloat("maxFloat")); |
| 306 | + assertEquals(Float.MIN_VALUE, metadata.getFloat("minFloat")); |
| 307 | + assertEquals(Double.MAX_VALUE, metadata.getDouble("maxDouble")); |
| 308 | + assertEquals(Double.MIN_VALUE, metadata.getDouble("minDouble")); |
| 309 | + } |
| 310 | +} |
0 commit comments