From 17f71d3f73fe1bcb6723c8b0718d9a6e05574146 Mon Sep 17 00:00:00 2001 From: Sijie Guo Date: Wed, 4 Mar 2020 17:34:41 -0800 Subject: [PATCH] Make tests more stable by using JSONAssert equals (#6435) Similar to the change you already merged for AvroSchemaTest.java(#6247): `jsonSchema.getSchemaInfo().getSchema()` in `pulsar-client/src/test/java/org/apache/pulsar/client/impl/schema/JSONSchemaTest.java` returns a JSON object. `schemaJson` compares with hard-coded JSON String. However, the order of entries in `schemaJson` is not guaranteed. Similarly, test `testKeyValueSchemaInfoToString` in `pulsar-client/src/test/java/org/apache/pulsar/client/impl/schema/KeyValueSchemaInfoTest.java` returns a JSON object. `havePrimitiveType` compares with hard-coded JSON String, and the order of entries in `havePrimitiveType` is not guaranteed. 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 `assertJSONEqual` method and replacing `assertEquals` with it in tests `testAllowNullSchema`, `testNotAllowNullSchema` and `testKeyValueSchemaInfoToString`. --- .../pulsar/client/impl/schema/JSONSchemaTest.java | 13 +++++++++---- .../client/impl/schema/KeyValueSchemaInfoTest.java | 7 ++++--- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/pulsar-client/src/test/java/org/apache/pulsar/client/impl/schema/JSONSchemaTest.java b/pulsar-client/src/test/java/org/apache/pulsar/client/impl/schema/JSONSchemaTest.java index c303ce9293329..d17bbf0b8a0f0 100644 --- a/pulsar-client/src/test/java/org/apache/pulsar/client/impl/schema/JSONSchemaTest.java +++ b/pulsar-client/src/test/java/org/apache/pulsar/client/impl/schema/JSONSchemaTest.java @@ -33,8 +33,10 @@ import org.apache.pulsar.client.impl.schema.SchemaTestUtils.NestedBar; import org.apache.pulsar.client.impl.schema.SchemaTestUtils.NestedBarList; import org.apache.pulsar.common.schema.SchemaType; +import org.skyscreamer.jsonassert.JSONAssert; import org.testng.Assert; import org.testng.annotations.Test; +import org.json.JSONException; import static org.apache.pulsar.client.impl.schema.SchemaTestUtils.FOO_FIELDS; import static org.apache.pulsar.client.impl.schema.SchemaTestUtils.SCHEMA_JSON_NOT_ALLOW_NULL; @@ -44,13 +46,16 @@ @Slf4j public class JSONSchemaTest { + public static void assertJSONEqual(String s1, String s2) throws JSONException{ + JSONAssert.assertEquals(s1, s2, false); + } @Test - public void testNotAllowNullSchema() { + public void testNotAllowNullSchema() throws JSONException { JSONSchema jsonSchema = JSONSchema.of(SchemaDefinition.builder().withPojo(Foo.class).withAlwaysAllowNull(false).build()); Assert.assertEquals(jsonSchema.getSchemaInfo().getType(), SchemaType.JSON); Schema.Parser parser = new Schema.Parser(); String schemaJson = new String(jsonSchema.getSchemaInfo().getSchema()); - Assert.assertEquals(schemaJson, SCHEMA_JSON_NOT_ALLOW_NULL); + assertJSONEqual(schemaJson, SCHEMA_JSON_NOT_ALLOW_NULL); Schema schema = parser.parse(schemaJson); for (String fieldName : FOO_FIELDS) { @@ -67,13 +72,13 @@ public void testNotAllowNullSchema() { } @Test - public void testAllowNullSchema() { + public void testAllowNullSchema() throws JSONException { JSONSchema jsonSchema = JSONSchema.of(SchemaDefinition.builder().withPojo(Foo.class).build()); Assert.assertEquals(jsonSchema.getSchemaInfo().getType(), SchemaType.JSON); Schema.Parser parser = new Schema.Parser(); parser.setValidateDefaults(false); String schemaJson = new String(jsonSchema.getSchemaInfo().getSchema()); - Assert.assertEquals(schemaJson, SCHEMA_JSON_ALLOW_NULL); + assertJSONEqual(schemaJson, SCHEMA_JSON_ALLOW_NULL); Schema schema = parser.parse(schemaJson); for (String fieldName : FOO_FIELDS) { diff --git a/pulsar-client/src/test/java/org/apache/pulsar/client/impl/schema/KeyValueSchemaInfoTest.java b/pulsar-client/src/test/java/org/apache/pulsar/client/impl/schema/KeyValueSchemaInfoTest.java index 99a1724a83ab2..994f01386c4bf 100644 --- a/pulsar-client/src/test/java/org/apache/pulsar/client/impl/schema/KeyValueSchemaInfoTest.java +++ b/pulsar-client/src/test/java/org/apache/pulsar/client/impl/schema/KeyValueSchemaInfoTest.java @@ -38,6 +38,7 @@ import org.apache.pulsar.common.schema.KeyValueEncodingType; import org.apache.pulsar.common.schema.SchemaInfo; import org.apache.pulsar.common.schema.SchemaType; +import org.json.JSONException; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; @@ -209,18 +210,18 @@ public void testKeyValueSchemaInfoBackwardCompatibility() { } @Test - public void testKeyValueSchemaInfoToString() { + public void testKeyValueSchemaInfoToString() throws JSONException { String havePrimitiveType = DefaultImplementation .convertKeyValueSchemaInfoDataToString(KeyValueSchemaInfo .decodeKeyValueSchemaInfo(Schema.KeyValue(Schema.AVRO(Foo.class), Schema.STRING) .getSchemaInfo())); - assertEquals(havePrimitiveType, KEY_VALUE_SCHEMA_INFO_INCLUDE_PRIMITIVE); + JSONSchemaTest.assertJSONEqual(havePrimitiveType, KEY_VALUE_SCHEMA_INFO_INCLUDE_PRIMITIVE); String notHavePrimitiveType = DefaultImplementation .convertKeyValueSchemaInfoDataToString(KeyValueSchemaInfo .decodeKeyValueSchemaInfo(Schema.KeyValue(Schema.AVRO(Foo.class), Schema.AVRO(Foo.class)).getSchemaInfo())); - assertEquals(notHavePrimitiveType, KEY_VALUE_SCHEMA_INFO_NOT_INCLUDE_PRIMITIVE); + JSONSchemaTest.assertJSONEqual(notHavePrimitiveType, KEY_VALUE_SCHEMA_INFO_NOT_INCLUDE_PRIMITIVE); } }