|
1 | 1 | package test_with_remote_apis.methods; |
2 | 2 |
|
3 | 3 | import com.slack.api.Slack; |
| 4 | +import com.slack.api.methods.MethodsClient; |
4 | 5 | import com.slack.api.methods.SlackApiException; |
5 | 6 | import com.slack.api.methods.response.chat.ChatDeleteResponse; |
6 | 7 | import com.slack.api.methods.response.chat.ChatPostMessageResponse; |
| 8 | +import com.slack.api.methods.response.chat.ChatUpdateResponse; |
7 | 9 | import com.slack.api.methods.response.files.remote.*; |
8 | 10 | import com.slack.api.methods.response.search.SearchFilesResponse; |
9 | 11 | import com.slack.api.model.File; |
|
21 | 23 | import java.io.InputStream; |
22 | 24 | import java.net.URL; |
23 | 25 | import java.net.URLConnection; |
| 26 | +import java.nio.file.Files; |
24 | 27 | import java.util.Arrays; |
| 28 | +import java.util.UUID; |
25 | 29 |
|
| 30 | +import static com.slack.api.model.block.Blocks.*; |
| 31 | +import static com.slack.api.model.block.composition.BlockCompositions.plainText; |
26 | 32 | import static org.hamcrest.CoreMatchers.is; |
27 | 33 | import static org.hamcrest.CoreMatchers.nullValue; |
28 | 34 | import static org.hamcrest.MatcherAssert.assertThat; |
@@ -238,4 +244,57 @@ public void updateAndShare_searchable() throws Exception { |
238 | 244 | log.info("Searchable file contents took {} milliseconds to get indexed", millis); |
239 | 245 | } |
240 | 246 |
|
| 247 | + @Test |
| 248 | + public void testPreviewImageReplacement() throws Exception { |
| 249 | + loadRandomChannel(); |
| 250 | + |
| 251 | + MethodsClient client = slack.methods(botToken); |
| 252 | + String externalUrl = "https://www.example.com/test-image"; |
| 253 | + |
| 254 | + String firstExternalId = "remote-file-slack-logo-" + UUID.randomUUID(); |
| 255 | + byte[] firstPreviewImage = Files.readAllBytes(new java.io.File("src/test/resources/seratch.jpg").toPath()); |
| 256 | + FilesRemoteAddResponse firstFileCreation = client.filesRemoteAdd(r -> r |
| 257 | + .externalId(firstExternalId) |
| 258 | + .externalUrl(externalUrl) |
| 259 | + .title("Some image") |
| 260 | + .previewImage(firstPreviewImage) |
| 261 | + ); |
| 262 | + assertThat(firstFileCreation.getError(), is(nullValue())); |
| 263 | + |
| 264 | + ChatPostMessageResponse newMessage = client.chatPostMessage(r -> r |
| 265 | + .channel(randomChannelId) |
| 266 | + .text("This is v1") |
| 267 | + .blocks(asBlocks( |
| 268 | + section(s -> s.text(plainText("This is v1"))), |
| 269 | + file(f -> f.externalId(firstExternalId).source("remote")) |
| 270 | + )) |
| 271 | + ); |
| 272 | + assertThat(newMessage.getError(), is(nullValue())); |
| 273 | + |
| 274 | + // For humans |
| 275 | + Thread.sleep(2000); |
| 276 | + |
| 277 | + String secondExternalId = "remote-file-slack-logo-" + UUID.randomUUID(); |
| 278 | + byte[] secondPreviewImage = Files.readAllBytes(new java.io.File("src/test/resources/slack-logo.gif").toPath()); |
| 279 | + FilesRemoteAddResponse secondFileCreation = client.filesRemoteAdd(r -> r |
| 280 | + .externalId(secondExternalId) |
| 281 | + .externalUrl(externalUrl) |
| 282 | + .title("Some image") |
| 283 | + .previewImage(secondPreviewImage) |
| 284 | + ); |
| 285 | + assertThat(secondFileCreation.getError(), is(nullValue())); |
| 286 | + |
| 287 | + ChatUpdateResponse messageModification = client.chatUpdate(r -> r |
| 288 | + .channel(randomChannelId) |
| 289 | + .ts(newMessage.getTs()) |
| 290 | + .text("This is v2") |
| 291 | + .blocks(asBlocks( |
| 292 | + section(s -> s.text(plainText("This is v2"))), |
| 293 | + file(f -> f.externalId(secondExternalId).source("remote")) |
| 294 | + )) |
| 295 | + ); |
| 296 | + assertThat(messageModification.getError(), is(nullValue())); |
| 297 | + |
| 298 | + } |
| 299 | + |
241 | 300 | } |
0 commit comments