Search in sources :

Example 1 with SmackIntegrationTest

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

the class GeolocationIntegrationTest method testNotification.

/**
 * Verifies that a notification is sent when a publication is received, assuming that notification filtering
 * has been adjusted to allow for the notification to be delivered.
 *
 * @throws Exception if the test fails
 */
@SmackIntegrationTest
public void testNotification() throws Exception {
    GeoLocation.Builder builder = GeoLocation.builder();
    GeoLocation data = builder.setAccuracy(23d).setAlt(1000d).setAltAccuracy(10d).setArea("Delhi").setBearing(10d).setBuilding("Small Building").setCountry("India").setCountryCode("IN").setDescription("My Description").setFloor("top").setLat(25.098345d).setLocality("awesome").setLon(77.992034).setPostalcode("110085").setRegion("North").setRoom("small").setSpeed(250.0d).setStreet("Wall Street").setText("Unit Testing GeoLocation").setTimestamp(XmppDateTime.parseDate("2004-02-19")).setTzo("+5:30").setUri(new URI("http://xmpp.org")).build();
    IntegrationTestRosterUtil.ensureBothAccountsAreSubscribedToEachOther(conOne, conTwo, timeout);
    final SimpleResultSyncPoint geoLocationReceived = new SimpleResultSyncPoint();
    final PepEventListener<GeoLocation> geoLocationListener = (jid, geoLocation, id, message) -> {
        if (geoLocation.equals(data)) {
            geoLocationReceived.signal();
        }
    };
    try {
        // Register ConTwo's interest in receiving geolocation notifications, and wait for that interest to have been propagated.
        registerListenerAndWait(glm2, ServiceDiscoveryManager.getInstanceFor(conTwo), geoLocationListener);
        // Publish the data.
        // for the purpose of this test, this needs not be blocking/use publishAndWait();
        glm1.publishGeoLocation(data);
        // Wait for the data to be received.
        try {
            Object result = geoLocationReceived.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(glm2, geoLocationListener);
    }
}
Also used : SmackIntegrationTestEnvironment(org.igniterealtime.smack.inttest.SmackIntegrationTestEnvironment) GeoLocation(org.jivesoftware.smackx.geoloc.packet.GeoLocation) AfterClass(org.igniterealtime.smack.inttest.annotations.AfterClass) NotConnectedException(org.jivesoftware.smack.SmackException.NotConnectedException) EntityCapabilitiesChangedListener(org.jivesoftware.smackx.disco.EntityCapabilitiesChangedListener) SimpleResultSyncPoint(org.igniterealtime.smack.inttest.util.SimpleResultSyncPoint) AbstractSmackIntegrationTest(org.igniterealtime.smack.inttest.AbstractSmackIntegrationTest) NotLoggedInException(org.jivesoftware.smack.SmackException.NotLoggedInException) ServiceDiscoveryManager(org.jivesoftware.smackx.disco.ServiceDiscoveryManager) TimeoutException(java.util.concurrent.TimeoutException) XMPPErrorException(org.jivesoftware.smack.XMPPException.XMPPErrorException) NoResponseException(org.jivesoftware.smack.SmackException.NoResponseException) GeoLocationManager(org.jivesoftware.smackx.geoloc.GeoLocationManager) PepEventListener(org.jivesoftware.smackx.pep.PepEventListener) Assertions(org.junit.jupiter.api.Assertions) IntegrationTestRosterUtil(org.igniterealtime.smack.inttest.util.IntegrationTestRosterUtil) XmppDateTime(org.jxmpp.util.XmppDateTime) URI(java.net.URI) SmackIntegrationTest(org.igniterealtime.smack.inttest.annotations.SmackIntegrationTest) GeoLocation(org.jivesoftware.smackx.geoloc.packet.GeoLocation) URI(java.net.URI) 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 2 with SmackIntegrationTest

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

the class HttpFileUploadIntegrationTest method httpFileUploadTest.

@SmackIntegrationTest
public void httpFileUploadTest() throws IOException, XMPPErrorException, InterruptedException, SmackException {
    final int fileSize = FILE_SIZE;
    File file = createNewTempFile();
    FileOutputStream fos = new FileOutputStream(file.getCanonicalPath());
    byte[] upBytes;
    try {
        upBytes = new byte[fileSize];
        INSECURE_RANDOM.nextBytes(upBytes);
        fos.write(upBytes);
    } finally {
        fos.close();
    }
    URL getUrl = hfumOne.uploadFile(file, new UploadProgressListener() {

        @Override
        public void onUploadProgress(long uploadedBytes, long totalBytes) {
            double progress = uploadedBytes / totalBytes;
            LOGGER.fine("HTTP File Upload progress " + progress + "% (" + uploadedBytes + '/' + totalBytes + ')');
        }
    });
    HttpURLConnection urlConnection = getHttpUrlConnectionFor(getUrl);
    ByteArrayOutputStream baos = new ByteArrayOutputStream(fileSize);
    byte[] buffer = new byte[4096];
    int n;
    try {
        InputStream is = new BufferedInputStream(urlConnection.getInputStream());
        while ((n = is.read(buffer)) != -1) {
            baos.write(buffer, 0, n);
        }
    } finally {
        urlConnection.disconnect();
    }
    byte[] downBytes = baos.toByteArray();
    assertArrayEquals(upBytes, downBytes);
}
Also used : HttpURLConnection(java.net.HttpURLConnection) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) File(java.io.File) URL(java.net.URL) AbstractSmackIntegrationTest(org.igniterealtime.smack.inttest.AbstractSmackIntegrationTest) SmackIntegrationTest(org.igniterealtime.smack.inttest.annotations.SmackIntegrationTest)

Example 3 with SmackIntegrationTest

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

the class IoTDiscoveryIntegrationTest method registerClaimAndUnregisterThing.

@SmackIntegrationTest
public void registerClaimAndUnregisterThing() throws XMPPErrorException, InterruptedException, SmackException {
    final String key = StringUtils.randomString(12);
    final String sn = StringUtils.randomString(12);
    final Thing thing = Thing.builder().setKey(key).setSerialNumber(sn).setManufacturer("Ignite Realtime").setModel("Smack").setVersion("0.1").build();
    registerThing(discoveryManagerOne, thing);
    IoTClaimed iotClaimed = discoveryManagerTwo.claimThing(thing.getMetaTags());
    assertEquals(conOne.getUser().asBareJid(), iotClaimed.getJid());
    discoveryManagerTwo.disownThing(iotClaimed.getJid());
    discoveryManagerOne.unregister();
}
Also used : IoTClaimed(org.jivesoftware.smackx.iot.discovery.element.IoTClaimed) AbstractSmackIntegrationTest(org.igniterealtime.smack.inttest.AbstractSmackIntegrationTest) SmackIntegrationTest(org.igniterealtime.smack.inttest.annotations.SmackIntegrationTest)

Example 4 with SmackIntegrationTest

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

the class VersionIntegrationTest method testVersion.

@SmackIntegrationTest
public void testVersion() throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
    // TODO put into @BeforeClass method
    VersionManager.setAutoAppendSmackVersion(false);
    VersionManager versionManagerOne = VersionManager.getInstanceFor(conOne);
    VersionManager versionManagerTwo = VersionManager.getInstanceFor(conTwo);
    final String versionName = "Smack Integration Test " + testRunId;
    versionManagerTwo.setVersion(versionName, "1.0");
    assertTrue(versionManagerOne.isSupported(conTwo.getUser()));
    Version version = versionManagerOne.getVersion(conTwo.getUser());
    assertEquals(versionName, version.getName());
}
Also used : Version(org.jivesoftware.smackx.iqversion.packet.Version) AbstractSmackIntegrationTest(org.igniterealtime.smack.inttest.AbstractSmackIntegrationTest) SmackIntegrationTest(org.igniterealtime.smack.inttest.annotations.SmackIntegrationTest)

Example 5 with SmackIntegrationTest

use of org.igniterealtime.smack.inttest.annotations.SmackIntegrationTest 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)

Aggregations

SmackIntegrationTest (org.igniterealtime.smack.inttest.annotations.SmackIntegrationTest)66 EntityBareJid (org.jxmpp.jid.EntityBareJid)30 AbstractSmackIntegrationTest (org.igniterealtime.smack.inttest.AbstractSmackIntegrationTest)25 XMPPException (org.jivesoftware.smack.XMPPException)23 SmackException (org.jivesoftware.smack.SmackException)20 Resourcepart (org.jxmpp.jid.parts.Resourcepart)20 TestNotPossibleException (org.igniterealtime.smack.inttest.TestNotPossibleException)17 SimpleResultSyncPoint (org.igniterealtime.smack.inttest.util.SimpleResultSyncPoint)17 ResultSyncPoint (org.igniterealtime.smack.inttest.util.ResultSyncPoint)16 EntityFullJid (org.jxmpp.jid.EntityFullJid)12 Message (org.jivesoftware.smack.packet.Message)9 TimeoutException (java.util.concurrent.TimeoutException)8 Presence (org.jivesoftware.smack.packet.Presence)7 SmackIntegrationTestEnvironment (org.igniterealtime.smack.inttest.SmackIntegrationTestEnvironment)6 AfterClass (org.igniterealtime.smack.inttest.annotations.AfterClass)6 IntegrationTestRosterUtil (org.igniterealtime.smack.inttest.util.IntegrationTestRosterUtil)6 EntityCapabilitiesChangedListener (org.jivesoftware.smackx.disco.EntityCapabilitiesChangedListener)6 ServiceDiscoveryManager (org.jivesoftware.smackx.disco.ServiceDiscoveryManager)6 PepEventListener (org.jivesoftware.smackx.pep.PepEventListener)6 Assertions (org.junit.jupiter.api.Assertions)6