Skip to content

Integration tests for Sticker Request #124

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,10 @@ private <T> void success(
) {
if (isSuccessfulResponse(response)) {
try {
T result = OBJECT_MAPPER.readValue(response.body(), type);
T result = null;
if (!response.body().isEmpty()) {
result = OBJECT_MAPPER.readValue(response.body(), type);
}
asyncResponse.setResult(result);
} catch (JsonProcessingException e) {
asyncResponse.setException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ public StickerRequest(DiscordResponseParser responseParser, long guildId) {
}

public AsyncResponse<Sticker> createGuildSticker(
long guildId,
String name,
String description,
String tags,
Expand All @@ -30,9 +29,9 @@ public AsyncResponse<Sticker> createGuildSticker(
);
}

public AsyncResponse<Sticker> deleteGuildSticker(long stickerId) {
public AsyncResponse<Void> deleteGuildSticker(long stickerId) {
return responseParser.callAndParse(
Sticker.class, new DeleteGuildStickerRequest(guildId, stickerId)
Void.class, new DeleteGuildStickerRequest(guildId, stickerId)
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static boolean isNotImage(Path path) {
return !IMAGE_EXTENSIONS.contains(getExtension(path));
}

private static String getExtension(Path path) {
public static String getExtension(Path path) {
String fileName = path.getFileName().toString().toLowerCase();
return fileName.substring(fileName.lastIndexOf('.') + 1);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public DiscordRequestBuilder body(HttpRequest.BodyPublisher body) {

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package com.javadiscord.jdi.internal.api.sticker;

import java.io.FileNotFoundException;
import java.net.http.HttpRequest;
import java.nio.file.Path;

import com.javadiscord.jdi.core.api.utils.DiscordImageUtil;
import com.javadiscord.jdi.internal.api.DiscordRequest;
import com.javadiscord.jdi.internal.api.DiscordRequestBuilder;

import com.github.mizosoft.methanol.MediaType;
import com.github.mizosoft.methanol.MultipartBodyPublisher;

public record CreateGuildStickerRequest(
Expand All @@ -19,21 +20,29 @@ public record CreateGuildStickerRequest(

@Override
public DiscordRequestBuilder create() {
HttpRequest.BodyPublisher body;

try {
body =
MultipartBodyPublisher.Builder body =
MultipartBodyPublisher.newBuilder()
.textPart("name", name)
.textPart("description", description)
.textPart("tags", tags)
.filePart("file", filePath)
.build();
.textPart("tags", tags);

String extension = DiscordImageUtil.getExtension(filePath);

switch (extension) {
case "png" -> body.filePart("file", filePath, MediaType.IMAGE_PNG);
case "jpg", "jpeg" -> body.filePart("file", filePath, MediaType.IMAGE_JPEG);
case "gif" -> body.filePart("file", filePath, MediaType.IMAGE_GIF);
}

return new DiscordRequestBuilder()
.post()
.path("/guilds/%s/stickers".formatted(guildId))
.multipartBody(body.build());

} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
return new DiscordRequestBuilder()
.post()
.path("/guilds/%s/stickers".formatted(guildId))
.body(body);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package com.javadiscord.jdi.core.api;

import com.javadiscord.jdi.core.Guild;
import com.javadiscord.jdi.core.api.builders.ModifyGuildStickerBuilder;
import com.javadiscord.jdi.core.models.message.Sticker;
import com.javadiscord.jdi.core.models.message.StickerFormatType;
import com.javadiscord.jdi.core.models.message.StickerType;
import helpers.LiveDiscordHelper;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Paths;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;

import static org.junit.jupiter.api.Assertions.*;

class StickerRequestTest {
private static Guild guild;

@BeforeAll
public static void setup() throws InterruptedException {
guild = new LiveDiscordHelper().getGuild();
}

@Test
void testCreateGuildStickerThenGetAndDelete() throws InterruptedException, URISyntaxException {
CountDownLatch latch = new CountDownLatch(1);

String stickerName = "sticker-" + ThreadLocalRandom.current().nextInt(1,10);
String description = "d-" + ThreadLocalRandom.current().nextInt(1,10);
String tags = "tag-" + ThreadLocalRandom.current().nextInt(1,10);

URL url = StickerRequestTest.class.getResource("/test-sticker.png");

if (url == null) {
fail("/test-sticker.png not found");
return;
}

AsyncResponse<Sticker> asyncResponse = guild
.sticker()
.createGuildSticker(stickerName, description, tags, Paths.get(url.toURI()));

AtomicReference<Long> stickerId = new AtomicReference<>();

asyncResponse.onSuccess(res -> {
assertEquals(stickerName, res.name());
assertEquals(description, res.description());
assertEquals(tags, res.tags());
assertEquals(StickerType.GUILD, res.type());
assertEquals(StickerFormatType.PNG, res.formatType());
stickerId.set(res.id());
latch.countDown();
});

asyncResponse.onError(Assertions::fail);

assertTrue(latch.await(30, TimeUnit.SECONDS));

if(stickerId.get() != null) {
guild.sticker().deleteGuildSticker(stickerId.get())
.onError(Assertions::fail);
} else {
fail();
}
}

@Test
void testModifySticker() throws InterruptedException, URISyntaxException {
CountDownLatch createLatch = new CountDownLatch(1);

URL testSticker = StickerRequestTest.class.getResource("/test-sticker.png");
URL testSticker2 = StickerRequestTest.class.getResource("/test-sticker-2.png");

if (testSticker == null) {
fail("/test-sticker.png not found");
return;
}
if (testSticker2 == null) {
fail("/test-sticker-2.png not found");
return;
}

AtomicReference<Long> stickerId = new AtomicReference<>();

guild.sticker()
.createGuildSticker("test-sticker", "description", "tags", Paths.get(testSticker.toURI()))
.onSuccess(res -> {
stickerId.set(res.id());
createLatch.countDown();
})
.onError(Assertions::fail);

assertTrue(createLatch.await(30, TimeUnit.SECONDS));

CountDownLatch modifyLatch = new CountDownLatch(1);

guild.sticker()
.modifyGuildSticker(new ModifyGuildStickerBuilder(stickerId.get())
.description("new description")
.name("new name")
.tags("new tags"))
.onSuccess(res -> {
assertEquals("new name", res.name());
assertEquals("new description", res.description());
assertEquals("new tags", res.tags());
modifyLatch.countDown();
})
.onError(err -> {
System.err.println("error: " + err.getMessage());
});

assertTrue(modifyLatch.await(30, TimeUnit.SECONDS));

CountDownLatch deleteLatch = new CountDownLatch(1);

AsyncResponse<Void> delete = guild.sticker()
.deleteGuildSticker(stickerId.get());

delete.onSuccess(res -> deleteLatch.countDown());
delete.onError(Assertions::fail);

assertTrue(deleteLatch.await(30, TimeUnit.SECONDS));
}
}
Binary file added api/src/test/resources/test-sticker-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added api/src/test/resources/test-sticker.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading