diff --git a/src/test/java/itest/CustomEventTemplateTest.java b/src/test/java/itest/CustomEventTemplateTest.java new file mode 100644 index 000000000..2b439146b --- /dev/null +++ b/src/test/java/itest/CustomEventTemplateTest.java @@ -0,0 +1,127 @@ +/* + * 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.io.File; + +import io.quarkus.test.junit.QuarkusTest; +import io.vertx.core.buffer.Buffer; +import io.vertx.core.json.JsonObject; +import io.vertx.ext.web.client.HttpResponse; +import io.vertx.ext.web.multipart.MultipartForm; +import itest.bases.StandardSelfTest; +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; + +@QuarkusTest +@Disabled("TODO") +public class CustomEventTemplateTest extends StandardSelfTest { + + static final String INVALID_TEMPLATE_FILE_NAME = "invalidTemplate.xml"; + static final String SANITIZE_TEMPLATE_FILE_NAME = "TemplateToSanitize.jfc"; + static final String TEMPLATE_NAME = "invalidTemplate"; + static final String MEDIA_TYPE = "application/xml"; + static final String REQ_URL = "/api/v1/templates"; + + @Test + public void shouldThrowIfTemplateUploadNameInvalid() throws Exception { + ClassLoader classLoader = getClass().getClassLoader(); + File invalidTemplate = + new File(classLoader.getResource(INVALID_TEMPLATE_FILE_NAME).getFile()); + String path = invalidTemplate.getAbsolutePath(); + + MultipartForm form = + MultipartForm.create() + .attribute("invalidTemplateAttribute", INVALID_TEMPLATE_FILE_NAME) + .binaryFileUpload( + TEMPLATE_NAME, INVALID_TEMPLATE_FILE_NAME, path, MEDIA_TYPE); + + HttpResponse resp = + webClient.extensions().post(REQ_URL, form, REQUEST_TIMEOUT_SECONDS); + MatcherAssert.assertThat(resp.statusCode(), Matchers.equalTo(400)); + } + + @Test + public void shouldThrowWhenPostingInvalidTemplate() throws Exception { + ClassLoader classLoader = getClass().getClassLoader(); + File invalidTemplate = + new File(classLoader.getResource(INVALID_TEMPLATE_FILE_NAME).getFile()); + String path = invalidTemplate.getAbsolutePath(); + + MultipartForm form = + MultipartForm.create() + .attribute("template", INVALID_TEMPLATE_FILE_NAME) + .binaryFileUpload( + TEMPLATE_NAME, INVALID_TEMPLATE_FILE_NAME, path, MEDIA_TYPE); + + HttpResponse resp = + webClient.extensions().post(REQ_URL, form, REQUEST_TIMEOUT_SECONDS); + MatcherAssert.assertThat(resp.statusCode(), Matchers.equalTo(400)); + } + + @Test + public void testDeleteRecordingThrowsOnNonExistentTemplate() throws Exception { + HttpResponse resp = + webClient + .extensions() + .delete( + String.format("%s/%s", REQ_URL, INVALID_TEMPLATE_FILE_NAME), + REQUEST_TIMEOUT_SECONDS); + MatcherAssert.assertThat(resp.statusCode(), Matchers.equalTo(404)); + } + + @Test + public void testPostedTemplateIsSanitizedAndCanBeDeleted() throws Exception { + try { + ClassLoader classLoader = getClass().getClassLoader(); + File templateToSanitize = + new File(classLoader.getResource(SANITIZE_TEMPLATE_FILE_NAME).getFile()); + String path = templateToSanitize.getAbsolutePath(); + MultipartForm form = + MultipartForm.create() + .attribute("template", SANITIZE_TEMPLATE_FILE_NAME) + .binaryFileUpload( + "template", SANITIZE_TEMPLATE_FILE_NAME, path, MEDIA_TYPE); + HttpResponse postResp = + webClient.extensions().post(REQ_URL, form, REQUEST_TIMEOUT_SECONDS); + MatcherAssert.assertThat(postResp.statusCode(), Matchers.equalTo(200)); + + HttpResponse getResp = + webClient + .extensions() + .get( + String.format( + "/api/v1/targets/%s/templates", + getSelfReferenceConnectUrlEncoded()), + REQUEST_TIMEOUT_SECONDS); + boolean foundSanitizedTemplate = false; + for (Object o : getResp.bodyAsJsonArray()) { + JsonObject json = (JsonObject) o; + foundSanitizedTemplate = + foundSanitizedTemplate + || json.getString("name").equals("Template_To_Sanitize"); + } + Assertions.assertTrue(foundSanitizedTemplate); + } finally { + webClient + .extensions() + .delete(REQ_URL + "/Template_To_Sanitize", REQUEST_TIMEOUT_SECONDS); + } + } +} diff --git a/src/test/java/itest/TemplatePostDeleteIT.java b/src/test/java/itest/TemplatePostDeleteIT.java deleted file mode 100644 index bc17e83e7..000000000 --- a/src/test/java/itest/TemplatePostDeleteIT.java +++ /dev/null @@ -1,181 +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.io.File; -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.ExecutionException; - -import io.quarkus.test.junit.QuarkusTest; -import io.vertx.core.json.JsonArray; -import io.vertx.core.json.JsonObject; -import io.vertx.ext.web.handler.HttpException; -import io.vertx.ext.web.multipart.MultipartForm; -import itest.bases.StandardSelfTest; -import org.hamcrest.MatcherAssert; -import org.hamcrest.Matchers; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Disabled; -import org.junit.jupiter.api.Test; - -@QuarkusTest -@Disabled("TODO") -public class TemplatePostDeleteIT extends StandardSelfTest { - - static final String INVALID_TEMPLATE_FILE_NAME = "invalidTemplate.xml"; - static final String SANITIZE_TEMPLATE_FILE_NAME = "TemplateToSanitize.jfc"; - static final String TEMPLATE_NAME = "invalidTemplate"; - static final String MEDIA_TYPE = "application/xml"; - static final String REQ_URL = "/api/v1/templates"; - - @Test - public void shouldThrowIfTemplateUploadNameInvalid() throws Exception { - - CompletableFuture response = new CompletableFuture<>(); - ClassLoader classLoader = getClass().getClassLoader(); - File invalidTemplate = - new File(classLoader.getResource(INVALID_TEMPLATE_FILE_NAME).getFile()); - String path = invalidTemplate.getAbsolutePath(); - - MultipartForm form = - MultipartForm.create() - .attribute("invalidTemplateAttribute", INVALID_TEMPLATE_FILE_NAME) - .binaryFileUpload( - TEMPLATE_NAME, INVALID_TEMPLATE_FILE_NAME, path, MEDIA_TYPE); - - webClient - .post(REQ_URL) - .followRedirects(true) - .sendMultipartForm( - form, - ar -> { - assertRequestStatus(ar, response); - }); - ExecutionException ex = - Assertions.assertThrows(ExecutionException.class, () -> response.get()); - MatcherAssert.assertThat( - ((HttpException) ex.getCause()).getStatusCode(), Matchers.equalTo(400)); - MatcherAssert.assertThat(ex.getCause().getMessage(), Matchers.equalTo("Bad Request")); - } - - @Test - public void shouldThrowWhenPostingInvalidTemplate() throws Exception { - - CompletableFuture response = new CompletableFuture<>(); - ClassLoader classLoader = getClass().getClassLoader(); - File invalidTemplate = - new File(classLoader.getResource(INVALID_TEMPLATE_FILE_NAME).getFile()); - String path = invalidTemplate.getAbsolutePath(); - - MultipartForm form = - MultipartForm.create() - .attribute("template", INVALID_TEMPLATE_FILE_NAME) - .binaryFileUpload( - TEMPLATE_NAME, INVALID_TEMPLATE_FILE_NAME, path, MEDIA_TYPE); - - webClient - .post(REQ_URL) - .followRedirects(true) - .sendMultipartForm( - form, - ar -> { - assertRequestStatus(ar, response); - }); - ExecutionException ex = - Assertions.assertThrows(ExecutionException.class, () -> response.get()); - MatcherAssert.assertThat( - ((HttpException) ex.getCause()).getStatusCode(), Matchers.equalTo(400)); - MatcherAssert.assertThat(ex.getCause().getMessage(), Matchers.equalTo("Bad Request")); - } - - @Test - public void testDeleteRecordingThrowsOnNonExistentTemplate() throws Exception { - - CompletableFuture response = new CompletableFuture<>(); - - webClient - .delete(String.format("%s/%s", REQ_URL, INVALID_TEMPLATE_FILE_NAME)) - .followRedirects(true) - .send( - ar -> { - assertRequestStatus(ar, response); - }); - ExecutionException ex = - Assertions.assertThrows(ExecutionException.class, () -> response.get()); - MatcherAssert.assertThat( - ((HttpException) ex.getCause()).getStatusCode(), Matchers.equalTo(404)); - MatcherAssert.assertThat(ex.getCause().getMessage(), Matchers.equalTo("Not Found")); - } - - @Test - public void testPostedTemplateIsSanitizedAndCanBeDeleted() throws Exception { - try { - CompletableFuture postResponse = new CompletableFuture<>(); - ClassLoader classLoader = getClass().getClassLoader(); - File templateToSanitize = - new File(classLoader.getResource(SANITIZE_TEMPLATE_FILE_NAME).getFile()); - String path = templateToSanitize.getAbsolutePath(); - MultipartForm form = - MultipartForm.create() - .attribute("template", SANITIZE_TEMPLATE_FILE_NAME) - .binaryFileUpload( - "template", SANITIZE_TEMPLATE_FILE_NAME, path, MEDIA_TYPE); - webClient - .post(REQ_URL) - .followRedirects(true) - .sendMultipartForm( - form, - ar -> { - assertRequestStatus(ar, postResponse); - postResponse.complete(ar.result().statusCode()); - }); - MatcherAssert.assertThat(postResponse.get(), Matchers.equalTo(200)); - - CompletableFuture getResponse = new CompletableFuture<>(); - webClient - .get( - String.format( - "/api/v1/targets/%s/templates", - getSelfReferenceConnectUrlEncoded())) - .followRedirects(true) - .send( - ar -> { - assertRequestStatus(ar, getResponse); - JsonArray response = ar.result().bodyAsJsonArray(); - getResponse.complete(response); - }); - boolean foundSanitizedTemplate = false; - for (Object o : getResponse.get()) { - JsonObject json = (JsonObject) o; - foundSanitizedTemplate = - foundSanitizedTemplate - || json.getString("name").equals("Template_To_Sanitize"); - } - Assertions.assertTrue(foundSanitizedTemplate); - } finally { - CompletableFuture deleteResponse = new CompletableFuture<>(); - webClient - .delete(REQ_URL + "/Template_To_Sanitize") - .followRedirects(true) - .send( - ar -> { - assertRequestStatus(ar, deleteResponse); - deleteResponse.complete(ar.result().statusCode()); - }); - MatcherAssert.assertThat(deleteResponse.get(), Matchers.equalTo(200)); - } - } -} diff --git a/src/test/java/itest/util/Utils.java b/src/test/java/itest/util/Utils.java index d8172de2a..3bfcb6dcf 100644 --- a/src/test/java/itest/util/Utils.java +++ b/src/test/java/itest/util/Utils.java @@ -35,6 +35,7 @@ import io.vertx.ext.web.client.HttpResponse; import io.vertx.ext.web.client.WebClientOptions; import io.vertx.ext.web.client.impl.WebClientBase; +import io.vertx.ext.web.multipart.MultipartForm; public class Utils { @@ -92,6 +93,9 @@ HttpResponse post(String url, Buffer payload, int timeout) HttpResponse post(String url, MultiMap payload, int timeout) throws InterruptedException, ExecutionException, TimeoutException; + HttpResponse post(String url, MultipartForm payload, int timeout) + throws InterruptedException, ExecutionException, TimeoutException; + HttpResponse delete(String url, int timeout) throws InterruptedException, ExecutionException, TimeoutException; @@ -197,6 +201,37 @@ public HttpResponse post(String url, MultiMap payload, int timeout) return future.get(timeout, TimeUnit.SECONDS); } + public HttpResponse post(String url, MultipartForm payload, int timeout) + throws InterruptedException, ExecutionException, TimeoutException { + CompletableFuture> future = new CompletableFuture<>(); + RequestOptions options = new RequestOptions().setURI(url); + HttpRequest req = TestWebClient.this.request(HttpMethod.POST, options); + if (payload != null) { + req.sendMultipartForm( + payload, + ar -> { + if (ar.succeeded()) { + future.complete(ar.result()); + } else { + future.completeExceptionally(ar.cause()); + } + }); + } else { + req.send( + ar -> { + if (ar.succeeded()) { + future.complete(ar.result()); + } else { + future.completeExceptionally(ar.cause()); + } + }); + } + if (future.get().statusCode() == 308) { + return post(future.get().getHeader("Location"), payload, timeout); + } + return future.get(timeout, TimeUnit.SECONDS); + } + public HttpResponse delete(String url, int timeout) throws InterruptedException, ExecutionException, TimeoutException { CompletableFuture> future = new CompletableFuture<>();