Skip to content

Commit ebc185d

Browse files
committed
[fix][test] Made ProtobufSchemaTest.testParsingInfoProperty order-independent
1 parent f5547dc commit ebc185d

File tree

1 file changed

+47
-2
lines changed

1 file changed

+47
-2
lines changed

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

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,19 @@
1919
package org.apache.pulsar.client.impl.schema;
2020

2121
import com.fasterxml.jackson.core.JsonProcessingException;
22+
import com.fasterxml.jackson.core.type.TypeReference;
23+
import com.fasterxml.jackson.databind.JsonNode;
2224
import com.fasterxml.jackson.databind.ObjectMapper;
25+
import com.fasterxml.jackson.databind.node.ArrayNode;
26+
import com.fasterxml.jackson.databind.node.ObjectNode;
2327
import io.netty.buffer.ByteBuf;
2428
import io.netty.buffer.ByteBufAllocator;
29+
import java.util.ArrayList;
2530
import java.util.Collections;
31+
import java.util.Comparator;
2632
import java.util.HashMap;
33+
import java.util.List;
34+
import java.util.Map;
2735
import lombok.extern.slf4j.Slf4j;
2836
import org.apache.avro.Schema;
2937
import org.apache.pulsar.common.schema.SchemaType;
@@ -68,6 +76,38 @@ public class ProtobufSchemaTest {
6876
+ "\"externalMessage\\\",\\\"type\\\":\\\"MESSAGE\\\",\\\"label\\\":\\\"LABEL_OPTIONAL\\\",\\\"definition"
6977
+ "\\\":null}]\"}";
7078

79+
private static final ObjectMapper MAPPER = new ObjectMapper();
80+
81+
private static ObjectNode normalizeAllProps(Map<String, String> props, String specialKey)
82+
throws JsonProcessingException {
83+
ObjectNode out = MAPPER.createObjectNode();
84+
85+
props.forEach((k, v) -> {
86+
if (specialKey.equals(k)) {
87+
try {
88+
// Store the special key's stringified json data as a json array
89+
ArrayNode arr = (ArrayNode) MAPPER.readTree(v);
90+
91+
// sort deterministically by (number, name)
92+
List<JsonNode> items = new ArrayList<>();
93+
arr.forEach(items::add);
94+
items.sort(Comparator
95+
.comparing((JsonNode n) -> n.path("number").asInt())
96+
.thenComparing(n -> n.path("name").asText()));
97+
ArrayNode sorted = MAPPER.createArrayNode();
98+
items.forEach(sorted::add);
99+
out.set(k, sorted);
100+
} catch (Exception e) {
101+
// If it's not valid JSON for some reason, store as raw string
102+
out.put(k, v);
103+
}
104+
} else {
105+
out.put(k, v);
106+
}
107+
});
108+
return out;
109+
}
110+
71111
@Test
72112
public void testEncodeAndDecode() {
73113
Function.FunctionDetails functionDetails = Function.FunctionDetails.newBuilder().setName(NAME).build();
@@ -120,9 +160,14 @@ public void testParsingInfoProperty() throws JsonProcessingException {
120160
ProtobufSchema<org.apache.pulsar.client.schema.proto.Test.TestMessage> protobufSchema =
121161
ProtobufSchema.of(org.apache.pulsar.client.schema.proto.Test.TestMessage.class);
122162

123-
Assert.assertEquals(new ObjectMapper().writeValueAsString(
124-
protobufSchema.getSchemaInfo().getProperties()), EXPECTED_PARSING_INFO);
163+
Map<String, String> actualProps = protobufSchema.getSchemaInfo().getProperties();
164+
Map<String, String> expectedProps = MAPPER.readValue(
165+
EXPECTED_PARSING_INFO, new TypeReference<Map<String, String>>() {});
166+
167+
ObjectNode normActual = normalizeAllProps(actualProps, "__PARSING_INFO__");
168+
ObjectNode normExpected = normalizeAllProps(expectedProps, "__PARSING_INFO__");
125169

170+
Assert.assertEquals(normActual, normExpected);
126171
}
127172

128173
@Test

0 commit comments

Comments
 (0)