From df3b9987a61b2f69e2de555da3b2300d988db781 Mon Sep 17 00:00:00 2001 From: Andrew Azores Date: Fri, 28 Feb 2025 12:16:58 -0500 Subject: [PATCH] test(api): test OpenAPI listing endpoint --- src/test/java/io/cryostat/ApiListingTest.java | 93 +++++++++++++++++++ src/test/java/itest/ApiListingIT.java | 81 ---------------- 2 files changed, 93 insertions(+), 81 deletions(-) create mode 100644 src/test/java/io/cryostat/ApiListingTest.java delete mode 100644 src/test/java/itest/ApiListingIT.java diff --git a/src/test/java/io/cryostat/ApiListingTest.java b/src/test/java/io/cryostat/ApiListingTest.java new file mode 100644 index 000000000..ad97e3913 --- /dev/null +++ b/src/test/java/io/cryostat/ApiListingTest.java @@ -0,0 +1,93 @@ +/* + * Copyright The Cryostat Authors. + * + * Licensed 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 io.cryostat; + +import static io.restassured.RestAssured.given; + +import java.util.regex.Pattern; + +import io.quarkus.test.junit.QuarkusTest; +import io.restassured.http.ContentType; +import io.restassured.response.ValidatableResponse; +import org.hamcrest.Matchers; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; + +@QuarkusTest +public class ApiListingTest extends AbstractTestBase { + + @Test + void shouldDefaultToYamlResponse() { + given().log() + .all() + .when() + .get("/api") + .then() + .assertThat() + .contentType("application/yaml") + .statusCode(200); + } + + @Nested + class JsonResponses { + + ValidatableResponse resp; + + @BeforeEach + void setup() { + resp = given().log().all().accept(ContentType.JSON).when().get("/api").then(); + } + + @Test + void shouldBeOk() { + resp.statusCode(200); + } + + @Test + void shouldRespondWithJson() { + resp.contentType(ContentType.JSON); + } + + @Test + void shouldIncludeOpenApiInfo() { + resp.body("openapi", Matchers.matchesRegex("^\\d+\\.\\d+\\.\\d+$")); + } + + @Test + void shouldIncludeApplicationInfo() { + resp.body("info.title", Matchers.equalTo("Cryostat API (test)")) + .body("info.description", Matchers.equalTo("Cloud-Native JDK Flight Recorder")) + .body("info.contact.name", Matchers.equalTo("Cryostat Community")) + .body("info.contact.url", Matchers.equalTo("https://cryostat.io")) + .body( + "info.contact.email", + Matchers.equalTo("cryostat-development@googlegroups.com")) + .body("info.license.name", Matchers.equalTo("Apache 2.0")) + .body( + "info.license.url", + Matchers.equalTo( + "https://github.com/cryostatio/cryostat/blob/main/LICENSE")) + .body( + "info.version", + Matchers.matchesRegex( + Pattern.compile( + "^\\d+\\.\\d+\\.\\d+(?:-snapshot)?$", + Pattern.CASE_INSENSITIVE | Pattern.MULTILINE))) + .body("paths", Matchers.not(Matchers.blankOrNullString())); + } + } +} diff --git a/src/test/java/itest/ApiListingIT.java b/src/test/java/itest/ApiListingIT.java deleted file mode 100644 index 798afbe38..000000000 --- a/src/test/java/itest/ApiListingIT.java +++ /dev/null @@ -1,81 +0,0 @@ -/* - * Copyright The Cryostat Authors. - * - * Licensed 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 itest; - -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.TimeUnit; - -import io.quarkus.test.junit.QuarkusIntegrationTest; -import io.vertx.core.buffer.Buffer; -import io.vertx.core.json.JsonObject; -import io.vertx.ext.web.client.HttpRequest; -import itest.bases.StandardSelfTest; -import itest.util.Utils; -import org.hamcrest.MatcherAssert; -import org.hamcrest.Matchers; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Disabled; -import org.junit.jupiter.api.Test; - -@QuarkusIntegrationTest -@Disabled("TODO") -public class ApiListingIT extends StandardSelfTest { - - HttpRequest req; - - @BeforeEach - void createRequest() { - req = webClient.get("/api"); - } - - @Test - void shouldIncludeHttpApiMdAndNonEmptyEndpoints() throws Exception { - CompletableFuture future = new CompletableFuture<>(); - req.send( - ar -> { - if (ar.failed()) { - future.completeExceptionally(ar.cause()); - return; - } - future.complete(ar.result().bodyAsJsonObject()); - }); - JsonObject response = future.get(REQUEST_TIMEOUT_SECONDS, TimeUnit.SECONDS); - - MatcherAssert.assertThat(response.getJsonObject("meta"), Matchers.notNullValue()); - MatcherAssert.assertThat( - response.getJsonObject("meta").getString("type"), - Matchers.equalTo("application/json")); - MatcherAssert.assertThat( - response.getJsonObject("meta").getString("status"), Matchers.equalTo("OK")); - - MatcherAssert.assertThat(response.getJsonObject("data"), Matchers.notNullValue()); - MatcherAssert.assertThat( - response.getJsonObject("data").getJsonObject("result"), Matchers.notNullValue()); - MatcherAssert.assertThat( - response.getJsonObject("data").getJsonObject("result").getString("overview"), - Matchers.equalTo( - String.format("http://%s:%d/HTTP_API.md", Utils.WEB_HOST, Utils.WEB_PORT))); - MatcherAssert.assertThat( - response.getJsonObject("data").getJsonObject("result").getJsonArray("endpoints"), - Matchers.notNullValue()); - MatcherAssert.assertThat( - response.getJsonObject("data") - .getJsonObject("result") - .getJsonArray("endpoints") - .size(), - Matchers.greaterThan(0)); - } -}