Skip to content

Commit

Permalink
test: fix tests for adapting them to new API
Browse files Browse the repository at this point in the history
  • Loading branch information
cake-lier committed Jan 17, 2024
1 parent 79a1a10 commit 5b40264
Show file tree
Hide file tree
Showing 8 changed files with 267 additions and 20 deletions.
37 changes: 29 additions & 8 deletions src/test/java/org/hyperagents/yggdrasil/MainVerticleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
@SuppressWarnings("PMD.JUnitTestsShouldIncludeAssert")
@ExtendWith(VertxExtension.class)
public class MainVerticleTest {
private static final String TEST_AGENT_ID = "test_agent";
private static final String TEST_AGENT_ID = "http://localhost:8080/agents/test_agent";
private static final String AGENT_ID_HEADER = "X-Agent-WebID";
private static final String HINT_HEADER = "Slug";
private static final String MAIN_WORKSPACE_NAME = "test";
Expand Down Expand Up @@ -63,7 +63,7 @@ public class MainVerticleTest {
public MainVerticleTest() {
this.callbackMessages =
Stream.generate(Promise::<Map.Entry<String, String>>promise)
.limit(8)
.limit(9)
.collect(Collectors.toList());
}

Expand Down Expand Up @@ -144,6 +144,16 @@ public void testRun(final VertxTestContext ctx) throws URISyntaxException, IOExc
Path.of(ClassLoader.getSystemResource("sub_workspace_c0_td.ttl").toURI()),
StandardCharsets.UTF_8
);
final var testAgentBodyRepresentation =
Files.readString(
Path.of(ClassLoader.getSystemResource("test_agent_body.ttl").toURI()),
StandardCharsets.UTF_8
);
final var subWorkspaceWithArtifactAndBodyRepresentation =
Files.readString(
Path.of(ClassLoader.getSystemResource("sub_workspace_c0_body.ttl").toURI()),
StandardCharsets.UTF_8
);
this.client.post(TEST_PORT, TEST_HOST, HUB_PATH)
.sendJsonObject(JsonObject.of(
HUB_MODE_PARAM,
Expand Down Expand Up @@ -195,7 +205,7 @@ public void testRun(final VertxTestContext ctx) throws URISyntaxException, IOExc
r.bodyAsString()
);
})
.compose(r -> this.callbackMessages.get(0).future())
.compose(r -> this.callbackMessages.getFirst().future())
.onSuccess(m -> {
Assertions.assertEquals(
this.getUrl("/"),
Expand Down Expand Up @@ -381,10 +391,21 @@ public void testRun(final VertxTestContext ctx) throws URISyntaxException, IOExc
r.statusCode(),
OK_STATUS_MESSAGE
);
this.assertEqualsThingDescriptions(
testAgentBodyRepresentation,
r.bodyAsString()
);
})
.compose(r -> this.callbackMessages.get(6).future())
.onSuccess(m -> {
Assertions.assertEquals(
String.valueOf(HttpStatus.SC_OK),
r.bodyAsString(),
RESPONSE_BODY_OK_MESSAGE
this.getUrl(WORKSPACES_PATH + SUB_WORKSPACE_NAME),
m.getKey(),
URIS_EQUAL_MESSAGE
);
this.assertEqualsThingDescriptions(
subWorkspaceWithArtifactAndBodyRepresentation,
m.getValue()
);
})
.compose(r -> this.client
Expand Down Expand Up @@ -412,7 +433,7 @@ public void testRun(final VertxTestContext ctx) throws URISyntaxException, IOExc
RESPONSE_BODY_OK_MESSAGE
);
})
.compose(r -> this.callbackMessages.get(6).future())
.compose(r -> this.callbackMessages.get(7).future())
.onSuccess(m -> {
Assertions.assertEquals(
this.getUrl(
Expand Down Expand Up @@ -450,7 +471,7 @@ public void testRun(final VertxTestContext ctx) throws URISyntaxException, IOExc
);
Assertions.assertNull(r.bodyAsString(), RESPONSE_BODY_EMPTY_MESSAGE);
})
.compose(r -> this.callbackMessages.get(7).future())
.compose(r -> this.callbackMessages.get(8).future())
.onSuccess(m -> {
Assertions.assertEquals(
this.getUrl(
Expand Down
81 changes: 81 additions & 0 deletions src/test/resources/sub_workspace_c0_body.ttl
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
@prefix hmas: <https://purl.org/hmas/core/> .
@prefix td: <https://www.w3.org/2019/wot/td#> .
@prefix htv: <http://www.w3.org/2011/http#> .
@prefix hctl: <https://www.w3.org/2019/wot/hypermedia#> .
@prefix wotsec: <https://www.w3.org/2019/wot/security#> .
@prefix dct: <http://purl.org/dc/terms/> .
@prefix js: <https://www.w3.org/2019/wot/json-schema#> .
@prefix saref: <https://w3id.org/saref#> .

<http://localhost:8080/workspaces/sub> a td:Thing, hmas:Workspace;
td:title "sub";
td:hasSecurityConfiguration [ a wotsec:NoSecurityScheme
];
td:hasActionAffordance [ a td:ActionAffordance;
td:name "makeArtifact";
td:hasForm [
htv:methodName "POST";
hctl:hasTarget <http://localhost:8080/workspaces/sub/artifacts/>;
hctl:forContentType "application/json";
hctl:hasOperationType td:invokeAction
];
td:hasInputSchema [ a js:ObjectSchema;
js:properties [ a js:StringSchema;
js:propertyName "artifactClass";
js:enum <http://example.org/Counter>
], [ a js:ArraySchema;
js:propertyName "initParams"
], [ a js:StringSchema;
js:propertyName "artifactName"
];
js:required "artifactClass", "artifactName"
]
], [ a td:ActionAffordance;
td:name "joinWorkspace";
td:hasForm [
htv:methodName "POST";
hctl:hasTarget <http://localhost:8080/workspaces/sub/join>;
hctl:forContentType "application/json";
hctl:hasOperationType td:invokeAction
]
], [ a td:ActionAffordance;
td:name "quitWorkspace";
td:hasForm [
htv:methodName "POST";
hctl:hasTarget <http://localhost:8080/workspaces/sub/leave>;
hctl:forContentType "application/json";
hctl:hasOperationType td:invokeAction
]
], [ a td:ActionAffordance;
td:name "focus";
td:hasForm [
htv:methodName "POST";
hctl:hasTarget <http://localhost:8080/workspaces/sub/focus>;
hctl:forContentType "application/json";
hctl:hasOperationType td:invokeAction
];
td:hasInputSchema [ a js:ObjectSchema;
js:properties [ a js:StringSchema;
js:propertyName "artifactName"
], [ a js:StringSchema;
js:propertyName "callbackIri"
];
js:required "artifactName", "callbackIri"
]
], [ a td:ActionAffordance;
td:name "createSubWorkspace";
td:hasForm [
htv:methodName "POST";
hctl:hasTarget <http://localhost:8080/workspaces/sub>;
hctl:forContentType "application/json";
hctl:hasOperationType td:invokeAction
]
];
hmas:isContainedIn <http://localhost:8080/workspaces/test>;
hmas:contains <http://localhost:8080/workspaces/sub/artifacts/c0>, <http://localhost:8080/workspaces/sub/agents/test_agent> .

<http://localhost:8080/workspaces/test> a hmas:Workspace .

<http://localhost:8080/workspaces/sub/artifacts/c0> a hmas:Artifact .

<http://localhost:8080/workspaces/sub/agents/test_agent> a hmas:Artifact .
17 changes: 17 additions & 0 deletions src/test/resources/test_agent_body.ttl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
@prefix hmas: <https://purl.org/hmas/core/> .
@prefix td: <https://www.w3.org/2019/wot/td#> .
@prefix htv: <http://www.w3.org/2011/http#> .
@prefix hctl: <https://www.w3.org/2019/wot/hypermedia#> .
@prefix wotsec: <https://www.w3.org/2019/wot/security#> .
@prefix dct: <http://purl.org/dc/terms/> .
@prefix js: <https://www.w3.org/2019/wot/json-schema#> .
@prefix saref: <https://w3id.org/saref#> .

<http://localhost:8080/workspaces/sub/agents/test_agent> a td:Thing, hmas:Artifact;
td:title "test_agent";
td:hasSecurityConfiguration [ a wotsec:NoSecurityScheme
] .

<http://localhost:8080/workspaces/sub/agents/test_agent> hmas:isContainedIn <http://localhost:8080/workspaces/sub> .

<http://localhost:8080/workspaces/sub> a hmas:Workspace .
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,15 @@ public class CartagoVerticleTest {
private static final String SUB_WORKSPACE_NAME = "sub";
private static final String TEST_AGENT_IRI = "http://localhost:8080/agents/test";
private static final String FOCUSING_AGENT_IRI = "http://localhost:8080/agents/focusing_agent";
private static final String TEST_AGENT_BODY_URI =
"http://localhost:8080/workspaces/" + SUB_WORKSPACE_NAME + "/agents/test";
private static final String ADDER_SEMANTIC_TYPE = "http://example.org/Adder";
private static final String COUNTER_SEMANTIC_TYPE = "http://example.org/Counter";
private static final String CALLBACK_IRI = "http://localhost:8080/callback";
private static final String NONEXISTENT_NAME = "nonexistent";
private static final String ARTIFACT_SEMANTIC_TYPE_PARAM = "artifactClass";
private static final String ARTIFACT_INIT_PARAMS = "initParams";
private static final String INCREMENT_OPERATION = "inc";
private static final String ADD_OPERATION = "add";
private static final String TDS_EQUAL_MESSAGE = "The Thing Descriptions should be equal";
private static final String OPERATION_FAIL_MESSAGE =
Expand Down Expand Up @@ -123,17 +126,24 @@ public void testCreateWorkspaceFailsWithAlreadyCreatedOne(final VertxTestContext
}

@Test
public void testJoinWorkspaceSucceeds(final VertxTestContext ctx) {
public void testJoinWorkspaceSucceeds(final VertxTestContext ctx)
throws URISyntaxException, IOException {
final var expectedBodyThingDescription =
Files.readString(
Path.of(ClassLoader.getSystemResource("test_agent_body.ttl").toURI()),
StandardCharsets.UTF_8
);
this.cartagoMessagebox
.sendMessage(new CartagoMessage.CreateWorkspace(MAIN_WORKSPACE_NAME))
.compose(r -> this.cartagoMessagebox
.sendMessage(new CartagoMessage.JoinWorkspace(
TEST_AGENT_IRI, MAIN_WORKSPACE_NAME
TEST_AGENT_IRI,
MAIN_WORKSPACE_NAME
)))
.onSuccess(r -> Assertions.assertEquals(
String.valueOf(HttpStatus.SC_OK),
expectedBodyThingDescription,
r.body(),
OPERATION_SUCCESS_MESSAGE
TDS_EQUAL_MESSAGE
))
.onComplete(ctx.succeedingThenComplete());
}
Expand Down Expand Up @@ -627,7 +637,7 @@ public void testDoActionSucceeds(final VertxTestContext ctx) {
TEST_AGENT_IRI,
MAIN_WORKSPACE_NAME,
"c0",
"inc",
INCREMENT_OPERATION,
Optional.empty()
)))
.onSuccess(r -> Assertions.assertEquals(
Expand Down Expand Up @@ -685,7 +695,7 @@ public void testDoActionAfterFocusSucceeds(final VertxTestContext ctx) {
TEST_AGENT_IRI,
SUB_WORKSPACE_NAME,
"c1",
"inc",
INCREMENT_OPERATION,
Optional.empty()
));
})
Expand All @@ -696,12 +706,52 @@ public void testDoActionAfterFocusSucceeds(final VertxTestContext ctx) {
OPERATION_SUCCESS_MESSAGE
);
try {
final var notifyActionRequestedMessage =
(HttpNotificationDispatcherMessage.ActionRequested)
this.notificationQueue.take();
Assertions.assertEquals(
TEST_AGENT_BODY_URI,
notifyActionRequestedMessage.requestIri(),
URIS_EQUAL_MESSAGE
);
Assertions.assertEquals(
notifyActionRequestedMessage.content(),
JsonObject
.of(
"artifactName",
"c1",
"actionName",
INCREMENT_OPERATION
)
.encode(),
"The properties should be equal"
);
assertNotificationReceived(
SUB_WORKSPACE_NAME,
"c1",
"count(6)"
);
} catch (Exception e) {
final var notifyActionCompletedMessage =
(HttpNotificationDispatcherMessage.ActionSucceeded)
this.notificationQueue.take();
Assertions.assertEquals(
TEST_AGENT_BODY_URI,
notifyActionCompletedMessage.requestIri(),
URIS_EQUAL_MESSAGE
);
Assertions.assertEquals(
notifyActionCompletedMessage.content(),
JsonObject
.of(
"artifactName",
"c1",
"actionName",
INCREMENT_OPERATION
)
.encode(),
"The properties should be equal"
);
} catch (final Exception e) {
ctx.failNow(e);
}
})
Expand Down
12 changes: 12 additions & 0 deletions yggdrasil-cartago/src/test/resources/test_agent_body.ttl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
@prefix td: <https://www.w3.org/2019/wot/td#> .
@prefix htv: <http://www.w3.org/2011/http#> .
@prefix hctl: <https://www.w3.org/2019/wot/hypermedia#> .
@prefix wotsec: <https://www.w3.org/2019/wot/security#> .
@prefix dct: <http://purl.org/dc/terms/> .
@prefix js: <https://www.w3.org/2019/wot/json-schema#> .
@prefix hmas: <https://purl.org/hmas/core/> .

<http://localhost:8080/workspaces/test/agents/test> a td:Thing, hmas:Artifact;
td:title "test";
td:hasSecurityConfiguration [ a wotsec:NoSecurityScheme
] .
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class CartagoHttpHandlersTest {
private static final int TEST_PORT = 8080;
private static final String TEST_HOST = "localhost";
private static final String AGENT_WEBID = "X-Agent-WebID";
private static final String TEST_AGENT_ID = "test_agent";
private static final String TEST_AGENT_ID = "http://localhost:8080/agents/test_agent";
private static final String SLUG_HEADER = "Slug";
private static final String MAIN_WORKSPACE_NAME = "test";
private static final String SUB_WORKSPACE_NAME = "sub";
Expand Down Expand Up @@ -473,7 +473,17 @@ public void testPostArtifactFailsWithoutContentType(final VertxTestContext ctx)

@Test
public void testPostJoinWorkspaceSucceeds(final VertxTestContext ctx)
throws InterruptedException {
throws InterruptedException, URISyntaxException, IOException {
final var initialBodyRepresentation =
Files.readString(
Path.of(ClassLoader.getSystemResource("test_agent_body.ttl").toURI()),
StandardCharsets.UTF_8
);
final var fullBodyRepresentation =
Files.readString(
Path.of(ClassLoader.getSystemResource("test_agent_body_full.ttl").toURI()),
StandardCharsets.UTF_8
);
final var request = this.client.post(TEST_PORT, TEST_HOST, MAIN_WORKSPACE_PATH + "/join")
.putHeader(AGENT_WEBID, TEST_AGENT_ID)
.send();
Expand All @@ -489,7 +499,25 @@ public void testPostJoinWorkspaceSucceeds(final VertxTestContext ctx)
joinWorkspaceMessage.workspaceName(),
NAMES_EQUAL_MESSAGE
);
cartagoMessage.reply(String.valueOf(HttpStatus.SC_OK));
cartagoMessage.reply(initialBodyRepresentation);
final var storeMessage = this.storeMessageQueue.take();
final var createBodyMessage = (RdfStoreMessage.CreateArtifact) storeMessage.body();
Assertions.assertEquals(
this.helper.getUri(MAIN_WORKSPACE_PATH + "/agents/"),
createBodyMessage.requestUri(),
NAMES_EQUAL_MESSAGE
);
Assertions.assertEquals(
"test_agent",
createBodyMessage.artifactName(),
NAMES_EQUAL_MESSAGE
);
Assertions.assertEquals(
initialBodyRepresentation,
createBodyMessage.artifactRepresentation(),
NAMES_EQUAL_MESSAGE
);
storeMessage.reply(fullBodyRepresentation);
request
.onSuccess(r -> {
Assertions.assertEquals(
Expand All @@ -498,9 +526,9 @@ public void testPostJoinWorkspaceSucceeds(final VertxTestContext ctx)
OK_STATUS_MESSAGE
);
Assertions.assertEquals(
String.valueOf(HttpStatus.SC_OK),
fullBodyRepresentation,
r.bodyAsString(),
RESPONSE_BODY_STATUS_CODE_MESSAGE
TDS_EQUAL_MESSAGE
);
})
.onComplete(ctx.succeedingThenComplete());
Expand Down Expand Up @@ -577,6 +605,14 @@ public void testPostLeaveWorkspaceSucceeds(final VertxTestContext ctx)
NAMES_EQUAL_MESSAGE
);
cartagoMessage.reply(String.valueOf(HttpStatus.SC_OK));
final var storeMessage = this.storeMessageQueue.take();
final var deleteBodyMessage = (RdfStoreMessage.DeleteEntity) storeMessage.body();
Assertions.assertEquals(
this.helper.getUri(MAIN_WORKSPACE_PATH + "/agents/test_agent"),
deleteBodyMessage.requestUri(),
NAMES_EQUAL_MESSAGE
);
storeMessage.reply(String.valueOf(HttpStatus.SC_OK));
request
.onSuccess(r -> {
Assertions.assertEquals(
Expand Down
Loading

0 comments on commit 5b40264

Please sign in to comment.