-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathStreamHTTPClientTest.java
More file actions
132 lines (111 loc) · 4.54 KB
/
StreamHTTPClientTest.java
File metadata and controls
132 lines (111 loc) · 4.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package io.getstream;
import static org.junit.jupiter.api.Assertions.*;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.getstream.models.MessageResponse;
import io.getstream.models.UpdateAppRequest;
import io.getstream.services.framework.StreamHTTPClient;
import java.util.Date;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
public class StreamHTTPClientTest {
private static StreamHTTPClient client;
private static ObjectMapper objectMapper;
@BeforeAll
static void setup() {
// Initialize with test credentials (secret must be at least 32 characters for HS256)
client = new StreamHTTPClient();
objectMapper = client.getObjectMapper();
}
@Test
void testUnixNanosecondTimestampParsing() throws Exception {
long timestampInNanos = 1704542400000000000L;
long timestampInMillis = timestampInNanos / 1_000_000;
// Create a JSON response with unix microsecond timestamp
String json =
String.format(
"""
{
"id": "test-message-id",
"text": "Test message",
"type": "regular",
"created_at": %d,
"updated_at": %d
}
""",
timestampInNanos, timestampInNanos);
// Parse the JSON
MessageResponse message = objectMapper.readValue(json, MessageResponse.class);
// Expected date: 2024-01-06 12:00:00 UTC
Date expectedDate = new Date(timestampInMillis);
// Assert that the parsed date matches the expected date
assertEquals(
expectedDate.getTime(),
message.getCreatedAt().getTime(),
1000, // Allow 1 second tolerance
String.format(
"Expected timestamp %d (2024-01-06) but got %d (%s)",
expectedDate.getTime(), message.getCreatedAt().getTime(), message.getCreatedAt()));
assertEquals(
expectedDate.getTime(),
message.getUpdatedAt().getTime(),
1000, // Allow 1 second tolerance
String.format(
"Expected timestamp %d (2024-01-06) but got %d (%s)",
expectedDate.getTime(), message.getUpdatedAt().getTime(), message.getUpdatedAt()));
}
@Test
void testNullFieldsOmittedFromSerialization() throws JsonProcessingException {
// Only set one field, leave everything else null
UpdateAppRequest request = UpdateAppRequest.builder().enforceUniqueUsernames("no").build();
String json = objectMapper.writeValueAsString(request);
// The set field must be present
assertTrue(json.contains("\"enforce_unique_usernames\":\"no\""));
// Null fields must be omitted, not serialized as null
assertFalse(json.contains("null"), "Null fields should be omitted, got: " + json);
assertFalse(json.contains("webhook_url"));
assertFalse(json.contains("multi_tenant_enabled"));
}
@Test
void testCollectionFieldsSerializedWhenSet() throws JsonProcessingException {
// An explicitly set empty list should still be serialized
UpdateAppRequest request = UpdateAppRequest.builder().grants(new java.util.HashMap<>()).build();
String json = objectMapper.writeValueAsString(request);
assertTrue(
json.contains("\"grants\":{}"),
"Empty collections should be serialized when explicitly set, got: " + json);
}
@Test
void testRFC3339TimestampParsing() throws Exception {
// Create a JSON response with RFC 3339 formatted timestamp
String json =
"""
{
"id": "test-message-id",
"text": "Test message",
"type": "regular",
"created_at": "2024-01-06T12:00:00.000Z",
"updated_at": "2024-01-06T12:00:00Z"
}
""";
// Parse the JSON
MessageResponse message = objectMapper.readValue(json, MessageResponse.class);
// Expected date: 2024-01-06 12:00:00 UTC
Date expectedDate = new Date(1704542400000L);
// Assert that the parsed date matches the expected date
assertEquals(
expectedDate.getTime(),
message.getCreatedAt().getTime(),
1000, // Allow 1 second tolerance
String.format(
"Expected timestamp %d (2024-01-06T12:00:00Z) but got %d (%s)",
expectedDate.getTime(), message.getCreatedAt().getTime(), message.getCreatedAt()));
assertEquals(
expectedDate.getTime(),
message.getUpdatedAt().getTime(),
1000, // Allow 1 second tolerance
String.format(
"Expected timestamp %d (2024-01-06T12:00:00Z) but got %d (%s)",
expectedDate.getTime(), message.getUpdatedAt().getTime(), message.getUpdatedAt()));
}
}