/** * Uses the SwaggerParser to read the Swagger source. * * @param swaggerLocation the location of the Swagger source * @return the Swagger model */ private Swagger readSwagger(String swaggerLocation) { Swagger swagger = new SwaggerParser().read(swaggerLocation); if (swagger == null) { throw new IllegalArgumentException("Failed to read the Swagger source"); } return swagger; }
private static Swagger parseSwagger(String location) { SwaggerParser parser = new SwaggerParser(); return parser.read(location); }
private SwaggerDiff(JsonNode oldSpec, JsonNode newSpec) { SwaggerParser swaggerParser = new SwaggerParser(); oldSpecSwagger = swaggerParser.read(oldSpec, true); newSpecSwagger = swaggerParser.read(newSpec, true); if (null == oldSpecSwagger || null == newSpecSwagger) { throw new RuntimeException( "cannot read api-doc from spec."); } }
private Swagger parse(String filePath) { final Swagger swagger = parser.read(filePath); if (swagger != null && swagger.getPaths() != null) { LOG.info("Parsed Swagger with " + swagger.getPaths().size() + " paths"); } return swagger; }
/** * Verifies that the actual value is equal to the given one. * * @param expectedLocation the location of the given value to compare the actual value to. * @return {@code this} assertion object. * @throws AssertionError if the actual value is not equal to the given one or if the actual value is {@code null}.. */ public SwaggerAssert isEqualTo(String expectedLocation) { return isEqualTo(new SwaggerParser().read(expectedLocation)); }
/** * Verifies that the actual value is equal to the given one. * * @param expectedLocation the location of the given value to compare the actual value to. * @return {@code this} assertion object. * @throws AssertionError if the actual value is not equal to the given one or if the actual value is {@code null}.. */ public SwaggerAssert satisfiesContract(String expectedLocation) { return satisfiesContract(new SwaggerParser().read(expectedLocation)); }
/** * Creates a new instance of <code>{@link SwaggerAssert}</code>. * * @param actualLocation the location the actual Swagger value. * @return the created assertion object. */ public static SwaggerAssert assertThat(String actualLocation) { Validate.notNull(actualLocation, "actualLocation must not be null!"); return new SwaggerAssert(new SwaggerParser().read(actualLocation)); } }
@Test public void shouldHandleExpectedPathsWithPrefix() { File implFirstSwaggerLocation = new File( SwaggerDocumentationDrivenAssertTest.class.getResource("/swagger_with_path_prefixes.json").getPath()); File designFirstSwaggerLocation = new File( SwaggerDocumentationDrivenAssertTest.class.getResource("/swagger.yaml").getPath()); Validate.notNull(implFirstSwaggerLocation.getAbsolutePath(), "actualLocation must not be null!"); new SwaggerAssert(new SwaggerParser().read(implFirstSwaggerLocation.getAbsolutePath()), "/assertj-swagger-path-prefix.properties") .isEqualTo(designFirstSwaggerLocation.getAbsolutePath()); }
@Test public void shouldHandleDefinitionsUsingAllOf() { File implFirstSwaggerLocation = new File( SwaggerConsumerDrivenAssertTest.class.getResource("/swagger-allOf-test-flat.json").getPath()); File designFirstSwaggerLocation = new File( SwaggerConsumerDrivenAssertTest.class.getResource("/swagger-allOf-test-inheritance.json").getPath()); Validate.notNull(implFirstSwaggerLocation.getAbsolutePath(), "actualLocation must not be null!"); new SwaggerAssert(new SwaggerParser().read(implFirstSwaggerLocation.getAbsolutePath()), "/assertj-swagger-allOf.properties") .isEqualTo(designFirstSwaggerLocation.getAbsolutePath()); }
@Test public void shouldHandleDefinitionsUsingAllOf() { File implFirstSwaggerLocation = new File(SwaggerConsumerDrivenAssertTest.class.getResource("/swagger-allOf-test-flat.json").getPath()); File designFirstSwaggerLocation = new File(SwaggerConsumerDrivenAssertTest.class.getResource("/swagger-allOf-test-inheritance.json").getPath()); Validate.notNull(implFirstSwaggerLocation.getAbsolutePath(), "actualLocation must not be null!"); new SwaggerAssert(new SwaggerParser().read(implFirstSwaggerLocation.getAbsolutePath()), "/assertj-swagger-allOf.properties") .satisfiesContract(designFirstSwaggerLocation.getAbsolutePath()); }
@Test public void shouldHandleDefinitionsUsingAllOfForComposition() { File implFirstSwaggerLocation = new File( SwaggerConsumerDrivenAssertTest.class.getResource("/swagger-allOf-composition-flat.json").getPath()); File designFirstSwaggerLocation = new File( SwaggerConsumerDrivenAssertTest.class.getResource("/swagger-allOf-composition.json").getPath()); Validate.notNull(implFirstSwaggerLocation.getAbsolutePath(), "actualLocation must not be null!"); new SwaggerAssert(new SwaggerParser().read(implFirstSwaggerLocation.getAbsolutePath())) .isEqualTo(designFirstSwaggerLocation.getAbsolutePath()); }
@Test public void shouldHandleByteArrayValues() { File implFirstSwaggerLocation = new File( SwaggerConsumerDrivenAssertTest.class.getResource("/swagger-bytearray.json").getPath()); File designFirstSwaggerLocation = new File( SwaggerConsumerDrivenAssertTest.class.getResource("/swagger-bytearray.yaml").getPath()); Validate.notNull(implFirstSwaggerLocation.getAbsolutePath(), "actualLocation must not be null!"); new SwaggerAssert(new SwaggerParser().read(implFirstSwaggerLocation.getAbsolutePath())) .isEqualTo(designFirstSwaggerLocation.getAbsolutePath()); }
@Test(expected = AssertionError.class) public void shouldFindDifferentByteArrayValues() { File implFirstSwaggerLocation = new File( SwaggerConsumerDrivenAssertTest.class.getResource("/swagger-bytearray-wrong.json").getPath()); File designFirstSwaggerLocation = new File( SwaggerConsumerDrivenAssertTest.class.getResource("/swagger-bytearray.yaml").getPath()); Validate.notNull(implFirstSwaggerLocation.getAbsolutePath(), "actualLocation must not be null!"); new SwaggerAssert(new SwaggerParser().read(implFirstSwaggerLocation.getAbsolutePath())) .isEqualTo(designFirstSwaggerLocation.getAbsolutePath()); }
@Test public void shouldRefHandleByteArrayValues() { File implFirstSwaggerLocation = new File( SwaggerConsumerDrivenAssertTest.class.getResource("/swagger-bytearray-ref.json").getPath()); File designFirstSwaggerLocation = new File( SwaggerConsumerDrivenAssertTest.class.getResource("/swagger-bytearray-ref.yaml").getPath()); Validate.notNull(implFirstSwaggerLocation.getAbsolutePath(), "actualLocation must not be null!"); new SwaggerAssert(new SwaggerParser().read(implFirstSwaggerLocation.getAbsolutePath())) .isEqualTo(designFirstSwaggerLocation.getAbsolutePath()); }
@Test public void shouldHandleDefinitionsUsingAllOfIncludingCycles() { File implFirstSwaggerLocation = new File(SwaggerConsumerDrivenAssertTest.class.getResource("/swagger-allOf-test-flat.json").getPath()); File designFirstSwaggerLocation = new File(SwaggerConsumerDrivenAssertTest.class.getResource("/swagger-allOf-test-inheritance-cycles.json").getPath()); Validate.notNull(implFirstSwaggerLocation.getAbsolutePath(), "actualLocation must not be null!"); new SwaggerAssert(new SwaggerParser().read(implFirstSwaggerLocation.getAbsolutePath()), "/assertj-swagger-allOf.properties") .satisfiesContract(designFirstSwaggerLocation.getAbsolutePath()); }
@Test public void shouldHandlePartiallyImplementedApi() { File implFirstSwaggerLocation = new File( SwaggerDocumentationDrivenAssertTest.class.getResource("/partial_impl_swagger.json").getPath()); File designFirstSwaggerLocation = new File( SwaggerDocumentationDrivenAssertTest.class.getResource("/swagger.yaml").getPath()); Validate.notNull(implFirstSwaggerLocation.getAbsolutePath(), "actualLocation must not be null!"); new SwaggerAssert(new SwaggerParser().read(implFirstSwaggerLocation.getAbsolutePath()), "/assertj-swagger-partial-impl.properties") .isEqualTo(designFirstSwaggerLocation.getAbsolutePath()); }
@Test(expected = AssertionError.class) public void shouldFindDifferentRefByteArrayValues() { File implFirstSwaggerLocation = new File( SwaggerConsumerDrivenAssertTest.class.getResource("/swagger-bytearray-ref-wrong.json").getPath()); File designFirstSwaggerLocation = new File( SwaggerConsumerDrivenAssertTest.class.getResource("/swagger-bytearray-ref.yaml").getPath()); Validate.notNull(implFirstSwaggerLocation.getAbsolutePath(), "actualLocation must not be null!"); new SwaggerAssert(new SwaggerParser().read(implFirstSwaggerLocation.getAbsolutePath())) .isEqualTo(designFirstSwaggerLocation.getAbsolutePath()); }
@Test(expected = AssertionError.class) public void shouldFindDifferentRefEnumValues() { File implFirstSwaggerLocation = new File( SwaggerConsumerDrivenAssertTest.class.getResource("/swagger-enum-ref-wrong.json").getPath()); File designFirstSwaggerLocation = new File( SwaggerConsumerDrivenAssertTest.class.getResource("/swagger-enum-ref.yaml").getPath()); Validate.notNull(implFirstSwaggerLocation.getAbsolutePath(), "actualLocation must not be null!"); new SwaggerAssert(new SwaggerParser().read(implFirstSwaggerLocation.getAbsolutePath())) .isEqualTo(designFirstSwaggerLocation.getAbsolutePath()); } }
@Test public void shouldHandleExpectedPathsWithPrefix() { File implFirstSwaggerLocation = new File(SwaggerConsumerDrivenAssertTest.class.getResource("/swagger_with_path_prefixes.json").getPath()); File designFirstSwaggerLocation = new File(SwaggerConsumerDrivenAssertTest.class.getResource("/swagger.yaml").getPath()); Validate.notNull(implFirstSwaggerLocation.getAbsolutePath(), "actualLocation must not be null!"); new SwaggerAssert(new SwaggerParser().read(implFirstSwaggerLocation.getAbsolutePath()), "/assertj-swagger-path-prefix.properties") .satisfiesContract(designFirstSwaggerLocation.getAbsolutePath()); }
private NewApiEntity prepareV2(ImportSwaggerDescriptorEntity swaggerDescriptor) { NewApiEntity apiEntity; logger.info("Trying to loading a Swagger descriptor in v2"); if (swaggerDescriptor.getType() == ImportSwaggerDescriptorEntity.Type.INLINE) { apiEntity = mapSwagger12ToNewApi(new SwaggerParser().parse(swaggerDescriptor.getPayload())); } else { apiEntity = mapSwagger12ToNewApi(new SwaggerParser().read(swaggerDescriptor.getPayload())); } return apiEntity; }