### Question:
MessageFilter { public boolean acceptMessage(Message message) { if (commands.isEmpty()) { return true; } String command = String.valueOf(message.getCommand()); if (commands.contains(command)) { return true; } if (!(message instanceof UscMessage)) { return false; } String messageType = String.valueOf(((UscMessage)message).getMessage().getMessageType()); for (String cmd : commands) { if (!cmd.contains(":")) { continue; } String[] parts = cmd.split("\\:"); if (parts.length != 2) { continue; } if (!parts[0].equals(command)) { continue; } if (parts[1].equals(messageType)) { return true; } } return false; } MessageFilter(List<String> commands); boolean acceptMessage(Message message);  }### Answer:
@Test public void filterWithoutFilteredCommandsAcceptsAnyMessage() { MessageFilter filter = new MessageFilter(null); Message uscMessage = WriterMessageRecorderTest.createUscMessage(); Message ethMessage = WriterMessageRecorderTest.createEthMessage(); Assert.assertTrue(filter.acceptMessage(uscMessage)); Assert.assertTrue(filter.acceptMessage(ethMessage)); }
@Test public void filterWithEmptyCommandsAcceptsAnyMessage() { List<String> commands = new ArrayList<>(); MessageFilter filter = new MessageFilter(commands); Message uscMessage = WriterMessageRecorderTest.createUscMessage(); Message ethMessage = WriterMessageRecorderTest.createEthMessage(); Assert.assertTrue(filter.acceptMessage(uscMessage)); Assert.assertTrue(filter.acceptMessage(ethMessage)); }
@Test public void filterAcceptsEthMessageWhenCommandIsSpecified() { List<String> commands = new ArrayList<>(); commands.add("TRANSACTIONS"); MessageFilter filter = new MessageFilter(commands); Message uscMessage = WriterMessageRecorderTest.createUscMessage(); Message ethMessage = WriterMessageRecorderTest.createEthMessage(); Assert.assertFalse(filter.acceptMessage(uscMessage)); Assert.assertTrue(filter.acceptMessage(ethMessage)); }
@Test public void filterAcceptUscMessageWhenCommandIsSpecified() { List<String> commands = new ArrayList<>(); commands.add("USC_MESSAGE"); MessageFilter filter = new MessageFilter(commands); Message uscMessage = WriterMessageRecorderTest.createUscMessage(); Message ethMessage = WriterMessageRecorderTest.createEthMessage(); Assert.assertTrue(filter.acceptMessage(uscMessage)); Assert.assertFalse(filter.acceptMessage(ethMessage)); }
@Test public void filterMessagesWhenCommandsAreSpecified() { List<String> commands = new ArrayList<>(); commands.add("USC_MESSAGE"); commands.add("TRANSACTIONS"); commands.add("BLOCKS"); MessageFilter filter = new MessageFilter(commands); Message uscMessage = WriterMessageRecorderTest.createUscMessage(); Message ethMessage = WriterMessageRecorderTest.createEthMessage(); Assert.assertTrue(filter.acceptMessage(uscMessage)); Assert.assertTrue(filter.acceptMessage(ethMessage)); }
@Test public void filterMessagesWhenUscMessageTypeIsSpecified() { List<String> commands = new ArrayList<>(); commands.add("USC_MESSAGE:GET_BLOCK_MESSAGE"); commands.add("TRANSACTIONS"); commands.add("BLOCKS"); MessageFilter filter = new MessageFilter(commands); Message uscMessage = WriterMessageRecorderTest.createUscMessage(); Message ethMessage = WriterMessageRecorderTest.createEthMessage(); Assert.assertTrue(filter.acceptMessage(uscMessage)); Assert.assertTrue(filter.acceptMessage(ethMessage)); }
@Test public void rejectUscMessageIfMessageTypeDoesNotMatch() { List<String> commands = new ArrayList<>(); commands.add("USC_MESSAGE:GET_BLOCKS_MESSAGE"); commands.add("TRANSACTIONS"); commands.add("BLOCKS"); MessageFilter filter = new MessageFilter(commands); Message uscMessage = WriterMessageRecorderTest.createUscMessage(); Message ethMessage = WriterMessageRecorderTest.createEthMessage(); Assert.assertFalse(filter.acceptMessage(uscMessage)); Assert.assertTrue(filter.acceptMessage(ethMessage)); }