Skip to content

Commit 2cfa0b0

Browse files
authored
Integration tests for Sticker Request (#124)
1 parent 4a40c37 commit 2cfa0b0

File tree

8 files changed

+158
-16
lines changed

8 files changed

+158
-16
lines changed

api/src/main/java/com/javadiscord/jdi/core/api/DiscordResponseParser.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,10 @@ private <T> void success(
100100
) {
101101
if (isSuccessfulResponse(response)) {
102102
try {
103-
T result = OBJECT_MAPPER.readValue(response.body(), type);
103+
T result = null;
104+
if (!response.body().isEmpty()) {
105+
result = OBJECT_MAPPER.readValue(response.body(), type);
106+
}
104107
asyncResponse.setResult(result);
105108
} catch (JsonProcessingException e) {
106109
asyncResponse.setException(e);

api/src/main/java/com/javadiscord/jdi/core/api/StickerRequest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ public StickerRequest(DiscordResponseParser responseParser, long guildId) {
1818
}
1919

2020
public AsyncResponse<Sticker> createGuildSticker(
21-
long guildId,
2221
String name,
2322
String description,
2423
String tags,
@@ -30,9 +29,9 @@ public AsyncResponse<Sticker> createGuildSticker(
3029
);
3130
}
3231

33-
public AsyncResponse<Sticker> deleteGuildSticker(long stickerId) {
32+
public AsyncResponse<Void> deleteGuildSticker(long stickerId) {
3433
return responseParser.callAndParse(
35-
Sticker.class, new DeleteGuildStickerRequest(guildId, stickerId)
34+
Void.class, new DeleteGuildStickerRequest(guildId, stickerId)
3635
);
3736
}
3837

api/src/main/java/com/javadiscord/jdi/core/api/utils/DiscordImageUtil.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public static boolean isNotImage(Path path) {
2626
return !IMAGE_EXTENSIONS.contains(getExtension(path));
2727
}
2828

29-
private static String getExtension(Path path) {
29+
public static String getExtension(Path path) {
3030
String fileName = path.getFileName().toString().toLowerCase();
3131
return fileName.substring(fileName.lastIndexOf('.') + 1);
3232
}

api/src/main/java/com/javadiscord/jdi/internal/api/DiscordRequestBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public DiscordRequestBuilder body(HttpRequest.BodyPublisher body) {
5757

5858
public DiscordRequestBuilder multipartBody(MultipartBodyPublisher body) {
5959
this.body = body;
60-
this.headers.put("Content-Type", "multipart/form-data");
60+
this.headers.put("Content-Type", "multipart/form-data; boundary=" + body.boundary());
6161
return this;
6262
}
6363

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package com.javadiscord.jdi.internal.api.sticker;
22

33
import java.io.FileNotFoundException;
4-
import java.net.http.HttpRequest;
54
import java.nio.file.Path;
65

6+
import com.javadiscord.jdi.core.api.utils.DiscordImageUtil;
77
import com.javadiscord.jdi.internal.api.DiscordRequest;
88
import com.javadiscord.jdi.internal.api.DiscordRequestBuilder;
99

10+
import com.github.mizosoft.methanol.MediaType;
1011
import com.github.mizosoft.methanol.MultipartBodyPublisher;
1112

1213
public record CreateGuildStickerRequest(
@@ -19,21 +20,29 @@ public record CreateGuildStickerRequest(
1920

2021
@Override
2122
public DiscordRequestBuilder create() {
22-
HttpRequest.BodyPublisher body;
23+
2324
try {
24-
body =
25+
MultipartBodyPublisher.Builder body =
2526
MultipartBodyPublisher.newBuilder()
2627
.textPart("name", name)
2728
.textPart("description", description)
28-
.textPart("tags", tags)
29-
.filePart("file", filePath)
30-
.build();
29+
.textPart("tags", tags);
30+
31+
String extension = DiscordImageUtil.getExtension(filePath);
32+
33+
switch (extension) {
34+
case "png" -> body.filePart("file", filePath, MediaType.IMAGE_PNG);
35+
case "jpg", "jpeg" -> body.filePart("file", filePath, MediaType.IMAGE_JPEG);
36+
case "gif" -> body.filePart("file", filePath, MediaType.IMAGE_GIF);
37+
}
38+
39+
return new DiscordRequestBuilder()
40+
.post()
41+
.path("/guilds/%s/stickers".formatted(guildId))
42+
.multipartBody(body.build());
43+
3144
} catch (FileNotFoundException e) {
3245
throw new RuntimeException(e);
3346
}
34-
return new DiscordRequestBuilder()
35-
.post()
36-
.path("/guilds/%s/stickers".formatted(guildId))
37-
.body(body);
3847
}
3948
}
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
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+
}
2.83 KB
Loading
25.2 KB
Loading

0 commit comments

Comments
 (0)