From 61c63138d66246631b22f1570ca5d6d2981786f1 Mon Sep 17 00:00:00 2001 From: xinying7 Date: Fri, 15 Nov 2024 17:34:15 -0600 Subject: [PATCH] fixing flaky test testFormatOfBigDecimal fixing flaky test serializeDoubleProperty fix flaky test testFormatOfBigDecimal fix flaky test testExtensionObjectWithProperties fix flaky test serializeReadOnlyStringProperty fix flaky test serializeArrayModel fix flaky test deserializeArrayModel fix flaky test serializeArrayStringProperty fix flaky test deserializeArrayStringProperty fix flaky test serializeDateTimeProperty fix flaky test deserializeDateTimeProperty fix flaky test serializeObjectPropertyWithRequiredProperties fix flaky test serializeLongMapProperty fix flaky test deserializeLongMapProperty fixed flaky test deserializeIntegerProperty fixed 9 flaky tests in PropertySerializationTest.java: 'deserializeNotReadOnlyStringProperty', 'deserializeIntegerMapProperty', 'serializeFloatProperty', 'deserializeDoubleProperty', 'serializeIntegerProperty', 'serializeLongProperty', 'deserializeLongProperty', 'deserializeFloatProperty', 'serializeIntegerMapProperty' --- modules/swagger-core/pom.xml | 10 ++ .../v3/core/converting/NumericFormatTest.java | 23 ++- .../serialization/JsonSerializationTest.java | 8 +- .../serialization/ModelSerializerTest.java | 17 +- .../properties/PropertySerializationTest.java | 160 ++++++++++++------ 5 files changed, 158 insertions(+), 60 deletions(-) diff --git a/modules/swagger-core/pom.xml b/modules/swagger-core/pom.xml index 5c64e4204f..2667290482 100644 --- a/modules/swagger-core/pom.xml +++ b/modules/swagger-core/pom.xml @@ -52,6 +52,16 @@ + + org.json + json + 20230227 + + + org.skyscreamer + jsonassert + 1.5.0 + jakarta.xml.bind jakarta.xml.bind-api diff --git a/modules/swagger-core/src/test/java/io/swagger/v3/core/converting/NumericFormatTest.java b/modules/swagger-core/src/test/java/io/swagger/v3/core/converting/NumericFormatTest.java index c93dc2a7ae..4e2e22a433 100644 --- a/modules/swagger-core/src/test/java/io/swagger/v3/core/converting/NumericFormatTest.java +++ b/modules/swagger-core/src/test/java/io/swagger/v3/core/converting/NumericFormatTest.java @@ -14,6 +14,9 @@ import static io.swagger.v3.core.util.TestUtils.normalizeLineEnds; import static org.testng.Assert.assertEquals; +import org.json.JSONObject; +import org.skyscreamer.jsonassert.JSONAssert; + public class NumericFormatTest { @Test public void testFormatOfInteger() { @@ -41,8 +44,8 @@ public void testFormatOfDecimal() { final Map models = ModelConverters.getInstance().readAll(ModelWithDecimalFields.class); assertEquals(models.size(), 1); - String json = Json.pretty(models); - assertEquals(normalizeLineEnds(json), + String json1 = Json.pretty(models); + String json2 = "{\n" + " \"ModelWithDecimalFields\" : {\n" + " \"type\" : \"object\",\n" + @@ -55,7 +58,11 @@ public void testFormatOfDecimal() { " }\n" + " }\n" + " }\n" + - "}"); + "}"; + JSONObject jsonObj1 = new JSONObject(json1); + JSONObject jsonObj2 = new JSONObject(json2); + + JSONAssert.assertEquals(jsonObj1, jsonObj2, true); } @Test @@ -63,9 +70,9 @@ public void testFormatOfBigDecimal() { final Map models = ModelConverters.getInstance().readAll(ModelWithoutScientificFields.class); assertEquals(models.size(), 1); - String json = Json.pretty(models); + String json1 = Json.pretty(models); - assertEquals(normalizeLineEnds(json), + String json2 = "{\n" + " \"ModelWithoutScientificFields\" : {\n" + " \"type\" : \"object\",\n" + @@ -79,7 +86,11 @@ public void testFormatOfBigDecimal() { " }\n" + " }\n" + " }\n" + - "}"); + "}"; + JSONObject jsonObj1 = new JSONObject(json1); + JSONObject jsonObj2 = new JSONObject(json2); + + JSONAssert.assertEquals(jsonObj1, jsonObj2, true); } diff --git a/modules/swagger-core/src/test/java/io/swagger/v3/core/serialization/JsonSerializationTest.java b/modules/swagger-core/src/test/java/io/swagger/v3/core/serialization/JsonSerializationTest.java index 29163d0cca..4769e30c1f 100644 --- a/modules/swagger-core/src/test/java/io/swagger/v3/core/serialization/JsonSerializationTest.java +++ b/modules/swagger-core/src/test/java/io/swagger/v3/core/serialization/JsonSerializationTest.java @@ -17,6 +17,8 @@ import io.swagger.v3.oas.models.servers.Server; import org.testng.annotations.Test; import org.yaml.snakeyaml.LoaderOptions; +import org.json.JSONObject; +import org.skyscreamer.jsonassert.JSONAssert; import java.util.HashMap; import java.util.Map; @@ -68,7 +70,11 @@ public void testExtensionObjectWithProperties() throws Exception { swagger.addExtension("x-extension-with-properties", extensionObjectProps); String swaggerJson = Json.mapper().writeValueAsString(swagger); - assertEquals(swaggerJson, "{\"openapi\":\"3.0.1\",\"x-extension-with-properties\":{\"x-foo-bar\":\"foo bar\",\"x-bar-foo\":null}}"); + String json2 = "{\"openapi\":\"3.0.1\",\"x-extension-with-properties\":{\"x-foo-bar\":\"foo bar\",\"x-bar-foo\":null}}"; + JSONObject jsonObj1 = new JSONObject(swaggerJson); + JSONObject jsonObj2 = new JSONObject(json2); + + JSONAssert.assertEquals(jsonObj1, jsonObj2, true); } @Test diff --git a/modules/swagger-core/src/test/java/io/swagger/v3/core/serialization/ModelSerializerTest.java b/modules/swagger-core/src/test/java/io/swagger/v3/core/serialization/ModelSerializerTest.java index b13386269c..7f1ebddab4 100644 --- a/modules/swagger-core/src/test/java/io/swagger/v3/core/serialization/ModelSerializerTest.java +++ b/modules/swagger-core/src/test/java/io/swagger/v3/core/serialization/ModelSerializerTest.java @@ -17,6 +17,8 @@ import io.swagger.v3.oas.models.media.Schema; import io.swagger.v3.oas.models.media.StringSchema; import org.testng.annotations.Test; +import org.json.JSONObject; +import org.skyscreamer.jsonassert.JSONAssert; import java.io.IOException; import java.math.BigDecimal; @@ -110,15 +112,22 @@ public void deserializeModel() throws IOException { public void serializeArrayModel() throws IOException { final ArraySchema model = new ArraySchema(); model.setItems(new Schema().$ref("Pet")); - assertEquals(m.writeValueAsString(model), "{\"type\":\"array\",\"items\":{\"$ref\":\"#/components/schemas/Pet\"}}"); + String json1 = m.writeValueAsString(model); + String json2 = "{\"type\":\"array\",\"items\":{\"$ref\":\"#/components/schemas/Pet\"}}"; + JSONObject jsonObj1 = new JSONObject(json1); + JSONObject jsonObj2 = new JSONObject(json2); + JSONAssert.assertEquals(jsonObj1, jsonObj2, true); } @Test(description = "it should deserialize an array model") public void deserializeArrayModel() throws IOException { - final String json = "{\"type\":\"array\",\"items\":{\"$ref\":\"#/definitions/Pet\"}}"; - final Schema p = m.readValue(json, Schema.class); + final String json1 = "{\"type\":\"array\",\"items\":{\"$ref\":\"#/definitions/Pet\"}}"; + final Schema p = m.readValue(json1, Schema.class); + String json2 = m.writeValueAsString(p); + JSONObject jsonObj1 = new JSONObject(json1); + JSONObject jsonObj2 = new JSONObject(json2); assertTrue(p instanceof ArraySchema); - assertEquals(m.writeValueAsString(p), json); + JSONAssert.assertEquals(jsonObj1, jsonObj2, true); } @Test(description = "it should not create an xml object for $ref") diff --git a/modules/swagger-core/src/test/java/io/swagger/v3/core/serialization/properties/PropertySerializationTest.java b/modules/swagger-core/src/test/java/io/swagger/v3/core/serialization/properties/PropertySerializationTest.java index 719b936be1..70c195ea1e 100644 --- a/modules/swagger-core/src/test/java/io/swagger/v3/core/serialization/properties/PropertySerializationTest.java +++ b/modules/swagger-core/src/test/java/io/swagger/v3/core/serialization/properties/PropertySerializationTest.java @@ -13,6 +13,8 @@ import io.swagger.v3.oas.models.media.Schema; import io.swagger.v3.oas.models.media.StringSchema; import org.testng.annotations.Test; +import org.json.JSONObject; +import org.skyscreamer.jsonassert.JSONAssert; import java.io.IOException; import java.math.BigDecimal; @@ -66,18 +68,24 @@ public void deserializeDateProperty() throws IOException { @Test(description = "it should serialize a DateTimeProperty") public void serializeDateTimeProperty() throws IOException { final DateTimeSchema p = new DateTimeSchema(); - final String json = "{\"type\":\"string\",\"format\":\"date-time\"}"; - assertEquals(m.writeValueAsString(p), json); + final String json1 = "{\"type\":\"string\",\"format\":\"date-time\"}"; + String json2 = m.writeValueAsString(p); + JSONObject jsonObj1 = new JSONObject(json1); + JSONObject jsonObj2 = new JSONObject(json2); + JSONAssert.assertEquals(jsonObj1, jsonObj2, true); } @Test(description = "it should deserialize a DateTimeProperty") public void deserializeDateTimeProperty() throws IOException { - final String json = "{\"type\":\"string\",\"format\":\"date-time\"}"; - final Schema p = m.readValue(json, Schema.class); + final String json1 = "{\"type\":\"string\",\"format\":\"date-time\"}"; + final Schema p = m.readValue(json1, Schema.class); assertEquals(p.getType(), "string"); assertEquals(p.getFormat(), "date-time"); assertEquals(p.getClass(), DateTimeSchema.class); - assertEquals(m.writeValueAsString(p), json); + String json2 = m.writeValueAsString(p); + JSONObject jsonObj1 = new JSONObject(json1); + JSONObject jsonObj2 = new JSONObject(json2); + JSONAssert.assertEquals(jsonObj1, jsonObj2, true); } @Test(description = "it should serialize a DoubleProperty") @@ -85,18 +93,24 @@ public void serializeDoubleProperty() throws IOException { final NumberSchema p = new NumberSchema() ._default(new BigDecimal("3.14159")); p.format("double"); - final String json = "{\"type\":\"number\",\"format\":\"double\",\"default\":3.14159}"; - assertEquals(m.writeValueAsString(p), json); + final String json1 = "{\"type\":\"number\",\"format\":\"double\",\"default\":3.14159}"; + String json2 = m.writeValueAsString(p); + JSONObject jsonObj1 = new JSONObject(json1); + JSONObject jsonObj2 = new JSONObject(json2); + JSONAssert.assertEquals(jsonObj1, jsonObj2, true); } @Test(description = "it should deserialize a DoubleProperty") public void deserializeDoubleProperty() throws IOException { - final String json = "{\"type\":\"number\",\"format\":\"double\"}"; - final Schema p = m.readValue(json, Schema.class); + final String json1 = "{\"type\":\"number\",\"format\":\"double\"}"; + final Schema p = m.readValue(json1, Schema.class); assertEquals(p.getType(), "number"); assertEquals(p.getFormat(), "double"); assertEquals(p.getClass(), NumberSchema.class); - assertEquals(m.writeValueAsString(p), json); + String json2 = m.writeValueAsString(p); + JSONObject jsonObj1 = new JSONObject(json1); + JSONObject jsonObj2 = new JSONObject(json2); + JSONAssert.assertEquals(jsonObj1, jsonObj2, true); } @Test(description = "it should serialize a FloatProperty") @@ -104,36 +118,48 @@ public void serializeFloatProperty() throws IOException { final NumberSchema p = new NumberSchema() ._default(new BigDecimal("1.2")); p.format("float"); - final String json = "{\"type\":\"number\",\"format\":\"float\",\"default\":1.2}"; - assertEquals(m.writeValueAsString(p), json); + final String json1 = "{\"type\":\"number\",\"format\":\"float\",\"default\":1.2}"; + String json2 = m.writeValueAsString(p); + JSONObject jsonObj1 = new JSONObject(json1); + JSONObject jsonObj2 = new JSONObject(json2); + JSONAssert.assertEquals(jsonObj1, jsonObj2, true); } @Test(description = "it should deserialize a FloatProperty") public void deserializeFloatProperty() throws IOException { - final String json = "{\"type\":\"number\",\"format\":\"float\"}"; - final Schema p = m.readValue(json, Schema.class); + final String json1 = "{\"type\":\"number\",\"format\":\"float\"}"; + final Schema p = m.readValue(json1, Schema.class); assertEquals(p.getType(), "number"); assertEquals(p.getFormat(), "float"); assertEquals(p.getClass(), NumberSchema.class); - assertEquals(m.writeValueAsString(p), json); + String json2 = m.writeValueAsString(p); + JSONObject jsonObj1 = new JSONObject(json1); + JSONObject jsonObj2 = new JSONObject(json2); + JSONAssert.assertEquals(jsonObj1, jsonObj2, true); } @Test(description = "it should serialize an IntegerProperty") public void serializeIntegerProperty() throws IOException { final IntegerSchema p = new IntegerSchema() ._default(32); - final String json = "{\"type\":\"integer\",\"format\":\"int32\",\"default\":32}"; - assertEquals(m.writeValueAsString(p), json); + final String json1 = "{\"type\":\"integer\",\"format\":\"int32\",\"default\":32}"; + String json2 = m.writeValueAsString(p); + JSONObject jsonObj1 = new JSONObject(json1); + JSONObject jsonObj2 = new JSONObject(json2); + JSONAssert.assertEquals(jsonObj1, jsonObj2, true); } @Test(description = "it should deserialize a IntegerProperty") public void deserializeIntegerProperty() throws IOException { - final String json = "{\"type\":\"integer\",\"format\":\"int32\"}"; - final Schema p = m.readValue(json, Schema.class); + final String json1 = "{\"type\":\"integer\",\"format\":\"int32\"}"; + final Schema p = m.readValue(json1, Schema.class); assertEquals(p.getType(), "integer"); assertEquals(p.getFormat(), "int32"); assertEquals(p.getClass(), IntegerSchema.class); - assertEquals(m.writeValueAsString(p), json); + String json2 = m.writeValueAsString(p); + JSONObject jsonObj1 = new JSONObject(json1); + JSONObject jsonObj2 = new JSONObject(json2); + JSONAssert.assertEquals(jsonObj1, jsonObj2, true); } @Test(description = "it should serialize a LongProperty") @@ -141,18 +167,24 @@ public void serializeLongProperty() throws IOException { final IntegerSchema p = new IntegerSchema() .format("int64") ._default(8675309); - final String json = "{\"type\":\"integer\",\"format\":\"int64\",\"default\":8675309}"; - assertEquals(m.writeValueAsString(p), json); + final String json1 = "{\"type\":\"integer\",\"format\":\"int64\",\"default\":8675309}"; + String json2 = m.writeValueAsString(p); + JSONObject jsonObj1 = new JSONObject(json1); + JSONObject jsonObj2 = new JSONObject(json2); + JSONAssert.assertEquals(jsonObj1, jsonObj2, true); } @Test(description = "it should deserialize a LongProperty") public void deserializeLongProperty() throws IOException { - final String json = "{\"type\":\"integer\",\"format\":\"int64\"}"; - final Schema p = m.readValue(json, Schema.class); + final String json1 = "{\"type\":\"integer\",\"format\":\"int64\"}"; + final Schema p = m.readValue(json1, Schema.class); assertEquals(p.getType(), "integer"); assertEquals(p.getFormat(), "int64"); assertEquals(p.getClass(), IntegerSchema.class); - assertEquals(m.writeValueAsString(p), json); + String json2 = m.writeValueAsString(p); + JSONObject jsonObj1 = new JSONObject(json1); + JSONObject jsonObj2 = new JSONObject(json2); + JSONAssert.assertEquals(jsonObj1, jsonObj2, true); } @Test(description = "it should serialize a string MapProperty") @@ -174,33 +206,45 @@ public void deserializeStringMapProperty() throws IOException { @Test(description = "it should serialize a integer MapProperty") public void serializeIntegerMapProperty() throws IOException { final Schema p = new MapSchema().additionalProperties(new IntegerSchema()); - final String json = "{\"type\":\"object\",\"additionalProperties\":{\"type\":\"integer\",\"format\":\"int32\"}}"; - assertEquals(m.writeValueAsString(p), json); + final String json1 = "{\"type\":\"object\",\"additionalProperties\":{\"type\":\"integer\",\"format\":\"int32\"}}"; + String json2 = m.writeValueAsString(p); + JSONObject jsonObj1 = new JSONObject(json1); + JSONObject jsonObj2 = new JSONObject(json2); + JSONAssert.assertEquals(jsonObj1, jsonObj2, true); } @Test(description = "it should deserialize a integer MapProperty") public void deserializeIntegerMapProperty() throws IOException { - final String json = "{\"type\":\"object\",\"additionalProperties\":{\"type\":\"integer\",\"format\":\"int32\"}}"; - final Schema p = m.readValue(json, Schema.class); + final String json1 = "{\"type\":\"object\",\"additionalProperties\":{\"type\":\"integer\",\"format\":\"int32\"}}"; + final Schema p = m.readValue(json1, Schema.class); assertEquals(p.getType(), "object"); assertEquals(p.getClass(), MapSchema.class); - assertEquals(m.writeValueAsString(p), json); + String json2 = m.writeValueAsString(p); + JSONObject jsonObj1 = new JSONObject(json1); + JSONObject jsonObj2 = new JSONObject(json2); + JSONAssert.assertEquals(jsonObj1, jsonObj2, true); } @Test(description = "it should serialize a long MapProperty") public void serializeLongMapProperty() throws IOException { final Schema p = new MapSchema().additionalProperties(new IntegerSchema().format("int64")); - final String json = "{\"type\":\"object\",\"additionalProperties\":{\"type\":\"integer\",\"format\":\"int64\"}}"; - assertEquals(m.writeValueAsString(p), json); + final String json1 = "{\"type\":\"object\",\"additionalProperties\":{\"type\":\"integer\",\"format\":\"int64\"}}"; + String json2 = m.writeValueAsString(p); + JSONObject jsonObj1 = new JSONObject(json1); + JSONObject jsonObj2 = new JSONObject(json2); + JSONAssert.assertEquals(jsonObj1, jsonObj2, true); } @Test(description = "it should deserialize a long MapProperty") public void deserializeLongMapProperty() throws IOException { - final String json = "{\"type\":\"object\",\"additionalProperties\":{\"type\":\"integer\",\"format\":\"int64\"}}"; - final Schema p = m.readValue(json, Schema.class); + final String json1 = "{\"type\":\"object\",\"additionalProperties\":{\"type\":\"integer\",\"format\":\"int64\"}}"; + final Schema p = m.readValue(json1, Schema.class); assertEquals(p.getType(), "object"); assertEquals(p.getClass(), MapSchema.class); - assertEquals(m.writeValueAsString(p), json); + String json2 = m.writeValueAsString(p); + JSONObject jsonObj1 = new JSONObject(json1); + JSONObject jsonObj2 = new JSONObject(json2); + JSONAssert.assertEquals(jsonObj1, jsonObj2, true); } @Test(description = "it should serialize a RefProperty") @@ -260,45 +304,60 @@ public void deserializeEnumStringProperty() throws IOException { @Test(description = "it should deserialize an IntegerProperty with enums") public void deserializeEnumIntegerProperty() throws IOException { - final String json = "{\"type\":\"integer\",\"format\":\"int32\",\"enum\":[1,2]}"; - final Schema p = m.readValue(json, Schema.class); + final String json1 = "{\"type\":\"integer\",\"format\":\"int32\",\"enum\":[1,2]}"; + final Schema p = m.readValue(json1, Schema.class); assertEquals(p.getType(), "integer"); List _enum = ((IntegerSchema) p).getEnum(); assertNotNull(_enum); assertEquals(_enum, Arrays.asList(1, 2)); assertEquals(p.getClass(), IntegerSchema.class); - assertEquals(m.writeValueAsString(p), json); + String json2 = m.writeValueAsString(p); + JSONObject jsonObj1 = new JSONObject(json1); + JSONObject jsonObj2 = new JSONObject(json2); + JSONAssert.assertEquals(jsonObj1, jsonObj2, true); } @Test(description = "it should serialize a string array property") public void serializeArrayStringProperty() throws IOException { final Schema p = new ArraySchema().items(new StringSchema()); - final String json = "{\"type\":\"array\",\"items\":{\"type\":\"string\"}}"; - assertEquals(m.writeValueAsString(p), json); + final String json1 = "{\"type\":\"array\",\"items\":{\"type\":\"string\"}}"; + String json2 = m.writeValueAsString(p); + JSONObject jsonObj1 = new JSONObject(json1); + JSONObject jsonObj2 = new JSONObject(json2); + JSONAssert.assertEquals(jsonObj1, jsonObj2, true); } @Test(description = "it should deserialize a string array property") public void deserializeArrayStringProperty() throws IOException { - final String json = "{\"type\":\"array\",\"items\":{\"type\":\"string\"}}"; - final Schema p = m.readValue(json, Schema.class); + final String json1 = "{\"type\":\"array\",\"items\":{\"type\":\"string\"}}"; + final Schema p = m.readValue(json1, Schema.class); + String json2 = m.writeValueAsString(p); assertEquals(p.getType(), "array"); assertEquals(p.getClass(), ArraySchema.class); - assertEquals(m.writeValueAsString(p), json); + JSONObject jsonObj1 = new JSONObject(json1); + JSONObject jsonObj2 = new JSONObject(json2); + JSONAssert.assertEquals(jsonObj1, jsonObj2, true); } @Test(description = "it should serialize a string property with readOnly set") public void serializeReadOnlyStringProperty() throws IOException { final Schema p = new StringSchema().readOnly(true); - final String json = "{\"type\":\"string\",\"readOnly\":true}"; - assertEquals(m.writeValueAsString(p), json); + final String json1 = "{\"type\":\"string\",\"readOnly\":true}"; + String json2 = m.writeValueAsString(p); + JSONObject jsonObj1 = new JSONObject(json1); + JSONObject jsonObj2 = new JSONObject(json2); + JSONAssert.assertEquals(jsonObj1, jsonObj2, true); } @Test(description = "it should serialize a string property with readOnly unset") public void deserializeNotReadOnlyStringProperty() throws IOException { final StringSchema p = new StringSchema(); p.setReadOnly(false); - final String json = "{\"type\":\"string\",\"readOnly\":false}"; - assertEquals(m.writeValueAsString(p), json); + final String json1 = "{\"type\":\"string\",\"readOnly\":false}"; + String json2 = m.writeValueAsString(p); + JSONObject jsonObj1 = new JSONObject(json1); + JSONObject jsonObj2 = new JSONObject(json2); + JSONAssert.assertEquals(jsonObj1, jsonObj2, true); } @Test(description = "it should serialize an object property with required set") @@ -306,8 +365,11 @@ public void serializeObjectPropertyWithRequiredProperties() throws IOException { final Schema p = new ObjectSchema() .addProperties("stringProperty", new StringSchema()); p.required(Arrays.asList("stringProperty")); - final String json = "{\"required\":[\"stringProperty\"],\"type\":\"object\",\"properties\":{\"stringProperty\":{\"type\":\"string\"}}}"; - assertEquals(m.writeValueAsString(p), json); + final String json1 = "{\"required\":[\"stringProperty\"],\"type\":\"object\",\"properties\":{\"stringProperty\":{\"type\":\"string\"}}}"; + String json2 = m.writeValueAsString(p); + JSONObject jsonObj1 = new JSONObject(json1); + JSONObject jsonObj2 = new JSONObject(json2); + JSONAssert.assertEquals(jsonObj1, jsonObj2, true); } @Test(description = "it should deserialize an object property with required set")