### Question:
FixMessageDecoder extends MessageToMessageDecoder<ByteBuf> { @Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) { assert (in != null) : "No Buffer"; int arrayIndex = 0; int tagNum = 0; byte[] bytes = new byte[in.readableBytes()]; in.readBytes(bytes); int length = bytes.length; for (; arrayIndex < length; arrayIndex++) { byte b = bytes[arrayIndex]; if (b == '=') { break; } tagNum = tagNum * 10 + (b - '0'); } int offset = arrayIndex + 1; int valueLength = length - arrayIndex - 1; int sumBytes = 0; if (tagNum != 10) { for (byte b : bytes) { sumBytes += b; } sumBytes += 1; } switch (tagNum) { case 8: if (message != null) { throw new DecoderException("Unexpected BeginString(8)"); } message = new FixMessageImpl(); checksum = sumBytes; message.add(tagNum, bytes, offset, valueLength); break; case 10: appendField(tagNum, bytes, offset, valueLength); verifyChecksum(message.getChecksum()); out.add(message); message = null; checksum = 0; break; default: if (tagNum > 0) { appendField(tagNum, bytes, offset, valueLength); checksum += sumBytes; } else { throw new DecoderException("Tag num must be positive but got " + tagNum); } } }  @Override void channelInactive(ChannelHandlerContext ctx); @Override void exceptionCaught(ChannelHandlerContext ctx, Throwable cause);  }### Answer:
@Test public void testDecode() { FixMessageImpl fixMessage = decodeOne("8=FIX.4.1\u00019=90\u000135=0\u000149=INVMGR\u000156=BRKR\u000134=240\u000152=19980604-08:03:31\u000110=129\u0001", decoder); FixMessageHeader header = fixMessage.getHeader(); assertEquals("FIX.4.1", fixMessage.getHeader().getBeginString()); assertEquals(MessageTypes.HEARTBEAT, fixMessage.getMessageType()); assertEquals("INVMGR", header.getSenderCompID()); assertEquals("BRKR", header.getTargetCompID()); assertEquals(240, header.getMsgSeqNum()); assertEquals(129, fixMessage.getChecksum()); final ZonedDateTime value = fixMessage.getValue(FieldType.SendingTime); ZonedDateTime expected = ZonedDateTime.of(LocalDate.of(1998, 6, 4), LocalTime.of(8, 3, 31, 0), DEFAULT_ZONE_ID); assertEquals(expected, value); }
@Test public void testNoBeginTag() { String random = randomAlphanumeric(50); try { decode("100=" + random + "\u00018=FIX.4.2...", decoder); fail("DecoderException is expected"); } catch (DecoderException e) { assertEquals("BeginString tag expected, but got: 100=" + random.substring(0, 10) + "...", e.getMessage()); } }