20
20
21
21
import static org .testng .Assert .assertEquals ;
22
22
import 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 ;
23
27
import com .google .protobuf .Descriptors ;
28
+ import com .google .protobuf .util .JsonFormat ;
24
29
import io .netty .buffer .ByteBuf ;
25
30
import io .netty .buffer .ByteBufAllocator ;
26
31
import java .nio .charset .StandardCharsets ;
32
+ import java .util .Base64 ;
27
33
import java .util .Collections ;
28
34
import java .util .HashMap ;
29
35
import lombok .extern .slf4j .Slf4j ;
30
36
import org .apache .pulsar .common .schema .SchemaType ;
37
+ import org .json .JSONObject ;
38
+ import org .skyscreamer .jsonassert .JSONAssert ;
39
+ import org .skyscreamer .jsonassert .JSONCompareMode ;
31
40
import org .testng .Assert ;
32
41
import org .testng .annotations .Test ;
33
42
@@ -46,6 +55,17 @@ public class ProtobufNativeSchemaTest {
46
55
+ "gBQjUKJW9yZy5hcGFjaGUucHVsc2FyLmNsaWVudC5zY2hlbWEucHJvdG9CDEV4dGVybmFsVGVzdGIGcHJvdG8z\" ,\" rootMessageT"
47
56
+ "ypeName\" :\" proto.TestMessage\" ,\" rootFileDescriptorName\" :\" Test.proto\" }" ;
48
57
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
+
49
69
@ Test
50
70
public void testEncodeAndDecode () {
51
71
final String stringFieldValue = "StringFieldValue" ;
@@ -61,15 +81,38 @@ public void testEncodeAndDecode() {
61
81
}
62
82
63
83
@ Test
64
- public void testSchema () {
84
+ public void testSchema () throws Exception {
65
85
ProtobufNativeSchema <org .apache .pulsar .client .schema .proto .Test .TestMessage > protobufSchema =
66
86
ProtobufNativeSchema .of (org .apache .pulsar .client .schema .proto .Test .TestMessage .class );
67
87
68
88
assertEquals (protobufSchema .getSchemaInfo ().getType (), SchemaType .PROTOBUF_NATIVE );
69
89
70
90
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
+ );
73
116
}
74
117
75
118
@ Test
0 commit comments