|
| 1 | +package io.getstream; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.*; |
| 4 | + |
| 5 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 6 | +import io.getstream.models.MessageResponse; |
| 7 | +import io.getstream.services.framework.StreamHTTPClient; |
| 8 | +import java.util.Date; |
| 9 | +import org.junit.jupiter.api.BeforeAll; |
| 10 | +import org.junit.jupiter.api.Test; |
| 11 | + |
| 12 | +public class StreamHTTPClientTest { |
| 13 | + private static StreamHTTPClient client; |
| 14 | + private static ObjectMapper objectMapper; |
| 15 | + |
| 16 | + @BeforeAll |
| 17 | + static void setup() { |
| 18 | + // Initialize with test credentials (secret must be at least 32 characters for HS256) |
| 19 | + client = new StreamHTTPClient(); |
| 20 | + objectMapper = client.getObjectMapper(); |
| 21 | + } |
| 22 | + |
| 23 | + @Test |
| 24 | + void testUnixNanosecondTimestampParsing() throws Exception { |
| 25 | + long timestampInNanos = 1704542400000000000L; |
| 26 | + long timestampInMillis = timestampInNanos / 1_000_000; |
| 27 | + |
| 28 | + // Create a JSON response with unix microsecond timestamp |
| 29 | + String json = |
| 30 | + String.format( |
| 31 | + """ |
| 32 | + { |
| 33 | + "id": "test-message-id", |
| 34 | + "text": "Test message", |
| 35 | + "type": "regular", |
| 36 | + "created_at": %d, |
| 37 | + "updated_at": %d |
| 38 | + } |
| 39 | + """, |
| 40 | + timestampInNanos, timestampInNanos); |
| 41 | + |
| 42 | + // Parse the JSON |
| 43 | + MessageResponse message = objectMapper.readValue(json, MessageResponse.class); |
| 44 | + |
| 45 | + // Expected date: 2024-01-06 12:00:00 UTC |
| 46 | + Date expectedDate = new Date(timestampInMillis); |
| 47 | + |
| 48 | + // Assert that the parsed date matches the expected date |
| 49 | + assertEquals( |
| 50 | + expectedDate.getTime(), |
| 51 | + message.getCreatedAt().getTime(), |
| 52 | + 1000, // Allow 1 second tolerance |
| 53 | + String.format( |
| 54 | + "Expected timestamp %d (2024-01-06) but got %d (%s)", |
| 55 | + expectedDate.getTime(), message.getCreatedAt().getTime(), message.getCreatedAt())); |
| 56 | + |
| 57 | + assertEquals( |
| 58 | + expectedDate.getTime(), |
| 59 | + message.getUpdatedAt().getTime(), |
| 60 | + 1000, // Allow 1 second tolerance |
| 61 | + String.format( |
| 62 | + "Expected timestamp %d (2024-01-06) but got %d (%s)", |
| 63 | + expectedDate.getTime(), message.getUpdatedAt().getTime(), message.getUpdatedAt())); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + void testRFC3339TimestampParsing() throws Exception { |
| 68 | + // Create a JSON response with RFC 3339 formatted timestamp |
| 69 | + String json = |
| 70 | + """ |
| 71 | + { |
| 72 | + "id": "test-message-id", |
| 73 | + "text": "Test message", |
| 74 | + "type": "regular", |
| 75 | + "created_at": "2024-01-06T12:00:00.000Z", |
| 76 | + "updated_at": "2024-01-06T12:00:00Z" |
| 77 | + } |
| 78 | + """; |
| 79 | + |
| 80 | + // Parse the JSON |
| 81 | + MessageResponse message = objectMapper.readValue(json, MessageResponse.class); |
| 82 | + |
| 83 | + // Expected date: 2024-01-06 12:00:00 UTC |
| 84 | + Date expectedDate = new Date(1704542400000L); |
| 85 | + |
| 86 | + // Assert that the parsed date matches the expected date |
| 87 | + assertEquals( |
| 88 | + expectedDate.getTime(), |
| 89 | + message.getCreatedAt().getTime(), |
| 90 | + 1000, // Allow 1 second tolerance |
| 91 | + String.format( |
| 92 | + "Expected timestamp %d (2024-01-06T12:00:00Z) but got %d (%s)", |
| 93 | + expectedDate.getTime(), message.getCreatedAt().getTime(), message.getCreatedAt())); |
| 94 | + |
| 95 | + assertEquals( |
| 96 | + expectedDate.getTime(), |
| 97 | + message.getUpdatedAt().getTime(), |
| 98 | + 1000, // Allow 1 second tolerance |
| 99 | + String.format( |
| 100 | + "Expected timestamp %d (2024-01-06T12:00:00Z) but got %d (%s)", |
| 101 | + expectedDate.getTime(), message.getUpdatedAt().getTime(), message.getUpdatedAt())); |
| 102 | + } |
| 103 | +} |
0 commit comments