Skip to content

Commit e41c4cb

Browse files
committed
Adding some basic tests for the WebSocket functionality
Signed-off-by: James Sutton <james.sutton@uk.ibm.com>
1 parent 2bd10f1 commit e41c4cb

3 files changed

Lines changed: 277 additions & 5 deletions

File tree

Lines changed: 253 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,253 @@
1+
/* Copyright (c) 2009, 2014 IBM Corp.
2+
*
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* and Eclipse Distribution License v1.0 which accompany this distribution.
6+
*
7+
* The Eclipse Public License is available at
8+
* http://www.eclipse.org/legal/epl-v10.html
9+
* and the Eclipse Distribution License is available at
10+
* http://www.eclipse.org/org/documents/edl-v10.php.
11+
*
12+
*******************************************************************************/
13+
14+
package org.eclipse.paho.client.mqttv3.test;
15+
16+
import java.net.URI;
17+
import java.util.ArrayList;
18+
import java.util.logging.Level;
19+
import java.util.logging.Logger;
20+
21+
import org.eclipse.paho.client.mqttv3.IMqttClient;
22+
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
23+
import org.eclipse.paho.client.mqttv3.MqttCallback;
24+
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
25+
import org.eclipse.paho.client.mqttv3.MqttException;
26+
import org.eclipse.paho.client.mqttv3.MqttMessage;
27+
import org.eclipse.paho.client.mqttv3.MqttTopic;
28+
import org.eclipse.paho.client.mqttv3.test.client.MqttClientFactoryPaho;
29+
import org.eclipse.paho.client.mqttv3.test.logging.LoggingUtilities;
30+
import org.eclipse.paho.client.mqttv3.test.properties.TestProperties;
31+
import org.eclipse.paho.client.mqttv3.test.utilities.Utility;
32+
import org.junit.AfterClass;
33+
import org.junit.Assert;
34+
import org.junit.BeforeClass;
35+
import org.junit.Test;
36+
37+
38+
/**
39+
* Tests providing a basic general coverage for the MQTT WebSocket Functionality
40+
*/
41+
42+
public class WebSocketTest {
43+
44+
static final Class<?> cclass = WebSocketTest.class;
45+
private static final String className = cclass.getName();
46+
private static final Logger log = Logger.getLogger(className);
47+
48+
private static URI serverURI;
49+
private static MqttClientFactoryPaho clientFactory;
50+
51+
/**
52+
* @throws Exception
53+
*/
54+
@BeforeClass
55+
public static void setUpBeforeClass() throws Exception {
56+
57+
try {
58+
String methodName = Utility.getMethodName();
59+
LoggingUtilities.banner(log, cclass, methodName);
60+
61+
serverURI = TestProperties.getWebSocketServerURI();
62+
clientFactory = new MqttClientFactoryPaho();
63+
clientFactory.open();
64+
}
65+
catch (Exception exception) {
66+
log.log(Level.SEVERE, "caught exception:", exception);
67+
throw exception;
68+
}
69+
}
70+
71+
/**
72+
* @throws Exception
73+
*/
74+
@AfterClass
75+
public static void tearDownAfterClass() throws Exception {
76+
String methodName = Utility.getMethodName();
77+
LoggingUtilities.banner(log, cclass, methodName);
78+
79+
try {
80+
if (clientFactory != null) {
81+
clientFactory.close();
82+
clientFactory.disconnect();
83+
}
84+
}
85+
catch (Exception exception) {
86+
log.log(Level.SEVERE, "caught exception:", exception);
87+
}
88+
}
89+
90+
/**
91+
* @throws Exception
92+
*/
93+
@Test
94+
public void testWebSocketConnect() throws Exception {
95+
String methodName = Utility.getMethodName();
96+
LoggingUtilities.banner(log, cclass, methodName);
97+
98+
IMqttClient client = null;
99+
try {
100+
String clientId = methodName;
101+
client = clientFactory.createMqttClient(serverURI, clientId);
102+
103+
log.info("Connecting...(serverURI:" + serverURI + ", ClientId:" + clientId);
104+
client.connect();
105+
106+
String clientId2 = client.getClientId();
107+
log.info("clientId = " + clientId2);
108+
109+
boolean isConnected = client.isConnected();
110+
log.info("isConnected = " + isConnected);
111+
112+
String id = client.getServerURI();
113+
log.info("ServerURI = " + id);
114+
115+
log.info("Disconnecting...");
116+
client.disconnect();
117+
118+
log.info("Re-Connecting...");
119+
client.connect();
120+
121+
log.info("Disconnecting...");
122+
client.disconnect();
123+
}
124+
catch (MqttException exception) {
125+
log.log(Level.SEVERE, "caught exception:", exception);
126+
Assert.fail("Unexpected exception: " + exception);
127+
}
128+
finally {
129+
if (client != null) {
130+
log.info("Close...");
131+
client.close();
132+
}
133+
}
134+
}
135+
136+
137+
/**
138+
* @throws Exception
139+
*/
140+
@Test
141+
public void testWebSocketPubSub() throws Exception {
142+
String methodName = Utility.getMethodName();
143+
LoggingUtilities.banner(log, cclass, methodName);
144+
145+
IMqttClient client = null;
146+
try {
147+
String topicStr = "topic" + "_02";
148+
String clientId = methodName;
149+
client = clientFactory.createMqttClient(serverURI, clientId);
150+
151+
log.info("Assigning callback...");
152+
MessageListener listener = new MessageListener();
153+
client.setCallback(listener);
154+
155+
log.info("Connecting...(serverURI:" + serverURI + ", ClientId:" + clientId);
156+
client.connect();
157+
158+
log.info("Subscribing to..." + topicStr);
159+
client.subscribe(topicStr);
160+
161+
log.info("Publishing to..." + topicStr);
162+
MqttTopic topic = client.getTopic(topicStr);
163+
MqttMessage message = new MqttMessage("foo".getBytes());
164+
topic.publish(message);
165+
166+
log.info("Checking msg");
167+
MqttMessage msg = listener.getNextMessage();
168+
Assert.assertNotNull(msg);
169+
Assert.assertEquals("foo", msg.toString());
170+
171+
log.info("getTopic name");
172+
String topicName = topic.getName();
173+
log.info("topicName = " + topicName);
174+
Assert.assertEquals(topicName, topicStr);
175+
176+
log.info("Disconnecting...");
177+
client.disconnect();
178+
}
179+
finally {
180+
if (client != null) {
181+
log.info("Close...");
182+
client.close();
183+
}
184+
}
185+
}
186+
187+
188+
189+
190+
// -------------------------------------------------------------
191+
// Helper methods/classes
192+
// -------------------------------------------------------------
193+
194+
static final Class<MessageListener> cclass2 = MessageListener.class;
195+
static final String classSimpleName2 = cclass2.getSimpleName();
196+
static final String classCanonicalName2 = cclass2.getCanonicalName();
197+
static final Logger logger2 = Logger.getLogger(classCanonicalName2);
198+
199+
/**
200+
*
201+
*/
202+
class MessageListener implements MqttCallback {
203+
204+
ArrayList<MqttMessage> messages;
205+
206+
public MessageListener() {
207+
messages = new ArrayList<MqttMessage>();
208+
}
209+
210+
public MqttMessage getNextMessage() {
211+
synchronized (messages) {
212+
if (messages.size() == 0) {
213+
try {
214+
messages.wait(1000);
215+
}
216+
catch (InterruptedException e) {
217+
// empty
218+
}
219+
}
220+
221+
if (messages.size() == 0) {
222+
return null;
223+
}
224+
return messages.remove(0);
225+
}
226+
}
227+
228+
public void connectionLost(Throwable cause) {
229+
logger2.info("connection lost: " + cause.getMessage());
230+
}
231+
232+
/**
233+
* @param token
234+
*/
235+
public void deliveryComplete(IMqttDeliveryToken token) {
236+
logger2.info("delivery complete");
237+
}
238+
239+
/**
240+
* @param topic
241+
* @param message
242+
* @throws Exception
243+
*/
244+
public void messageArrived(String topic, MqttMessage message) throws Exception {
245+
logger2.info("message arrived: " + new String(message.getPayload()) + "'");
246+
247+
synchronized (messages) {
248+
messages.add(message);
249+
messages.notifyAll();
250+
}
251+
}
252+
}
253+
}

org.eclipse.paho.client.mqttv3.test/src/test/java/org/eclipse/paho/client/mqttv3/test/properties/TestProperties.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ public class TestProperties {
108108
static public final String KEY_CLIENT_TRUST_STORE = "CLIENT_TRUST_STORE";
109109

110110
static public final String KEY_SERVER_SSL_PORT = "SERVER_SSL_PORT";
111+
112+
static public final String KEY_SERVER_WEBSOCKET_URI = "SERVER_WEBSOCKET_URI";
111113

112114
static private Map<String, String> defaults = new HashMap<String, String>();
113115

@@ -378,6 +380,22 @@ public static URI getServerURI() throws URISyntaxException {
378380
log.exiting(className, methodName, string);
379381
return uri;
380382
}
383+
384+
385+
/**
386+
* @return The WebSocket Server URI which may be set in the constructor of an MqttClient
387+
* @throws URISyntaxException
388+
*/
389+
public static URI getWebSocketServerURI() throws URISyntaxException {
390+
String methodName = Utility.getMethodName();
391+
log.entering(className, methodName);
392+
393+
String string = getInstance().getProperty(KEY_SERVER_WEBSOCKET_URI);
394+
URI uri = new URI(string);
395+
396+
log.exiting(className, methodName, string);
397+
return uri;
398+
}
381399

382400
/**
383401
* Returns a list of URIs which may set in the MQTTConnectOptions for an HA testcase
Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
# This is the server URI which will be set in the constructor of an MQTT Client
2-
# The default is "tcp://<localhost>:1883" with <localhost> expressed in IPV4 dotted decimal notation
1+
# This is the server URI which will be set in the constructor of an MQTT Client
2+
# The default is "tcp://<localhost>:1883" with <localhost> expressed in IPV4 dotted decimal notation
33
SERVER_URI=tcp://iot.eclipse.org:1883
44
CLIENT_KEY_STORE=clientkeystore.jks
55
CLIENT_KEY_STORE_PASSWORD=password
66
CLIENT_TRUST_STORE=clientkeystore.jks
7-
SERVER_SSL_PORT=18884
7+
SERVER_SSL_PORT=8883
8+
SERVER_WEBSOCKET_URI=ws://iot.eclipse.org:80
89

910
# The list of server URIs which may be set in the MQTT ConnectOptions for an HA testcase.
10-
# There is no default value
11-
# URI.0=tcp://localhost:1883
11+
# There is no default value
12+
# URI.0=tcp://localhost:1883
1213
# URI.1=tcp://localhost:1884
1314
# URI.2=tcp://localhost:1885

0 commit comments

Comments
 (0)