Skip to content

Commit

Permalink
Make tests more stable by using JSONAssert equals (#6247)
Browse files Browse the repository at this point in the history
`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 7dd52b8)
  • Loading branch information
testfixer authored and tuteng committed Apr 5, 2020
1 parent ba60a3f commit 274b67e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ flexible messaging model and an intuitive client API.</description>
<powermock.version>2.0.2</powermock.version>
<javassist.version>3.25.0-GA</javassist.version>
<failsafe.version>2.3.1</failsafe.version>
<skyscreamer.version>1.5.0</skyscreamer.version>

<!-- Plugin dependencies -->
<protobuf-maven-plugin.version>0.6.1</protobuf-maven-plugin.version>
Expand Down
7 changes: 7 additions & 0 deletions pulsar-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,13 @@
<version>${project.parent.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.skyscreamer</groupId>
<artifactId>jsonassert</artifactId>
<version>${skyscreamer.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

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

Expand Down Expand Up @@ -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<Foo> avroSchema = AvroSchema.of(SchemaDefinition.<Foo>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) {
Expand All @@ -165,13 +171,13 @@ public void testNotAllowNullSchema() {
}

@Test
public void testAllowNullSchema() {
public void testAllowNullSchema() throws JSONException {
AvroSchema<Foo> avroSchema = AvroSchema.of(SchemaDefinition.<Foo>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) {
Expand Down

0 comments on commit 274b67e

Please sign in to comment.