Skip to content

Commit

Permalink
Add support for LTO templates (#262)
Browse files Browse the repository at this point in the history
Add unpause method support
  • Loading branch information
alyona007 authored Sep 23, 2024
1 parent b742d17 commit aa7ab0b
Show file tree
Hide file tree
Showing 12 changed files with 443 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,27 @@
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;

import java.util.*;

public enum MessageComponentType {

HEADER("header"),
BODY("body"),
FOOTER("footer"),
BUTTON("button"),
CARD("card"),
CAROUSEL("carousel");
CAROUSEL("carousel"),
LIMITED_TIME_OFFER("limited_time_offer");

private static final Map<String, MessageComponentType> TYPE_MAP;

static {
Map<String, MessageComponentType> map = new HashMap<>();
for (MessageComponentType componentType : MessageComponentType.values()) {
map.put(componentType.getType().toLowerCase(), componentType);
}
TYPE_MAP = Collections.unmodifiableMap(map);
}

private final String type;

Expand All @@ -21,13 +33,8 @@ public enum MessageComponentType {

@JsonCreator
public static MessageComponentType forValue(String value) {
for (MessageComponentType componentType: MessageComponentType.values()) {
if (componentType.getType().equals(value)) {
return componentType;
}
}

return null;
Objects.requireNonNull(value, "Value cannot be null");
return TYPE_MAP.get(value.toLowerCase(Locale.ROOT));
}

@JsonValue
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.messagebird.objects.conversations;

import com.fasterxml.jackson.annotation.JsonProperty;
import org.apache.commons.lang3.StringUtils;

public class MessageParam {

private TemplateMediaType type;
Expand All @@ -10,6 +13,8 @@ public class MessageParam {
private Media document;
private Media image;
private Media video;
@JsonProperty("expiration_time")
private String expirationTime;

public TemplateMediaType getType() {
return type;
Expand All @@ -24,6 +29,9 @@ public String getText() {
}

public void setText(String text) {
if (StringUtils.isBlank(text)) {
throw new IllegalArgumentException("Text cannot be null or empty");
}
this.text = text;
}

Expand All @@ -48,6 +56,9 @@ public String getDateTime() {
}

public void setDateTime(String dateTime) {
if (StringUtils.isBlank(dateTime)) {
throw new IllegalArgumentException("dateTime cannot be null or empty");
}
this.dateTime = dateTime;
}

Expand All @@ -71,17 +82,30 @@ public void setImage(Media image) {

public void setVideo(Media video) { this.video = video; }

public String getExpirationTime() {
return expirationTime;
}

public void setExpirationTime(String expirationTime) {
if (StringUtils.isBlank(expirationTime)) {
throw new IllegalArgumentException("expirationTime cannot be null or empty");
}
this.expirationTime = expirationTime;
}

@Override
public String toString() {
return "MessageParam{" +
"type=" + type + '\'' +
", text='" + text + '\'' +
", payload='" + payload + '\'' +
", currency=" + currency + '\'' +
", dateTime='" + dateTime + '\'' +
", document=" + document + '\'' +
", image=" + image + '\'' +
", video=" + video +
'}';
StringBuilder sb = new StringBuilder("MessageParam{");
sb.append("type=").append(type)
.append(", text='").append(text).append('\'')
.append(", payload='").append(payload).append('\'')
.append(", currency=").append(currency)
.append(", dateTime='").append(dateTime).append('\'')
.append(", document=").append(document)
.append(", image=").append(image)
.append(", video=").append(video)
.append(", expirationTime='").append(expirationTime).append('\'')
.append('}');
return sb.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import java.util.Map;
import java.util.HashMap;
import java.util.Collections;

public enum TemplateMediaType {

Expand All @@ -11,7 +14,19 @@ public enum TemplateMediaType {
TEXT("text"),
CURRENCY("currency"),
DATETIME("date_time"),
PAYLOAD("payload");
PAYLOAD("payload"),
EXPIRATION_TIME("expiration_time");

private static final Map<String, TemplateMediaType> TYPE_MAP;

static {
Map<String, TemplateMediaType> map = new HashMap<>();
for (TemplateMediaType templateMediaType : TemplateMediaType.values()) {
map.put(templateMediaType.getType().toLowerCase(), templateMediaType);
}
TYPE_MAP = Collections.unmodifiableMap(map);
}


private final String type;

Expand All @@ -21,13 +36,10 @@ public enum TemplateMediaType {

@JsonCreator
public static TemplateMediaType forValue(String value) {
for (TemplateMediaType templateMediaType: TemplateMediaType.values()) {
if (templateMediaType.getType().equals(value)) {
return templateMediaType;
}
if (value == null) {
throw new IllegalArgumentException("Value cannot be null");
}

return null;
return TYPE_MAP.get(value.toLowerCase());
}

@JsonValue
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.messagebird.objects.integrations;

import com.fasterxml.jackson.annotation.JsonProperty;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

Expand All @@ -20,6 +21,8 @@ public class HSMComponent {
@JsonProperty("code_expiration_minutes")
private Integer codeExpirationMinutes;
private List<HSMComponentButton> buttons;
@JsonProperty("has_expiration")
private Boolean hasExpiration;

private List<HSMComponentCard> cards;

Expand All @@ -46,6 +49,9 @@ public String getText() {
}

public void setText(String text) {
if (StringUtils.isBlank(text)) {
throw new IllegalArgumentException("Text cannot be null or empty");
}
this.text = text;
}

Expand Down Expand Up @@ -88,15 +94,29 @@ public Integer getCodeExpirationMinutes() {
public void setCodeExpirationMinutes(Integer codeExpirationMinutes) {
this.codeExpirationMinutes = codeExpirationMinutes;
}

public Boolean getHasExpiration() {
return hasExpiration;
}

public void setHasExpiration(Boolean hasExpiration) {
this.hasExpiration = hasExpiration;
}

@Override
public String toString() {
return "HSMComponent{" +
"type='" + type + '\'' +
", format='" + format + '\'' +
", text='" + text + '\'' +
", buttons=" + buttons +
", example=" + example +
'}';
StringBuilder sb = new StringBuilder("HSMComponent{");
sb.append("type=").append(type)
.append(", format=").append(format)
.append(", text='").append(text).append('\'')
.append(", addSecurityRecommendation=").append(addSecurityRecommendation)
.append(", codeExpirationMinutes=").append(codeExpirationMinutes)
.append(", buttons=").append(buttons)
.append(", hasExpiration=").append(hasExpiration)
.append(", cards=").append(cards)
.append(", example=").append(example)
.append('}');
return sb.toString();
}

/**
Expand All @@ -105,8 +125,12 @@ public String toString() {
* @throws IllegalArgumentException Occurs when validation is not passed.
*/
public void validateComponent() throws IllegalArgumentException {
this.validateButtons();
this.validateComponentExample();
try {
this.validateButtons();
this.validateComponentExample();
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException("Component validation failed: " + e.getMessage(), e);
}
}

/**
Expand Down Expand Up @@ -153,9 +177,7 @@ private void validateComponentExample() throws IllegalArgumentException {
* @throws IllegalArgumentException Occurs when type is not {@code HEADER} and format is not {@code TEXT}.
*/
private void checkHeaderText() throws IllegalArgumentException {
if (!(type.equals(HSMComponentType.HEADER)
&& format.equals(HSMComponentFormat.TEXT))
) {
if (!(HSMComponentType.HEADER.equals(type) && HSMComponentFormat.TEXT.equals(format))) {
throw new IllegalArgumentException("\"header_text\" is available for only HEADER type and TEXT format.");
}
}
Expand All @@ -166,9 +188,9 @@ private void checkHeaderText() throws IllegalArgumentException {
* @throws IllegalArgumentException Occurs when type is not {@code HEADER} and format is not {@code IMAGE}.
*/
private void checkHeaderUrl() throws IllegalArgumentException {
if (!(type.equals(HSMComponentType.HEADER) &&
(format.equals(HSMComponentFormat.IMAGE) || format.equals(HSMComponentFormat.VIDEO) || format.equals(HSMComponentFormat.DOCUMENT)))) {
throw new IllegalArgumentException("\"header_url\" is available for only HEADER type and IMAGE, VIDEO and DOCUMENT format.");
if (!(HSMComponentType.HEADER.equals(type) &&
(HSMComponentFormat.IMAGE.equals(format) || HSMComponentFormat.VIDEO.equals(format) || HSMComponentFormat.DOCUMENT.equals(format)))) {
throw new IllegalArgumentException("\"header_url\" is available for only HEADER type and IMAGE, VIDEO, or DOCUMENT formats.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import java.util.Map;
import java.util.HashMap;
import java.util.Collections;
import java.util.Locale;
import java.util.Objects;

/**
* An enum for HSMComponentType
Expand All @@ -13,7 +18,18 @@ public enum HSMComponentType {
HEADER("HEADER"),
FOOTER("FOOTER"),
BUTTONS("BUTTONS"),
CAROUSEL("CAROUSEL");
CAROUSEL("CAROUSEL"),
LIMITED_TIME_OFFER("LIMITED_TIME_OFFER");

private static final Map<String, HSMComponentType> TYPE_MAP;

static {
Map<String, HSMComponentType> map = new HashMap<>();
for (HSMComponentType hsmComponentType : HSMComponentType.values()) {
map.put(hsmComponentType.getType().toLowerCase(), hsmComponentType);
}
TYPE_MAP = Collections.unmodifiableMap(map);
}

private final String type;

Expand All @@ -23,13 +39,8 @@ public enum HSMComponentType {

@JsonCreator
public static HSMComponentType forValue(String value) {
for (HSMComponentType hsmComponentType : HSMComponentType.values()) {
if (hsmComponentType.getType().equals(value)) {
return hsmComponentType;
}
}

return null;
Objects.requireNonNull(value, "Value cannot be null");
return TYPE_MAP.get(value.toLowerCase(Locale.ROOT));
}

@JsonValue
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.messagebird.objects.conversations;

import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

public class MessageComponentTypeTest {
@Test
public void testMessageComponentTypeForValueValid() {
assertEquals(MessageComponentType.HEADER, MessageComponentType.forValue("header"));
assertEquals(MessageComponentType.BUTTON, MessageComponentType.forValue("button"));
}

@Test(expected = NullPointerException.class)
public void testMessageComponentTypeForValueNull() {
MessageComponentType.forValue(null);
}

@Test
public void testMessageComponentTypeForValueInvalid() {
assertNull(MessageComponentType.forValue("invalid_type"));
}

@Test
public void testMessageComponentTypeToString() {
assertEquals("header", MessageComponentType.HEADER.toString());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.messagebird.objects.conversations;

import org.junit.Test;
import static org.junit.Assert.assertEquals;

public class MessageParamTest {
@Test
public void testMessageParamToString() {
MessageParam param = new MessageParam();
param.setType(TemplateMediaType.IMAGE);
param.setText("Sample text");
param.setPayload("Sample payload");

String expected = "MessageParam{type=image, text='Sample text', payload='Sample payload', currency=null, dateTime='null', document=null, image=null, video=null, expirationTime='null'}";
assertEquals(expected, param.toString());
}

@Test(expected = IllegalArgumentException.class)
public void testMessageParamSetTextInvalid() {
MessageParam param = new MessageParam();
param.setText("");
}

@Test
public void testMessageParamSetTextValid() {
MessageParam param = new MessageParam();
param.setText("Valid text");
assertEquals("Valid text", param.getText());
}
}
Loading

0 comments on commit aa7ab0b

Please sign in to comment.