### Question:
SubscribeEncoder extends DemuxEncoder<SubscribeMessage> { @Override protected void encode(ChannelHandlerContext chc, SubscribeMessage message, ByteBuf out) { if (message.subscriptions().isEmpty()) { throw new IllegalArgumentException("Found a subscribe message with empty topics"); } if (message.getQos() != AbstractMessage.QOSType.LEAST_ONE) { throw new IllegalArgumentException("Expected a message with QOS 1, found " + message.getQos()); } ByteBuf variableHeaderBuff = chc.alloc().buffer(4); variableHeaderBuff.writeShort(message.getMessageID()); for (SubscribeMessage.Couple c : message.subscriptions()) { variableHeaderBuff.writeBytes(Utils.encodeString(c.getTopic())); variableHeaderBuff.writeByte(c.getQos()); } int variableHeaderSize = variableHeaderBuff.readableBytes(); byte flags = Utils.encodeFlags(message); ByteBuf buff = chc.alloc().buffer(2 + variableHeaderSize); buff.writeByte(AbstractMessage.SUBSCRIBE << 4 | flags); buff.writeBytes(Utils.encodeRemainingLength(variableHeaderSize)); buff.writeBytes(variableHeaderBuff); out.writeBytes(buff); }    }### Answer:
@Test public void testEncodeWithMultiTopic() throws Exception { SubscribeMessage msg = new SubscribeMessage(); msg.setQos(AbstractMessage.QOSType.LEAST_ONE); msg.setMessageID(0xAABB); SubscribeMessage.Couple c1 = new SubscribeMessage.Couple((byte)1, "a/b"); SubscribeMessage.Couple c2 = new SubscribeMessage.Couple((byte)0, "a/b/c"); msg.addSubscription(c1); msg.addSubscription(c2); m_encoder.encode(m_mockedContext, msg, m_out); assertEquals((byte)0x82, (byte)m_out.readByte()); assertEquals(16, m_out.readByte()); assertEquals((byte)0xAA, m_out.readByte()); assertEquals((byte)0xBB, m_out.readByte()); verifyString(c1.getTopic(), m_out); assertEquals(c1.getQos(), m_out.readByte()); verifyString(c2.getTopic(), m_out); assertEquals(c2.getQos(), m_out.readByte()); }
@Test(expected = IllegalArgumentException.class) public void testEncode_empty_subscription() throws Exception { SubscribeMessage msg = new SubscribeMessage(); msg.setQos(AbstractMessage.QOSType.LEAST_ONE); msg.setMessageID(0xAABB); m_encoder.encode(m_mockedContext, msg, m_out); }
@Test(expected = IllegalArgumentException.class) public void testEncode_badQos() throws Exception { SubscribeMessage msg = new SubscribeMessage(); msg.setQos(AbstractMessage.QOSType.EXACTLY_ONCE); msg.setMessageID(0xAABB); m_encoder.encode(m_mockedContext, msg, m_out); }