Skip to content

Commit

Permalink
[ECO-5193][TM*] Added test helpers for Chat message edit, update and …
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
sacOO7 committed Jan 17, 2025
1 parent e8fe70b commit 3616398
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 0 deletions.
30 changes: 30 additions & 0 deletions lib/src/test/java/io/ably/lib/chat/ChatAPIClient.java
Original file line number Diff line number Diff line change
@@ -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<JsonElement> 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();
}
}
104 changes: 104 additions & 0 deletions lib/src/test/java/io/ably/lib/chat/ChatRoom.java
Original file line number Diff line number Diff line change
@@ -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<String, String> headers;

public SendMessageParams(String text, JsonElement metadata, Map<String, String> 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;
}
}
}

0 comments on commit 3616398

Please sign in to comment.