|
3 | 3 |
|
4 | 4 | import static org.junit.jupiter.api.Assertions.assertEquals; |
5 | 5 | import static org.junit.jupiter.api.Assertions.assertFalse; |
| 6 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
6 | 7 |
|
7 | 8 | import com.hedera.pbj.integration.EverythingTestData; |
| 9 | +import com.hedera.pbj.runtime.OneOf; |
8 | 10 | import com.hedera.pbj.runtime.ProtoConstants; |
9 | 11 | import com.hedera.pbj.runtime.UnknownField; |
10 | 12 | import com.hedera.pbj.runtime.io.buffer.BufferedData; |
11 | 13 | import com.hedera.pbj.runtime.io.buffer.Bytes; |
12 | 14 | import com.hedera.pbj.test.proto.pbj.Everything; |
13 | 15 | import com.hedera.pbj.test.proto.pbj.MessageWithBytes; |
14 | 16 | import com.hedera.pbj.test.proto.pbj.MessageWithBytesAndString; |
| 17 | +import com.hedera.pbj.test.proto.pbj.MessageWithBytesWrapper; |
15 | 18 | import org.junit.jupiter.api.Test; |
16 | 19 | import pbj.integration.tests.pbj.integration.tests.MessageWithEverythingUnknownFields; |
17 | 20 |
|
@@ -95,4 +98,40 @@ void testEverythingRoundTrip() throws Exception { |
95 | 98 | // and ensure it's equal to what we started with: |
96 | 99 | assertEquals(EverythingTestData.EVERYTHING, everything); |
97 | 100 | } |
| 101 | + |
| 102 | + @Test |
| 103 | + public void testUnknownFieldsInInnerMessage() throws Exception { |
| 104 | + // write MessageWithBytesAndString |
| 105 | + MessageWithBytesAndString messageWithBytesAndString = new MessageWithBytesAndString(TEST_BYTES, TEST_STRING); |
| 106 | + final Bytes messageWithBytesAndStringBytes = |
| 107 | + MessageWithBytesAndString.PROTOBUF.toBytes(messageWithBytesAndString); |
| 108 | + |
| 109 | + // then read it as MessageWithBytes with unknown fields |
| 110 | + final MessageWithBytes messageWithBytes = MessageWithBytes.PROTOBUF.parse( |
| 111 | + messageWithBytesAndStringBytes.toReadableSequentialData(), false, true, 16); |
| 112 | + |
| 113 | + final MessageWithBytesWrapper messageWithBytesWrapper = new MessageWithBytesWrapper( |
| 114 | + new OneOf<>(MessageWithBytesWrapper.MessageValidOneOfType.MESSAGE_WITH_BYTES, messageWithBytes)); |
| 115 | + assertFalse( |
| 116 | + messageWithBytesWrapper.messageWithBytes().getUnknownFields().isEmpty()); |
| 117 | + assertEquals( |
| 118 | + 1, messageWithBytesWrapper.messageWithBytes().getUnknownFields().size()); |
| 119 | + |
| 120 | + // write to bytes to simulate sending over the wire |
| 121 | + final Bytes messageWithBytesWrapperBytes = MessageWithBytesWrapper.PROTOBUF.toBytes(messageWithBytesWrapper); |
| 122 | + |
| 123 | + // parse bytes back as a receiving user would and confirm unknown fields exist in inner message |
| 124 | + final MessageWithBytesWrapper parsedWrapper = MessageWithBytesWrapper.PROTOBUF.parse( |
| 125 | + messageWithBytesWrapperBytes.toReadableSequentialData(), false, true, 16); |
| 126 | + MessageWithBytes parsedBytes = parsedWrapper.messageWithBytes(); |
| 127 | + assertFalse(parsedBytes.getUnknownFields().isEmpty()); |
| 128 | + assertEquals(1, parsedBytes.getUnknownFields().size()); |
| 129 | + |
| 130 | + // now confirm that user can retrieve unknown fields when using expanded message MessageWithBytesAndString |
| 131 | + final Bytes messageWithBytesBytes = MessageWithBytes.PROTOBUF.toBytes(parsedBytes); |
| 132 | + final MessageWithBytesAndString messageWithBytesAndStringParsed = |
| 133 | + MessageWithBytesAndString.PROTOBUF.parse(messageWithBytesBytes.toReadableSequentialData()); |
| 134 | + assertTrue(messageWithBytesAndStringParsed.getUnknownFields().isEmpty()); |
| 135 | + assertEquals(messageWithBytesAndString, messageWithBytesAndStringParsed); |
| 136 | + } |
98 | 137 | } |
0 commit comments