IpfixMessageDecoder extends ByteToMessageDecoder { @Override protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception { byte[] array = new byte[msg.readableBytes()]; msg.getBytes(msg.readerIndex(), array); handleCreateIpfixMessages(array, out, msg); }    }
@Test public void testDecode1Message() throws Exception { byte[] inputByte = IPFIXUtilTestHelper.collectMessage("ONT1", "$6$SW2yXjnVMkUwIUeQ$c.JydO461NKo/VTgTYbvtEyVnKQhUr4NRFaFQD3YI5UfC28WTSewVAaR6UjloBFa.N4lVYrr1at25omRhbPOI.", "admin"); IpfixMessageDecoder IpfixMessageDecoder = new IpfixMessageDecoder(); ChannelHandlerContext ctx = mock(ChannelHandlerContext.class); List<Object> checkedList = new ArrayList<>(); IpfixMessageDecoder.decode(ctx, Unpooled.wrappedBuffer(inputByte), checkedList); assertEquals(1, checkedList.size()); IpfixMessagesWrapper ipfixMessagesWrapper = (IpfixMessagesWrapper) checkedList.get(0); assertArrayEquals(inputByte, ipfixMessagesWrapper.getData()); }
@Test public void testDecode2Message() throws Exception { byte[] inputByte = IPFIXUtilTestHelper.collectMessage("ONT1", "$6$SW2yXjnVMkUwIUeQ$c.JydO461NKo/VTgTYbvtEyVnKQhUr4NRFaFQD3YI5UfC28WTSewVAaR6UjloBFa.N4lVYrr1at25omRhbPOI.", "admin"); byte[] inputByte2 = IPFIXUtilTestHelper.collectMessage("ONT1", "$6$SW2yXjnVMkUwIUeQ$c.JydO461NKo/VTgTYbvtEyVnKQhUr4NRFaFQD3YI5UfC28WTSewVAaR6UjloBFa.N4lVYrr1at25omRhbPOI.", "admin"); byte[] testByte = ArrayUtils.addAll(inputByte, inputByte2); assertEquals(inputByte.length + inputByte2.length, testByte.length); IpfixMessageDecoder IpfixMessageDecoder = new IpfixMessageDecoder(); ChannelHandlerContext ctx = mock(ChannelHandlerContext.class); List<Object> checkedList = new ArrayList<>(); IpfixMessageDecoder.decode(ctx, Unpooled.wrappedBuffer(testByte), checkedList); assertEquals(2, checkedList.size()); IpfixMessagesWrapper ipfixMessagesWrapper = (IpfixMessagesWrapper) checkedList.get(0); IpfixMessagesWrapper ipfixMessagesWrapper2 = (IpfixMessagesWrapper) checkedList.get(1); assertArrayEquals(inputByte, ipfixMessagesWrapper.getData()); assertArrayEquals(inputByte2, ipfixMessagesWrapper2.getData()); }
@Test public void testDecodeNotCompleteMessage() throws Exception { byte[] inputByte = IPFIXUtilTestHelper.collectMessage("ONT1", "$6$SW2yXjnVMkUwIUeQ$c.JydO461NKo/VTgTYbvtEyVnKQhUr4NRFaFQD3YI5UfC28WTSewVAaR6UjloBFa.N4lVYrr1at25omRhbPOI.", "admin"); byte[] inputByteFirstHalf = IpfixUtilities.copyByteArray(inputByte, 0, 300); byte[] inputByteSecondHalf = IpfixUtilities.copyByteArray(inputByte, 300, inputByte.length - 300); byte[] testByte = ArrayUtils.addAll(inputByte, inputByteFirstHalf); ByteBuf byteBuf = Unpooled.buffer(testByte.length + inputByteSecondHalf.length); byteBuf.writeBytes(testByte); IpfixMessageDecoder IpfixMessageDecoder = new IpfixMessageDecoder(); ChannelHandlerContext ctx = mock(ChannelHandlerContext.class); List<Object> checkedList = new ArrayList<>(); IpfixMessageDecoder.decode(ctx, byteBuf, checkedList); assertEquals(1, checkedList.size()); IpfixMessagesWrapper ipfixMessagesWrapper = (IpfixMessagesWrapper) checkedList.get(0); assertArrayEquals(inputByte, ipfixMessagesWrapper.getData()); byteBuf.writeBytes(inputByteSecondHalf); IpfixMessageDecoder.decode(ctx, byteBuf, checkedList); assertEquals(2, checkedList.size()); Object checkedOutput2 = checkedList.get(1); IpfixMessagesWrapper ipfixMessagesWrapper2 = (IpfixMessagesWrapper) checkedOutput2; assertArrayEquals(inputByte, ipfixMessagesWrapper2.getData()); }