2020
2121import static org .testng .Assert .assertEquals ;
2222import static org .testng .Assert .assertNotNull ;
23+ import com .fasterxml .jackson .databind .JsonNode ;
24+ import com .fasterxml .jackson .databind .ObjectMapper ;
25+ import com .fasterxml .jackson .databind .node .ObjectNode ;
26+ import com .google .protobuf .DescriptorProtos ;
2327import com .google .protobuf .Descriptors ;
28+ import com .google .protobuf .util .JsonFormat ;
2429import io .netty .buffer .ByteBuf ;
2530import io .netty .buffer .ByteBufAllocator ;
2631import java .nio .charset .StandardCharsets ;
32+ import java .util .Base64 ;
2733import java .util .Collections ;
2834import java .util .HashMap ;
2935import lombok .extern .slf4j .Slf4j ;
3036import org .apache .pulsar .common .schema .SchemaType ;
37+ import org .json .JSONObject ;
38+ import org .skyscreamer .jsonassert .JSONAssert ;
39+ import org .skyscreamer .jsonassert .JSONCompareMode ;
3140import org .testng .Assert ;
3241import org .testng .annotations .Test ;
3342
@@ -46,6 +55,17 @@ public class ProtobufNativeSchemaTest {
4655 + "gBQjUKJW9yZy5hcGFjaGUucHVsc2FyLmNsaWVudC5zY2hlbWEucHJvdG9CDEV4dGVybmFsVGVzdGIGcHJvdG8z\" ,\" rootMessageT"
4756 + "ypeName\" :\" proto.TestMessage\" ,\" rootFileDescriptorName\" :\" Test.proto\" }" ;
4857
58+ private static final ObjectMapper MAPPER = new ObjectMapper ();
59+
60+ private static String fdsToJson (String b64 ) throws Exception {
61+ DescriptorProtos .FileDescriptorSet fds =
62+ DescriptorProtos .FileDescriptorSet .parseFrom (Base64 .getDecoder ().decode (b64 ));
63+ return JsonFormat .printer ()
64+ .includingDefaultValueFields ()
65+ .omittingInsignificantWhitespace ()
66+ .print (fds );
67+ }
68+
4969 @ Test
5070 public void testEncodeAndDecode () {
5171 final String stringFieldValue = "StringFieldValue" ;
@@ -61,15 +81,38 @@ public void testEncodeAndDecode() {
6181 }
6282
6383 @ Test
64- public void testSchema () {
84+ public void testSchema () throws Exception {
6585 ProtobufNativeSchema <org .apache .pulsar .client .schema .proto .Test .TestMessage > protobufSchema =
6686 ProtobufNativeSchema .of (org .apache .pulsar .client .schema .proto .Test .TestMessage .class );
6787
6888 assertEquals (protobufSchema .getSchemaInfo ().getType (), SchemaType .PROTOBUF_NATIVE );
6989
7090 assertNotNull (ProtobufNativeSchemaUtils .deserialize (protobufSchema .getSchemaInfo ().getSchema ()));
71- assertEquals (new String (protobufSchema .getSchemaInfo ().getSchema (),
72- StandardCharsets .UTF_8 ), EXPECTED_SCHEMA_JSON );
91+
92+ String actualJson = new String (protobufSchema .getSchemaInfo ().getSchema (), StandardCharsets .UTF_8 );
93+ String expectedJson = EXPECTED_SCHEMA_JSON ;
94+
95+ JsonNode actualRoot = MAPPER .readTree (actualJson );
96+ JsonNode expectedRoot = MAPPER .readTree (expectedJson );
97+
98+ String fdSetField = "fileDescriptorSet" ;
99+ String actualFdsJson = fdsToJson (actualRoot .path (fdSetField ).asText ());
100+ String expectedFdsJson = fdsToJson (expectedRoot .path (fdSetField ).asText ());
101+
102+ JSONAssert .assertEquals (
103+ new JSONObject (expectedFdsJson ),
104+ new JSONObject (actualFdsJson ),
105+ JSONCompareMode .NON_EXTENSIBLE
106+ );
107+
108+ ((ObjectNode ) actualRoot ).remove (fdSetField );
109+ ((ObjectNode ) expectedRoot ).remove (fdSetField );
110+
111+ JSONAssert .assertEquals (
112+ MAPPER .writeValueAsString (expectedRoot ),
113+ MAPPER .writeValueAsString (actualRoot ),
114+ JSONCompareMode .NON_EXTENSIBLE
115+ );
73116 }
74117
75118 @ Test
0 commit comments