Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ECO-5193] Support message edits and deletes #1059

Merged
merged 10 commits into from
Feb 7, 2025
Prev Previous commit
Next Next commit
[ECO-5193][TM*] Updated ChaneMessagesTest.java, implemented message d…
…elete test
  • Loading branch information
sacOO7 committed Jan 21, 2025

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit e8f3f87a4b5ddc4fd1be25483357986e01de9ec4
85 changes: 85 additions & 0 deletions lib/src/test/java/io/ably/lib/chat/ChatMessagesTest.java
Original file line number Diff line number Diff line change
@@ -229,6 +229,91 @@ public void test_room_message_is_updated() {
*/
@Test
public void test_room_message_is_deleted() {
String roomId = "1234";
String channelName = roomId + "::$chat::$chatMessages";
AblyRealtime ablyClient1 = null;
AblyRealtime ablyClient2 = null;
try {
ClientOptions opts1 = createOptions(testVars.keys[7].keyStr);
opts1.clientId = "clientId1";
ablyClient1 = new AblyRealtime(opts1);

ClientOptions opts2 = createOptions(testVars.keys[7].keyStr);
opts2.clientId = "clientId2";
ablyClient2 = new AblyRealtime(opts2);

ChatRoom room = new ChatRoom(roomId, ablyClient1);

// Create a channel and attach with client1
final Channel channel1 = ablyClient1.channels.get(channelName);
channel1.attach();
(new Helpers.ChannelWaiter(channel1)).waitFor(ChannelState.attached);

// Subscribe to messages with client2
final Channel channel2 = ablyClient2.channels.get(channelName);
channel2.attach();
(new Helpers.ChannelWaiter(channel2)).waitFor(ChannelState.attached);

List<Message> receivedMsg = new ArrayList<>();
channel2.subscribe(receivedMsg::add);

// Send message to room
ChatRoom.SendMessageParams params = new ChatRoom.SendMessageParams();
params.text = "hello there";
JsonObject sendMessageResult = (JsonObject) room.sendMessage(params);
String originalSerial = sendMessageResult.get("serial").getAsString();
String originalCreatedAt = sendMessageResult.get("createdAt").getAsString();

// Wait for the message to be received
Exception err = new Helpers.ConditionalWaiter().wait(() -> !receivedMsg.isEmpty(), 10_000);
Assert.assertNull(err);

// Delete the message
ChatRoom.DeleteMessageParams deleteParams = new ChatRoom.DeleteMessageParams();
deleteParams.description = "message deleted by clientId1";
Map<String, String> deleteMetadata = new HashMap<>();
deleteMetadata.put("foo", "bar");
deleteMetadata.put("naruto", "hero");
deleteParams.metadata = deleteMetadata;

JsonObject deleteMessageResult = (JsonObject) room.deleteMessage(originalSerial, deleteParams);
String deleteResultVersion = deleteMessageResult.get("version").getAsString();
String deleteResultTimestamp = deleteMessageResult.get("timestamp").getAsString();

// Wait for the deleted message to be received
err = new Helpers.ConditionalWaiter().wait(() -> receivedMsg.size() == 2, 10_000);
Assert.assertNull(err);

// Verify the deleted message
Message deletedMessage = receivedMsg.get(1);

Assert.assertEquals(MessageAction.MESSAGE_DELETE, deletedMessage.action);

Assert.assertFalse("Message ID should not be empty", deletedMessage.id.isEmpty());
Assert.assertEquals("chat.message", deletedMessage.name);
Assert.assertEquals("clientId1", deletedMessage.clientId);

Assert.assertEquals(originalSerial, deletedMessage.serial);
Assert.assertEquals(originalCreatedAt, deletedMessage.createdAt.toString());

Assert.assertEquals(deleteResultVersion, deletedMessage.version);
Assert.assertEquals(deleteResultTimestamp, String.valueOf(deletedMessage.timestamp));

// deletedMessage contains `operation` with fields as clientId, reason
Message.Operation operation = deletedMessage.operation;
Assert.assertEquals("clientId1", operation.clientId);
Assert.assertEquals("message deleted by clientId1", operation.description);
// assert on metadata
Assert.assertEquals(2, operation.metadata.size());
Assert.assertEquals("bar", operation.metadata.get("foo"));
Assert.assertEquals("hero", operation.metadata.get("naruto"));

} catch (Exception e) {
e.printStackTrace();
Assert.fail("Unexpected exception instantiating library");
} finally {
if (ablyClient1 != null) ablyClient1.close();
if (ablyClient2 != null) ablyClient2.close();
}
}
}
Loading