Skip to content

Commit

Permalink
Fix code format and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
seratch committed May 21, 2020
1 parent b8c0cee commit ac2a010
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 39 deletions.
45 changes: 45 additions & 0 deletions json-logs/samples/api/chat.postMessage.json
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,51 @@
"image_bytes": 12345
}
],
"call_id": "R00000000",
"api_decoration_available": false,
"call": {
"v1": {
"id": "R00000000",
"app_id": "A00000000",
"app_icon_urls": {
"image_32": "https://www.example.com/",
"image_36": "https://www.example.com/",
"image_48": "https://www.example.com/",
"image_64": "https://www.example.com/",
"image_72": "https://www.example.com/",
"image_96": "https://www.example.com/",
"image_128": "https://www.example.com/",
"image_192": "https://www.example.com/",
"image_512": "https://www.example.com/",
"image_1024": "https://www.example.com/",
"image_original": "https://www.example.com/"
},
"date_start": 12345,
"active_participants": [
{
"slack_id": "U00000000"
}
],
"all_participants": [
{
"slack_id": "U00000000"
}
],
"display_id": "",
"join_url": "https://www.example.com/",
"name": "",
"created_by": "U00000000",
"date_end": 12345,
"channels": [
"C00000000"
],
"is_dm_call": false,
"was_rejected": false,
"was_missed": false,
"was_accepted": false,
"has_ended": false
}
},
"fallback": "",
"image_url": "",
"image_width": 12345,
Expand Down
28 changes: 28 additions & 0 deletions json-logs/samples/api/rtm.start.json
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,7 @@
"team": "",
"user": "",
"username": "",
"parent_user_id": "",
"text": "",
"topic": "",
"root": {
Expand Down Expand Up @@ -713,6 +714,19 @@
"display_as_bot": false,
"bot_id": "",
"bot_link": "",
"bot_profile": {
"id": "",
"deleted": false,
"name": "",
"updated": 123,
"app_id": "",
"icons": {
"image_36": "",
"image_48": "",
"image_72": ""
},
"team_id": ""
},
"thread_ts": "",
"ts": "",
"icons": {
Expand Down Expand Up @@ -815,6 +829,7 @@
"team": "",
"user": "",
"username": "",
"parent_user_id": "",
"text": "",
"topic": "",
"root": {
Expand Down Expand Up @@ -860,6 +875,19 @@
"display_as_bot": false,
"bot_id": "",
"bot_link": "",
"bot_profile": {
"id": "",
"deleted": false,
"name": "",
"updated": 123,
"app_id": "",
"icons": {
"image_36": "",
"image_48": "",
"image_72": ""
},
"team_id": ""
},
"thread_ts": "",
"ts": "",
"icons": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,18 @@

import java.time.Instant;

/**
* https://api.slack.com/rtm#ping_and_pong
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class PingMessage implements RTMMessage {

public static final String TYPE_NAME = "ping";
public static final String TYPE_NAME = "ping";

private Long id;
private final String type = TYPE_NAME;
private Instant time;
private Long id;
private final String type = TYPE_NAME;
private Instant time;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,8 @@
import com.slack.api.model.event.MessageBotEvent;
import com.slack.api.model.event.PongEvent;
import com.slack.api.model.event.UserTypingEvent;
import com.slack.api.rtm.RTMClient;
import com.slack.api.rtm.RTMEventHandler;
import com.slack.api.rtm.RTMEventsDispatcher;
import com.slack.api.rtm.RTMEventsDispatcherFactory;
import com.slack.api.rtm.RTMMessageHandler;
import com.slack.api.rtm.message.Message;
import com.slack.api.rtm.message.PingMessage;
import com.slack.api.rtm.message.PresenceQuery;
import com.slack.api.rtm.message.PresenceSub;
import com.slack.api.rtm.message.Typing;
import com.slack.api.rtm.*;
import com.slack.api.rtm.message.*;
import config.Constants;
import config.SlackTestConfig;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -41,10 +33,7 @@
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;

Expand Down Expand Up @@ -314,7 +303,7 @@ public void handle(String message) {


@Test
public void givenRTMClient_whenPing_ensureReceivesPong() throws Exception {
public void ping_pong() throws Exception {

// given
SlackConfig config = new SlackConfig();
Expand All @@ -325,36 +314,41 @@ public void givenRTMClient_whenPing_ensureReceivesPong() throws Exception {
final Instant now = Instant.now();
final long pingId = now.toEpochMilli();

class PongReceived { PongEvent event = null; }
class PongReceived {
PongEvent event = null;
}
final PongReceived pongReceived = new PongReceived();

RTMEventsDispatcher dispatcher = RTMEventsDispatcherFactory.getInstance();
dispatcher.register(new RTMEventHandler<PongEvent>() {
@Override public void handle(PongEvent event) {
@Override
public void handle(PongEvent event) {
if (Objects.equals(event.getReplyTo(), pingId)) {
synchronized(pongReceived) {
pongReceived.event = event;
pongReceived.notifyAll();
}
pongReceived.event = event;
pongReceived.notifyAll();
}
}
});

try (RTMClient rtm = slack.rtmStart(classicAppBotToken)) {
rtm.connect();
rtm.addMessageHandler(dispatcher.toMessageHandler());
rtm.connect();

Thread.sleep(3000);

// when
rtm.sendMessage(PingMessage.builder().id(pingId).time(now).build().toJSONString());

// ensure
synchronized(pongReceived) {
pongReceived.wait(5000L);
long millis = 0L;
while (pongReceived.event == null && millis < 30_000L) {
Thread.sleep(1000L);
millis += 1000;
}
assertThat(pongReceived.event, notNullValue());
assertThat(pongReceived.event.getReplyTo(), equalTo(pingId));
assertThat(pongReceived.event.getTime(), equalTo(now));
}
assertThat(pongReceived.event, notNullValue());
assertThat(pongReceived.event.getReplyTo(), equalTo(pingId));
assertThat(pongReceived.event.getTime(), equalTo(now));

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@
* The pong event is sent in response to a 'ping' message previously sent. The id and
* other fields will match that of the ping message.
* <p>
* https://api.slack.com/rtm
* https://api.slack.com/rtm#ping_and_pong
*/
@Data
public class PongEvent implements Event
{
public static final String TYPE_NAME = "pong";
private final String type = TYPE_NAME;
public class PongEvent implements Event {
public static final String TYPE_NAME = "pong";
private final String type = TYPE_NAME;

private Long replyTo;
private Instant time;
}
private Long replyTo;
private Instant time;
}

0 comments on commit ac2a010

Please sign in to comment.