Search in sources :

Example 6 with MoodElement

use of org.jivesoftware.smackx.mood.element.MoodElement in project Smack by igniterealtime.

the class MoodManager method clearMood.

public void clearMood() throws InterruptedException, SmackException.NotLoggedInException, SmackException.NoResponseException, SmackException.NotConnectedException, XMPPException.XMPPErrorException, PubSubException.NotALeafNodeException {
    MoodElement element = buildMood(null, null, null);
    publishMood(element);
}
Also used : MoodElement(org.jivesoftware.smackx.mood.element.MoodElement)

Example 7 with MoodElement

use of org.jivesoftware.smackx.mood.element.MoodElement in project Smack by igniterealtime.

the class MoodProvider method parse.

@Override
public MoodElement parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException {
    String text = null;
    Mood mood = null;
    MoodConcretisation concretisation = null;
    outerloop: while (true) {
        XmlPullParser.Event tag = parser.next();
        switch(tag) {
            case START_ELEMENT:
                String name = parser.getName();
                String namespace = parser.getNamespace();
                if (MoodElement.ELEM_TEXT.equals(name)) {
                    text = parser.nextText();
                    continue outerloop;
                }
                if (!MoodElement.NAMESPACE.equals(namespace)) {
                    LOGGER.log(Level.FINE, "Foreign namespace " + namespace + " detected. Try to find suitable MoodConcretisationProvider.");
                    MoodConcretisationProvider<?> provider = (MoodConcretisationProvider<?>) ProviderManager.getExtensionProvider(name, namespace);
                    if (provider != null) {
                        concretisation = provider.parse(parser);
                    } else {
                        LOGGER.log(Level.FINE, "No provider for <" + name + " xmlns:'" + namespace + "'/> found. Ignore.");
                    }
                    continue outerloop;
                }
                try {
                    mood = Mood.valueOf(name);
                    continue outerloop;
                } catch (IllegalArgumentException e) {
                    throw new XmlPullParserException("Unknown mood value: " + name + " encountered.");
                }
            case END_ELEMENT:
                if (MoodElement.ELEMENT.equals(parser.getName())) {
                    MoodElement.MoodSubjectElement subjectElement = (mood == null && concretisation == null) ? null : new MoodElement.MoodSubjectElement(mood, concretisation);
                    return new MoodElement(subjectElement, text);
                }
                break;
            default:
                // Catch all for incomplete switch (MissingCasesInEnumSwitch) statement.
                break;
        }
    }
}
Also used : Mood(org.jivesoftware.smackx.mood.Mood) MoodConcretisation(org.jivesoftware.smackx.mood.element.MoodConcretisation) XmlPullParserException(org.jivesoftware.smack.xml.XmlPullParserException) MoodElement(org.jivesoftware.smackx.mood.element.MoodElement)

Example 8 with MoodElement

use of org.jivesoftware.smackx.mood.element.MoodElement in project Smack by igniterealtime.

the class MoodIntegrationTest method registerListenerAndWait.

/**
 * Registers a listener for User Tune data. This implicitly publishes a CAPS update to include a notification
 * filter for the mood node. This method blocks until the server has indicated that this update has been
 * received.
 *
 * @param moodManager The MoodManager instance for the connection that is expected to receive data.
 * @param discoManager The ServiceDiscoveryManager instance for the connection that is expected to publish data.
 * @param listener A listener instance for Mood data that is to be registered.
 *
 * @throws Exception if the test fails
 */
public void registerListenerAndWait(MoodManager moodManager, ServiceDiscoveryManager discoManager, PepEventListener<MoodElement> listener) throws Exception {
    final SimpleResultSyncPoint notificationFilterReceived = new SimpleResultSyncPoint();
    final EntityCapabilitiesChangedListener notificationFilterReceivedListener = info -> {
        if (info.containsFeature(MoodManager.MOOD_NODE + "+notify")) {
            notificationFilterReceived.signal();
        }
    };
    discoManager.addEntityCapabilitiesChangedListener(notificationFilterReceivedListener);
    try {
        moodManager.addMoodListener(listener);
        notificationFilterReceived.waitForResult(timeout);
    } finally {
        discoManager.removeEntityCapabilitiesChangedListener(notificationFilterReceivedListener);
    }
}
Also used : SmackException(org.jivesoftware.smack.SmackException) SmackIntegrationTestEnvironment(org.igniterealtime.smack.inttest.SmackIntegrationTestEnvironment) AfterClass(org.igniterealtime.smack.inttest.annotations.AfterClass) EntityCapabilitiesChangedListener(org.jivesoftware.smackx.disco.EntityCapabilitiesChangedListener) SimpleResultSyncPoint(org.igniterealtime.smack.inttest.util.SimpleResultSyncPoint) AbstractSmackIntegrationTest(org.igniterealtime.smack.inttest.AbstractSmackIntegrationTest) ServiceDiscoveryManager(org.jivesoftware.smackx.disco.ServiceDiscoveryManager) TimeoutException(java.util.concurrent.TimeoutException) MoodElement(org.jivesoftware.smackx.mood.element.MoodElement) PepEventListener(org.jivesoftware.smackx.pep.PepEventListener) Assertions(org.junit.jupiter.api.Assertions) IntegrationTestRosterUtil(org.igniterealtime.smack.inttest.util.IntegrationTestRosterUtil) SmackIntegrationTest(org.igniterealtime.smack.inttest.annotations.SmackIntegrationTest) XMPPException(org.jivesoftware.smack.XMPPException) EntityCapabilitiesChangedListener(org.jivesoftware.smackx.disco.EntityCapabilitiesChangedListener) SimpleResultSyncPoint(org.igniterealtime.smack.inttest.util.SimpleResultSyncPoint)

Example 9 with MoodElement

use of org.jivesoftware.smackx.mood.element.MoodElement in project Smack by igniterealtime.

the class MoodIntegrationTest method testNotificationAfterFilterChange.

/**
 * Verifies that a notification for a previously sent publication is received as soon as notification filtering
 * has been adjusted to allow for the notification to be delivered.
 *
 * @throws Exception if the test fails
 */
@SmackIntegrationTest
public void testNotificationAfterFilterChange() throws Exception {
    Mood data = Mood.cautious;
    IntegrationTestRosterUtil.ensureBothAccountsAreSubscribedToEachOther(conOne, conTwo, timeout);
    final SimpleResultSyncPoint moodReceived = new SimpleResultSyncPoint();
    final PepEventListener<MoodElement> moodListener = (jid, moodElement, id, message) -> {
        if (moodElement.getMood().equals(data)) {
            moodReceived.signal();
        }
    };
    // TODO Ensure that pre-existing filtering notification excludes mood.
    try {
        // Publish the data
        publishAndWait(mm1, ServiceDiscoveryManager.getInstanceFor(conOne), data);
        // Adds listener, which implicitly publishes a disco/info filter for mood notification.
        registerListenerAndWait(mm2, ServiceDiscoveryManager.getInstanceFor(conTwo), moodListener);
        // Wait for the data to be received.
        try {
            Object result = moodReceived.waitForResult(timeout);
            // Explicitly assert the success case.
            Assertions.assertNotNull(result, "Expected to receive a PEP notification, but did not.");
        } catch (TimeoutException e) {
            Assertions.fail("Expected to receive a PEP notification, but did not.");
        }
    } finally {
        unregisterListener(mm2, moodListener);
    }
}
Also used : SmackException(org.jivesoftware.smack.SmackException) SmackIntegrationTestEnvironment(org.igniterealtime.smack.inttest.SmackIntegrationTestEnvironment) AfterClass(org.igniterealtime.smack.inttest.annotations.AfterClass) EntityCapabilitiesChangedListener(org.jivesoftware.smackx.disco.EntityCapabilitiesChangedListener) SimpleResultSyncPoint(org.igniterealtime.smack.inttest.util.SimpleResultSyncPoint) AbstractSmackIntegrationTest(org.igniterealtime.smack.inttest.AbstractSmackIntegrationTest) ServiceDiscoveryManager(org.jivesoftware.smackx.disco.ServiceDiscoveryManager) TimeoutException(java.util.concurrent.TimeoutException) MoodElement(org.jivesoftware.smackx.mood.element.MoodElement) PepEventListener(org.jivesoftware.smackx.pep.PepEventListener) Assertions(org.junit.jupiter.api.Assertions) IntegrationTestRosterUtil(org.igniterealtime.smack.inttest.util.IntegrationTestRosterUtil) SmackIntegrationTest(org.igniterealtime.smack.inttest.annotations.SmackIntegrationTest) XMPPException(org.jivesoftware.smack.XMPPException) MoodElement(org.jivesoftware.smackx.mood.element.MoodElement) SimpleResultSyncPoint(org.igniterealtime.smack.inttest.util.SimpleResultSyncPoint) TimeoutException(java.util.concurrent.TimeoutException) AbstractSmackIntegrationTest(org.igniterealtime.smack.inttest.AbstractSmackIntegrationTest) SmackIntegrationTest(org.igniterealtime.smack.inttest.annotations.SmackIntegrationTest)

Example 10 with MoodElement

use of org.jivesoftware.smackx.mood.element.MoodElement in project Smack by igniterealtime.

the class MoodConcretisationTest method concretisationTest.

@Test
public void concretisationTest() throws Exception {
    ProviderManager.addExtensionProvider(EcstaticMoodConcretisation.ELEMENT, EcstaticMoodConcretisation.NAMESPACE, EcstaticMoodConcretisationProvider.INSTANCE);
    String xml = "<mood xmlns='http://jabber.org/protocol/mood'>" + "<happy>" + "<ecstatic xmlns='https://example.org/'/>" + "</happy>" + "<text>Yay, the mood spec has been approved!</text>" + "</mood>";
    MoodElement element = new MoodElement(new MoodElement.MoodSubjectElement(Mood.happy, new EcstaticMoodConcretisation()), "Yay, the mood spec has been approved!");
    assertXmlSimilar(xml, element.toXML().toString());
    XmlPullParser parser = TestUtils.getParser(xml);
    MoodElement parsed = MoodProvider.INSTANCE.parse(parser);
    assertXmlSimilar(xml, parsed.toXML().toString());
    assertTrue(parsed.hasConcretisation());
    assertTrue(parsed.hasText());
    assertEquals(EcstaticMoodConcretisation.ELEMENT, parsed.getMoodConcretisation().getMood());
}
Also used : XmlPullParser(org.jivesoftware.smack.xml.XmlPullParser) MoodElement(org.jivesoftware.smackx.mood.element.MoodElement) Test(org.junit.jupiter.api.Test)

Aggregations

MoodElement (org.jivesoftware.smackx.mood.element.MoodElement)13 Test (org.junit.jupiter.api.Test)5 TimeoutException (java.util.concurrent.TimeoutException)4 AbstractSmackIntegrationTest (org.igniterealtime.smack.inttest.AbstractSmackIntegrationTest)4 SmackIntegrationTestEnvironment (org.igniterealtime.smack.inttest.SmackIntegrationTestEnvironment)4 AfterClass (org.igniterealtime.smack.inttest.annotations.AfterClass)4 SmackIntegrationTest (org.igniterealtime.smack.inttest.annotations.SmackIntegrationTest)4 IntegrationTestRosterUtil (org.igniterealtime.smack.inttest.util.IntegrationTestRosterUtil)4 SimpleResultSyncPoint (org.igniterealtime.smack.inttest.util.SimpleResultSyncPoint)4 SmackException (org.jivesoftware.smack.SmackException)4 XMPPException (org.jivesoftware.smack.XMPPException)4 XmlPullParser (org.jivesoftware.smack.xml.XmlPullParser)4 EntityCapabilitiesChangedListener (org.jivesoftware.smackx.disco.EntityCapabilitiesChangedListener)4 ServiceDiscoveryManager (org.jivesoftware.smackx.disco.ServiceDiscoveryManager)4 PepEventListener (org.jivesoftware.smackx.pep.PepEventListener)4 Assertions (org.junit.jupiter.api.Assertions)4 Message (org.jivesoftware.smack.packet.Message)1 XmlPullParserException (org.jivesoftware.smack.xml.XmlPullParserException)1 Mood (org.jivesoftware.smackx.mood.Mood)1 MoodConcretisation (org.jivesoftware.smackx.mood.element.MoodConcretisation)1