Skip to content

Commit

Permalink
working on test
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewazores committed Feb 21, 2024
1 parent cfa46db commit bca3ec9
Show file tree
Hide file tree
Showing 3 changed files with 162 additions and 181 deletions.
127 changes: 127 additions & 0 deletions src/test/java/itest/CustomEventTemplateTest.java
Original file line number Diff line number Diff line change
@@ -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<Buffer> 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<Buffer> resp =
webClient.extensions().post(REQ_URL, form, REQUEST_TIMEOUT_SECONDS);
MatcherAssert.assertThat(resp.statusCode(), Matchers.equalTo(400));
}

@Test
public void testDeleteRecordingThrowsOnNonExistentTemplate() throws Exception {
HttpResponse<Buffer> 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<Buffer> postResp =
webClient.extensions().post(REQ_URL, form, REQUEST_TIMEOUT_SECONDS);
MatcherAssert.assertThat(postResp.statusCode(), Matchers.equalTo(200));

HttpResponse<Buffer> 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);
}
}
}
181 changes: 0 additions & 181 deletions src/test/java/itest/TemplatePostDeleteIT.java

This file was deleted.

35 changes: 35 additions & 0 deletions src/test/java/itest/util/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -92,6 +93,9 @@ HttpResponse<Buffer> post(String url, Buffer payload, int timeout)
HttpResponse<Buffer> post(String url, MultiMap payload, int timeout)
throws InterruptedException, ExecutionException, TimeoutException;

HttpResponse<Buffer> post(String url, MultipartForm payload, int timeout)
throws InterruptedException, ExecutionException, TimeoutException;

HttpResponse<Buffer> delete(String url, int timeout)
throws InterruptedException, ExecutionException, TimeoutException;

Expand Down Expand Up @@ -197,6 +201,37 @@ public HttpResponse<Buffer> post(String url, MultiMap payload, int timeout)
return future.get(timeout, TimeUnit.SECONDS);
}

public HttpResponse<Buffer> post(String url, MultipartForm payload, int timeout)
throws InterruptedException, ExecutionException, TimeoutException {
CompletableFuture<HttpResponse<Buffer>> future = new CompletableFuture<>();
RequestOptions options = new RequestOptions().setURI(url);
HttpRequest<Buffer> 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<Buffer> delete(String url, int timeout)
throws InterruptedException, ExecutionException, TimeoutException {
CompletableFuture<HttpResponse<Buffer>> future = new CompletableFuture<>();
Expand Down

0 comments on commit bca3ec9

Please sign in to comment.