Skip to content

Commit

Permalink
Require context for Reaction
Browse files Browse the repository at this point in the history
  • Loading branch information
SMadani committed Sep 18, 2024
1 parent 146abe8 commit 28c4c42
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import com.vonage.client.messages.MessageType;
import java.util.Objects;
import java.util.UUID;

/**
* {@link com.vonage.client.messages.Channel#WHATSAPP}, {@link MessageType#REACTION} request.
Expand All @@ -30,6 +31,9 @@ public final class WhatsappReactionRequest extends WhatsappRequest {
WhatsappReactionRequest(Builder builder) {
super(builder, MessageType.REACTION);
reaction = Objects.requireNonNull(builder.reaction, "Reaction is required.");
if (getContext() == null) {
throw new IllegalStateException("Context message ID is required.");
}
}

@JsonProperty("reaction")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ protected abstract static class Builder<M extends WhatsappRequest, B extends Bui
UUID messageUuid;

/**
* (REQUIRED for replies and reaction messages)
* An optional context used for quoting/replying to a specific message in a conversation. When used,
* the WhatsApp UI will display the new message along with a contextual bubble that displays the
* quoted/replied to message's content.<br>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public void testSendWhatsappSuccess() throws Exception {
assertResponse(WhatsappVideoRequest.builder().url(VIDEO));
assertResponse(WhatsappFileRequest.builder().url(FILE));
assertResponse(WhatsappStickerRequest.builder().url(STICKER));
assertResponse(WhatsappReactionRequest.builder().unreact());
assertResponse(WhatsappReactionRequest.builder().unreact().contextMessageId(MESSAGE_ID));
assertResponse(WhatsappLocationRequest.builder().latitude(40.34772).longitude(-74.18847));
assertResponse(WhatsappSingleProductRequest.builder().catalogId("c1d").productRetailerId("p1d"));
assertResponse(WhatsappMultiProductRequest.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,36 +21,46 @@
import java.util.UUID;

public class WhatsappReactionRequestTest {
private final String messageId = UUID.randomUUID().toString();

@Test
public void testSerializeReact() {
String emoji = "😀";
String from = "317900000002", to = "447900000001";
String json = WhatsappReactionRequest.builder()
.from(from).to(to).reaction(emoji).build().toJson();
.from(from).to(to).reaction(emoji).contextMessageId(messageId).build().toJson();
assertTrue(json.contains("\"reaction\":{\"action\":\"react\",\"emoji\":\""+emoji+"\"}"));
assertTrue(json.contains("\"message_type\":\"reaction\""));
assertTrue(json.contains("\"channel\":\"whatsapp\""));
assertTrue(json.contains("\"from\":\""+from+"\""));
assertTrue(json.contains("\"to\":\""+to+"\""));
assertTrue(json.contains("\"context\":{\"message_uuid\":\""+messageId+"\"}"));
}

@Test
public void testSerializeUnreact() {
String from = "317900000002", to = "447900000001";
String json = WhatsappReactionRequest.builder()
.from(from).to(to).unreact().build().toJson();
.from(from).to(to).unreact().contextMessageId(messageId).build().toJson();
assertTrue(json.contains("\"reaction\":{\"action\":\"unreact\"}"));
assertTrue(json.contains("\"message_type\":\"reaction\""));
assertTrue(json.contains("\"channel\":\"whatsapp\""));
assertTrue(json.contains("\"from\":\""+from+"\""));
assertTrue(json.contains("\"to\":\""+to+"\""));
assertTrue(json.contains("\"context\":{\"message_uuid\":\""+messageId+"\"}"));
}

@Test
public void testConstructNoAction() {
assertThrows(NullPointerException.class, () -> WhatsappReactionRequest.builder()
.from("447900000001").to("317900000002").build()
.contextMessageId(messageId).from("447900000001").to("317900000002").build()
);
}

@Test
public void testConstructNoContext() {
assertThrows(IllegalStateException.class, () -> WhatsappReactionRequest.builder()
.unreact().from("447900000001").to("317900000002").build()
);
}
}

0 comments on commit 28c4c42

Please sign in to comment.