|
3 | 3 | import com.fasterxml.jackson.databind.DeserializationFeature; |
4 | 4 | import com.fasterxml.jackson.databind.ObjectMapper; |
5 | 5 | import com.fasterxml.jackson.databind.PropertyNamingStrategies; |
| 6 | +import com.uploadcare.data.FileData; |
6 | 7 | import org.junit.Assert; |
| 8 | +import org.junit.Before; |
7 | 9 | import org.junit.Test; |
8 | 10 |
|
| 11 | +import static org.junit.Assert.*; |
| 12 | + |
9 | 13 | public class FileTest |
10 | 14 | { |
11 | 15 |
|
12 | | - @Test |
13 | | - public void enumFails() throws Exception { |
14 | | - String json = "{ \"color_mode\": \"RGBa\"}"; |
| 16 | + private ObjectMapper mapper; |
15 | 17 |
|
16 | | - // duplicate the way the mapper is configured in uploadcare |
17 | | - ObjectMapper mapper = new ObjectMapper(); |
| 18 | + @Before |
| 19 | + public void setUp() { |
| 20 | + mapper = new ObjectMapper(); |
18 | 21 | mapper.setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE); |
19 | 22 | mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); |
| 23 | + } |
| 24 | + |
| 25 | + @Test |
| 26 | + public void enumFails() throws Exception { |
| 27 | + String json = "{ \"color_mode\": \"RGBa\"}"; |
20 | 28 |
|
21 | 29 | Bug bug = mapper.readValue(json, Bug.class); |
22 | 30 |
|
23 | 31 | Assert.assertTrue("Color mode was not properly converted!", File.ColorMode.RGBa.equals(bug.colorMode)); |
24 | 32 | } |
25 | 33 |
|
| 34 | + @Test |
| 35 | + public void testContentInfoDeserialization() throws Exception { |
| 36 | + String json = "{" |
| 37 | + + "\"uuid\": \"22240276-2f06-41f8-9411-755c8ce926ed\"," |
| 38 | + + "\"is_image\": true," |
| 39 | + + "\"is_ready\": true," |
| 40 | + + "\"mime_type\": \"image/jpeg\"," |
| 41 | + + "\"size\": 642," |
| 42 | + + "\"content_info\": {" |
| 43 | + + " \"mime\": {" |
| 44 | + + " \"mime\": \"image/jpeg\"," |
| 45 | + + " \"type\": \"image\"," |
| 46 | + + " \"subtype\": \"jpeg\"" |
| 47 | + + " }," |
| 48 | + + " \"image\": {" |
| 49 | + + " \"format\": \"JPEG\"," |
| 50 | + + " \"width\": 500," |
| 51 | + + " \"height\": 500," |
| 52 | + + " \"sequence\": false," |
| 53 | + + " \"color_mode\": \"RGB\"," |
| 54 | + + " \"orientation\": 6" |
| 55 | + + " }" |
| 56 | + + "}" |
| 57 | + + "}"; |
| 58 | + |
| 59 | + FileData fileData = mapper.readValue(json, FileData.class); |
| 60 | + |
| 61 | + assertNotNull("contentInfo should not be null", fileData.contentInfo); |
| 62 | + assertNotNull("contentInfo.mime should not be null", fileData.contentInfo.mime); |
| 63 | + assertEquals("image/jpeg", fileData.contentInfo.mime.mime); |
| 64 | + assertEquals("image", fileData.contentInfo.mime.type); |
| 65 | + assertEquals("jpeg", fileData.contentInfo.mime.subtype); |
| 66 | + |
| 67 | + assertNotNull("contentInfo.image should not be null", fileData.contentInfo.image); |
| 68 | + assertEquals("JPEG", fileData.contentInfo.image.format); |
| 69 | + assertEquals(500, fileData.contentInfo.image.width); |
| 70 | + assertEquals(500, fileData.contentInfo.image.height); |
| 71 | + assertEquals(File.ColorMode.RGB, fileData.contentInfo.image.colorMode); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void testVideoContentInfoDeserialization() throws Exception { |
| 76 | + String json = "{" |
| 77 | + + "\"uuid\": \"abc123\"," |
| 78 | + + "\"is_image\": false," |
| 79 | + + "\"is_ready\": true," |
| 80 | + + "\"mime_type\": \"video/mp4\"," |
| 81 | + + "\"size\": 1048576," |
| 82 | + + "\"content_info\": {" |
| 83 | + + " \"mime\": {" |
| 84 | + + " \"mime\": \"video/mp4\"," |
| 85 | + + " \"type\": \"video\"," |
| 86 | + + " \"subtype\": \"mp4\"" |
| 87 | + + " }," |
| 88 | + + " \"video\": {" |
| 89 | + + " \"format\": \"MP4\"," |
| 90 | + + " \"duration\": 12000," |
| 91 | + + " \"bitrate\": 1500000," |
| 92 | + + " \"video\": {" |
| 93 | + + " \"height\": 1080," |
| 94 | + + " \"width\": 1920," |
| 95 | + + " \"frame_rate\": 25.0," |
| 96 | + + " \"bitrate\": 1400000," |
| 97 | + + " \"codec\": \"h264\"" |
| 98 | + + " }," |
| 99 | + + " \"audio\": {" |
| 100 | + + " \"bitrate\": 128000," |
| 101 | + + " \"codec\": \"aac\"," |
| 102 | + + " \"channels\": \"2\"," |
| 103 | + + " \"sample_rate\": 44100" |
| 104 | + + " }" |
| 105 | + + " }" |
| 106 | + + "}" |
| 107 | + + "}"; |
| 108 | + |
| 109 | + FileData fileData = mapper.readValue(json, FileData.class); |
| 110 | + |
| 111 | + assertNotNull("contentInfo should not be null", fileData.contentInfo); |
| 112 | + assertNotNull("contentInfo.mime should not be null", fileData.contentInfo.mime); |
| 113 | + assertEquals("video/mp4", fileData.contentInfo.mime.mime); |
| 114 | + assertEquals("video", fileData.contentInfo.mime.type); |
| 115 | + |
| 116 | + assertNotNull("contentInfo.video should not be null", fileData.contentInfo.video); |
| 117 | + assertEquals("MP4", fileData.contentInfo.video.format); |
| 118 | + assertEquals(12000, fileData.contentInfo.video.duration); |
| 119 | + assertNotNull("video.video should not be null", fileData.contentInfo.video.video); |
| 120 | + assertEquals(1920, fileData.contentInfo.video.video.width); |
| 121 | + assertEquals(1080, fileData.contentInfo.video.video.height); |
| 122 | + assertNotNull("video.audio should not be null", fileData.contentInfo.video.audio); |
| 123 | + assertEquals("aac", fileData.contentInfo.video.audio.codec); |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + public void testMetadataDeserialization() throws Exception { |
| 128 | + String json = "{" |
| 129 | + + "\"uuid\": \"22240276-2f06-41f8-9411-755c8ce926ed\"," |
| 130 | + + "\"metadata\": {" |
| 131 | + + " \"subsystem\": \"uploader\"," |
| 132 | + + " \"pet\": \"cat\"" |
| 133 | + + "}" |
| 134 | + + "}"; |
| 135 | + |
| 136 | + FileData fileData = mapper.readValue(json, FileData.class); |
| 137 | + |
| 138 | + assertNotNull("metadata should not be null", fileData.metadata); |
| 139 | + assertEquals("uploader", fileData.metadata.get("subsystem")); |
| 140 | + assertEquals("cat", fileData.metadata.get("pet")); |
| 141 | + } |
| 142 | + |
| 143 | + @Test |
| 144 | + public void testAppDataDeserialization() throws Exception { |
| 145 | + String json = "{" |
| 146 | + + "\"uuid\": \"22240276-2f06-41f8-9411-755c8ce926ed\"," |
| 147 | + + "\"appdata\": {" |
| 148 | + + " \"uc_clamav_virus_scan\": {" |
| 149 | + + " \"data\": {" |
| 150 | + + " \"infected\": false," |
| 151 | + + " \"infected_with\": null" |
| 152 | + + " }," |
| 153 | + + " \"version\": \"0.104.2\"," |
| 154 | + + " \"datetime_created\": \"2021-09-21T11:24:33\"," |
| 155 | + + " \"datetime_updated\": \"2021-09-21T11:24:33\"" |
| 156 | + + " }" |
| 157 | + + "}" |
| 158 | + + "}"; |
| 159 | + |
| 160 | + FileData fileData = mapper.readValue(json, FileData.class); |
| 161 | + |
| 162 | + assertNotNull("appdata should not be null", fileData.appdata); |
| 163 | + assertTrue("appdata should contain uc_clamav_virus_scan", fileData.appdata.containsKey("uc_clamav_virus_scan")); |
| 164 | + |
| 165 | + File.AppData appData = fileData.appdata.get("uc_clamav_virus_scan"); |
| 166 | + |
| 167 | + assertNotNull("appdata entry should not be null", appData); |
| 168 | + assertEquals("0.104.2", appData.version); |
| 169 | + assertNotNull("appdata.data should not be null", appData.data); |
| 170 | + } |
| 171 | + |
26 | 172 | static class Bug { |
27 | 173 | public File.ColorMode colorMode; |
28 | 174 | } |
|
0 commit comments