### Question:
SubscribeEncoder implements MessageEncoder<SubscribeMessage> { public void encode(IoSession session, SubscribeMessage message, ProtocolEncoderOutput out) throws Exception { if (message.subscriptions().isEmpty()) { throw new IllegalArgumentException("Found a subscribe message with empty topics"); } if (message.getQos() != QOSType.LEAST_ONE) { throw new IllegalArgumentException("Expected a message with QOS 1, found " + message.getQos()); } IoBuffer variableHeaderBuff = IoBuffer.allocate(4).setAutoExpand(true); Utils.writeWord(variableHeaderBuff, message.getMessageID()); for (Couple c : message.subscriptions()) { variableHeaderBuff.put(Utils.encodeString(c.getTopic())); variableHeaderBuff.put(c.getQos()); } variableHeaderBuff.flip(); int variableHeaderSize = variableHeaderBuff.remaining(); byte flags = Utils.encodeFlags(message); IoBuffer buff = IoBuffer.allocate(2 + variableHeaderSize); buff.put((byte) (AbstractMessage.SUBSCRIBE << 4 | flags)); buff.put(Utils.encodeRemainingLength(variableHeaderSize)); buff.put(variableHeaderBuff).flip(); out.write(buff); }  void encode(IoSession session, SubscribeMessage message, ProtocolEncoderOutput out);  }### Answer:
@Test public void testEncodeWithMultiTopic() throws Exception { SubscribeMessage msg = new SubscribeMessage(); msg.setQos(QOSType.LEAST_ONE); msg.setMessageID(0xAABB); Couple c1 = new Couple((byte)1, "a/b"); Couple c2 = new Couple((byte)0, "a/b/c"); msg.addSubscription(c1); msg.addSubscription(c2); m_encoder.encode(null, msg, m_mockProtoEncoder); assertEquals((byte)0x82, (byte)m_mockProtoEncoder.getBuffer().get()); assertEquals(16, m_mockProtoEncoder.getBuffer().get()); assertEquals((byte)0xAA, m_mockProtoEncoder.getBuffer().get()); assertEquals((byte)0xBB, m_mockProtoEncoder.getBuffer().get()); verifyString(c1.getTopic(), m_mockProtoEncoder.getBuffer()); assertEquals(c1.getQos(), m_mockProtoEncoder.getBuffer().get()); verifyString(c2.getTopic(), m_mockProtoEncoder.getBuffer()); assertEquals(c2.getQos(), m_mockProtoEncoder.getBuffer().get()); }
@Test(expected = IllegalArgumentException.class) public void testEncode_empty_subscription() throws Exception { SubscribeMessage msg = new SubscribeMessage(); msg.setQos(QOSType.LEAST_ONE); msg.setMessageID(0xAABB); m_encoder.encode(null, msg, m_mockProtoEncoder); }
@Test(expected = IllegalArgumentException.class) public void testEncode_badQos() throws Exception { SubscribeMessage msg = new SubscribeMessage(); msg.setQos(QOSType.EXACTLY_ONCE); msg.setMessageID(0xAABB); m_encoder.encode(null, msg, m_mockProtoEncoder); }