### Question:
UnsubscribeEncoder extends DemuxEncoder<UnsubscribeMessage> { @Override protected void encode(ChannelHandlerContext chc, UnsubscribeMessage message, ByteBuf out) { if (message.topics().isEmpty()) { throw new IllegalArgumentException("Found an unsubscribe 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 (String topic : message.topics()) { variableHeaderBuff.writeBytes(Utils.encodeString(topic)); } int variableHeaderSize = variableHeaderBuff.readableBytes(); byte flags = Utils.encodeFlags(message); ByteBuf buff = chc.alloc().buffer(2 + variableHeaderSize); buff.writeByte(AbstractMessage.UNSUBSCRIBE << 4 | flags); buff.writeBytes(Utils.encodeRemainingLength(variableHeaderSize)); buff.writeBytes(variableHeaderBuff); out.writeBytes(buff); }    }### Answer:
@Test public void testEncodeWithSingleTopic() throws Exception { m_msg.setQos(AbstractMessage.QOSType.LEAST_ONE); String topic1 = "/topic"; m_msg.addTopic(topic1); m_encoder.encode(m_mockedContext, m_msg, m_out); assertEquals((byte)0xA2, (byte)m_out.readByte()); assertEquals(10, m_out.readByte()); assertEquals((byte)0xAA, m_out.readByte()); assertEquals((byte)0xBB, m_out.readByte()); verifyString(topic1, m_out); }
@Test public void testEncodeWithMultiTopic() throws Exception { m_msg.setQos(AbstractMessage.QOSType.LEAST_ONE); String topic1 = "a/b"; String topic2 = "a/b/c"; m_msg.addTopic(topic1); m_msg.addTopic(topic2); m_encoder.encode(m_mockedContext, m_msg, m_out); assertEquals((byte)0xA2, (byte)m_out.readByte()); assertEquals(14, m_out.readByte()); assertEquals((byte)0xAA, m_out.readByte()); assertEquals((byte)0xBB, m_out.readByte()); 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); 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); m_encoder.encode(m_mockedContext, m_msg, m_out); }