|
| 1 | +package com.javadiscord.jdi.core.api; |
| 2 | + |
| 3 | +import com.javadiscord.jdi.core.Guild; |
| 4 | +import com.javadiscord.jdi.core.api.builders.ModifyGuildStickerBuilder; |
| 5 | +import com.javadiscord.jdi.core.models.message.Sticker; |
| 6 | +import com.javadiscord.jdi.core.models.message.StickerFormatType; |
| 7 | +import com.javadiscord.jdi.core.models.message.StickerType; |
| 8 | +import helpers.LiveDiscordHelper; |
| 9 | +import org.junit.jupiter.api.Assertions; |
| 10 | +import org.junit.jupiter.api.BeforeAll; |
| 11 | +import org.junit.jupiter.api.Test; |
| 12 | + |
| 13 | +import java.net.URISyntaxException; |
| 14 | +import java.net.URL; |
| 15 | +import java.nio.file.Paths; |
| 16 | +import java.util.concurrent.CountDownLatch; |
| 17 | +import java.util.concurrent.ThreadLocalRandom; |
| 18 | +import java.util.concurrent.TimeUnit; |
| 19 | +import java.util.concurrent.atomic.AtomicReference; |
| 20 | + |
| 21 | +import static org.junit.jupiter.api.Assertions.*; |
| 22 | + |
| 23 | +class StickerRequestTest { |
| 24 | + private static Guild guild; |
| 25 | + |
| 26 | + @BeforeAll |
| 27 | + public static void setup() throws InterruptedException { |
| 28 | + guild = new LiveDiscordHelper().getGuild(); |
| 29 | + } |
| 30 | + |
| 31 | + @Test |
| 32 | + void testCreateGuildStickerThenGetAndDelete() throws InterruptedException, URISyntaxException { |
| 33 | + CountDownLatch latch = new CountDownLatch(1); |
| 34 | + |
| 35 | + String stickerName = "sticker-" + ThreadLocalRandom.current().nextInt(1,10); |
| 36 | + String description = "d-" + ThreadLocalRandom.current().nextInt(1,10); |
| 37 | + String tags = "tag-" + ThreadLocalRandom.current().nextInt(1,10); |
| 38 | + |
| 39 | + URL url = StickerRequestTest.class.getResource("/test-sticker.png"); |
| 40 | + |
| 41 | + if (url == null) { |
| 42 | + fail("/test-sticker.png not found"); |
| 43 | + return; |
| 44 | + } |
| 45 | + |
| 46 | + AsyncResponse<Sticker> asyncResponse = guild |
| 47 | + .sticker() |
| 48 | + .createGuildSticker(stickerName, description, tags, Paths.get(url.toURI())); |
| 49 | + |
| 50 | + AtomicReference<Long> stickerId = new AtomicReference<>(); |
| 51 | + |
| 52 | + asyncResponse.onSuccess(res -> { |
| 53 | + assertEquals(stickerName, res.name()); |
| 54 | + assertEquals(description, res.description()); |
| 55 | + assertEquals(tags, res.tags()); |
| 56 | + assertEquals(StickerType.GUILD, res.type()); |
| 57 | + assertEquals(StickerFormatType.PNG, res.formatType()); |
| 58 | + stickerId.set(res.id()); |
| 59 | + latch.countDown(); |
| 60 | + }); |
| 61 | + |
| 62 | + asyncResponse.onError(Assertions::fail); |
| 63 | + |
| 64 | + assertTrue(latch.await(30, TimeUnit.SECONDS)); |
| 65 | + |
| 66 | + if(stickerId.get() != null) { |
| 67 | + guild.sticker().deleteGuildSticker(stickerId.get()) |
| 68 | + .onError(Assertions::fail); |
| 69 | + } else { |
| 70 | + fail(); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + void testModifySticker() throws InterruptedException, URISyntaxException { |
| 76 | + CountDownLatch createLatch = new CountDownLatch(1); |
| 77 | + |
| 78 | + URL testSticker = StickerRequestTest.class.getResource("/test-sticker.png"); |
| 79 | + URL testSticker2 = StickerRequestTest.class.getResource("/test-sticker-2.png"); |
| 80 | + |
| 81 | + if (testSticker == null) { |
| 82 | + fail("/test-sticker.png not found"); |
| 83 | + return; |
| 84 | + } |
| 85 | + if (testSticker2 == null) { |
| 86 | + fail("/test-sticker-2.png not found"); |
| 87 | + return; |
| 88 | + } |
| 89 | + |
| 90 | + AtomicReference<Long> stickerId = new AtomicReference<>(); |
| 91 | + |
| 92 | + guild.sticker() |
| 93 | + .createGuildSticker("test-sticker", "description", "tags", Paths.get(testSticker.toURI())) |
| 94 | + .onSuccess(res -> { |
| 95 | + stickerId.set(res.id()); |
| 96 | + createLatch.countDown(); |
| 97 | + }) |
| 98 | + .onError(Assertions::fail); |
| 99 | + |
| 100 | + assertTrue(createLatch.await(30, TimeUnit.SECONDS)); |
| 101 | + |
| 102 | + CountDownLatch modifyLatch = new CountDownLatch(1); |
| 103 | + |
| 104 | + guild.sticker() |
| 105 | + .modifyGuildSticker(new ModifyGuildStickerBuilder(stickerId.get()) |
| 106 | + .description("new description") |
| 107 | + .name("new name") |
| 108 | + .tags("new tags")) |
| 109 | + .onSuccess(res -> { |
| 110 | + assertEquals("new name", res.name()); |
| 111 | + assertEquals("new description", res.description()); |
| 112 | + assertEquals("new tags", res.tags()); |
| 113 | + modifyLatch.countDown(); |
| 114 | + }) |
| 115 | + .onError(err -> { |
| 116 | + System.err.println("error: " + err.getMessage()); |
| 117 | + }); |
| 118 | + |
| 119 | + assertTrue(modifyLatch.await(30, TimeUnit.SECONDS)); |
| 120 | + |
| 121 | + CountDownLatch deleteLatch = new CountDownLatch(1); |
| 122 | + |
| 123 | + AsyncResponse<Void> delete = guild.sticker() |
| 124 | + .deleteGuildSticker(stickerId.get()); |
| 125 | + |
| 126 | + delete.onSuccess(res -> deleteLatch.countDown()); |
| 127 | + delete.onError(Assertions::fail); |
| 128 | + |
| 129 | + assertTrue(deleteLatch.await(30, TimeUnit.SECONDS)); |
| 130 | + } |
| 131 | +} |
0 commit comments