From 52ba91e7daf9bd4d282adce14c3ca2e935eaa274 Mon Sep 17 00:00:00 2001 From: Etzelia Date: Thu, 10 Jun 2021 21:47:12 -0500 Subject: [PATCH] Update to new maven and add tests Signed-off-by: Etzelia --- .drone.yml | 1 + README.md | 4 +-- build.gradle | 19 +++++++++++--- src/main/java/xyz/etztech/Webhook.java | 4 +-- src/main/java/xyz/etztech/embed/Author.java | 8 +++--- src/main/java/xyz/etztech/embed/Embed.java | 24 +++++++++--------- src/main/java/xyz/etztech/embed/Field.java | 6 ++--- src/main/java/xyz/etztech/embed/Footer.java | 6 ++--- src/main/java/xyz/etztech/embed/Image.java | 8 +++--- src/test/java/xyz/etztech/JavacordTest.java | 23 +++++++++++++++++ .../java/xyz/etztech/embed/EmbedTest.java | 25 +++++++++++++++++++ 11 files changed, 95 insertions(+), 33 deletions(-) create mode 100644 src/test/java/xyz/etztech/JavacordTest.java create mode 100644 src/test/java/xyz/etztech/embed/EmbedTest.java diff --git a/.drone.yml b/.drone.yml index c9e7d7c..acffe40 100644 --- a/.drone.yml +++ b/.drone.yml @@ -12,6 +12,7 @@ steps: image: gradle:6.6 commands: - gradle build + - gradle test --- kind: pipeline diff --git a/README.md b/README.md index 4b769f2..98aac00 100644 --- a/README.md +++ b/README.md @@ -4,8 +4,8 @@ A small, no-dependency Discord utility library. Currently used for webhooks/embe ## Releases -[Reposilite](https://repo.etztech.xyz/releases/xyz/etztech/javacord) +[Reposilite](https://mvn.birbmc.com/releases/xyz/etztech/javacord) ## License -[MIT](LICENSE``) \ No newline at end of file +[MIT](LICENSE) \ No newline at end of file diff --git a/build.gradle b/build.gradle index 9284eec..45a9ee7 100644 --- a/build.gradle +++ b/build.gradle @@ -7,6 +7,19 @@ group = 'xyz.etztech' version = '0.2.2' sourceCompatibility = '8' +repositories { + mavenLocal() +} + +test { + useJUnitPlatform() +} + +dependencies { + testImplementation("org.junit.jupiter:junit-jupiter-api:5.6.2") + testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.6.2") +} + publishing { publications { maven(MavenPublication) { @@ -15,10 +28,10 @@ publishing { } repositories { maven { - name = "etztech" - url = "https://repo.etztech.xyz/releases" + name = "birbmc" + url = "https://mvn.birbmc.com/releases" credentials { - username = "etzelia" + username = "admin" password = System.getenv("MAVEN_PASSWORD") } } diff --git a/src/main/java/xyz/etztech/Webhook.java b/src/main/java/xyz/etztech/Webhook.java index 9863ce3..fbc714a 100644 --- a/src/main/java/xyz/etztech/Webhook.java +++ b/src/main/java/xyz/etztech/Webhook.java @@ -19,8 +19,8 @@ public class Webhook { public String toString() { StringBuilder builder = new StringBuilder("{"); List json = new ArrayList<>(); - if (!"".equals(content)) json.add(String.format("\"content\": \"%s\"", Javacord.escapeQuote(content))); - if (embeds.size() > 0) json.add(String.format("\"embeds\": [%s]", embeds.stream().map(Embed::toJSON).collect(Collectors.joining(",")))); + if (!"".equals(content)) json.add(String.format("\"content\":\"%s\"", Javacord.escapeQuote(content))); + if (embeds.size() > 0) json.add(String.format("\"embeds\":[%s]", embeds.stream().map(Embed::toJSON).collect(Collectors.joining(",")))); builder.append(String.join(",", json)); return builder.append("}").toString(); } diff --git a/src/main/java/xyz/etztech/embed/Author.java b/src/main/java/xyz/etztech/embed/Author.java index d9744de..003c25d 100644 --- a/src/main/java/xyz/etztech/embed/Author.java +++ b/src/main/java/xyz/etztech/embed/Author.java @@ -21,10 +21,10 @@ public class Author { public String toJSON() { StringBuilder builder = new StringBuilder("{"); List json = new ArrayList<>(); - if (!"".equals(name)) json.add(String.format("\"name\": \"%s\"", Javacord.escapeQuote(name))); - if (!"".equals(url)) json.add(String.format("\"url\": \"%s\"", Javacord.escapeQuote(url))); - if (!"".equals(iconURL)) json.add(String.format("\"icon_url\": \"%s\"", Javacord.escapeQuote(iconURL))); - if (!"".equals(proxyIconURL)) json.add(String.format("\"proxy_icon_url\": \"%s\"", Javacord.escapeQuote(proxyIconURL))); + if (!"".equals(name)) json.add(String.format("\"name\":\"%s\"", Javacord.escapeQuote(name))); + if (!"".equals(url)) json.add(String.format("\"url\":\"%s\"", Javacord.escapeQuote(url))); + if (!"".equals(iconURL)) json.add(String.format("\"icon_url\":\"%s\"", Javacord.escapeQuote(iconURL))); + if (!"".equals(proxyIconURL)) json.add(String.format("\"proxy_icon_url\":\"%s\"", Javacord.escapeQuote(proxyIconURL))); builder.append(String.join(",", json)); return builder.append("}").toString(); } diff --git a/src/main/java/xyz/etztech/embed/Embed.java b/src/main/java/xyz/etztech/embed/Embed.java index 06e4253..cc1ea79 100644 --- a/src/main/java/xyz/etztech/embed/Embed.java +++ b/src/main/java/xyz/etztech/embed/Embed.java @@ -100,16 +100,16 @@ public class Embed { public String toJSON() { StringBuilder builder = new StringBuilder("{"); List json = new ArrayList<>(); - if (!"".equals(title)) json.add(String.format("\"title\": \"%s\"", Javacord.escapeQuote(title))); - if (!"".equals(description)) json.add(String.format("\"description\": \"%s\"", Javacord.escapeQuote(description))); - if (!"".equals(url)) json.add(String.format("\"url\": \"%s\"", Javacord.escapeQuote(url))); - if (color != 0) json.add(String.format("\"color\": \"%d\"", color)); - if (timestamp != null) json.add(String.format("\"timestamp\": \"%s\"", timestamp.format(DateTimeFormatter.ISO_INSTANT))); - if (footer != null) json.add(String.format("\"footer\": %s", footer.toJSON())); - if (thumbnail != null) json.add(String.format("\"thumbnail\": %s", thumbnail.toJSON())); - if (image != null) json.add(String.format("\"image\": %s", image.toJSON())); - if (author != null) json.add(String.format("\"author\": %s", author.toJSON())); - if (fields.size() > 0) json.add(String.format("\"fields\": [%s]", fields.stream().map(Field::toJSON).collect(Collectors.joining(",")))); + if (!"".equals(title)) json.add(String.format("\"title\":\"%s\"", Javacord.escapeQuote(title))); + if (!"".equals(description)) json.add(String.format("\"description\":\"%s\"", Javacord.escapeQuote(description))); + if (!"".equals(url)) json.add(String.format("\"url\":\"%s\"", Javacord.escapeQuote(url))); + if (color != 0) json.add(String.format("\"color\":\"%d\"", color)); + if (timestamp != null) json.add(String.format("\"timestamp\":\"%s\"", timestamp.format(DateTimeFormatter.ISO_INSTANT))); + if (footer != null) json.add(String.format("\"footer\":%s", footer.toJSON())); + if (thumbnail != null) json.add(String.format("\"thumbnail\":%s", thumbnail.toJSON())); + if (image != null) json.add(String.format("\"image\":%s", image.toJSON())); + if (author != null) json.add(String.format("\"author\":%s", author.toJSON())); + if (fields.size() > 0) json.add(String.format("\"fields\":[%s]", fields.stream().map(Field::toJSON).collect(Collectors.joining(",")))); builder.append(String.join(",", json)); return builder.append("}").toString(); } @@ -117,8 +117,8 @@ public class Embed { public String toString() { StringBuilder builder = new StringBuilder("{"); List json = new ArrayList<>(); - if (!"".equals(content)) json.add(String.format("\"content\": \"%s\"", Javacord.escapeQuote(content))); - if (!"{}".equals(toJSON())) json.add(String.format("\"embed\": %s", toJSON())); + if (!"".equals(content)) json.add(String.format("\"content\":\"%s\"", Javacord.escapeQuote(content))); + if (!"{}".equals(toJSON())) json.add(String.format("\"embed\":%s", toJSON())); builder.append(String.join(",", json)); return builder.append("}").toString(); } diff --git a/src/main/java/xyz/etztech/embed/Field.java b/src/main/java/xyz/etztech/embed/Field.java index 75ad462..13998c6 100644 --- a/src/main/java/xyz/etztech/embed/Field.java +++ b/src/main/java/xyz/etztech/embed/Field.java @@ -25,9 +25,9 @@ public class Field { public String toJSON() { StringBuilder builder = new StringBuilder("{"); List json = new ArrayList<>(); - json.add(String.format("\"name\": \"%s\"", Javacord.escapeQuote(name))); - json.add(String.format("\"value\": \"%s\"", Javacord.escapeQuote(value))); - if (inline) json.add(String.format("\"inline\": %s", true)); + json.add(String.format("\"name\":\"%s\"", Javacord.escapeQuote(name))); + json.add(String.format("\"value\":\"%s\"", Javacord.escapeQuote(value))); + if (inline) json.add(String.format("\"inline\":%s", true)); builder.append(String.join(",", json)); return builder.append("}").toString(); } diff --git a/src/main/java/xyz/etztech/embed/Footer.java b/src/main/java/xyz/etztech/embed/Footer.java index 5d472ea..0d31a25 100644 --- a/src/main/java/xyz/etztech/embed/Footer.java +++ b/src/main/java/xyz/etztech/embed/Footer.java @@ -25,10 +25,10 @@ public class Footer { public String toJSON() { StringBuilder builder = new StringBuilder("{"); - builder.append(String.format("\"text\": \"%s\"", Javacord.escapeQuote(text))); + builder.append(String.format("\"text\":\"%s\"", Javacord.escapeQuote(text))); List json = new ArrayList<>(); - if (!"".equals(iconURL)) json.add(String.format("\"icon_url\": \"%s\"", Javacord.escapeQuote(iconURL))); - if (!"".equals(proxyIconURL)) json.add(String.format("\"proxy_icon_url\": \"%s\"", Javacord.escapeQuote(proxyIconURL))); + if (!"".equals(iconURL)) json.add(String.format("\"icon_url\":\"%s\"", Javacord.escapeQuote(iconURL))); + if (!"".equals(proxyIconURL)) json.add(String.format("\"proxy_icon_url\":\"%s\"", Javacord.escapeQuote(proxyIconURL))); builder.append(String.join(",", json)); return builder.append("}").toString(); } diff --git a/src/main/java/xyz/etztech/embed/Image.java b/src/main/java/xyz/etztech/embed/Image.java index 249c125..aebc285 100644 --- a/src/main/java/xyz/etztech/embed/Image.java +++ b/src/main/java/xyz/etztech/embed/Image.java @@ -21,10 +21,10 @@ public class Image { public String toJSON() { StringBuilder builder = new StringBuilder("{"); List json = new ArrayList<>(); - if (!"".equals(url)) json.add(String.format("\"url\": \"%s\"", Javacord.escapeQuote(url))); - if (!"".equals(proxyURL)) json.add(String.format("\"proxy_url\": \"%s\"", Javacord.escapeQuote(proxyURL))); - if (height != 0) json.add(String.format("\"height\": \"%d\"", height)); - if (width != 0) json.add(String.format("\"width\": \"%d\"", width)); + if (!"".equals(url)) json.add(String.format("\"url\":\"%s\"", Javacord.escapeQuote(url))); + if (!"".equals(proxyURL)) json.add(String.format("\"proxy_url\":\"%s\"", Javacord.escapeQuote(proxyURL))); + if (height != 0) json.add(String.format("\"height\":\"%d\"", height)); + if (width != 0) json.add(String.format("\"width\":\"%d\"", width)); builder.append(String.join(",", json)); return builder.append("}").toString(); } diff --git a/src/test/java/xyz/etztech/JavacordTest.java b/src/test/java/xyz/etztech/JavacordTest.java new file mode 100644 index 0000000..283483b --- /dev/null +++ b/src/test/java/xyz/etztech/JavacordTest.java @@ -0,0 +1,23 @@ +package xyz.etztech; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class JavacordTest { + + @Test + void TestEscapeFormat() { + String raw = "*Test* **Test** _Test_ __Test__ ~Test~ ~~Test~~ |Test| ||Test||"; + String escaped = Javacord.escapeFormat(raw); + String expected = "\\\\*Test\\\\* \\\\*\\\\*Test\\\\*\\\\* \\\\_Test\\\\_ \\\\_\\\\_Test\\\\_\\\\_ \\\\~Test\\\\~ \\\\~\\\\~Test\\\\~\\\\~ \\\\|Test\\\\| \\\\|\\\\|Test\\\\|\\\\|"; + Assertions.assertEquals(expected, escaped); + } + + @Test + void TestEscapeQuote() { + String raw = "\"Test\""; + String escaped = Javacord.escapeQuote(raw); + String expected = "\\\"Test\\\""; + Assertions.assertEquals(expected, escaped); + } +} diff --git a/src/test/java/xyz/etztech/embed/EmbedTest.java b/src/test/java/xyz/etztech/embed/EmbedTest.java new file mode 100644 index 0000000..6eb8301 --- /dev/null +++ b/src/test/java/xyz/etztech/embed/EmbedTest.java @@ -0,0 +1,25 @@ +package xyz.etztech.embed; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.util.Arrays; + +public class EmbedTest { + + @Test + void TestEmbed() { + Embed embed = new Embed() + .color(0x151515) + .author(new Author("Etzelia", "https://etzel.ia", "" ,"")) + .title("Test \"Title\"") + .description("Test Description") + .fields(Arrays.asList( + new Field("Field1", "foo"), + new Field("Field2", "bar"), + new Field("Field3", "baz", true) + )); + String expected = "{\"embed\":{\"title\":\"Test \\\"Title\\\"\",\"description\":\"Test Description\",\"color\":\"1381653\",\"author\":{\"name\":\"Etzelia\",\"url\":\"https://etzel.ia\"},\"fields\":[{\"name\":\"Field1\",\"value\":\"foo\"},{\"name\":\"Field2\",\"value\":\"bar\"},{\"name\":\"Field3\",\"value\":\"baz\",\"inline\":true}]}}"; + Assertions.assertEquals(expected, embed.toString()); + } +}