Skip to content

Commit 399bf05

Browse files
authored
Add example configuration for springdoc-openapi-gradle-plugin to springwolf-kafka-example
- Use springdoc-openapi-gradle-plugin to generate the api spec before test execution - Add a test to ensure that the plugin still works for springwolf (#134)
1 parent ee8da96 commit 399bf05

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

springwolf-examples/springwolf-kafka-example/build.gradle

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ plugins {
55
id 'io.spring.dependency-management' version '1.1.0'
66

77
id 'com.bmuschko.docker-spring-boot-application' version '6.7.0'
8+
id 'org.springdoc.openapi-gradle-plugin' version '1.6.0'
89
}
910

1011
sourceCompatibility = '1.8'
@@ -67,3 +68,13 @@ test {
6768
exceptionFormat = 'full'
6869
}
6970
}
71+
72+
openApi {
73+
apiDocsUrl = "http://localhost:8080/springwolf/docs"
74+
// For testing purposes we put the generated json into the test resources, but it could be any other directory
75+
outputDir = file("$buildDir/resources/test")
76+
outputFileName = "openapi-generated.json"
77+
}
78+
79+
// generate the open api docs before tests are executed so that if it works, the json is already there
80+
test.dependsOn("generateOpenApiDocs")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package io.github.stavshamir.springwolf.example;
2+
3+
import org.json.JSONException;
4+
import org.junit.jupiter.api.Test;
5+
import org.skyscreamer.jsonassert.JSONAssert;
6+
import org.skyscreamer.jsonassert.JSONCompareMode;
7+
import org.testcontainers.shaded.org.apache.commons.io.IOUtils;
8+
9+
import java.io.IOException;
10+
import java.io.InputStream;
11+
import java.nio.charset.StandardCharsets;
12+
13+
/**
14+
* This test is strongly coupled with the configuration of the springdoc openapi plugin in build.gradle
15+
* <p>
16+
* The gradle project is configured to run the generateOpenApiDocs task before the test task.
17+
* The generateOpenApiDocs task will generate the openapi-generated.json into the test-resources so that
18+
* this test can pick up the openapi-generated.json afterwards and compare it to the reference asyncapi.json
19+
*/
20+
public class OpenApiGeneratorTest {
21+
22+
@Test
23+
public void asyncApiResourceArtifactTest() throws JSONException, IOException {
24+
25+
InputStream expectedStream = this.getClass().getResourceAsStream("/asyncapi.json");
26+
27+
// When running with EmbeddedKafka, localhost is used as hostname
28+
String expectedWithoutServersKafkaUrlPatch = IOUtils.toString(expectedStream, StandardCharsets.UTF_8);
29+
String expected = expectedWithoutServersKafkaUrlPatch.replace("kafka:29092", "localhost:29092");
30+
31+
InputStream actualStream = this.getClass().getResourceAsStream("/openapi-generated.json");
32+
String actual = IOUtils.toString(actualStream, StandardCharsets.UTF_8);
33+
34+
JSONAssert.assertEquals(expected, actual, JSONCompareMode.STRICT_ORDER);
35+
}
36+
}

0 commit comments

Comments
 (0)