-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathStreamHTTPClientTest.java
More file actions
208 lines (176 loc) · 7.57 KB
/
StreamHTTPClientTest.java
File metadata and controls
208 lines (176 loc) · 7.57 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
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.TrackActivityMetricsEvent;
import io.getstream.models.TrackActivityMetricsRequest;
import io.getstream.models.UpdateAppRequest;
import io.getstream.services.framework.StreamHTTPClient;
import io.getstream.services.framework.StreamSDKClient;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import okhttp3.ConnectionPool;
import okhttp3.OkHttpClient;
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 testActivityMetricsConfigSerializedWhenSet() throws JsonProcessingException {
UpdateAppRequest request =
UpdateAppRequest.builder()
.activityMetricsConfig(Map.of("views", 10, "clicks", 5, "shares", 25))
.build();
String json = objectMapper.writeValueAsString(request);
assertTrue(json.contains("\"activity_metrics_config\""), "Expected config field in: " + json);
assertTrue(json.contains("\"views\":10"), "Expected default metric override in: " + json);
assertTrue(json.contains("\"clicks\":5"), "Expected default metric override in: " + json);
assertTrue(json.contains("\"shares\":25"), "Expected custom metric in: " + json);
}
@Test
void testTrackActivityMetricsRequestSerializedWithCustomMetric() throws JsonProcessingException {
TrackActivityMetricsRequest request =
TrackActivityMetricsRequest.builder()
.userID("user-123")
.events(
List.of(
TrackActivityMetricsEvent.builder()
.activityID("activity-123")
.metric("shares")
.delta(3)
.build()))
.build();
String json = objectMapper.writeValueAsString(request);
assertTrue(json.contains("\"user_id\":\"user-123\""), "Expected user_id in: " + json);
assertTrue(json.contains("\"events\""), "Expected events array in: " + json);
assertTrue(
json.contains("\"activity_id\":\"activity-123\""), "Expected activity ID in: " + json);
assertTrue(json.contains("\"metric\":\"shares\""), "Expected custom metric in: " + json);
assertTrue(json.contains("\"delta\":3"), "Expected delta in: " + json);
}
@Test
void testCustomOkHttpClientPreservesConfig() {
ConnectionPool customPool = new ConnectionPool(20, 120, TimeUnit.SECONDS);
OkHttpClient customHttp =
new OkHttpClient.Builder()
.connectionPool(customPool)
.connectTimeout(30, TimeUnit.SECONDS)
.readTimeout(45, TimeUnit.SECONDS)
.build();
var sdkClient =
new StreamSDKClient(
System.getenv("STREAM_API_KEY"), System.getenv("STREAM_API_SECRET"), customHttp);
OkHttpClient builtClient = sdkClient.getHttpClient().getHttpClient();
assertSame(customPool, builtClient.connectionPool());
assertEquals(30_000, builtClient.connectTimeoutMillis());
assertEquals(45_000, builtClient.readTimeoutMillis());
assertFalse(
builtClient.interceptors().isEmpty(), "SDK should add its interceptors to the client");
}
@Test
void testDefaultConstructorStillWorks() {
assertNotNull(client.getHttpClient());
assertFalse(client.getHttpClient().interceptors().isEmpty());
}
@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()));
}
}