public static <T> T deserializeJsonFileFromClasspath(String path, Class<T> type) { return deserializeFileFromClasspath(path, type, Json.mapper()); }
@Test public void testDeserializePetStoreFile() throws Exception { TestUtils.deserializeJsonFileFromClasspath("specFiles/petstore.json", OpenAPI.class); }
@Test public void testFormatOfDecimal() { final Map<String, Schema> models = ModelConverters.getInstance().readAll(ModelWithDecimalFields.class); assertEquals(models.size(), 1); String json = Json.pretty(models); assertEquals(normalizeLineEnds(json), "{\n" + " \"ModelWithDecimalFields\" : {\n" + " \"type\" : \"object\",\n" + " \"properties\" : {\n" + " \"id\" : {\n" + " \"minimum\" : 3.3,\n" + " \"exclusiveMinimum\" : false,\n" + " \"type\" : \"number\",\n" + " \"format\" : \"double\"\n" + " }\n" + " }\n" + " }\n" + "}"); }
@Test public void testFormatOfBigDecimal() { final Map<String, Schema> models = ModelConverters.getInstance().readAll(ModelWithoutScientificFields.class); assertEquals(models.size(), 1); String json = Json.pretty(models); assertEquals(normalizeLineEnds(json), "{\n" + " \"ModelWithoutScientificFields\" : {\n" + " \"type\" : \"object\",\n" + " \"properties\" : {\n" + " \"id\" : {\n" + " \"maximum\" : 9999999999999999.99,\n" + " \"exclusiveMaximum\" : false,\n" + " \"minimum\" : -9999999999999999.99,\n" + " \"exclusiveMinimum\" : false,\n" + " \"type\" : \"number\"\n" + " }\n" + " }\n" + " }\n" + "}"); }
@Test public void testDeserializeCompositionTest() throws Exception { TestUtils.deserializeJsonFileFromClasspath("specFiles/compositionTest.json", OpenAPI.class); }
@Test public void testFormatOfInteger() { final Map<String, Schema> models = ModelConverters.getInstance().readAll(ModelWithIntegerFields.class); assertEquals(models.size(), 1); String json = Json.pretty(models); assertEquals(normalizeLineEnds(json), "{\n" + " \"ModelWithIntegerFields\" : {\n" + " \"type\" : \"object\",\n" + " \"properties\" : {\n" + " \"id\" : {\n" + " \"minimum\" : 3,\n" + " \"type\" : \"integer\",\n" + " \"format\" : \"int32\"\n" + " }\n" + " }\n" + " }\n" + "}"); }
public static <T> T deserializeYamlFileFromClasspath(String path, Class<T> type) { return deserializeFileFromClasspath(path, type, Yaml.mapper()); }
@Test public void givenMapProperty_shouldDeserializeMaxProperties() { String path = "json-schema-validation/map.json"; MapSchema property = (MapSchema) TestUtils.deserializeJsonFileFromClasspath(path, Schema.class); assertNotNull(property.getMaxProperties()); assertEquals(property.getMaxProperties().intValue(), 10); } }
@Test(description = "it should serialize a boolean additionalProperties") public void testBooleanAdditionalPropertiesSerialization() throws Exception { Operation operation = Json.mapper().readValue(json, Operation.class); ApiResponse response = operation.getResponses().get("200"); assertNotNull(response); Schema responseSchema = response.getContent().get("*/*").getSchema(); Schema schema = new ObjectSchema().additionalProperties(true); assertEquals(normalizeLineEnds(Json.pretty(schema)), "{\n" + " \"type\" : \"object\",\n" + " \"additionalProperties\" : true\n" + "}"); schema = new ObjectSchema().additionalProperties(responseSchema); assertEquals(normalizeLineEnds(Json.pretty(schema)), "{\n" + " \"type\" : \"object\",\n" + " \"additionalProperties\" : {\n" + " \"type\" : \"object\",\n" + " \"additionalProperties\" : {\n" + " \"type\" : \"integer\",\n" + " \"format\" : \"int32\"\n" + " },\n" + " \"x-foo\" : \"vendor x\"\n" + " }\n" + "}"); }
@Test public void shouldDeserializeArrayPropertyMinItems() throws Exception { String path = "json-schema-validation/array.json"; ArraySchema property = (ArraySchema) TestUtils.deserializeJsonFileFromClasspath(path, Schema.class); assertNotNull(property.getMinItems()); assertEquals(property.getMinItems().intValue(), 1); }
@Test public void shouldDeserializeArrayPropertyMaxItems() throws Exception { String path = "json-schema-validation/array.json"; ArraySchema property = (ArraySchema) TestUtils.deserializeJsonFileFromClasspath(path, Schema.class); assertNotNull(property.getMaxItems()); assertEquals(property.getMaxItems().intValue(), 10); }
@Test public void givenMapProperty_shouldDeserializeMinProperties() { String path = "json-schema-validation/map.json"; MapSchema property = (MapSchema) TestUtils.deserializeJsonFileFromClasspath(path, Schema.class); assertNotNull(property.getMinProperties()); assertEquals(property.getMinProperties().intValue(), 1); }
@Test public void shouldDeserializeArrayPropertyUniqueItems() throws Exception { String path = "json-schema-validation/array.json"; ArraySchema property = (ArraySchema) TestUtils.deserializeJsonFileFromClasspath(path, Schema.class); assertNotNull(property.getUniqueItems()); assertTrue(property.getUniqueItems()); }
@Test(description = "it should serialize correctly security") public void serializeSecurity() throws IOException { final OpenAPI oas = TestUtils.deserializeJsonFileFromClasspath("specFiles/securityDefinitions.json", OpenAPI.class); final String json = ResourceUtils.loadClassResource(getClass(), "specFiles/securityDefinitions.json"); SerializationMatchers.assertEqualsToJson(oas, json); }
@Test(description = "should deserialize a string property with constraints") public void testDeserializeConstrainedStringProperty() throws Exception { OpenAPI oas = TestUtils.deserializeJsonFileFromClasspath("specFiles/propertiesWithConstraints.json", OpenAPI.class); StringSchema property = (StringSchema) oas.getComponents().getSchemas().get("Health").getProperties().get("string_with_constraints"); assertEquals(property.getMinLength(), Integer.valueOf(10)); assertEquals(property.getMaxLength(), Integer.valueOf(100)); assertEquals(property.getPattern(), "apattern"); }
@Test public void testDeserializeAPathRef() throws Exception { final OpenAPI oas = TestUtils.deserializeJsonFileFromClasspath("specFiles/pathRef.json", OpenAPI.class); final PathItem petPath = oas.getPaths().get("/pet"); assertNotNull(petPath.get$ref()); assertEquals(petPath.get$ref(), "http://my.company.com/paths/health.json"); assertTrue(oas.getPaths().get("/user") instanceof PathItem); }
@Test(description = "should deserialize a property with vendor extensions of different types") public void testDeserializePropertyWithVendorExtensions() throws Exception { OpenAPI oas = TestUtils.deserializeJsonFileFromClasspath("specFiles/propertyWithVendorExtensions.json", OpenAPI.class); Map<String, Object> oasVendorExtensions = oas.getExtensions(); Map<String, Object> vendorExtensions = ((Schema) oas.getComponents().getSchemas().get("Health").getProperties().get("status")).getExtensions(); assertVendorExtensions(oasVendorExtensions); assertVendorExtensions(vendorExtensions); //check for vendor extensions in array property types vendorExtensions = ((Schema) oas.getComponents().getSchemas().get("Health").getProperties().get("array")).getExtensions(); String xStringValue = (String) vendorExtensions.get("x-string-value"); assertNotNull(xStringValue); assertEquals(xStringValue, "string_value"); }
@Test(description = "should deserialize an array property with constraints") public void testDeserializeConstrainedArrayProperties() throws Exception { OpenAPI oas = TestUtils.deserializeJsonFileFromClasspath("specFiles/propertiesWithConstraints.json", OpenAPI.class); Map<String, Schema> properties = oas.getComponents().getSchemas().get("Health").getProperties(); ArraySchema withMin = (ArraySchema) properties.get("array_with_min"); assertEquals(withMin.getMinItems(), Integer.valueOf(5)); assertNull(withMin.getMaxItems()); assertNull(withMin.getUniqueItems()); ArraySchema withMax = (ArraySchema) properties.get("array_with_max"); assertNull(withMax.getMinItems()); assertEquals(withMax.getMaxItems(), Integer.valueOf(10)); assertNull(withMax.getUniqueItems()); ArraySchema withUnique = (ArraySchema) properties.get("array_with_unique"); assertNull(withUnique.getMinItems()); assertNull(withUnique.getMaxItems()); assertEquals(withUnique.getUniqueItems(), Boolean.TRUE); ArraySchema withAll = (ArraySchema) properties.get("array_with_all"); assertEquals(withAll.getMinItems(), Integer.valueOf(1)); assertEquals(withAll.getMaxItems(), Integer.valueOf(10)); assertEquals(withAll.getUniqueItems(), Boolean.TRUE); }
@Test public void testDeserializeSecurity() throws Exception { final OpenAPI swagger = TestUtils.deserializeJsonFileFromClasspath("specFiles/securityDefinitions.json", OpenAPI.class);
@Test public void testDeserializeAResponseRef() throws Exception { final OpenAPI oas = TestUtils.deserializeJsonFileFromClasspath("specFiles/responseRef.json", OpenAPI.class); final ApiResponses responseMap = oas.getPaths().get("/pet").getPut().getResponses(); // TODO: missing response ref assertIsRefResponse(responseMap.get("405"), "http://my.company.com/responses/errors.json#/method-not-allowed"); assertIsRefResponse(responseMap.get("404"), "http://my.company.com/responses/errors.json#/not-found"); assertTrue(responseMap.get("400") instanceof ApiResponse); }