protected void connect(MQTTConnection connection, String clientId) { MqttConnectMessage connectMessage = ConnectionTestUtils.buildConnect(clientId); connect(connection, connectMessage); }
@Test public void forwardQoS1PublishesWhenNotCleanSessionReconnects() { connection.processConnect(ConnectionTestUtils.buildConnectNotClean(FAKE_CLIENT_ID)); ConnectionTestUtils.assertConnectAccepted(channel); subscribe(connection, NEWS_TOPIC, AT_LEAST_ONCE); connection.processDisconnect(null); // publish a QoS 1 message from another client publish a message on the topic EmbeddedChannel pubChannel = new EmbeddedChannel(); MQTTConnection pubConn = createMQTTConnection(ALLOW_ANONYMOUS_AND_ZERO_BYTES_CLID, pubChannel); pubConn.processConnect(ConnectionTestUtils.buildConnect(PUBLISHER_ID)); ConnectionTestUtils.assertConnectAccepted(pubChannel); final ByteBuf anyPayload = Unpooled.copiedBuffer("Any payload", Charset.defaultCharset()); sut.receivedPublishQos1(pubConn, new Topic(NEWS_TOPIC), TEST_USER, anyPayload, 1, true, MqttMessageBuilders.publish() .payload(anyPayload.retainedDuplicate()) .qos(MqttQoS.AT_LEAST_ONCE) .topicName(NEWS_TOPIC).build()); // simulate a reconnection from the other client connection = createMQTTConnection(ALLOW_ANONYMOUS_AND_ZERO_BYTES_CLID); connectMessage = ConnectionTestUtils.buildConnectNotClean(FAKE_CLIENT_ID); connection.processConnect(connectMessage); ConnectionTestUtils.assertConnectAccepted(channel); // Verify ConnectionTestUtils.verifyPublishIsReceived(channel, AT_LEAST_ONCE, "Any payload"); }
@Test public void noPublishToInactiveSession() { // create an inactive session for Subscriber connection.processConnect(ConnectionTestUtils.buildConnectNotClean(SUBSCRIBER_ID)); ConnectionTestUtils.assertConnectAccepted(channel); subscribe(connection, NEWS_TOPIC, AT_LEAST_ONCE); connection.processDisconnect(null); // Exercise EmbeddedChannel pubChannel = new EmbeddedChannel(); MQTTConnection pubConn = createMQTTConnection(ALLOW_ANONYMOUS_AND_ZERO_BYTES_CLID, pubChannel); pubConn.processConnect(ConnectionTestUtils.buildConnect(PUBLISHER_ID)); ConnectionTestUtils.assertConnectAccepted(pubChannel); final ByteBuf anyPayload = Unpooled.copiedBuffer("Any payload", Charset.defaultCharset()); sut.receivedPublishQos1(pubConn, new Topic(NEWS_TOPIC), TEST_USER, anyPayload, 1, true, MqttMessageBuilders.publish() .payload(anyPayload) .qos(MqttQoS.AT_LEAST_ONCE) .retained(true) .topicName(NEWS_TOPIC).build()); verifyNoPublishIsReceived(channel); }
@Before public void setUp() { sessionRegistry = initPostOfficeAndSubsystems(); mockAuthenticator = new MockAuthenticator(singleton(FAKE_CLIENT_ID), singletonMap(TEST_USER, TEST_PWD)); connection = createMQTTConnection(ALLOW_ANONYMOUS_AND_ZERO_BYTES_CLID); connectMessage = ConnectionTestUtils.buildConnect(FAKE_CLIENT_ID); }
@Test public void checkReceivePublishedMessage_after_a_reconnect_with_notCleanSession() { // first connect - subscribe -disconnect connection.processConnect(ConnectionTestUtils.buildConnectNotClean(FAKE_CLIENT_ID)); ConnectionTestUtils.assertConnectAccepted(channel); subscribe(connection, NEWS_TOPIC, AT_LEAST_ONCE); connection.processDisconnect(null); // connect - subscribe from another connection but with same ClientID EmbeddedChannel secondChannel = new EmbeddedChannel(); MQTTConnection secondConn = createMQTTConnection(ALLOW_ANONYMOUS_AND_ZERO_BYTES_CLID, secondChannel); secondConn.processConnect(ConnectionTestUtils.buildConnect(FAKE_CLIENT_ID)); ConnectionTestUtils.assertConnectAccepted(secondChannel); subscribe(secondConn, NEWS_TOPIC, AT_LEAST_ONCE); // publish a QoS 1 message another client publish a message on the topic EmbeddedChannel pubChannel = new EmbeddedChannel(); MQTTConnection pubConn = createMQTTConnection(ALLOW_ANONYMOUS_AND_ZERO_BYTES_CLID, pubChannel); pubConn.processConnect(ConnectionTestUtils.buildConnect(PUBLISHER_ID)); ConnectionTestUtils.assertConnectAccepted(pubChannel); final ByteBuf anyPayload = Unpooled.copiedBuffer("Any payload", Charset.defaultCharset()); sut.receivedPublishQos1(pubConn, new Topic(NEWS_TOPIC), TEST_USER, anyPayload, 1, true, MqttMessageBuilders.publish() .payload(anyPayload.retainedDuplicate()) .qos(MqttQoS.AT_LEAST_ONCE) .topicName(NEWS_TOPIC).build()); // Verify that after a reconnection the client receive the message ConnectionTestUtils.verifyPublishIsReceived(secondChannel, AT_LEAST_ONCE, "Any payload"); }
@Before public void setUp() { sessionRegistry = initPostOfficeAndSubsystems(); mockAuthenticator = new MockAuthenticator(singleton(FAKE_CLIENT_ID), singletonMap(TEST_USER, TEST_PWD)); connection = createMQTTConnection(ALLOW_ANONYMOUS_AND_ZERO_BYTES_CLID); connectMessage = ConnectionTestUtils.buildConnect(FAKE_CLIENT_ID); connection.processConnect(connectMessage); ConnectionTestUtils.assertConnectAccepted(channel); }
private MQTTConnection connectAs(String clientId) { EmbeddedChannel channel = new EmbeddedChannel(); MQTTConnection connection = createMQTTConnection(CONFIG, channel); connection.processConnect(ConnectionTestUtils.buildConnect(clientId)); ConnectionTestUtils.assertConnectAccepted(channel); return connection; }
private MQTTConnection connectAs(String clientId) { EmbeddedChannel channel = new EmbeddedChannel(); MQTTConnection connection = createMQTTConnection(CONFIG, channel); connection.processConnect(ConnectionTestUtils.buildConnect(clientId)); ConnectionTestUtils.assertConnectAccepted(channel); return connection; }
@Test public void testPublishToMultipleSubscribers() { final Set<String> clientIds = new HashSet<>(Arrays.asList(FAKE_CLIENT_ID, FAKE_CLIENT_ID2)); mockAuthenticator = new MockAuthenticator(clientIds, singletonMap(TEST_USER, TEST_PWD)); EmbeddedChannel channel1 = new EmbeddedChannel(); MQTTConnection connection1 = createMQTTConnection(ALLOW_ANONYMOUS_AND_ZERO_BYTES_CLID, channel1); connection1.processConnect(ConnectionTestUtils.buildConnect(FAKE_CLIENT_ID)); ConnectionTestUtils.assertConnectAccepted(channel1); EmbeddedChannel channel2 = new EmbeddedChannel(); MQTTConnection connection2 = createMQTTConnection(ALLOW_ANONYMOUS_AND_ZERO_BYTES_CLID, channel2); connection2.processConnect(ConnectionTestUtils.buildConnect(FAKE_CLIENT_ID2)); ConnectionTestUtils.assertConnectAccepted(channel2); // subscribe final MqttQoS qos = AT_MOST_ONCE; final String newsTopic = NEWS_TOPIC; subscribe(qos, newsTopic, connection1); subscribe(qos, newsTopic, connection2); // Exercise final ByteBuf payload = Unpooled.copiedBuffer("Hello world!", Charset.defaultCharset()); sut.receivedPublishQos0(new Topic(NEWS_TOPIC), TEST_USER, FAKE_CLIENT_ID, payload, false, MqttMessageBuilders.publish() .payload(payload.retainedDuplicate()) .qos(MqttQoS.AT_MOST_ONCE) .retained(false) .topicName(NEWS_TOPIC).build()); // Verify ConnectionTestUtils.verifyReceivePublish(channel1, NEWS_TOPIC, "Hello world!"); ConnectionTestUtils.verifyReceivePublish(channel2, NEWS_TOPIC, "Hello world!"); }
@Test public void testReceiveRetainedPublishRespectingSubscriptionQoSAndNotPublisher() { // publisher publish a retained message on topic /news connection.processConnect(connectMessage); ConnectionTestUtils.assertConnectAccepted(channel); final ByteBuf payload = Unpooled.copiedBuffer("Hello world!", Charset.defaultCharset()); final MqttPublishMessage retainedPubQoS1Msg = MqttMessageBuilders.publish() .payload(payload.retainedDuplicate()) .qos(MqttQoS.AT_LEAST_ONCE) .topicName(NEWS_TOPIC).build(); sut.receivedPublishQos1(connection, new Topic(NEWS_TOPIC), TEST_USER, payload, 1, true, retainedPubQoS1Msg); // subscriber connects subscribe to topic /news and receive the last retained message EmbeddedChannel subChannel = new EmbeddedChannel(); MQTTConnection subConn = createMQTTConnection(ALLOW_ANONYMOUS_AND_ZERO_BYTES_CLID, subChannel); subConn.processConnect(ConnectionTestUtils.buildConnect(SUBSCRIBER_ID)); ConnectionTestUtils.assertConnectAccepted(subChannel); subscribe(subConn, NEWS_TOPIC, MqttQoS.AT_MOST_ONCE); // Verify publish is received ConnectionTestUtils.verifyReceiveRetainedPublish(subChannel, NEWS_TOPIC, "Hello world!", MqttQoS.AT_MOST_ONCE); }
final MqttConnectMessage notCleanConnect = ConnectionTestUtils.buildConnect(FAKE_CLIENT_ID); EmbeddedChannel subscriberChannel = new EmbeddedChannel(); MQTTConnection subscriberConnection = createMQTTConnection(CONFIG, subscriberChannel);
@Test public void testCleanSession_maintainClientSubscriptions() { connection.processConnect(connectMessage); ConnectionTestUtils.assertConnectAccepted(channel); assertEquals("After CONNECT subscription MUST be empty", 0, subscriptions.size()); subscribe(channel, NEWS_TOPIC, AT_MOST_ONCE); assertEquals("Subscribe MUST contain one subscription", 1, subscriptions.size()); connection.processDisconnect(null); assertEquals("Disconnection MUSTN'T clear subscriptions", 1, subscriptions.size()); EmbeddedChannel anotherChannel = new EmbeddedChannel(); MQTTConnection anotherConn = createMQTTConnection(ALLOW_ANONYMOUS_AND_ZERO_BYTES_CLID, anotherChannel); anotherConn.processConnect(ConnectionTestUtils.buildConnect(FAKE_CLIENT_ID)); ConnectionTestUtils.assertConnectAccepted(anotherChannel); assertEquals("After a reconnect, subscription MUST be still present", 1, subscriptions.size()); final ByteBuf payload = Unpooled.copiedBuffer("Hello world!", Charset.defaultCharset()); sut.receivedPublishQos0(new Topic(NEWS_TOPIC), TEST_USER, TEST_PWD, payload, false, MqttMessageBuilders.publish() .payload(payload.retainedDuplicate()) .qos(MqttQoS.AT_MOST_ONCE) .retained(false) .topicName(NEWS_TOPIC).build()); ConnectionTestUtils.verifyPublishIsReceived(anotherChannel, AT_MOST_ONCE, "Hello world!"); }