@Test public void testWithInterDocumentCrossReferencesAndNoOutputPath() { Swagger2MarkupConfig config = new Swagger2MarkupConfigBuilder() .withInterDocumentCrossReferences() .build(); Swagger2MarkupConverter.Context context = createContext(config); assertThat(new DefinitionDocumentNameResolver(context).apply("DefinitionName")) .isEqualTo("definitions.adoc"); }
/** * Builds the Swagger2MarkupConfig. * * @return the Swagger2MarkupConfig */ public Swagger2MarkupConfig build() { buildNaturalOrdering(); return config; }
@Test public void testWithInterDocumentCrossReferencesAndMarkdown() { Swagger2MarkupConfig config = new Swagger2MarkupConfigBuilder() .withInterDocumentCrossReferences() .withMarkupLanguage(MarkupLanguage.MARKDOWN) .build(); Swagger2MarkupConverter.Context context = createContext(config); context.setOutputPath(Paths.get("/tmp")); assertThat(new DefinitionDocumentNameResolver(context).apply("DefinitionName")) .isEqualTo("definitions.md"); } }
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[] }
@Test public void testWithSeparatedOperationsAndInterDocumentCrossReferences() { Swagger2MarkupConfig config = new Swagger2MarkupConfigBuilder() .withSeparatedOperations() .withInterDocumentCrossReferences() .build(); Swagger2MarkupConverter.Context context = createContext(config); assertThat(new OperationDocumentNameResolver(context).apply(operation)) .isEqualTo("operations" + fileSeparator + "test_get.adoc"); }
public Swagger2MarkupConverter build() { if (config == null) config = new Swagger2MarkupConfigBuilder().build(); if (extensionRegistry == null) extensionRegistry = new Swagger2MarkupExtensionRegistryBuilder().build(); Context context = new Context(config, extensionRegistry, swagger, swaggerLocation); initExtensions(context); applySwaggerExtensions(context); return new Swagger2MarkupConverter(context); }
@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 testWithMinMaxItems() throws IOException, URISyntaxException { //Given Path file = Paths.get(MarkdownConverterTest.class.getResource("/yaml/swagger_petstore_min_max_items.yaml").toURI()); Path outputDirectory = Paths.get("build/test/markdown/to_folder"); FileUtils.deleteQuietly(outputDirectory.toFile()); //When Swagger2MarkupConfig config = new Swagger2MarkupConfigBuilder() .withMarkupLanguage(MarkupLanguage.MARKDOWN) .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/markdown/min_max_items").toURI()); DiffUtils.assertThatAllFilesAreEqual(expectedFilesDirectory, outputDirectory, "min_max_items.html"); }
@Test public void testWithSeparatedDefinitions() { Swagger2MarkupConfig config = new Swagger2MarkupConfigBuilder() .withSeparatedDefinitions() .build(); Swagger2MarkupConverter.Context context = createContext(config); assertThat(new DefinitionDocumentResolverDefault(context).apply("DefinitionName")).isNull(); }
@Test public void testFreeFormWithEmptyCurlyBracket() throws IOException, URISyntaxException { //Given Path file = Paths.get(MarkdownConverterTest.class.getResource("/yaml/swagger_freeform_with_emtpy_curly_brackets.yaml").toURI()); Path outputDirectory = Paths.get("build/test/markdown/freeform"); FileUtils.deleteQuietly(outputDirectory.toFile()); //When Swagger2MarkupConfig config = new Swagger2MarkupConfigBuilder() .withSeparatedDefinitions() .withMarkupLanguage(MarkupLanguage.MARKDOWN) .build(); Swagger2MarkupConverter.from(file) .withConfig(config) .build() .toFolder(outputDirectory); // Then String[] files = outputDirectory.toFile().list(); expectedFiles.add("definitions"); assertThat(files).hasSize(5).containsAll(expectedFiles); Path expectedFilesDirectory = Paths.get(MarkdownConverterTest.class.getResource("/expected/markdown/freeform").toURI()); DiffUtils.assertThatAllFilesAreEqual(expectedFilesDirectory, outputDirectory, "freeform.html"); } }
@Test public void testWithSeparatedOperations() { Swagger2MarkupConfig config = new Swagger2MarkupConfigBuilder() .withSeparatedOperations() .build(); Swagger2MarkupConverter.Context context = createContext(config); assertThat(new OperationDocumentNameResolver(context).apply(operation)) .isEqualTo("operations" + fileSeparator + "test_get.adoc"); }
@Test public void testWithGeneratedExamples() throws IOException, URISyntaxException { //Given String swaggerJsonString = IOUtils.toString(getClass().getResourceAsStream("/json/swagger_examples.json")); Path outputDirectory = Paths.get("build/test/asciidoc/generated_examples"); FileUtils.deleteQuietly(outputDirectory.toFile()); //When Swagger2MarkupConfig config = new Swagger2MarkupConfigBuilder() .withGeneratedExamples() .build(); Swagger2MarkupConverter.from(swaggerJsonString) .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/generated_examples").toURI()); DiffUtils.assertThatAllFilesAreEqual(expectedFilesDirectory, outputDirectory, "testWithGeneratedExamples.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"); } }
private void testWithOutputLanguage(Language language, String outputFilename, String expected) throws IOException, URISyntaxException { //Given Path file = Paths.get(AsciidocConverterTest.class.getResource("/yaml/swagger_petstore.yaml").toURI()); Path outputDirectory = Paths.get("build/test/asciidoc/language"); FileUtils.deleteQuietly(outputDirectory.toFile()); //When Swagger2MarkupConfig config = new Swagger2MarkupConfigBuilder() .withOutputLanguage(language) .build(); Swagger2MarkupConverter.from(file) .withConfig(config) .build() .toFolder(outputDirectory); //Then assertThat(new String(Files.readAllBytes(outputDirectory.resolve(outputFilename)), Charset.forName("UTF-8"))) .contains(expected); }
@Test public void testWithGeneratedRecursiveExamples() throws IOException, URISyntaxException { // Given String swaggerJsonString = IOUtils.toString(getClass().getResourceAsStream("/json/swagger_recursion.json")); Path outputDirectory = Paths.get("build/test/asciidoc/generated_recursion_examples"); FileUtils.deleteQuietly(outputDirectory.toFile()); // When Swagger2MarkupConfig config = new Swagger2MarkupConfigBuilder().withoutInlineSchema().withGeneratedExamples().build(); Swagger2MarkupConverter.from(swaggerJsonString).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/generated_recursion_examples").toURI()); DiffUtils.assertThatAllFilesAreEqual(expectedFilesDirectory, outputDirectory, "testWithGeneratedRecursiveExamples.html"); }
@Test public void testWithBasePathPrefix() throws IOException, URISyntaxException { //Given Path file = Paths.get(AsciidocConverterTest.class.getResource("/json/swagger_examples.json").toURI()); Path outputDirectory = Paths.get("build/test/asciidoc/basepathprefix"); FileUtils.deleteQuietly(outputDirectory.toFile()); //When Swagger2MarkupConfig config = new Swagger2MarkupConfigBuilder() .withBasePathPrefix() .withGeneratedExamples() .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/basepathprefix").toURI()); DiffUtils.assertThatAllFilesAreEqual(expectedFilesDirectory, outputDirectory, "testWithBasePathPrefix.html"); }
@Test public void testMarkupRenderingInInstagram() throws IOException, URISyntaxException { //Given Path file = Paths.get(AsciidocConverterTest.class.getResource("/yaml/swagger_instagram.yaml").toURI()); Path outputDirectory = Paths.get("build/test/asciidoc/instagram"); FileUtils.deleteQuietly(outputDirectory.toFile()); //When Swagger2MarkupConfig config = new Swagger2MarkupConfigBuilder() .withTagOrdering(OrderBy.AS_IS) .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/instagram").toURI()); DiffUtils.assertThatAllFilesAreEqual(expectedFilesDirectory, outputDirectory, "testMarkupRenderingInInstagram.html"); }
@Test public void testWithInlineSchemaAndFlatBody() throws IOException, URISyntaxException { //Given Path file = Paths.get(AsciidocConverterTest.class.getResource("/yaml/swagger_inlineSchema.yaml").toURI()); Path outputDirectory = Paths.get("build/test/asciidoc/inline_schema_flat_body"); FileUtils.deleteQuietly(outputDirectory.toFile()); //When Swagger2MarkupConfig config = new Swagger2MarkupConfigBuilder() .withFlatBody() .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/inline_schema_flat_body").toURI()); DiffUtils.assertThatAllFilesAreEqual(expectedFilesDirectory, outputDirectory, "testWithInlineSchemaAndFlatBody.html"); }
@Test public void testWithPageBreaks() throws IOException, URISyntaxException { //Given String swaggerJsonString = IOUtils.toString(getClass().getResourceAsStream("/json/swagger_examples.json")); Path outputDirectory = Paths.get("build/test/asciidoc/page_breaks"); FileUtils.deleteQuietly(outputDirectory.toFile()); //When Swagger2MarkupConfig config = new Swagger2MarkupConfigBuilder() .withPageBreaks(new ArrayList<>(asList(PageBreakLocations.BEFORE_OPERATION, PageBreakLocations.BEFORE_OPERATION_EXAMPLE_REQUEST))) .build(); Swagger2MarkupConverter.from(swaggerJsonString) .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/page_breaks").toURI()); DiffUtils.assertThatAllFilesAreEqual(expectedFilesDirectory, outputDirectory, "testWithPageBreaks.html"); }
@Test public void testExamplesInUrl() throws IOException, URISyntaxException { //Given String swaggerJsonString = IOUtils.toString(getClass().getResourceAsStream("/yaml/swagger_url_examples.yaml")); Path outputDirectory = Paths.get("build/test/asciidoc/url_examples"); FileUtils.deleteQuietly(outputDirectory.toFile()); //When Swagger2MarkupConfig config = new Swagger2MarkupConfigBuilder() .withPageBreaks(new ArrayList<>(asList(PageBreakLocations.BEFORE_OPERATION, PageBreakLocations.BEFORE_OPERATION_EXAMPLE_REQUEST))) .withGeneratedExamples() .build(); Swagger2MarkupConverter.from(swaggerJsonString) .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/url_examples").toURI()); DiffUtils.assertThatAllFilesAreEqual(expectedFilesDirectory, outputDirectory, "testExamplesInUrl.html"); } }