|
| 1 | +package org.eclipse.paho.mqttv5.client.test; |
| 2 | + |
| 3 | +import java.net.URI; |
| 4 | +import java.util.ArrayList; |
| 5 | +import java.util.List; |
| 6 | +import java.util.UUID; |
| 7 | +import java.util.logging.Level; |
| 8 | +import java.util.logging.Logger; |
| 9 | + |
| 10 | +import org.eclipse.paho.mqttv5.client.IMqttDeliveryToken; |
| 11 | +import org.eclipse.paho.mqttv5.client.IMqttToken; |
| 12 | +import org.eclipse.paho.mqttv5.client.MqttAsyncClient; |
| 13 | +import org.eclipse.paho.mqttv5.client.test.client.MqttClientFactoryPaho; |
| 14 | +import org.eclipse.paho.mqttv5.client.test.logging.LoggingUtilities; |
| 15 | +import org.eclipse.paho.mqttv5.client.test.properties.TestProperties; |
| 16 | +import org.eclipse.paho.mqttv5.client.test.utilities.MqttV5Receiver; |
| 17 | +import org.eclipse.paho.mqttv5.client.test.utilities.MqttV5Receiver.ReceivedMessage; |
| 18 | +import org.eclipse.paho.mqttv5.client.test.utilities.Utility; |
| 19 | +import org.eclipse.paho.mqttv5.common.MqttMessage; |
| 20 | +import org.eclipse.paho.mqttv5.common.MqttSubscription; |
| 21 | +import org.eclipse.paho.mqttv5.common.packet.MqttProperties; |
| 22 | +import org.eclipse.paho.mqttv5.common.packet.UserProperty; |
| 23 | +import org.junit.AfterClass; |
| 24 | +import org.junit.Assert; |
| 25 | +import org.junit.BeforeClass; |
| 26 | +import org.junit.Test; |
| 27 | + |
| 28 | +public class SubscribeTests { |
| 29 | + |
| 30 | + static final Class<?> cclass = SubscribeTests.class; |
| 31 | + private static final String className = cclass.getName(); |
| 32 | + private static final Logger log = Logger.getLogger(className); |
| 33 | + |
| 34 | + private static URI serverURI; |
| 35 | + private static MqttClientFactoryPaho clientFactory; |
| 36 | + private static String topicPrefix; |
| 37 | + |
| 38 | + |
| 39 | + @BeforeClass |
| 40 | + public static void setUpBeforeClass() throws Exception { |
| 41 | + try { |
| 42 | + String methodName = Utility.getMethodName(); |
| 43 | + LoggingUtilities.banner(log, cclass, methodName); |
| 44 | + |
| 45 | + serverURI = TestProperties.getServerURI(); |
| 46 | + clientFactory = new MqttClientFactoryPaho(); |
| 47 | + clientFactory.open(); |
| 48 | + topicPrefix = "BasicTest-" + UUID.randomUUID().toString() + "-"; |
| 49 | + |
| 50 | + } catch (Exception exception) { |
| 51 | + log.log(Level.SEVERE , "caught exception:", exception); |
| 52 | + throw exception; |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + @AfterClass |
| 57 | + public static void tearDownAfterClass() throws Exception { |
| 58 | + String methodName = Utility.getMethodName(); |
| 59 | + LoggingUtilities.banner(log, cclass, methodName); |
| 60 | + |
| 61 | + try { |
| 62 | + if(clientFactory != null) { |
| 63 | + clientFactory.close(); |
| 64 | + clientFactory.disconnect(); |
| 65 | + } |
| 66 | + } catch (Exception exception) { |
| 67 | + log.log(Level.SEVERE, "caught exception:", exception); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void testPublishRecieveUserProperties() throws Exception { |
| 73 | + String methodName = Utility.getMethodName(); |
| 74 | + LoggingUtilities.banner(log, cclass, methodName); |
| 75 | + String clientId = methodName; |
| 76 | + String exampleKey = "exampleKey"; |
| 77 | + String exampleValue = "exampleValue"; |
| 78 | + MqttAsyncClient asyncClient = new MqttAsyncClient(serverURI.toString(), clientId); |
| 79 | + MqttV5Receiver mqttV5Receiver = new MqttV5Receiver(asyncClient, LoggingUtilities.getPrintStream()); |
| 80 | + asyncClient.setCallback(mqttV5Receiver); |
| 81 | + |
| 82 | + // Connect to the server |
| 83 | + log.info("Connecting: [serverURI: " + serverURI + ", ClientId: " + clientId + "]"); |
| 84 | + IMqttToken connectToken = asyncClient.connect(); |
| 85 | + connectToken.waitForCompletion(5000); |
| 86 | + String clientId2 = asyncClient.getClientId(); |
| 87 | + log.info("Client ID = " + clientId2); |
| 88 | + boolean isConnected = asyncClient.isConnected(); |
| 89 | + log.info("isConnected: " + isConnected); |
| 90 | + |
| 91 | + // Subscribe to a topic |
| 92 | + log.info("Subscribing to: " + topicPrefix + methodName); |
| 93 | + MqttSubscription subscription = new MqttSubscription(topicPrefix + methodName); |
| 94 | + IMqttToken subscribeToken = asyncClient.subscribe(new MqttSubscription[] {subscription}); |
| 95 | + subscribeToken.waitForCompletion(5000); |
| 96 | + |
| 97 | + // Publish a message to a random topic |
| 98 | + MqttProperties messageProps = new MqttProperties(); |
| 99 | + List<UserProperty> userProps = new ArrayList<UserProperty>(); |
| 100 | + userProps.add(new UserProperty(exampleKey, exampleValue)); |
| 101 | + |
| 102 | + messageProps.setUserProperties(userProps); |
| 103 | + MqttMessage testMessage = new MqttMessage("Test Payload".getBytes(), 2, false, messageProps); |
| 104 | + log.info("Publishing Message with User Properties to: " + topicPrefix + methodName); |
| 105 | + IMqttDeliveryToken deliveryToken = asyncClient.publish(topicPrefix + methodName, testMessage); |
| 106 | + deliveryToken.waitForCompletion(5000); |
| 107 | + |
| 108 | + |
| 109 | + log.info("Waiting for delivery and validating message."); |
| 110 | + ReceivedMessage receivedMessage = mqttV5Receiver.receiveNext(10000); |
| 111 | + MqttProperties receivedProps = receivedMessage.message.getProperties(); |
| 112 | + Assert.assertEquals(1, receivedProps.getUserProperties().size()); |
| 113 | + UserProperty recievedUserProps = receivedProps.getUserProperties().get(0); |
| 114 | + Assert.assertEquals(exampleKey, recievedUserProps.getKey()); |
| 115 | + Assert.assertEquals(exampleValue, recievedUserProps.getValue()); |
| 116 | + log.info("Received User Property: " + recievedUserProps.toString()); |
| 117 | + |
| 118 | + |
| 119 | + log.info("Disconnecting..."); |
| 120 | + IMqttToken disconnectToken = asyncClient.disconnect(); |
| 121 | + disconnectToken.waitForCompletion(5000); |
| 122 | + Assert.assertFalse(asyncClient.isConnected()); |
| 123 | + asyncClient.close(); |
| 124 | + |
| 125 | + } |
| 126 | + |
| 127 | + |
| 128 | +} |
0 commit comments