From 88cf68e9c5d6e4f713cc119128594408c601f7d8 Mon Sep 17 00:00:00 2001 From: Fabio Burzigotti Date: Tue, 17 Dec 2019 16:07:40 +0100 Subject: [PATCH] MP OpenAPI - Adding CDI integration tests --- .../routing/router/rest/legacy/Contact.java | 25 +++ .../cdi/IntegrationWithCDITest.java | 155 ++++++++++++++++++ .../v10/OpenApi10OnJaxRsAnnotationsTest.java | 4 +- 3 files changed, 182 insertions(+), 2 deletions(-) create mode 100644 microprofile-open-api/src/main/java/org/jboss/eap/qe/microprofile/openapi/apps/routing/router/rest/legacy/Contact.java create mode 100644 microprofile-open-api/src/test/java/org/jboss/eap/qe/microprofile/openapi/integration/cdi/IntegrationWithCDITest.java diff --git a/microprofile-open-api/src/main/java/org/jboss/eap/qe/microprofile/openapi/apps/routing/router/rest/legacy/Contact.java b/microprofile-open-api/src/main/java/org/jboss/eap/qe/microprofile/openapi/apps/routing/router/rest/legacy/Contact.java new file mode 100644 index 00000000..ab8693c9 --- /dev/null +++ b/microprofile-open-api/src/main/java/org/jboss/eap/qe/microprofile/openapi/apps/routing/router/rest/legacy/Contact.java @@ -0,0 +1,25 @@ +package org.jboss.eap.qe.microprofile.openapi.apps.routing.router.rest.legacy; + +import javax.enterprise.context.RequestScoped; +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; + +@Path("/contact/{id}") +@RequestScoped +public class Contact { + private final String id; + + public Contact(@PathParam("id") String id) { + this.id = id; + } + + @Path("details") + @GET + @Produces(MediaType.TEXT_PLAIN) + public String getDetails() { + return String.format("ID: %s", id); + } +} \ No newline at end of file diff --git a/microprofile-open-api/src/test/java/org/jboss/eap/qe/microprofile/openapi/integration/cdi/IntegrationWithCDITest.java b/microprofile-open-api/src/test/java/org/jboss/eap/qe/microprofile/openapi/integration/cdi/IntegrationWithCDITest.java new file mode 100644 index 00000000..12d75863 --- /dev/null +++ b/microprofile-open-api/src/test/java/org/jboss/eap/qe/microprofile/openapi/integration/cdi/IntegrationWithCDITest.java @@ -0,0 +1,155 @@ +package org.jboss.eap.qe.microprofile.openapi.integration.cdi; + +import static io.restassured.RestAssured.get; +import static org.hamcrest.Matchers.equalToIgnoringCase; + +import java.io.IOException; +import java.util.List; +import java.util.Map; + +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.arquillian.container.test.api.RunAsClient; +import org.jboss.arquillian.junit.Arquillian; +import org.jboss.eap.qe.microprofile.openapi.OpenApiDeploymentUrlProvider; +import org.jboss.eap.qe.microprofile.openapi.OpenApiServerConfiguration; +import org.jboss.eap.qe.microprofile.openapi.apps.routing.router.RouterApplication; +import org.jboss.eap.qe.microprofile.openapi.apps.routing.router.rest.legacy.Contact; +import org.jboss.eap.qe.microprofile.tooling.server.configuration.ConfigurationException; +import org.jboss.eap.qe.microprofile.tooling.server.configuration.creaper.ManagementClientProvider; +import org.jboss.eap.qe.microprofile.tooling.server.configuration.creaper.ManagementClientRelatedException; +import org.jboss.shrinkwrap.api.Archive; +import org.jboss.shrinkwrap.api.ShrinkWrap; +import org.jboss.shrinkwrap.api.asset.EmptyAsset; +import org.jboss.shrinkwrap.api.spec.WebArchive; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.wildfly.extras.creaper.core.online.OnlineManagementClient; +import org.yaml.snakeyaml.Yaml; + +/** + * Test cases for MP OpenAPI and CDI integration, see https://javaee.github.io/tutorial/jaxrs-advanced004.html + */ +@RunWith(Arquillian.class) +@RunAsClient +public class IntegrationWithCDITest { + private final static String ROUTER_DEPLOYMENT_NAME = "localServicesRouterDeployment"; + private static String openApiUrl; + private static OnlineManagementClient onlineManagementClient; + + @BeforeClass + public static void setup() throws ManagementClientRelatedException, ConfigurationException { + openApiUrl = OpenApiDeploymentUrlProvider.composeDefaultOpenApiUrl(); + // MP OpenAPI up + onlineManagementClient = ManagementClientProvider.onlineStandalone(); + if (!OpenApiServerConfiguration.openapiSubsystemExists(onlineManagementClient)) { + OpenApiServerConfiguration.enableOpenApi(onlineManagementClient); + } + } + + @AfterClass + public static void tearDown() throws ManagementClientRelatedException, IOException { + // MP OpenAPI down + try { + OpenApiServerConfiguration.disableOpenApi(onlineManagementClient); + } finally { + onlineManagementClient.close(); + } + } + + @Deployment(testable = false) + public static Archive localServicesRouterDeployment() { + WebArchive deployment = ShrinkWrap.create( + WebArchive.class, ROUTER_DEPLOYMENT_NAME + ".war") + .addClasses( + RouterApplication.class, + Contact.class) + .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml"); + return deployment; + } + + /** + * @tpTestDetails Integration test to verify that a "legacy" CDI bean converted to "requestScoped" JAX-RS resource + * and having JAX-RS annotated constructor is returning expected content + * @tpPassCrit The endpoint is returning expected response code and content + * @tpSince EAP 7.4.0.CD19 + */ + @Test + public void testAppEndpoint() { + String id = "1"; + String response = get( + OpenApiDeploymentUrlProvider.composeDefaultDeploymentBaseUrl( + ROUTER_DEPLOYMENT_NAME + String.format("/contact/%s/details", id))) + .then() + .statusCode(200) + // RestEasy >3.9.3 adds charset info unless resteasy.add.charset is set to false, + // thus expecting for it to be there, see + // https://docs.jboss.org/resteasy/docs/3.9.3.Final/userguide/html/Installation_Configuration.html#configuration_switches + .contentType(equalToIgnoringCase("text/plain;charset=UTF-8")) + .extract().asString(); + Assert.assertEquals(response, String.format("ID: %s", id)); + } + + /** + * @tpTestDetails Tests for proper OpenAPI documentation of application endpoint represented by a JAX-RS resource + * having its constructor accepting a {@code @PathParam} annotated argument, see + * https://javaee.github.io/tutorial/jaxrs-advanced004.html + * @tpPassCrit Verifies that the returned YAML data has corresponding values for JAX-RS annotations + * @tpSince EAP 7.4.0.CD19 + */ + @Test + @SuppressWarnings("unchecked") + public void testOpenApiDocumentForDocumentedConstructorParam() { + + String responseContent = get(openApiUrl) + .then() + .statusCode(200) + .extract().asString(); + + Yaml yaml = new Yaml(); + Object yamlObject = yaml.load(responseContent); + Map yamlMap = (Map) yamlObject; + + Map paths = (Map) yamlMap.get("paths"); + Assert.assertFalse("\"paths\" property is empty", paths.isEmpty()); + + Map getConctactIdDetailsPath = (Map) paths.get("/contact/{id}/details"); + Assert.assertFalse("\"/contact/{id}/details\" property is empty", getConctactIdDetailsPath.isEmpty()); + + Map getMethod = (Map) getConctactIdDetailsPath.get("get"); + Assert.assertFalse("\"/contact/{id}/details\" \"get\" property is empty", getMethod.isEmpty()); + Assert.assertNotNull("\"/contact/{id}/details\" \"responses\" for GET verb is null", getMethod.get("responses")); + + Map responses = (Map) getMethod.get("responses"); + Assert.assertNotNull("\"/contact/{id}/details\" \"response\" for GET verb and HTTP status 200 is null", + responses.get(200)); + + Map http200Response = (Map) responses.get(200); + Assert.assertNotNull( + "\"/contact/{id}/details\" \"response\" for GET verb and HTTP status 200 has null \"content\" property", + http200Response.get("content")); + + Map http200ResponseContent = (Map) http200Response.get("content"); + Assert.assertNotNull( + "\"/contact/{id}/details\" \"response\" for GET verb and HTTP status 200 has \"content\" but null \"application/json\" property", + http200ResponseContent.get("text/plain")); + + List parameters = (List) getConctactIdDetailsPath.get("parameters"); + Assert.assertEquals("\"/contact/{id}/details\" operation for GET verb should have exactly 1 parameters", + parameters.size(), 1); + + Map pathParam = (Map) parameters.get(0); + Assert.assertFalse("Parameter [0] for \"/contact/{id}/details\" operation for GET verb is empty", pathParam.isEmpty()); + Assert.assertEquals( + "\"name\" property of parameter [0] for \"/contact/{id}/details\" operation (GET verb) should be set to \"id\"", + pathParam.get("name"), "id"); + Assert.assertEquals( + "\"in\" property of parameter [0] for \"/contact/{id}/details\" operation (GET verb) should be set to \"path\"", + pathParam.get("in"), "path"); + Assert.assertEquals( + "\"required\" property of parameter [0] for \"/contact/{id}/details\" operation (GET verb) should be set to \"true\"", + pathParam.get("required"), Boolean.TRUE); + } +} diff --git a/microprofile-open-api/src/test/java/org/jboss/eap/qe/microprofile/openapi/v10/OpenApi10OnJaxRsAnnotationsTest.java b/microprofile-open-api/src/test/java/org/jboss/eap/qe/microprofile/openapi/v10/OpenApi10OnJaxRsAnnotationsTest.java index 62aa3ff5..a0caed08 100644 --- a/microprofile-open-api/src/test/java/org/jboss/eap/qe/microprofile/openapi/v10/OpenApi10OnJaxRsAnnotationsTest.java +++ b/microprofile-open-api/src/test/java/org/jboss/eap/qe/microprofile/openapi/v10/OpenApi10OnJaxRsAnnotationsTest.java @@ -103,7 +103,7 @@ public void testAppEndpoint() { /** * @tpTestDetails Tests for proper OpenAPI documentation of application endpoint containing JAX-RS annotations - * @tpPassCrit Verifies that the returned YAML data has corresponding values for JAX-RS annotation + * @tpPassCrit Verifies that the returned YAML data has corresponding values for JAX-RS annotations * @tpSince EAP 7.4.0.CD19 */ @Test @@ -159,7 +159,7 @@ public void testOpenApiDocumentForPathAndQueryParam() { */ @Test @SuppressWarnings("unchecked") - public void testOpenApiDocumentForOfResponseTypeSchema() { + public void testOpenApiDocumentForResponseTypeSchema() { String responseContent = get(openApiUrl) .then()