Skip to content

Commit 3146821

Browse files
committed
[fix][test] Made ProtobufNativeSchemaTest.testSchema order-independent
1 parent 771ce41 commit 3146821

File tree

1 file changed

+46
-3
lines changed

1 file changed

+46
-3
lines changed

pulsar-client/src/test/java/org/apache/pulsar/client/impl/schema/ProtobufNativeSchemaTest.java

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,23 @@
2020

2121
import static org.testng.Assert.assertEquals;
2222
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;
2327
import com.google.protobuf.Descriptors;
28+
import com.google.protobuf.util.JsonFormat;
2429
import io.netty.buffer.ByteBuf;
2530
import io.netty.buffer.ByteBufAllocator;
2631
import java.nio.charset.StandardCharsets;
32+
import java.util.Base64;
2733
import java.util.Collections;
2834
import java.util.HashMap;
2935
import lombok.extern.slf4j.Slf4j;
3036
import org.apache.pulsar.common.schema.SchemaType;
37+
import org.json.JSONObject;
38+
import org.skyscreamer.jsonassert.JSONAssert;
39+
import org.skyscreamer.jsonassert.JSONCompareMode;
3140
import org.testng.Assert;
3241
import 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

Comments
 (0)