This is an automated email from the ASF dual-hosted git repository.

sijie pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/pulsar.git


The following commit(s) were added to refs/heads/master by this push:
     new 17f71d3  Make tests more stable by using JSONAssert equals (#6435)
17f71d3 is described below

commit 17f71d3f73fe1bcb6723c8b0718d9a6e05574146
Author: Sijie Guo <si...@apache.org>
AuthorDate: Wed Mar 4 17:34:41 2020 -0800

    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 [...]
    
    
    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`.
---
 .../apache/pulsar/client/impl/schema/JSONSchemaTest.java    | 13 +++++++++----
 .../pulsar/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 c303ce9..d17bbf0 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.Foo;
 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 @@ import static org.testng.Assert.assertEquals;
 @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<Foo> jsonSchema = 
JSONSchema.of(SchemaDefinition.<Foo>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 class JSONSchemaTest {
     }
 
     @Test
-    public void testAllowNullSchema() {
+    public void testAllowNullSchema() throws JSONException {
         JSONSchema<Foo> jsonSchema = 
JSONSchema.of(SchemaDefinition.<Foo>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 99a1724..994f013 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.KeyValue;
 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 class KeyValueSchemaInfoTest {
     }
 
     @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);
     }
 
 }

Reply via email to