Skip to content

Commit

Permalink
feat: add GET and PUT URLs to Web API for body artifacts
Browse files Browse the repository at this point in the history
  • Loading branch information
cake-lier committed Jan 22, 2024
1 parent fd55462 commit ff996c8
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class HttpServerVerticle extends AbstractVerticle {
private static final String WORKSPACE_PATH = "/workspaces/:wkspid";
private static final String ARTIFACT_PATH = "/workspaces/:wkspid/artifacts/:artid";
private static final String TURTLE_CONTENT_TYPE = "text/turtle";
public static final String BODY_PATH = "/workspaces/:wkspid/agents/:agtname";

private HttpServer server;

Expand Down Expand Up @@ -106,6 +107,13 @@ private Router createRouter() {
router.delete(ARTIFACT_PATH + "/").handler(handler::handleRedirectWithoutSlash);
router.delete(ARTIFACT_PATH).handler(handler::handleDeleteEntity);

router.get(BODY_PATH + "/").handler(handler::handleRedirectWithoutSlash);
router.get(BODY_PATH).handler(handler::handleGetEntity);
router.put(BODY_PATH + "/").handler(handler::handleRedirectWithoutSlash);
router.put(BODY_PATH)
.consumes(TURTLE_CONTENT_TYPE)
.handler(handler::handleUpdateEntity);

router.post(ARTIFACT_PATH + "/*").handler(handler::handleAction);

router.post("/hub/").handler(handler::handleEntitySubscription);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
package org.hyperagents.yggdrasil.http;

import com.google.common.net.HttpHeaders;
import io.vertx.core.Vertx;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.eventbus.Message;
import io.vertx.core.http.HttpMethod;
import io.vertx.ext.web.client.WebClient;
import io.vertx.junit5.VertxExtension;
import io.vertx.junit5.VertxTestContext;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import org.hyperagents.yggdrasil.eventbus.messageboxes.RdfStoreMessagebox;
import org.hyperagents.yggdrasil.eventbus.messages.RdfStoreMessage;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

@SuppressWarnings("PMD.JUnitTestsShouldIncludeAssert")
@ExtendWith(VertxExtension.class)
public class AgentBodyHttpHandlersTest {
private static final int TEST_PORT = 8080;
private static final String TEST_HOST = "localhost";
private static final String TEST_AGENT_ID = "test_agent";
private static final String TURTLE_CONTENT_TYPE = "text/turtle";
private static final String BODIES_PATH = "/workspaces/test/agents/";
private static final String BODY_PATH = BODIES_PATH + TEST_AGENT_ID;
private static final String NONEXISTENT_NAME = "nonexistent";
private static final String BODY_FILE = "test_agent_body.ttl";

private final BlockingQueue<Message<RdfStoreMessage>> storeMessageQueue;
private WebClient client;
private HttpServerVerticleTestHelper helper;

public AgentBodyHttpHandlersTest() {
this.storeMessageQueue = new LinkedBlockingQueue<>();
}

@BeforeEach
public void setUp(final Vertx vertx, final VertxTestContext ctx) {
this.client = WebClient.create(vertx);
this.helper = new HttpServerVerticleTestHelper(this.client, this.storeMessageQueue);
final var storeMessagebox = new RdfStoreMessagebox(vertx.eventBus());
storeMessagebox.init();
storeMessagebox.receiveMessages(this.storeMessageQueue::add);
vertx.deployVerticle(new HttpServerVerticle(), ctx.succeedingThenComplete());
}

@AfterEach
public void tearDown(final Vertx vertx, final VertxTestContext ctx) {
vertx.close(ctx.succeedingThenComplete());
}

@Test
public void testGetBodySucceeds(final VertxTestContext ctx)
throws URISyntaxException, IOException, InterruptedException {
this.helper.testGetResourceSucceeds(ctx, BODY_FILE, BODY_PATH);
}

@Test
public void testGetBodyRedirectsWithSlash(final VertxTestContext ctx) {
this.helper.testResourceRequestRedirectsWithAddedSlash(
ctx,
HttpMethod.GET,
BODY_PATH + "/"
);
}

@Test
public void testGetBodyFailsWithNotFound(final VertxTestContext ctx)
throws InterruptedException {
this.helper.testResourceRequestFailsWithNotFound(
ctx,
BODIES_PATH + NONEXISTENT_NAME,
this.client.get(TEST_PORT, TEST_HOST, BODIES_PATH + NONEXISTENT_NAME).send()
);
}

@Test
public void testPutTurtleArtifactSucceeds(final VertxTestContext ctx)
throws URISyntaxException, IOException, InterruptedException {
this.helper.testPutTurtleResourceSucceeds(
ctx,
BODY_PATH,
BODY_FILE
);
}

@Test
public void testPutTurtleArtifactFailsWithNotFound(final VertxTestContext ctx)
throws URISyntaxException, IOException, InterruptedException {
this.helper.testResourceRequestFailsWithNotFound(
ctx,
BODIES_PATH + NONEXISTENT_NAME,
this.client.put(TEST_PORT, TEST_HOST, BODIES_PATH + NONEXISTENT_NAME)
.putHeader("X-Agent-WebID", TEST_AGENT_ID)
.putHeader(HttpHeaders.CONTENT_TYPE, TURTLE_CONTENT_TYPE)
.sendBuffer(Buffer.buffer(Files.readString(
Path.of(ClassLoader.getSystemResource(BODY_FILE).toURI()),
StandardCharsets.UTF_8
)))
);
}

@Test
public void testPutTurtleArtifactFailsWithoutWebId(final VertxTestContext ctx)
throws URISyntaxException, IOException {
this.helper.testResourceRequestFailsWithoutWebId(
ctx,
this.client.put(TEST_PORT, TEST_HOST, BODY_PATH)
.putHeader(HttpHeaders.CONTENT_TYPE, TURTLE_CONTENT_TYPE)
.sendBuffer(Buffer.buffer(Files.readString(
Path.of(ClassLoader.getSystemResource(BODY_FILE).toURI()),
StandardCharsets.UTF_8
)))
);
}

@Test
public void testPutTurtleArtifactFailsWithoutContentType(final VertxTestContext ctx)
throws URISyntaxException, IOException {
this.helper.testResourceRequestFailsWithoutContentType(
ctx,
HttpMethod.PUT,
BODY_PATH,
Buffer.buffer(Files.readString(
Path.of(ClassLoader.getSystemResource(BODY_FILE).toURI()),
StandardCharsets.UTF_8
))
);
}

@Test
public void testPutTurtleArtifactRedirectsWithSlash(final VertxTestContext ctx) {
this.helper.testResourceRequestRedirectsWithAddedSlash(
ctx,
HttpMethod.PUT,
BODY_PATH
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ public void testPutTurtleArtifactFailsWithNotFound(final VertxTestContext ctx)
.putHeader(AGENT_WEBID, TEST_AGENT_ID)
.putHeader(HttpHeaders.CONTENT_TYPE, TURTLE_CONTENT_TYPE)
.sendBuffer(Buffer.buffer(Files.readString(
Path.of(ClassLoader.getSystemResource(TEST_WORKSPACE_FILE).toURI()),
Path.of(ClassLoader.getSystemResource(COUNTER_ARTIFACT_FILE).toURI()),
StandardCharsets.UTF_8
)))
);
Expand Down

0 comments on commit ff996c8

Please sign in to comment.