EmojiCounterBolt extends ChatAlyticsBaseBolt { @VisibleForTesting protected List<EmojiEntity> getEmojisFromMessage(FatMessage fatMessage) { String message = fatMessage.getMessage().getMessage(); if (message == null) { return ImmutableList.of(); } Map<String, EmojiEntity> emojis = Maps.newHashMap(); OfInt charIterator = message.chars().iterator(); Room room = fatMessage.getRoom(); String roomName = null; if (room != null) { roomName = room.getName(); } boolean capturingEmoji = false; StringBuilder emojiStrBuilder = new StringBuilder(); while (charIterator.hasNext()) { int asciiValue = charIterator.next(); char ch = (char) asciiValue; if (ch == ':') { if (capturingEmoji) { if (emojiStrBuilder.length() <= 0) { emojiStrBuilder = new StringBuilder(); continue; } String emoji = emojiStrBuilder.toString(); EmojiEntity existingEmoji = emojis.remove(emoji); int occurrences; if (existingEmoji == null) { occurrences = 1; } else { occurrences = existingEmoji.getOccurrences() + 1; } emojis.put(emoji, new EmojiEntity(fatMessage.getUser().getMentionName(), roomName, fatMessage.getMessage().getDate(), emoji, occurrences, fatMessage.getUser().isBot())); emojiStrBuilder = new StringBuilder(); } capturingEmoji = !capturingEmoji; continue; } if (capturingEmoji) { if (BLACKLISTED_CHARS.contains(ch)) { capturingEmoji = !capturingEmoji; emojiStrBuilder = new StringBuilder(); continue; } emojiStrBuilder.append(ch); } } LOG.debug("Extracted {} emojis", emojis.size()); return Lists.newArrayList(emojis.values()); }  @Override void prepare(ChatAlyticsConfig config, @SuppressWarnings("rawtypes") Map conf,
                        TopologyContext context); @Override void execute(Tuple input, BasicOutputCollector collector); @Override void declareOutputFields(OutputFieldsDeclarer fields); @Override void cleanup(); static final String BOLT_ID; }
@Test public void testGetEmojisFromMessage() { Message message = new Message(mentionTime, "randomFrom", "randomUserId", String.format("test message with :%s:", emoji), "randomRoomId", MessageType.MESSAGE); FatMessage fatMessage = new FatMessage(message, user, room); List<EmojiEntity> emojis = underTest.getEmojisFromMessage(fatMessage); assertEquals(1, emojis.size()); EmojiEntity firstEmoji = emojis.get(0); assertEquals(user.getMentionName(), firstEmoji.getUsername()); assertEquals(room.getName(), firstEmoji.getRoomName()); assertEquals(mentionTime, firstEmoji.getMentionTime()); assertEquals(emoji, firstEmoji.getValue()); assertEquals(1, firstEmoji.getOccurrences()); }
@Test public void testGetEmojisFromMessage_noRoom() { Message message = new Message(mentionTime, "randomFrom", "randomUserId", String.format("test message with :%s:", emoji), "randomRoomId", MessageType.MESSAGE); FatMessage fatMessage = new FatMessage(message, user, null); List<EmojiEntity> emojis = underTest.getEmojisFromMessage(fatMessage); assertEquals(1, emojis.size()); EmojiEntity firstEmoji = emojis.get(0); assertEquals(user.getMentionName(), firstEmoji.getUsername()); assertNull(firstEmoji.getRoomName()); assertEquals(mentionTime, firstEmoji.getMentionTime()); assertEquals(emoji, firstEmoji.getValue()); assertEquals(1, firstEmoji.getOccurrences()); }
@Test public void testGetEmojisFromMessage_nullMessage() { Message message = new Message(mentionTime, "randomFrom", "randomUserId", null, "randomRoomId", MessageType.MESSAGE); FatMessage fatMessage = new FatMessage(message, user, room); List<EmojiEntity> emojis = underTest.getEmojisFromMessage(fatMessage); assertTrue(emojis.isEmpty()); }
@Test public void testGetEmojisFromMessage_withMultipleOccurrences() { Message message = new Message(mentionTime, "randomFrom", "randomUserId", String.format("test message with :%s::%s: test :%s:", emoji, emoji, emoji), "randomRoomId", MessageType.MESSAGE); FatMessage fatMessage = new FatMessage(message, user, room); List<EmojiEntity> emojis = underTest.getEmojisFromMessage(fatMessage); assertEquals(1, emojis.size()); EmojiEntity firstEmoji = emojis.get(0); assertEquals(user.getMentionName(), firstEmoji.getUsername()); assertEquals(room.getName(), firstEmoji.getRoomName()); assertEquals(mentionTime, firstEmoji.getMentionTime()); assertEquals(emoji, firstEmoji.getValue()); assertEquals(3, firstEmoji.getOccurrences()); }
@Test public void testGetEmojisFromMessage_withDanglingColons() { Message message = new Message(mentionTime, "randomFrom", "randomUserId", String.format("test http: + "test :%s:", emoji, emoji, emoji), "randomRoomId", MessageType.MESSAGE); FatMessage fatMessage = new FatMessage(message, user, room); List<EmojiEntity> emojis = underTest.getEmojisFromMessage(fatMessage); assertEquals(1, emojis.size()); EmojiEntity firstEmoji = emojis.get(0); assertEquals(user.getMentionName(), firstEmoji.getUsername()); assertEquals(room.getName(), firstEmoji.getRoomName()); assertEquals(mentionTime, firstEmoji.getMentionTime()); assertEquals(emoji, firstEmoji.getValue()); assertEquals(3, firstEmoji.getOccurrences()); }
@Test public void testGetEmojisFromMessage_withSpaces() { Message message = new Message(mentionTime, "randomFrom", "randomUserId", String.format("::%s: test :%s:", emoji, emoji), "randomRoomId", MessageType.MESSAGE); FatMessage fatMessage = new FatMessage(message, user, room); List<EmojiEntity> emojis = underTest.getEmojisFromMessage(fatMessage); assertEquals(1, emojis.size()); EmojiEntity firstEmoji = emojis.get(0); assertEquals(user.getMentionName(), firstEmoji.getUsername()); assertEquals(room.getName(), firstEmoji.getRoomName()); assertEquals(mentionTime, firstEmoji.getMentionTime()); assertEquals(emoji, firstEmoji.getValue()); assertEquals(2, firstEmoji.getOccurrences()); }
@Test public void testGetEmojisFromMessage_withJSON() { Message message = new Message(mentionTime, "randomFrom", "randomUserId", "{'test':true,'value':'hello'}", "randomRoomId", MessageType.MESSAGE); FatMessage fatMessage = new FatMessage(message, user, room); List<EmojiEntity> emojis = underTest.getEmojisFromMessage(fatMessage); assertEquals(0, emojis.size()); }