Skip to content

Commit

Permalink
[sint] use idiomatic filters in presence subscription tests
Browse files Browse the repository at this point in the history
  • Loading branch information
guusdk committed Nov 26, 2023
1 parent 5e567ad commit acbc4a5
Showing 1 changed file with 28 additions and 51 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
*
* Copyright 2021 Guus der Kinderen
* Copyright 2021-2023 Guus der Kinderen
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,6 +17,11 @@
package org.jivesoftware.smack.subscription;

import org.jivesoftware.smack.AbstractXMPPConnection;
import org.jivesoftware.smack.filter.AndFilter;
import org.jivesoftware.smack.filter.FromMatchesFilter;
import org.jivesoftware.smack.filter.PresenceTypeFilter;
import org.jivesoftware.smack.filter.StanzaExtensionFilter;
import org.jivesoftware.smack.filter.StanzaFilter;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.packet.StandardExtensionElement;

Expand Down Expand Up @@ -53,19 +58,13 @@ public void testSubscriptionRequest() throws Exception {

final SimpleResultSyncPoint received = new SimpleResultSyncPoint();

conOne.addAsyncStanzaListener(p -> received.signal(),
stanza -> {
if (!(stanza instanceof Presence)) {
return false;
}
if (!stanza.getFrom().asBareJid().equals(conTwo.getUser().asBareJid())) {
return false;
}
final Presence presence = (Presence) stanza;
return Presence.Type.subscribe.equals(presence.getType());
}
final StanzaFilter resultFilter = new AndFilter(
PresenceTypeFilter.SUBSCRIBE,
FromMatchesFilter.createBare(conTwo.getUser())
);

conOne.addAsyncStanzaListener(p -> received.signal(), resultFilter);

conTwo.sendStanza(subscriptionRequest);
received.waitForResult(timeout);
}
Expand Down Expand Up @@ -93,19 +92,13 @@ public void testSubscriptionRequestOffline() throws Exception {

final SimpleResultSyncPoint received = new SimpleResultSyncPoint();

conOne.addAsyncStanzaListener(p -> received.signal(),
stanza -> {
if (!(stanza instanceof Presence)) {
return false;
}
if (!stanza.getFrom().asBareJid().equals(conTwo.getUser().asBareJid())) {
return false;
}
final Presence presence = (Presence) stanza;
return Presence.Type.subscribe.equals(presence.getType());
}
final StanzaFilter resultFilter = new AndFilter(
PresenceTypeFilter.SUBSCRIBE,
FromMatchesFilter.createBare(conTwo.getUser())
);

conOne.addAsyncStanzaListener(p -> received.signal(), resultFilter);

((AbstractXMPPConnection) conOne).login();
received.waitForResult(timeout);
}
Expand All @@ -128,22 +121,14 @@ public void testSubscriptionRequestWithExtension() throws Exception {

final SimpleResultSyncPoint received = new SimpleResultSyncPoint();

conOne.addAsyncStanzaListener(p -> received.signal(),
stanza -> {
if (!(stanza instanceof Presence)) {
return false;
}
if (!stanza.getFrom().asBareJid().equals(conTwo.getUser().asBareJid())) {
return false;
}
final Presence presence = (Presence) stanza;
if (!Presence.Type.subscribe.equals(presence.getType())) {
return false;
}
return stanza.hasExtension("test", "org.example.test");
}
final StanzaFilter resultFilter = new AndFilter(
PresenceTypeFilter.SUBSCRIBE,
FromMatchesFilter.createBare(conTwo.getUser()),
new StanzaExtensionFilter("test", "org.example.test")
);

conOne.addAsyncStanzaListener(p -> received.signal(), resultFilter);

conTwo.sendStanza(subscriptionRequest);
received.waitForResult(timeout);
}
Expand Down Expand Up @@ -174,22 +159,14 @@ public void testSubscriptionRequestOfflineWithExtension() throws Exception {

final SimpleResultSyncPoint received = new SimpleResultSyncPoint();

conOne.addAsyncStanzaListener(p -> received.signal(),
stanza -> {
if (!(stanza instanceof Presence)) {
return false;
}
if (!stanza.getFrom().asBareJid().equals(conTwo.getUser().asBareJid())) {
return false;
}
final Presence presence = (Presence) stanza;
if (!Presence.Type.subscribe.equals(presence.getType())) {
return false;
}
return stanza.hasExtension("test", "org.example.test");
}
final StanzaFilter resultFilter = new AndFilter(
PresenceTypeFilter.SUBSCRIBE,
FromMatchesFilter.createBare(conTwo.getUser()),
new StanzaExtensionFilter("test", "org.example.test")
);

conOne.addAsyncStanzaListener(p -> received.signal(), resultFilter);

((AbstractXMPPConnection) conOne).login();
received.waitForResult(timeout);
}
Expand Down

0 comments on commit acbc4a5

Please sign in to comment.