JsonSlackDAO extends AbstractJSONChatApiDAO { @Override public Map<String, String> getEmojis() { WebResource emojiResource = resource.path("emoji.list"); String jsonStr = getJsonResultWithRetries(emojiResource, apiRetries); JsonNode node; try { node = objMapper.readTree(jsonStr); } catch (IOException e) { LOG.error("Can't read emoji result from chat API", e); return ImmutableMap.of(); } TypeFactory typeFactory = objMapper.getTypeFactory(); MapLikeType type = typeFactory.constructMapLikeType(Map.class, String.class, String.class); try { JsonNode emojiJson = node.get("emoji"); if (emojiJson == null) { return ImmutableMap.of(); } Map<String, String> apiResponse = objMapper.readValue(emojiJson.toString(), type); for (Map.Entry<String, String> emojiEntry : apiResponse.entrySet()) { String value = emojiEntry.getValue(); if (value.startsWith("alias:")) { String alias = value.substring("alias:".length(), value.length()); apiResponse.put(emojiEntry.getKey(), apiResponse.get(alias)); } } return apiResponse; } catch (IOException e) { LOG.error("Can't deserialize emojis"); return ImmutableMap.of(); } } JsonSlackDAO(ChatAlyticsConfig config, Client client); @Override Map<String, Room> getRooms(); @Override Map<String, User> getUsers(); @Override Map<String, User> getUsersForRoom(Room room); URI getRealtimeWebSocketURI(); @Override List<Message> getMessages(DateTime start, DateTime end, Room room); @Override Map<String, String> getEmojis();  }
@Test public void testGetEmojis() throws Exception { WebResource emojiResource = mock(WebResource.class); when(mockResource.path("emoji.list")).thenReturn(emojiResource); URI emojiURI = Resources.getResource("slack_api_responses/emoji.list.txt").toURI(); Path emojiPath = Paths.get(emojiURI); String emojiResponseStr = new String(Files.readAllBytes(emojiPath)); doReturn(emojiResponseStr).when(underTest).getJsonResultWithRetries(emojiResource, apiRetries); Map<String, String> result = underTest.getEmojis(); assertEquals(3, result.size()); assertTrue(result.containsKey("bowtie")); assertEquals("https: assertTrue(result.containsKey("squirrel")); assertEquals("https: assertTrue(result.containsKey("shipit")); assertEquals("https: }
@Test public void testGetEmojis_malformedJson() throws Exception { WebResource emojiResource = mock(WebResource.class); when(mockResource.path("emoji.list")).thenReturn(emojiResource); String emojiResponseStr = "{ badJSON"; doReturn(emojiResponseStr).when(underTest).getJsonResultWithRetries(emojiResource, apiRetries); Map<String, String> result = underTest.getEmojis(); assertTrue(result.isEmpty()); }
@Test public void testGetEmojis_emptyEmojiList() throws Exception { WebResource emojiResource = mock(WebResource.class); when(mockResource.path("emoji.list")).thenReturn(emojiResource); String emojiResponseStr = "{}"; doReturn(emojiResponseStr).when(underTest).getJsonResultWithRetries(emojiResource, apiRetries); Map<String, String> result = underTest.getEmojis(); assertTrue(result.isEmpty()); }
@Test public void testGetEmojis_badEmojiList() throws Exception { WebResource emojiResource = mock(WebResource.class); when(mockResource.path("emoji.list")).thenReturn(emojiResource); String emojiResponseStr = "{\"emoji\": \"BAD JSON\"}"; doReturn(emojiResponseStr).when(underTest).getJsonResultWithRetries(emojiResource, apiRetries); Map<String, String> result = underTest.getEmojis(); assertTrue(result.isEmpty()); }