Skip to content

Commit

Permalink
Add WebFlux test for YAML support, completes #14
Browse files Browse the repository at this point in the history
  • Loading branch information
neiser committed Dec 6, 2020
1 parent 8d47430 commit 06c57d1
Show file tree
Hide file tree
Showing 12 changed files with 163 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.reactive.server.WebTestClient;
import org.springframework.test.web.reactive.server.WebTestClient.RequestHeadersSpec;
import org.springframework.test.web.reactive.server.WebTestClient.ResponseSpec;
import org.springframework.web.util.UriBuilder;

import java.util.function.Function;

import static org.assertj.core.api.Assertions.assertThat;

Expand All @@ -19,13 +24,27 @@ public abstract class AbstractOpenApiGeneratorWebFluxBaseIntTest {
@Autowired
protected WebTestClient webTestClient;

protected static void assertResponseBodyMatchesOpenApiJson(String expectedJsonFile, WebTestClient.RequestHeadersSpec<?> performResult) throws Exception {
protected static void assertResponseBodyMatchesOpenApiJson(String expectedJsonFile, ResponseSpec performResult) throws Exception {
OpenApiJsonIntegrationTestUtils.assertMatchesOpenApiJson(expectedJsonFile, () -> getResponseBodyAsString(performResult));
}

protected static String getResponseBodyAsString(WebTestClient.RequestHeadersSpec<?> performResult) {
protected static void assertResponseBodyMatchesOpenApiYaml(String expectedYamlFile, ResponseSpec performResult) throws Exception {
OpenApiJsonIntegrationTestUtils.assertMatchesOpenApiYaml(expectedYamlFile, () -> getResponseBodyAsString(performResult));
}

protected ResponseSpec performApiDocsRequest(
Function<UriBuilder, UriBuilder> uriModifier,
Function<RequestHeadersSpec<?>, RequestHeadersSpec<?>> requestModifier
) {
return requestModifier.apply(
webTestClient.get().uri(
uriBuilder -> uriModifier.apply(uriBuilder.path("/v3/api-docs")).build()
)
).exchange();
}

protected static String getResponseBodyAsString(ResponseSpec performResult) {
byte[] responseBody = performResult
.exchange()
.expectStatus().isOk()
.expectBody().returnResult()
.getResponseBody();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ protected AbstractOpenApiGeneratorWebFluxIntTest(String expectedJsonFile) {

@Test
public void getOpenApiAsJson() throws Exception {
assertResponseBodyMatchesOpenApiJson(expectedJsonFile, webTestClient.get().uri("/v3/api-docs"));
assertResponseBodyMatchesOpenApiJson(expectedJsonFile, performApiDocsRequest(x -> x, x -> x));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,14 @@ public class App18Test extends AbstractOpenApiGeneratorWebFluxBaseIntTest {
@Test
public void testWithQueryParam() throws Exception {
assertResponseBodyMatchesOpenApiJson("app18_admin.json",
webTestClient.get().uri(uriBuilder -> uriBuilder
.path("/v3/api-docs")
.queryParam("pathPrefix", "/admin")
.build()
)
performApiDocsRequest(x -> x.queryParam("pathPrefix", "/admin"), x -> x)
);
}

@Test
public void testWithHeaderParam() throws Exception {
assertResponseBodyMatchesOpenApiJson("app18_user.json",
webTestClient.get().uri(uriBuilder -> uriBuilder
.path("/v3/api-docs")
.build()
).header("X-Path-Prefix", "/user")
performApiDocsRequest(x -> x, x -> x.header("X-Path-Prefix", "/user"))
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@
public class App23Test extends AbstractOpenApiGeneratorWebFluxBaseIntTest {

@Test
public void testOpenApiIsNotFoundWhenDisabled() throws Exception {
webTestClient.get().uri(uriBuilder -> uriBuilder
.path("/v3/api-docs")
.build())
.exchange()
.expectStatus().isNotFound();
public void testOpenApiIsNotFoundWhenDisabled() {
performApiDocsRequest(x -> x, x -> x).expectStatus().isNotFound();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@ public class App25Test extends AbstractOpenApiGeneratorWebFluxBaseIntTest {

@Test
public void testSwaggerUiIsNotFoundWhenDisabled() throws Exception {
webTestClient.get().uri(uriBuilder -> uriBuilder
.path("/v3/api-docs")
.build())
.exchange()
.expectStatus().isOk();
performApiDocsRequest(x -> x, x -> x).expectStatus().isOk();
webTestClient.get().uri(uriBuilder -> uriBuilder
.path("/swagger-ui/index.html")
.build())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package de.qaware.openapigeneratorforspring.test.app37;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class App37 {
public static void main(String[] args) {
SpringApplication.run(App37.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package de.qaware.openapigeneratorforspring.test.app37;

import de.qaware.openapigeneratorforspring.test.AbstractOpenApiGeneratorWebFluxBaseIntTest;
import org.junit.Test;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;

public class App37Test extends AbstractOpenApiGeneratorWebFluxBaseIntTest {

private static final String YAML_FILE = "app37.yaml";

@Test
public void getOpenApiAsJson_withAcceptTextHtml() throws Exception {
performApiDocsRequest(x -> x, x -> x.accept(new MediaType("text", "html")))
.expectStatus().isEqualTo(HttpStatus.NOT_ACCEPTABLE);
}

@Test
public void getOpenApiAsJson_withAcceptApplicationYaml() throws Exception {
assertResponseBodyMatchesOpenApiYaml(YAML_FILE,
performApiDocsRequest(x -> x, x -> x.accept(new MediaType("application", "yaml")))
);
}

@Test
public void getOpenApiAsJson_withAcceptApplicationXYaml() throws Exception {
assertResponseBodyMatchesOpenApiYaml(YAML_FILE,
performApiDocsRequest(x -> x, x -> x.accept(new MediaType("application", "yaml")))
);
}

@Test
public void getOpenApiAsJson_withAcceptTextYaml() throws Exception {
assertResponseBodyMatchesOpenApiYaml(YAML_FILE,
performApiDocsRequest(x -> x, x -> x.accept(new MediaType("text", "yaml")))
);
}

@Test
public void getOpenApiAsJson_withAcceptTextXYaml() throws Exception {
assertResponseBodyMatchesOpenApiYaml(YAML_FILE,
performApiDocsRequest(x -> x, x -> x.accept(new MediaType("text", "x-yaml")))
);
}

@Test
public void getOpenApiAsJson_withAcceptTextVndYaml() throws Exception {
assertResponseBodyMatchesOpenApiYaml(YAML_FILE,
performApiDocsRequest(x -> x, x -> x.accept(new MediaType("text", "vnd.yaml")))
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package de.qaware.openapigeneratorforspring.test.app38;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class App38 {
public static void main(String[] args) {
SpringApplication.run(App38.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package de.qaware.openapigeneratorforspring.test.app38;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class App38Controller {
@GetMapping("mapping1")
public String mapping1(@RequestBody String request, @RequestParam String param1) {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package de.qaware.openapigeneratorforspring.test.app38;

import de.qaware.openapigeneratorforspring.test.AbstractOpenApiGeneratorWebFluxBaseIntTest;
import org.junit.Test;
import org.springframework.http.MediaType;

public class App38Test extends AbstractOpenApiGeneratorWebFluxBaseIntTest {

@Test
public void getOpenApiAsJson() throws Exception {
assertResponseBodyMatchesOpenApiYaml("app38.yaml",
performApiDocsRequest(x -> x, x -> x.accept(new MediaType("application", "yaml")))
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
openapi: 3.0.1
info:
title: API for App37
version: unknown
paths: { }
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
openapi: 3.0.1
info:
title: API for App38
version: unknown
paths:
/mapping1:
get:
operationId: mapping1
parameters:
- name: param1
in: query
required: true
schema:
type: string
requestBody:
content:
'*/*':
schema:
type: string
required: true
responses:
"200":
description: Default response
content:
'*/*':
schema:
type: string

0 comments on commit 06c57d1

Please sign in to comment.