Search in sources :

Example 16 with AtomContent

use of won.utils.content.model.AtomContent in project webofneeds by researchstudio-sat.

the class SimpleTests method testCreatePersonaWithoutACL.

@Test(timeout = 60 * 1000)
public void testCreatePersonaWithoutACL() 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 ");
        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.Persona.getURI()));
        atomContent.addType(URI.create(WON.Atom.getURI()));
        WonMessage createMessage = WonMessageBuilder.createAtom().atom(atomUri).content().graph(RdfOutput.toGraph(atomContent)).build();
        createMessage = ctx.getWonMessageSender().prepareMessage(createMessage);
        ctx.getBotContextWrapper().rememberAtomUri(atomUri);
        final String action = "Create Atom action";
        EventListener successCallback = makeSuccessCallbackToPassTest(bot, bus, action);
        EventListener failureCallback = makeFailureCallbackToFailTest(bot, ctx, bus, action);
        EventBotActionUtils.makeAndSubscribeResponseListener(createMessage, successCallback, failureCallback, ctx);
        ctx.getWonMessageSender().sendMessage(createMessage);
    });
}
Also used : WonMessage(won.protocol.message.WonMessage) EventBus(won.bot.framework.eventbot.bus.EventBus) EventListener(won.bot.framework.eventbot.listener.EventListener) AtomContent(won.utils.content.model.AtomContent) URI(java.net.URI) Socket(won.utils.content.model.Socket) Test(org.junit.Test)

Example 17 with AtomContent

use of won.utils.content.model.AtomContent in project webofneeds by researchstudio-sat.

the class SimpleTests method testQueryBasedMatch.

@Test(timeout = 60 * 1000)
public void testQueryBasedMatch() throws Exception {
    final AtomicBoolean atom1Created = new AtomicBoolean(false);
    final AtomicBoolean atom2Created = new AtomicBoolean(false);
    runTest(ctx -> {
        EventBus bus = ctx.getEventBus();
        final URI wonNodeUri = ctx.getNodeURISource().getNodeURI();
        final URI atomUri1 = ctx.getWonNodeInformationService().generateAtomURI(wonNodeUri);
        {
            final String atomUriString = atomUri1.toString();
            final AtomContent atomContent = new AtomContent(atomUriString);
            atomContent.addTitle("Match target atom");
            atomContent.addTag("tag-to-match");
            final Socket chatSocket = new Socket(atomUriString + "#chatSocket");
            chatSocket.setSocketDefinition(WXCHAT.ChatSocket.asURI());
            atomContent.addSocket(chatSocket);
            atomContent.addType(URI.create(WON.Atom.getURI()));
            WonMessage createMessage = WonMessageBuilder.createAtom().atom(atomUri1).content().graph(RdfOutput.toGraph(atomContent)).build();
            createMessage = ctx.getWonMessageSender().prepareMessage(createMessage);
            ctx.getBotContextWrapper().rememberAtomUri(atomUri1);
            final String action = "Create match target atom";
            EventListener successCallback = event -> {
                logger.debug("Match target atom created");
                atom1Created.set(true);
            };
            EventListener failureCallback = makeFailureCallbackToFailTest(bot, ctx, bus, action);
            EventBotActionUtils.makeAndSubscribeResponseListener(createMessage, successCallback, failureCallback, ctx);
            ctx.getWonMessageSender().sendMessage(createMessage);
        }
        // create match source
        final URI atomUri2 = ctx.getWonNodeInformationService().generateAtomURI(wonNodeUri);
        {
            final String atomUriString = atomUri2.toString();
            final AtomContent atomContent = new AtomContent(atomUriString);
            atomContent.addTitle("Match source atom");
            atomContent.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\"." + "}");
            final Socket chatSocket = new Socket(atomUriString + "#chatSocket");
            chatSocket.setSocketDefinition(WXCHAT.ChatSocket.asURI());
            atomContent.addSocket(chatSocket);
            atomContent.addType(URI.create(WON.Atom.getURI()));
            WonMessage createMessage = WonMessageBuilder.createAtom().atom(atomUri2).content().graph(RdfOutput.toGraph(atomContent)).build();
            createMessage = ctx.getWonMessageSender().prepareMessage(createMessage);
            ctx.getBotContextWrapper().rememberAtomUri(atomUri2);
            final String action = "Create match source atom";
            EventListener successCallback = event -> {
                logger.debug("Match source atom created");
                atom2Created.set(true);
            };
            EventListener failureCallback = makeFailureCallbackToFailTest(bot, ctx, bus, action);
            EventBotActionUtils.makeAndSubscribeResponseListener(createMessage, successCallback, failureCallback, ctx);
            ctx.getWonMessageSender().sendMessage(createMessage);
        }
        // create listener waiting for the hint
        bus.subscribe(HintFromMatcherEvent.class, (Event event) -> ((HintFromMatcherEvent) event).getRecipientAtom().equals(atomUri2) && ((HintFromMatcherEvent) event).getHintTargetAtom().equals(atomUri1), makeAction(ctx, event -> bus.publish(new TestPassedEvent(bot))));
        logger.debug("Finished initializing test 'testQueryBasedMatch()'");
    });
}
Also used : Socket(won.utils.content.model.Socket) Logger(org.slf4j.Logger) WON(won.protocol.vocabulary.WON) TestPassedEvent(won.bot.framework.eventbot.event.impl.test.TestPassedEvent) MethodHandles(java.lang.invoke.MethodHandles) LoggerFactory(org.slf4j.LoggerFactory) WXBUDDY(won.protocol.vocabulary.WXBUDDY) EventBus(won.bot.framework.eventbot.bus.EventBus) RdfOutput(won.utils.content.model.RdfOutput) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test) EventBotActionUtils(won.bot.framework.eventbot.action.EventBotActionUtils) AtomContent(won.utils.content.model.AtomContent) HintFromMatcherEvent(won.bot.framework.eventbot.event.impl.wonmessage.HintFromMatcherEvent) WonMessage(won.protocol.message.WonMessage) WonMessageBuilder(won.protocol.message.builder.WonMessageBuilder) WXHOLD(won.protocol.vocabulary.WXHOLD) Event(won.bot.framework.eventbot.event.Event) EventBotActionUtils.makeAction(won.bot.framework.eventbot.action.EventBotActionUtils.makeAction) EventListener(won.bot.framework.eventbot.listener.EventListener) WXCHAT(won.protocol.vocabulary.WXCHAT) URI(java.net.URI) TestPassedEvent(won.bot.framework.eventbot.event.impl.test.TestPassedEvent) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) WonMessage(won.protocol.message.WonMessage) TestPassedEvent(won.bot.framework.eventbot.event.impl.test.TestPassedEvent) HintFromMatcherEvent(won.bot.framework.eventbot.event.impl.wonmessage.HintFromMatcherEvent) Event(won.bot.framework.eventbot.event.Event) EventBus(won.bot.framework.eventbot.bus.EventBus) EventListener(won.bot.framework.eventbot.listener.EventListener) AtomContent(won.utils.content.model.AtomContent) URI(java.net.URI) Socket(won.utils.content.model.Socket) Test(org.junit.Test)

Aggregations

URI (java.net.URI)17 Test (org.junit.Test)17 EventBus (won.bot.framework.eventbot.bus.EventBus)17 EventListener (won.bot.framework.eventbot.listener.EventListener)17 WonMessage (won.protocol.message.WonMessage)17 AtomContent (won.utils.content.model.AtomContent)17 MethodHandles (java.lang.invoke.MethodHandles)16 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)16 Logger (org.slf4j.Logger)16 LoggerFactory (org.slf4j.LoggerFactory)16 EventBotActionUtils (won.bot.framework.eventbot.action.EventBotActionUtils)16 Event (won.bot.framework.eventbot.event.Event)16 WonMessageBuilder (won.protocol.message.builder.WonMessageBuilder)16 RdfOutput (won.utils.content.model.RdfOutput)16 HashSet (java.util.HashSet)15 Iterator (java.util.Iterator)15 Optional (java.util.Optional)15 Set (java.util.Set)15 CountDownLatch (java.util.concurrent.CountDownLatch)15 AtomicReference (java.util.concurrent.atomic.AtomicReference)15