Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/main/java/org/zendesk/client/v2/model/Comment.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static com.fasterxml.jackson.annotation.JsonTypeInfo.As.EXTERNAL_PROPERTY;
import static com.fasterxml.jackson.annotation.JsonTypeInfo.Id.NAME;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
Expand All @@ -16,12 +17,18 @@
* @author stephenc
* @since 09/04/2013 15:09
*/
@JsonTypeInfo(use = NAME, include = EXTERNAL_PROPERTY, property = "type", visible = true)
@JsonTypeInfo(
use = NAME,
include = EXTERNAL_PROPERTY,
property = "type",
defaultImpl = Comment.class,
visible = true)
@JsonSubTypes({
@JsonSubTypes.Type(value = Comment.class, name = "Comment"),
@JsonSubTypes.Type(value = VoiceComment.class, name = "VoiceComment"),
@JsonSubTypes.Type(value = VoiceComment.class, name = "TpeVoiceComment")
})
@JsonIgnoreProperties(ignoreUnknown = true)
public class Comment implements Serializable {

private static final long serialVersionUID = 1L;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package org.zendesk.client.v2.model.comments;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.zendesk.client.v2.model.Comment;

@JsonIgnoreProperties(ignoreUnknown = true)
public class VoiceComment extends Comment {

private VoiceCommentData data;
Expand Down
99 changes: 99 additions & 0 deletions src/test/java/org/zendesk/client/v2/model/CommentTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package org.zendesk.client.v2.model;

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

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.exc.InvalidFormatException;
import com.fasterxml.jackson.databind.node.ObjectNode;
import java.util.function.Function;
import org.jetbrains.annotations.Nullable;
import org.junit.Test;
import org.zendesk.client.v2.Zendesk;
import org.zendesk.client.v2.model.comments.VoiceComment;

public class CommentTest {

private static final long COMMENT_ID = 123L;
private static final String COMMENT_BODY = "Foo";
private static final String COMMENT_VOICE_URL = "http://yourdomain.com/recordings/1.mp3";

private static final ObjectMapper MAPPER = Zendesk.createMapper(Function.identity());

@Test
public void defaultType() throws JsonProcessingException {
String json = createCommentJson(null);
Comment comment = parseJson(json);
assertEquals(Comment.class, comment.getClass());
assertEquals(Long.valueOf(COMMENT_ID), comment.getId());
assertEquals(COMMENT_BODY, comment.getBody());
}

@Test
public void explicitTypeComment() throws JsonProcessingException {
String json = createCommentJson("Comment");
Comment comment = parseJson(json);
assertEquals(Comment.class, comment.getClass());
assertEquals(Long.valueOf(COMMENT_ID), comment.getId());
assertEquals(COMMENT_BODY, comment.getBody());
}

@Test
public void explicitTypeVoiceComment() throws JsonProcessingException {
String json = createCommentJson("VoiceComment");
Comment comment = parseJson(json);
assertEquals(VoiceComment.class, comment.getClass());

VoiceComment voiceComment = (VoiceComment) comment;
assertEquals(Long.valueOf(COMMENT_ID), voiceComment.getId());
assertEquals(COMMENT_BODY, voiceComment.getBody());
assertEquals(COMMENT_VOICE_URL, voiceComment.getData().getRecordingUrl());
}

@Test
public void explicitTypeTpeVoiceCommentType() throws JsonProcessingException {
String json = createCommentJson("TpeVoiceComment");
Comment comment = parseJson(json);
assertEquals(VoiceComment.class, comment.getClass());

VoiceComment voiceComment = (VoiceComment) comment;
assertEquals(Long.valueOf(COMMENT_ID), voiceComment.getId());
assertEquals(COMMENT_BODY, voiceComment.getBody());
assertEquals(COMMENT_VOICE_URL, voiceComment.getData().getRecordingUrl());
}

@Test
public void invalidType() throws JsonProcessingException {
String json = createCommentJson("InvalidCommentType");
assertThrows(InvalidFormatException.class, () -> parseJson(json));
}

private static String createCommentJson(@Nullable String type) throws JsonProcessingException {
// https://developer.zendesk.com/documentation/ticketing/managing-tickets/adding-voice-comments-to-tickets/
ObjectNode voiceData =
MAPPER
.createObjectNode()
.put("from", "+16617480240")
.put("to", "+16617480123")
.put("recording_url", COMMENT_VOICE_URL)
.put("started_at", "2019-04-16T09:14:57Z")
.put("call_duration", 42)
.put("answered_by_id", 28765)
.put("transcription_text", "The transcription of the call")
.put("location", "Topeka, Kansas");

ObjectNode res =
MAPPER.createObjectNode().put("id", COMMENT_ID).put("body", "Foo").set("data", voiceData);

if (type != null) {
res.put("type", type);
}

return MAPPER.writeValueAsString(res);
}

private static Comment parseJson(String json) throws JsonProcessingException {
return MAPPER.readValue(json, Comment.class);
}
}
Loading