From bb88267a7842266aaa2ae96ddce36c486f27b541 Mon Sep 17 00:00:00 2001 From: Stein Desmet Date: Tue, 2 Jan 2024 14:21:36 +0100 Subject: [PATCH] Replace some strings with enums --- .../com/whatsapp/api/domain/webhook/Interactive.java | 7 ++++++- .../com/whatsapp/api/domain/webhook/WebHookEvent.java | 7 ++++++- .../api/domain/webhook/type/InteractiveType.java | 9 +++++++++ .../whatsapp/api/domain/webhook/type/WebhookType.java | 9 +++++++++ .../whatsapp/api/domain/webhook/WebHookPayloadTest.java | 4 ++-- 5 files changed, 32 insertions(+), 4 deletions(-) create mode 100644 src/main/java/com/whatsapp/api/domain/webhook/type/InteractiveType.java create mode 100644 src/main/java/com/whatsapp/api/domain/webhook/type/WebhookType.java diff --git a/src/main/java/com/whatsapp/api/domain/webhook/Interactive.java b/src/main/java/com/whatsapp/api/domain/webhook/Interactive.java index d07907d57..0f28ed1f0 100644 --- a/src/main/java/com/whatsapp/api/domain/webhook/Interactive.java +++ b/src/main/java/com/whatsapp/api/domain/webhook/Interactive.java @@ -1,7 +1,11 @@ package com.whatsapp.api.domain.webhook; +import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonProperty; +import com.whatsapp.api.domain.webhook.type.InteractiveType; + +import static com.fasterxml.jackson.annotation.JsonFormat.Feature.READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE; /** * The type Interactive. @@ -11,11 +15,12 @@ * @param buttonReply Used on Webhooks related to Reply Buttons. Contains a {@link ButtonReply} reply object. */ @JsonIgnoreProperties(ignoreUnknown = true) +@JsonFormat(with = READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE) public record Interactive( @JsonProperty("list_reply") ListReply listReply, - @JsonProperty("type") String type, + @JsonProperty("type") InteractiveType type, @JsonProperty("button_reply") ButtonReply buttonReply) { diff --git a/src/main/java/com/whatsapp/api/domain/webhook/WebHookEvent.java b/src/main/java/com/whatsapp/api/domain/webhook/WebHookEvent.java index ffb149f16..f520af371 100644 --- a/src/main/java/com/whatsapp/api/domain/webhook/WebHookEvent.java +++ b/src/main/java/com/whatsapp/api/domain/webhook/WebHookEvent.java @@ -1,10 +1,14 @@ package com.whatsapp.api.domain.webhook; +import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonProperty; +import com.whatsapp.api.domain.webhook.type.WebhookType; import java.util.List; +import static com.fasterxml.jackson.annotation.JsonFormat.Feature.READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE; + /** * This class is a representation of the json object sent by the WhatsApp webhook. * 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 @@ * @see Webhooks Setup Guide to more details. **/ @JsonIgnoreProperties(ignoreUnknown = true) -public record WebHookEvent(@JsonProperty("entry") List entry, @JsonProperty("object") String object) { +@JsonFormat(with = READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE) +public record WebHookEvent(@JsonProperty("entry") List entry, @JsonProperty("object") WebhookType object) { } diff --git a/src/main/java/com/whatsapp/api/domain/webhook/type/InteractiveType.java b/src/main/java/com/whatsapp/api/domain/webhook/type/InteractiveType.java new file mode 100644 index 000000000..1c0bae3d1 --- /dev/null +++ b/src/main/java/com/whatsapp/api/domain/webhook/type/InteractiveType.java @@ -0,0 +1,9 @@ +package com.whatsapp.api.domain.webhook.type; + +import com.fasterxml.jackson.annotation.JsonEnumDefaultValue; + +public enum InteractiveType { + button_reply, list_reply, + @JsonEnumDefaultValue + unknown +} \ No newline at end of file diff --git a/src/main/java/com/whatsapp/api/domain/webhook/type/WebhookType.java b/src/main/java/com/whatsapp/api/domain/webhook/type/WebhookType.java new file mode 100644 index 000000000..2b6a58121 --- /dev/null +++ b/src/main/java/com/whatsapp/api/domain/webhook/type/WebhookType.java @@ -0,0 +1,9 @@ +package com.whatsapp.api.domain.webhook.type; + +import com.fasterxml.jackson.annotation.JsonEnumDefaultValue; + +public enum WebhookType { + whatsapp_business_account, + @JsonEnumDefaultValue + unknown +} \ No newline at end of file diff --git a/src/test/java/com/whatsapp/api/domain/webhook/WebHookPayloadTest.java b/src/test/java/com/whatsapp/api/domain/webhook/WebHookPayloadTest.java index 40f3d6b94..0f6729309 100644 --- a/src/test/java/com/whatsapp/api/domain/webhook/WebHookPayloadTest.java +++ b/src/test/java/com/whatsapp/api/domain/webhook/WebHookPayloadTest.java @@ -210,7 +210,7 @@ void testDeserializationStickerMessage() throws IOException, URISyntaxException var obj = WebHook.constructEvent(payload); - Assertions.assertEquals("whatsapp_business_account", obj.object()); + Assertions.assertEquals("whatsapp_business_account", obj.object().name()); Assertions.assertFalse(obj.entry().isEmpty()); Assertions.assertEquals("880480571844883", obj.entry().get(0).id()); @@ -229,7 +229,7 @@ void testDeserializationVideoMessage() throws IOException, URISyntaxException { var obj = WebHook.constructEvent(payload); - Assertions.assertEquals("whatsapp_business_account", obj.object()); + Assertions.assertEquals("whatsapp_business_account", obj.object().name()); Assertions.assertFalse(obj.entry().isEmpty()); Assertions.assertEquals("880480571844883", obj.entry().get(0).id());