1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2025-09-09 18:29:45 +02:00

Add (partial) support for IoT XEPs

That is XEP-0323, -0324, -0325, and -0347.

SMACK-727.
This commit is contained in:
Florian Schmaus 2016-07-20 20:57:04 +02:00
parent d1fe5c2933
commit b91978dcc4
110 changed files with 5395 additions and 40 deletions

View file

@ -10,6 +10,7 @@ dependencies {
compile project(':smack-java7')
compile project(':smack-tcp')
compile project(':smack-extensions')
compile project(':smack-experimental')
compile 'org.reflections:reflections:0.9.9-RC1'
compile 'eu.geekplace.javapinning:java-pinning-java7:1.1.0-alpha1'
compile "junit:junit:$junitVersion"

View file

@ -16,10 +16,13 @@
*/
package org.igniterealtime.smack.inttest;
import java.util.Random;
import java.util.logging.Logger;
public abstract class AbstractSmackIntTest {
protected static final Logger LOGGER = Logger.getLogger(AbstractSmackIntTest.class.getName());
protected static final Random INSECURE_RANDOM = new Random();
}

View file

@ -30,6 +30,11 @@ public abstract class AbstractSmackIntegrationTest extends AbstractSmackIntTest
*/
protected final XMPPConnection conTwo;
/**
* The third connection.
*/
protected final XMPPConnection conThree;
/**
* An alias for the first connection {@link #conOne}.
*/
@ -42,6 +47,7 @@ public abstract class AbstractSmackIntegrationTest extends AbstractSmackIntTest
public AbstractSmackIntegrationTest(SmackIntegrationTestEnvironment environment) {
this.connection = this.conOne = environment.conOne;
this.conTwo = environment.conTwo;
this.conThree = environment.conThree;
if (environment.configuration.replyTimeout > 0) {
this.defaultTimeout = environment.configuration.replyTimeout;
} else {

View file

@ -53,6 +53,10 @@ public final class Configuration {
public final String accountTwoPassword;
public final String accountThreeUsername;
public final String accountThreePassword;
public final boolean debug;
public final Set<String> enabledTests;
@ -63,7 +67,7 @@ public final class Configuration {
private Configuration(DomainBareJid service, String serviceTlsPin, SecurityMode securityMode, int replyTimeout,
boolean debug, String accountOneUsername, String accountOnePassword, String accountTwoUsername,
String accountTwoPassword, Set<String> enabledTests, Set<String> disabledTests,
String accountTwoPassword, String accountThreeUsername, String accountThreePassword, Set<String> enabledTests, Set<String> disabledTests,
Set<String> testPackages) {
this.service = Objects.requireNonNull(service,
"'service' must be set. Either via 'properties' files or via system property 'sinttest.service'.");
@ -83,6 +87,8 @@ public final class Configuration {
this.accountOnePassword = accountOnePassword;
this.accountTwoUsername = accountTwoUsername;
this.accountTwoPassword = accountTwoPassword;
this.accountThreeUsername = accountThreeUsername;
this.accountThreePassword = accountThreePassword;
this.enabledTests = enabledTests;
this.disabledTests = disabledTests;
this.testPackages = testPackages;
@ -110,6 +116,10 @@ public final class Configuration {
private String accountTwoPassword;
public String accountThreeUsername;
public String accountThreePassword;
private boolean debug;
private Set<String> enabledTests;
@ -153,11 +163,13 @@ public final class Configuration {
}
public Builder setUsernamesAndPassword(String accountOneUsername, String accountOnePassword,
String accountTwoUsername, String accountTwoPassword) {
this.accountOneUsername = accountOneUsername;
this.accountOnePassword = accountOnePassword;
this.accountTwoUsername = accountTwoUsername;
this.accountTwoPassword = accountTwoPassword;
String accountTwoUsername, String accountTwoPassword, String accountThreeUsername, String accountThreePassword) {
this.accountOneUsername = StringUtils.requireNotNullOrEmpty(accountOneUsername, "accountOneUsername must not be null or empty");
this.accountOnePassword = StringUtils.requireNotNullOrEmpty(accountOnePassword, "accountOnePassword must not be null or empty");
this.accountTwoUsername = StringUtils.requireNotNullOrEmpty(accountTwoUsername, "accountTwoUsername must not be null or empty");
this.accountTwoPassword = StringUtils.requireNotNullOrEmpty(accountTwoPassword, "accountTwoPasswordmust not be null or empty");
this.accountThreeUsername = StringUtils.requireNotNullOrEmpty(accountThreeUsername, "accountThreeUsername must not be null or empty");
this.accountThreePassword = StringUtils.requireNotNullOrEmpty(accountThreePassword, "accountThreePassword must not be null or empty");
return this;
}
@ -213,7 +225,7 @@ public final class Configuration {
public Configuration build() {
return new Configuration(service, serviceTlsPin, securityMode, replyTimeout, debug, accountOneUsername,
accountOnePassword, accountTwoUsername, accountTwoPassword, enabledTests, disabledTests,
accountOnePassword, accountTwoUsername, accountTwoPassword, accountThreeUsername, accountThreePassword, enabledTests, disabledTests,
testPackages);
}
}
@ -252,7 +264,9 @@ public final class Configuration {
String accountOnePassword = properties.getProperty("accountOnePassword");
String accountTwoUsername = properties.getProperty("accountTwoUsername");
String accountTwoPassword = properties.getProperty("accountTwoPassword");
builder.setUsernamesAndPassword(accountOneUsername, accountOnePassword, accountTwoUsername, accountTwoPassword);
String accountThreeUsername = properties.getProperty("accountThreeUsername");
String accountThreePassword = properties.getProperty("accountThreePassword");
builder.setUsernamesAndPassword(accountOneUsername, accountOnePassword, accountTwoUsername, accountTwoPassword, accountThreeUsername, accountThreePassword);
builder.setDebug(properties.getProperty("debug"));
builder.setEnabledTests(properties.getProperty("enabledTests"));
@ -262,7 +276,7 @@ public final class Configuration {
return builder.build();
}
private static File findPropertiesFile() throws IOException {
private static File findPropertiesFile() {
List<String> possibleLocations = new LinkedList<>();
possibleLocations.add("properties");
String userHome = System.getProperty("user.home");

View file

@ -24,14 +24,17 @@ public class SmackIntegrationTestEnvironment {
public final XMPPTCPConnection conTwo;
public final XMPPTCPConnection conThree;
public final String testRunId;
public final Configuration configuration;
SmackIntegrationTestEnvironment(XMPPTCPConnection conOne, XMPPTCPConnection conTwo, String testRunId,
SmackIntegrationTestEnvironment(XMPPTCPConnection conOne, XMPPTCPConnection conTwo, XMPPTCPConnection conThree, String testRunId,
Configuration configuration) {
this.conOne = conOne;
this.conTwo = conTwo;
this.conThree = conThree;
this.testRunId = testRunId;
this.configuration = configuration;
}

View file

@ -50,7 +50,6 @@ import org.jivesoftware.smack.ConnectionConfiguration.SecurityMode;
import org.jivesoftware.smack.SmackConfiguration;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.SmackException.NoResponseException;
import org.jivesoftware.smack.SmackException.NotConnectedException;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration;
@ -166,6 +165,7 @@ public class SmackIntegrationTestFramework {
// Ensure that the accounts are deleted and disconnected before we continue
disconnectAndMaybeDelete(environment.conOne);
disconnectAndMaybeDelete(environment.conTwo);
disconnectAndMaybeDelete(environment.conThree);
}
return testRunResult;
@ -173,16 +173,16 @@ public class SmackIntegrationTestFramework {
@SuppressWarnings({"unchecked", "Finally"})
private void runTests(Set<Class<? extends AbstractSmackIntTest>> classes)
throws NoResponseException, NotConnectedException, InterruptedException {
throws NoResponseException, InterruptedException {
for (Class<? extends AbstractSmackIntTest> testClass : classes) {
final String testClassName = testClass.getName();
if (config.enabledTests != null && !config.enabledTests.contains(testClassName)) {
if (config.enabledTests != null && !isInSet(testClass, config.enabledTests)) {
LOGGER.info("Skipping test class " + testClassName + " because it is not enabled");
continue;
}
if (config.disabledTests != null && config.disabledTests.contains(testClassName)) {
if (isInSet(testClass, config.disabledTests)) {
LOGGER.info("Skipping test class " + testClassName + " because it is disalbed");
continue;
}
@ -233,9 +233,8 @@ public class SmackIntegrationTestFramework {
while (it.hasNext()) {
final Method method = it.next();
final String methodName = method.getName();
final String className = method.getDeclaringClass().getName();
if (config.enabledTests != null && !config.enabledTests.contains(methodName)
&& !config.enabledTests.contains(className)) {
if (config.enabledTests != null && !(config.enabledTests.contains(methodName)
|| isInSet(testClass, config.enabledTests))) {
LOGGER.fine("Skipping test method " + methodName + " because it is not enabled");
it.remove();
continue;
@ -472,26 +471,33 @@ public class SmackIntegrationTestFramework {
NoSuchAlgorithmException {
XMPPTCPConnection conOne = null;
XMPPTCPConnection conTwo = null;
XMPPTCPConnection conThree = null;
try {
conOne = getConnectedConnectionFor(AccountNum.One);
conTwo = getConnectedConnectionFor(AccountNum.Two);
conThree = getConnectedConnectionFor(AccountNum.Three);
}
catch (Exception e) {
// TODO Reverse the order, i.e. conThree should be disconnected first.
if (conOne != null) {
conOne.disconnect();
}
if (conTwo != null) {
conTwo.disconnect();
}
if (conThree != null) {
conThree.disconnect();
}
throw e;
}
return new SmackIntegrationTestEnvironment(conOne, conTwo, testRunResult.testRunId, config);
return new SmackIntegrationTestEnvironment(conOne, conTwo, conThree, testRunResult.testRunId, config);
}
enum AccountNum {
One,
Two,
Three,
}
private static final String USERNAME_PREFIX = "smack-inttest";
@ -513,6 +519,11 @@ public class SmackIntegrationTestFramework {
accountPassword = config.accountTwoPassword;
middlefix = "two";
break;
case Three:
accountUsername = config.accountThreeUsername;
accountPassword = config.accountThreePassword;
middlefix = "three";
break;
default:
throw new IllegalStateException();
}
@ -584,6 +595,15 @@ public class SmackIntegrationTestFramework {
return (Exception) e;
}
private static boolean isInSet(Class<?> clz, Set<String> classes) {
if (classes == null) {
return false;
}
final String className = clz.getName();
final String unqualifiedClassName = clz.getSimpleName();
return (classes.contains(className) || classes.contains(unqualifiedClassName));
}
public static final class TestRunResult {
/**

View file

@ -0,0 +1,97 @@
/**
*
* Copyright 2016 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.iot;
import static org.junit.Assert.assertNotNull;
import java.util.Collection;
import java.util.concurrent.TimeoutException;
import org.igniterealtime.smack.inttest.AbstractSmackIntegrationTest;
import org.igniterealtime.smack.inttest.SmackIntegrationTest;
import org.igniterealtime.smack.inttest.SmackIntegrationTestEnvironment;
import org.igniterealtime.smack.inttest.util.SimpleResultSyncPoint;
import org.jivesoftware.smack.XMPPException.XMPPErrorException;
import org.jivesoftware.smack.roster.RosterIntegrationTest;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smackx.iot.control.IoTControlManager;
import org.jivesoftware.smackx.iot.control.ThingControlRequest;
import org.jivesoftware.smackx.iot.control.element.IoTSetResponse;
import org.jivesoftware.smackx.iot.control.element.SetBoolData;
import org.jivesoftware.smackx.iot.control.element.SetData;
import org.jxmpp.jid.Jid;
public class IoTControlIntegrationTest extends AbstractSmackIntegrationTest {
private final IoTControlManager IoTControlManagerOne;
private final IoTControlManager IoTControlManagerTwo;
public IoTControlIntegrationTest(SmackIntegrationTestEnvironment environment) {
super(environment);
IoTControlManagerOne = IoTControlManager.getInstanceFor(conOne);
IoTControlManagerTwo = IoTControlManager.getInstanceFor(conTwo);
}
/**
* Connection one provides a thing, which is controlled by connection two.
*
* @throws Exception
* @throws TimeoutException
*/
@SmackIntegrationTest
// @SmackSerialIntegrationTest
public 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, defaultTimeout);
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(defaultTimeout);
}
}

View file

@ -0,0 +1,104 @@
/**
*
* Copyright 2016 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.iot;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeoutException;
import org.igniterealtime.smack.inttest.AbstractSmackIntegrationTest;
import org.igniterealtime.smack.inttest.SmackIntegrationTest;
import org.igniterealtime.smack.inttest.SmackIntegrationTestEnvironment;
import org.jivesoftware.smack.roster.RosterIntegrationTest;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smackx.iot.data.IoTDataManager;
import org.jivesoftware.smackx.iot.data.ThingMomentaryReadOutRequest;
import org.jivesoftware.smackx.iot.data.ThingMomentaryReadOutResult;
import org.jivesoftware.smackx.iot.data.element.IoTDataField;
import org.jivesoftware.smackx.iot.data.element.IoTDataField.IntField;
import org.jivesoftware.smackx.iot.data.element.IoTFieldsExtension;
import org.jivesoftware.smackx.iot.data.element.NodeElement;
import org.jivesoftware.smackx.iot.data.element.TimestampElement;
public class IoTDataIntegrationTest extends AbstractSmackIntegrationTest {
private final IoTDataManager iotDataManagerOne;
private final IoTDataManager iotDataManagerTwo;
public IoTDataIntegrationTest(SmackIntegrationTestEnvironment environment) {
super(environment);
iotDataManagerOne = IoTDataManager.getInstanceFor(conOne);
iotDataManagerTwo = IoTDataManager.getInstanceFor(conTwo);
}
/**
* Connection one provides a thing, which momentary value is read out by connection two.
*
* @throws Exception
* @throws TimeoutException
*/
@SmackIntegrationTest
public void dataTest() throws TimeoutException, Exception {
final String key = StringUtils.randomString(12);
final String sn = StringUtils.randomString(12);
final int value = INSECURE_RANDOM.nextInt();
Thing dataThing = Thing.builder().setKey(key).setSerialNumber(sn).setMomentaryReadOutRequestHandler(new ThingMomentaryReadOutRequest() {
@Override
public void momentaryReadOutRequest(ThingMomentaryReadOutResult callback) {
IoTDataField.IntField field = new IntField(testRunId, value);
callback.momentaryReadOut(Collections.singletonList(field));
}
}).build();
iotDataManagerOne.installThing(dataThing);
List<IoTFieldsExtension> values;
try {
RosterIntegrationTest.ensureBothAccountsAreSubscribedToEachOther(conOne, conTwo, defaultTimeout);
values = iotDataManagerTwo.requestMomentaryValuesReadOut(conOne.getUser());
}
finally {
iotDataManagerOne.uninstallThing(dataThing);
RosterIntegrationTest.ensureBothAccountsAreNotInEachOthersRoster(conOne, conTwo);
}
assertEquals(1, values.size());
IoTFieldsExtension iotFieldsExtension = values.get(0);
List<NodeElement> nodes = iotFieldsExtension.getNodes();
assertEquals(1, nodes.size());
NodeElement node = nodes.get(0);
List<TimestampElement> timestamps = node.getTimestampElements();
assertEquals(1, timestamps.size());
TimestampElement timestamp = timestamps.get(0);
List<? extends IoTDataField> fields = timestamp.getDataFields();
assertEquals(1, fields.size());
IoTDataField dataField = fields.get(0);
assertTrue(dataField instanceof IoTDataField.IntField);
IoTDataField.IntField intDataField = (IoTDataField.IntField) dataField;
assertEquals(testRunId, intDataField.getName());
assertEquals(value, intDataField.getValue());
}
}

View file

@ -0,0 +1,92 @@
/**
*
* Copyright 2016 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.iot;
import static org.junit.Assert.assertEquals;
import org.igniterealtime.smack.inttest.AbstractSmackIntegrationTest;
import org.igniterealtime.smack.inttest.SmackIntegrationTest;
import org.igniterealtime.smack.inttest.SmackIntegrationTestEnvironment;
import org.igniterealtime.smack.inttest.TestNotPossibleException;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.SmackException.NoResponseException;
import org.jivesoftware.smack.SmackException.NotConnectedException;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException.XMPPErrorException;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smackx.iot.discovery.IoTClaimedException;
import org.jivesoftware.smackx.iot.discovery.IoTDiscoveryManager;
import org.jivesoftware.smackx.iot.discovery.ThingState;
import org.jivesoftware.smackx.iot.discovery.element.IoTClaimed;
import org.jxmpp.jid.Jid;
public class IoTDiscoveryIntegrationTest extends AbstractSmackIntegrationTest {
private final IoTDiscoveryManager discoveryManagerOne;
private final IoTDiscoveryManager discoveryManagerTwo;
public IoTDiscoveryIntegrationTest(SmackIntegrationTestEnvironment environment) throws NoResponseException,
XMPPErrorException, NotConnectedException, InterruptedException, TestNotPossibleException {
super(environment);
discoveryManagerOne = IoTDiscoveryManager.getInstanceFor(conOne);
discoveryManagerTwo = IoTDiscoveryManager.getInstanceFor(conTwo);
checkPrerequisites(conOne);
}
@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();
}
static void checkPrerequisites(XMPPConnection connection) throws NoResponseException, XMPPErrorException,
NotConnectedException, InterruptedException, TestNotPossibleException {
IoTDiscoveryManager discoveryManager = IoTDiscoveryManager.getInstanceFor(connection);
Jid registry = discoveryManager.findRegistry();
if (registry == null) {
throw new TestNotPossibleException("Could not find IoT Registry");
}
}
public static ThingState registerThing(IoTDiscoveryManager iotDiscoveryManager, Thing thing) throws XMPPErrorException, InterruptedException, SmackException {
int attempts = 0;
while (true) {
try {
return iotDiscoveryManager.registerThing(thing);
}
catch (IoTClaimedException e) {
iotDiscoveryManager.unregister();
}
if (attempts++ > 3) {
throw new SmackException("Could no register thing");
}
}
}
}

View file

@ -0,0 +1,21 @@
/**
*
* Copyright 2015 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* TODO describe me.
*/
package org.jivesoftware.smackx.iot;

View file

@ -26,7 +26,7 @@ public class DummySmackIntegrationTestFramework extends SmackIntegrationTestFram
@Override
protected SmackIntegrationTestEnvironment prepareEnvironment() {
return new SmackIntegrationTestEnvironment(null, null, testRunResult.getTestRunId(), config);
return new SmackIntegrationTestEnvironment(null, null, null, testRunResult.getTestRunId(), config);
}
@Override

View file

@ -24,7 +24,7 @@ public class SmackIntegrationTestUnitTestUtil {
// @formatter:off
Configuration configuration = Configuration.builder()
.setService(JidTestUtil.DOMAIN_BARE_JID_1)
.setUsernamesAndPassword("dummy1", "dummy1pass", "dummy2", "dummy2pass")
.setUsernamesAndPassword("dummy1", "dummy1pass", "dummy2", "dummy2pass", "dummy3", "dummy3pass")
.addEnabledTest(unitTest).build();
// @formatter:on
return new DummySmackIntegrationTestFramework(configuration);