Added encode/decode for Unsubscribe message
This commit is contained in:
+95
@@ -0,0 +1,95 @@
|
||||
package org.dna.mqtt.moquette.parser.netty;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.buffer.Unpooled;
|
||||
import io.netty.handler.codec.CorruptedFrameException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import org.dna.mqtt.moquette.proto.messages.AbstractMessage;
|
||||
import org.dna.mqtt.moquette.proto.messages.SubscribeMessage;
|
||||
import org.dna.mqtt.moquette.proto.messages.UnsubscribeMessage;
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author andrea
|
||||
*/
|
||||
public class UnsubscribeDecoderTest {
|
||||
ByteBuf m_buff;
|
||||
UnsubscribeDecoder m_msgdec;
|
||||
List<Object> m_results;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
m_msgdec = new UnsubscribeDecoder();
|
||||
m_results = new ArrayList<Object >();
|
||||
}
|
||||
|
||||
|
||||
@Test(expected = CorruptedFrameException.class)
|
||||
public void testBadQos() throws Exception {
|
||||
m_buff = Unpooled.buffer(2);
|
||||
initHeaderBadQos(m_buff);
|
||||
|
||||
//Excercise
|
||||
m_msgdec.decode(null, m_buff, m_results);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMultiTopic() throws Exception {
|
||||
m_buff = Unpooled.buffer(4);
|
||||
String topic1 = "a/b";
|
||||
String topic2 = "c/d/e";
|
||||
initMultiTopic(m_buff, 123, topic1, topic2);
|
||||
|
||||
//Excercise
|
||||
m_msgdec.decode(null, m_buff, m_results);
|
||||
|
||||
//Verify
|
||||
assertFalse(m_results.isEmpty());
|
||||
UnsubscribeMessage message = (UnsubscribeMessage)m_results.get(0);
|
||||
assertNotNull(message);
|
||||
assertEquals(2, message.topics().size());
|
||||
assertEquals(topic1, message.topics().get(0));
|
||||
assertEquals(topic2, message.topics().get(1));
|
||||
assertEquals(AbstractMessage.UNSUBSCRIBE, message.getMessageType());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testDecodeSingleTopic_bug() throws Exception {
|
||||
//A2 0C 00 01 00 06 2F 74 6F 70 69 63 //12 byte
|
||||
byte[] overallMessage = new byte[] {(byte)0xA2, 0x0A, //fixed header
|
||||
0x00, 0x01, //MSG ID
|
||||
0x00, 0x06, 0x2F, 0x74, 0x6F, 0x70, 0x69, 0x63}; //"/topic" string
|
||||
m_buff = Unpooled.buffer(overallMessage.length);
|
||||
m_buff.writeBytes(overallMessage);
|
||||
|
||||
m_msgdec.decode(null, m_buff, m_results);
|
||||
|
||||
assertFalse(m_results.isEmpty());
|
||||
UnsubscribeMessage message = (UnsubscribeMessage)m_results.get(0);
|
||||
assertNotNull(message);
|
||||
assertEquals(0x01, (int)message.getMessageID());
|
||||
}
|
||||
|
||||
|
||||
private void initHeaderBadQos(ByteBuf buff) {
|
||||
buff.clear().writeByte(AbstractMessage.UNSUBSCRIBE << 4).writeByte(0);
|
||||
}
|
||||
|
||||
private void initMultiTopic(ByteBuf buff, int messageID, String... topics) throws IllegalAccessException {
|
||||
ByteBuf topicBuffer = Unpooled.buffer(4);
|
||||
topicBuffer.writeShort(messageID);
|
||||
for (String topic : topics) {
|
||||
topicBuffer.writeBytes(Utils.encodeString(topic));
|
||||
}
|
||||
|
||||
buff.clear().writeByte(AbstractMessage.UNSUBSCRIBE << 4 | (byte)0x02)
|
||||
.writeBytes(Utils.encodeRemainingLength(topicBuffer.readableBytes()));
|
||||
buff.writeBytes(topicBuffer);
|
||||
}
|
||||
|
||||
}
|
||||
+99
@@ -0,0 +1,99 @@
|
||||
package org.dna.mqtt.moquette.parser.netty;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.buffer.Unpooled;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import static org.dna.mqtt.moquette.parser.netty.TestUtils.mockChannelHandler;
|
||||
import org.dna.mqtt.moquette.proto.messages.AbstractMessage;
|
||||
import org.dna.mqtt.moquette.proto.messages.UnsubscribeMessage;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import static org.dna.mqtt.moquette.parser.netty.TestUtils.*;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author andrea
|
||||
*/
|
||||
public class UnsubscribeEncoderTest {
|
||||
UnsubscribeEncoder m_encoder = new UnsubscribeEncoder();
|
||||
ChannelHandlerContext m_mockedContext;
|
||||
ByteBuf m_out;
|
||||
UnsubscribeMessage m_msg;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
//mock the ChannelHandlerContext to return an UnpooledAllocator
|
||||
m_mockedContext = mockChannelHandler();
|
||||
m_out = Unpooled.buffer();
|
||||
m_msg = new UnsubscribeMessage();
|
||||
m_msg.setMessageID(0xAABB);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEncodeWithSingleTopic() throws Exception {
|
||||
m_msg.setQos(AbstractMessage.QOSType.LEAST_ONE);
|
||||
|
||||
//variable part
|
||||
String topic1 = "/topic";
|
||||
m_msg.addTopic(topic1);
|
||||
|
||||
//Exercise
|
||||
m_encoder.encode(m_mockedContext, m_msg, m_out);
|
||||
|
||||
//Verify
|
||||
assertEquals((byte)0xA2, (byte)m_out.readByte()); //1 byte
|
||||
//2 messageID + 2 length + 6 chars = 10
|
||||
assertEquals(10, m_out.readByte()); //remaining length
|
||||
|
||||
//verify M1ssageID
|
||||
assertEquals((byte)0xAA, m_out.readByte());
|
||||
assertEquals((byte)0xBB, m_out.readByte());
|
||||
|
||||
//Variable part
|
||||
verifyString(topic1, m_out);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testEncodeWithMultiTopic() throws Exception {
|
||||
m_msg.setQos(AbstractMessage.QOSType.LEAST_ONE);
|
||||
|
||||
//variable part
|
||||
String topic1 = "a/b";
|
||||
String topic2 = "a/b/c";
|
||||
m_msg.addTopic(topic1);
|
||||
m_msg.addTopic(topic2);
|
||||
|
||||
//Exercise
|
||||
m_encoder.encode(m_mockedContext, m_msg, m_out);
|
||||
|
||||
//Verify
|
||||
assertEquals((byte)0xA2, (byte)m_out.readByte()); //1 byte
|
||||
assertEquals(14, m_out.readByte()); //remaining length
|
||||
|
||||
//verify M1ssageID
|
||||
assertEquals((byte)0xAA, m_out.readByte());
|
||||
assertEquals((byte)0xBB, m_out.readByte());
|
||||
|
||||
//Variable part
|
||||
verifyString(topic1, m_out);
|
||||
verifyString(topic2, m_out);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testEncode_empty_topics() throws Exception {
|
||||
m_msg.setQos(AbstractMessage.QOSType.LEAST_ONE);
|
||||
|
||||
//Exercise
|
||||
m_encoder.encode(m_mockedContext, m_msg, m_out);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testEncode_badQos() throws Exception {
|
||||
m_msg.setQos(AbstractMessage.QOSType.EXACTLY_ONCE);
|
||||
|
||||
//Exercise
|
||||
m_encoder.encode(m_mockedContext, m_msg, m_out);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user