|
| 1 | +package org.zendesk.client.v2.model; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | +import static org.junit.Assert.assertThrows; |
| 5 | + |
| 6 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 7 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 8 | +import com.fasterxml.jackson.databind.exc.InvalidFormatException; |
| 9 | +import com.fasterxml.jackson.databind.node.ObjectNode; |
| 10 | +import java.util.function.Function; |
| 11 | +import org.jetbrains.annotations.Nullable; |
| 12 | +import org.junit.Test; |
| 13 | +import org.zendesk.client.v2.Zendesk; |
| 14 | +import org.zendesk.client.v2.model.comments.VoiceComment; |
| 15 | + |
| 16 | +public class CommentTest { |
| 17 | + |
| 18 | + private static final long COMMENT_ID = 123L; |
| 19 | + private static final String COMMENT_BODY = "Foo"; |
| 20 | + private static final String COMMENT_VOICE_URL = "http://yourdomain.com/recordings/1.mp3"; |
| 21 | + |
| 22 | + private static final ObjectMapper MAPPER = Zendesk.createMapper(Function.identity()); |
| 23 | + |
| 24 | + @Test |
| 25 | + public void defaultType() throws JsonProcessingException { |
| 26 | + String json = createCommentJson(null); |
| 27 | + Comment comment = parseJson(json); |
| 28 | + assertEquals(Comment.class, comment.getClass()); |
| 29 | + assertEquals(Long.valueOf(COMMENT_ID), comment.getId()); |
| 30 | + assertEquals(COMMENT_BODY, comment.getBody()); |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + public void explicitTypeComment() throws JsonProcessingException { |
| 35 | + String json = createCommentJson("Comment"); |
| 36 | + Comment comment = parseJson(json); |
| 37 | + assertEquals(Comment.class, comment.getClass()); |
| 38 | + assertEquals(Long.valueOf(COMMENT_ID), comment.getId()); |
| 39 | + assertEquals(COMMENT_BODY, comment.getBody()); |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + public void explicitTypeVoiceComment() throws JsonProcessingException { |
| 44 | + String json = createCommentJson("VoiceComment"); |
| 45 | + Comment comment = parseJson(json); |
| 46 | + assertEquals(VoiceComment.class, comment.getClass()); |
| 47 | + |
| 48 | + VoiceComment voiceComment = (VoiceComment) comment; |
| 49 | + assertEquals(Long.valueOf(COMMENT_ID), voiceComment.getId()); |
| 50 | + assertEquals(COMMENT_BODY, voiceComment.getBody()); |
| 51 | + assertEquals(COMMENT_VOICE_URL, voiceComment.getData().getRecordingUrl()); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void explicitTypeTpeVoiceCommentType() throws JsonProcessingException { |
| 56 | + String json = createCommentJson("TpeVoiceComment"); |
| 57 | + Comment comment = parseJson(json); |
| 58 | + assertEquals(VoiceComment.class, comment.getClass()); |
| 59 | + |
| 60 | + VoiceComment voiceComment = (VoiceComment) comment; |
| 61 | + assertEquals(Long.valueOf(COMMENT_ID), voiceComment.getId()); |
| 62 | + assertEquals(COMMENT_BODY, voiceComment.getBody()); |
| 63 | + assertEquals(COMMENT_VOICE_URL, voiceComment.getData().getRecordingUrl()); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void invalidType() throws JsonProcessingException { |
| 68 | + String json = createCommentJson("InvalidCommentType"); |
| 69 | + assertThrows(InvalidFormatException.class, () -> parseJson(json)); |
| 70 | + } |
| 71 | + |
| 72 | + private static String createCommentJson(@Nullable String type) throws JsonProcessingException { |
| 73 | + // https://developer.zendesk.com/documentation/ticketing/managing-tickets/adding-voice-comments-to-tickets/ |
| 74 | + ObjectNode voiceData = |
| 75 | + MAPPER |
| 76 | + .createObjectNode() |
| 77 | + .put("from", "+16617480240") |
| 78 | + .put("to", "+16617480123") |
| 79 | + .put("recording_url", COMMENT_VOICE_URL) |
| 80 | + .put("started_at", "2019-04-16T09:14:57Z") |
| 81 | + .put("call_duration", 42) |
| 82 | + .put("answered_by_id", 28765) |
| 83 | + .put("transcription_text", "The transcription of the call") |
| 84 | + .put("location", "Topeka, Kansas"); |
| 85 | + |
| 86 | + ObjectNode res = |
| 87 | + MAPPER.createObjectNode().put("id", COMMENT_ID).put("body", "Foo").set("data", voiceData); |
| 88 | + |
| 89 | + if (type != null) { |
| 90 | + res.put("type", type); |
| 91 | + } |
| 92 | + |
| 93 | + return MAPPER.writeValueAsString(res); |
| 94 | + } |
| 95 | + |
| 96 | + private static Comment parseJson(String json) throws JsonProcessingException { |
| 97 | + return MAPPER.readValue(json, Comment.class); |
| 98 | + } |
| 99 | +} |
0 commit comments