Skip to content

Commit

Permalink
simplified implementation, got tests working
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewazores committed Feb 21, 2024
1 parent 769816c commit 059bc10
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 78 deletions.
44 changes: 14 additions & 30 deletions src/main/java/io/cryostat/events/EventTemplates.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,20 @@
*/
package io.cryostat.events;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;

import io.cryostat.core.sys.FileSystem;
import io.cryostat.core.templates.MutableTemplateService.InvalidEventTemplateException;
import io.cryostat.core.templates.MutableTemplateService.InvalidXmlException;
import io.cryostat.core.templates.Template;
import io.cryostat.core.templates.TemplateType;
import io.cryostat.targets.Target;
import io.cryostat.util.HttpMimeType;

import io.smallrye.common.annotation.Blocking;
import io.smallrye.mutiny.Uni;
import io.vertx.core.Vertx;
import jakarta.annotation.security.RolesAllowed;
import jakarta.inject.Inject;
import jakarta.ws.rs.BadRequestException;
Expand Down Expand Up @@ -58,7 +57,7 @@ public class EventTemplates {
"Cryostat",
TemplateType.TARGET);

@Inject Vertx vertx;
@Inject FileSystem fs;
@Inject TargetTemplateService.Factory targetTemplateServiceFactory;
@Inject S3TemplateService customTemplateService;
@Inject Logger logger;
Expand Down Expand Up @@ -86,30 +85,15 @@ public Response postTemplatesV1(@RestForm("template") FileUpload body) {
@POST
@Path("/api/v3/event_templates")
@RolesAllowed("write")
public Uni<Void> postTemplates(@RestForm("template") FileUpload body) {
CompletableFuture<Void> cf = new CompletableFuture<>();
var path = body.filePath();
vertx.fileSystem()
.readFile(path.toString())
.onComplete(
ar -> {
var str = ar.result().toString();
try (var stream =
new ByteArrayInputStream(
str.getBytes(StandardCharsets.UTF_8))) {
customTemplateService.addTemplate(stream);
cf.complete(null);
} catch (Exception e) {
logger.error(e);
cf.completeExceptionally(e);
}
})
.onFailure(
ar -> {
logger.error(ar.getCause());
cf.completeExceptionally(ar.getCause());
});
return Uni.createFrom().future(cf);
public void postTemplates(@RestForm("template") FileUpload body) throws IOException {
if (body == null || body.filePath() == null || !"template".equals(body.name())) {
throw new BadRequestException();
}
try (var stream = fs.newInputStream(body.filePath())) {
customTemplateService.addTemplate(stream);
} catch (InvalidEventTemplateException | InvalidXmlException e) {
throw new BadRequestException(e);
}
}

@DELETE
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/cryostat/events/S3TemplateService.java
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ public Template addTemplate(InputStream stream)
"Template has no configuration label attribute"));
}

String templateName = labelAttr.getExplicitValue();
String templateName = labelAttr.getExplicitValue().replaceAll("[\\W]+", "_");

XMLTagInstance root = model.getRoot();
root.setValue(JFCGrammar.ATTRIBUTE_LABEL_MANDATORY, templateName);
Expand Down
88 changes: 41 additions & 47 deletions src/test/java/itest/CustomEventTemplateTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
*/
package itest;

import java.io.File;

import io.quarkus.test.junit.DisabledOnIntegrationTest;
import io.quarkus.test.junit.QuarkusTest;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.json.JsonObject;
Expand All @@ -27,11 +26,10 @@
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")
@DisabledOnIntegrationTest("classpath resources are not loadable in integration test")
public class CustomEventTemplateTest extends StandardSelfTest {

static final String INVALID_TEMPLATE_FILE_NAME = "invalidTemplate.xml";
Expand All @@ -43,37 +41,33 @@ public class CustomEventTemplateTest extends StandardSelfTest {
@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);
try (var stream = classLoader.getResourceAsStream(INVALID_TEMPLATE_FILE_NAME)) {
var buf = Buffer.buffer(stream.readAllBytes());
MultipartForm form =
MultipartForm.create()
.binaryFileUpload(
TEMPLATE_NAME, INVALID_TEMPLATE_FILE_NAME, buf, MEDIA_TYPE);

HttpResponse<Buffer> resp =
webClient.extensions().post(REQ_URL, form, REQUEST_TIMEOUT_SECONDS);
MatcherAssert.assertThat(resp.statusCode(), Matchers.equalTo(400));
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);
try (var stream = classLoader.getResourceAsStream(INVALID_TEMPLATE_FILE_NAME)) {
var buf = Buffer.buffer(stream.readAllBytes());
MultipartForm form =
MultipartForm.create()
.binaryFileUpload(
TEMPLATE_NAME, INVALID_TEMPLATE_FILE_NAME, buf, MEDIA_TYPE);

HttpResponse<Buffer> resp =
webClient.extensions().post(REQ_URL, form, REQUEST_TIMEOUT_SECONDS);
MatcherAssert.assertThat(resp.statusCode(), Matchers.equalTo(400));
HttpResponse<Buffer> resp =
webClient.extensions().post(REQ_URL, form, REQUEST_TIMEOUT_SECONDS);
MatcherAssert.assertThat(resp.statusCode(), Matchers.equalTo(400));
}
}

@Test
Expand All @@ -88,19 +82,16 @@ public void testDeleteRecordingThrowsOnNonExistentTemplate() throws Exception {
}

@Test
public void testPostedTemplateCanBeDeleted() throws Exception {
try {
ClassLoader classLoader = getClass().getClassLoader();
File customEventTemplate =
new File(classLoader.getResource(TEMPLATE_FILE_NAME).getFile());
String path = customEventTemplate.getAbsolutePath();
public void testPostedTemplateNameIsSanitizedAndCanBeDeleted() throws Exception {
ClassLoader classLoader = getClass().getClassLoader();
try (var stream = classLoader.getResourceAsStream(TEMPLATE_FILE_NAME)) {
var buf = Buffer.buffer(stream.readAllBytes());
MultipartForm form =
MultipartForm.create()
.attribute("template", TEMPLATE_FILE_NAME)
.binaryFileUpload("template", TEMPLATE_FILE_NAME, path, MEDIA_TYPE);
.binaryFileUpload("template", TEMPLATE_FILE_NAME, buf, MEDIA_TYPE);
HttpResponse<Buffer> postResp =
webClient.extensions().post(REQ_URL, form, REQUEST_TIMEOUT_SECONDS);
MatcherAssert.assertThat(postResp.statusCode(), Matchers.equalTo(200));
MatcherAssert.assertThat(postResp.statusCode(), Matchers.equalTo(204));

HttpResponse<Buffer> getResp =
webClient
Expand All @@ -113,20 +104,23 @@ public void testPostedTemplateCanBeDeleted() throws Exception {
boolean foundSanitizedTemplate = false;
for (Object o : getResp.bodyAsJsonArray()) {
JsonObject json = (JsonObject) o;
var name = json.getString("name");
foundSanitizedTemplate =
foundSanitizedTemplate
|| json.getString("name").equals("Custom Event Template");
foundSanitizedTemplate || name.equals("Custom_Event_Template");
}
Assertions.assertTrue(foundSanitizedTemplate);
} finally {
webClient
.extensions()
.delete(
String.format(
"%s/%s",
REQ_URL,
URLEncodedUtils.formatSegments("/Template_To_Sanitize")),
REQUEST_TIMEOUT_SECONDS);
var delResp =
webClient
.extensions()
.delete(
String.format(
"%s%s",
REQ_URL,
URLEncodedUtils.formatSegments(
"Custom_Event_Template")),
REQUEST_TIMEOUT_SECONDS);
MatcherAssert.assertThat(delResp.statusCode(), Matchers.equalTo(204));
}
}
}

0 comments on commit 059bc10

Please sign in to comment.