Skip to content

Commit aa7ab0b

Browse files
authored
Add support for LTO templates (#262)
Add unpause method support
1 parent b742d17 commit aa7ab0b

File tree

12 files changed

+443
-48
lines changed

12 files changed

+443
-48
lines changed

api/src/main/java/com/messagebird/objects/conversations/MessageComponentType.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,27 @@
33
import com.fasterxml.jackson.annotation.JsonCreator;
44
import com.fasterxml.jackson.annotation.JsonValue;
55

6+
import java.util.*;
7+
68
public enum MessageComponentType {
79

810
HEADER("header"),
911
BODY("body"),
1012
FOOTER("footer"),
1113
BUTTON("button"),
1214
CARD("card"),
13-
CAROUSEL("carousel");
15+
CAROUSEL("carousel"),
16+
LIMITED_TIME_OFFER("limited_time_offer");
17+
18+
private static final Map<String, MessageComponentType> TYPE_MAP;
1419

20+
static {
21+
Map<String, MessageComponentType> map = new HashMap<>();
22+
for (MessageComponentType componentType : MessageComponentType.values()) {
23+
map.put(componentType.getType().toLowerCase(), componentType);
24+
}
25+
TYPE_MAP = Collections.unmodifiableMap(map);
26+
}
1527

1628
private final String type;
1729

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

2234
@JsonCreator
2335
public static MessageComponentType forValue(String value) {
24-
for (MessageComponentType componentType: MessageComponentType.values()) {
25-
if (componentType.getType().equals(value)) {
26-
return componentType;
27-
}
28-
}
29-
30-
return null;
36+
Objects.requireNonNull(value, "Value cannot be null");
37+
return TYPE_MAP.get(value.toLowerCase(Locale.ROOT));
3138
}
3239

3340
@JsonValue

api/src/main/java/com/messagebird/objects/conversations/MessageParam.java

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.messagebird.objects.conversations;
22

3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
import org.apache.commons.lang3.StringUtils;
5+
36
public class MessageParam {
47

58
private TemplateMediaType type;
@@ -10,6 +13,8 @@ public class MessageParam {
1013
private Media document;
1114
private Media image;
1215
private Media video;
16+
@JsonProperty("expiration_time")
17+
private String expirationTime;
1318

1419
public TemplateMediaType getType() {
1520
return type;
@@ -24,6 +29,9 @@ public String getText() {
2429
}
2530

2631
public void setText(String text) {
32+
if (StringUtils.isBlank(text)) {
33+
throw new IllegalArgumentException("Text cannot be null or empty");
34+
}
2735
this.text = text;
2836
}
2937

@@ -48,6 +56,9 @@ public String getDateTime() {
4856
}
4957

5058
public void setDateTime(String dateTime) {
59+
if (StringUtils.isBlank(dateTime)) {
60+
throw new IllegalArgumentException("dateTime cannot be null or empty");
61+
}
5162
this.dateTime = dateTime;
5263
}
5364

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

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

85+
public String getExpirationTime() {
86+
return expirationTime;
87+
}
88+
89+
public void setExpirationTime(String expirationTime) {
90+
if (StringUtils.isBlank(expirationTime)) {
91+
throw new IllegalArgumentException("expirationTime cannot be null or empty");
92+
}
93+
this.expirationTime = expirationTime;
94+
}
95+
7496
@Override
7597
public String toString() {
76-
return "MessageParam{" +
77-
"type=" + type + '\'' +
78-
", text='" + text + '\'' +
79-
", payload='" + payload + '\'' +
80-
", currency=" + currency + '\'' +
81-
", dateTime='" + dateTime + '\'' +
82-
", document=" + document + '\'' +
83-
", image=" + image + '\'' +
84-
", video=" + video +
85-
'}';
98+
StringBuilder sb = new StringBuilder("MessageParam{");
99+
sb.append("type=").append(type)
100+
.append(", text='").append(text).append('\'')
101+
.append(", payload='").append(payload).append('\'')
102+
.append(", currency=").append(currency)
103+
.append(", dateTime='").append(dateTime).append('\'')
104+
.append(", document=").append(document)
105+
.append(", image=").append(image)
106+
.append(", video=").append(video)
107+
.append(", expirationTime='").append(expirationTime).append('\'')
108+
.append('}');
109+
return sb.toString();
86110
}
87111
}

api/src/main/java/com/messagebird/objects/conversations/TemplateMediaType.java

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
import com.fasterxml.jackson.annotation.JsonCreator;
44
import com.fasterxml.jackson.annotation.JsonValue;
5+
import java.util.Map;
6+
import java.util.HashMap;
7+
import java.util.Collections;
58

69
public enum TemplateMediaType {
710

@@ -11,7 +14,19 @@ public enum TemplateMediaType {
1114
TEXT("text"),
1215
CURRENCY("currency"),
1316
DATETIME("date_time"),
14-
PAYLOAD("payload");
17+
PAYLOAD("payload"),
18+
EXPIRATION_TIME("expiration_time");
19+
20+
private static final Map<String, TemplateMediaType> TYPE_MAP;
21+
22+
static {
23+
Map<String, TemplateMediaType> map = new HashMap<>();
24+
for (TemplateMediaType templateMediaType : TemplateMediaType.values()) {
25+
map.put(templateMediaType.getType().toLowerCase(), templateMediaType);
26+
}
27+
TYPE_MAP = Collections.unmodifiableMap(map);
28+
}
29+
1530

1631
private final String type;
1732

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

2237
@JsonCreator
2338
public static TemplateMediaType forValue(String value) {
24-
for (TemplateMediaType templateMediaType: TemplateMediaType.values()) {
25-
if (templateMediaType.getType().equals(value)) {
26-
return templateMediaType;
27-
}
39+
if (value == null) {
40+
throw new IllegalArgumentException("Value cannot be null");
2841
}
29-
30-
return null;
42+
return TYPE_MAP.get(value.toLowerCase());
3143
}
3244

3345
@JsonValue

api/src/main/java/com/messagebird/objects/integrations/HSMComponent.java

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.messagebird.objects.integrations;
22

33
import com.fasterxml.jackson.annotation.JsonProperty;
4+
import org.apache.commons.lang3.StringUtils;
45

56
import java.util.List;
67

@@ -20,6 +21,8 @@ public class HSMComponent {
2021
@JsonProperty("code_expiration_minutes")
2122
private Integer codeExpirationMinutes;
2223
private List<HSMComponentButton> buttons;
24+
@JsonProperty("has_expiration")
25+
private Boolean hasExpiration;
2326

2427
private List<HSMComponentCard> cards;
2528

@@ -46,6 +49,9 @@ public String getText() {
4649
}
4750

4851
public void setText(String text) {
52+
if (StringUtils.isBlank(text)) {
53+
throw new IllegalArgumentException("Text cannot be null or empty");
54+
}
4955
this.text = text;
5056
}
5157

@@ -88,15 +94,29 @@ public Integer getCodeExpirationMinutes() {
8894
public void setCodeExpirationMinutes(Integer codeExpirationMinutes) {
8995
this.codeExpirationMinutes = codeExpirationMinutes;
9096
}
97+
98+
public Boolean getHasExpiration() {
99+
return hasExpiration;
100+
}
101+
102+
public void setHasExpiration(Boolean hasExpiration) {
103+
this.hasExpiration = hasExpiration;
104+
}
105+
91106
@Override
92107
public String toString() {
93-
return "HSMComponent{" +
94-
"type='" + type + '\'' +
95-
", format='" + format + '\'' +
96-
", text='" + text + '\'' +
97-
", buttons=" + buttons +
98-
", example=" + example +
99-
'}';
108+
StringBuilder sb = new StringBuilder("HSMComponent{");
109+
sb.append("type=").append(type)
110+
.append(", format=").append(format)
111+
.append(", text='").append(text).append('\'')
112+
.append(", addSecurityRecommendation=").append(addSecurityRecommendation)
113+
.append(", codeExpirationMinutes=").append(codeExpirationMinutes)
114+
.append(", buttons=").append(buttons)
115+
.append(", hasExpiration=").append(hasExpiration)
116+
.append(", cards=").append(cards)
117+
.append(", example=").append(example)
118+
.append('}');
119+
return sb.toString();
100120
}
101121

102122
/**
@@ -105,8 +125,12 @@ public String toString() {
105125
* @throws IllegalArgumentException Occurs when validation is not passed.
106126
*/
107127
public void validateComponent() throws IllegalArgumentException {
108-
this.validateButtons();
109-
this.validateComponentExample();
128+
try {
129+
this.validateButtons();
130+
this.validateComponentExample();
131+
} catch (IllegalArgumentException e) {
132+
throw new IllegalArgumentException("Component validation failed: " + e.getMessage(), e);
133+
}
110134
}
111135

112136
/**
@@ -153,9 +177,7 @@ private void validateComponentExample() throws IllegalArgumentException {
153177
* @throws IllegalArgumentException Occurs when type is not {@code HEADER} and format is not {@code TEXT}.
154178
*/
155179
private void checkHeaderText() throws IllegalArgumentException {
156-
if (!(type.equals(HSMComponentType.HEADER)
157-
&& format.equals(HSMComponentFormat.TEXT))
158-
) {
180+
if (!(HSMComponentType.HEADER.equals(type) && HSMComponentFormat.TEXT.equals(format))) {
159181
throw new IllegalArgumentException("\"header_text\" is available for only HEADER type and TEXT format.");
160182
}
161183
}
@@ -166,9 +188,9 @@ private void checkHeaderText() throws IllegalArgumentException {
166188
* @throws IllegalArgumentException Occurs when type is not {@code HEADER} and format is not {@code IMAGE}.
167189
*/
168190
private void checkHeaderUrl() throws IllegalArgumentException {
169-
if (!(type.equals(HSMComponentType.HEADER) &&
170-
(format.equals(HSMComponentFormat.IMAGE) || format.equals(HSMComponentFormat.VIDEO) || format.equals(HSMComponentFormat.DOCUMENT)))) {
171-
throw new IllegalArgumentException("\"header_url\" is available for only HEADER type and IMAGE, VIDEO and DOCUMENT format.");
191+
if (!(HSMComponentType.HEADER.equals(type) &&
192+
(HSMComponentFormat.IMAGE.equals(format) || HSMComponentFormat.VIDEO.equals(format) || HSMComponentFormat.DOCUMENT.equals(format)))) {
193+
throw new IllegalArgumentException("\"header_url\" is available for only HEADER type and IMAGE, VIDEO, or DOCUMENT formats.");
172194
}
173195
}
174196
}

api/src/main/java/com/messagebird/objects/integrations/HSMComponentType.java

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
import com.fasterxml.jackson.annotation.JsonCreator;
44
import com.fasterxml.jackson.annotation.JsonValue;
5+
import java.util.Map;
6+
import java.util.HashMap;
7+
import java.util.Collections;
8+
import java.util.Locale;
9+
import java.util.Objects;
510

611
/**
712
* An enum for HSMComponentType
@@ -13,7 +18,18 @@ public enum HSMComponentType {
1318
HEADER("HEADER"),
1419
FOOTER("FOOTER"),
1520
BUTTONS("BUTTONS"),
16-
CAROUSEL("CAROUSEL");
21+
CAROUSEL("CAROUSEL"),
22+
LIMITED_TIME_OFFER("LIMITED_TIME_OFFER");
23+
24+
private static final Map<String, HSMComponentType> TYPE_MAP;
25+
26+
static {
27+
Map<String, HSMComponentType> map = new HashMap<>();
28+
for (HSMComponentType hsmComponentType : HSMComponentType.values()) {
29+
map.put(hsmComponentType.getType().toLowerCase(), hsmComponentType);
30+
}
31+
TYPE_MAP = Collections.unmodifiableMap(map);
32+
}
1733

1834
private final String type;
1935

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

2440
@JsonCreator
2541
public static HSMComponentType forValue(String value) {
26-
for (HSMComponentType hsmComponentType : HSMComponentType.values()) {
27-
if (hsmComponentType.getType().equals(value)) {
28-
return hsmComponentType;
29-
}
30-
}
31-
32-
return null;
42+
Objects.requireNonNull(value, "Value cannot be null");
43+
return TYPE_MAP.get(value.toLowerCase(Locale.ROOT));
3344
}
3445

3546
@JsonValue
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.messagebird.objects.conversations;
2+
3+
import org.junit.Test;
4+
import static org.junit.Assert.assertEquals;
5+
import static org.junit.Assert.assertNull;
6+
7+
public class MessageComponentTypeTest {
8+
@Test
9+
public void testMessageComponentTypeForValueValid() {
10+
assertEquals(MessageComponentType.HEADER, MessageComponentType.forValue("header"));
11+
assertEquals(MessageComponentType.BUTTON, MessageComponentType.forValue("button"));
12+
}
13+
14+
@Test(expected = NullPointerException.class)
15+
public void testMessageComponentTypeForValueNull() {
16+
MessageComponentType.forValue(null);
17+
}
18+
19+
@Test
20+
public void testMessageComponentTypeForValueInvalid() {
21+
assertNull(MessageComponentType.forValue("invalid_type"));
22+
}
23+
24+
@Test
25+
public void testMessageComponentTypeToString() {
26+
assertEquals("header", MessageComponentType.HEADER.toString());
27+
}
28+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.messagebird.objects.conversations;
2+
3+
import org.junit.Test;
4+
import static org.junit.Assert.assertEquals;
5+
6+
public class MessageParamTest {
7+
@Test
8+
public void testMessageParamToString() {
9+
MessageParam param = new MessageParam();
10+
param.setType(TemplateMediaType.IMAGE);
11+
param.setText("Sample text");
12+
param.setPayload("Sample payload");
13+
14+
String expected = "MessageParam{type=image, text='Sample text', payload='Sample payload', currency=null, dateTime='null', document=null, image=null, video=null, expirationTime='null'}";
15+
assertEquals(expected, param.toString());
16+
}
17+
18+
@Test(expected = IllegalArgumentException.class)
19+
public void testMessageParamSetTextInvalid() {
20+
MessageParam param = new MessageParam();
21+
param.setText("");
22+
}
23+
24+
@Test
25+
public void testMessageParamSetTextValid() {
26+
MessageParam param = new MessageParam();
27+
param.setText("Valid text");
28+
assertEquals("Valid text", param.getText());
29+
}
30+
}

0 commit comments

Comments
 (0)