Repository navigation
Expand file tree
/
Copy pathTaskSerializationTest.java
More file actions
786 lines (666 loc) · 28.6 KB
/
Copy pathTaskSerializationTest.java
File metadata and controls
786 lines (666 loc) · 28.6 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
package io.a2a.jsonrpc.common.json;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.OffsetDateTime;
import java.util.Base64;
import java.util.List;
import java.util.Map;
import io.a2a.spec.Artifact;
import io.a2a.spec.DataPart;
import io.a2a.spec.FileContent;
import io.a2a.spec.FilePart;
import io.a2a.spec.FileWithBytes;
import io.a2a.spec.FileWithUri;
import io.a2a.spec.Message;
import io.a2a.spec.Part;
import io.a2a.spec.Task;
import io.a2a.spec.TaskState;
import io.a2a.spec.TaskStatus;
import io.a2a.spec.TextPart;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
/**
* Tests for Task serialization and deserialization using Gson.
*/
class TaskSerializationTest {
@Test
void testBasicTaskSerialization() throws JsonProcessingException {
// Create a basic task
Task task = Task.builder()
.id("task-123")
.contextId("context-456")
.status(new TaskStatus(TaskState.TASK_STATE_SUBMITTED))
.build();
// Serialize to JSON
String json = JsonUtil.toJson(task);
// Verify JSON contains expected fields
assertNotNull(json);
assertTrue(json.contains("\"id\":\"task-123\""));
assertTrue(json.contains("\"state\":\"TASK_STATE_SUBMITTED\""));
// Deserialize back to Task
Task deserialized = JsonUtil.fromJson(json, Task.class);
// Verify deserialized task matches original
assertEquals(task.id(), deserialized.id());
assertEquals(task.status().state(), deserialized.status().state());
}
@Test
void testTaskWithTimestamp() throws JsonProcessingException {
OffsetDateTime timestamp = OffsetDateTime.now();
Task task = Task.builder()
.id("task-123")
.contextId("context-456")
.status(new TaskStatus(TaskState.TASK_STATE_WORKING, null, timestamp))
.build();
// Serialize
String json = JsonUtil.toJson(task);
// Deserialize
Task deserialized = JsonUtil.fromJson(json, Task.class);
// Verify OffsetDateTime timestamp is preserved
assertNotNull(deserialized.status().timestamp());
assertEquals(task.status().timestamp(), deserialized.status().timestamp());
}
@Test
void testTaskWithArtifacts() throws JsonProcessingException {
Artifact artifact = Artifact.builder()
.artifactId("artifact-1")
.name("Test Artifact")
.description("Description of artifact")
.parts(List.of(
new TextPart("Hello"),
new TextPart("World")
))
.build();
Task task = Task.builder()
.id("task-123")
.contextId("context-456")
.status(new TaskStatus(TaskState.TASK_STATE_COMPLETED))
.artifacts(List.of(artifact))
.build();
// Serialize
String json = JsonUtil.toJson(task);
// Verify JSON contains artifact data
assertTrue(json.contains("\"artifactId\":\"artifact-1\""));
assertTrue(json.contains("Hello"));
assertTrue(json.contains("World"));
// Deserialize
Task deserialized = JsonUtil.fromJson(json, Task.class);
// Verify artifacts are preserved
assertNotNull(deserialized.artifacts());
assertEquals(1, deserialized.artifacts().size());
assertEquals("artifact-1", deserialized.artifacts().get(0).artifactId());
assertEquals(2, deserialized.artifacts().get(0).parts().size());
}
@Test
void testTaskWithHistory() throws JsonProcessingException {
Message message = Message.builder()
.role(Message.Role.ROLE_USER)
.parts(List.of(new TextPart("Test message")))
.build();
Task task = Task.builder()
.id("task-123")
.contextId("context-456")
.status(new TaskStatus(TaskState.TASK_STATE_WORKING))
.history(List.of(message))
.build();
// Serialize
String json = JsonUtil.toJson(task);
// Verify JSON contains history data
assertTrue(json.contains("\"role\":\"ROLE_USER\""));
assertTrue(json.contains("Test message"));
// Deserialize
Task deserialized = JsonUtil.fromJson(json, Task.class);
// Verify history is preserved
assertNotNull(deserialized.history());
assertEquals(1, deserialized.history().size());
assertEquals(Message.Role.ROLE_USER, deserialized.history().get(0).role());
assertEquals(1, deserialized.history().get(0).parts().size());
}
@Test
void testTaskWithAllFields() throws JsonProcessingException {
OffsetDateTime timestamp = OffsetDateTime.now();
Task task = Task.builder()
.id("task-123")
.contextId("context-789")
.status(new TaskStatus(TaskState.TASK_STATE_WORKING, null, timestamp))
.history(List.of(
Message.builder()
.role(Message.Role.ROLE_USER)
.parts(List.of(new TextPart("User message")))
.build(),
Message.builder()
.role(Message.Role.ROLE_AGENT)
.parts(List.of(new TextPart("Agent response")))
.build()
))
.artifacts(List.of(
Artifact.builder()
.artifactId("artifact-1")
.parts(List.of(new TextPart("Artifact content")))
.build()
))
.metadata(Map.of("key1", "value1", "key2", 42))
.build();
// Serialize
String json = JsonUtil.toJson(task);
// Deserialize
Task deserialized = JsonUtil.fromJson(json, Task.class);
// Verify all fields are preserved
assertEquals(task.id(), deserialized.id());
assertEquals(task.contextId(), deserialized.contextId());
assertEquals(task.status().state(), deserialized.status().state());
assertEquals(task.status().timestamp(), deserialized.status().timestamp());
assertEquals(task.history().size(), deserialized.history().size());
assertEquals(task.artifacts().size(), deserialized.artifacts().size());
assertNotNull(deserialized.metadata());
assertEquals("value1", deserialized.metadata().get("key1"));
}
@Test
void testTaskWithDifferentStates() throws JsonProcessingException {
for (TaskState state : TaskState.values()) {
Task task = Task.builder()
.id("task-" + state)
.contextId("context-123")
.status(new TaskStatus(state))
.build();
// Serialize
String json = JsonUtil.toJson(task);
// Verify state is serialized correctly
assertTrue(json.contains("\"state\":\"" + state + "\""));
// Deserialize
Task deserialized = JsonUtil.fromJson(json, Task.class);
// Verify state is preserved
assertEquals(state, deserialized.status().state());
}
}
@Test
void testTaskWithNullOptionalFields() throws JsonProcessingException {
Task task = Task.builder()
.id("task-123")
.contextId("context-456")
.status(new TaskStatus(TaskState.TASK_STATE_SUBMITTED))
// artifacts, history, metadata not set
.build();
// Serialize
String json = JsonUtil.toJson(task);
// Deserialize
Task deserialized = JsonUtil.fromJson(json, Task.class);
// Verify required fields are present
assertEquals("task-123", deserialized.id());
assertEquals("context-456", deserialized.contextId());
assertEquals(TaskState.TASK_STATE_SUBMITTED, deserialized.status().state());
// Verify optional lists default to empty
assertNotNull(deserialized.artifacts());
assertEquals(0, deserialized.artifacts().size());
assertNotNull(deserialized.history());
assertEquals(0, deserialized.history().size());
}
@Test
void testTaskWithFilePartBytes() throws JsonProcessingException {
FilePart filePart = new FilePart(new FileWithBytes("application/pdf", "document.pdf", "base64data"));
Artifact artifact = Artifact.builder()
.artifactId("file-artifact")
.parts(List.of(filePart))
.build();
Task task = Task.builder()
.id("task-123")
.contextId("context-456")
.status(new TaskStatus(TaskState.TASK_STATE_COMPLETED))
.artifacts(List.of(artifact))
.build();
// Serialize
String json = JsonUtil.toJson(task);
// Verify JSON contains file part data in flat format (raw/filename/mediaType, not "file" wrapper)
assertTrue(json.contains("\"raw\""));
assertFalse(json.contains("\"kind\""));
assertTrue(json.contains("document.pdf"));
assertTrue(json.contains("application/pdf"));
// Deserialize
Task deserialized = JsonUtil.fromJson(json, Task.class);
// Verify file part is preserved
Part<?> part = deserialized.artifacts().get(0).parts().get(0);
assertTrue(part instanceof FilePart);
FilePart deserializedFilePart = (FilePart) part;
assertTrue(deserializedFilePart.file() instanceof FileWithBytes);
FileWithBytes fileWithBytes = (FileWithBytes) deserializedFilePart.file();
assertEquals("document.pdf", fileWithBytes.name());
assertEquals("application/pdf", fileWithBytes.mimeType());
}
@Test
void testTaskWithFilePartUri() throws JsonProcessingException {
FilePart filePart = new FilePart(new FileWithUri("image/png", "photo.png", "https://example.com/photo.png"));
Artifact artifact = Artifact.builder()
.artifactId("uri-artifact")
.parts(List.of(filePart))
.build();
Task task = Task.builder()
.id("task-123")
.contextId("context-456")
.status(new TaskStatus(TaskState.TASK_STATE_COMPLETED))
.artifacts(List.of(artifact))
.build();
// Serialize
String json = JsonUtil.toJson(task);
// Verify JSON contains URI
assertTrue(json.contains("https://example.com/photo.png"));
assertFalse(json.contains("\"kind\"")); // Removed in spec 1.0
// Deserialize
Task deserialized = JsonUtil.fromJson(json, Task.class);
// Verify file part URI is preserved
Part<?> part = deserialized.artifacts().get(0).parts().get(0);
assertTrue(part instanceof FilePart);
FilePart deserializedFilePart = (FilePart) part;
assertTrue(deserializedFilePart.file() instanceof FileWithUri);
FileWithUri fileWithUri = (FileWithUri) deserializedFilePart.file();
assertEquals("https://example.com/photo.png", fileWithUri.uri());
}
@Test
void testTaskWithDataPart() throws JsonProcessingException {
DataPart dataPart = new DataPart(Map.of("temperature", 22.5, "humidity", 65));
Artifact artifact = Artifact.builder()
.artifactId("data-artifact")
.parts(List.of(dataPart))
.build();
Task task = Task.builder()
.id("task-123")
.contextId("context-456")
.status(new TaskStatus(TaskState.TASK_STATE_COMPLETED))
.artifacts(List.of(artifact))
.build();
// Serialize
String json = JsonUtil.toJson(task);
// Verify JSON contains data part (v1.0 format uses member name "data", not "kind")
assertTrue(json.contains("\"data\""));
assertFalse(json.contains("\"kind\""));
assertTrue(json.contains("temperature"));
// Deserialize
Task deserialized = JsonUtil.fromJson(json, Task.class);
// Verify data part is preserved
Part<?> part = deserialized.artifacts().get(0).parts().get(0);
assertTrue(part instanceof DataPart);
DataPart deserializedDataPart = (DataPart) part;
assertNotNull(deserializedDataPart.data());
}
@Test
void testTaskRoundTrip() throws JsonProcessingException {
// Create a comprehensive task with all part types
OffsetDateTime timestamp = OffsetDateTime.now();
Task original = Task.builder()
.id("task-123")
.contextId("context-789")
.status(new TaskStatus(TaskState.TASK_STATE_WORKING, null, timestamp))
.history(List.of(
Message.builder()
.role(Message.Role.ROLE_USER)
.parts(List.of(
new TextPart("Text"),
new FilePart(new FileWithBytes("text/plain", "file.txt", "data")),
new DataPart(Map.of("key", "value"))
))
.build()
))
.artifacts(List.of(
Artifact.builder()
.artifactId("artifact-1")
.parts(List.of(new TextPart("Content")))
.build()
))
.metadata(Map.of("meta1", "value1"))
.build();
// Serialize to JSON
String json = JsonUtil.toJson(original);
// Deserialize back to Task
Task deserialized = JsonUtil.fromJson(json, Task.class);
// Serialize again
String json2 = JsonUtil.toJson(deserialized);
// Deserialize again
Task deserialized2 = JsonUtil.fromJson(json2, Task.class);
// Verify multiple round-trips produce identical results
assertEquals(deserialized.id(), deserialized2.id());
assertEquals(deserialized.contextId(), deserialized2.contextId());
assertEquals(deserialized.status().state(), deserialized2.status().state());
assertEquals(deserialized.history().size(), deserialized2.history().size());
assertEquals(deserialized.artifacts().size(), deserialized2.artifacts().size());
}
@Test
void testTaskStatusWithMessage() throws JsonProcessingException {
Message statusMessage = Message.builder()
.role(Message.Role.ROLE_AGENT)
.parts(List.of(new TextPart("Processing complete")))
.build();
Task task = Task.builder()
.id("task-123")
.contextId("context-456")
.status(new TaskStatus(TaskState.TASK_STATE_COMPLETED, statusMessage, null))
.build();
// Serialize
String json = JsonUtil.toJson(task);
// Verify JSON contains status message
assertTrue(json.contains("\"state\":\"TASK_STATE_COMPLETED\""));
assertTrue(json.contains("Processing complete"));
// Deserialize
Task deserialized = JsonUtil.fromJson(json, Task.class);
// Verify status message is preserved
assertEquals(TaskState.TASK_STATE_COMPLETED, deserialized.status().state());
assertNotNull(deserialized.status().message());
assertEquals(Message.Role.ROLE_AGENT, deserialized.status().message().role());
assertTrue(deserialized.status().message().parts().get(0) instanceof TextPart);
}
@Test
void testDeserializeTaskFromJson() throws JsonProcessingException {
String json = """
{
"id": "task-123",
"contextId": "context-456",
"status": {
"state": "TASK_STATE_SUBMITTED"
}
}
""";
Task task = JsonUtil.fromJson(json, Task.class);
assertEquals("task-123", task.id());
assertEquals("context-456", task.contextId());
assertEquals(TaskState.TASK_STATE_SUBMITTED, task.status().state());
assertNull(task.status().message());
// TaskStatus automatically sets timestamp to current time if not provided
assertNotNull(task.status().timestamp());
}
@Test
void testDeserializeTaskWithArtifactsFromJson() throws JsonProcessingException {
String json = """
{
"id": "task-123",
"contextId": "context-456",
"status": {
"state": "TASK_STATE_COMPLETED"
},
"artifacts": [
{
"artifactId": "artifact-1",
"name": "Result",
"parts": [
{
"text": "Hello World"
}
]
}
]
}
""";
Task task = JsonUtil.fromJson(json, Task.class);
assertEquals("task-123", task.id());
assertEquals(TaskState.TASK_STATE_COMPLETED, task.status().state());
assertEquals(1, task.artifacts().size());
assertEquals("artifact-1", task.artifacts().get(0).artifactId());
assertEquals("Result", task.artifacts().get(0).name());
assertEquals(1, task.artifacts().get(0).parts().size());
assertTrue(task.artifacts().get(0).parts().get(0) instanceof TextPart);
assertEquals("Hello World", ((TextPart) task.artifacts().get(0).parts().get(0)).text());
}
@Test
void testDeserializeTaskWithFilePartBytesFromJson() throws JsonProcessingException {
String json = """
{
"id": "task-123",
"contextId": "context-456",
"status": {
"state": "TASK_STATE_COMPLETED"
},
"artifacts": [
{
"artifactId": "file-artifact",
"parts": [
{
"raw": "base64encodeddata",
"filename": "document.pdf",
"mediaType": "application/pdf"
}
]
}
]
}
""";
Task task = JsonUtil.fromJson(json, Task.class);
assertEquals("task-123", task.id());
assertEquals(1, task.artifacts().size());
Part<?> part = task.artifacts().get(0).parts().get(0);
assertTrue(part instanceof FilePart);
FilePart filePart = (FilePart) part;
assertTrue(filePart.file() instanceof FileWithBytes);
FileWithBytes fileWithBytes = (FileWithBytes) filePart.file();
assertEquals("application/pdf", fileWithBytes.mimeType());
assertEquals("document.pdf", fileWithBytes.name());
assertEquals("base64encodeddata", fileWithBytes.bytes());
}
@Test
void testDeserializeTaskWithFilePartUriFromJson() throws JsonProcessingException {
String json = """
{
"id": "task-123",
"contextId": "context-456",
"status": {
"state": "TASK_STATE_COMPLETED"
},
"artifacts": [
{
"artifactId": "uri-artifact",
"parts": [
{
"url": "https://example.com/photo.png",
"filename": "photo.png",
"mediaType": "image/png"
}
]
}
]
}
""";
Task task = JsonUtil.fromJson(json, Task.class);
assertEquals("task-123", task.id());
Part<?> part = task.artifacts().get(0).parts().get(0);
assertTrue(part instanceof FilePart);
FilePart filePart = (FilePart) part;
assertTrue(filePart.file() instanceof FileWithUri);
FileWithUri fileWithUri = (FileWithUri) filePart.file();
assertEquals("image/png", fileWithUri.mimeType());
assertEquals("photo.png", fileWithUri.name());
assertEquals("https://example.com/photo.png", fileWithUri.uri());
}
@Test
void testDeserializeTaskWithDataPartFromJson() throws JsonProcessingException {
String json = """
{
"id": "task-123",
"contextId": "context-456",
"status": {
"state": "TASK_STATE_COMPLETED"
},
"artifacts": [
{
"artifactId": "data-artifact",
"parts": [
{
"data": {
"temperature": 22.5,
"humidity": 65
}
}
]
}
]
}
""";
Task task = JsonUtil.fromJson(json, Task.class);
assertEquals("task-123", task.id());
Part<?> part = task.artifacts().get(0).parts().get(0);
assertTrue(part instanceof DataPart);
DataPart dataPart = (DataPart) part;
assertNotNull(dataPart.data());
}
@Test
void testDeserializeTaskWithHistoryFromJson() throws JsonProcessingException {
String json = """
{
"id": "task-123",
"contextId": "context-456",
"status": {
"state": "TASK_STATE_WORKING"
},
"history": [
{
"role": "ROLE_USER",
"parts": [
{
"text": "User message"
}
],
"messageId": "msg-1"
},
{
"role": "ROLE_AGENT",
"parts": [
{
"text": "Agent response"
}
],
"messageId": "msg-2"
}
]
}
""";
Task task = JsonUtil.fromJson(json, Task.class);
assertEquals("task-123", task.id());
assertEquals(2, task.history().size());
assertEquals(Message.Role.ROLE_USER, task.history().get(0).role());
assertEquals(Message.Role.ROLE_AGENT, task.history().get(1).role());
assertTrue(task.history().get(0).parts().get(0) instanceof TextPart);
assertEquals("User message", ((TextPart) task.history().get(0).parts().get(0)).text());
}
@Test
void testDeserializeTaskWithTimestampFromJson() throws JsonProcessingException {
String json = """
{
"id": "task-123",
"contextId": "context-456",
"status": {
"state": "TASK_STATE_WORKING",
"timestamp": "2023-10-01T12:00:00.234-05:00"
}
}
""";
Task task = JsonUtil.fromJson(json, Task.class);
assertEquals("task-123", task.id());
assertEquals(TaskState.TASK_STATE_WORKING, task.status().state());
assertNotNull(task.status().timestamp());
assertEquals("2023-10-01T12:00:00.234-05:00", task.status().timestamp().toString());
}
@Test
void testDeserializeTaskWithMetadataFromJson() throws JsonProcessingException {
String json = """
{
"id": "task-123",
"contextId": "context-456",
"status": {
"state": "TASK_STATE_COMPLETED"
},
"metadata": {
"key1": "value1",
"key2": 42
}
}
""";
Task task = JsonUtil.fromJson(json, Task.class);
assertEquals("task-123", task.id());
assertNotNull(task.metadata());
assertEquals("value1", task.metadata().get("key1"));
}
@Test
void testTaskWithMixedPartTypes() throws JsonProcessingException {
Artifact artifact = Artifact.builder()
.artifactId("mixed-artifact")
.parts(List.of(
new TextPart("Text content"),
new FilePart(new FileWithBytes("application/json", "data.json", "{}")),
new DataPart(Map.of("result", 42)),
new FilePart(new FileWithUri("image/png", "image.png", "https://example.com/img.png"))
))
.build();
Task task = Task.builder()
.id("task-123")
.contextId("context-456")
.status(new TaskStatus(TaskState.TASK_STATE_COMPLETED))
.artifacts(List.of(artifact))
.build();
// Serialize
String json = JsonUtil.toJson(task);
// Deserialize
Task deserialized = JsonUtil.fromJson(json, Task.class);
// Verify all part types are preserved
List<Part<?>> parts = deserialized.artifacts().get(0).parts();
assertEquals(4, parts.size());
assertTrue(parts.get(0) instanceof TextPart);
assertTrue(parts.get(1) instanceof FilePart);
assertTrue(parts.get(2) instanceof DataPart);
assertTrue(parts.get(3) instanceof FilePart);
}
// ========== FileContentTypeAdapter tests ==========
@TempDir
Path tempDir;
@Test
void testFileWithBytesSerializationDoesNotLeakInternalFields() throws Exception {
FileWithBytes fwb = new FileWithBytes("application/pdf", "doc.pdf", "base64data");
String json = JsonUtil.toJson(fwb);
// Must contain the three protocol fields
assertTrue(json.contains("\"mimeType\""), "missing mimeType: " + json);
assertTrue(json.contains("\"name\""), "missing name: " + json);
assertTrue(json.contains("\"bytes\""), "missing bytes: " + json);
// Must NOT contain internal implementation fields
assertFalse(json.contains("\"source\""), "internal source field leaked: " + json);
assertFalse(json.contains("\"cachedBytes\""), "internal cachedBytes field leaked: " + json);
}
@Test
void testFileWithBytesRoundTripViaFileContentTypeAdapter() throws Exception {
FileWithBytes original = new FileWithBytes("image/png", "photo.png", "abc123");
String json = JsonUtil.toJson(original);
FileContent deserialized = JsonUtil.fromJson(json, FileContent.class);
assertInstanceOf(FileWithBytes.class, deserialized);
FileWithBytes result = (FileWithBytes) deserialized;
assertEquals("image/png", result.mimeType());
assertEquals("photo.png", result.name());
assertEquals("abc123", result.bytes());
}
@Test
void testPathBackedFileWithBytesDoesNotLeakFilePath() throws Exception {
byte[] content = "hello".getBytes();
Path file = tempDir.resolve("secret.txt");
Files.write(file, content);
FileWithBytes fwb = new FileWithBytes("text/plain", file);
String json = JsonUtil.toJson(fwb);
// File path must not appear in the serialized JSON
assertFalse(json.contains(file.toString()), "file path leaked in JSON: " + json);
assertFalse(json.contains(tempDir.toString()), "temp dir path leaked in JSON: " + json);
// Must contain the three protocol fields, not internal implementation fields
assertTrue(json.contains("\"bytes\""), "missing bytes field: " + json);
assertFalse(json.contains("\"source\""), "internal source field leaked: " + json);
}
@Test
void testPathBackedFileWithBytesRoundTrip() throws Exception {
byte[] content = "round-trip".getBytes();
Path file = tempDir.resolve("data.bin");
Files.write(file, content);
FileWithBytes original = new FileWithBytes("application/octet-stream", file);
String json = JsonUtil.toJson(original);
FileContent deserialized = JsonUtil.fromJson(json, FileContent.class);
assertInstanceOf(FileWithBytes.class, deserialized);
FileWithBytes result = (FileWithBytes) deserialized;
assertEquals("application/octet-stream", result.mimeType());
assertEquals("data.bin", result.name());
assertEquals(Base64.getEncoder().encodeToString(content), result.bytes());
}
}