From faf4917369bc1fb833727fcc53d1fe4423189d7c Mon Sep 17 00:00:00 2001 From: testfixer <60407054+testfixer@users.noreply.github.com> Date: Thu, 13 Feb 2020 17:14:35 -0600 Subject: [PATCH] Make tests more stable by using JSONAssert equals (#6247) `avroSchema.getSchemaInfo().getSchema()` in `src/test/java/org/apache/pulsar/client/impl/schema/AvroSchemaTest.java` returns a JSON object. `schemaJson` compares with hard-coded JSON String. However, the order of entries in `schemaJson` is not guaranteed so the json results can contain entries in any order. This PR proposes to use JSONAssert and modify the corresponding json test assertions so that the test is more stable. ### Motivation Using JSONAssert and modifying the corresponding json test assertions so that the test is more stable. ### Modifications Adding `assertJSONEquals` method and replacing `assertEquals` with it in tests `testNotAllowNullSchema` and `testAllowNullSchema`. (cherry picked from commit 7dd52b8ae6ed5f7b392d6a921502c4c34509e52d) --- pom.xml | 1 + pulsar-client/pom.xml | 7 +++++++ .../pulsar/client/impl/schema/AvroSchemaTest.java | 14 ++++++++++---- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/pom.xml b/pom.xml index 0ad6e693bc5b1..e06c25891dfdf 100644 --- a/pom.xml +++ b/pom.xml @@ -215,6 +215,7 @@ flexible messaging model and an intuitive client API. 2.0.2 3.25.0-GA 2.3.1 + 1.5.0 0.6.1 diff --git a/pulsar-client/pom.xml b/pulsar-client/pom.xml index e2f086ca53bdb..dfd0c3c8b6033 100644 --- a/pulsar-client/pom.xml +++ b/pulsar-client/pom.xml @@ -151,6 +151,13 @@ ${project.parent.version} test + + + org.skyscreamer + jsonassert + ${skyscreamer.version} + test + diff --git a/pulsar-client/src/test/java/org/apache/pulsar/client/impl/schema/AvroSchemaTest.java b/pulsar-client/src/test/java/org/apache/pulsar/client/impl/schema/AvroSchemaTest.java index caeb8ac8393a6..0aa45aff4356b 100644 --- a/pulsar-client/src/test/java/org/apache/pulsar/client/impl/schema/AvroSchemaTest.java +++ b/pulsar-client/src/test/java/org/apache/pulsar/client/impl/schema/AvroSchemaTest.java @@ -53,6 +53,8 @@ import org.apache.pulsar.client.impl.schema.SchemaTestUtils.Foo; import org.apache.pulsar.common.schema.SchemaInfo; import org.apache.pulsar.common.schema.SchemaType; +import org.json.JSONException; +import org.skyscreamer.jsonassert.JSONAssert; import org.testng.Assert; import org.testng.annotations.Test; @@ -142,13 +144,17 @@ public void testSchemaDefinition() throws SchemaValidationException { } } + public void assertJSONEquals(String s1, String s2) throws JSONException { + JSONAssert.assertEquals(s1, s2, false); + } + @Test - public void testNotAllowNullSchema() { + public void testNotAllowNullSchema() throws JSONException { AvroSchema avroSchema = AvroSchema.of(SchemaDefinition.builder().withPojo(Foo.class).withAlwaysAllowNull(false).build()); assertEquals(avroSchema.getSchemaInfo().getType(), SchemaType.AVRO); Schema.Parser parser = new Schema.Parser(); String schemaJson = new String(avroSchema.getSchemaInfo().getSchema()); - assertEquals(schemaJson, SCHEMA_AVRO_NOT_ALLOW_NULL); + assertJSONEquals(schemaJson, SCHEMA_AVRO_NOT_ALLOW_NULL); Schema schema = parser.parse(schemaJson); for (String fieldName : FOO_FIELDS) { @@ -165,13 +171,13 @@ public void testNotAllowNullSchema() { } @Test - public void testAllowNullSchema() { + public void testAllowNullSchema() throws JSONException { AvroSchema avroSchema = AvroSchema.of(SchemaDefinition.builder().withPojo(Foo.class).build()); assertEquals(avroSchema.getSchemaInfo().getType(), SchemaType.AVRO); Schema.Parser parser = new Schema.Parser(); parser.setValidateDefaults(false); String schemaJson = new String(avroSchema.getSchemaInfo().getSchema()); - assertEquals(schemaJson, SCHEMA_AVRO_ALLOW_NULL); + assertJSONEquals(schemaJson, SCHEMA_AVRO_ALLOW_NULL); Schema schema = parser.parse(schemaJson); for (String fieldName : FOO_FIELDS) {