Search in sources :

Example 1 with MamQuery

use of org.jivesoftware.smackx.mam.MamManager.MamQuery in project Smack by igniterealtime.

the class MamIntegrationTest method mamPageTest.

@SmackIntegrationTest
public void mamPageTest() throws TimeoutException, Exception {
    final int messagesPerPage = 10;
    final int numPages = 3;
    final int totalMessages = messagesPerPage * numPages;
    final List<Message> outgoingMessages = new ArrayList<>(totalMessages);
    final EntityBareJid userOne = conOne.getUser().asEntityBareJid();
    final EntityBareJid userTwo = conTwo.getUser().asEntityBareJid();
    final SimpleResultSyncPoint allMessagesReceived = new SimpleResultSyncPoint();
    final String lastMessageArchiveUid = mamManagerConTwo.getMessageUidOfLatestMessage();
    for (int i = 0; i < totalMessages; i++) {
        String messageBody = "MAM Page Test " + testRunId + ' ' + (i + 1);
        Message message = StanzaBuilder.buildMessage().to(userTwo).setBody(messageBody).build();
        outgoingMessages.add(message);
    }
    final String lastBody = outgoingMessages.get(outgoingMessages.size() - 1).getBody();
    final StanzaListener stanzaListener = new StanzaListener() {

        @Override
        public void processStanza(Stanza stanza) {
            Message message = (Message) stanza;
            if (message.getBody().equals(lastBody)) {
                allMessagesReceived.signal();
            }
        }
    };
    conTwo.addAsyncStanzaListener(stanzaListener, MessageWithBodiesFilter.INSTANCE);
    try {
        for (Message message : outgoingMessages) {
            conOne.sendStanza(message);
        }
        allMessagesReceived.waitForResult(timeout);
    } finally {
        conTwo.removeAsyncStanzaListener(stanzaListener);
    }
    MamQueryArgs mamQueryArgs = MamQueryArgs.builder().setResultPageSize(messagesPerPage).limitResultsToJid(userOne).afterUid(lastMessageArchiveUid).build();
    MamQuery mamQuery = mamManagerConTwo.queryArchive(mamQueryArgs);
    assertFalse(mamQuery.isComplete());
    assertEquals(messagesPerPage, mamQuery.getMessageCount());
    List<List<Message>> pages = new ArrayList<>(numPages);
    pages.add(mamQuery.getMessages());
    for (int additionalPageRequestNum = 0; additionalPageRequestNum < numPages - 1; additionalPageRequestNum++) {
        List<Message> page = mamQuery.pageNext(messagesPerPage);
        boolean isLastQuery = additionalPageRequestNum == numPages - 2;
        if (isLastQuery) {
            assertTrue(mamQuery.isComplete());
        } else {
            assertFalse(mamQuery.isComplete());
        }
        assertEquals(messagesPerPage, page.size());
        pages.add(page);
    }
    List<Message> queriedMessages = new ArrayList<>(totalMessages);
    for (List<Message> messages : pages) {
        queriedMessages.addAll(messages);
    }
    assertEquals(outgoingMessages.size(), queriedMessages.size());
    for (int i = 0; i < outgoingMessages.size(); i++) {
        Message outgoingMessage = outgoingMessages.get(i);
        Message queriedMessage = queriedMessages.get(i);
        assertEquals(outgoingMessage.getBody(), queriedMessage.getBody());
    }
}
Also used : Message(org.jivesoftware.smack.packet.Message) Stanza(org.jivesoftware.smack.packet.Stanza) ArrayList(java.util.ArrayList) StanzaListener(org.jivesoftware.smack.StanzaListener) MamQuery(org.jivesoftware.smackx.mam.MamManager.MamQuery) SimpleResultSyncPoint(org.igniterealtime.smack.inttest.util.SimpleResultSyncPoint) ArrayList(java.util.ArrayList) List(java.util.List) EntityBareJid(org.jxmpp.jid.EntityBareJid) SimpleResultSyncPoint(org.igniterealtime.smack.inttest.util.SimpleResultSyncPoint) MamQueryArgs(org.jivesoftware.smackx.mam.MamManager.MamQueryArgs) SmackIntegrationTest(org.igniterealtime.smack.inttest.annotations.SmackIntegrationTest) AbstractSmackIntegrationTest(org.igniterealtime.smack.inttest.AbstractSmackIntegrationTest)

Example 2 with MamQuery

use of org.jivesoftware.smackx.mam.MamManager.MamQuery in project Smack by igniterealtime.

the class MamIntegrationTest method mamTest.

@SmackIntegrationTest
public void mamTest() throws TimeoutException, Exception {
    EntityBareJid userOne = conOne.getUser().asEntityBareJid();
    EntityBareJid userTwo = conTwo.getUser().asEntityBareJid();
    final String messageBody = "Test MAM message (" + testRunId + ')';
    Message message = conTwo.getStanzaFactory().buildMessageStanza().to(userTwo).setBody(messageBody).build();
    final String messageId = message.getStanzaId();
    final SimpleResultSyncPoint messageReceived = new SimpleResultSyncPoint();
    final StanzaListener stanzaListener = new StanzaListener() {

        @Override
        public void processStanza(Stanza stanza) {
            Message message = (Message) stanza;
            if (message.getBody().equals(messageBody)) {
                messageReceived.signal();
            }
        }
    };
    conTwo.addAsyncStanzaListener(stanzaListener, MessageWithBodiesFilter.INSTANCE);
    try {
        conOne.sendStanza(message);
        messageReceived.waitForResult(timeout);
    } finally {
        conTwo.removeAsyncStanzaListener(stanzaListener);
    }
    MamQueryArgs mamQueryArgs = MamQueryArgs.builder().setResultPageSizeTo(1).limitResultsToJid(userOne).queryLastPage().build();
    MamQuery mamQuery = mamManagerConTwo.queryArchive(mamQueryArgs);
    assertEquals(1, mamQuery.getMessages().size());
    Message mamMessage = mamQuery.getMessages().get(0);
    assertEquals(messageId, mamMessage.getStanzaId());
    assertEquals(messageBody, mamMessage.getBody());
    assertEquals(conOne.getUser(), mamMessage.getFrom());
    assertEquals(userTwo, mamMessage.getTo());
}
Also used : Message(org.jivesoftware.smack.packet.Message) Stanza(org.jivesoftware.smack.packet.Stanza) StanzaListener(org.jivesoftware.smack.StanzaListener) MamQuery(org.jivesoftware.smackx.mam.MamManager.MamQuery) EntityBareJid(org.jxmpp.jid.EntityBareJid) SimpleResultSyncPoint(org.igniterealtime.smack.inttest.util.SimpleResultSyncPoint) MamQueryArgs(org.jivesoftware.smackx.mam.MamManager.MamQueryArgs) SmackIntegrationTest(org.igniterealtime.smack.inttest.annotations.SmackIntegrationTest) AbstractSmackIntegrationTest(org.igniterealtime.smack.inttest.AbstractSmackIntegrationTest)

Aggregations

AbstractSmackIntegrationTest (org.igniterealtime.smack.inttest.AbstractSmackIntegrationTest)2 SmackIntegrationTest (org.igniterealtime.smack.inttest.annotations.SmackIntegrationTest)2 SimpleResultSyncPoint (org.igniterealtime.smack.inttest.util.SimpleResultSyncPoint)2 StanzaListener (org.jivesoftware.smack.StanzaListener)2 Message (org.jivesoftware.smack.packet.Message)2 Stanza (org.jivesoftware.smack.packet.Stanza)2 MamQuery (org.jivesoftware.smackx.mam.MamManager.MamQuery)2 MamQueryArgs (org.jivesoftware.smackx.mam.MamManager.MamQueryArgs)2 EntityBareJid (org.jxmpp.jid.EntityBareJid)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1