|
12 | 12 | import org.mockito.Mock; |
13 | 13 | import org.mockito.junit.jupiter.MockitoExtension; |
14 | 14 | import org.springframework.http.HttpStatus; |
| 15 | +import org.springframework.test.util.ReflectionTestUtils; |
15 | 16 |
|
| 17 | +import java.time.Instant; |
16 | 18 | import java.util.*; |
17 | 19 |
|
18 | 20 | import static org.junit.jupiter.api.Assertions.*; |
@@ -255,4 +257,68 @@ void testMapPayloadToCassandraColumns() { |
255 | 257 | Map<String, Object> result = ContentStateServiceImpl.mapPayloadToCassandraColumns(payload, mapping); |
256 | 258 | assertEquals("bar", result.get("baz")); |
257 | 259 | } |
| 260 | + |
| 261 | + @Test |
| 262 | + void testConvertInstantsToString_singleInstant() { |
| 263 | + Instant now = Instant.now(); |
| 264 | + Object result = invokeConvert(now); |
| 265 | + |
| 266 | + assertTrue(result instanceof String); |
| 267 | + assertEquals(now.toString(), result); |
| 268 | + } |
| 269 | + |
| 270 | + @Test |
| 271 | + void testConvertInstantsToString_mapWithInstant() { |
| 272 | + Instant now = Instant.now(); |
| 273 | + Map<String, Object> input = Map.of("time", now); |
| 274 | + |
| 275 | + Object result = invokeConvert(input); |
| 276 | + |
| 277 | + assertTrue(result instanceof Map); |
| 278 | + assertEquals(now.toString(), ((Map<?, ?>) result).get("time")); |
| 279 | + } |
| 280 | + |
| 281 | + @Test |
| 282 | + void testConvertInstantsToString_listWithInstant() { |
| 283 | + Instant now = Instant.now(); |
| 284 | + List<Object> input = List.of(now, "test"); |
| 285 | + |
| 286 | + Object result = invokeConvert(input); |
| 287 | + |
| 288 | + assertTrue(result instanceof List); |
| 289 | + assertEquals(now.toString(), ((List<?>) result).get(0)); |
| 290 | + assertEquals("test", ((List<?>) result).get(1)); |
| 291 | + } |
| 292 | + |
| 293 | + @Test |
| 294 | + void testConvertInstantsToString_nestedMapAndList() { |
| 295 | + Instant now = Instant.now(); |
| 296 | + Map<String, Object> input = Map.of( |
| 297 | + "list", List.of(Map.of("time", now)) |
| 298 | + ); |
| 299 | + |
| 300 | + Object result = invokeConvert(input); |
| 301 | + |
| 302 | + assertEquals( |
| 303 | + now.toString(), |
| 304 | + ((Map<?, ?>) ((List<?>) ((Map<?, ?>) result).get("list")).get(0)).get("time") |
| 305 | + ); |
| 306 | + } |
| 307 | + |
| 308 | + @Test |
| 309 | + void testConvertInstantsToString_nonInstantValue() { |
| 310 | + String value = "not a time"; |
| 311 | + Object result = invokeConvert(value); |
| 312 | + |
| 313 | + assertSame(value, result); |
| 314 | + } |
| 315 | + |
| 316 | + private Object invokeConvert(Object value) { |
| 317 | + // Call the private static method using ReflectionTestUtils |
| 318 | + return ReflectionTestUtils.invokeMethod( |
| 319 | + ContentStateServiceImpl.class, |
| 320 | + "convertInstantsToString", |
| 321 | + value |
| 322 | + ); |
| 323 | + } |
258 | 324 | } |
0 commit comments