Adapted Netty parser to use ByteBuffer instead of byte[]

This commit is contained in:
andrea
2013-11-26 12:25:48 +01:00
parent d596e41716
commit e9bb34898b
5 changed files with 49 additions and 17 deletions
@@ -2,6 +2,8 @@ package org.dna.mqtt.moquette.parser.netty;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
import org.dna.mqtt.moquette.proto.messages.AbstractMessage;
@@ -65,7 +67,8 @@ public class PublishDecoderTest {
public void testHeaderWithMessageID_Payload() throws Exception {
m_buff = Unpooled.buffer(14);
int messageID = 123;
byte[] payload = new byte[]{0x0A, 0x0B, 0x0C};
// byte[] payload = new byte[]{0x0A, 0x0B, 0x0C};
ByteBuffer payload = ByteBuffer.allocate(3).put(new byte[]{0x0A, 0x0B, 0x0C});
initHeaderWithMessageID_Payload(m_buff, messageID, payload);
//Exercise
@@ -76,7 +79,8 @@ public class PublishDecoderTest {
assertNotNull(message);
assertEquals("Fake Topic", message.getTopicName());
assertEquals(messageID, (int) message.getMessageID());
TestUtils.verifyEquals(payload, message.getPayload());
// TestUtils.verifyEquals(payload, message.getPayload());
assertEquals(payload, message.getPayload());
}
@Test
@@ -141,7 +145,9 @@ public class PublishDecoderTest {
PublishMessage message = (PublishMessage)m_results.get(0);
assertNotNull(message);
assertEquals("/topic", message.getTopicName());
assertEquals("Test my payload", new String(message.getPayload()));
// assertEquals("Test my payload", new String(message.getPayload()));
Buffer expectedPayload = ByteBuffer.allocate(15).put("Test my payload".getBytes()).flip();
assertEquals(expectedPayload, message.getPayload());
}
private void initHeader(ByteBuf buff) throws IllegalAccessException {
@@ -170,6 +176,16 @@ public class PublishDecoderTest {
buff.writeBytes(tmp);
}
private void initHeaderWithMessageID_Payload(ByteBuf buff, int messageID, ByteBuffer payload) throws IllegalAccessException {
ByteBuf tmp = Unpooled.buffer(4).writeBytes(Utils.encodeString("Fake Topic"));
tmp.writeShort(messageID);
tmp.writeBytes(payload);
buff.clear().writeByte(AbstractMessage.PUBLISH << 4 | 0x02) //set Qos to 1
.writeBytes(Utils.encodeRemainingLength(tmp.readableBytes()));
//topic name
buff.writeBytes(tmp);
}
private ByteBuf generatePublishQoS0(ByteBuf payload) throws IllegalAccessException {
int size = payload.capacity();
ByteBuf messageBody = Unpooled.buffer(size);
@@ -3,6 +3,7 @@ package org.dna.mqtt.moquette.parser.netty;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import java.nio.ByteBuffer;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
@@ -35,7 +36,8 @@ public class PublishEncoderTest {
msg.setTopicName(topic);
//variable part
byte[] payload = new byte[]{0x0A, 0x0B, 0x0C};
byte[] bpayload = new byte[]{0x0A, 0x0B, 0x0C};
ByteBuffer payload = (ByteBuffer) ByteBuffer.allocate(3).put(bpayload).flip();
msg.setPayload(payload);
//Exercise
@@ -48,7 +50,8 @@ public class PublishEncoderTest {
//Variable part
verifyString(topic, m_out);
verifyBuff(payload.length, payload, m_out);
payload.rewind();
verifyBuff(bpayload.length, payload, m_out);
}
@@ -61,7 +64,8 @@ public class PublishEncoderTest {
msg.setTopicName(topic);
//variable part
byte[] payload = new byte[]{0x0A, 0x0B, 0x0C};
byte[] bpayload = new byte[]{0x0A, 0x0B, 0x0C};
ByteBuffer payload = (ByteBuffer) ByteBuffer.allocate(3).put(bpayload).flip();
msg.setPayload(payload);
//Exercise
@@ -76,7 +80,8 @@ public class PublishEncoderTest {
verifyString(topic, m_out);
assertEquals(0, m_out.readByte()); //MessageID MSB
assertEquals(1, m_out.readByte()); //MessageID LSB
verifyBuff(payload.length, payload, m_out);
payload.rewind();
verifyBuff(bpayload.length, payload, m_out);
}
@Test(expected = IllegalArgumentException.class)
@@ -120,7 +125,8 @@ public class PublishEncoderTest {
msg.setTopicName(null);
//variable part
byte[] payload = new byte[]{0x0A, 0x0B, 0x0C};
byte[] bpayload = new byte[]{0x0A, 0x0B, 0x0C};
ByteBuffer payload = (ByteBuffer) ByteBuffer.allocate(3).put(bpayload).flip();
msg.setPayload(payload);
ByteBuf out = Unpooled.buffer();
@@ -136,7 +142,8 @@ public class PublishEncoderTest {
msg.setTopicName(topic);
//variable part
byte[] payload = "Test my payload".getBytes();
byte[] bpayload = "Test my payload".getBytes();
ByteBuffer payload = (ByteBuffer) ByteBuffer.allocate(bpayload.length).put(bpayload).flip();
msg.setPayload(payload);
//Exercise
@@ -145,16 +152,17 @@ public class PublishEncoderTest {
//Verify
//2 byte header + (2+6) topic name + [2 message ID if QoQ <> 0]+ payload
int size = m_out.readableBytes();
assertEquals(10+payload.length, size);
assertEquals(10 + bpayload.length, size);
assertEquals(0x30, m_out.readByte()); //1 byte
assertEquals(23, m_out.readByte()); //1 byte the length
}
@Test
public void testEncodeBigMessage() throws Exception {
byte[] payload = new byte[256];
byte[] bpayload = new byte[256];
TestUtils.generateRandomPayload(256).readBytes(payload);
TestUtils.generateRandomPayload(256).readBytes(bpayload);
ByteBuffer payload = (ByteBuffer) ByteBuffer.allocate(bpayload.length).put(bpayload).flip();
String topic = "/topic";
PublishMessage msg = new PublishMessage();
@@ -168,7 +176,7 @@ public class PublishEncoderTest {
m_encoder.encode(m_mockedContext, msg, m_out);
int size = m_out.readableBytes();
assertEquals(11+payload.length, size);
assertEquals(11 + bpayload.length, size);
assertEquals(0x30, m_out.readByte()); //1 byte
}
@@ -6,6 +6,7 @@ import io.netty.buffer.Unpooled;
import io.netty.buffer.UnpooledByteBufAllocator;
import io.netty.channel.ChannelHandlerContext;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.*;
@@ -31,6 +32,11 @@ public class TestUtils {
}
}
static void verifyBuff(int numBytes, ByteBuffer bytes, ByteBuf buff) {
assertTrue(numBytes <= buff.readableBytes());
assertEquals(bytes, buff.nioBuffer());
}
/**
* Verify the presence of the given string starting from the current position
* inside the buffer.