2014-06-11 23:40:19 +02:00
|
|
|
/*
|
2017-03-19 22:01:23 +01:00
|
|
|
* Copyright (c) 2012-2017 The original author or authors
|
2014-06-11 23:40:19 +02:00
|
|
|
* ------------------------------------------------------
|
|
|
|
|
* All rights reserved. This program and the accompanying materials
|
|
|
|
|
* are made available under the terms of the Eclipse Public License v1.0
|
|
|
|
|
* and Apache License v2.0 which accompanies this distribution.
|
|
|
|
|
*
|
|
|
|
|
* The Eclipse Public License is available at
|
|
|
|
|
* http://www.eclipse.org/legal/epl-v10.html
|
|
|
|
|
*
|
|
|
|
|
* The Apache License v2.0 is available at
|
|
|
|
|
* http://www.opensource.org/licenses/apache2.0.php
|
|
|
|
|
*
|
|
|
|
|
* You may elect to redistribute this code under either of these licenses.
|
|
|
|
|
*/
|
2013-12-27 17:55:10 +01:00
|
|
|
|
2017-02-01 16:15:48 +01:00
|
|
|
package io.moquette.server;
|
2013-12-27 17:55:10 +01:00
|
|
|
|
2015-11-20 20:54:27 +01:00
|
|
|
import io.moquette.server.config.IConfig;
|
|
|
|
|
import io.moquette.server.config.MemoryConfig;
|
2013-12-27 17:55:10 +01:00
|
|
|
import org.eclipse.paho.client.mqttv3.IMqttClient;
|
|
|
|
|
import org.eclipse.paho.client.mqttv3.MqttClient;
|
|
|
|
|
import org.eclipse.paho.client.mqttv3.MqttClientPersistence;
|
|
|
|
|
import org.eclipse.paho.client.mqttv3.MqttMessage;
|
|
|
|
|
import org.eclipse.paho.client.mqttv3.persist.MqttDefaultFilePersistence;
|
|
|
|
|
import org.junit.After;
|
|
|
|
|
import org.junit.Before;
|
|
|
|
|
import org.junit.BeforeClass;
|
2015-03-15 16:45:11 +01:00
|
|
|
import org.junit.Test;
|
2013-12-27 17:55:10 +01:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
2015-03-15 16:45:11 +01:00
|
|
|
import java.io.File;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.util.Properties;
|
2018-04-25 19:39:12 +02:00
|
|
|
|
|
|
|
|
import static java.nio.charset.StandardCharsets.UTF_8;
|
2015-03-15 16:45:11 +01:00
|
|
|
import static org.junit.Assert.assertEquals;
|
|
|
|
|
|
2013-12-27 17:55:10 +01:00
|
|
|
public class ServerIntegrationQoSValidationTest {
|
2017-02-01 16:15:48 +01:00
|
|
|
|
2013-12-27 17:55:10 +01:00
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(ServerIntegrationPahoTest.class);
|
|
|
|
|
|
|
|
|
|
Server m_server;
|
|
|
|
|
static MqttClientPersistence s_subDataStore;
|
|
|
|
|
static MqttClientPersistence s_pubDataStore;
|
2017-02-01 16:15:48 +01:00
|
|
|
|
2013-12-27 17:55:10 +01:00
|
|
|
IMqttClient m_subscriber;
|
|
|
|
|
IMqttClient m_publisher;
|
2016-03-06 19:03:01 +01:00
|
|
|
MessageCollector m_callback;
|
2015-08-22 15:35:11 +02:00
|
|
|
IConfig m_config;
|
2013-12-27 17:55:10 +01:00
|
|
|
|
|
|
|
|
@BeforeClass
|
|
|
|
|
public static void beforeTests() {
|
|
|
|
|
String tmpDir = System.getProperty("java.io.tmpdir");
|
|
|
|
|
s_subDataStore = new MqttDefaultFilePersistence(tmpDir + File.separator + "subscriber");
|
|
|
|
|
s_pubDataStore = new MqttDefaultFilePersistence(tmpDir + File.separator + "publisher");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void startServer() throws IOException {
|
|
|
|
|
m_server = new Server();
|
2016-05-18 00:09:40 +02:00
|
|
|
final Properties configProps = IntegrationUtils.prepareTestProperties();
|
2015-08-22 15:35:11 +02:00
|
|
|
m_config = new MemoryConfig(configProps);
|
|
|
|
|
m_server.startServer(m_config);
|
2013-12-27 17:55:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Before
|
|
|
|
|
public void setUp() throws Exception {
|
|
|
|
|
startServer();
|
|
|
|
|
|
|
|
|
|
m_subscriber = new MqttClient("tcp://localhost:1883", "Subscriber", s_subDataStore);
|
2016-03-06 19:03:01 +01:00
|
|
|
m_callback = new MessageCollector();
|
2013-12-27 17:55:10 +01:00
|
|
|
m_subscriber.setCallback(m_callback);
|
|
|
|
|
m_subscriber.connect();
|
2017-02-01 16:15:48 +01:00
|
|
|
|
2013-12-27 17:55:10 +01:00
|
|
|
m_publisher = new MqttClient("tcp://localhost:1883", "Publisher", s_pubDataStore);
|
|
|
|
|
m_publisher.connect();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@After
|
|
|
|
|
public void tearDown() throws Exception {
|
|
|
|
|
if (m_publisher.isConnected()) {
|
|
|
|
|
m_publisher.disconnect();
|
|
|
|
|
}
|
2017-02-01 16:15:48 +01:00
|
|
|
|
2013-12-27 17:55:10 +01:00
|
|
|
if (m_subscriber.isConnected()) {
|
|
|
|
|
m_subscriber.disconnect();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_server.stopServer();
|
|
|
|
|
}
|
2017-02-01 16:15:48 +01:00
|
|
|
|
2013-12-27 17:55:10 +01:00
|
|
|
@Test
|
|
|
|
|
public void checkSubscriberQoS0ReceiveQoS0publishes() throws Exception {
|
|
|
|
|
LOG.info("*** checkSubscriberQoS0ReceiveQoS0publishes ***");
|
|
|
|
|
m_subscriber.subscribe("/topic", 0);
|
2017-02-01 16:15:48 +01:00
|
|
|
|
2018-04-25 19:39:12 +02:00
|
|
|
m_publisher.publish("/topic", "Hello world MQTT QoS0".getBytes(UTF_8), 0, false);
|
2017-01-28 10:06:30 +01:00
|
|
|
MqttMessage message = m_callback.waitMessage(1);
|
2013-12-27 17:55:10 +01:00
|
|
|
assertEquals("Hello world MQTT QoS0", message.toString());
|
|
|
|
|
assertEquals(0, message.getQos());
|
|
|
|
|
}
|
2017-02-01 16:15:48 +01:00
|
|
|
|
2013-12-27 17:55:10 +01:00
|
|
|
@Test
|
|
|
|
|
public void checkSubscriberQoS0ReceiveQoS1publishes_downgrade() throws Exception {
|
|
|
|
|
LOG.info("*** checkSubscriberQoS0ReceiveQoS1publishes_downgrade ***");
|
|
|
|
|
m_subscriber.subscribe("/topic", 0);
|
2017-02-01 16:15:48 +01:00
|
|
|
|
2018-04-25 19:39:12 +02:00
|
|
|
m_publisher.publish("/topic", "Hello world MQTT QoS1".getBytes(UTF_8), 1, false);
|
2017-01-28 10:06:30 +01:00
|
|
|
MqttMessage message = m_callback.waitMessage(1);
|
2013-12-27 17:55:10 +01:00
|
|
|
assertEquals("Hello world MQTT QoS1", message.toString());
|
|
|
|
|
assertEquals(0, message.getQos());
|
|
|
|
|
}
|
2017-02-01 16:15:48 +01:00
|
|
|
|
2013-12-27 17:55:10 +01:00
|
|
|
@Test
|
|
|
|
|
public void checkSubscriberQoS0ReceiveQoS2publishes_downgrade() throws Exception {
|
|
|
|
|
LOG.info("*** checkSubscriberQoS0ReceiveQoS2publishes_downgrade ***");
|
|
|
|
|
m_subscriber.subscribe("/topic", 0);
|
2017-02-01 16:15:48 +01:00
|
|
|
|
2018-04-25 19:39:12 +02:00
|
|
|
m_publisher.publish("/topic", "Hello world MQTT QoS2".getBytes(UTF_8), 2, false);
|
2017-01-28 10:06:30 +01:00
|
|
|
MqttMessage message = m_callback.waitMessage(1);
|
2013-12-27 17:55:10 +01:00
|
|
|
assertEquals("Hello world MQTT QoS2", message.toString());
|
|
|
|
|
assertEquals(0, message.getQos());
|
|
|
|
|
}
|
2017-02-01 16:15:48 +01:00
|
|
|
|
2013-12-27 17:55:10 +01:00
|
|
|
@Test
|
|
|
|
|
public void checkSubscriberQoS1ReceiveQoS0publishes() throws Exception {
|
|
|
|
|
LOG.info("*** checkSubscriberQoS1ReceiveQoS0publishes ***");
|
|
|
|
|
m_subscriber.subscribe("/topic", 1);
|
2017-02-01 16:15:48 +01:00
|
|
|
|
2018-04-25 19:39:12 +02:00
|
|
|
m_publisher.publish("/topic", "Hello world MQTT QoS0".getBytes(UTF_8), 0, false);
|
2017-01-28 10:06:30 +01:00
|
|
|
MqttMessage message = m_callback.waitMessage(1);
|
2013-12-27 17:55:10 +01:00
|
|
|
assertEquals("Hello world MQTT QoS0", message.toString());
|
|
|
|
|
assertEquals(0, message.getQos());
|
|
|
|
|
}
|
2017-02-01 16:15:48 +01:00
|
|
|
|
2013-12-27 17:55:10 +01:00
|
|
|
@Test
|
|
|
|
|
public void checkSubscriberQoS1ReceiveQoS1publishes() throws Exception {
|
|
|
|
|
LOG.info("*** checkSubscriberQoS1ReceiveQoS1publishes ***");
|
|
|
|
|
m_subscriber.subscribe("/topic", 1);
|
2017-02-01 16:15:48 +01:00
|
|
|
|
2018-04-25 19:39:12 +02:00
|
|
|
m_publisher.publish("/topic", "Hello world MQTT QoS1".getBytes(UTF_8), 1, false);
|
2017-01-28 10:06:30 +01:00
|
|
|
MqttMessage message = m_callback.waitMessage(1);
|
2013-12-27 17:55:10 +01:00
|
|
|
assertEquals("Hello world MQTT QoS1", message.toString());
|
|
|
|
|
assertEquals(1, message.getQos());
|
|
|
|
|
}
|
2017-02-01 16:15:48 +01:00
|
|
|
|
2013-12-27 17:55:10 +01:00
|
|
|
@Test
|
|
|
|
|
public void checkSubscriberQoS1ReceiveQoS2publishes_downgrade() throws Exception {
|
|
|
|
|
LOG.info("*** checkSubscriberQoS1ReceiveQoS2publishes_downgrade ***");
|
|
|
|
|
m_subscriber.subscribe("/topic", 1);
|
2017-02-01 16:15:48 +01:00
|
|
|
|
2018-04-25 19:39:12 +02:00
|
|
|
m_publisher.publish("/topic", "Hello world MQTT QoS2".getBytes(UTF_8), 2, false);
|
2017-01-28 10:06:30 +01:00
|
|
|
MqttMessage message = m_callback.waitMessage(1);
|
2013-12-27 17:55:10 +01:00
|
|
|
assertEquals("Hello world MQTT QoS2", message.toString());
|
|
|
|
|
assertEquals(1, message.getQos());
|
|
|
|
|
}
|
2017-02-01 16:15:48 +01:00
|
|
|
|
2013-12-27 17:55:10 +01:00
|
|
|
@Test
|
|
|
|
|
public void checkSubscriberQoS2ReceiveQoS0publishes() throws Exception {
|
|
|
|
|
LOG.info("*** checkSubscriberQoS2ReceiveQoS0publishes ***");
|
|
|
|
|
m_subscriber.subscribe("/topic", 2);
|
2017-02-01 16:15:48 +01:00
|
|
|
|
2018-04-25 19:39:12 +02:00
|
|
|
m_publisher.publish("/topic", "Hello world MQTT QoS2".getBytes(UTF_8), 0, false);
|
2017-01-28 10:06:30 +01:00
|
|
|
MqttMessage message = m_callback.waitMessage(1);
|
2013-12-27 17:55:10 +01:00
|
|
|
assertEquals("Hello world MQTT QoS2", message.toString());
|
|
|
|
|
assertEquals(0, message.getQos());
|
|
|
|
|
}
|
2017-02-01 16:15:48 +01:00
|
|
|
|
2013-12-27 17:55:10 +01:00
|
|
|
@Test
|
|
|
|
|
public void checkSubscriberQoS2ReceiveQoS1publishes() throws Exception {
|
|
|
|
|
LOG.info("*** checkSubscriberQoS2ReceiveQoS1publishes ***");
|
|
|
|
|
m_subscriber.subscribe("/topic", 2);
|
2017-02-01 16:15:48 +01:00
|
|
|
|
2018-04-25 19:39:12 +02:00
|
|
|
m_publisher.publish("/topic", "Hello world MQTT QoS2".getBytes(UTF_8), 1, false);
|
2017-01-28 10:06:30 +01:00
|
|
|
MqttMessage message = m_callback.waitMessage(1);
|
2013-12-27 17:55:10 +01:00
|
|
|
assertEquals("Hello world MQTT QoS2", message.toString());
|
|
|
|
|
assertEquals(1, message.getQos());
|
|
|
|
|
}
|
2017-02-01 16:15:48 +01:00
|
|
|
|
2013-12-27 17:55:10 +01:00
|
|
|
@Test
|
|
|
|
|
public void checkSubscriberQoS2ReceiveQoS2publishes() throws Exception {
|
|
|
|
|
LOG.info("*** checkSubscriberQoS2ReceiveQoS2publishes ***");
|
|
|
|
|
m_subscriber.subscribe("/topic", 2);
|
2017-02-01 16:15:48 +01:00
|
|
|
|
2018-04-25 19:39:12 +02:00
|
|
|
m_publisher.publish("/topic", "Hello world MQTT QoS2".getBytes(UTF_8), 2, false);
|
2017-01-28 10:06:30 +01:00
|
|
|
MqttMessage message = m_callback.waitMessage(1);
|
2013-12-27 17:55:10 +01:00
|
|
|
assertEquals("Hello world MQTT QoS2", message.toString());
|
|
|
|
|
assertEquals(2, message.getQos());
|
|
|
|
|
}
|
2017-02-01 16:15:48 +01:00
|
|
|
|
2013-12-27 17:55:10 +01:00
|
|
|
}
|