JsonSlackDAO extends AbstractJSONChatApiDAO { public URI getRealtimeWebSocketURI() { WebResource rtmResource = resource.path("rtm.start"); String jsonStr = getJsonResultWithRetries(rtmResource, apiRetries); JsonNode tree; try { tree = objMapper.readTree(jsonStr); } catch (IOException e) { throw new RuntimeException("Unable to parse realtime resource response", e); } boolean ok = tree.get("ok").asBoolean(); if (!ok) { throw new NotConnectedException("Failed to connect to Slack API. Reason: " + tree.get("error").asText()); } String webSocketUrl = tree.get("url").asText(); return URI.create(webSocketUrl); } 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 testGetRealtimeWebSocketURI() throws Exception { WebResource mockRtmResrc = mock(WebResource.class); when(mockResource.path("rtm.start")).thenReturn(mockRtmResrc); String rtmResponseStr = "{\"ok\": true, \"url\":\"wss:\\/\\/ms9.slack-msgs.com\\/websocket\\/7I5yBpcvk\"}"; doReturn(rtmResponseStr).when(underTest).getJsonResultWithRetries(mockRtmResrc, apiRetries); assertEquals(URI.create("wss: underTest.getRealtimeWebSocketURI()); }
@Test(expected = IOException.class) public void testGetRealtimeWebSocketURI_withoutURLInResponse() throws Exception { WebResource mockRtmResrc = mock(WebResource.class); when(mockResource.path("rtm.start")).thenReturn(mockRtmResrc); String rtmResponseStr = "{malformedJSON"; doReturn(rtmResponseStr).when(underTest).getJsonResultWithRetries(mockRtmResrc, apiRetries); try { underTest.getRealtimeWebSocketURI(); } catch (Exception e) { throw (Exception) e.getCause(); } }
@Test(expected = NotConnectedException.class) public void testGetRealtimeWebSocketURI_withBadResponse() throws Exception { WebResource mockRtmResrc = mock(WebResource.class); when(mockResource.path("rtm.start")).thenReturn(mockRtmResrc); when(mockRtmResrc.queryParam(anyString(), anyString())).thenReturn(mockRtmResrc); Builder builder = mock(Builder.class); String errorReason = "broken"; String jsonResult = String.format("{\"ok\":false, \"error\":\"%s\"}", errorReason); when(builder.get(String.class)).thenReturn(jsonResult); when(mockRtmResrc.accept(MediaType.APPLICATION_JSON)).thenReturn(builder); underTest.getRealtimeWebSocketURI(); }