2014-04-16 23:59:09 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2014 The Netty Project
|
|
|
|
*
|
|
|
|
* The Netty Project licenses this file to you under the Apache License,
|
|
|
|
* version 2.0 (the "License"); you may not use this file except in compliance
|
|
|
|
* with the License. You may obtain a copy of the License at:
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package io.netty.handler.codec.mqtt;
|
|
|
|
|
|
|
|
import io.netty.buffer.ByteBuf;
|
|
|
|
import io.netty.buffer.ByteBufAllocator;
|
|
|
|
import io.netty.buffer.UnpooledByteBufAllocator;
|
|
|
|
import io.netty.channel.Channel;
|
|
|
|
import io.netty.channel.ChannelHandlerContext;
|
2016-03-16 06:25:39 +01:00
|
|
|
import io.netty.handler.codec.DecoderException;
|
2014-04-16 23:59:09 +02:00
|
|
|
import io.netty.util.CharsetUtil;
|
|
|
|
import org.junit.Before;
|
|
|
|
import org.junit.Test;
|
2017-01-09 22:04:18 +01:00
|
|
|
import org.mockito.Mock;
|
2014-04-16 23:59:09 +02:00
|
|
|
import org.mockito.MockitoAnnotations;
|
|
|
|
|
2017-06-06 02:32:44 +02:00
|
|
|
import java.util.Arrays;
|
2014-04-16 23:59:09 +02:00
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import static org.junit.Assert.*;
|
|
|
|
import static org.mockito.Mockito.*;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unit tests for MqttEncoder and MqttDecoder.
|
|
|
|
*/
|
|
|
|
public class MqttCodecTest {
|
|
|
|
|
|
|
|
private static final String CLIENT_ID = "RANDOM_TEST_CLIENT";
|
|
|
|
private static final String WILL_TOPIC = "/my_will";
|
|
|
|
private static final String WILL_MESSAGE = "gone";
|
|
|
|
private static final String USER_NAME = "happy_user";
|
|
|
|
private static final String PASSWORD = "123_or_no_pwd";
|
|
|
|
|
|
|
|
private static final int KEEP_ALIVE_SECONDS = 600;
|
|
|
|
|
|
|
|
private static final ByteBufAllocator ALLOCATOR = new UnpooledByteBufAllocator(false);
|
|
|
|
|
|
|
|
@Mock
|
|
|
|
private final ChannelHandlerContext ctx = mock(ChannelHandlerContext.class);
|
|
|
|
|
|
|
|
@Mock
|
|
|
|
private final Channel channel = mock(Channel.class);
|
|
|
|
|
|
|
|
private final MqttDecoder mqttDecoder = new MqttDecoder();
|
|
|
|
|
|
|
|
@Before
|
|
|
|
public void setup() {
|
|
|
|
MockitoAnnotations.initMocks(this);
|
|
|
|
when(ctx.channel()).thenReturn(channel);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2014-11-14 11:59:45 +01:00
|
|
|
public void testConnectMessageForMqtt31() throws Exception {
|
|
|
|
final MqttConnectMessage message = createConnectMessage(MqttVersion.MQTT_3_1);
|
|
|
|
ByteBuf byteBuf = MqttEncoder.doEncode(ALLOCATOR, message);
|
|
|
|
|
|
|
|
final List<Object> out = new LinkedList<Object>();
|
|
|
|
mqttDecoder.decode(ctx, byteBuf, out);
|
|
|
|
|
2016-06-14 23:58:42 +02:00
|
|
|
assertEquals("Expected one object but got " + out.size(), 1, out.size());
|
2014-11-14 11:59:45 +01:00
|
|
|
|
|
|
|
final MqttConnectMessage decodedMessage = (MqttConnectMessage) out.get(0);
|
|
|
|
|
|
|
|
validateFixedHeaders(message.fixedHeader(), decodedMessage.fixedHeader());
|
2017-02-17 21:48:58 +01:00
|
|
|
validateConnectVariableHeader(message.variableHeader(), decodedMessage.variableHeader());
|
2014-11-14 11:59:45 +01:00
|
|
|
validateConnectPayload(message.payload(), decodedMessage.payload());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testConnectMessageForMqtt311() throws Exception {
|
|
|
|
final MqttConnectMessage message = createConnectMessage(MqttVersion.MQTT_3_1_1);
|
2014-04-16 23:59:09 +02:00
|
|
|
ByteBuf byteBuf = MqttEncoder.doEncode(ALLOCATOR, message);
|
|
|
|
|
|
|
|
final List<Object> out = new LinkedList<Object>();
|
|
|
|
mqttDecoder.decode(ctx, byteBuf, out);
|
|
|
|
|
2016-06-14 23:58:42 +02:00
|
|
|
assertEquals("Expected one object but got " + out.size(), 1, out.size());
|
2014-04-16 23:59:09 +02:00
|
|
|
|
|
|
|
final MqttConnectMessage decodedMessage = (MqttConnectMessage) out.get(0);
|
|
|
|
|
|
|
|
validateFixedHeaders(message.fixedHeader(), decodedMessage.fixedHeader());
|
2017-02-17 21:48:58 +01:00
|
|
|
validateConnectVariableHeader(message.variableHeader(), decodedMessage.variableHeader());
|
2014-04-16 23:59:09 +02:00
|
|
|
validateConnectPayload(message.payload(), decodedMessage.payload());
|
|
|
|
}
|
|
|
|
|
2016-03-16 06:25:39 +01:00
|
|
|
@Test
|
|
|
|
public void testConnectMessageWithNonZeroReservedFlagForMqtt311() throws Exception {
|
|
|
|
final MqttConnectMessage message = createConnectMessage(MqttVersion.MQTT_3_1_1);
|
|
|
|
ByteBuf byteBuf = MqttEncoder.doEncode(ALLOCATOR, message);
|
|
|
|
try {
|
|
|
|
// Set the reserved flag in the CONNECT Packet to 1
|
|
|
|
byteBuf.setByte(9, byteBuf.getByte(9) | 0x1);
|
|
|
|
final List<Object> out = new LinkedList<Object>();
|
|
|
|
mqttDecoder.decode(ctx, byteBuf, out);
|
|
|
|
|
2016-06-14 23:58:42 +02:00
|
|
|
assertEquals("Expected one object but got " + out.size(), 1, out.size());
|
2016-03-16 06:25:39 +01:00
|
|
|
|
|
|
|
final MqttMessage decodedMessage = (MqttMessage) out.get(0);
|
|
|
|
assertTrue(decodedMessage.decoderResult().isFailure());
|
|
|
|
Throwable cause = decodedMessage.decoderResult().cause();
|
|
|
|
assertTrue(cause instanceof DecoderException);
|
|
|
|
assertEquals("non-zero reserved flag", cause.getMessage());
|
|
|
|
} finally {
|
|
|
|
byteBuf.release();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-16 23:59:09 +02:00
|
|
|
@Test
|
|
|
|
public void testConnAckMessage() throws Exception {
|
|
|
|
final MqttConnAckMessage message = createConnAckMessage();
|
|
|
|
ByteBuf byteBuf = MqttEncoder.doEncode(ALLOCATOR, message);
|
|
|
|
|
|
|
|
final List<Object> out = new LinkedList<Object>();
|
|
|
|
mqttDecoder.decode(ctx, byteBuf, out);
|
|
|
|
|
2016-06-14 23:58:42 +02:00
|
|
|
assertEquals("Expected one object but got " + out.size(), 1, out.size());
|
2014-04-16 23:59:09 +02:00
|
|
|
|
|
|
|
final MqttConnAckMessage decodedMessage = (MqttConnAckMessage) out.get(0);
|
|
|
|
validateFixedHeaders(message.fixedHeader(), decodedMessage.fixedHeader());
|
|
|
|
validateConnAckVariableHeader(message.variableHeader(), decodedMessage.variableHeader());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testPublishMessage() throws Exception {
|
|
|
|
final MqttPublishMessage message = createPublishMessage();
|
|
|
|
ByteBuf byteBuf = MqttEncoder.doEncode(ALLOCATOR, message);
|
|
|
|
|
|
|
|
final List<Object> out = new LinkedList<Object>();
|
|
|
|
mqttDecoder.decode(ctx, byteBuf, out);
|
|
|
|
|
2016-06-14 23:58:42 +02:00
|
|
|
assertEquals("Expected one object but got " + out.size(), 1, out.size());
|
2014-04-16 23:59:09 +02:00
|
|
|
|
|
|
|
final MqttPublishMessage decodedMessage = (MqttPublishMessage) out.get(0);
|
|
|
|
validateFixedHeaders(message.fixedHeader(), decodedMessage.fixedHeader());
|
|
|
|
validatePublishVariableHeader(message.variableHeader(), decodedMessage.variableHeader());
|
|
|
|
validatePublishPayload(message.payload(), decodedMessage.payload());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testPubAckMessage() throws Exception {
|
|
|
|
testMessageWithOnlyFixedHeaderAndMessageIdVariableHeader(MqttMessageType.PUBACK);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testPubRecMessage() throws Exception {
|
|
|
|
testMessageWithOnlyFixedHeaderAndMessageIdVariableHeader(MqttMessageType.PUBREC);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testPubRelMessage() throws Exception {
|
|
|
|
testMessageWithOnlyFixedHeaderAndMessageIdVariableHeader(MqttMessageType.PUBREL);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testPubCompMessage() throws Exception {
|
|
|
|
testMessageWithOnlyFixedHeaderAndMessageIdVariableHeader(MqttMessageType.PUBCOMP);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testSubscribeMessage() throws Exception {
|
|
|
|
final MqttSubscribeMessage message = createSubscribeMessage();
|
|
|
|
ByteBuf byteBuf = MqttEncoder.doEncode(ALLOCATOR, message);
|
|
|
|
|
|
|
|
final List<Object> out = new LinkedList<Object>();
|
|
|
|
mqttDecoder.decode(ctx, byteBuf, out);
|
|
|
|
|
2016-06-14 23:58:42 +02:00
|
|
|
assertEquals("Expected one object but got " + out.size(), 1, out.size());
|
2014-04-16 23:59:09 +02:00
|
|
|
|
|
|
|
final MqttSubscribeMessage decodedMessage = (MqttSubscribeMessage) out.get(0);
|
|
|
|
validateFixedHeaders(message.fixedHeader(), decodedMessage.fixedHeader());
|
|
|
|
validateMessageIdVariableHeader(message.variableHeader(), decodedMessage.variableHeader());
|
|
|
|
validateSubscribePayload(message.payload(), decodedMessage.payload());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testSubAckMessage() throws Exception {
|
|
|
|
final MqttSubAckMessage message = createSubAckMessage();
|
|
|
|
ByteBuf byteBuf = MqttEncoder.doEncode(ALLOCATOR, message);
|
|
|
|
|
|
|
|
final List<Object> out = new LinkedList<Object>();
|
|
|
|
mqttDecoder.decode(ctx, byteBuf, out);
|
|
|
|
|
2016-06-14 23:58:42 +02:00
|
|
|
assertEquals("Expected one object but got " + out.size(), 1, out.size());
|
2014-04-16 23:59:09 +02:00
|
|
|
|
|
|
|
final MqttSubAckMessage decodedMessage = (MqttSubAckMessage) out.get(0);
|
|
|
|
validateFixedHeaders(message.fixedHeader(), decodedMessage.fixedHeader());
|
|
|
|
validateMessageIdVariableHeader(message.variableHeader(), decodedMessage.variableHeader());
|
|
|
|
validateSubAckPayload(message.payload(), decodedMessage.payload());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testUnSubscribeMessage() throws Exception {
|
|
|
|
final MqttUnsubscribeMessage message = createUnsubscribeMessage();
|
|
|
|
ByteBuf byteBuf = MqttEncoder.doEncode(ALLOCATOR, message);
|
|
|
|
|
|
|
|
final List<Object> out = new LinkedList<Object>();
|
|
|
|
mqttDecoder.decode(ctx, byteBuf, out);
|
|
|
|
|
2016-06-14 23:58:42 +02:00
|
|
|
assertEquals("Expected one object but got " + out.size(), 1, out.size());
|
2014-04-16 23:59:09 +02:00
|
|
|
|
|
|
|
final MqttUnsubscribeMessage decodedMessage = (MqttUnsubscribeMessage) out.get(0);
|
|
|
|
validateFixedHeaders(message.fixedHeader(), decodedMessage.fixedHeader());
|
|
|
|
validateMessageIdVariableHeader(message.variableHeader(), decodedMessage.variableHeader());
|
|
|
|
validateUnsubscribePayload(message.payload(), decodedMessage.payload());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testUnsubAckMessage() throws Exception {
|
|
|
|
testMessageWithOnlyFixedHeaderAndMessageIdVariableHeader(MqttMessageType.UNSUBACK);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testPingReqMessage() throws Exception {
|
|
|
|
testMessageWithOnlyFixedHeader(MqttMessageType.PINGREQ);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testPingRespMessage() throws Exception {
|
|
|
|
testMessageWithOnlyFixedHeader(MqttMessageType.PINGRESP);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testDisconnectMessage() throws Exception {
|
|
|
|
testMessageWithOnlyFixedHeader(MqttMessageType.DISCONNECT);
|
|
|
|
}
|
|
|
|
|
2017-07-18 12:26:54 +02:00
|
|
|
@Test
|
|
|
|
public void testUnknownMessageType() throws Exception {
|
|
|
|
|
|
|
|
final MqttMessage message = createMessageWithFixedHeader(MqttMessageType.PINGREQ);
|
|
|
|
ByteBuf byteBuf = MqttEncoder.doEncode(ALLOCATOR, message);
|
|
|
|
try {
|
|
|
|
// setting an invalid message type (15, reserved and forbidden by MQTT 3.1.1 spec)
|
|
|
|
byteBuf.setByte(0, 0xF0);
|
|
|
|
final List<Object> out = new LinkedList<Object>();
|
|
|
|
mqttDecoder.decode(ctx, byteBuf, out);
|
|
|
|
|
|
|
|
assertEquals("Expected one object but got " + out.size(), 1, out.size());
|
|
|
|
|
|
|
|
final MqttMessage decodedMessage = (MqttMessage) out.get(0);
|
|
|
|
assertTrue(decodedMessage.decoderResult().isFailure());
|
|
|
|
Throwable cause = decodedMessage.decoderResult().cause();
|
|
|
|
assertTrue(cause instanceof IllegalArgumentException);
|
|
|
|
assertEquals("unknown message type: 15", cause.getMessage());
|
|
|
|
} finally {
|
|
|
|
byteBuf.release();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-16 23:59:09 +02:00
|
|
|
private void testMessageWithOnlyFixedHeader(MqttMessageType messageType) throws Exception {
|
|
|
|
MqttMessage message = createMessageWithFixedHeader(messageType);
|
|
|
|
ByteBuf byteBuf = MqttEncoder.doEncode(ALLOCATOR, message);
|
|
|
|
|
|
|
|
final List<Object> out = new LinkedList<Object>();
|
|
|
|
mqttDecoder.decode(ctx, byteBuf, out);
|
|
|
|
|
2016-06-14 23:58:42 +02:00
|
|
|
assertEquals("Expected one object but got " + out.size(), 1, out.size());
|
2014-04-16 23:59:09 +02:00
|
|
|
|
|
|
|
final MqttMessage decodedMessage = (MqttMessage) out.get(0);
|
|
|
|
validateFixedHeaders(message.fixedHeader(), decodedMessage.fixedHeader());
|
|
|
|
}
|
|
|
|
|
|
|
|
private void testMessageWithOnlyFixedHeaderAndMessageIdVariableHeader(MqttMessageType messageType)
|
|
|
|
throws Exception {
|
|
|
|
MqttMessage message = createMessageWithFixedHeaderAndMessageIdVariableHeader(messageType);
|
|
|
|
|
|
|
|
ByteBuf byteBuf = MqttEncoder.doEncode(ALLOCATOR, message);
|
|
|
|
|
|
|
|
final List<Object> out = new LinkedList<Object>();
|
|
|
|
mqttDecoder.decode(ctx, byteBuf, out);
|
|
|
|
|
2016-06-14 23:58:42 +02:00
|
|
|
assertEquals("Expected one object but got " + out.size(), 1, out.size());
|
2014-04-16 23:59:09 +02:00
|
|
|
|
|
|
|
final MqttMessage decodedMessage = (MqttMessage) out.get(0);
|
|
|
|
validateFixedHeaders(message.fixedHeader(), decodedMessage.fixedHeader());
|
|
|
|
validateMessageIdVariableHeader(
|
|
|
|
(MqttMessageIdVariableHeader) message.variableHeader(),
|
|
|
|
(MqttMessageIdVariableHeader) decodedMessage.variableHeader());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Factory methods of different MQTT
|
|
|
|
// Message types to help testing
|
|
|
|
|
|
|
|
private static MqttMessage createMessageWithFixedHeader(MqttMessageType messageType) {
|
2014-06-21 09:46:09 +02:00
|
|
|
return new MqttMessage(new MqttFixedHeader(messageType, false, MqttQoS.AT_MOST_ONCE, false, 0));
|
2014-04-16 23:59:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private static MqttMessage createMessageWithFixedHeaderAndMessageIdVariableHeader(MqttMessageType messageType) {
|
|
|
|
MqttFixedHeader mqttFixedHeader =
|
|
|
|
new MqttFixedHeader(
|
|
|
|
messageType,
|
|
|
|
false,
|
2014-06-21 09:46:09 +02:00
|
|
|
messageType == MqttMessageType.PUBREL ? MqttQoS.AT_LEAST_ONCE : MqttQoS.AT_MOST_ONCE,
|
2014-04-16 23:59:09 +02:00
|
|
|
false,
|
|
|
|
0);
|
|
|
|
MqttMessageIdVariableHeader mqttMessageIdVariableHeader = MqttMessageIdVariableHeader.from(12345);
|
|
|
|
return new MqttMessage(mqttFixedHeader, mqttMessageIdVariableHeader);
|
|
|
|
}
|
|
|
|
|
2014-11-14 11:59:45 +01:00
|
|
|
private static MqttConnectMessage createConnectMessage(MqttVersion mqttVersion) {
|
2017-02-17 21:48:58 +01:00
|
|
|
return MqttMessageBuilders.connect()
|
|
|
|
.clientId(CLIENT_ID)
|
|
|
|
.protocolVersion(mqttVersion)
|
|
|
|
.username(USER_NAME)
|
|
|
|
.password(PASSWORD)
|
|
|
|
.willRetain(true)
|
|
|
|
.willQoS(MqttQoS.AT_LEAST_ONCE)
|
|
|
|
.willFlag(true)
|
|
|
|
.willTopic(WILL_TOPIC)
|
|
|
|
.willMessage(WILL_MESSAGE)
|
|
|
|
.cleanSession(true)
|
|
|
|
.keepAlive(KEEP_ALIVE_SECONDS)
|
|
|
|
.build();
|
2014-04-16 23:59:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private static MqttConnAckMessage createConnAckMessage() {
|
2017-02-17 21:48:58 +01:00
|
|
|
return MqttMessageBuilders.connAck()
|
|
|
|
.returnCode(MqttConnectReturnCode.CONNECTION_ACCEPTED)
|
|
|
|
.sessionPresent(true)
|
|
|
|
.build();
|
2014-04-16 23:59:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private static MqttPublishMessage createPublishMessage() {
|
|
|
|
MqttFixedHeader mqttFixedHeader =
|
2014-06-21 09:46:09 +02:00
|
|
|
new MqttFixedHeader(MqttMessageType.PUBLISH, false, MqttQoS.AT_LEAST_ONCE, true, 0);
|
2014-04-16 23:59:09 +02:00
|
|
|
MqttPublishVariableHeader mqttPublishVariableHeader = new MqttPublishVariableHeader("/abc", 1234);
|
|
|
|
ByteBuf payload = ALLOCATOR.buffer();
|
|
|
|
payload.writeBytes("whatever".getBytes(CharsetUtil.UTF_8));
|
|
|
|
return new MqttPublishMessage(mqttFixedHeader, mqttPublishVariableHeader, payload);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static MqttSubscribeMessage createSubscribeMessage() {
|
|
|
|
MqttFixedHeader mqttFixedHeader =
|
2014-06-21 09:46:09 +02:00
|
|
|
new MqttFixedHeader(MqttMessageType.SUBSCRIBE, false, MqttQoS.AT_LEAST_ONCE, true, 0);
|
2014-04-16 23:59:09 +02:00
|
|
|
MqttMessageIdVariableHeader mqttMessageIdVariableHeader = MqttMessageIdVariableHeader.from(12345);
|
|
|
|
|
|
|
|
List<MqttTopicSubscription> topicSubscriptions = new LinkedList<MqttTopicSubscription>();
|
2014-06-21 09:46:09 +02:00
|
|
|
topicSubscriptions.add(new MqttTopicSubscription("/abc", MqttQoS.AT_LEAST_ONCE));
|
|
|
|
topicSubscriptions.add(new MqttTopicSubscription("/def", MqttQoS.AT_LEAST_ONCE));
|
|
|
|
topicSubscriptions.add(new MqttTopicSubscription("/xyz", MqttQoS.EXACTLY_ONCE));
|
2014-04-16 23:59:09 +02:00
|
|
|
|
|
|
|
MqttSubscribePayload mqttSubscribePayload = new MqttSubscribePayload(topicSubscriptions);
|
|
|
|
return new MqttSubscribeMessage(mqttFixedHeader, mqttMessageIdVariableHeader, mqttSubscribePayload);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static MqttSubAckMessage createSubAckMessage() {
|
|
|
|
MqttFixedHeader mqttFixedHeader =
|
2014-06-21 09:46:09 +02:00
|
|
|
new MqttFixedHeader(MqttMessageType.SUBACK, false, MqttQoS.AT_MOST_ONCE, false, 0);
|
2014-04-16 23:59:09 +02:00
|
|
|
MqttMessageIdVariableHeader mqttMessageIdVariableHeader = MqttMessageIdVariableHeader.from(12345);
|
2014-06-21 09:46:09 +02:00
|
|
|
MqttSubAckPayload mqttSubAckPayload = new MqttSubAckPayload(1, 2, 0);
|
2014-04-16 23:59:09 +02:00
|
|
|
return new MqttSubAckMessage(mqttFixedHeader, mqttMessageIdVariableHeader, mqttSubAckPayload);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static MqttUnsubscribeMessage createUnsubscribeMessage() {
|
|
|
|
MqttFixedHeader mqttFixedHeader =
|
2014-06-21 09:46:09 +02:00
|
|
|
new MqttFixedHeader(MqttMessageType.UNSUBSCRIBE, false, MqttQoS.AT_LEAST_ONCE, true, 0);
|
2014-04-16 23:59:09 +02:00
|
|
|
MqttMessageIdVariableHeader mqttMessageIdVariableHeader = MqttMessageIdVariableHeader.from(12345);
|
|
|
|
|
|
|
|
List<String> topics = new LinkedList<String>();
|
|
|
|
topics.add("/abc");
|
|
|
|
topics.add("/def");
|
|
|
|
topics.add("/xyz");
|
|
|
|
|
|
|
|
MqttUnsubscribePayload mqttUnsubscribePayload = new MqttUnsubscribePayload(topics);
|
|
|
|
return new MqttUnsubscribeMessage(mqttFixedHeader, mqttMessageIdVariableHeader, mqttUnsubscribePayload);
|
|
|
|
}
|
|
|
|
|
2017-04-19 22:37:03 +02:00
|
|
|
// Helper methods to compare expected and actual
|
2014-04-16 23:59:09 +02:00
|
|
|
// MQTT messages
|
|
|
|
|
|
|
|
private static void validateFixedHeaders(MqttFixedHeader expected, MqttFixedHeader actual) {
|
|
|
|
assertEquals("MqttFixedHeader MqttMessageType mismatch ", expected.messageType(), actual.messageType());
|
|
|
|
assertEquals("MqttFixedHeader Qos mismatch ", expected.qosLevel(), actual.qosLevel());
|
|
|
|
}
|
|
|
|
|
2017-02-17 21:48:58 +01:00
|
|
|
private static void validateConnectVariableHeader(
|
2014-04-16 23:59:09 +02:00
|
|
|
MqttConnectVariableHeader expected,
|
|
|
|
MqttConnectVariableHeader actual) {
|
|
|
|
assertEquals("MqttConnectVariableHeader Name mismatch ", expected.name(), actual.name());
|
|
|
|
assertEquals(
|
|
|
|
"MqttConnectVariableHeader KeepAliveTimeSeconds mismatch ",
|
|
|
|
expected.keepAliveTimeSeconds(),
|
|
|
|
actual.keepAliveTimeSeconds());
|
|
|
|
assertEquals("MqttConnectVariableHeader Version mismatch ", expected.version(), actual.version());
|
|
|
|
assertEquals("MqttConnectVariableHeader WillQos mismatch ", expected.willQos(), actual.willQos());
|
|
|
|
|
|
|
|
assertEquals("MqttConnectVariableHeader HasUserName mismatch ", expected.hasUserName(), actual.hasUserName());
|
|
|
|
assertEquals("MqttConnectVariableHeader HasPassword mismatch ", expected.hasPassword(), actual.hasPassword());
|
|
|
|
assertEquals(
|
|
|
|
"MqttConnectVariableHeader IsCleanSession mismatch ",
|
|
|
|
expected.isCleanSession(),
|
|
|
|
actual.isCleanSession());
|
|
|
|
assertEquals("MqttConnectVariableHeader IsWillFlag mismatch ", expected.isWillFlag(), actual.isWillFlag());
|
|
|
|
assertEquals(
|
|
|
|
"MqttConnectVariableHeader IsWillRetain mismatch ",
|
|
|
|
expected.isWillRetain(),
|
|
|
|
actual.isWillRetain());
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void validateConnectPayload(MqttConnectPayload expected, MqttConnectPayload actual) {
|
|
|
|
assertEquals(
|
|
|
|
"MqttConnectPayload ClientIdentifier mismatch ",
|
|
|
|
expected.clientIdentifier(),
|
|
|
|
actual.clientIdentifier());
|
|
|
|
assertEquals("MqttConnectPayload UserName mismatch ", expected.userName(), actual.userName());
|
|
|
|
assertEquals("MqttConnectPayload Password mismatch ", expected.password(), actual.password());
|
2017-06-06 02:32:44 +02:00
|
|
|
assertTrue(
|
|
|
|
"MqttConnectPayload Password bytes mismatch ",
|
|
|
|
Arrays.equals(expected.passwordInBytes(), actual.passwordInBytes()));
|
2014-04-16 23:59:09 +02:00
|
|
|
assertEquals("MqttConnectPayload WillMessage mismatch ", expected.willMessage(), actual.willMessage());
|
2017-06-06 02:32:44 +02:00
|
|
|
assertTrue(
|
|
|
|
"MqttConnectPayload WillMessage bytes mismatch ",
|
|
|
|
Arrays.equals(expected.willMessageInBytes(), actual.willMessageInBytes()));
|
2014-04-16 23:59:09 +02:00
|
|
|
assertEquals("MqttConnectPayload WillTopic mismatch ", expected.willTopic(), actual.willTopic());
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void validateConnAckVariableHeader(
|
|
|
|
MqttConnAckVariableHeader expected,
|
|
|
|
MqttConnAckVariableHeader actual) {
|
|
|
|
assertEquals(
|
|
|
|
"MqttConnAckVariableHeader MqttConnectReturnCode mismatch",
|
|
|
|
expected.connectReturnCode(),
|
|
|
|
actual.connectReturnCode());
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void validatePublishVariableHeader(
|
|
|
|
MqttPublishVariableHeader expected,
|
|
|
|
MqttPublishVariableHeader actual) {
|
|
|
|
assertEquals("MqttPublishVariableHeader TopicName mismatch ", expected.topicName(), actual.topicName());
|
|
|
|
assertEquals("MqttPublishVariableHeader MessageId mismatch ", expected.messageId(), actual.messageId());
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void validatePublishPayload(ByteBuf expected, ByteBuf actual) {
|
|
|
|
assertEquals("PublishPayload mismatch ", 0, expected.compareTo(actual));
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void validateMessageIdVariableHeader(
|
|
|
|
MqttMessageIdVariableHeader expected,
|
|
|
|
MqttMessageIdVariableHeader actual) {
|
|
|
|
assertEquals("MqttMessageIdVariableHeader MessageId mismatch ", expected.messageId(), actual.messageId());
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void validateSubscribePayload(MqttSubscribePayload expected, MqttSubscribePayload actual) {
|
|
|
|
List<MqttTopicSubscription> expectedTopicSubscriptions = expected.topicSubscriptions();
|
|
|
|
List<MqttTopicSubscription> actualTopicSubscriptions = actual.topicSubscriptions();
|
|
|
|
|
|
|
|
assertEquals(
|
|
|
|
"MqttSubscribePayload TopicSubscriptionList size mismatch ",
|
|
|
|
expectedTopicSubscriptions.size(),
|
|
|
|
actualTopicSubscriptions.size());
|
|
|
|
for (int i = 0; i < expectedTopicSubscriptions.size(); i++) {
|
|
|
|
validateTopicSubscription(expectedTopicSubscriptions.get(i), actualTopicSubscriptions.get(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void validateTopicSubscription(
|
|
|
|
MqttTopicSubscription expected,
|
|
|
|
MqttTopicSubscription actual) {
|
|
|
|
assertEquals("MqttTopicSubscription TopicName mismatch ", expected.topicName(), actual.topicName());
|
|
|
|
assertEquals(
|
|
|
|
"MqttTopicSubscription Qos mismatch ",
|
|
|
|
expected.qualityOfService(),
|
|
|
|
actual.qualityOfService());
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void validateSubAckPayload(MqttSubAckPayload expected, MqttSubAckPayload actual) {
|
|
|
|
assertArrayEquals(
|
|
|
|
"MqttSubAckPayload GrantedQosLevels mismatch ",
|
|
|
|
expected.grantedQoSLevels().toArray(),
|
|
|
|
actual.grantedQoSLevels().toArray());
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void validateUnsubscribePayload(MqttUnsubscribePayload expected, MqttUnsubscribePayload actual) {
|
|
|
|
assertArrayEquals(
|
|
|
|
"MqttUnsubscribePayload TopicList mismatch ",
|
|
|
|
expected.topics().toArray(),
|
|
|
|
actual.topics().toArray());
|
|
|
|
}
|
|
|
|
}
|