TaskGenerateOpenApi extends AbstractTaskConnectGenerator { @Override public void execute() throws ExecutionFailedException { OpenApiSpecGenerator openApiSpecGenerator = new OpenApiSpecGenerator( readApplicationProperties()); openApiSpecGenerator.generateOpenApiSpec( Collections.singletonList(javaSourceFolder.toPath()), classLoader, output.toPath()); } TaskGenerateOpenApi(File properties, File javaSourceFolder,
            ClassLoader classLoader, File output); @Override void execute();  }
@Test public void should_UseDefaultProperties_when_applicationPropertiesIsEmpty() throws Exception { taskGenerateOpenApi = new TaskGenerateOpenApi(applicationPropertiesFile, javaSource, this.getClass().getClassLoader(), generatedOpenAPI); taskGenerateOpenApi.execute(); OpenAPI generatedOpenAPI = getGeneratedOpenAPI(); Info info = generatedOpenAPI.getInfo(); Assert.assertEquals( "Generated OpenAPI should have default application title", OpenApiSpecGenerator.DEFAULT_APPLICATION_TITLE, info.getTitle()); Assert.assertEquals( "Generated OpenAPI should have default " + "application API version", OpenApiSpecGenerator.DEFAULT_APPLICATION_API_VERSION, info.getVersion()); List<Server> servers = generatedOpenAPI.getServers(); Assert.assertEquals("Generated OpenAPI should a default server", 1, servers.size()); Assert.assertEquals("Generated OpenAPI should have default url server", OpenApiSpecGenerator.DEFAULT_SERVER + OpenApiSpecGenerator.DEFAULT_PREFIX, servers.get(0).getUrl()); Assert.assertEquals( "Generated OpenAPI should have default server description", OpenApiSpecGenerator.DEFAULT_SERVER_DESCRIPTION, servers.get(0).getDescription()); }
@Test public void should_UseGivenProperties_when_applicationPropertiesDefinesThem() throws Exception { String applicationTitle = "My title"; String applicationAPIVersion = "1.1.1"; String applicationServer = "https: String applicationPrefix = "/api"; String applicationServerDescription = "Example API server"; StringBuilder applicationPropertiesBuilder = new StringBuilder(); applicationPropertiesBuilder .append(OpenApiSpecGenerator.APPLICATION_TITLE).append("=") .append(applicationTitle).append("\n") .append(OpenApiSpecGenerator.APPLICATION_API_VERSION) .append("=").append(applicationAPIVersion).append("\n") .append(OpenApiSpecGenerator.SERVER).append("=") .append(applicationServer).append("\n") .append(OpenApiSpecGenerator.PREFIX).append("=") .append(applicationPrefix).append("\n") .append(OpenApiSpecGenerator.SERVER_DESCRIPTION).append("=") .append(applicationServerDescription); FileUtils.writeStringToFile(applicationPropertiesFile, applicationPropertiesBuilder.toString(), StandardCharsets.UTF_8); taskGenerateOpenApi = new TaskGenerateOpenApi(applicationPropertiesFile, javaSource, this.getClass().getClassLoader(), generatedOpenAPI); taskGenerateOpenApi.execute(); OpenAPI generatedOpenAPI = getGeneratedOpenAPI(); Info info = generatedOpenAPI.getInfo(); Assert.assertEquals( "Generated OpenAPI should use given application title", applicationTitle, info.getTitle()); Assert.assertEquals( "Generated OpenAPI should use given" + "application API version", applicationAPIVersion, info.getVersion()); List<Server> servers = generatedOpenAPI.getServers(); Assert.assertEquals("Generated OpenAPI should a defined server", 1, servers.size()); Assert.assertEquals("Generated OpenAPI should use given url server", applicationServer + applicationPrefix, servers.get(0).getUrl()); Assert.assertEquals( "Generated OpenAPI should use given server description", applicationServerDescription, servers.get(0).getDescription()); }
@Test public void should_UseCustomEndpointName_InsteadOf_UsingClassName() throws Exception { taskGenerateOpenApi = new TaskGenerateOpenApi(applicationPropertiesFile, javaSource, this.getClass().getClassLoader(), generatedOpenAPI); taskGenerateOpenApi.execute(); OpenAPI generatedOpenAPI = getGeneratedOpenAPI(); Assert.assertTrue(generatedOpenAPI.getPaths().containsKey("/CustomEndpointName/bar")); Assert.assertFalse(generatedOpenAPI.getPaths().containsKey("/CustomEndpoint/bar")); Assert.assertTrue(generatedOpenAPI.getPaths().containsKey("/CustomEndpointName/foo")); Assert.assertFalse(generatedOpenAPI.getPaths().containsKey("/CustomEndpoint/foo")); }
@Test public void should_UseCustomEndpointNameWithoutValueEqual_InsteadOf_UsingClassName() throws Exception { taskGenerateOpenApi = new TaskGenerateOpenApi(applicationPropertiesFile, javaSource, this.getClass().getClassLoader(), generatedOpenAPI); taskGenerateOpenApi.execute(); OpenAPI generatedOpenAPI = getGeneratedOpenAPI(); Assert.assertTrue(generatedOpenAPI.getPaths().containsKey("/WithoutValueEqual/bar")); Assert.assertFalse(generatedOpenAPI.getPaths().containsKey("/EndpointNoValue/bar")); Assert.assertTrue(generatedOpenAPI.getPaths().containsKey("/WithoutValueEqual/foo")); Assert.assertFalse(generatedOpenAPI.getPaths().containsKey("/EndpointNoValue/foo")); }