### Question:
WriterMessageRecorder implements MessageRecorder { @Override public synchronized void recordMessage(NodeID sender, Message message) { if (this.filter != null && !this.filter.acceptMessage(message)) { return; } try { writer.write(formatter.format(new Date())); writer.write(","); writer.write(String.valueOf(message.getCode())); writer.write(","); writer.write(String.valueOf(message.getCommand())); writer.write(","); if (message instanceof UscMessage) { writer.write(String.valueOf(((UscMessage) message).getMessage().getMessageType())); } writer.write(","); writer.write(Hex.toHexString(message.getEncoded())); writer.write(","); if (sender != null) { writer.write(Hex.toHexString(sender.getID())); } writer.newLine(); writer.flush(); } catch (IOException ex) { logger.error("Exception recording message: ", ex); } } WriterMessageRecorder(BufferedWriter writer, MessageFilter filter); @Override synchronized void recordMessage(NodeID sender, Message message);  }### Answer:
@Test public void recordUscMessage() throws IOException { Message message = createUscMessage(); StringWriter writer = new StringWriter(); BufferedWriter bwriter = new BufferedWriter(writer); WriterMessageRecorder recorder = new WriterMessageRecorder(bwriter, null); recorder.recordMessage(null, message); bwriter.close(); String result = writer.toString(); String encoded = Hex.toHexString(message.getEncoded()); Assert.assertTrue(result.contains("," + encoded + ",")); Assert.assertTrue(result.contains(",0,USC_MESSAGE,GET_BLOCK_MESSAGE,")); }
@Test public void recordUscMessageWithSender() throws IOException { Message message = createUscMessage(); StringWriter writer = new StringWriter(); BufferedWriter bwriter = new BufferedWriter(writer); Random random = new Random(); byte[] nodeId = new byte[10]; random.nextBytes(nodeId); NodeID sender = new NodeID(nodeId); WriterMessageRecorder recorder = new WriterMessageRecorder(bwriter, null); recorder.recordMessage(sender, message); bwriter.close(); String result = writer.toString(); String encoded = Hex.toHexString(message.getEncoded()); Assert.assertTrue(result.contains("," + encoded + "," + Hex.toHexString(nodeId))); Assert.assertTrue(result.contains(",0,USC_MESSAGE,GET_BLOCK_MESSAGE,")); }
@Test public void filterMessage() throws IOException { Message message = createUscMessage(); StringWriter writer = new StringWriter(); BufferedWriter bwriter = new BufferedWriter(writer); List<String> commands = new ArrayList<>(); commands.add("TRANSACTIONS"); MessageFilter filter = new MessageFilter(commands); Random random = new Random(); byte[] nodeId = new byte[10]; random.nextBytes(nodeId); NodeID sender = new NodeID(nodeId); WriterMessageRecorder recorder = new WriterMessageRecorder(bwriter, filter); recorder.recordMessage(sender, message); bwriter.close(); String result = writer.toString(); Assert.assertEquals(0, result.length()); }
@Test public void recordEthMessage() throws IOException { Message message = createEthMessage(); StringWriter writer = new StringWriter(); BufferedWriter bwriter = new BufferedWriter(writer); WriterMessageRecorder recorder = new WriterMessageRecorder(bwriter, null); recorder.recordMessage(null, message); bwriter.close(); String result = writer.toString(); String encoded = Hex.toHexString(message.getEncoded()); Assert.assertTrue(result.contains("," + encoded)); Assert.assertTrue(result.contains(",0,TRANSACTIONS,,")); }