Search in sources :

Example 11 with SmackIntegrationTest

use of org.igniterealtime.smack.inttest.SmackIntegrationTest in project Smack by igniterealtime.

the class FormTest method testFilloutForm.

/**
     * 1. Create a form to fill out and send it to the other user
     * 2. Retrieve the form to fill out, complete it and return it to the requestor
     * 3. Retrieve the completed form and check that everything is OK
     *
     * @throws InterruptedException 
     * @throws NotConnectedException 
     */
@SuppressWarnings("deprecation")
@SmackIntegrationTest
public void testFilloutForm() throws NotConnectedException, InterruptedException {
    Form formToSend = new Form(DataForm.Type.form);
    formToSend.setInstructions("Fill out this form to report your case.\nThe case will be created automatically.");
    formToSend.setTitle("Case configurations");
    // Add a hidden variable
    FormField field = new FormField("hidden_var");
    field.setType(FormField.Type.hidden);
    field.addValue("Some value for the hidden variable");
    formToSend.addField(field);
    // Add a fixed variable
    field = new FormField();
    field.addValue("Section 1: Case description");
    formToSend.addField(field);
    // Add a text-single variable
    field = new FormField("name");
    field.setLabel("Enter a name for the case");
    field.setType(FormField.Type.text_single);
    formToSend.addField(field);
    // Add a text-multi variable
    field = new FormField("description");
    field.setLabel("Enter a description");
    field.setType(FormField.Type.text_multi);
    formToSend.addField(field);
    // Add a boolean variable
    field = new FormField("time");
    field.setLabel("Is this your first case?");
    field.setType(FormField.Type.bool);
    formToSend.addField(field);
    // Add a text variable where an int value is expected
    field = new FormField("age");
    field.setLabel("How old are you?");
    field.setType(FormField.Type.text_single);
    formToSend.addField(field);
    // Create the chats between the two participants
    org.jivesoftware.smack.chat.Chat chat = org.jivesoftware.smack.chat.ChatManager.getInstanceFor(conOne).createChat(conTwo.getUser(), null);
    StanzaCollector collector = conOne.createStanzaCollector(new ThreadFilter(chat.getThreadID()));
    StanzaCollector collector2 = conTwo.createStanzaCollector(new ThreadFilter(chat.getThreadID()));
    Message msg = new Message();
    msg.setBody("To enter a case please fill out this form and send it back to me");
    msg.addExtension(formToSend.getDataFormToSend());
    try {
        // Send the message with the form to fill out
        chat.sendMessage(msg);
        // Get the message with the form to fill out
        Message msg2 = (Message) collector2.nextResult();
        assertNotNull("Messge not found", msg2);
        // Retrieve the form to fill out
        Form formToRespond = Form.getFormFrom(msg2);
        assertNotNull(formToRespond);
        assertNotNull(formToRespond.getField("name"));
        assertNotNull(formToRespond.getField("description"));
        // Obtain the form to send with the replies
        Form completedForm = formToRespond.createAnswerForm();
        assertNotNull(completedForm.getField("hidden_var"));
        // Check that a field of type String does not accept booleans
        try {
            completedForm.setAnswer("name", true);
            fail("A boolean value was set to a field of type String");
        } catch (IllegalArgumentException e) {
        // This exception is expected.
        }
        completedForm.setAnswer("name", "Credit card number invalid");
        completedForm.setAnswer("description", "The ATM says that my credit card number is invalid. What's going on?");
        completedForm.setAnswer("time", true);
        completedForm.setAnswer("age", 20);
        // Create a new message to send with the completed form
        msg2 = new Message();
        msg2.setTo(conOne.getUser().asBareJid());
        msg2.setThread(msg.getThread());
        msg2.setType(Message.Type.chat);
        msg2.setBody("To enter a case please fill out this form and send it back to me");
        // Add the completed form to the message
        msg2.addExtension(completedForm.getDataFormToSend());
        // Send the message with the completed form
        conTwo.sendStanza(msg2);
        // Get the message with the completed form
        Message msg3 = (Message) collector.nextResult();
        assertNotNull("Messge not found", msg3);
        // Retrieve the completed form
        completedForm = Form.getFormFrom(msg3);
        assertNotNull(completedForm);
        assertNotNull(completedForm.getField("name"));
        assertNotNull(completedForm.getField("description"));
        assertEquals(completedForm.getField("name").getValues().get(0), "Credit card number invalid");
        assertNotNull(completedForm.getField("time"));
        assertNotNull(completedForm.getField("age"));
        assertEquals("The age is bad", "20", completedForm.getField("age").getValues().get(0));
    } finally {
        collector.cancel();
        collector2.cancel();
    }
}
Also used : Message(org.jivesoftware.smack.packet.Message) DataForm(org.jivesoftware.smackx.xdata.packet.DataForm) ThreadFilter(org.jivesoftware.smack.filter.ThreadFilter) StanzaCollector(org.jivesoftware.smack.StanzaCollector) AbstractSmackIntegrationTest(org.igniterealtime.smack.inttest.AbstractSmackIntegrationTest) SmackIntegrationTest(org.igniterealtime.smack.inttest.SmackIntegrationTest)

Example 12 with SmackIntegrationTest

use of org.igniterealtime.smack.inttest.SmackIntegrationTest in project Smack by igniterealtime.

the class IoTControlIntegrationTest method controlTest.

/**
     * Connection one provides a thing, which is controlled by connection two.
     *
     * @throws Exception 
     * @throws TimeoutException 
     */
@SmackIntegrationTest
public // @SmackSerialIntegrationTest
void controlTest() throws TimeoutException, Exception {
    final String key = StringUtils.randomString(12);
    final String sn = StringUtils.randomString(12);
    final SimpleResultSyncPoint syncPoint = new SimpleResultSyncPoint();
    Thing controlThing = Thing.builder().setKey(key).setSerialNumber(sn).setControlRequestHandler(new ThingControlRequest() {

        @Override
        public void processRequest(Jid from, Collection<SetData> setData) throws XMPPErrorException {
            if (!from.equals(conTwo.getUser())) {
                return;
            }
            for (final SetData data : setData) {
                if (!data.getName().equals(testRunId))
                    continue;
                if (!(data instanceof SetBoolData))
                    continue;
                SetBoolData boolData = (SetBoolData) data;
                if (boolData.getBooleanValue()) {
                    syncPoint.signal();
                    break;
                }
            }
        }
    }).build();
    IoTControlManagerOne.installThing(controlThing);
    try {
        RosterIntegrationTest.ensureBothAccountsAreSubscribedToEachOther(conOne, conTwo, timeout);
        SetData data = new SetBoolData(testRunId, true);
        IoTSetResponse response = IoTControlManagerTwo.setUsingIq(conOne.getUser(), data);
        assertNotNull(response);
    } finally {
        IoTControlManagerOne.uninstallThing(controlThing);
        RosterIntegrationTest.ensureBothAccountsAreNotInEachOthersRoster(conOne, conTwo);
    }
    syncPoint.waitForResult(timeout);
}
Also used : Jid(org.jxmpp.jid.Jid) ThingControlRequest(org.jivesoftware.smackx.iot.control.ThingControlRequest) Collection(java.util.Collection) SetBoolData(org.jivesoftware.smackx.iot.control.element.SetBoolData) SimpleResultSyncPoint(org.igniterealtime.smack.inttest.util.SimpleResultSyncPoint) SetData(org.jivesoftware.smackx.iot.control.element.SetData) IoTSetResponse(org.jivesoftware.smackx.iot.control.element.IoTSetResponse) AbstractSmackIntegrationTest(org.igniterealtime.smack.inttest.AbstractSmackIntegrationTest) SmackIntegrationTest(org.igniterealtime.smack.inttest.SmackIntegrationTest)

Example 13 with SmackIntegrationTest

use of org.igniterealtime.smack.inttest.SmackIntegrationTest in project Smack by igniterealtime.

the class MamIntegrationTest method mamTest.

@SmackIntegrationTest
public void mamTest() throws NotConnectedException, InterruptedException, NoResponseException, XMPPErrorException, NotLoggedInException {
    EntityBareJid userOne = conOne.getUser().asEntityBareJid();
    EntityBareJid userTwo = conTwo.getUser().asEntityBareJid();
    Message message = new Message(userTwo);
    String messageId = message.setStanzaId();
    String messageBody = "test message";
    message.setBody(messageBody);
    conOne.sendStanza(message);
    int pageSize = 20;
    MamQueryResult mamQueryResult = mamManagerConTwo.queryArchive(pageSize, null, null, userOne, null);
    while (!mamQueryResult.mamFin.isComplete()) {
        mamQueryResult = mamManagerConTwo.pageNext(mamQueryResult, pageSize);
    }
    List<Forwarded> forwardedMessages = mamQueryResult.forwardedMessages;
    Message mamMessage = (Message) forwardedMessages.get(forwardedMessages.size() - 1).getForwardedStanza();
    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) Forwarded(org.jivesoftware.smackx.forward.packet.Forwarded) MamQueryResult(org.jivesoftware.smackx.mam.MamManager.MamQueryResult) EntityBareJid(org.jxmpp.jid.EntityBareJid) AbstractSmackIntegrationTest(org.igniterealtime.smack.inttest.AbstractSmackIntegrationTest) SmackIntegrationTest(org.igniterealtime.smack.inttest.SmackIntegrationTest)

Example 14 with SmackIntegrationTest

use of org.igniterealtime.smack.inttest.SmackIntegrationTest in project Smack by igniterealtime.

the class MultiUserChatLowLevelIntegrationTest method testMucBookmarksAutojoin.

@SmackIntegrationTest
public void testMucBookmarksAutojoin(XMPPTCPConnection connection) throws InterruptedException, TestNotPossibleException, XMPPException, SmackException, IOException {
    final BookmarkManager bookmarkManager = BookmarkManager.getBookmarkManager(connection);
    if (!bookmarkManager.isSupported()) {
        throw new TestNotPossibleException("Private data storage not supported");
    }
    final MultiUserChatManager multiUserChatManager = MultiUserChatManager.getInstanceFor(connection);
    final Resourcepart mucNickname = Resourcepart.from("Nick-" + StringUtils.randomString(6));
    final String randomMucName = StringUtils.randomString(6);
    final DomainBareJid mucComponent = multiUserChatManager.getXMPPServiceDomains().get(0);
    final MultiUserChat muc = multiUserChatManager.getMultiUserChat(JidCreate.entityBareFrom(Localpart.from(randomMucName), mucComponent));
    MucCreateConfigFormHandle handle = muc.createOrJoin(mucNickname);
    if (handle != null) {
        handle.makeInstant();
    }
    muc.leave();
    bookmarkManager.addBookmarkedConference("Smack Inttest: " + testRunId, muc.getRoom(), true, mucNickname, null);
    connection.disconnect();
    connection.connect().login();
    // MucBookmarkAutojoinManager is also able to do its task automatically
    // after every login, it's not determinstic when this will be finished.
    // So we trigger it manually here.
    MucBookmarkAutojoinManager.getInstanceFor(connection).autojoinBookmarkedConferences();
    assertTrue(muc.isJoined());
    // If the test went well, leave the MUC
    muc.leave();
}
Also used : TestNotPossibleException(org.igniterealtime.smack.inttest.TestNotPossibleException) DomainBareJid(org.jxmpp.jid.DomainBareJid) MucCreateConfigFormHandle(org.jivesoftware.smackx.muc.MultiUserChat.MucCreateConfigFormHandle) BookmarkManager(org.jivesoftware.smackx.bookmarks.BookmarkManager) Resourcepart(org.jxmpp.jid.parts.Resourcepart) SmackIntegrationTest(org.igniterealtime.smack.inttest.SmackIntegrationTest)

Example 15 with SmackIntegrationTest

use of org.igniterealtime.smack.inttest.SmackIntegrationTest in project Smack by igniterealtime.

the class ChatTest method testProperties.

@SuppressWarnings("deprecation")
@SmackIntegrationTest
public void testProperties() throws XmppStringprepException, NotConnectedException, Exception {
    org.jivesoftware.smack.chat.Chat newChat = chatManagerOne.createChat(conTwo.getUser());
    StanzaCollector collector = conTwo.createStanzaCollector(new ThreadFilter(newChat.getThreadID()));
    Message msg = new Message();
    msg.setSubject("Subject of the chat");
    msg.setBody("Body of the chat");
    addProperty(msg, "favoriteColor", "red");
    addProperty(msg, "age", 30);
    addProperty(msg, "distance", 30f);
    addProperty(msg, "weight", 30d);
    addProperty(msg, "male", true);
    addProperty(msg, "birthdate", new Date());
    newChat.sendMessage(msg);
    Message msg2 = (Message) collector.nextResult(2000);
    assertNotNull("No message was received", msg2);
    assertEquals("Subjects are different", msg.getSubject(), msg2.getSubject());
    assertEquals("Bodies are different", msg.getBody(), msg2.getBody());
    assertEquals("favoriteColors are different", getProperty(msg, "favoriteColor"), getProperty(msg2, "favoriteColor"));
    assertEquals("ages are different", getProperty(msg, "age"), getProperty(msg2, "age"));
    assertEquals("distances are different", getProperty(msg, "distance"), getProperty(msg2, "distance"));
    assertEquals("weights are different", getProperty(msg, "weight"), getProperty(msg2, "weight"));
    assertEquals("males are different", getProperty(msg, "male"), getProperty(msg2, "male"));
    assertEquals("birthdates are different", getProperty(msg, "birthdate"), getProperty(msg2, "birthdate"));
}
Also used : Message(org.jivesoftware.smack.packet.Message) ThreadFilter(org.jivesoftware.smack.filter.ThreadFilter) Date(java.util.Date) AbstractSmackIntegrationTest(org.igniterealtime.smack.inttest.AbstractSmackIntegrationTest) SmackIntegrationTest(org.igniterealtime.smack.inttest.SmackIntegrationTest)

Aggregations

SmackIntegrationTest (org.igniterealtime.smack.inttest.SmackIntegrationTest)19 AbstractSmackIntegrationTest (org.igniterealtime.smack.inttest.AbstractSmackIntegrationTest)14 AndFilter (org.jivesoftware.smack.filter.AndFilter)4 Message (org.jivesoftware.smack.packet.Message)4 SimpleResultSyncPoint (org.igniterealtime.smack.inttest.util.SimpleResultSyncPoint)3 DiscoverInfo (org.jivesoftware.smackx.disco.packet.DiscoverInfo)3 TimeoutException (java.util.concurrent.TimeoutException)2 TestNotPossibleException (org.igniterealtime.smack.inttest.TestNotPossibleException)2 NoResponseException (org.jivesoftware.smack.SmackException.NoResponseException)2 NotConnectedException (org.jivesoftware.smack.SmackException.NotConnectedException)2 XMPPErrorException (org.jivesoftware.smack.XMPPException.XMPPErrorException)2 ThreadFilter (org.jivesoftware.smack.filter.ThreadFilter)2 Presence (org.jivesoftware.smack.packet.Presence)2 MucCreateConfigFormHandle (org.jivesoftware.smackx.muc.MultiUserChat.MucCreateConfigFormHandle)2 EntityBareJid (org.jxmpp.jid.EntityBareJid)2 Jid (org.jxmpp.jid.Jid)2 BufferedInputStream (java.io.BufferedInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1