From 17ca4c541be15866a485b7abe9b1fd639cff7ce2 Mon Sep 17 00:00:00 2001 From: Aditya Borikar Date: Thu, 14 May 2020 16:48:07 +0530 Subject: [PATCH] Add support for XEP-0232: Software Information By making use of an extended data format, service discovery responses can be used to constitute software information. Solves SMACK-853. --- documentation/extensions/index.md | 1 + .../softwareinfo/SoftwareInfoManager.java | 112 ++++++++ .../softwareinfo/form/SoftwareInfoForm.java | 268 ++++++++++++++++++ .../softwareinfo/form/package-info.java | 21 ++ .../smackx/softwareinfo/package-info.java | 21 ++ .../softwareinfo/SoftwareInfoFormTest.java | 143 ++++++++++ .../softwareinfo/SoftwareInfoManagerTest.java | 102 +++++++ .../SoftwareInfoIntegrationTest.java | 77 +++++ .../smackx/softwareInfo/package-info.java | 17 ++ 9 files changed, 762 insertions(+) create mode 100644 smack-extensions/src/main/java/org/jivesoftware/smackx/softwareinfo/SoftwareInfoManager.java create mode 100644 smack-extensions/src/main/java/org/jivesoftware/smackx/softwareinfo/form/SoftwareInfoForm.java create mode 100644 smack-extensions/src/main/java/org/jivesoftware/smackx/softwareinfo/form/package-info.java create mode 100644 smack-extensions/src/main/java/org/jivesoftware/smackx/softwareinfo/package-info.java create mode 100644 smack-extensions/src/test/java/org/jivesoftware/smackx/softwareinfo/SoftwareInfoFormTest.java create mode 100644 smack-extensions/src/test/java/org/jivesoftware/smackx/softwareinfo/SoftwareInfoManagerTest.java create mode 100644 smack-integration-test/src/main/java/org/jivesoftware/smackx/softwareInfo/SoftwareInfoIntegrationTest.java create mode 100644 smack-integration-test/src/main/java/org/jivesoftware/smackx/softwareInfo/package-info.java diff --git a/documentation/extensions/index.md b/documentation/extensions/index.md index 00a9680a7..c7d1a0e01 100644 --- a/documentation/extensions/index.md +++ b/documentation/extensions/index.md @@ -82,6 +82,7 @@ Smack Extensions and currently supported XEPs of smack-extensions | Data Forms Media Element | [XEP-0221](https://xmpp.org/extensions/xep-0221.html) | n/a | Allows to include media data in XEP-0004 data forms. | | Attention | [XEP-0224](https://xmpp.org/extensions/xep-0224.html) | n/a | Getting attention of another user. | | Bits of Binary | [XEP-0231](https://xmpp.org/extensions/xep-0231.html) | n/a | Including or referring to small bits of binary data in an XML stanza. | +| Software Information | [XEP-0232](https://xmpp.org/extensions/xep-0232.html) | 0.3 | Allows an entity to provide detailed data about itself in Service Discovery responses. | | Best Practices for Resource Locking | [XEP-0296](https://xmpp.org/extensions/xep-0296.html) | n/a | Specifies best practices to be followed by Jabber/XMPP clients about when to lock into, and unlock away from, resources. | | Stanza Forwarding | [XEP-0297](https://xmpp.org/extensions/xep-0297.html) | n/a | Allows forwarding of Stanzas. | | Last Message Correction | [XEP-0308](https://xmpp.org/extensions/xep-0308.html) | n/a | Provides a method for indicating that a message is a correction of the last sent message. | diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/softwareinfo/SoftwareInfoManager.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/softwareinfo/SoftwareInfoManager.java new file mode 100644 index 000000000..11af9b810 --- /dev/null +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/softwareinfo/SoftwareInfoManager.java @@ -0,0 +1,112 @@ +/** + * + * Copyright 2020 Aditya Borikar + * + * 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.softwareinfo; + +import java.io.IOException; +import java.util.Map; +import java.util.WeakHashMap; + +import org.jivesoftware.smack.Manager; +import org.jivesoftware.smack.SmackException.FeatureNotSupportedException; +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.parsing.SmackParsingException; +import org.jivesoftware.smack.xml.XmlPullParserException; +import org.jivesoftware.smackx.disco.ServiceDiscoveryManager; +import org.jivesoftware.smackx.disco.packet.DiscoverInfo; +import org.jivesoftware.smackx.softwareinfo.form.SoftwareInfoForm; +import org.jivesoftware.smackx.xdata.packet.DataForm; + +import org.jxmpp.jid.Jid; + +/** +* Entry point for Smack's API for XEP-0232: Software Information. +*
+* @see +* XEP-0232 : Software Information. +* +*/ +public final class SoftwareInfoManager extends Manager { + + private static final String FEATURE = "http://jabber.org/protocol/disco"; + private static final Map INSTANCES = new WeakHashMap<>(); + private final ServiceDiscoveryManager serviceDiscoveryManager; + + public static synchronized SoftwareInfoManager getInstanceFor (XMPPConnection connection) throws IOException, XmlPullParserException, SmackParsingException { + SoftwareInfoManager manager = INSTANCES.get(connection); + if (manager == null) { + manager = new SoftwareInfoManager(connection); + INSTANCES.put(connection, manager); + } + return manager; + } + + private SoftwareInfoManager(XMPPConnection connection) throws IOException, XmlPullParserException, SmackParsingException { + super(connection); + serviceDiscoveryManager = ServiceDiscoveryManager.getInstanceFor(connection); + } + + /** + * Returns true if the feature is supported by the Jid. + *
+ * @param jid Jid to be checked for support + * @return boolean + * @throws NoResponseException if there was no response from the remote entity + * @throws XMPPErrorException if there was an XMPP error returned + * @throws NotConnectedException if the XMPP connection is not connected + * @throws InterruptedException if the calling thread was interrupted + */ + public boolean isSupported(Jid jid) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { + return serviceDiscoveryManager.supportsFeatures(jid, FEATURE); + } + + /** + * Publishes the provided {@link SoftwareInfoForm} as an extended info. + *
+ * @param softwareInfoForm form to be added as an extended info + */ + public void publishSoftwareInformationForm(SoftwareInfoForm softwareInfoForm) { + serviceDiscoveryManager.addExtendedInfo(softwareInfoForm.getDataForm()); + } + + /** + * Get SoftwareInfoForm from Jid provided. + *
+ * @param jid jid to get software information from + * @return {@link SoftwareInfoForm} Form containing software information + * @throws NoResponseException if there was no response from the remote entity + * @throws XMPPErrorException if there was an XMPP error returned + * @throws NotConnectedException if the XMPP connection is not connected + * @throws InterruptedException if the calling thread was interrupted + * @throws FeatureNotSupportedException if the feature is not supported + */ + public SoftwareInfoForm fromJid(Jid jid) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException, FeatureNotSupportedException { + if (!isSupported(jid)) { + throw new FeatureNotSupportedException(SoftwareInfoForm.FORM_TYPE, jid); + } + DiscoverInfo discoverInfo = serviceDiscoveryManager.discoverInfo(jid); + DataForm dataForm = DataForm.from(discoverInfo, SoftwareInfoForm.FORM_TYPE); + if (dataForm == null) { + return null; + } + return SoftwareInfoForm.getBuilder() + .setDataForm(dataForm) + .build(); + } +} diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/softwareinfo/form/SoftwareInfoForm.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/softwareinfo/form/SoftwareInfoForm.java new file mode 100644 index 000000000..6774c3b73 --- /dev/null +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/softwareinfo/form/SoftwareInfoForm.java @@ -0,0 +1,268 @@ +/** + * + * Copyright 2020 Aditya Borikar + * + * 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.softwareinfo.form; + +import java.util.List; + +import org.jivesoftware.smack.util.EqualsUtil; +import org.jivesoftware.smack.util.HashCode; +import org.jivesoftware.smackx.mediaelement.element.MediaElement; +import org.jivesoftware.smackx.xdata.FormField; +import org.jivesoftware.smackx.xdata.FormFieldChildElement; +import org.jivesoftware.smackx.xdata.TextSingleFormField; +import org.jivesoftware.smackx.xdata.form.FilledForm; +import org.jivesoftware.smackx.xdata.form.Form; +import org.jivesoftware.smackx.xdata.packet.DataForm; +import org.jivesoftware.smackx.xdata.packet.DataForm.Type; + +/** + * {@link Form} that contains the software information. + *
+ * Instance of {@link SoftwareInfoForm} can be created using {@link Builder#build()} method. + *
+ * To obtain an instance of {@link Builder}, use {@link SoftwareInfoForm#getBuilder()} method. + *
+ * An example to illustrate is provided inside SoftwareInfoFormTest inside the test package. + */ +public final class SoftwareInfoForm extends FilledForm { + + public static final String FORM_TYPE = "urn:xmpp:dataforms:softwareinfo"; + public static final String OS = "os"; + public static final String OS_VERSION = "os_version"; + public static final String SOFTWARE = "software"; + public static final String SOFTWARE_VERSION = "software_version"; + public static final String ICON = "icon"; + + private SoftwareInfoForm(DataForm dataForm) { + super(dataForm); + } + + /** + * Returns name of the OS used by client. + *
+ * @return os + */ + public String getOS() { + return readFirstValue(OS); + } + + /** + * Returns version of the OS used by client. + *
+ * @return os_version + */ + public String getOSVersion() { + return readFirstValue(OS_VERSION); + } + + /** + * Returns name of the software used by client. + *
+ * @return software + */ + public String getSoftwareName() { + return readFirstValue(SOFTWARE); + } + + /** + * Returns version of the software used by client. + *
+ * @return software_version + */ + public String getSoftwareVersion () { + return readFirstValue(SOFTWARE_VERSION); + } + + /** + * Returns the software icon if used by client. + *
+ * @return {@link MediaElement} MediaElement or null + */ + public MediaElement getIcon () { + FormField field = getField(ICON); + if (field == null) { + return null; + } + FormFieldChildElement media = field.getFormFieldChildElement(MediaElement.QNAME); + if (media == null) { + return null; + } + return (MediaElement) media; + } + + @Override + public boolean equals(Object obj) { + return EqualsUtil.equals(this, obj, (equalsBuilder, otherObj) -> { + equalsBuilder.append(getDataForm().getType(), otherObj.getDataForm().getType()) + .append(getDataForm().getTitle(), otherObj.getDataForm().getTitle()) + .append(getDataForm().getReportedData(), otherObj.getDataForm().getReportedData()) + .append(getDataForm().getItems(), otherObj.getDataForm().getItems()) + .append(getDataForm().getFields(), otherObj.getDataForm().getFields()) + .append(getDataForm().getExtensionElements(), otherObj.getDataForm().getExtensionElements()); + }); + } + + @Override + public int hashCode() { + HashCode.Builder builder = HashCode.builder(); + builder.append(getDataForm().getFields()); + builder.append(getDataForm().getItems()); + builder.append(getDataForm().getExtensionElements()); + return builder.build(); + } + + /** + * Returns a new instance of {@link Builder}. + *
+ * @return Builder + */ + public static Builder getBuilder() { + return new Builder(); + } + + /** + * Builder class for {@link SoftwareInfoForm}. + *
+ * To obtain an instance of {@link Builder}, use {@link SoftwareInfoForm#getBuilder()} method. + *
+ * Use appropriate setters to include information inside SoftwareInfoForms. + */ + public static final class Builder { + DataForm.Builder dataFormBuilder; + + private Builder() { + dataFormBuilder = DataForm.builder(Type.result); + TextSingleFormField formField = FormField.buildHiddenFormType(FORM_TYPE); + dataFormBuilder.addField(formField); + } + + /** + * This will allow to include Icon using height, width and Uri's as a + * {@link FormField}. + *
+ * @param height Height of the image + * @param width Width of the image + * @param uriList List of URIs + * @return Builder + */ + public Builder setIcon(int height, int width, List uriList) { + MediaElement.Builder mediaBuilder = MediaElement.builder(); + for (MediaElement.Uri uri : uriList) { + mediaBuilder.addUri(uri); + } + MediaElement mediaElement = mediaBuilder.setHeightAndWidth(height, width).build(); + return setIcon(mediaElement); + } + + /** + * This will allow to include {@link MediaElement} directly as a + * {@link FormField}. + *
+ * @param mediaElement MediaElement to be included + * @return Builder + */ + public Builder setIcon(MediaElement mediaElement) { + FormField.Builder builder = FormField.builder(ICON); + builder.addFormFieldChildElement(mediaElement); + dataFormBuilder.addField(builder.build()); + return this; + } + + /** + * Include Operating System's name as a {@link FormField}. + *
+ * @param os Name of the OS + * @return Builder + */ + public Builder setOS(String os) { + TextSingleFormField.Builder builder = FormField.builder(OS); + builder.setValue(os); + dataFormBuilder.addField(builder.build()); + return this; + } + + /** + * Include Operating System's version as a {@link FormField}. + *
+ * @param os_version Version of OS + * @return Builder + */ + public Builder setOSVersion(String os_version) { + TextSingleFormField.Builder builder = FormField.builder(OS_VERSION); + builder.setValue(os_version); + dataFormBuilder.addField(builder.build()); + return this; + } + + /** + * Include Software name as a {@link FormField}. + *
+ * @param software Name of the software + * @return Builder + */ + public Builder setSoftware(String software) { + TextSingleFormField.Builder builder = FormField.builder(SOFTWARE); + builder.setValue(software); + dataFormBuilder.addField(builder.build()); + return this; + } + + /** + * Include Software Version as a {@link FormField}. + *
+ * @param softwareVersion Version of the Software in use + * @return Builder + */ + public Builder setSoftwareVersion(String softwareVersion) { + TextSingleFormField.Builder builder = FormField.builder(SOFTWARE_VERSION); + builder.setValue(softwareVersion); + dataFormBuilder.addField(builder.build()); + return this; + } + + /** + * Include {@link DataForm} to be encapsulated under SoftwareInfoForm. + *
+ * @param dataForm The dataform containing Software Information + * @return Builder + */ + public Builder setDataForm(DataForm dataForm) { + if (dataForm.getTitle() != null || !dataForm.getItems().isEmpty() + || dataForm.getReportedData() != null || !dataForm.getInstructions().isEmpty()) { + throw new IllegalArgumentException("Illegal Arguements for SoftwareInformation"); + } + String formTypeValue = dataForm.getFormType(); + if (formTypeValue == null) { + throw new IllegalArgumentException("FORM_TYPE Formfield missing"); + } + if (!formTypeValue.equals(SoftwareInfoForm.FORM_TYPE)) { + throw new IllegalArgumentException("Malformed FORM_TYPE Formfield encountered"); + } + this.dataFormBuilder = dataForm.asBuilder(); + return this; + } + + /** + * This method is called to build a {@link SoftwareInfoForm}. + *
+ * @return Builder + */ + public SoftwareInfoForm build() { + return new SoftwareInfoForm(dataFormBuilder.build()); + } + } +} diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/softwareinfo/form/package-info.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/softwareinfo/form/package-info.java new file mode 100644 index 000000000..82ed15af2 --- /dev/null +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/softwareinfo/form/package-info.java @@ -0,0 +1,21 @@ +/** + * + * Copyright 2020 Aditya Borikar + * + * 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. + */ + +/** + * Form class needed for XEP-0232: Software Information. + */ +package org.jivesoftware.smackx.softwareinfo.form; diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/softwareinfo/package-info.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/softwareinfo/package-info.java new file mode 100644 index 000000000..05d8777ab --- /dev/null +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/softwareinfo/package-info.java @@ -0,0 +1,21 @@ +/** + * + * Copyright 2020 Aditya Borikar + * + * 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. + */ + +/** + * Smacks implementation of XEP-0232: Software Information. + */ +package org.jivesoftware.smackx.softwareinfo; diff --git a/smack-extensions/src/test/java/org/jivesoftware/smackx/softwareinfo/SoftwareInfoFormTest.java b/smack-extensions/src/test/java/org/jivesoftware/smackx/softwareinfo/SoftwareInfoFormTest.java new file mode 100644 index 000000000..48613cd02 --- /dev/null +++ b/smack-extensions/src/test/java/org/jivesoftware/smackx/softwareinfo/SoftwareInfoFormTest.java @@ -0,0 +1,143 @@ +/** + * + * Copyright 2020 Aditya Borikar + * + * 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.softwareinfo; + +import static org.jivesoftware.smack.test.util.XmlUnitUtils.assertXmlSimilar; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import java.net.URI; +import java.net.URISyntaxException; + +import org.jivesoftware.smack.test.util.SmackTestSuite; +import org.jivesoftware.smackx.mediaelement.element.MediaElement; +import org.jivesoftware.smackx.softwareinfo.form.SoftwareInfoForm; +import org.jivesoftware.smackx.xdata.FormField; +import org.jivesoftware.smackx.xdata.TextSingleFormField; +import org.jivesoftware.smackx.xdata.packet.DataForm; +import org.jivesoftware.smackx.xdata.packet.DataForm.Type; + +import org.junit.jupiter.api.Test; + +public class SoftwareInfoFormTest extends SmackTestSuite { + + private final String xml = + "" + + "" + + "urn:xmpp:dataforms:softwareinfo" + + "" + + "" + + "" + + "" + + "http://www.shakespeare.lit/clients/exodus.jpg" + + "" + + "" + + "cid:sha1+f24030b8d91d233bac14777be5ab531ca3b9f102@bob.xmpp.org" + + "" + + "" + + "" + + "" + + "Windows" + + "" + + "" + + "XP" + + "" + + "" + + "Exodus" + + "" + + "" + + "0.9.1" + + "" + + ""; + + @Test + public void softwareInfoBuilderTest() throws URISyntaxException { + SoftwareInfoForm softwareInfoForm = createSoftwareInfoForm(); + assertXmlSimilar(xml, softwareInfoForm.getDataForm().toXML()); + + softwareInfoForm = createSoftwareInfoFormUsingDataForm(); + assertXmlSimilar(xml, softwareInfoForm.getDataForm().toXML()); + } + + @Test + public void getInfoFromSoftwareInfoFormTest() throws URISyntaxException { + SoftwareInfoForm softwareInfoForm = createSoftwareInfoForm(); + assertEquals("Windows", softwareInfoForm.getOS()); + assertEquals("XP", softwareInfoForm.getOSVersion()); + assertEquals("Exodus", softwareInfoForm.getSoftwareName()); + assertEquals("0.9.1", softwareInfoForm.getSoftwareVersion()); + assertXmlSimilar(createMediaElement().toXML(), softwareInfoForm.getIcon().toXML()); + } + + @Test + public void faultySoftwareInfoFormsTest() { + DataForm.Builder dataFormbuilder = DataForm.builder(Type.result); + TextSingleFormField formField = FormField.buildHiddenFormType("faulty_formtype"); + dataFormbuilder.addField(formField); + assertThrows(IllegalArgumentException.class, () -> { + SoftwareInfoForm.getBuilder().setDataForm(dataFormbuilder.build()).build(); + }); + + DataForm.Builder builderWithoutFormType = DataForm.builder(Type.result); + assertThrows(IllegalArgumentException.class, () -> { + SoftwareInfoForm.getBuilder().setDataForm(builderWithoutFormType.build()).build(); + }); + } + + public static SoftwareInfoForm createSoftwareInfoFormUsingDataForm() throws URISyntaxException { + DataForm.Builder dataFormBuilder = DataForm.builder(Type.result); + TextSingleFormField formField = FormField.buildHiddenFormType(SoftwareInfoForm.FORM_TYPE); + dataFormBuilder.addField(formField); + + dataFormBuilder.addField(FormField.builder("icon") + .addFormFieldChildElement(createMediaElement()) + .build()); + dataFormBuilder.addField(FormField.builder("os") + .setValue("Windows") + .build()); + dataFormBuilder.addField(FormField.builder("os_version") + .setValue("XP") + .build()); + dataFormBuilder.addField(FormField.builder("software") + .setValue("Exodus") + .build()); + dataFormBuilder.addField(FormField.builder("software_version") + .setValue("0.9.1") + .build()); + + SoftwareInfoForm softwareInfoForm = SoftwareInfoForm.getBuilder().setDataForm(dataFormBuilder.build()).build(); + return softwareInfoForm; + } + + public static SoftwareInfoForm createSoftwareInfoForm() throws URISyntaxException { + return SoftwareInfoForm.getBuilder() + .setIcon(createMediaElement()) + .setOS("Windows") + .setOSVersion("XP") + .setSoftware("Exodus") + .setSoftwareVersion("0.9.1") + .build(); + } + + public static MediaElement createMediaElement() throws URISyntaxException { + return MediaElement.builder() + .addUri(new URI("http://www.shakespeare.lit/clients/exodus.jpg"), "image/jpeg") + .addUri(new URI("cid:sha1+f24030b8d91d233bac14777be5ab531ca3b9f102@bob.xmpp.org"), "image/jpeg") + .setHeightAndWidth(80, 290) + .build(); + } +} diff --git a/smack-extensions/src/test/java/org/jivesoftware/smackx/softwareinfo/SoftwareInfoManagerTest.java b/smack-extensions/src/test/java/org/jivesoftware/smackx/softwareinfo/SoftwareInfoManagerTest.java new file mode 100644 index 000000000..09b17bb75 --- /dev/null +++ b/smack-extensions/src/test/java/org/jivesoftware/smackx/softwareinfo/SoftwareInfoManagerTest.java @@ -0,0 +1,102 @@ +/** + * + * Copyright 2020 Aditya Borikar + * + * 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.softwareinfo; + +import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; + +import org.jivesoftware.smack.SmackException; +import org.jivesoftware.smack.XMPPConnection; +import org.jivesoftware.smack.XMPPException; +import org.jivesoftware.smack.parsing.SmackParsingException; +import org.jivesoftware.smack.xml.XmlPullParserException; + +import org.jivesoftware.smackx.mediaelement.element.MediaElement; +import org.jivesoftware.smackx.softwareinfo.form.SoftwareInfoForm; +import org.jivesoftware.smackx.xdata.FormField; +import org.jivesoftware.smackx.xdata.packet.DataForm; +import org.jivesoftware.smackx.xdata.packet.DataForm.Type; + +import org.jivesoftware.util.ConnectionUtils; +import org.jivesoftware.util.Protocol; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.jxmpp.jid.EntityFullJid; +import org.jxmpp.jid.JidTestUtil; + +public class SoftwareInfoManagerTest { + + private static final EntityFullJid initiatorJID = JidTestUtil.DUMMY_AT_EXAMPLE_ORG_SLASH_DUMMYRESOURCE; + private XMPPConnection connection; + private Protocol protocol; + + @BeforeEach + public void setup() throws XMPPException, SmackException, InterruptedException { + protocol = new Protocol(); + connection = ConnectionUtils.createMockedConnection(protocol, initiatorJID); + } + + @Test + public void softwareInfoManagerTest() throws IOException, XmlPullParserException, SmackParsingException, URISyntaxException { + SoftwareInfoManager manager = SoftwareInfoManager.getInstanceFor(connection); + manager.publishSoftwareInformationForm(buildSoftwareInfoFormUsingBuilder()); + manager.publishSoftwareInformationForm(buildSoftwareInfoFromDataForm()); + } + + public static SoftwareInfoForm buildSoftwareInfoFormUsingBuilder() throws URISyntaxException { + SoftwareInfoForm.Builder builder = SoftwareInfoForm.getBuilder(); + MediaElement mediaElement = createMediaElement(); + builder.setIcon(mediaElement); + builder.setOS("Windows"); + builder.setOSVersion("XP"); + builder.setSoftware("Exodus"); + builder.setSoftwareVersion("0.9.1"); + return builder.build(); + } + + public static SoftwareInfoForm buildSoftwareInfoFromDataForm() throws URISyntaxException { + DataForm.Builder dataFormBuilder = DataForm.builder(Type.result); + dataFormBuilder.addField(FormField.buildHiddenFormType(SoftwareInfoForm.FORM_TYPE)); + dataFormBuilder.addField(FormField.builder("icon") + .addFormFieldChildElement(createMediaElement()) + .build()); + dataFormBuilder.addField(FormField.builder("os") + .setValue("Windows") + .build()); + dataFormBuilder.addField(FormField.builder("os_version") + .setValue("XP") + .build()); + dataFormBuilder.addField(FormField.builder("software") + .setValue("Exodus") + .build()); + dataFormBuilder.addField(FormField.builder("software_version") + .setValue("0.9.1") + .build()); + SoftwareInfoForm softwareInfoForm = SoftwareInfoForm.getBuilder() + .setDataForm(dataFormBuilder.build()) + .build(); + return softwareInfoForm; + } + + public static MediaElement createMediaElement() throws URISyntaxException { + return MediaElement.builder() + .addUri(new MediaElement.Uri(new URI("http://example.org"), "test-type")) + .setHeightAndWidth(16, 16) + .build(); + } +} diff --git a/smack-integration-test/src/main/java/org/jivesoftware/smackx/softwareInfo/SoftwareInfoIntegrationTest.java b/smack-integration-test/src/main/java/org/jivesoftware/smackx/softwareInfo/SoftwareInfoIntegrationTest.java new file mode 100644 index 000000000..e2e37fae5 --- /dev/null +++ b/smack-integration-test/src/main/java/org/jivesoftware/smackx/softwareInfo/SoftwareInfoIntegrationTest.java @@ -0,0 +1,77 @@ +/** + * + * Copyright 2020 Aditya Borikar + * + * 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.softwareInfo; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; + +import org.jivesoftware.smack.parsing.SmackParsingException; +import org.jivesoftware.smack.xml.XmlPullParserException; +import org.jivesoftware.smackx.mediaelement.element.MediaElement; +import org.jivesoftware.smackx.softwareinfo.SoftwareInfoManager; +import org.jivesoftware.smackx.softwareinfo.form.SoftwareInfoForm; + +import org.igniterealtime.smack.inttest.AbstractSmackIntegrationTest; +import org.igniterealtime.smack.inttest.SmackIntegrationTestEnvironment; +import org.igniterealtime.smack.inttest.annotations.BeforeClass; +import org.igniterealtime.smack.inttest.annotations.SmackIntegrationTest; +import org.igniterealtime.smack.inttest.util.IntegrationTestRosterUtil; + +public class SoftwareInfoIntegrationTest extends AbstractSmackIntegrationTest { + + public final SoftwareInfoManager sim1; + public final SoftwareInfoManager sim2; + + public SoftwareInfoIntegrationTest(SmackIntegrationTestEnvironment environment) + throws IOException, XmlPullParserException, SmackParsingException { + super(environment); + sim1 = SoftwareInfoManager.getInstanceFor(conOne); + sim2 = SoftwareInfoManager.getInstanceFor(conTwo); + } + + @BeforeClass + public void setUp() throws Exception { + IntegrationTestRosterUtil.ensureBothAccountsAreSubscribedToEachOther(conOne, conTwo, timeout); + } + + @SmackIntegrationTest + public void test() throws Exception { + SoftwareInfoForm softwareInfoSent = createSoftwareInfoForm(); + sim1.publishSoftwareInformationForm(softwareInfoSent); + SoftwareInfoForm softwareInfoFormReceived = sim2.fromJid(conOne.getUser()); + assertTrue(softwareInfoFormReceived.equals(softwareInfoSent)); + } + + private static SoftwareInfoForm createSoftwareInfoForm() throws URISyntaxException { + SoftwareInfoForm.Builder builder = SoftwareInfoForm.getBuilder(); + MediaElement mediaElement = MediaElement.builder() + .addUri(new MediaElement.Uri(new URI("http://example.org"), "test-type")) + .setHeightAndWidth(16, 16) + .build(); + + SoftwareInfoForm softwareInfoForm = builder.setIcon(mediaElement) + .setOS("Windows") + .setOSVersion("XP") + .setSoftware("Exodus") + .setSoftwareVersion("0.9.1") + .build(); + return softwareInfoForm; + } +} diff --git a/smack-integration-test/src/main/java/org/jivesoftware/smackx/softwareInfo/package-info.java b/smack-integration-test/src/main/java/org/jivesoftware/smackx/softwareInfo/package-info.java new file mode 100644 index 000000000..904fcd506 --- /dev/null +++ b/smack-integration-test/src/main/java/org/jivesoftware/smackx/softwareInfo/package-info.java @@ -0,0 +1,17 @@ +/** + * + * Copyright 2020 Aditya Borikar + * + * 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.softwareInfo;