Skip to content

Commit 27cd151

Browse files
LucasEbylhotari
authored andcommitted
[fix][test] Fixed nondeterministic JSON ordering in multiple tests (#24821)
(cherry picked from commit 6c67e79)
1 parent f2233d6 commit 27cd151

11 files changed

Lines changed: 113 additions & 28 deletions

File tree

pulsar-common/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,13 @@
282282
<artifactId>awaitility</artifactId>
283283
<scope>test</scope>
284284
</dependency>
285+
286+
<dependency>
287+
<groupId>org.skyscreamer</groupId>
288+
<artifactId>jsonassert</artifactId>
289+
<version>${skyscreamer.version}</version>
290+
<scope>test</scope>
291+
</dependency>
285292
</dependencies>
286293

287294
<build>

pulsar-common/src/test/java/org/apache/pulsar/common/policies/data/NamespaceOwnershipStatusTest.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import com.fasterxml.jackson.databind.ObjectMapper;
2626
import java.util.Map;
2727
import org.apache.pulsar.common.util.ObjectMapperFactory;
28+
import org.skyscreamer.jsonassert.JSONAssert;
29+
import org.skyscreamer.jsonassert.JSONCompareMode;
2830
import org.testng.annotations.Test;
2931

3032
public class NamespaceOwnershipStatusTest {
@@ -55,6 +57,10 @@ public void testSerialization() throws Exception {
5557
assertTrue(nsStatus.is_active);
5658
}
5759
}
58-
assertEquals(jsonMapper.writeValueAsString(nsMap), jsonStr);
60+
JSONAssert.assertEquals(
61+
jsonMapper.writeValueAsString(nsMap),
62+
jsonStr,
63+
JSONCompareMode.STRICT
64+
);
5965
}
6066
}

pulsar-common/src/test/java/org/apache/pulsar/common/policies/impl/NamespaceIsolationPoliciesTest.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
import org.apache.pulsar.common.policies.data.NamespaceIsolationData;
4141
import org.apache.pulsar.common.policies.data.NamespaceIsolationDataImpl;
4242
import org.apache.pulsar.common.util.ObjectMapperFactory;
43+
import org.skyscreamer.jsonassert.JSONAssert;
44+
import org.skyscreamer.jsonassert.JSONCompareMode;
4345
import org.testng.annotations.Test;
4446

4547
public class NamespaceIsolationPoliciesTest {
@@ -67,7 +69,11 @@ public void testJsonSerialization() throws Exception {
6769
assertEquals(new String(secondaryBrokersJson), "[\"prod1-broker.*.use.example.com\"]");
6870

6971
byte[] outJson = jsonMapperForWriter.writeValueAsBytes(policies.getPolicies());
70-
assertEquals(new String(outJson), this.defaultJson);
72+
JSONAssert.assertEquals(
73+
new String(outJson),
74+
this.defaultJson,
75+
JSONCompareMode.STRICT
76+
);
7177

7278
Map<String, String> parameters = new HashMap<>();
7379
parameters.put("min_limit", "1");

pulsar-functions/utils/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,13 @@
123123
<scope>test</scope>
124124
</dependency>
125125

126+
<dependency>
127+
<groupId>org.skyscreamer</groupId>
128+
<artifactId>jsonassert</artifactId>
129+
<version>${skyscreamer.version}</version>
130+
<scope>test</scope>
131+
</dependency>
132+
126133
</dependencies>
127134
<build>
128135
<plugins>

pulsar-functions/utils/src/test/java/org/apache/pulsar/functions/utils/FunctionConfigUtilsTest.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@
5252
import org.apache.pulsar.functions.api.utils.IdentityFunction;
5353
import org.apache.pulsar.functions.proto.Function;
5454
import org.apache.pulsar.functions.proto.Function.FunctionDetails;
55+
import org.json.JSONException;
56+
import org.skyscreamer.jsonassert.JSONAssert;
57+
import org.skyscreamer.jsonassert.JSONCompareMode;
5558
import org.testng.annotations.Test;
5659

5760
/**
@@ -83,7 +86,7 @@ public void testAutoAckConvertFailed() {
8386
}
8487

8588
@Test
86-
public void testConvertBackFidelity() {
89+
public void testConvertBackFidelity() throws JSONException {
8790
FunctionConfig functionConfig = new FunctionConfig();
8891
functionConfig.setTenant("test-tenant");
8992
functionConfig.setNamespace("test-namespace");
@@ -121,9 +124,10 @@ public void testConvertBackFidelity() {
121124
functionConfig.setResources(Resources.getDefaultResources());
122125
// set default cleanupSubscription config
123126
functionConfig.setCleanupSubscription(true);
124-
assertEquals(
127+
JSONAssert.assertEquals(
125128
new Gson().toJson(functionConfig),
126-
new Gson().toJson(convertedConfig)
129+
new Gson().toJson(convertedConfig),
130+
JSONCompareMode.STRICT
127131
);
128132
}
129133

pulsar-functions/utils/src/test/java/org/apache/pulsar/functions/utils/SinkConfigUtilsTest.java

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@
4949
import org.apache.pulsar.functions.proto.Function;
5050
import org.apache.pulsar.io.core.Sink;
5151
import org.apache.pulsar.io.core.SinkContext;
52+
import org.json.JSONException;
53+
import org.skyscreamer.jsonassert.JSONAssert;
54+
import org.skyscreamer.jsonassert.JSONCompareMode;
5255
import org.testng.annotations.Test;
5356

5457
/**
@@ -99,7 +102,7 @@ public void testAutoAckConvertFailed() throws IOException {
99102
}
100103

101104
@Test
102-
public void testConvertBackFidelity() throws IOException {
105+
public void testConvertBackFidelity() throws IOException, JSONException {
103106
SinkConfig sinkConfig = new SinkConfig();
104107
sinkConfig.setTenant("test-tenant");
105108
sinkConfig.setNamespace("test-namespace");
@@ -143,9 +146,10 @@ public void testConvertBackFidelity() throws IOException {
143146
new SinkConfigUtils.ExtractedSinkDetails(null, null, null));
144147
assertEquals(Function.SubscriptionType.SHARED, functionDetails.getSource().getSubscriptionType());
145148
SinkConfig convertedConfig = SinkConfigUtils.convertFromDetails(functionDetails);
146-
assertEquals(
149+
JSONAssert.assertEquals(
147150
new Gson().toJson(convertedConfig),
148-
new Gson().toJson(sinkConfig)
151+
new Gson().toJson(sinkConfig),
152+
JSONCompareMode.STRICT
149153
);
150154

151155
sinkConfig.setRetainOrdering(true);
@@ -155,9 +159,11 @@ public void testConvertBackFidelity() throws IOException {
155159
new SinkConfigUtils.ExtractedSinkDetails(null, null, null));
156160
assertEquals(Function.SubscriptionType.FAILOVER, functionDetails.getSource().getSubscriptionType());
157161
convertedConfig = SinkConfigUtils.convertFromDetails(functionDetails);
158-
assertEquals(
162+
JSONAssert.assertEquals(
159163
new Gson().toJson(convertedConfig),
160-
new Gson().toJson(sinkConfig));
164+
new Gson().toJson(sinkConfig),
165+
JSONCompareMode.STRICT
166+
);
161167

162168
sinkConfig.setRetainOrdering(false);
163169
sinkConfig.setRetainKeyOrdering(true);
@@ -166,9 +172,11 @@ public void testConvertBackFidelity() throws IOException {
166172
new SinkConfigUtils.ExtractedSinkDetails(null, null, null));
167173
assertEquals(Function.SubscriptionType.KEY_SHARED, functionDetails.getSource().getSubscriptionType());
168174
convertedConfig = SinkConfigUtils.convertFromDetails(functionDetails);
169-
assertEquals(
175+
JSONAssert.assertEquals(
170176
new Gson().toJson(convertedConfig),
171-
new Gson().toJson(sinkConfig));
177+
new Gson().toJson(sinkConfig),
178+
JSONCompareMode.STRICT
179+
);
172180
}
173181

174182
@Test

pulsar-functions/utils/src/test/java/org/apache/pulsar/functions/utils/SourceConfigUtilsTest.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@
4040
import org.apache.pulsar.functions.proto.Function;
4141
import org.apache.pulsar.io.core.BatchSourceTriggerer;
4242
import org.apache.pulsar.io.core.SourceContext;
43+
import org.json.JSONException;
44+
import org.skyscreamer.jsonassert.JSONAssert;
45+
import org.skyscreamer.jsonassert.JSONCompareMode;
4346
import org.testng.annotations.Test;
4447

4548
/**
@@ -115,13 +118,14 @@ public void testMergeEqual() {
115118
}
116119

117120
@Test
118-
public void testBatchConfigMergeEqual() {
121+
public void testBatchConfigMergeEqual() throws JSONException {
119122
SourceConfig sourceConfig = createSourceConfigWithBatch();
120123
SourceConfig newSourceConfig = createSourceConfigWithBatch();
121124
SourceConfig mergedConfig = SourceConfigUtils.validateUpdate(sourceConfig, newSourceConfig);
122-
assertEquals(
125+
JSONAssert.assertEquals(
123126
new Gson().toJson(sourceConfig),
124-
new Gson().toJson(mergedConfig)
127+
new Gson().toJson(mergedConfig),
128+
JSONCompareMode.STRICT
125129
);
126130
}
127131

pulsar-io/elastic-search/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,13 @@
105105
<scope>test</scope>
106106
</dependency>
107107

108+
<dependency>
109+
<groupId>org.skyscreamer</groupId>
110+
<artifactId>jsonassert</artifactId>
111+
<version>${skyscreamer.version}</version>
112+
<scope>test</scope>
113+
</dependency>
114+
108115
</dependencies>
109116

110117
<build>

pulsar-io/elastic-search/src/test/java/org/apache/pulsar/io/elasticsearch/ElasticSearchExtractTest.java

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
import org.apache.pulsar.common.schema.KeyValueEncodingType;
3434
import org.apache.pulsar.common.schema.SchemaType;
3535
import org.apache.pulsar.functions.api.Record;
36+
import org.skyscreamer.jsonassert.JSONAssert;
37+
import org.skyscreamer.jsonassert.JSONCompareMode;
3638
import org.testng.annotations.DataProvider;
3739
import org.testng.annotations.Test;
3840

@@ -99,8 +101,12 @@ public GenericObject getValue() {
99101
"keyIgnore", "true"), null);
100102
Pair<String, String> pair = elasticSearchSink.extractIdAndDocument(genericObjectRecord);
101103
assertEquals(pair.getLeft(), "1");
102-
assertEquals(pair.getRight(), "{\"c\":\"1\",\"d\":1,\"e\":{\"a\":\"a\",\"b\":true,\"d\":1.0,"
103-
+ "\"f\":1.0,\"i\":1,\"l\":10}}");
104+
JSONAssert.assertEquals(
105+
pair.getRight(),
106+
"{\"c\":\"1\",\"d\":1,\"e\":{\"a\":\"a\",\"b\":true,\"d\":1.0,"
107+
+ "\"f\":1.0,\"i\":1,\"l\":10}}",
108+
JSONCompareMode.STRICT
109+
);
104110
elasticSearchSink.close();
105111

106112
// two fields PK
@@ -112,9 +118,20 @@ public GenericObject getValue() {
112118
"schemaEnable", "true",
113119
"keyIgnore", "true"), null);
114120
Pair<String, String> pair2 = elasticSearchSink2.extractIdAndDocument(genericObjectRecord);
115-
assertEquals(pair2.getLeft(), "[\"1\",1]");
116-
assertEquals(pair2.getRight(), "{\"c\":\"1\",\"d\":1,\"e\":{\"a\":\"a\",\"b\":true,\"d\":1.0,"
117-
+ "\"f\":1.0,\"i\":1,\"l\":10}}");
121+
122+
// NON_EXTENSIBLE is NOT extensible and does NOT have strict ordering so both
123+
// possibilities ["1",1] and [1,"1"] will pass
124+
JSONAssert.assertEquals(
125+
pair2.getLeft(),
126+
"[\"1\",1]",
127+
JSONCompareMode.NON_EXTENSIBLE
128+
);
129+
JSONAssert.assertEquals(
130+
pair2.getRight(),
131+
"{\"c\":\"1\",\"d\":1,\"e\":{\"a\":\"a\",\"b\":true,\"d\":1.0,"
132+
+ "\"f\":1.0,\"i\":1,\"l\":10}}",
133+
JSONCompareMode.STRICT
134+
);
118135
elasticSearchSink2.close();
119136

120137
// default config with null PK => indexed with auto generated _id
@@ -124,8 +141,12 @@ public GenericObject getValue() {
124141
"compatibilityMode", "ELASTICSEARCH"), null);
125142
Pair<String, String> pair3 = elasticSearchSink3.extractIdAndDocument(genericObjectRecord);
126143
assertNull(pair3.getLeft());
127-
assertEquals(pair3.getRight(), "{\"c\":\"1\",\"d\":1,\"e\":{\"a\":\"a\",\"b\":true,\"d\":1.0,"
128-
+ "\"f\":1.0,\"i\":1,\"l\":10}}");
144+
JSONAssert.assertEquals(
145+
pair3.getRight(),
146+
"{\"c\":\"1\",\"d\":1,\"e\":{\"a\":\"a\",\"b\":true,\"d\":1.0,"
147+
+ "\"f\":1.0,\"i\":1,\"l\":10}}",
148+
JSONCompareMode.STRICT
149+
);
129150
elasticSearchSink3.close();
130151

131152
// default config with null PK + null value

pulsar-io/kinesis/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,13 @@
162162
<scope>test</scope>
163163
</dependency>
164164

165+
<dependency>
166+
<groupId>org.skyscreamer</groupId>
167+
<artifactId>jsonassert</artifactId>
168+
<version>${skyscreamer.version}</version>
169+
<scope>test</scope>
170+
</dependency>
171+
165172
</dependencies>
166173

167174
<build>

0 commit comments

Comments
 (0)