Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test OpenAPI with YAML responses #3062

Merged
merged 1 commit into from
Sep 2, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,8 @@
## limitations under the License.
## ---------------------------------------------------------------------------

#
# Quarkus
#
quarkus.log.level = INFO
quarkus.log.category."org.apache.camel".level = DEBUG

#
# Camel - REST
#
camel.rest.component = platform-http
camel.rest.api-context-path = /openapi.json
camel.rest.api-context-path = /openapi
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.containsInAnyOrder;
Expand All @@ -28,6 +31,12 @@

@QuarkusTest
class OpenApiTest {

@BeforeAll
public static void beforeAll() {
RestAssured.filters(new YamlToJsonFilter());
}

@Test
public void invokeApiEndpoint() {
RestAssured.given()
Expand All @@ -40,10 +49,13 @@ public void invokeApiEndpoint() {
"name", containsInAnyOrder("Apple", "Pineapple"));
}

@Test
public void invokeApiDocumentEndpoint() {
RestAssured.given()
.get("/openapi.json")
@ParameterizedTest
@MethodSource("getOpenApiContentTypes")
public void invokeApiDocumentEndpoint(String contentType) {
RestAssured
.given()
.header("Accept", contentType)
.get("/openapi")
.then()
.contentType(ContentType.JSON)
.statusCode(200)
Expand All @@ -52,10 +64,12 @@ public void invokeApiDocumentEndpoint() {
"paths.'/fruits/list'.get.operationId", is("list"));
}

@Test
public void openApiEndpointSecurity() {
@ParameterizedTest
@MethodSource("getOpenApiContentTypes")
public void openApiEndpointSecurity(String contentType) {
RestAssured.given()
.get("/openapi.json")
.header("Accept", contentType)
.get("/openapi")
.then()
.contentType(ContentType.JSON)
.statusCode(200)
Expand All @@ -65,10 +79,12 @@ public void openApiEndpointSecurity() {

}

@Test
public void openApiKeySecurityDefinition() {
@ParameterizedTest
@MethodSource("getOpenApiContentTypes")
public void openApiKeySecurityDefinition(String contentType) {
RestAssured.given()
.get("/openapi.json")
.header("Accept", contentType)
.get("/openapi")
.then()
.contentType(ContentType.JSON)
.statusCode(200)
Expand All @@ -81,10 +97,12 @@ public void openApiKeySecurityDefinition() {

}

@Test
public void openApiBasicAuthSecurityDefinition() {
@ParameterizedTest
@MethodSource("getOpenApiContentTypes")
public void openApiBasicAuthSecurityDefinition(String contentType) {
RestAssured.given()
.get("/openapi.json")
.header("Accept", contentType)
.get("/openapi")
.then()
.contentType(ContentType.JSON)
.statusCode(200)
Expand All @@ -96,10 +114,12 @@ public void openApiBasicAuthSecurityDefinition() {

}

@Test
public void openApiBearerAuthSecurityDefinition() {
@ParameterizedTest
@MethodSource("getOpenApiContentTypes")
public void openApiBearerAuthSecurityDefinition(String contentType) {
RestAssured.given()
.get("/openapi.json")
.header("Accept", contentType)
.get("/openapi")
.then()
.contentType(ContentType.JSON)
.statusCode(200)
Expand All @@ -110,10 +130,12 @@ public void openApiBearerAuthSecurityDefinition() {
"components.securitySchemes.bearerAuth.bearerFormat", is("Bearer Token Authentication"));
}

@Test
public void openApiMutualTlsSecurityDefinition() {
@ParameterizedTest
@MethodSource("getOpenApiContentTypes")
public void openApiMutualTlsSecurityDefinition(String contentType) {
RestAssured.given()
.get("/openapi.json")
.header("Accept", contentType)
.get("/openapi")
.then()
.contentType(ContentType.JSON)
.statusCode(200)
Expand All @@ -122,10 +144,12 @@ public void openApiMutualTlsSecurityDefinition() {
"components.securitySchemes.mutualTLS.type", is("mutualTLS"));
}

@Test
public void openApiOauth2SecurityDefinition() {
@ParameterizedTest
@MethodSource("getOpenApiContentTypes")
public void openApiOauth2SecurityDefinition(String contentType) {
RestAssured.given()
.get("/openapi.json")
.header("Accept", contentType)
.get("/openapi")
.then()
.contentType(ContentType.JSON)
.statusCode(200)
Expand All @@ -138,10 +162,12 @@ public void openApiOauth2SecurityDefinition() {
"components.securitySchemes.oauth2.flows.implicit.scopes.scope3", is("Scope 3"));
}

@Test
public void openApiOpenIdSecurityDefinition() {
@ParameterizedTest
@MethodSource("getOpenApiContentTypes")
public void openApiOpenIdSecurityDefinition(String contentType) {
RestAssured.given()
.get("/openapi.json")
.header("Accept", contentType)
.get("/openapi")
.then()
.contentType(ContentType.JSON)
.statusCode(200)
Expand All @@ -152,10 +178,12 @@ public void openApiOpenIdSecurityDefinition() {
"components.securitySchemes.openId.type", is("openIdConnect"));
}

@Test
public void openApiOperationSpecification() {
@ParameterizedTest
@MethodSource("getOpenApiContentTypes")
public void openApiOperationSpecification(String contentType) {
RestAssured.given()
.get("/openapi.json")
.header("Accept", contentType)
.get("/openapi")
.then()
.contentType(ContentType.JSON)
.statusCode(200)
Expand All @@ -181,4 +209,8 @@ public void openApiOperationSpecification() {
"paths.'/operation/spec'.get.responses.418.description", is("I am a teapot"),
"paths.'/operation/spec'.get.responses.error.description", is("Response Error"));
}

static String[] getOpenApiContentTypes() {
return new String[] { "application/json", "text/yaml" };
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.camel.quarkus.component.openapijava.it;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import io.restassured.builder.ResponseBuilder;
import io.restassured.filter.FilterContext;
import io.restassured.filter.OrderedFilter;
import io.restassured.http.ContentType;
import io.restassured.response.Response;
import io.restassured.specification.FilterableRequestSpecification;
import io.restassured.specification.FilterableResponseSpecification;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.jboss.logging.Logger;

/**
* Enable YAML responses to be inspected with REST Assured JsonPath expressions.
*
* Inspired by the original microprofile-open-api source.
*
* https://github.com/eclipse/microprofile-open-api/blob/master/tck/src/main/java/org/eclipse/microprofile/openapi/tck/utils/YamlToJsonFilter.java
*/
public class YamlToJsonFilter implements OrderedFilter {

private static final Logger LOG = Logger.getLogger(YamlToJsonFilter.class);

@Override
public int getOrder() {
return OrderedFilter.HIGHEST_PRECEDENCE;
}

@Override
public Response filter(FilterableRequestSpecification requestSpec, FilterableResponseSpecification responseSpec,
FilterContext ctx) {
try {
Response response = ctx.next(requestSpec, responseSpec);

if (response.getContentType().equals("text/yaml")) {
String yaml = response.getBody().asString();
if (LOG.isDebugEnabled()) {
LOG.debug(yaml);
}

ObjectMapper yamlReader = new ObjectMapper(new YAMLFactory());
Object obj = yamlReader.readValue(yaml, Object.class);

ObjectMapper jsonWriter = new ObjectMapper();
String json = jsonWriter.writeValueAsString(obj);

ResponseBuilder builder = new ResponseBuilder();
builder.clone(response);
builder.setBody(json);
builder.setContentType(ContentType.JSON);
return builder.build();
}

return response;
} catch (Exception e) {
throw new IllegalStateException("Failed to convert the request: " + ExceptionUtils.getMessage(e), e);
}
}
}