Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions springdoc-openapi-gradle-plugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,8 @@ publishing {

test {
useJUnitPlatform()
// Expose the resolved project version so the functional tests depend on the
// artifacts that were actually built/installed (e.g. 5.1.0 at release time,
// 5.1.0-SNAPSHOT during development) instead of a hardcoded version.
systemProperty 'springdocVersion', project.version
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@

import org.gradle.testkit.runner.BuildResult;
import org.gradle.testkit.runner.GradleRunner;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
Expand All @@ -21,40 +19,23 @@ class SpringDocOpenApiGradlePluginFunctionalTest {
@TempDir
Path testProjectDir;

private Path sampleApp;

@BeforeEach
void setUp() throws IOException {
sampleApp = Paths.get("src/test/resources/sample-app-webflux");
}
// Resolved version of the springdoc artifacts that were actually built/installed
// (e.g. 5.1.0 at release time, 5.1.0-SNAPSHOT during development). Passed in via the
// springdocVersion system property by build.gradle, falling back to the snapshot for
// local development.
private static final String SPRINGDOC_VERSION =
System.getProperty("springdocVersion", "5.1.0-SNAPSHOT");

@Test
void generatesOpenApiSpecFromReactiveApp() throws IOException {
// Copy sample app into the temp project
Path sampleApp = Paths.get("src/test/resources/sample-app-webflux");
copyRecursively(sampleApp, testProjectDir);

// Add settings + build for the sample app, using the plugin under test
Files.writeString(testProjectDir.resolve("settings.gradle"), "rootProject.name = 'sample-app-webflux'\n");
Files.writeString(testProjectDir.resolve("build.gradle"), """
plugins {
id 'java'
id 'io.github.vpelikh.springdoc-openapi-gradle-plugin'
}

repositories {
mavenLocal()
mavenCentral()
}

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-webflux:4.1.1'
implementation 'io.github.vpelikh:springdoc-openapi-starter-webflux-api:5.1.0-SNAPSHOT'
}

openApiGenerate {
mainClass = 'test.SampleApp'
}
""");
Files.writeString(testProjectDir.resolve("build.gradle"),
buildGradle("webflux", """
mainClass = 'test.SampleApp'
"""));

BuildResult result = GradleRunner.create()
.withProjectDir(testProjectDir.toFile())
Expand All @@ -75,29 +56,13 @@ void generatesOpenApiSpecFromServletApp() throws IOException {
copyRecursively(Paths.get("src/test/resources/sample-app-webmvc"), testProjectDir);

Files.writeString(testProjectDir.resolve("settings.gradle"), "rootProject.name = 'sample-app-webmvc'\n");
Files.writeString(testProjectDir.resolve("build.gradle"), """
plugins {
id 'java'
id 'io.github.vpelikh.springdoc-openapi-gradle-plugin'
}

repositories {
mavenLocal()
mavenCentral()
}

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web:4.1.1'
implementation 'io.github.vpelikh:springdoc-openapi-starter-webmvc-api:5.1.0-SNAPSHOT'
}

openApiGenerate {
mainClass = 'test.SampleApp'
systemProperties = [
'spring.main.banner-mode': 'off'
]
}
""");
Files.writeString(testProjectDir.resolve("build.gradle"),
buildGradle("webmvc", """
mainClass = 'test.SampleApp'
systemProperties = [
'spring.main.banner-mode': 'off'
]
"""));

BuildResult result = GradleRunner.create()
.withProjectDir(testProjectDir.toFile())
Expand All @@ -114,30 +79,15 @@ void generatesOpenApiSpecFromServletApp() throws IOException {

@Test
void skipFlagProducesNoSpec() throws IOException {
Path sampleApp = Paths.get("src/test/resources/sample-app-webflux");
copyRecursively(sampleApp, testProjectDir);

Files.writeString(testProjectDir.resolve("settings.gradle"), "rootProject.name = 'sample-app-skip'\n");
Files.writeString(testProjectDir.resolve("build.gradle"), """
plugins {
id 'java'
id 'io.github.vpelikh.springdoc-openapi-gradle-plugin'
}

repositories {
mavenLocal()
mavenCentral()
}

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-webflux:4.1.1'
implementation 'io.github.vpelikh:springdoc-openapi-starter-webflux-api:5.1.0-SNAPSHOT'
}

openApiGenerate {
mainClass = 'test.SampleApp'
skip = true
}
""");
Files.writeString(testProjectDir.resolve("build.gradle"),
buildGradle("webflux", """
mainClass = 'test.SampleApp'
skip = true
"""));

BuildResult result = GradleRunner.create()
.withProjectDir(testProjectDir.toFile())
Expand All @@ -154,30 +104,15 @@ void skipFlagProducesNoSpec() throws IOException {

@Test
void generatesOpenApiYamlFormat() throws IOException {
Path sampleApp = Paths.get("src/test/resources/sample-app-webflux");
copyRecursively(sampleApp, testProjectDir);

Files.writeString(testProjectDir.resolve("settings.gradle"), "rootProject.name = 'sample-app-yaml'\n");
Files.writeString(testProjectDir.resolve("build.gradle"), """
plugins {
id 'java'
id 'io.github.vpelikh.springdoc-openapi-gradle-plugin'
}

repositories {
mavenLocal()
mavenCentral()
}

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-webflux:4.1.1'
implementation 'io.github.vpelikh:springdoc-openapi-starter-webflux-api:5.1.0-SNAPSHOT'
}

openApiGenerate {
mainClass = 'test.SampleApp'
format = 'yaml'
}
""");
Files.writeString(testProjectDir.resolve("build.gradle"),
buildGradle("webflux", """
mainClass = 'test.SampleApp'
format = 'yaml'
"""));

BuildResult result = GradleRunner.create()
.withProjectDir(testProjectDir.toFile())
Expand All @@ -197,6 +132,36 @@ void generatesOpenApiYamlFormat() throws IOException {
"expected YAML output but got JSON: " + content);
}

/**
* Builds the sample app build.gradle for the functional tests.
* @param starterModule springdoc starter module suffix, e.g. "webflux" or "webmvc".
* @param openApiExtension the extra {@code openApiGenerate { ... }} block body.
*/
private String buildGradle(String starterModule, String openApiExtension) {
String bootDependency = "implementation 'org.springframework.boot:spring-boot-starter-" + starterModule + ":4.1.1'\n";
String springdocDependency =
"implementation 'io.github.vpelikh:springdoc-openapi-starter-" + starterModule + "-api:" + SPRINGDOC_VERSION + "'\n";
return """
plugins {
id 'java'
id 'io.github.vpelikh.springdoc-openapi-gradle-plugin'
}

repositories {
mavenLocal()
mavenCentral()
}

dependencies {
""" + bootDependency + springdocDependency + """
}

openApiGenerate {
""" + openApiExtension + """
}
""";
}

private void copyRecursively(Path source, Path target) throws IOException {
try (var stream = Files.walk(source)) {
for (Path src : (Iterable<Path>) stream::iterator) {
Expand All @@ -211,4 +176,4 @@ private void copyRecursively(Path source, Path target) throws IOException {
}
}
}
}
}
Loading