use of won.utils.content.model.AtomContent in project webofneeds by researchstudio-sat.
the class AclTests method testModifyOnBehalf_addGraph.
@Test(timeout = 60 * 1000)
public void testModifyOnBehalf_addGraph() throws Exception {
final AtomicBoolean atom1Created = new AtomicBoolean(false);
final AtomicBoolean atom2Created = new AtomicBoolean(false);
final CountDownLatch latch = new CountDownLatch(2);
final AtomicReference<URI> createMessageUri1 = new AtomicReference();
final AtomicReference<URI> createMessageUri2 = new AtomicReference();
runTest(ctx -> {
EventBus bus = ctx.getEventBus();
final URI wonNodeUri = ctx.getNodeURISource().getNodeURI();
final URI atomUri1 = ctx.getWonNodeInformationService().generateAtomURI(wonNodeUri);
final URI atomUri2 = ctx.getWonNodeInformationService().generateAtomURI(wonNodeUri);
BotBehaviour bbAtom1 = new BotBehaviour(ctx) {
@Override
protected void onActivate(Optional<Object> message) {
final String atomUriString = atomUri1.toString();
final AtomContent atomContent = AtomContent.builder(atomUriString).addTitle("Granting atom").addSocket(Socket.builder(atomUriString + "#chatSocket").setSocketDefinition(WXCHAT.ChatSocket.asURI()).build()).addType(URI.create(WON.Atom.getURI())).build();
Authorization auth = Authorization.builder().addGranteesAtomExpression(ae -> ae.addAtomsURI(atomUri2)).addGrant(ase -> ase.addOperationsSimpleOperationExpression(OP_READ).addGraph(g -> g.addGraphType(GraphType.CONTENT_GRAPH).addOperationsMessageOperationExpression(moe -> moe.addMessageOnBehalfsMessageType(MessageType.REPLACE_MESSAGE)))).build();
WonMessage createMessage = WonMessageBuilder.createAtom().atom(atomUri1).content().graph("#content", RdfOutput.toGraph(atomContent)).content().aclGraph(won.auth.model.RdfOutput.toGraph(auth)).build();
createMessage = ctx.getWonMessageSender().prepareMessage(createMessage);
ctx.getBotContextWrapper().rememberAtomUri(atomUri1);
final String action = "Create granting atom";
EventListener successCallback = event -> {
logger.debug("Granting atom created");
createMessageUri1.set(((SuccessResponseEvent) event).getOriginalMessageURI());
deactivate();
};
EventListener failureCallback = makeFailureCallbackToFailTest(bot, ctx, bus, action);
EventBotActionUtils.makeAndSubscribeResponseListener(createMessage, successCallback, failureCallback, ctx);
ctx.getWonMessageSender().sendMessage(createMessage);
}
};
BotBehaviour bbAtom2 = new BotBehaviour(ctx) {
@Override
protected void onActivate(Optional<Object> message) {
final String atomUriString = atomUri2.toString();
final AtomContent atomContent = AtomContent.builder(atomUriString).addTitle("Grantee atom").addSocket(Socket.builder(atomUriString + "#chatSocket").setSocketDefinition(WXCHAT.ChatSocket.asURI()).build()).addType(URI.create(WON.Atom.getURI())).build();
WonMessage createMessage = WonMessageBuilder.createAtom().atom(atomUri2).content().graph(RdfOutput.toGraph(atomContent)).content().aclGraph(won.auth.model.RdfOutput.toGraph(getNobodyMayDoNothing())).build();
createMessage = ctx.getWonMessageSender().prepareMessage(createMessage);
ctx.getBotContextWrapper().rememberAtomUri(atomUri2);
final String action = "Create grantee atom";
EventListener successCallback = event -> {
logger.debug("Grantee atom created");
createMessageUri2.set(((SuccessResponseEvent) event).getOriginalMessageURI());
deactivate();
};
EventListener failureCallback = makeFailureCallbackToFailTest(bot, ctx, bus, action);
EventBotActionUtils.makeAndSubscribeResponseListener(createMessage, successCallback, failureCallback, ctx);
ctx.getWonMessageSender().sendMessage(createMessage);
}
};
BotBehaviour bbReplaceAtom1FromAtom2 = new BotBehaviour(ctx) {
@Override
protected void onActivate(Optional<Object> message) {
WonMessage replaceMessage = WonMessageBuilder.replace().atom(atomUri1).content().graph("#content", RdfOutput.toGraph(AtomContent.builder(atomUri1).addTitle("Replaced Title").addTag("A-New-Tag").addType(URI.create(WON.Atom.getURI())).addType(URI.create(WON.Persona.getURI())).addSocket(Socket.builder(atomUri1.toString() + "#buddySocket").setSocketDefinition(WXBUDDY.BuddySocket.asURI()).build()).addSocket(Socket.builder(atomUri1.toString() + "#holderSocket").setSocketDefinition(WXHOLD.HolderSocket.asURI()).build()).build())).content().graph("#content2", RdfOutput.toGraph(AtomContent.builder(atomUri1).addTitle("New Title").addType(URI.create(WON.Atom.getURI())).addSocket(Socket.builder(atomUri1.toString() + "#chatSocket").setSocketDefinition(WXCHAT.ChatSocket.asURI()).build()).build())).build();
String action = "replace content of atom1 using webid of atom2, adding a graph";
EventListener failureCallback = makeFailureCallbackToFailTest(bot, ctx, bus, action);
replaceMessage = ctx.getWonMessageSender().prepareMessageOnBehalf(replaceMessage, atomUri2);
EventListener successCallback = makeLoggingCallback(bot, bus, action, () -> deactivate());
EventBotActionUtils.makeAndSubscribeResponseListener(replaceMessage, successCallback, failureCallback, ctx);
ctx.getWonMessageSender().sendMessage(replaceMessage);
}
};
BotBehaviour readModified = new BotBehaviour(ctx) {
@Override
protected void onActivate(Optional<Object> message) {
Dataset atomData = ctx.getLinkedDataSource().getDataForResource(atomUri1, atomUri1);
Set<String> expectedGraphNames = Set.of(atomUri1 + "#acl", atomUri1 + "#acl-sig", atomUri1 + "#content", atomUri1 + "#content-sig", atomUri1 + "#content2", atomUri1 + "#content2-sig", atomUri1 + "#key", atomUri1 + "#key-sig", atomUri1 + "#sysinfo");
Iterator<String> it = atomData.listNames();
Set<String> actualNames = new HashSet<>();
while (it.hasNext()) {
actualNames.add(it.next());
}
Assert.assertEquals(expectedGraphNames, actualNames);
Shacl2JavaInstanceFactory instanceFactory = ContentUtils.newInstanceFactory();
Shacl2JavaInstanceFactory.Accessor accessor = instanceFactory.accessor(atomData.getNamedModel(atomUri1 + "#content").getGraph());
Optional<AtomContent> atomContent = accessor.getInstanceOfType(atomUri1.toString(), AtomContent.class);
assertTrue(bus, "replacing content failed: replaced title not found", atomContent.get().getTitles().contains("Replaced Title"));
assertFalse(bus, "replacing content failed: old title still there", atomContent.get().getTitles().contains("Granting atom"));
accessor = instanceFactory.accessor(atomData.getNamedModel(atomUri1 + "#content2").getGraph());
atomContent = accessor.getInstanceOfType(atomUri1.toString(), AtomContent.class);
assertTrue(bus, "adding content failed: new title not found", atomContent.get().getTitles().contains("New Title"));
passTest(bus);
}
};
BehaviourBarrier barrier = new BehaviourBarrier(ctx);
barrier.waitFor(bbAtom1, bbAtom2);
barrier.thenStart(bbReplaceAtom1FromAtom2);
barrier.activate();
bbAtom1.activate();
bbAtom2.activate();
bbReplaceAtom1FromAtom2.onDeactivateActivate(readModified);
logger.debug("Finished initializing test 'testQueryBasedMatch()'");
});
}
use of won.utils.content.model.AtomContent in project webofneeds by researchstudio-sat.
the class AclTests method testConnectionMessages.
@Test(timeout = 60 * 1000)
public void testConnectionMessages() throws Exception {
final AtomicReference<URI> createMessageUri1 = new AtomicReference();
final AtomicReference<URI> createMessageUri2 = new AtomicReference();
final AtomicReference<URI> connectionUri12 = new AtomicReference<>();
final AtomicReference<URI> connectionUri21 = new AtomicReference<>();
final AtomicReference<URI> connectMessageUri12 = new AtomicReference<>();
final AtomicReference<URI> connectMessageUri21 = new AtomicReference<>();
runTest(ctx -> {
EventBus bus = ctx.getEventBus();
final URI wonNodeUri = ctx.getNodeURISource().getNodeURI();
final URI atomUri1 = ctx.getWonNodeInformationService().generateAtomURI(wonNodeUri);
final URI atomUri2 = ctx.getWonNodeInformationService().generateAtomURI(wonNodeUri);
final BotBehaviour bbCreateAtom1 = new BotBehaviour(ctx, "bbCreateAtom1") {
@Override
protected void onActivate(Optional<Object> message) {
final String atomUriString = atomUri1.toString();
final AtomContent atomContent = AtomContent.builder(atomUriString).addTitle("Connection initiating atom").addSocket(Socket.builder(atomUriString + "#chatSocket").setSocketDefinition(WXCHAT.ChatSocket.asURI()).build()).addType(URI.create(WON.Atom.getURI())).build();
Authorization auth = Authorization.builder().addGranteesAtomExpression(ae -> ae.addAtomsURI(atomUri2)).addGrant(ase -> ase.addGraph(ge -> ge.addOperationsSimpleOperationExpression(OP_READ))).build();
WonMessage createMessage = WonMessageBuilder.createAtom().atom(atomUri1).content().graph(RdfOutput.toGraph(atomContent)).content().aclGraph(won.auth.model.RdfOutput.toGraph(auth)).content().aclGraph(won.auth.model.RdfOutput.toGraph(getAnyoneMayConnectAuth())).content().aclGraph(won.auth.model.RdfOutput.toGraph(getConnectedMayCommunicateAuth())).build();
createMessage = ctx.getWonMessageSender().prepareMessage(createMessage);
ctx.getBotContextWrapper().rememberAtomUri(atomUri1);
EventListener successCallback = event -> {
logger.debug("Connection initiating atom created");
createMessageUri1.set(((SuccessResponseEvent) event).getOriginalMessageURI());
deactivate();
};
EventListener failureCallback = makeFailureCallbackToFailTest(bot, ctx, bus, "Create connection initiating atom");
EventBotActionUtils.makeAndSubscribeResponseListener(createMessage, successCallback, failureCallback, ctx);
ctx.getWonMessageSender().sendMessage(createMessage);
}
};
// create match source
final BotBehaviour bbCreateAtom2 = new BotBehaviour(ctx, "bbCreateAtom2") {
@Override
protected void onActivate(Optional<Object> message) {
final String atomUriString = atomUri2.toString();
final AtomContent atomContent = AtomContent.builder(atomUriString).addTitle("Grantee atom").addSparqlQuery("PREFIX won:<https://w3id.org/won/core#>\n" + "PREFIX con:<https://w3id.org/won/content#>\n" + "SELECT ?result (1.0 AS ?score) WHERE {" + "?result a won:Atom ;" + " con:tag \"tag-to-match\"." + "}").addSocket(Socket.builder(atomUriString + "#chatSocket").setSocketDefinition(WXCHAT.ChatSocket.asURI()).build()).addType(URI.create(WON.Atom.getURI())).build();
Authorization auth = Authorization.builder().addGranteesAtomExpression(ae -> ae.addAtomsURI(atomUri1)).addGrant(ase -> ase.addGraph(ge -> ge.addOperationsSimpleOperationExpression(OP_READ))).build();
WonMessage createMessage = WonMessageBuilder.createAtom().atom(atomUri2).content().graph(RdfOutput.toGraph(atomContent)).content().aclGraph(won.auth.model.RdfOutput.toGraph(auth)).content().aclGraph(won.auth.model.RdfOutput.toGraph(getAnyoneMayConnectAuth())).content().aclGraph(won.auth.model.RdfOutput.toGraph(getConnectedMayCommunicateAuth())).build();
createMessage = ctx.getWonMessageSender().prepareMessage(createMessage);
ctx.getBotContextWrapper().rememberAtomUri(atomUri2);
EventListener successCallback = event -> {
logger.debug("Connection accepting atom created");
createMessageUri2.set(((SuccessResponseEvent) event).getOriginalMessageURI());
deactivate();
};
EventListener failureCallback = makeFailureCallbackToFailTest(bot, ctx, bus, "Create connection accepting atom");
EventBotActionUtils.makeAndSubscribeResponseListener(createMessage, successCallback, failureCallback, ctx);
ctx.getWonMessageSender().sendMessage(createMessage);
}
};
BotBehaviour bbSendConnect = new BotBehaviour(ctx, "bbSendConnect") {
@Override
protected void onActivate(Optional<Object> message) {
WonMessage connectMessage = WonMessageBuilder.connect().sockets().sender(URI.create(atomUri1.toString() + "#chatSocket")).recipient(URI.create(atomUri2.toString() + "#chatSocket")).direction().fromOwner().content().text("Hello there!").build();
connectMessage = ctx.getWonMessageSender().prepareMessage(connectMessage);
connectMessageUri12.set(connectMessage.getMessageURIRequired());
EventListener successCallback = event -> {
logger.debug("Connection requested");
connectionUri12.set(((SuccessResponseEvent) event).getConnectionURI().get());
deactivate();
};
EventListener failureCallback = makeFailureCallbackToFailTest(bot, ctx, bus, "Requesting connection");
EventBotActionUtils.makeAndSubscribeResponseListener(connectMessage, successCallback, failureCallback, ctx);
ctx.getWonMessageSender().sendMessage(connectMessage);
}
};
BotBehaviour bbAcceptConnect = new BotBehaviour(ctx, "bbAcceptConnect") {
@Override
protected void onActivate(Optional<Object> message) {
WonMessage connectMessage = WonMessageBuilder.connect().sockets().sender(URI.create(atomUri2.toString() + "#chatSocket")).recipient(URI.create(atomUri1.toString() + "#chatSocket")).direction().fromOwner().content().text("Hello!").build();
connectMessage = ctx.getWonMessageSender().prepareMessage(connectMessage);
connectMessageUri21.set(connectMessage.getMessageURIRequired());
EventListener successCallback = event -> {
logger.debug("Connection accepted");
connectionUri21.set(((SuccessResponseEvent) event).getConnectionURI().get());
deactivate();
};
EventListener failureCallback = makeFailureCallbackToFailTest(bot, ctx, bus, "Accepting connection");
EventBotActionUtils.makeAndSubscribeResponseListener(connectMessage, successCallback, failureCallback, ctx);
ctx.getWonMessageSender().sendMessage(connectMessage);
}
};
BotBehaviour bbSendMessage = new BotBehaviour(ctx, "bbSendMessage") {
@Override
protected void onActivate(Optional<Object> message) {
WonMessage msg = WonMessageBuilder.connectionMessage().sockets().sender(URI.create(atomUri1.toString() + "#chatSocket")).recipient(URI.create(atomUri2.toString() + "#chatSocket")).direction().fromOwner().content().text("Nice, this works").build();
msg = ctx.getWonMessageSender().prepareMessage(msg);
final URI connectMessageUri = msg.getMessageURIRequired();
EventListener successCallback = event -> {
logger.debug("Message sent");
deactivate();
};
EventListener failureCallback = makeFailureCallbackToFailTest(bot, ctx, bus, "Sending connection message");
EventBotActionUtils.makeAndSubscribeResponseListener(msg, successCallback, failureCallback, ctx);
ctx.getWonMessageSender().sendMessage(msg);
}
};
BotBehaviour bbTestLinkedDataAccess = new BotBehaviour(ctx, "bbTestLinkedDataAccess") {
@Override
protected void onActivate(Optional<Object> message) {
URI connContainerUri1 = uriService.createConnectionContainerURIForAtom(atomUri1);
URI connContainerUri2 = uriService.createConnectionContainerURIForAtom(atomUri2);
boolean passed = true;
// both atoms allow each other read access
passed = passed && testLinkedDataRequestOk(ctx, bus, "test1.", atomUri1, atomUri1, atomUri2, // createMessageUri1.get(),
connectionUri12.get(), connectMessageUri12.get(), connectMessageUri21.get(), connContainerUri1);
passed = passed && testLinkedDataRequestFails(ctx, bus, "test2.", atomUri1, LinkedDataFetchingException.class, // createMessageUri2.get(),
connectionUri21.get(), connContainerUri2);
passed = passed && testLinkedDataRequestOk(ctx, bus, "test3.", atomUri2, atomUri2, atomUri1, // createMessageUri2.get(),
connectionUri21.get(), connectMessageUri21.get(), connectMessageUri12.get(), connContainerUri2);
passed = passed && testLinkedDataRequestFails(ctx, bus, "test4.", atomUri2, LinkedDataFetchingException.class, // createMessageUri1.get(),
connectionUri12.get(), connContainerUri1);
if (passed) {
passTest(bus);
}
}
};
BehaviourBarrier barrier = new BehaviourBarrier(ctx);
barrier.waitFor(bbCreateAtom1, bbCreateAtom2);
barrier.thenStart(bbSendConnect);
barrier.activate();
bbSendConnect.onDeactivateActivate(bbAcceptConnect);
bbAcceptConnect.onDeactivateActivate(bbSendMessage);
bbSendMessage.onDeactivateActivate(bbTestLinkedDataAccess);
bbCreateAtom1.activate();
bbCreateAtom2.activate();
});
}
use of won.utils.content.model.AtomContent in project webofneeds by researchstudio-sat.
the class AclTests method testAutoConnectByAcl.
@Test(timeout = 60 * 1000)
public void testAutoConnectByAcl() throws Exception {
runTest(ctx -> {
EventBus bus = ctx.getEventBus();
final URI wonNodeUri = ctx.getNodeURISource().getNodeURI();
final URI atomUri1 = ctx.getWonNodeInformationService().generateAtomURI(wonNodeUri);
final URI atomUri2 = ctx.getWonNodeInformationService().generateAtomURI(wonNodeUri);
final BotBehaviour bbCreateAtom1 = new BotBehaviour(ctx, "bbCreateAtom1") {
@Override
protected void onActivate(Optional<Object> message) {
final String atomUriString = atomUri1.toString();
final AtomContent atomContent = AtomContent.builder(atomUriString).addTitle("Autoconnecting atom").addSocket(Socket.builder(atomUriString + "#chatSocket").setSocketDefinition(WXCHAT.ChatSocket.asURI()).build()).addType(URI.create(WON.Atom.getURI())).build();
Authorization auth = Authorization.builder().addGranteesAtomExpression(ae -> ae.addAtomsURI(atomUri2)).addGrant(ase -> ase.addGraph(ge -> ge.addOperationsSimpleOperationExpression(OP_READ)).addSocket(s -> s.addSocketType(WXCHAT.ChatSocket.asURI())).addOperationsSimpleOperationExpression(OP_AUTO_CONNECT)).build();
WonMessage createMessage = WonMessageBuilder.createAtom().atom(atomUri1).content().graph(RdfOutput.toGraph(atomContent)).content().aclGraph(won.auth.model.RdfOutput.toGraph(auth)).content().aclGraph(won.auth.model.RdfOutput.toGraph(getAnyoneMayConnectAuth())).content().aclGraph(won.auth.model.RdfOutput.toGraph(getConnectedMayCommunicateAuth())).build();
createMessage = ctx.getWonMessageSender().prepareMessage(createMessage);
ctx.getBotContextWrapper().rememberAtomUri(atomUri1);
EventListener successCallback = event -> {
logger.debug("Connection autoconnecting atom created");
deactivate();
};
EventListener failureCallback = makeFailureCallbackToFailTest(bot, ctx, bus, "Create autoconnecting atom");
EventBotActionUtils.makeAndSubscribeResponseListener(createMessage, successCallback, failureCallback, ctx);
ctx.getWonMessageSender().sendMessage(createMessage);
}
};
// create match source
final BotBehaviour bbCreateAtom2 = new BotBehaviour(ctx, "bbCreateAtom2") {
@Override
protected void onActivate(Optional<Object> message) {
final String atomUriString = atomUri2.toString();
final AtomContent atomContent = AtomContent.builder(atomUriString).addTitle("Connecting atom").addSocket(Socket.builder(atomUriString + "#chatSocket").setSocketDefinition(WXCHAT.ChatSocket.asURI()).build()).addType(URI.create(WON.Atom.getURI())).build();
Authorization auth = Authorization.builder().addGranteesAtomExpression(ae -> ae.addAtomsURI(atomUri1)).addGrant(ase -> ase.addGraph(ge -> ge.addOperationsSimpleOperationExpression(OP_READ))).build();
WonMessage createMessage = WonMessageBuilder.createAtom().atom(atomUri2).content().graph(RdfOutput.toGraph(atomContent)).content().aclGraph(won.auth.model.RdfOutput.toGraph(auth)).content().aclGraph(won.auth.model.RdfOutput.toGraph(getAnyoneMayConnectAuth())).content().aclGraph(won.auth.model.RdfOutput.toGraph(getConnectedMayCommunicateAuth())).build();
createMessage = ctx.getWonMessageSender().prepareMessage(createMessage);
ctx.getBotContextWrapper().rememberAtomUri(atomUri2);
EventListener successCallback = event -> {
logger.debug("Connection initiating atom created");
deactivate();
};
EventListener failureCallback = makeFailureCallbackToFailTest(bot, ctx, bus, "Create connection initiating atom");
EventBotActionUtils.makeAndSubscribeResponseListener(createMessage, successCallback, failureCallback, ctx);
ctx.getWonMessageSender().sendMessage(createMessage);
}
};
BotBehaviour bbSendConnect = new BotBehaviour(ctx, "bbSendConnect") {
@Override
protected void onActivate(Optional<Object> message) {
WonMessage connectMessage = WonMessageBuilder.connect().sockets().sender(URI.create(atomUri2.toString() + "#chatSocket")).recipient(URI.create(atomUri1.toString() + "#chatSocket")).direction().fromOwner().content().text("Hello there!").build();
connectMessage = ctx.getWonMessageSender().prepareMessage(connectMessage);
EventListener successCallback = event -> {
logger.debug("Connection requested");
deactivate();
};
EventListener failureCallback = makeFailureCallbackToFailTest(bot, ctx, bus, "Requesting connection");
EventBotActionUtils.makeAndSubscribeResponseListener(connectMessage, successCallback, failureCallback, ctx);
ctx.getWonMessageSender().sendMessage(connectMessage);
}
};
BotBehaviour bbWaitForConnectFromAtom1 = new BotBehaviour(ctx, "bbWaitForConnectFromAtom1") {
@Override
protected void onActivate(Optional<Object> message) {
bus.subscribe(ConnectFromOtherAtomEvent.class, new EventFilter() {
@Override
public boolean accept(Event event) {
return event instanceof ConnectFromOtherAtomEvent && ((ConnectFromOtherAtomEvent) event).getSenderSocket().equals(URI.create(atomUri1.toString() + "#chatSocket"));
}
}, new BaseEventBotAction(ctx) {
@Override
protected void doRun(Event event, EventListener executingListener) throws Exception {
passTest(bus);
}
});
}
};
BehaviourBarrier barrier = new BehaviourBarrier(ctx);
barrier.waitFor(bbCreateAtom1, bbCreateAtom2);
barrier.thenStart(bbSendConnect);
barrier.activate();
bbCreateAtom1.activate();
bbCreateAtom2.activate();
bbWaitForConnectFromAtom1.activate();
});
}
use of won.utils.content.model.AtomContent in project webofneeds by researchstudio-sat.
the class AclTests method testGroupChat.
@Test(timeout = 60 * 1000)
public void testGroupChat() throws Exception {
runTest(ctx -> {
EventBus bus = ctx.getEventBus();
final URI wonNodeUri = ctx.getNodeURISource().getNodeURI();
final URI atomUri1 = ctx.getWonNodeInformationService().generateAtomURI(wonNodeUri);
final URI atomUri2 = ctx.getWonNodeInformationService().generateAtomURI(wonNodeUri);
final URI atomUri3 = ctx.getWonNodeInformationService().generateAtomURI(wonNodeUri);
final URI atomUri4 = ctx.getWonNodeInformationService().generateAtomURI(wonNodeUri);
final URI atom1GroupSocket = URI.create(atomUri1.toString() + "#groupSocket");
final BotBehaviour bbCreateAtom1 = new BotBehaviour(ctx, "bbCreateAtom1") {
@Override
protected void onActivate(Optional<Object> message) {
final String atomUriString = atomUri1.toString();
AtomContent atomContent = AtomContent.builder(atomUri1).addTitle("Group Chat Atom (1/4)").addSocket(Socket.builder(atom1GroupSocket).setSocketDefinition(WXGROUP.GroupSocket.asURI()).build()).addType(URI.create(WON.Atom.getURI())).build();
Authorization autoConnectGroupSocket = Authorization.builder().setGranteeGranteeWildcard(GranteeWildcard.ANYONE).addGrant(b -> b.addSocket(s -> s.addSocketType(WXGROUP.GroupSocket.asURI()).addOperationsSimpleOperationExpression(OP_AUTO_CONNECT))).build();
WonMessage createMessage = WonMessageBuilder.createAtom().atom(atomUri1).content().graph(RdfOutput.toGraph(atomContent)).content().aclGraph(won.auth.model.RdfOutput.toGraph(getAnyoneMayConnectAuth())).content().aclGraph(won.auth.model.RdfOutput.toGraph(getAnyoneMayReadAnything())).content().aclGraph(won.auth.model.RdfOutput.toGraph(autoConnectGroupSocket)).build();
createMessage = ctx.getWonMessageSender().prepareMessage(createMessage);
ctx.getBotContextWrapper().rememberAtomUri(atomUri1);
EventListener successCallback = event -> {
logger.debug("group chat atom created");
deactivate();
};
EventListener failureCallback = makeFailureCallbackToFailTest(bot, ctx, bus, "Create group chat atom");
EventBotActionUtils.makeAndSubscribeResponseListener(createMessage, successCallback, failureCallback, ctx);
ctx.getWonMessageSender().sendMessage(createMessage);
}
};
final BotBehaviour bbCreateAtom2 = new BotBehaviour(ctx, "bbCreateAtom2") {
@Override
protected void onActivate(Optional<Object> message) {
final String atomUriString = atomUri2.toString();
AtomContent atomContent = AtomContent.builder(atomUri2).addTitle("member atom A (2/4)").addSocket(Socket.builder(atomUriString + "#chatSocket").setSocketDefinition(WXCHAT.ChatSocket.asURI()).build()).addType(URI.create(WON.Atom.getURI())).build();
WonMessage createMessage = WonMessageBuilder.createAtom().atom(atomUri2).content().graph(RdfOutput.toGraph(atomContent)).content().aclGraph(won.auth.model.RdfOutput.toGraph(getAnyoneMayConnectAuth())).content().aclGraph(won.auth.model.RdfOutput.toGraph(getAnyoneMayReadAnything())).build();
createMessage = ctx.getWonMessageSender().prepareMessage(createMessage);
ctx.getBotContextWrapper().rememberAtomUri(atomUri2);
EventListener successCallback = event -> {
logger.debug("member atom A created");
deactivate();
};
EventListener failureCallback = makeFailureCallbackToFailTest(bot, ctx, bus, "Create member atom A");
EventBotActionUtils.makeAndSubscribeResponseListener(createMessage, successCallback, failureCallback, ctx);
ctx.getWonMessageSender().sendMessage(createMessage);
}
};
final BotBehaviour bbCreateAtom3 = new BotBehaviour(ctx, "bbCreateAtom3") {
@Override
protected void onActivate(Optional<Object> message) {
final String atomUriString = atomUri3.toString();
AtomContent atomContent = AtomContent.builder(atomUri3).addTitle("member atom B (3/4)").addSocket(Socket.builder(atomUriString + "#chatSocket").setSocketDefinition(WXCHAT.ChatSocket.asURI()).build()).addType(URI.create(WON.Atom.getURI())).build();
WonMessage createMessage = WonMessageBuilder.createAtom().atom(atomUri3).content().graph(RdfOutput.toGraph(atomContent)).content().aclGraph(won.auth.model.RdfOutput.toGraph(getAnyoneMayConnectAuth())).content().aclGraph(won.auth.model.RdfOutput.toGraph(getAnyoneMayReadAnything())).build();
createMessage = ctx.getWonMessageSender().prepareMessage(createMessage);
ctx.getBotContextWrapper().rememberAtomUri(atomUri3);
EventListener successCallback = event -> {
logger.debug("member atom B created");
deactivate();
};
EventListener failureCallback = makeFailureCallbackToFailTest(bot, ctx, bus, "Creating member atom B");
EventBotActionUtils.makeAndSubscribeResponseListener(createMessage, successCallback, failureCallback, ctx);
ctx.getWonMessageSender().sendMessage(createMessage);
}
};
final BotBehaviour bbCreateAtom4 = new BotBehaviour(ctx, "bbCreateAtom4") {
@Override
protected void onActivate(Optional<Object> message) {
final String atomUriString = atomUri4.toString();
AtomContent atomContent = AtomContent.builder(atomUri4).addTitle("member atom C (4/4)").addSocket(Socket.builder(atomUriString + "#chatSocket").setSocketDefinition(WXCHAT.ChatSocket.asURI()).build()).addType(URI.create(WON.Atom.getURI())).build();
WonMessage createMessage = WonMessageBuilder.createAtom().atom(atomUri4).content().graph(RdfOutput.toGraph(atomContent)).content().aclGraph(won.auth.model.RdfOutput.toGraph(getAnyoneMayConnectAuth())).content().aclGraph(won.auth.model.RdfOutput.toGraph(getAnyoneMayReadAnything())).build();
createMessage = ctx.getWonMessageSender().prepareMessage(createMessage);
ctx.getBotContextWrapper().rememberAtomUri(atomUri4);
EventListener successCallback = event -> {
logger.debug("member atom C created");
deactivate();
};
EventListener failureCallback = makeFailureCallbackToFailTest(bot, ctx, bus, "Creating member atom C");
EventBotActionUtils.makeAndSubscribeResponseListener(createMessage, successCallback, failureCallback, ctx);
ctx.getWonMessageSender().sendMessage(createMessage);
}
};
BotBehaviour bbSendConnect21 = new BotBehaviour(ctx, "bbSendConnect21") {
@Override
protected void onActivate(Optional<Object> message) {
WonMessage connectMessage = WonMessageBuilder.connect().sockets().sender(URI.create(atomUri2.toString() + "#chatSocket")).recipient(atom1GroupSocket).direction().fromOwner().build();
connectMessage = ctx.getWonMessageSender().prepareMessage(connectMessage);
EventListener successCallback = event -> {
logger.debug("member A -> group connection requested");
deactivate();
};
EventListener failureCallback = makeFailureCallbackToFailTest(bot, ctx, bus, "Requesting connection member A -> group");
EventBotActionUtils.makeAndSubscribeResponseListener(connectMessage, successCallback, failureCallback, ctx);
ctx.getWonMessageSender().sendMessage(connectMessage);
}
};
BotBehaviour bbSendConnect31 = new BotBehaviour(ctx, "bbSendConnect31") {
@Override
protected void onActivate(Optional<Object> message) {
WonMessage connectMessage = WonMessageBuilder.connect().sockets().sender(URI.create(atomUri3.toString() + "#chatSocket")).recipient(atom1GroupSocket).direction().fromOwner().build();
connectMessage = ctx.getWonMessageSender().prepareMessage(connectMessage);
EventListener successCallback = event -> {
logger.debug("member B -> group connection requested");
deactivate();
};
EventListener failureCallback = makeFailureCallbackToFailTest(bot, ctx, bus, "Requesting connection member B -> group");
EventBotActionUtils.makeAndSubscribeResponseListener(connectMessage, successCallback, failureCallback, ctx);
ctx.getWonMessageSender().sendMessage(connectMessage);
}
};
BotBehaviour bbSendConnect41 = new BotBehaviour(ctx, "bbSendConnect41") {
@Override
protected void onActivate(Optional<Object> message) {
WonMessage connectMessage = WonMessageBuilder.connect().sockets().sender(URI.create(atomUri4.toString() + "#chatSocket")).recipient(atom1GroupSocket).direction().fromOwner().build();
connectMessage = ctx.getWonMessageSender().prepareMessage(connectMessage);
EventListener successCallback = event -> {
logger.debug("member C -> group connection requested");
deactivate();
};
EventListener failureCallback = makeFailureCallbackToFailTest(bot, ctx, bus, "Requesting connection member C -> group");
EventBotActionUtils.makeAndSubscribeResponseListener(connectMessage, successCallback, failureCallback, ctx);
ctx.getWonMessageSender().sendMessage(connectMessage);
}
};
BotBehaviour bbWaitForAutoConnect = new BotBehaviour(ctx) {
@Override
protected void onActivate(Optional<Object> message) {
bus.subscribe(ConnectFromOtherAtomEvent.class, new ActionOnceAfterNEventsListener(ctx, new EventFilter() {
@Override
public boolean accept(Event event) {
boolean waitingForThis = ((ConnectFromOtherAtomEvent) event).getSenderSocket().equals(atom1GroupSocket);
return waitingForThis;
}
}, 3, new EventBotAction() {
@Override
public Runnable getActionTask(Event event, EventListener eventListener) {
return () -> deactivate();
}
}));
}
};
BotBehaviour bbSendMsg41 = new BotBehaviour(ctx, "bbSendMsg41") {
@Override
protected void onActivate(Optional<Object> message) {
WonMessage connectMessage = WonMessageBuilder.connectionMessage().sockets().sender(URI.create(atomUri4.toString() + "#chatSocket")).recipient(URI.create(atomUri1.toString() + "#groupSocket")).direction().fromOwner().content().text("Hello, world!").build();
connectMessage = ctx.getWonMessageSender().prepareMessage(connectMessage);
EventListener successCallback = event -> {
logger.debug("member C -> send message to group");
deactivate();
};
EventListener failureCallback = makeFailureCallbackToFailTest(bot, ctx, bus, "sending message member C -> group");
EventBotActionUtils.makeAndSubscribeResponseListener(connectMessage, successCallback, failureCallback, ctx);
ctx.getWonMessageSender().sendMessage(connectMessage);
}
};
BotBehaviour bbWaitForGroupMessage = new BotBehaviour(ctx) {
@Override
protected void onActivate(Optional<Object> message) {
bus.subscribe(MessageFromOtherAtomEvent.class, new ActionOnceAfterNEventsListener(ctx, new EventFilter() {
@Override
public boolean accept(Event event) {
boolean interestedInThis = ((MessageFromOtherAtomEvent) event).getWonMessage().getSenderSocketURIRequired().equals(atom1GroupSocket);
return interestedInThis;
}
}, 2, new EventBotAction() {
@Override
public Runnable getActionTask(Event event, EventListener eventListener) {
return () -> passTest(bus);
}
}));
}
};
BehaviourBarrier barrier = new BehaviourBarrier(ctx);
barrier.waitFor(bbCreateAtom1, bbCreateAtom2, bbCreateAtom3, bbCreateAtom4);
barrier.thenStart(bbSendConnect21, bbSendConnect31, bbSendConnect41);
barrier.activate();
bbWaitForAutoConnect.onDeactivateActivate(bbSendMsg41);
bbWaitForGroupMessage.activate();
bbWaitForAutoConnect.activate();
bbCreateAtom1.activate();
bbCreateAtom2.activate();
bbCreateAtom3.activate();
bbCreateAtom4.activate();
});
}
use of won.utils.content.model.AtomContent in project webofneeds by researchstudio-sat.
the class AclTests method testCreateAtomWithEmptyACLButSocketAuths_isNotPubliclyReadable.
@Test(timeout = 60 * 1000)
public void testCreateAtomWithEmptyACLButSocketAuths_isNotPubliclyReadable() throws Exception {
runTest(ctx -> {
EventBus bus = ctx.getEventBus();
final URI wonNodeUri = ctx.getNodeURISource().getNodeURI();
final URI atomUri = ctx.getWonNodeInformationService().generateAtomURI(wonNodeUri);
final String atomUriString = atomUri.toString();
final AtomContent atomContent = new AtomContent(atomUriString);
atomContent.addTitle("Unit Test Atom ACL Test 1");
final Socket holderSocket = new Socket(atomUriString + "#holderSocket");
holderSocket.setSocketDefinition(WXHOLD.HolderSocket.asURI());
final Socket buddySocket = new Socket(atomUriString + "#buddySocket");
buddySocket.setSocketDefinition(WXBUDDY.BuddySocket.asURI());
atomContent.addSocket(holderSocket);
atomContent.addSocket(buddySocket);
atomContent.addType(URI.create(WON.Atom.getURI()));
WonMessage createMessage = WonMessageBuilder.createAtom().atom(atomUri).content().graph(RdfOutput.toGraph(atomContent)).content().aclGraph(// add an empty acl graph
GraphFactory.createGraphMem()).build();
createMessage = ctx.getWonMessageSender().prepareMessage(createMessage);
ctx.getBotContextWrapper().rememberAtomUri(atomUri);
final String action = "Create Atom action";
EventListener successCallback = event -> {
((CachingLinkedDataSource) ctx.getLinkedDataSource()).clear();
boolean passed = true;
passed = passed && testLinkedDataRequestOk(ctx, bus, "withWebid", atomUri, atomUri);
passed = passed && testLinkedDataRequestFailsNoWebId(ctx, bus, "withoutWebId", LinkedDataFetchingException.Forbidden.class);
if (passed) {
passTest(bus);
}
};
EventListener failureCallback = makeFailureCallbackToFailTest(bot, ctx, bus, action);
EventBotActionUtils.makeAndSubscribeResponseListener(createMessage, successCallback, failureCallback, ctx);
ctx.getWonMessageSender().sendMessage(createMessage);
});
}
Aggregations