Skip to content

Commit

Permalink
Add example configuration for springdoc-openapi-gradle-plugin to spri…
Browse files Browse the repository at this point in the history
…ngwolf-kafka-example, use springdoc-openapi-gradle-plugin to generate the api spec before test execution and added a test to ensure that the plugin still works for springwolf
  • Loading branch information
sam0r040 committed Jan 23, 2023
1 parent 42d0730 commit 684d7fa
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
11 changes: 11 additions & 0 deletions springwolf-examples/springwolf-kafka-example/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ plugins {
id 'io.spring.dependency-management' version '1.1.0'

id 'com.bmuschko.docker-spring-boot-application' version '6.7.0'
id 'org.springdoc.openapi-gradle-plugin' version '1.6.0'
}

sourceCompatibility = '1.8'
Expand Down Expand Up @@ -67,3 +68,13 @@ test {
exceptionFormat = 'full'
}
}

openApi {
apiDocsUrl = "http://localhost:8080/springwolf/docs"
// For testing purposes we put the generated json into the test resources, but it could be any other directory
outputDir = file("$buildDir/resources/test")
outputFileName = "openapi-generated.json"
}

// generate the open api docs before tests are executed so that if it works, the json is already there
test.dependsOn("generateOpenApiDocs")
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package io.github.stavshamir.springwolf.example;

import org.json.JSONException;
import org.junit.jupiter.api.Test;
import org.skyscreamer.jsonassert.JSONAssert;
import org.skyscreamer.jsonassert.JSONCompareMode;
import org.testcontainers.shaded.org.apache.commons.io.IOUtils;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

/**
* This test is strongly coupled with the configuration of the springdoc openapi plugin in build.gradle
* <p>
* The gradle project is configured tp run the generateOpenApiDocs task before the test task.
* The generateOpenApiDocs task will then generate the openapi-generated.json into the test-resources so that
* this test can pick up the openapi-generated.json and compare it to the reference asyncapi.json
*/
public class OpenApiGeneratorTest {

@Test
public void asyncApiResourceArtifactTest() throws JSONException, IOException {

InputStream expectedStream = this.getClass().getResourceAsStream("/asyncapi.json");

// When running with EmbeddedKafka, localhost is used as hostname
String expectedWithoutServersKafkaUrlPatch = IOUtils.toString(expectedStream, StandardCharsets.UTF_8);
String expected = expectedWithoutServersKafkaUrlPatch.replace("kafka:29092", "localhost:29092");

InputStream actualStream = this.getClass().getResourceAsStream("/openapi-generated.json");
String actual = IOUtils.toString(actualStream, StandardCharsets.UTF_8);

JSONAssert.assertEquals(expected, actual, JSONCompareMode.STRICT_ORDER);
}
}

0 comments on commit 684d7fa

Please sign in to comment.