MultiUserChatLowLevelIntegrationTest.java

  1. /**
  2.  *
  3.  * Copyright 2015-2024 Florian Schmaus
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smackx.muc;

  18. import static org.junit.jupiter.api.Assertions.assertTrue;

  19. import java.io.IOException;

  20. import org.jivesoftware.smack.AbstractXMPPConnection;
  21. import org.jivesoftware.smack.SmackException;
  22. import org.jivesoftware.smack.XMPPException;
  23. import org.jivesoftware.smack.util.StringUtils;

  24. import org.jivesoftware.smackx.bookmarks.BookmarkManager;
  25. import org.jivesoftware.smackx.muc.MultiUserChat.MucCreateConfigFormHandle;
  26. import org.jivesoftware.smackx.muc.bookmarkautojoin.MucBookmarkAutojoinManager;

  27. import org.igniterealtime.smack.inttest.AbstractSmackLowLevelIntegrationTest;
  28. import org.igniterealtime.smack.inttest.SmackIntegrationTestEnvironment;
  29. import org.igniterealtime.smack.inttest.TestNotPossibleException;
  30. import org.igniterealtime.smack.inttest.annotations.SmackIntegrationTest;
  31. import org.igniterealtime.smack.inttest.annotations.SpecificationReference;
  32. import org.jxmpp.jid.DomainBareJid;
  33. import org.jxmpp.jid.impl.JidCreate;
  34. import org.jxmpp.jid.parts.Localpart;
  35. import org.jxmpp.jid.parts.Resourcepart;

  36. @SpecificationReference(document = "XEP-0048", version = "1.2")
  37. public class MultiUserChatLowLevelIntegrationTest extends AbstractSmackLowLevelIntegrationTest {

  38.     public MultiUserChatLowLevelIntegrationTest(SmackIntegrationTestEnvironment environment) throws Exception {
  39.         super(environment);
  40.         AbstractXMPPConnection connection = getConnectedConnection();
  41.         try {
  42.             if (MultiUserChatManager.getInstanceFor(connection).getMucServiceDomains().isEmpty()) {
  43.                 throw new TestNotPossibleException("MUC component not offered by service");
  44.             }
  45.         } finally {
  46.             recycle(connection);
  47.         }
  48.     }

  49.     @SmackIntegrationTest
  50.     public void testMucBookmarksAutojoin(AbstractXMPPConnection connection) throws InterruptedException,
  51.                     TestNotPossibleException, XMPPException, SmackException, IOException {
  52.         final BookmarkManager bookmarkManager = BookmarkManager.getBookmarkManager(connection);
  53.         if (!bookmarkManager.isSupported()) {
  54.             throw new TestNotPossibleException("Private data storage not supported");
  55.         }
  56.         final MultiUserChatManager multiUserChatManager = MultiUserChatManager.getInstanceFor(connection);
  57.         final Resourcepart mucNickname = Resourcepart.from("Nick-" + StringUtils.randomString(6));
  58.         final String randomMucName = StringUtils.randomString(6);
  59.         final DomainBareJid mucComponent = multiUserChatManager.getMucServiceDomains().get(0);
  60.         final MultiUserChat muc = multiUserChatManager.getMultiUserChat(JidCreate.entityBareFrom(
  61.                         Localpart.from(randomMucName), mucComponent));

  62.         MucCreateConfigFormHandle handle;
  63.         try {
  64.             handle = muc.createOrJoin(mucNickname);
  65.         } catch (XMPPException.XMPPErrorException e) {
  66.             AbstractMultiUserChatIntegrationTest.mucCreationDisallowedOrThrow(e);
  67.             throw new TestNotPossibleException("MUC service " + mucComponent + " does not allow MUC creation", e);
  68.         }
  69.         if (handle != null) {
  70.             handle.makeInstant();
  71.         }
  72.         muc.leave();

  73.         bookmarkManager.addBookmarkedConference("Smack Inttest: " + testRunId, muc.getRoom(), true,
  74.                         mucNickname, null);

  75.         connection.disconnect();
  76.         connection.connect().login();

  77.         // MucBookmarkAutojoinManager is also able to do its task automatically
  78.         // after every login, it's not deterministic when this will be finished.
  79.         // So we trigger it manually here.
  80.         MucBookmarkAutojoinManager.getInstanceFor(connection).autojoinBookmarkedConferences();

  81.        assertTrue(muc.isJoined(), "Expected " + connection.getUser() + " to automatically join room " + muc.getRoom() + " after a reconnect, but it did not.");

  82.        // If the test went well, leave the MUC
  83.        muc.leave();
  84.     }

  85. }