|
| 1 | +package io.swagger.v3.core.deserialization; |
| 2 | + |
| 3 | +import io.swagger.v3.core.matchers.SerializationMatchers; |
| 4 | +import io.swagger.v3.core.util.Json31; |
| 5 | +import io.swagger.v3.core.util.Yaml31; |
| 6 | +import io.swagger.v3.oas.models.OpenAPI; |
| 7 | +import org.testng.annotations.Test; |
| 8 | + |
| 9 | +import java.io.File; |
| 10 | +import java.io.IOException; |
| 11 | + |
| 12 | +import static org.testng.Assert.assertEquals; |
| 13 | +import static org.testng.Assert.assertNotNull; |
| 14 | +import static org.testng.Assert.assertTrue; |
| 15 | + |
| 16 | +/** |
| 17 | + * Test class to validate the comprehensive OpenAPI 3.1 files. |
| 18 | + */ |
| 19 | +public class ComprehensiveOAS31ValidationTest { |
| 20 | + |
| 21 | + /** |
| 22 | + * Test to validate the comprehensive OpenAPI 3.1 file. |
| 23 | + * This test ensures that the file can be properly deserialized by the swagger-core library |
| 24 | + * and that all references are correctly resolved. |
| 25 | + */ |
| 26 | + @Test |
| 27 | + public void testComprehensiveOAS31Validation() throws IOException { |
| 28 | + // Path to the comprehensive OpenAPI 3.1 file |
| 29 | + String filePath = "src/test/resources/comprehensiveOAS31/comprehensive-openapi.yaml"; |
| 30 | + |
| 31 | + // Verify the file exists |
| 32 | + File file = new File(filePath); |
| 33 | + assertTrue(file.exists(), "Comprehensive OpenAPI 3.1 file does not exist: " + filePath); |
| 34 | + |
| 35 | + // Deserialize the file |
| 36 | + OpenAPI openAPI = Yaml31.mapper().readValue(file, OpenAPI.class); |
| 37 | + |
| 38 | + // Verify the file was properly deserialized |
| 39 | + assertNotNull(openAPI, "Failed to deserialize the comprehensive OpenAPI 3.1 file"); |
| 40 | + |
| 41 | + // Verify basic structure |
| 42 | + assertNotNull(openAPI.getInfo(), "Info object is missing"); |
| 43 | + assertNotNull(openAPI.getPaths(), "Paths object is missing"); |
| 44 | + assertNotNull(openAPI.getComponents(), "Components object is missing"); |
| 45 | + |
| 46 | + // Verify components |
| 47 | + assertNotNull(openAPI.getComponents().getSchemas(), "Schemas object is missing"); |
| 48 | + assertNotNull(openAPI.getComponents().getPathItems(), "PathItems object is missing"); |
| 49 | + assertNotNull(openAPI.getComponents().getParameters(), "Parameters object is missing"); |
| 50 | + assertNotNull(openAPI.getComponents().getRequestBodies(), "RequestBodies object is missing"); |
| 51 | + assertNotNull(openAPI.getComponents().getResponses(), "Responses object is missing"); |
| 52 | + assertNotNull(openAPI.getComponents().getHeaders(), "Headers object is missing"); |
| 53 | + assertNotNull(openAPI.getComponents().getSecuritySchemes(), "SecuritySchemes object is missing"); |
| 54 | + assertNotNull(openAPI.getComponents().getExamples(), "Examples object is missing"); |
| 55 | + assertNotNull(openAPI.getComponents().getLinks(), "Links object is missing"); |
| 56 | + assertNotNull(openAPI.getComponents().getCallbacks(), "Callbacks object is missing"); |
| 57 | + |
| 58 | + // Verify webhooks |
| 59 | + assertNotNull(openAPI.getWebhooks(), "Webhooks object is missing"); |
| 60 | + |
| 61 | + // Verify schemas |
| 62 | + assertTrue(openAPI.getComponents().getSchemas().size() > 0, "No schemas found"); |
| 63 | + |
| 64 | + // Verify paths |
| 65 | + assertTrue(openAPI.getPaths().size() > 0, "No paths found"); |
| 66 | + |
| 67 | + // Verify webhooks |
| 68 | + assertTrue(openAPI.getWebhooks().size() > 0, "No webhooks found"); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Test round-trip serialization and deserialization of the comprehensive OpenAPI 3.1 file using YAML. |
| 73 | + * This test ensures that the file can be properly serialized and deserialized without losing information. |
| 74 | + */ |
| 75 | + @Test |
| 76 | + public void testComprehensiveOAS31YamlRoundTrip() throws IOException { |
| 77 | + // Path to the comprehensive OpenAPI 3.1 file |
| 78 | + String filePath = "src/test/resources/comprehensiveOAS31/comprehensive-openapi.yaml"; |
| 79 | + |
| 80 | + // Verify the file exists |
| 81 | + File file = new File(filePath); |
| 82 | + assertTrue(file.exists(), "Comprehensive OpenAPI 3.1 file does not exist: " + filePath); |
| 83 | + |
| 84 | + // Deserialize the file |
| 85 | + OpenAPI originalOpenAPI = Yaml31.mapper().readValue(file, OpenAPI.class); |
| 86 | + |
| 87 | + // Serialize to YAML |
| 88 | + String yaml = Yaml31.pretty(originalOpenAPI); |
| 89 | + |
| 90 | + // Deserialize back |
| 91 | + OpenAPI deserializedOpenAPI = Yaml31.mapper().readValue(yaml, OpenAPI.class); |
| 92 | + |
| 93 | + // Verify basic structure |
| 94 | + assertNotNull(deserializedOpenAPI, "Failed to deserialize the serialized YAML"); |
| 95 | + assertEquals(deserializedOpenAPI.getOpenapi(), "3.1.0", "OpenAPI version mismatch"); |
| 96 | + assertEquals(deserializedOpenAPI.getInfo().getTitle(), originalOpenAPI.getInfo().getTitle(), "Title mismatch"); |
| 97 | + assertEquals(deserializedOpenAPI.getInfo().getVersion(), originalOpenAPI.getInfo().getVersion(), "Version mismatch"); |
| 98 | + |
| 99 | + // Verify components |
| 100 | + assertNotNull(deserializedOpenAPI.getComponents(), "Components object is missing"); |
| 101 | + assertNotNull(deserializedOpenAPI.getComponents().getSchemas(), "Schemas object is missing"); |
| 102 | + assertEquals(deserializedOpenAPI.getComponents().getSchemas().size(), originalOpenAPI.getComponents().getSchemas().size(), "Schemas size mismatch"); |
| 103 | + |
| 104 | + // Verify paths |
| 105 | + assertNotNull(deserializedOpenAPI.getPaths(), "Paths object is missing"); |
| 106 | + assertEquals(deserializedOpenAPI.getPaths().size(), originalOpenAPI.getPaths().size(), "Paths size mismatch"); |
| 107 | + |
| 108 | + // Verify webhooks |
| 109 | + assertNotNull(deserializedOpenAPI.getWebhooks(), "Webhooks object is missing"); |
| 110 | + assertEquals(deserializedOpenAPI.getWebhooks().size(), originalOpenAPI.getWebhooks().size(), "Webhooks size mismatch"); |
| 111 | + |
| 112 | + // Serialize again |
| 113 | + String yamlAgain = Yaml31.pretty(deserializedOpenAPI); |
| 114 | + |
| 115 | + // Compare YAML strings (normalize whitespace) |
| 116 | + SerializationMatchers.assertEqualsToYaml31(originalOpenAPI, yamlAgain); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Test round-trip serialization and deserialization of the comprehensive OpenAPI 3.1 file using JSON. |
| 121 | + * This test ensures that the file can be properly serialized and deserialized without losing information. |
| 122 | + */ |
| 123 | + @Test |
| 124 | + public void testComprehensiveOAS31JsonRoundTrip() throws IOException { |
| 125 | + // Path to the comprehensive OpenAPI 3.1 file |
| 126 | + String filePath = "src/test/resources/comprehensiveOAS31/comprehensive-openapi.yaml"; |
| 127 | + |
| 128 | + // Verify the file exists |
| 129 | + File file = new File(filePath); |
| 130 | + assertTrue(file.exists(), "Comprehensive OpenAPI 3.1 file does not exist: " + filePath); |
| 131 | + |
| 132 | + // Deserialize the file |
| 133 | + OpenAPI originalOpenAPI = Yaml31.mapper().readValue(file, OpenAPI.class); |
| 134 | + |
| 135 | + // Serialize to JSON |
| 136 | + String json = Json31.pretty(originalOpenAPI); |
| 137 | + |
| 138 | + // Deserialize back |
| 139 | + OpenAPI deserializedOpenAPI = Json31.mapper().readValue(json, OpenAPI.class); |
| 140 | + |
| 141 | + // Verify basic structure |
| 142 | + assertNotNull(deserializedOpenAPI, "Failed to deserialize the serialized JSON"); |
| 143 | + assertEquals(deserializedOpenAPI.getOpenapi(), "3.1.0", "OpenAPI version mismatch"); |
| 144 | + assertEquals(deserializedOpenAPI.getInfo().getTitle(), originalOpenAPI.getInfo().getTitle(), "Title mismatch"); |
| 145 | + assertEquals(deserializedOpenAPI.getInfo().getVersion(), originalOpenAPI.getInfo().getVersion(), "Version mismatch"); |
| 146 | + |
| 147 | + // Verify components |
| 148 | + assertNotNull(deserializedOpenAPI.getComponents(), "Components object is missing"); |
| 149 | + assertNotNull(deserializedOpenAPI.getComponents().getSchemas(), "Schemas object is missing"); |
| 150 | + assertEquals(deserializedOpenAPI.getComponents().getSchemas().size(), originalOpenAPI.getComponents().getSchemas().size(), "Schemas size mismatch"); |
| 151 | + |
| 152 | + // Verify paths |
| 153 | + assertNotNull(deserializedOpenAPI.getPaths(), "Paths object is missing"); |
| 154 | + assertEquals(deserializedOpenAPI.getPaths().size(), originalOpenAPI.getPaths().size(), "Paths size mismatch"); |
| 155 | + |
| 156 | + // Verify webhooks |
| 157 | + assertNotNull(deserializedOpenAPI.getWebhooks(), "Webhooks object is missing"); |
| 158 | + assertEquals(deserializedOpenAPI.getWebhooks().size(), originalOpenAPI.getWebhooks().size(), "Webhooks size mismatch"); |
| 159 | + |
| 160 | + // Serialize again |
| 161 | + String jsonAgain = Json31.pretty(deserializedOpenAPI); |
| 162 | + |
| 163 | + // Compare JSON strings |
| 164 | + SerializationMatchers.assertEqualsToJson31(originalOpenAPI, jsonAgain); |
| 165 | + } |
| 166 | +} |
0 commit comments