@Test public void testGroupedByTags() throws IOException, URISyntaxException { //Given Path file = Paths.get(AsciidocConverterTest.class.getResource("/yaml/swagger_petstore.yaml").toURI()); Path outputDirectory = Paths.get("build/test/asciidoc/group_by_tags"); FileUtils.deleteQuietly(outputDirectory.toFile()); //When Swagger2MarkupConfig config = new Swagger2MarkupConfigBuilder() .withPathsGroupedBy(GroupBy.TAGS) .build(); Swagger2MarkupConverter.from(file) .withConfig(config) .build() .toFolder(outputDirectory); //Then String[] files = outputDirectory.toFile().list(); assertThat(files).hasSize(4).containsAll(expectedFiles); Path expectedFilesDirectory = Paths.get(AsciidocConverterTest.class.getResource("/expected/asciidoc/group_by_tags").toURI()); DiffUtils.assertThatAllFilesAreEqual(expectedFilesDirectory, outputDirectory, "testGroupedByTags.html"); }
@Test public void testByTagsWithMissingTag() throws IOException, URISyntaxException { //Given Path file = Paths.get(AsciidocConverterTest.class.getResource("/json/swagger_missing_tag.json").toURI()); Path outputDirectory = Paths.get("build/test/asciidoc/generated"); FileUtils.deleteQuietly(outputDirectory.toFile()); //When try { Swagger2MarkupConfig config = new Swagger2MarkupConfigBuilder() .withPathsGroupedBy(GroupBy.TAGS) .build(); Swagger2MarkupConverter.from(file) .withConfig(config) .build() .toFolder(outputDirectory); // If NullPointerException was not thrown, test would fail the specified message failBecauseExceptionWasNotThrown(NullPointerException.class); } catch (Exception e) { assertThat(e).hasMessage("Can't GroupBy.TAGS. Operation 'updatePet' has no tags"); } }
@Test public void testOrderByNatural() throws IOException, URISyntaxException { //Given Path file = Paths.get(AsciidocConverterTest.class.getResource("/yaml/swagger_ordering.yaml").toURI()); Path outputDirectory = Paths.get("build/test/asciidoc/ordering_natural"); FileUtils.deleteQuietly(outputDirectory.toFile()); Swagger2MarkupConfig config = new Swagger2MarkupConfigBuilder() .withTagOrdering(OrderBy.NATURAL) .withParameterOrdering(OrderBy.NATURAL) .withOperationOrdering(OrderBy.NATURAL) .withPathsGroupedBy(GroupBy.TAGS) .build(); //When Swagger2MarkupConverter.from(file).withConfig(config).build() .toFolder(outputDirectory); //Then String[] files = outputDirectory.toFile().list(); assertThat(files).hasSize(4).containsAll(expectedFiles); Path expectedFilesDirectory = Paths.get(AsciidocConverterTest.class.getResource("/expected/asciidoc/ordering_natural").toURI()); DiffUtils.assertThatAllFilesAreEqual(expectedFilesDirectory, outputDirectory, "testOrderingNatural.html"); }
@Test public void testOrderByAsIs() throws IOException, URISyntaxException { //Given Path file = Paths.get(AsciidocConverterTest.class.getResource("/yaml/swagger_ordering.yaml").toURI()); Path outputDirectory = Paths.get("build/test/asciidoc/ordering_asis"); FileUtils.deleteQuietly(outputDirectory.toFile()); //When Swagger2MarkupConfig config = new Swagger2MarkupConfigBuilder() .withTagOrdering(OrderBy.AS_IS) .withParameterOrdering(OrderBy.AS_IS) .withOperationOrdering(OrderBy.AS_IS) .withDefinitionOrdering(OrderBy.AS_IS) .withPathsGroupedBy(GroupBy.TAGS) .build(); Swagger2MarkupConverter.from(file).withConfig(config).build() .toFolder(outputDirectory); //Then String[] files = outputDirectory.toFile().list(); assertThat(files).hasSize(4).containsAll(expectedFiles); Path expectedFilesDirectory = Paths.get(AsciidocConverterTest.class.getResource("/expected/asciidoc/ordering_asis").toURI()); DiffUtils.assertThatAllFilesAreEqual(expectedFilesDirectory, outputDirectory, "testOrderingAsIs.html"); }
@Test public void testOrderByRegex() throws IOException, URISyntaxException { //Given Path file = Paths.get(AsciidocConverterTest.class.getResource("/yaml/swagger_ordering_regex.yaml").toURI()); Path outputDirectory = Paths.get("build/test/asciidoc/ordering_regex"); FileUtils.deleteQuietly(outputDirectory.toFile()); Swagger2MarkupConfig config = new Swagger2MarkupConfigBuilder() .withTagOrdering(OrderBy.NATURAL) .withParameterOrdering(OrderBy.NATURAL) .withOperationOrdering(OrderBy.NATURAL) .withPathsGroupedBy(GroupBy.REGEX) .withHeaderRegex("\\/(\\w+)(\\/|\\w)*$") .build(); //When Swagger2MarkupConverter.from(file).withConfig(config).build() .toFolder(outputDirectory); //Then String[] files = outputDirectory.toFile().list(); assertThat(files).hasSize(4).containsAll(expectedFiles); Path expectedFilesDirectory = Paths.get(AsciidocConverterTest.class.getResource("/expected/asciidoc/ordering_regex").toURI()); DiffUtils.assertThatAllFilesAreEqual(expectedFilesDirectory, outputDirectory, "testOrderingRegex.html"); }
public void swagger2MarkupConfigBuilder() { Path localSwaggerFile = Paths.get("/path/to/swagger.yaml"); // tag::swagger2MarkupConfigBuilder[] Swagger2MarkupConfig config = new Swagger2MarkupConfigBuilder() //<1> .withMarkupLanguage(MarkupLanguage.MARKDOWN) //<2> .withOutputLanguage(Language.DE) //<3> .withPathsGroupedBy(GroupBy.TAGS) //<4> .build(); //<5> Swagger2MarkupConverter converter = Swagger2MarkupConverter.from(localSwaggerFile) .withConfig(config) //<6> .build(); // end::swagger2MarkupConfigBuilder[] }
@Override public String convertSwaggerToAsciiDoc(final String swaggerSchemaPath) { final File swaggerSchemaFile; LOG.info("Converting Swagger Schema to Ascii Doc..."); try { swaggerSchemaFile = getSchemaFile(swaggerSchemaPath); } catch (final FileNotFoundException | URISyntaxException e) { throw new SwaggerConfluenceConfigurationException("Error Locating Swagger Schema", e); } final String swaggerAsciiDoc; try { final Swagger2MarkupConfig config = new Swagger2MarkupConfigBuilder() .withMarkupLanguage(MarkupLanguage.ASCIIDOC) .withOutputLanguage(Language.EN) .withPathsGroupedBy(GroupBy.AS_IS) .withOperationOrdering(OrderBy.AS_IS) .build(); final String swaggerSchema = FileUtils.readFileToString(swaggerSchemaFile, StandardCharsets.UTF_8); swaggerAsciiDoc = Swagger2MarkupConverter.from(swaggerSchema) .withConfig(config) .build() .toString(); } catch (IOException e) { throw new SwaggerConfluenceInternalSystemException( "Error Converting Swagger Schema to AsciiDoc", e); } LOG.info("AsciiDoc Conversion Complete!"); return swaggerAsciiDoc; }