From 40bd9c0805da4d55e041f17206d2b7b9b7fadbc6 Mon Sep 17 00:00:00 2001 From: Kazuhiro Sera Date: Fri, 15 Nov 2019 11:23:53 +0900 Subject: [PATCH] Fix #267 Wrong "metadata" property type in the jslack.api.model.Attachment --- .../seratch/jslack/api/model/Attachment.java | 30 +++- .../api/model/attachment/AttachmentsTest.java | 129 ++++++++++++++++++ 2 files changed, 157 insertions(+), 2 deletions(-) create mode 100644 jslack-api-model/src/test/java/test_locally/api/model/attachment/AttachmentsTest.java diff --git a/jslack-api-model/src/main/java/com/github/seratch/jslack/api/model/Attachment.java b/jslack-api-model/src/main/java/com/github/seratch/jslack/api/model/Attachment.java index 4602f7cda..ef17736c4 100644 --- a/jslack-api-model/src/main/java/com/github/seratch/jslack/api/model/Attachment.java +++ b/jslack-api-model/src/main/java/com/github/seratch/jslack/api/model/Attachment.java @@ -272,6 +272,32 @@ public void setAppUnfurl(Boolean appUnfurl) { private Integer size; private String mimetype; private String url; - private String metadata; + private AttachmentMetadata metadata; + + @Data + public static class AttachmentMetadata { + + @SerializedName("thumb_64") + private Boolean thumb64; + @SerializedName("thumb_80") + private Boolean thumb80; + @SerializedName("thumb_160") + private Boolean thumb160; + + @SerializedName("original_w") + private Integer originalWidth; + @SerializedName("original_h") + private Integer originalHeight; + + @SerializedName("thumb_360_w") + private Integer thumb360Width; + @SerializedName("thumb_360_h") + private Integer thumb360Height; + + private String format; + private String extension; + private Integer rotation; + private String thumbTiny; + } -} +} \ No newline at end of file diff --git a/jslack-api-model/src/test/java/test_locally/api/model/attachment/AttachmentsTest.java b/jslack-api-model/src/test/java/test_locally/api/model/attachment/AttachmentsTest.java new file mode 100644 index 000000000..848e1f077 --- /dev/null +++ b/jslack-api-model/src/test/java/test_locally/api/model/attachment/AttachmentsTest.java @@ -0,0 +1,129 @@ +package test_locally.api.model.attachment; + +import com.github.seratch.jslack.api.model.Message; +import org.junit.Test; +import test_locally.unit.GsonFactory; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.notNullValue; +import static org.junit.Assert.assertThat; + +public class AttachmentsTest { + + // Thanks to https://github.com/seratch/jslack/issues/267 + String json = "{\n" + + " \"type\": \"message\",\n" + + " \"text\": \"\",\n" + + " \"files\": [\n" + + " {\n" + + " \"id\": \"FPU0Q2QE5\",\n" + + " \"created\": 1572605991,\n" + + " \"timestamp\": 1572605971,\n" + + " \"name\": \"Fwd: Research Newsletter October 2019\",\n" + + " \"title\": \"Fwd: Research Newsletter October 2019\",\n" + + " \"mimetype\": \"text\\/html\",\n" + + " \"filetype\": \"email\",\n" + + " \"pretty_type\": \"Email\",\n" + + " \"user\": \"USLACKBOT\",\n" + + " \"editable\": true,\n" + + " \"size\": 322064,\n" + + " \"mode\": \"email\",\n" + + " \"is_external\": false,\n" + + " \"external_type\": \"\",\n" + + " \"is_public\": false,\n" + + " \"public_url_shared\": false,\n" + + " \"display_as_bot\": true,\n" + + " \"username\": \"email\",\n" + + " \"url_private\": \"https:\\/\\/files.slack.com\\/files-pri\\/T7K35E469-FPU0Q2QE5\\/fwd__research_newsletter_october_2019\",\n" + + " \"url_private_download\": \"https:\\/\\/files.slack.com\\/files-pri\\/T7K35E469-FPU0Q2QE5\\/download\\/fwd__research_newsletter_october_2019\",\n" + + " \"permalink\": \"https:\\/\\/fnalab.slack.com\\/files\\/USLACKBOT\\/FPU0Q2QE5\\/fwd__research_newsletter_october_2019\",\n" + + " \"permalink_public\": \"https:\\/\\/slack-files.com\\/T7K35E469-FPU0Q2QE5-4d361ed9bd\",\n" + + " \"subject\": \"Fwd: Research Newsletter October 2019\",\n" + + " \"to\": [\n" + + " {\n" + + " \"address\": \"data_science@fna.fi\",\n" + + " \"name\": \"Data Science\",\n" + + " \"original\": \"Data Science \"\n" + + " }\n" + + " ],\n" + + " \"from\": [\n" + + " {\n" + + " \"address\": \"kimmo@fna.fi\",\n" + + " \"name\": \"Kimmo Soram\\u00e4ki\",\n" + + " \"original\": \"Kimmo Soram\\u00e4ki \"\n" + + " }\n" + + " ],\n" + + " \"cc\": [],\n" + + " \"attachments\": [\n" + + " {\n" + + " \"filename\": \"image001.jpg\",\n" + + " \"size\": 3227,\n" + + " \"mimetype\": \"image\\/jpeg\",\n" + + " \"url\": \"https:\\/\\/files-origin.slack.com\\/files-email-priv\\/T7K35E469-FPU0Q2QE5-f0b03de283-f82ce67a\\/image001.jpg\",\n" + + " \"metadata\": {\n" + + " \"thumb_64\": true,\n" + + " \"original_w\": 206,\n" + + " \"original_h\": 67,\n" + + " \"thumb_360_w\": 206,\n" + + " \"thumb_360_h\": 67,\n" + + " \"format\": \"JPEG\",\n" + + " \"extension\": \".jpg\",\n" + + " \"rotation\": 1,\n" + + " \"thumb_tiny\": \"AwAPADCoEYjgZpfKfP3DWiNPQDAkk\\/Sl+wj\\/AJ7SfnW3OhWM3y3\\/ALpo8t\\/7prS+wrz+9k59xSNZqo\\/1sp\\/EUc6CxnGNx1U0hVgMkECtD7Iv9+X8xQLJGO0vJ+Yo50Fj\\/9k=\",\n" + + " \"thumb_80\": true,\n" + + " \"thumb_160\": true\n" + + " }\n" + + " },\n" + + " {\n" + + " \"filename\": \"image001.jpg\",\n" + + " \"size\": 3227,\n" + + " \"mimetype\": \"image\\/jpeg\",\n" + + " \"url\": \"https:\\/\\/files-origin.slack.com\\/files-email-priv\\/T7K35E469-FPU0Q2QE5-f0b03de283-4bc05bae\\/image001.jpg\",\n" + + " \"metadata\": {\n" + + " \"thumb_64\": true,\n" + + " \"original_w\": 206,\n" + + " \"original_h\": 67,\n" + + " \"thumb_360_w\": 206,\n" + + " \"thumb_360_h\": 67,\n" + + " \"format\": \"JPEG\",\n" + + " \"extension\": \".jpg\",\n" + + " \"rotation\": 1,\n" + + " \"thumb_tiny\": \"AwAPADCoEYjgZpfKfP3DWiNPQDAkk\\/Sl+wj\\/AJ7SfnW3OhWM3y3\\/ALpo8t\\/7prS+wrz+9k59xSNZqo\\/1sp\\/EUc6CxnGNx1U0hVgMkECtD7Iv9+X8xQLJGO0vJ+Yo50Fj\\/9k=\",\n" + + " \"thumb_80\": true,\n" + + " \"thumb_160\": true\n" + + " }\n" + + " },\n" + + " {\n" + + " \"filename\": \"Research Newsletter 42.pdf\",\n" + + " \"size\": 297507,\n" + + " \"mimetype\": \"application\\/pdf\",\n" + + " \"url\": \"https:\\/\\/files-origin.slack.com\\/files-email-priv\\/T7K35E469-FPU0Q2QE5-f0b03de283-ac5cb977\\/research_newsletter_42.pdf\",\n" + + " \"metadata\": null\n" + + " }\n" + + " ],\n" + + " \"plain_text\": \"\",\n" + + " \"preview\": \"\",\n" + + " \"preview_plain_text\": \"\",\n" + + " \"has_more\": true,\n" + + " \"sent_to_self\": false,\n" + + " \"bot_id\": \"BCS5JNV34\",\n" + + " \"is_starred\": false,\n" + + " \"has_rich_preview\": false\n" + + " }\n" + + " ],\n" + + " \"upload\": true,\n" + + " \"user\": \"USLACKBOT\",\n" + + " \"display_as_bot\": true,\n" + + " \"bot_id\": \"BCS5JNV34\",\n" + + " \"ts\": \"1572605991.115500\"\n" + + "}\n"; + + @Test + public void parse() { + Message message = GsonFactory.createSnakeCase().fromJson(json, Message.class); + assertThat(message.getFiles().size(), is(1)); + assertThat(message.getFiles().get(0).getAttachments().size(), is(3)); + assertThat(message.getFiles().get(0).getAttachments().get(0).getMetadata(), is(notNullValue())); + } + +}