From 3616398f66853b8b476c7e6ac4a722fac2e56c4c Mon Sep 17 00:00:00 2001 From: sacOO7 Date: Fri, 17 Jan 2025 18:08:57 +0530 Subject: [PATCH] [ECO-5193][TM*] Added test helpers for Chat message edit, update and delete 1. Added ChatAPIClient class that sends authorized request for given url 2. Added ChatRoom class that extends ChatAPIClients, provides methods to send, update and delete the given messsage --- .../java/io/ably/lib/chat/ChatAPIClient.java | 30 +++++ .../test/java/io/ably/lib/chat/ChatRoom.java | 104 ++++++++++++++++++ 2 files changed, 134 insertions(+) create mode 100644 lib/src/test/java/io/ably/lib/chat/ChatAPIClient.java create mode 100644 lib/src/test/java/io/ably/lib/chat/ChatRoom.java diff --git a/lib/src/test/java/io/ably/lib/chat/ChatAPIClient.java b/lib/src/test/java/io/ably/lib/chat/ChatAPIClient.java new file mode 100644 index 000000000..1ba9f34f9 --- /dev/null +++ b/lib/src/test/java/io/ably/lib/chat/ChatAPIClient.java @@ -0,0 +1,30 @@ +package io.ably.lib.chat; + +import com.google.gson.JsonElement; +import io.ably.lib.http.HttpCore; +import io.ably.lib.http.HttpUtils; +import io.ably.lib.rest.AblyRest; +import io.ably.lib.types.AblyException; +import io.ably.lib.types.HttpPaginatedResponse; +import io.ably.lib.types.Param; +import java.util.Arrays; +import java.util.Optional; + +public class ChatAPIClient { + + private final int API_PROTOCOL_VERSION = 3; + private final String PROTOCOL_VERSION_PARAM_NAME = "v"; + private final Param apiProtocolParam = new Param(PROTOCOL_VERSION_PARAM_NAME, API_PROTOCOL_VERSION); + + AblyRest ablyRest; + + ChatAPIClient(AblyRest ablyRest) { + this.ablyRest = ablyRest; + } + + protected Optional makeAuthorizedRequest(String url, String method, JsonElement body) throws AblyException { + HttpCore.RequestBody httpRequestBody = HttpUtils.requestBodyFromGson(body, ablyRest.options.useBinaryProtocol); + HttpPaginatedResponse response = ablyRest.request(method, url, new Param[]{apiProtocolParam}, httpRequestBody, null); + return Arrays.stream(response.items()).findFirst(); + } +} diff --git a/lib/src/test/java/io/ably/lib/chat/ChatRoom.java b/lib/src/test/java/io/ably/lib/chat/ChatRoom.java new file mode 100644 index 000000000..a3cbcb7c2 --- /dev/null +++ b/lib/src/test/java/io/ably/lib/chat/ChatRoom.java @@ -0,0 +1,104 @@ +package io.ably.lib.chat; + +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import io.ably.lib.rest.AblyRest; + +import java.util.Map; + +public class ChatRoom extends ChatAPIClient{ + private final String roomId; + + private ChatRoom(AblyRest ablyRest, String roomId) { + super(ablyRest); + this.roomId = roomId; + } + + public JsonElement sendMessage(SendMessageParams params) throws Exception { + return makeAuthorizedRequest("/chat/v2/rooms/" + roomId + "/messages", "POST", params.toJsonObject()) + .orElseThrow(() -> new Exception("Failed to send message")); + } + + public JsonElement updateMessage(String serial, UpdateMessageParams params) throws Exception { + return makeAuthorizedRequest("/chat/v2/rooms/" + roomId + "/messages/" + serial, "PUT", params.toJsonObject()) + .orElseThrow(() -> new Exception("Failed to update message")); + } + + public JsonElement deleteMessage(String serial, DeleteMessageParams params) throws Exception { + return makeAuthorizedRequest("/chat/v2/rooms/" + roomId + "/messages/" + serial + "/delete", "POST", params.toJsonObject()) + .orElseThrow(() -> new Exception("Failed to delete message")); + } + + public static class SendMessageParams { + private final String text; + private final JsonElement metadata; + private final Map headers; + + public SendMessageParams(String text, JsonElement metadata, Map headers) { + this.text = text; + this.metadata = metadata; + this.headers = headers; + } + + private JsonObject toJsonObject() { + JsonObject jsonObject = new JsonObject(); + jsonObject.addProperty("text", text); + // (CHA-M3b) + if (headers != null) { + JsonObject headersJson = new JsonObject(); + headers.forEach(headersJson::addProperty); + jsonObject.add("headers", headersJson); + } + // (CHA-M3b) + if (metadata != null) { + jsonObject.add("metadata", metadata); + } + return jsonObject; + } + } + + public static class UpdateMessageParams { + private final SendMessageParams message; + private final String description; + private final JsonElement metadata; + + public UpdateMessageParams(SendMessageParams message, String description, JsonElement metadata) { + this.message = message; + this.description = description; + this.metadata = metadata; + } + + private JsonObject toJsonObject() { + JsonObject jsonObject = new JsonObject(); + jsonObject.add("message", message.toJsonObject()); + if (description != null) { + jsonObject.addProperty("description", description); + } + if (metadata != null) { + jsonObject.add("metadata", metadata); + } + return jsonObject; + } + } + + public static class DeleteMessageParams { + private final String description; + private final JsonElement metadata; + + public DeleteMessageParams(String description, JsonElement metadata) { + this.description = description; + this.metadata = metadata; + } + + private JsonObject toJsonObject() { + JsonObject jsonObject = new JsonObject(); + if (description != null) { + jsonObject.addProperty("description", description); + } + if (metadata != null) { + jsonObject.add("metadata", metadata); + } + return jsonObject; + } + } +}