private void createMQTTConnection(BrokerConfiguration config) { channel = new EmbeddedChannel(); connection = createMQTTConnection(config, 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 connectNotCleanAs(String clientId) { EmbeddedChannel channel = new EmbeddedChannel(); MQTTConnection connection = createMQTTConnection(CONFIG, channel); connection.processConnect(ConnectionTestUtils.buildConnectNotClean(clientId)); ConnectionTestUtils.assertConnectAccepted(channel); return connection; }
@Before public void setUp() { connectMessage = MqttMessageBuilders.connect() .clientId(FAKE_CLIENT_ID) .build(); prepareSUT(); createMQTTConnection(CONFIG); }
@Test public void avoidMultipleNotificationsAfterMultipleReconnection_cleanSessionFalseQoS1() { final MqttConnectMessage notCleanConnect = ConnectionTestUtils.buildConnectNotClean(FAKE_CLIENT_ID); connect(connection, notCleanConnect); subscribe(connection, NEWS_TOPIC, AT_LEAST_ONCE); connection.processDisconnect(null); // connect on another channel final String firstPayload = "Hello MQTT 1"; connectPublishDisconnectFromAnotherClient(firstPayload, NEWS_TOPIC); // reconnect FAKE_CLIENT on another channel EmbeddedChannel anotherChannel2 = new EmbeddedChannel(); MQTTConnection anotherConnection2 = createMQTTConnection(CONFIG, anotherChannel2); anotherConnection2.processConnect(notCleanConnect); ConnectionTestUtils.assertConnectAccepted(anotherChannel2); ConnectionTestUtils.verifyPublishIsReceived(anotherChannel2, MqttQoS.AT_LEAST_ONCE, firstPayload); anotherConnection2.processDisconnect(null); final String secondPayload = "Hello MQTT 2"; connectPublishDisconnectFromAnotherClient(secondPayload, NEWS_TOPIC); EmbeddedChannel anotherChannel3 = new EmbeddedChannel(); MQTTConnection anotherConnection3 = createMQTTConnection(CONFIG, anotherChannel3); anotherConnection3.processConnect(notCleanConnect); ConnectionTestUtils.assertConnectAccepted(anotherChannel3); ConnectionTestUtils.verifyPublishIsReceived(anotherChannel3, MqttQoS.AT_LEAST_ONCE, secondPayload); }
/** * subscriber connect and subscribe on "topic" subscriber disconnects publisher connects and * send two message "hello1" "hello2" to "topic" subscriber connects again and receive "hello1" * "hello2" */ @Test public void checkQoS2SubscriberDisconnectReceivePersistedPublishes() { connect(this.connection, FAKE_CLIENT_ID); subscribe(connection, NEWS_TOPIC, EXACTLY_ONCE); connection.processDisconnect(null); final MQTTConnection publisher = connectAs("Publisher"); publishQos2(publisher, NEWS_TOPIC, "Hello world MQTT!!-1"); publishQos2(publisher, NEWS_TOPIC, "Hello world MQTT!!-2"); createMQTTConnection(CONFIG); connect(this.connection, FAKE_CLIENT_ID); ConnectionTestUtils.verifyPublishIsReceived(channel, EXACTLY_ONCE, "Hello world MQTT!!-1"); ConnectionTestUtils.verifyPublishIsReceived(channel, EXACTLY_ONCE, "Hello world MQTT!!-2"); }
@Test public void checkReplayofStoredPublishResumeAfter_a_disconnect_cleanSessionFalseQoS1() { final MQTTConnection publisher = connectAs("Publisher"); connect(this.connection, FAKE_CLIENT_ID); subscribe(connection, NEWS_TOPIC, AT_LEAST_ONCE); // publish from another channel publishQos1(publisher, NEWS_TOPIC, "Hello world MQTT!!-1", 99); ConnectionTestUtils.verifyPublishIsReceived(channel, AT_LEAST_ONCE, "Hello world MQTT!!-1"); connection.processDisconnect(null); publishQos1(publisher, NEWS_TOPIC, "Hello world MQTT!!-2", 100); publishQos1(publisher, NEWS_TOPIC, "Hello world MQTT!!-3", 101); createMQTTConnection(CONFIG); connect(this.connection, FAKE_CLIENT_ID); ConnectionTestUtils.verifyPublishIsReceived(channel, AT_LEAST_ONCE, "Hello world MQTT!!-2"); ConnectionTestUtils.verifyPublishIsReceived(channel, AT_LEAST_ONCE, "Hello world MQTT!!-3"); }
@Test public void testDontNotifyClientSubscribedToTopicAfterDisconnectedAndReconnectOnNewChannel() { connect(this.connection, FAKE_CLIENT_ID); subscribe(connection, NEWS_TOPIC, AT_MOST_ONCE); // publish on /news 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(channel, AT_MOST_ONCE, "Hello world!"); unsubscribeAndVerifyAck(NEWS_TOPIC); connection.processDisconnect(null); // connect on another channel EmbeddedChannel anotherChannel = new EmbeddedChannel(); MQTTConnection anotherConnection = createMQTTConnection(CONFIG, anotherChannel); anotherConnection.processConnect(connectMessage); ConnectionTestUtils.assertConnectAccepted(anotherChannel); // publish on /news final ByteBuf payload2 = Unpooled.copiedBuffer("Hello world!", Charset.defaultCharset()); sut.receivedPublishQos0(new Topic(NEWS_TOPIC), TEST_USER, TEST_PWD, payload2, false, MqttMessageBuilders.publish() .payload(payload2) .qos(MqttQoS.AT_MOST_ONCE) .retained(false) .topicName(NEWS_TOPIC).build()); ConnectionTestUtils.verifyNoPublishIsReceived(anotherChannel); }
MQTTConnection subscriberConnection = createMQTTConnection(CONFIG, subscriberChannel); subscriberConnection.processConnect(notCleanConnect); ConnectionTestUtils.assertConnectAccepted(subscriberChannel);