Skip to content

Commit

Permalink
Add whatsapp obj to MessageStatus
Browse files Browse the repository at this point in the history
  • Loading branch information
SMadani committed Jan 3, 2024
1 parent 03fa676 commit 077382b
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 35 deletions.
73 changes: 47 additions & 26 deletions src/main/java/com/vonage/client/messages/MessageStatus.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import com.fasterxml.jackson.annotation.*;
import com.vonage.client.Jsonable;
import com.vonage.client.messages.whatsapp.ConversationType;
import java.net.URI;
import java.time.Instant;
import java.util.Currency;
Expand Down Expand Up @@ -190,38 +191,25 @@ public String toString() {
@JsonIgnoreProperties(ignoreUnknown = true)
static class Destination {
@JsonProperty("network_code") String networkCode;

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Destination that = (Destination) o;
return Objects.equals(networkCode, that.networkCode);
}

@Override
public int hashCode() {
return Objects.hash(networkCode);
}
}

@JsonInclude(value = JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
static class Sms {
@JsonProperty("count_total") Integer countTotal;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Sms sms = (Sms) o;
return Objects.equals(countTotal, sms.countTotal);
}

@Override
public int hashCode() {
return Objects.hash(countTotal);
@JsonInclude(value = JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
static class Whatsapp {
static class Conversation {
static class Origin {
@JsonProperty("type") ConversationType type;
}
@JsonProperty("id") String id;
@JsonProperty("origin") Origin origin;
}
@JsonProperty("conversation") Conversation conversation;
}

protected MessageStatus() {
Expand All @@ -241,6 +229,7 @@ protected MessageStatus() {

@JsonProperty("destination") private Destination destination;
@JsonProperty("sms") private Sms sms;
@JsonProperty("whatsapp") private Whatsapp whatsapp;


/**
Expand Down Expand Up @@ -352,6 +341,35 @@ public Integer getSmsTotalCount() {
return sms != null ? sms.countTotal : null;
}

/**
* If the {@linkplain #getChannel()} is {@linkplain Channel#WHATSAPP} and {@linkplain #getStatus()} is
* {@linkplain Status#DELIVERED}, returns the conversation's origin type.
*
* @return The WhatsApp conversation category as an enum, {@code null} if absent or not applicable.
*
* @since 8.1.0
*/
@JsonIgnore
public ConversationType getWhatsappConversationType() {
return whatsapp != null &&
whatsapp.conversation != null &&
whatsapp.conversation.origin != null ?
whatsapp.conversation.origin.type : null;
}

/**
* If the {@linkplain #getChannel()} is {@linkplain Channel#WHATSAPP} and {@linkplain #getStatus()} is
* {@linkplain Status#DELIVERED}, returns the conversation ID of the message that triggered this callback.
*
* @return The WhatsApp conversation ID, {@code null} if absent or not applicable.
*
* @since 8.1.0
*/
@JsonIgnore
public String getWhatsappConversationId() {
return whatsapp != null && whatsapp.conversation != null ? whatsapp.conversation.id : null;
}

/**
* Catch-all for properties which are not mapped by this class during deserialization.
*
Expand Down Expand Up @@ -388,14 +406,17 @@ public boolean equals(Object o) {
status == that.status && channel == that.channel &&
Objects.equals(clientRef, that.clientRef) &&
Objects.equals(error, that.error) && Objects.equals(usage, that.usage) &&
Objects.equals(destination, that.destination) && Objects.equals(sms, that.sms);
Objects.equals(getDestinationNetworkCode(), that.getDestinationNetworkCode()) &&
Objects.equals(getSmsTotalCount(), that.getSmsTotalCount()) &&
Objects.equals(getWhatsappConversationId(), that.getWhatsappConversationId()) &&
Objects.equals(getWhatsappConversationType(), that.getWhatsappConversationType());
}

@Override
public int hashCode() {
return Objects.hash(
timestamp, messageUuid, to, from, status, channel,
clientRef, error, usage, destination, sms
clientRef, error, usage, getDestinationNetworkCode(), getSmsTotalCount()
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2023 Vonage
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.vonage.client.messages.whatsapp;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import com.vonage.client.messages.MessageStatus;

/**
* Represents the conversation category as returned by {@link MessageStatus#getWhatsappConversationType()}.
*
* @since 8.1.0
*/
public enum ConversationType {
MARKETING,
UTILITY,
AUTHENTICATION,
REFERRAL_CONVERSION,
SERVICE;

@JsonValue
@Override
public String toString() {
return name().toLowerCase();
}

@JsonCreator
public static ConversationType fromString(String value) {
if (value == null || value.trim().isEmpty()) return null;
return ConversationType.valueOf(value.toUpperCase());
}
}
38 changes: 29 additions & 9 deletions src/test/java/com/vonage/client/messages/MessageStatusTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import com.fasterxml.jackson.annotation.JsonProperty;
import com.vonage.client.VonageUnexpectedException;
import com.vonage.client.messages.whatsapp.ConversationType;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
import java.net.URI;
Expand Down Expand Up @@ -52,6 +53,15 @@ public void testSerdesAllFields() {
sms.countTotal = countTotal;
MessageStatus.Destination destination = new MessageStatus.Destination();
destination.networkCode = networkCode;
MessageStatus.Whatsapp whatsapp = new MessageStatus.Whatsapp();
MessageStatus.Whatsapp.Conversation conversation = new MessageStatus.Whatsapp.Conversation();
whatsapp.conversation = conversation;
MessageStatus.Whatsapp.Conversation.Origin origin = new MessageStatus.Whatsapp.Conversation.Origin();
conversation.origin = origin;
ConversationType whatsappConversationType = ConversationType.REFERRAL_CONVERSION;
origin.type = whatsappConversationType;
String whatsappConversationId = "1234567890";
conversation.id = whatsappConversationId;

String json = "{\n" +
" \"message_uuid\": \""+messageUuid+"\",\n" +
Expand All @@ -75,7 +85,15 @@ public void testSerdesAllFields() {
" },\n" +
" \"destination\": {\n" +
" \"network_code\": \""+networkCode+"\"\n" +
" }\n" +
" },\n" +
" \"whatsapp\": {\n" +
" \"conversation\": {\n" +
" \"id\": \""+whatsappConversationId+"\",\n" +
" \"origin\": {\n" +
" \"type\": \""+whatsappConversationType+"\"\n" +
" }\n" +
" }\n" +
" }\n" +
"}";

MessageStatus ms = MessageStatus.fromJson(json);
Expand All @@ -100,6 +118,8 @@ public void testSerdesAllFields() {
assertEquals(usage.toString(), ms.getUsage().toString());
assertEquals(networkCode, ms.getDestinationNetworkCode());
assertEquals(countTotal, ms.getSmsTotalCount());
assertEquals(whatsappConversationId, ms.getWhatsappConversationId());
assertEquals("referral_conversion", ms.getWhatsappConversationType().toString());
assertNull(ms.getAdditionalProperties());
}

Expand Down Expand Up @@ -155,10 +175,10 @@ public void testDeserializeUnknownProperties() {
" },\n" +
" \"client_ref\": \"string\",\n" +
" \"channel\": \"whatsapp\",\n" +
" \"whatsapp\": {\n" +
" \"conversation\": {\n" +
" \"id\": \"1234567890\",\n" +
" \"origin\": {\n" +
" \"wubwub\": {\n" +
" \"Prop0\": {\n" +
" \"id\": \"ab12cdhfgjk3\",\n" +
" \"N3s7ed\": {\n" +
" \"type\": \"user_initiated\"\n" +
" }\n" +
" }\n" +
Expand All @@ -168,10 +188,10 @@ public void testDeserializeUnknownProperties() {
Map<String, ?> unknown = ms.getAdditionalProperties();
assertNotNull(unknown);
assertEquals(1, unknown.size());
Map<String, ?> whatsapp = (Map<String, ?>) unknown.get("whatsapp");
Map<String, ?> conversation = (Map<String, ?>) whatsapp.get("conversation");
assertEquals("1234567890", conversation.get("id"));
Map<String, ?> origin = (Map<String, ?>) conversation.get("origin");
Map<String, ?> whatsapp = (Map<String, ?>) unknown.get("wubwub");
Map<String, ?> conversation = (Map<String, ?>) whatsapp.get("Prop0");
assertEquals("ab12cdhfgjk3", conversation.get("id"));
Map<String, ?> origin = (Map<String, ?>) conversation.get("N3s7ed");
assertEquals("user_initiated", origin.get("type"));
}

Expand Down

0 comments on commit 077382b

Please sign in to comment.