Skip to content

Commit bb88267

Browse files
committed
Replace some strings with enums
1 parent 5c2c310 commit bb88267

5 files changed

Lines changed: 32 additions & 4 deletions

File tree

src/main/java/com/whatsapp/api/domain/webhook/Interactive.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package com.whatsapp.api.domain.webhook;
22

3+
import com.fasterxml.jackson.annotation.JsonFormat;
34
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
45
import com.fasterxml.jackson.annotation.JsonProperty;
6+
import com.whatsapp.api.domain.webhook.type.InteractiveType;
7+
8+
import static com.fasterxml.jackson.annotation.JsonFormat.Feature.READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE;
59

610
/**
711
* The type Interactive.
@@ -11,11 +15,12 @@
1115
* @param buttonReply Used on Webhooks related to Reply Buttons. Contains a {@link ButtonReply} reply object.
1216
*/
1317
@JsonIgnoreProperties(ignoreUnknown = true)
18+
@JsonFormat(with = READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE)
1419
public record Interactive(
1520

1621
@JsonProperty("list_reply") ListReply listReply,
1722

18-
@JsonProperty("type") String type,
23+
@JsonProperty("type") InteractiveType type,
1924

2025
@JsonProperty("button_reply") ButtonReply buttonReply) {
2126

src/main/java/com/whatsapp/api/domain/webhook/WebHookEvent.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
package com.whatsapp.api.domain.webhook;
22

3+
import com.fasterxml.jackson.annotation.JsonFormat;
34
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
45
import com.fasterxml.jackson.annotation.JsonProperty;
6+
import com.whatsapp.api.domain.webhook.type.WebhookType;
57

68
import java.util.List;
79

10+
import static com.fasterxml.jackson.annotation.JsonFormat.Feature.READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE;
11+
812
/**
913
* This class is a representation of the json object sent by the WhatsApp webhook.
1014
* Whenever a trigger event occurs, the WhatsApp Business Platform sees the event and sends a notification to a Webhook URL you have previously specified.
@@ -15,5 +19,6 @@
1519
* @see <a href="https://developers.facebook.com/docs/whatsapp/cloud-api/guides/set-up-webhooks">Webhooks Setup Guide</a> to more details.
1620
**/
1721
@JsonIgnoreProperties(ignoreUnknown = true)
18-
public record WebHookEvent(@JsonProperty("entry") List<Entry> entry, @JsonProperty("object") String object) {
22+
@JsonFormat(with = READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE)
23+
public record WebHookEvent(@JsonProperty("entry") List<Entry> entry, @JsonProperty("object") WebhookType object) {
1924
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.whatsapp.api.domain.webhook.type;
2+
3+
import com.fasterxml.jackson.annotation.JsonEnumDefaultValue;
4+
5+
public enum InteractiveType {
6+
button_reply, list_reply,
7+
@JsonEnumDefaultValue
8+
unknown
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.whatsapp.api.domain.webhook.type;
2+
3+
import com.fasterxml.jackson.annotation.JsonEnumDefaultValue;
4+
5+
public enum WebhookType {
6+
whatsapp_business_account,
7+
@JsonEnumDefaultValue
8+
unknown
9+
}

src/test/java/com/whatsapp/api/domain/webhook/WebHookPayloadTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ void testDeserializationStickerMessage() throws IOException, URISyntaxException
210210

211211
var obj = WebHook.constructEvent(payload);
212212

213-
Assertions.assertEquals("whatsapp_business_account", obj.object());
213+
Assertions.assertEquals("whatsapp_business_account", obj.object().name());
214214
Assertions.assertFalse(obj.entry().isEmpty());
215215
Assertions.assertEquals("880480571844883", obj.entry().get(0).id());
216216

@@ -229,7 +229,7 @@ void testDeserializationVideoMessage() throws IOException, URISyntaxException {
229229

230230
var obj = WebHook.constructEvent(payload);
231231

232-
Assertions.assertEquals("whatsapp_business_account", obj.object());
232+
Assertions.assertEquals("whatsapp_business_account", obj.object().name());
233233
Assertions.assertFalse(obj.entry().isEmpty());
234234
Assertions.assertEquals("880480571844883", obj.entry().get(0).id());
235235

0 commit comments

Comments
 (0)