|
| 1 | +package org.a2aproject.sdk.jsonrpc.common.json; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertInstanceOf; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 6 | + |
| 7 | +import java.util.Map; |
| 8 | + |
| 9 | +import org.a2aproject.sdk.spec.DataPart; |
| 10 | +import org.a2aproject.sdk.spec.FilePart; |
| 11 | +import org.a2aproject.sdk.spec.FileWithBytes; |
| 12 | +import org.a2aproject.sdk.spec.FileWithUri; |
| 13 | +import org.a2aproject.sdk.spec.Part; |
| 14 | +import org.a2aproject.sdk.spec.TextPart; |
| 15 | +import org.junit.jupiter.api.Test; |
| 16 | +import org.junit.jupiter.params.ParameterizedTest; |
| 17 | +import org.junit.jupiter.params.provider.ValueSource; |
| 18 | + |
| 19 | +/** |
| 20 | + * Decoding of the flat Part oneOf when a producer emits more than one content key. |
| 21 | + */ |
| 22 | +public class PartSerializationTest { |
| 23 | + |
| 24 | + @Test |
| 25 | + void testPopulatedDataWinsOverEmptyTextPlaceholder() throws JsonProcessingException { |
| 26 | + String json = """ |
| 27 | + {"text": "", "data": {"answer": "42"}} |
| 28 | + """; |
| 29 | + Part<?> part = JsonUtil.fromJson(json, Part.class); |
| 30 | + assertInstanceOf(DataPart.class, part); |
| 31 | + @SuppressWarnings("unchecked") |
| 32 | + Map<String, Object> data = (Map<String, Object>) ((DataPart) part).data(); |
| 33 | + assertEquals("42", data.get("answer")); |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + void testDataPartDecodesRegardlessOfContentKeyOrder() throws JsonProcessingException { |
| 38 | + String json = """ |
| 39 | + {"data": {"answer": "42"}, "text": ""} |
| 40 | + """; |
| 41 | + Part<?> part = JsonUtil.fromJson(json, Part.class); |
| 42 | + assertInstanceOf(DataPart.class, part); |
| 43 | + @SuppressWarnings("unchecked") |
| 44 | + Map<String, Object> data = (Map<String, Object>) ((DataPart) part).data(); |
| 45 | + assertEquals("42", data.get("answer")); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + void testPopulatedDataWinsOverAllEmptyPlaceholders() throws JsonProcessingException { |
| 50 | + String json = """ |
| 51 | + {"text": "", "raw": "", "url": "", "data": {"answer": "42"}} |
| 52 | + """; |
| 53 | + Part<?> part = JsonUtil.fromJson(json, Part.class); |
| 54 | + assertInstanceOf(DataPart.class, part); |
| 55 | + @SuppressWarnings("unchecked") |
| 56 | + Map<String, Object> data = (Map<String, Object>) ((DataPart) part).data(); |
| 57 | + assertEquals("42", data.get("answer")); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + void testPopulatedUrlWinsOverEmptyTextPlaceholder() throws JsonProcessingException { |
| 62 | + String json = """ |
| 63 | + {"text": "", "url": "https://example.org/report.pdf", "mediaType": "application/pdf"} |
| 64 | + """; |
| 65 | + Part<?> part = JsonUtil.fromJson(json, Part.class); |
| 66 | + assertInstanceOf(FilePart.class, part); |
| 67 | + FileWithUri file = (FileWithUri) ((FilePart) part).file(); |
| 68 | + assertEquals("https://example.org/report.pdf", file.uri()); |
| 69 | + assertEquals("application/pdf", file.mimeType()); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + void testPopulatedRawWinsOverEmptyTextPlaceholder() throws JsonProcessingException { |
| 74 | + String json = """ |
| 75 | + {"text": "", "raw": "abc12w==", "filename": "diagram.png", "mediaType": "image/png"} |
| 76 | + """; |
| 77 | + Part<?> part = JsonUtil.fromJson(json, Part.class); |
| 78 | + assertInstanceOf(FilePart.class, part); |
| 79 | + FileWithBytes file = (FileWithBytes) ((FilePart) part).file(); |
| 80 | + assertEquals("abc12w==", file.bytes()); |
| 81 | + assertEquals("diagram.png", file.name()); |
| 82 | + assertEquals("image/png", file.mimeType()); |
| 83 | + } |
| 84 | + |
| 85 | + /** Emptiness is decided on the JSON string only, so a falsy data payload still outranks a placeholder. */ |
| 86 | + @ParameterizedTest |
| 87 | + @ValueSource(strings = {"0", "false", "{}", "[]"}) |
| 88 | + void testFalsyDataPayloadWinsOverEmptyTextPlaceholder(String payload) throws JsonProcessingException { |
| 89 | + Part<?> part = JsonUtil.fromJson("{\"text\": \"\", \"data\": " + payload + "}", Part.class); |
| 90 | + assertInstanceOf(DataPart.class, part); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + void testJsonNullContentKeyIsSkipped() throws JsonProcessingException { |
| 95 | + String json = """ |
| 96 | + {"text": "hello", "data": null} |
| 97 | + """; |
| 98 | + Part<?> part = JsonUtil.fromJson(json, Part.class); |
| 99 | + assertInstanceOf(TextPart.class, part); |
| 100 | + assertEquals("hello", ((TextPart) part).text()); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + void testPopulatedDataWinsOverJsonNullText() throws JsonProcessingException { |
| 105 | + String json = """ |
| 106 | + {"text": null, "data": {"answer": "42"}} |
| 107 | + """; |
| 108 | + Part<?> part = JsonUtil.fromJson(json, Part.class); |
| 109 | + assertInstanceOf(DataPart.class, part); |
| 110 | + @SuppressWarnings("unchecked") |
| 111 | + Map<String, Object> data = (Map<String, Object>) ((DataPart) part).data(); |
| 112 | + assertEquals("42", data.get("answer")); |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + void testDeliberatelyEmptyTextPartDecodes() throws JsonProcessingException { |
| 117 | + String json = """ |
| 118 | + {"text": ""} |
| 119 | + """; |
| 120 | + Part<?> part = JsonUtil.fromJson(json, Part.class); |
| 121 | + assertInstanceOf(TextPart.class, part); |
| 122 | + assertEquals("", ((TextPart) part).text()); |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + void testEmptyTextPartRoundTrips() throws JsonProcessingException { |
| 127 | + String json = JsonUtil.toJson(new TextPart("")); |
| 128 | + Part<?> part = JsonUtil.fromJson(json, Part.class); |
| 129 | + assertInstanceOf(TextPart.class, part); |
| 130 | + assertEquals("", ((TextPart) part).text()); |
| 131 | + } |
| 132 | + |
| 133 | + @Test |
| 134 | + void testDataPartWithEmptyStringPayloadDecodes() throws JsonProcessingException { |
| 135 | + String json = """ |
| 136 | + {"data": ""} |
| 137 | + """; |
| 138 | + Part<?> part = JsonUtil.fromJson(json, Part.class); |
| 139 | + assertInstanceOf(DataPart.class, part); |
| 140 | + assertEquals("", ((DataPart) part).data()); |
| 141 | + } |
| 142 | + |
| 143 | + /** Nothing distinguishes the intended content when every key is an empty string, so order decides. */ |
| 144 | + @Test |
| 145 | + void testAllEmptyContentKeysFallBackToTheFirst() throws JsonProcessingException { |
| 146 | + String json = """ |
| 147 | + {"text": "", "data": ""} |
| 148 | + """; |
| 149 | + Part<?> part = JsonUtil.fromJson(json, Part.class); |
| 150 | + assertInstanceOf(TextPart.class, part); |
| 151 | + assertEquals("", ((TextPart) part).text()); |
| 152 | + } |
| 153 | + |
| 154 | + @Test |
| 155 | + void testNonStringContentKeyIsRejected() { |
| 156 | + String json = """ |
| 157 | + {"text": "", "raw": {"nested": 1}} |
| 158 | + """; |
| 159 | + assertThrows(JsonProcessingException.class, () -> JsonUtil.fromJson(json, Part.class)); |
| 160 | + } |
| 161 | +} |
0 commit comments