Skip to content

Commit

Permalink
Make tests more stable by using JSONAssert equals (#6435)
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
sijie committed Mar 5, 2020
1 parent c3292a6 commit 17f71d3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<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) {
Expand All @@ -67,13 +72,13 @@ public void testNotAllowNullSchema() {
}

@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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}

}

0 comments on commit 17f71d3

Please sign in to comment.