From 902459e2ed9a4d125c4ad82b8dd5bd13f27bffde Mon Sep 17 00:00:00 2001 From: William Orlopp Date: Sun, 17 Nov 2024 00:31:47 -0800 Subject: [PATCH] Added Mutual Birthday Guilds --- .gitignore | 133 ++++++++++++++ api/.gitignore | 133 ++++++++++++++ bot/.gitignore | 3 + .../com/beanbeanjuice/cafebot/CafeBot.java | 16 +- .../birthday/self/BirthdaySetSubCommand.java | 2 +- .../listeners/MutualGuildsListener.java | 24 +++ .../sections/fun/birthday/BirthdayHelper.java | 24 ++- .../cafeapi/wrapper/CafeAPI.java | 2 +- .../users/DonationUsersEndpoint.java | 2 +- .../wrapper/endpoints/birthdays/Birthday.java | 7 +- .../birthdays/BirthdaysEndpoint.java | 30 ++-- .../cafeapi/beta/BirthdaysEndpointTests.java | 170 ++++++++---------- .../cafeapi/beta/CafeUsersEndpointTests.java | 38 +++- .../beta/DonationUsersEndpointTests.java | 37 +++- .../beta/InteractionsEndpointTests.java | 2 +- .../beanbeanjuice/classes/BirthdayTest.java | 12 +- 16 files changed, 472 insertions(+), 163 deletions(-) create mode 100644 .gitignore create mode 100644 api/.gitignore create mode 100644 bot/src/main/java/com/beanbeanjuice/cafebot/utility/listeners/MutualGuildsListener.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..e38aa84b --- /dev/null +++ b/.gitignore @@ -0,0 +1,133 @@ +# Node/NPM +/node_modules/ + +# Latex Files +*.aux +*.fdb_latexmk +*.fls +*.synctex.gz + +# Compiled class file +*.class + +# Log file +*.log + +# BlueJ files +*.ctxt + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.nar +*.ear +*.zip +*.tar.gz +*.rar + +# C Files +*.o +*.out + +# Maven +/target/ + +# Gradle +/build/ +/.gradle/ +/out/ +gradle.properties + +# JetBrains IDEA +/.idea/ +/.fleet/ +*.iml +*.ipr +*.iws + +# VSCode +.vscode/ + +# Eclipse +.classpath +.project +.settings/ +/bin/ + +# Files containing safety info +.env +beta.json +release.json + +# Logging Stuff +/logs/ + +# Mac Stuff +.DS_Store + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* + +# GITIGNORE FOR C PROJECTS +# Prerequisites +*.d + +# Object files +*.o +*.ko +*.obj +*.elf + +# Linker output +*.ilk +*.map +*.exp + +# Precompiled Headers +*.gch +*.pch + +# Libraries +*.lib +*.a +*.la +*.lo + +# Shared objects (inc. Windows DLLs) +*.dll +*.so +*.so.* +*.dylib + +# Executables +*.exe +*.out +*.app +*.i*86 +*.x86_64 +*.hex + +# Debug files +*.dSYM/ +*.su +*.idb +*.pdb +/cmake-build-debug/ + +# Kernel Module Compile Results +*.mod* +*.cmd +.tmp_versions/ +modules.order +Module.symvers +Mkfile.old +dkms.conf + +# Rust Stuff +debug/ +target/ +**/*.rs.bk +*.pdb diff --git a/api/.gitignore b/api/.gitignore new file mode 100644 index 00000000..e38aa84b --- /dev/null +++ b/api/.gitignore @@ -0,0 +1,133 @@ +# Node/NPM +/node_modules/ + +# Latex Files +*.aux +*.fdb_latexmk +*.fls +*.synctex.gz + +# Compiled class file +*.class + +# Log file +*.log + +# BlueJ files +*.ctxt + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.nar +*.ear +*.zip +*.tar.gz +*.rar + +# C Files +*.o +*.out + +# Maven +/target/ + +# Gradle +/build/ +/.gradle/ +/out/ +gradle.properties + +# JetBrains IDEA +/.idea/ +/.fleet/ +*.iml +*.ipr +*.iws + +# VSCode +.vscode/ + +# Eclipse +.classpath +.project +.settings/ +/bin/ + +# Files containing safety info +.env +beta.json +release.json + +# Logging Stuff +/logs/ + +# Mac Stuff +.DS_Store + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* + +# GITIGNORE FOR C PROJECTS +# Prerequisites +*.d + +# Object files +*.o +*.ko +*.obj +*.elf + +# Linker output +*.ilk +*.map +*.exp + +# Precompiled Headers +*.gch +*.pch + +# Libraries +*.lib +*.a +*.la +*.lo + +# Shared objects (inc. Windows DLLs) +*.dll +*.so +*.so.* +*.dylib + +# Executables +*.exe +*.out +*.app +*.i*86 +*.x86_64 +*.hex + +# Debug files +*.dSYM/ +*.su +*.idb +*.pdb +/cmake-build-debug/ + +# Kernel Module Compile Results +*.mod* +*.cmd +.tmp_versions/ +modules.order +Module.symvers +Mkfile.old +dkms.conf + +# Rust Stuff +debug/ +target/ +**/*.rs.bk +*.pdb diff --git a/bot/.gitignore b/bot/.gitignore index 5d77f25a..95a3f0b0 100644 --- a/bot/.gitignore +++ b/bot/.gitignore @@ -2,6 +2,9 @@ ai.json menu.json +# IntelliJ +./.idea/ + # Compiled class file *.class diff --git a/bot/src/main/java/com/beanbeanjuice/cafebot/CafeBot.java b/bot/src/main/java/com/beanbeanjuice/cafebot/CafeBot.java index dafd944e..d0553bf8 100644 --- a/bot/src/main/java/com/beanbeanjuice/cafebot/CafeBot.java +++ b/bot/src/main/java/com/beanbeanjuice/cafebot/CafeBot.java @@ -56,8 +56,6 @@ import net.dv8tion.jda.api.requests.RestAction; import net.dv8tion.jda.api.sharding.DefaultShardManagerBuilder; import net.dv8tion.jda.api.sharding.ShardManager; -import net.dv8tion.jda.api.utils.ChunkingFilter; -import net.dv8tion.jda.api.utils.MemberCachePolicy; import org.apache.hc.client5.http.async.methods.SimpleHttpRequest; import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient; import org.apache.hc.client5.http.impl.async.HttpAsyncClients; @@ -130,8 +128,8 @@ public CafeBot() throws InterruptedException { GatewayIntent.DIRECT_MESSAGES, GatewayIntent.MESSAGE_CONTENT ) - .setMemberCachePolicy(MemberCachePolicy.ALL) // ! - Needed for mutual guilds // TODO: REMOVE? - .setChunkingFilter(ChunkingFilter.ALL) // ! - Needed for mutual guilds // TODO: REMOVE? +// .setMemberCachePolicy(MemberCachePolicy.ALL) // ! - Needed for mutual guilds // TODO: REMOVE? +// .setChunkingFilter(ChunkingFilter.ALL) // ! - Needed for mutual guilds // TODO: REMOVE? .build(); logger.log(CafeBot.class, LogLevel.INFO, "Checking servers..."); @@ -331,7 +329,9 @@ private void setupListeners() { new GoodbyeListener(this), new WelcomeMessageModalListener(this.cafeAPI.getWelcomesEndpoint()), - new GoodbyeMessageModalListener(this.cafeAPI.getGoodbyesEndpoint()) + new GoodbyeMessageModalListener(this.cafeAPI.getGoodbyesEndpoint()), + + new MutualGuildsListener(this.cafeAPI.getMutualGuildsEndpoint()) ); } @@ -371,19 +371,21 @@ private void startBioUpdateTimer() { TimerTask timerTask = new TimerTask() { @Override public void run() { - int num = Helper.getRandomInteger(1, 4); + int num = Helper.getRandomInteger(1, 4); // (Inclusive, Exclusive) String finalString = ""; switch (num) { case 1 -> finalString = "Currently in " + getTotalServers() + " cafés!"; case 2 -> finalString = "Waiting " + getTotalChannels() + " tables!"; case 3 -> finalString = "Serving " + getTotalUsers() + " customers!"; + case 4 -> finalString = "Hmmm... I really want to go on break but my boss will get angry..."; + case 5 -> finalString = "Wow... I had to deal with " + commandsRun + " orders today..."; } bot.getShardManager().setActivity(Activity.customStatus(initialString + finalString)); } }; - timer.scheduleAtFixedRate(timerTask, 0, TimeUnit.MINUTES.toMillis(1)); + timer.scheduleAtFixedRate(timerTask, 0, TimeUnit.MINUTES.toMillis(10)); } public MessageEmbed getUpdateEmbed(final double gatewayPing) { diff --git a/bot/src/main/java/com/beanbeanjuice/cafebot/commands/fun/birthday/self/BirthdaySetSubCommand.java b/bot/src/main/java/com/beanbeanjuice/cafebot/commands/fun/birthday/self/BirthdaySetSubCommand.java index 25f65231..07ba301e 100644 --- a/bot/src/main/java/com/beanbeanjuice/cafebot/commands/fun/birthday/self/BirthdaySetSubCommand.java +++ b/bot/src/main/java/com/beanbeanjuice/cafebot/commands/fun/birthday/self/BirthdaySetSubCommand.java @@ -55,7 +55,7 @@ public void handle(SlashCommandInteractionEvent event) { private void updateUserBirthday(final BirthdayMonth month, final int day, final TimeZone timeZone, final SlashCommandInteractionEvent event) { User user = event.getUser(); - Birthday birthday = new Birthday(month, day, timeZone.getID(), false); + Birthday birthday = new Birthday(month, day, timeZone.getID()); BirthdaysEndpoint birthdaysEndpoint = cafeBot.getCafeAPI().getBirthdaysEndpoint(); birthdaysEndpoint.createUserBirthday(user.getId(), birthday) diff --git a/bot/src/main/java/com/beanbeanjuice/cafebot/utility/listeners/MutualGuildsListener.java b/bot/src/main/java/com/beanbeanjuice/cafebot/utility/listeners/MutualGuildsListener.java new file mode 100644 index 00000000..933e7413 --- /dev/null +++ b/bot/src/main/java/com/beanbeanjuice/cafebot/utility/listeners/MutualGuildsListener.java @@ -0,0 +1,24 @@ +package com.beanbeanjuice.cafebot.utility.listeners; + +import com.beanbeanjuice.cafeapi.wrapper.endpoints.mutualguilds.MutualGuildsEndpoint; +import lombok.RequiredArgsConstructor; +import net.dv8tion.jda.api.events.message.MessageReceivedEvent; +import net.dv8tion.jda.api.hooks.ListenerAdapter; + +@RequiredArgsConstructor +public class MutualGuildsListener extends ListenerAdapter { + + private final MutualGuildsEndpoint mutualGuildsEndpoint; + + @Override + public void onMessageReceived(MessageReceivedEvent event) { + if (!event.isFromGuild()) return; + if (event.getAuthor().isBot()) return; + + String guildID = event.getGuild().getId(); + String userID = event.getAuthor().getId(); + + this.mutualGuildsEndpoint.addMutualGuild(userID, guildID); + } + +} diff --git a/bot/src/main/java/com/beanbeanjuice/cafebot/utility/sections/fun/birthday/BirthdayHelper.java b/bot/src/main/java/com/beanbeanjuice/cafebot/utility/sections/fun/birthday/BirthdayHelper.java index 700579c7..748a4736 100644 --- a/bot/src/main/java/com/beanbeanjuice/cafebot/utility/sections/fun/birthday/BirthdayHelper.java +++ b/bot/src/main/java/com/beanbeanjuice/cafebot/utility/sections/fun/birthday/BirthdayHelper.java @@ -69,13 +69,25 @@ private void sendBirthdayWish(final String userID, final Birthday birthday) { ).queue(); }); - user.getMutualGuilds().forEach((guild) -> { - this.cafeBot.getLogger().log(BirthdayHelper.class, LogLevel.INFO, String.format("Sending Birthday To Guild: %s", guild.getId()), true, false); + this.cafeBot.getCafeAPI() + .getMutualGuildsEndpoint() + .getMutualGuilds(user.getId()) + .thenAccept((mutualGuildIDs) -> sendBirthdayMessageToEveryServer(mutualGuildIDs, user, birthday)); + }); + } - cafeBot.getCafeAPI().getGuildsEndpoint().getGuildInformation(guild.getId()).thenAcceptAsync((guildInformation) -> { - handleGuildBirthday(guild, guildInformation.getSetting(GuildInformationType.BIRTHDAY_CHANNEL_ID), user, birthday); - }); - }); + private void sendBirthdayMessageToEveryServer(final List guildIDs, final User user, final Birthday birthday) { + guildIDs.forEach((guildID) -> { + Optional guildOptional = Optional.ofNullable(cafeBot.getShardManager().getGuildById(guildID)); + if (guildOptional.isEmpty()) return; + + Guild guild = guildOptional.get(); + + this.cafeBot.getLogger().log(BirthdayHelper.class, LogLevel.INFO, String.format("Sending Birthday To Guild: %s", guild.getId()), true, false); + this.cafeBot.getCafeAPI() + .getGuildsEndpoint() + .getGuildInformation(guild.getId()) + .thenAcceptAsync((guildInformation) -> handleGuildBirthday(guild, guildInformation.getSetting(GuildInformationType.BIRTHDAY_CHANNEL_ID), user, birthday)); }); } diff --git a/bot/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/CafeAPI.java b/bot/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/CafeAPI.java index 4d47f101..1164c625 100644 --- a/bot/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/CafeAPI.java +++ b/bot/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/CafeAPI.java @@ -24,6 +24,7 @@ import com.beanbeanjuice.cafeapi.wrapper.endpoints.words.WordsEndpoint; import lombok.Getter; +import java.util.TimeZone; import java.util.Timer; import java.util.TimerTask; import java.util.concurrent.TimeUnit; @@ -66,7 +67,6 @@ public class CafeAPI { * @param requestLocation The {@link RequestLocation requestLocation}. */ public CafeAPI(String username, String password, RequestLocation requestLocation) { -// TimeZone.setDefault(TimeZone.getTimeZone("UTC")); this.userAgent = username; CafeAPI.requestLocation = requestLocation; diff --git a/bot/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/endpoints/beancoins/users/DonationUsersEndpoint.java b/bot/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/endpoints/beancoins/users/DonationUsersEndpoint.java index a1b29148..bd4b33ae 100644 --- a/bot/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/endpoints/beancoins/users/DonationUsersEndpoint.java +++ b/bot/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/endpoints/beancoins/users/DonationUsersEndpoint.java @@ -43,7 +43,7 @@ public CompletableFuture> getUserDonationTime(final String u .setRoute("/beanCoin/donation_users/" + userID) .setAuthorization(apiKey) .buildAsync() - .thenApplyAsync((request) -> CafeGeneric.parseTimestampFromAPI(request.getData().get("user").get("time_until_next_donation").asText())); + .thenApplyAsync((request) -> CafeGeneric.parseTimestampFromAPI(request.getData().get("time_until_next_donation").asText())); } public CompletableFuture addDonationUser(final String userID, final Timestamp timeUntilNextDonation) { diff --git a/bot/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/endpoints/birthdays/Birthday.java b/bot/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/endpoints/birthdays/Birthday.java index 877563ad..78215742 100644 --- a/bot/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/endpoints/birthdays/Birthday.java +++ b/bot/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/endpoints/birthdays/Birthday.java @@ -6,7 +6,6 @@ import lombok.Getter; import java.text.ParseException; -import java.time.Month; import java.time.Year; import java.util.Calendar; import java.util.Date; @@ -24,9 +23,8 @@ public class Birthday { @Getter private final BirthdayMonth month; @Getter private final int day; @Getter private final TimeZone timeZone; - @Getter private final boolean isAlreadyMentioned; - public Birthday(final BirthdayMonth month, final int day, final String timeZone, final boolean isAlreadyMentioned) { + public Birthday(final BirthdayMonth month, final int day, final String timeZone) { this.month = month; this.day = day; @@ -40,7 +38,6 @@ public Birthday(final BirthdayMonth month, final int day, final String timeZone, throw new InvalidTimeZoneException("The timezone specified is not allowed!"); this.timeZone = TimeZone.getTimeZone(timeZone); - this.isAlreadyMentioned = isAlreadyMentioned; } public Date getDate() throws ParseException { @@ -64,7 +61,7 @@ public boolean isBirthday() { } } - public static String convertToBirthdayDateString(final BirthdayMonth month, final int day, final int year) { + private static String convertToBirthdayDateString(final BirthdayMonth month, final int day, final int year) { return String.format("%s-%s-%d", month.getMonthNumber(), day, year); } diff --git a/bot/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/endpoints/birthdays/BirthdaysEndpoint.java b/bot/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/endpoints/birthdays/BirthdaysEndpoint.java index 30aa8686..d68a9ae5 100644 --- a/bot/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/endpoints/birthdays/BirthdaysEndpoint.java +++ b/bot/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/endpoints/birthdays/BirthdaysEndpoint.java @@ -9,6 +9,7 @@ import com.beanbeanjuice.cafeapi.wrapper.utility.Time; import com.fasterxml.jackson.databind.JsonNode; +import java.time.Year; import java.util.*; import java.util.concurrent.CompletableFuture; @@ -17,10 +18,12 @@ public class BirthdaysEndpoint extends CafeEndpoint { private HashMap requestToBirthdayMap(Request request) { HashMap birthdays = new HashMap<>(); - request.getData().get("birthdays").forEach((birthdayNode) -> { + JsonNode birthdaysNode = request.getData().get("birthdays"); + + for (JsonNode birthdayNode : birthdaysNode) { String userID = birthdayNode.get("user_id").asText(); parseBirthday(birthdayNode).ifPresent((birthday -> birthdays.put(userID, birthday))); - }); + } return birthdays; } @@ -47,29 +50,20 @@ public CompletableFuture updateUserBirthday(final String userID, final return RequestBuilder.create(RequestRoute.CAFEBOT, RequestType.PATCH) .setRoute("/birthdays/" + userID) - .addParameter("birthday", getBirthdayString(month, day)) + .addParameter("birth_date", getBirthdayString(month, day)) .addParameter("time_zone", birthday.getTimeZone().getID()) .setAuthorization(apiKey) .buildAsync() .thenApplyAsync((request) -> request.getStatusCode() == 200); } - public CompletableFuture updateUserBirthdayMention(final String userID, final boolean alreadyMentioned) { - return RequestBuilder.create(RequestRoute.CAFEBOT, RequestType.PATCH) - .setRoute("/birthdays/" + userID + "/mention") - .addParameter("already_mentioned", String.valueOf(alreadyMentioned)) - .setAuthorization(apiKey) - .buildAsync() - .thenApplyAsync((request) -> request.getStatusCode() == 200); - } - public CompletableFuture createUserBirthday(final String userID, final Birthday birthday) { BirthdayMonth month = birthday.getMonth(); int day = birthday.getDay(); return RequestBuilder.create(RequestRoute.CAFEBOT, RequestType.POST) .setRoute("/birthdays/" + userID) - .addParameter("birthday", getBirthdayString(month, day)) + .addParameter("birth_date", getBirthdayString(month, day)) .addParameter("time_zone", birthday.getTimeZone().getID()) .setAuthorization(apiKey) .buildAsync() @@ -87,16 +81,12 @@ public CompletableFuture removeUserBirthday(final String userID) { private Optional parseBirthday(final JsonNode birthday) { String unformattedDate = birthday.get("birth_date").asText(); String timeZoneString = birthday.get("time_zone").asText(); - boolean alreadyMentioned = birthday.get("already_mentioned").asBoolean(); try { - Date date = Time.getFullDate(unformattedDate + "-2020", TimeZone.getTimeZone(timeZoneString)); - Calendar calendar = Calendar.getInstance(); - calendar.setTime(date); - int month = calendar.get(Calendar.MONTH) + 1; - int day = calendar.get(Calendar.DAY_OF_MONTH); + int month = Integer.parseInt(unformattedDate.split("-")[0]); + int day = Integer.parseInt(unformattedDate.split("-")[1]); - return Optional.of(new Birthday(getBirthdayMonth(month), day, timeZoneString, alreadyMentioned)); + return Optional.of(new Birthday(getBirthdayMonth(month), day, timeZoneString)); } catch (Exception e) { return Optional.empty(); } diff --git a/bot/wrappers/cafe-api-wrapper/src/test/java/com/beanbeanjuice/cafeapi/beta/BirthdaysEndpointTests.java b/bot/wrappers/cafe-api-wrapper/src/test/java/com/beanbeanjuice/cafeapi/beta/BirthdaysEndpointTests.java index e4c01004..0dc486fe 100644 --- a/bot/wrappers/cafe-api-wrapper/src/test/java/com/beanbeanjuice/cafeapi/beta/BirthdaysEndpointTests.java +++ b/bot/wrappers/cafe-api-wrapper/src/test/java/com/beanbeanjuice/cafeapi/beta/BirthdaysEndpointTests.java @@ -7,10 +7,9 @@ import com.beanbeanjuice.cafeapi.wrapper.exception.api.NotFoundException; import com.beanbeanjuice.cafeapi.wrapper.exception.api.TeaPotException; import com.beanbeanjuice.cafeapi.wrapper.exception.program.BirthdayOverfillException; +import com.beanbeanjuice.cafeapi.wrapper.exception.program.InvalidTimeZoneException; import com.beanbeanjuice.cafeapi.wrapper.requests.RequestLocation; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.*; import java.util.Optional; import java.util.TimeZone; @@ -18,126 +17,99 @@ public class BirthdaysEndpointTests { - @Test - @DisplayName("Birthdays Endpoint Test") - public void testBirthdaysEndpoint() throws ExecutionException, InterruptedException { - CafeAPI cafeAPI = new CafeAPI("beanbeanjuice", System.getenv("API_PASSWORD"), RequestLocation.BETA); + private static CafeAPI cafeAPI; + + @BeforeAll + @DisplayName("Login to CafeAPI") + public static void login() { + cafeAPI = new CafeAPI("beanbeanjuice", System.getenv("API_PASSWORD"), RequestLocation.BETA); + } - // Makes sure the user's birthday doesn't exist before starting. + @BeforeEach + public void deleteUserBirthday() throws ExecutionException, InterruptedException { Assertions.assertTrue(cafeAPI.getBirthdaysEndpoint().removeUserBirthday("178272524533104642").get()); + } - // Makes sure the user's birthday cannot be found. + @Test + @DisplayName("Make Sure User Birthday Does Not Already Exist") + public void testIfBirthdayDoesNotExist() { try { cafeAPI.getBirthdaysEndpoint().getUserBirthday("178272524533104642").get(); Assertions.fail(); } catch (Exception e) { Assertions.assertInstanceOf(NotFoundException.class, e.getCause()); } + } + + @Test + @DisplayName("Test Birthday Creation") + public void testBirthdayCreation() throws ExecutionException, InterruptedException { + Assertions.assertTrue(cafeAPI.getBirthdaysEndpoint().createUserBirthday("178272524533104642", new Birthday(BirthdayMonth.DECEMBER, 31, "EST")).get()); + } - // Makes sure the user's birthday can be created. - Assertions.assertTrue(cafeAPI.getBirthdaysEndpoint().createUserBirthday("178272524533104642", new Birthday(BirthdayMonth.DECEMBER, 31, "EST", false)).get()); + @Test + @DisplayName("Test Birthday Duplication") + public void testBirthdayDuplication() throws ExecutionException, InterruptedException { + testBirthdayCreation(); - // Makes sure the user's birthday cannot be duplicated. try { - cafeAPI.getBirthdaysEndpoint().createUserBirthday("178272524533104642", new Birthday(BirthdayMonth.DECEMBER, 20, "EST", false)).get(); + cafeAPI.getBirthdaysEndpoint().createUserBirthday("178272524533104642", new Birthday(BirthdayMonth.DECEMBER, 20, "EST")).get(); Assertions.fail(); } catch (Exception e) { Assertions.assertInstanceOf(ConflictException.class, e.getCause()); } + } + + @Test + @DisplayName("Make Sure Created Birthday Matches Expected Value") + public void testExpectedValue() throws ExecutionException, InterruptedException { + testBirthdayCreation(); + + Optional optionalBirthday = cafeAPI.getBirthdaysEndpoint().getUserBirthday("178272524533104642").get(); + + Assertions.assertTrue(optionalBirthday.isPresent()); + Birthday birthday = optionalBirthday.get(); + + Assertions.assertEquals(birthday.getMonth(), BirthdayMonth.DECEMBER); + Assertions.assertEquals(birthday.getDay(), 31); + Assertions.assertEquals(birthday.getTimeZone(), TimeZone.getTimeZone("EST")); + } - // Makes sure the month is the same. - Assertions.assertEquals(BirthdayMonth.DECEMBER, cafeAPI.getBirthdaysEndpoint().getAllBirthdays().get().get("178272524533104642").getMonth()); + @Test + @DisplayName("Invalid Parameters Test") + public void testInvalidParameters() throws ExecutionException, InterruptedException { + // Makes sure the proper exception is thrown when there are more days than in the month. + Assertions.assertThrows(BirthdayOverfillException.class, () -> cafeAPI.getBirthdaysEndpoint().updateUserBirthday("178272524533104642", new Birthday(BirthdayMonth.FEBRUARY, 30, "EST")).get()); - // Makes sure the date is the same. - Assertions.assertTrue(() -> { - Optional optionalBirthday = null; - try { - optionalBirthday = cafeAPI.getBirthdaysEndpoint().getUserBirthday("178272524533104642").get(); - } catch (Exception e) { - Assertions.fail(); - } - Assertions.assertNotNull(optionalBirthday); - Assertions.assertTrue(optionalBirthday.isPresent()); - return optionalBirthday.get().getDay() == 31; - }); + // Makes sure the proper exception is thrown when the incorrect month is set. + Assertions.assertThrows(TeaPotException.class, () -> cafeAPI.getBirthdaysEndpoint().updateUserBirthday("178272524533104642", new Birthday(BirthdayMonth.ERROR, 15, "EST")).get()); - // Makes sure a TeaPotException is thrown when there are more days than in the month. - Assertions.assertThrows(BirthdayOverfillException.class, () -> cafeAPI.getBirthdaysEndpoint().updateUserBirthday("178272524533104642", new Birthday(BirthdayMonth.FEBRUARY, 30, "EST", false)).get()); + // Makes sure the proper exception is thrown when the incorrect timezone is set. + Assertions.assertThrows(InvalidTimeZoneException.class, () -> cafeAPI.getBirthdaysEndpoint().updateUserBirthday("178272524533104642", new Birthday(BirthdayMonth.FEBRUARY, 15, "BURGER")).get()); + } - // Makes sure only a valid month can be set - Assertions.assertThrows(TeaPotException.class, () -> cafeAPI.getBirthdaysEndpoint().updateUserBirthday("178272524533104642", new Birthday(BirthdayMonth.ERROR, 15, "EST", false)).get()); + @Test + @DisplayName("Confirm Birthday Can Change") + public void testBirthdayChange() throws ExecutionException, InterruptedException { + testBirthdayCreation(); // Makes sure the birthday can be changed. - Assertions.assertTrue(cafeAPI.getBirthdaysEndpoint().updateUserBirthday("178272524533104642", new Birthday(BirthdayMonth.FEBRUARY, 29, "UTC", false)).get()); - - // Makes sure the changed month is the same. - Assertions.assertTrue(() -> { - Optional optionalBirthday = null; - try { - optionalBirthday = cafeAPI.getBirthdaysEndpoint().getUserBirthday("178272524533104642").get(); - } catch (Exception e) { - Assertions.fail(); - } - Assertions.assertNotNull(optionalBirthday); - Assertions.assertTrue(optionalBirthday.isPresent()); - return optionalBirthday.get().getMonth() == BirthdayMonth.FEBRUARY; - }); - - // Makes sure the changed day is the same. - { - TimeZone oldTimeZone = TimeZone.getDefault(); - TimeZone.setDefault(TimeZone.getTimeZone("UTC")); - Optional optionalBirthday = null; - try { - optionalBirthday = cafeAPI.getBirthdaysEndpoint().getUserBirthday("178272524533104642").get(); - } catch (Exception e) { - Assertions.fail(); - } - Assertions.assertNotNull(optionalBirthday); - Assertions.assertTrue(optionalBirthday.isPresent()); - Assertions.assertEquals(optionalBirthday.get().getDay(), 29); - TimeZone.setDefault(oldTimeZone); - } + Assertions.assertTrue(cafeAPI.getBirthdaysEndpoint().updateUserBirthday("178272524533104642", new Birthday(BirthdayMonth.FEBRUARY, 29, "UTC")).get()); + } - // Makes sure that alreadyMentioned is false by default. - Assertions.assertFalse(() -> { - Optional optionalBirthday = null; - try { - optionalBirthday = cafeAPI.getBirthdaysEndpoint().getUserBirthday("178272524533104642").get(); - } catch (Exception e) { - Assertions.fail(); - } - Assertions.assertNotNull(optionalBirthday); - Assertions.assertTrue(optionalBirthday.isPresent()); - return optionalBirthday.get().isAlreadyMentioned(); - }); - - // Makes sure alreadyMentioned can be updated. - Assertions.assertTrue(cafeAPI.getBirthdaysEndpoint().updateUserBirthdayMention("178272524533104642", true).get()); - - // Makes sure alreadyMentioned HAS updated. - Assertions.assertTrue(() -> { - Optional optionalBirthday = null; - try { - optionalBirthday = cafeAPI.getBirthdaysEndpoint().getUserBirthday("178272524533104642").get(); - } catch (Exception e) { - Assertions.fail(); - } - Assertions.assertNotNull(optionalBirthday); - Assertions.assertTrue(optionalBirthday.isPresent()); - return optionalBirthday.get().isAlreadyMentioned(); - }); - - // Makes sure the user's birthday can be removed. - Assertions.assertTrue(cafeAPI.getBirthdaysEndpoint().removeUserBirthday("178272524533104642").get()); + @Test + @DisplayName("Confirm Changed Expected Values") + public void confirmChangedValues() throws ExecutionException, InterruptedException { + testBirthdayChange(); - // Makes sure the user no longer exists. - try { - cafeAPI.getBirthdaysEndpoint().getUserBirthday("178272524533104642").get(); - Assertions.fail(); - } catch (Exception e) { - Assertions.assertInstanceOf(NotFoundException.class, e.getCause()); - } + Optional optionalBirthday = cafeAPI.getBirthdaysEndpoint().getUserBirthday("178272524533104642").get(); + + Assertions.assertTrue(optionalBirthday.isPresent()); + Birthday birthday = optionalBirthday.get(); + + Assertions.assertEquals(birthday.getMonth(), BirthdayMonth.FEBRUARY); + Assertions.assertEquals(birthday.getDay(), 29); + Assertions.assertEquals(birthday.getTimeZone(), TimeZone.getTimeZone("UTC")); } } diff --git a/bot/wrappers/cafe-api-wrapper/src/test/java/com/beanbeanjuice/cafeapi/beta/CafeUsersEndpointTests.java b/bot/wrappers/cafe-api-wrapper/src/test/java/com/beanbeanjuice/cafeapi/beta/CafeUsersEndpointTests.java index 29f26446..52ed1c5c 100644 --- a/bot/wrappers/cafe-api-wrapper/src/test/java/com/beanbeanjuice/cafeapi/beta/CafeUsersEndpointTests.java +++ b/bot/wrappers/cafe-api-wrapper/src/test/java/com/beanbeanjuice/cafeapi/beta/CafeUsersEndpointTests.java @@ -8,23 +8,31 @@ import com.beanbeanjuice.cafeapi.wrapper.exception.api.TeaPotException; import com.beanbeanjuice.cafeapi.wrapper.generic.CafeGeneric; import com.beanbeanjuice.cafeapi.wrapper.requests.RequestLocation; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.*; import java.sql.Timestamp; import java.util.concurrent.ExecutionException; public class CafeUsersEndpointTests { - @Test - @DisplayName("Cafe Users Endpoint Test") - public void testCafeUsersEndpoint() throws ExecutionException, InterruptedException { - CafeAPI cafeAPI = new CafeAPI("beanbeanjuice", System.getenv("API_PASSWORD"), RequestLocation.BETA); + private static CafeAPI cafeAPI; + + @BeforeAll + @DisplayName("Login to CafeAPI") + public static void login() { + cafeAPI = new CafeAPI("beanbeanjuice", System.getenv("API_PASSWORD"), RequestLocation.BETA); + } + @BeforeEach + @DisplayName("Delete User") + public void deleteUser() throws ExecutionException, InterruptedException { // Makes sure the user does not exist beforehand. Assertions.assertTrue(cafeAPI.getCafeUsersEndpoint().deleteCafeUser("236654580300120064").get()); + } + @Test + @DisplayName("Make Sure User Does Not Exist") + public void confirmUserAbsence() { // Makes sure the user does not exist when trying to get it. try { cafeAPI.getCafeUsersEndpoint().getCafeUser("236654580300120064").get(); @@ -32,9 +40,19 @@ public void testCafeUsersEndpoint() throws ExecutionException, InterruptedExcept } catch (Exception e) { Assertions.assertInstanceOf(NotFoundException.class, e.getCause()); } + } + @Test + @DisplayName("Make Sure User Can Be Created") + public void createUser() throws ExecutionException, InterruptedException { // Creates the user. Assertions.assertTrue(cafeAPI.getCafeUsersEndpoint().createCafeUser("236654580300120064").get()); + } + + @Test + @DisplayName("Test Duplicate User Fail") + public void testDuplicateUserFail() throws ExecutionException, InterruptedException { + createUser(); // Makes sure the user cannot be created twice. try { @@ -43,6 +61,12 @@ public void testCafeUsersEndpoint() throws ExecutionException, InterruptedExcept } catch (Exception e) { Assertions.assertInstanceOf(ConflictException.class, e.getCause()); } + } + + @Test + @DisplayName("Cafe Users Endpoint Test") + public void testCafeUsersEndpoint() throws ExecutionException, InterruptedException { + createUser(); // Makes sure the user exists in the array list. Assertions.assertTrue(() -> { diff --git a/bot/wrappers/cafe-api-wrapper/src/test/java/com/beanbeanjuice/cafeapi/beta/DonationUsersEndpointTests.java b/bot/wrappers/cafe-api-wrapper/src/test/java/com/beanbeanjuice/cafeapi/beta/DonationUsersEndpointTests.java index 303244da..e3c95838 100644 --- a/bot/wrappers/cafe-api-wrapper/src/test/java/com/beanbeanjuice/cafeapi/beta/DonationUsersEndpointTests.java +++ b/bot/wrappers/cafe-api-wrapper/src/test/java/com/beanbeanjuice/cafeapi/beta/DonationUsersEndpointTests.java @@ -5,29 +5,46 @@ import com.beanbeanjuice.cafeapi.wrapper.exception.api.NotFoundException; import com.beanbeanjuice.cafeapi.wrapper.generic.CafeGeneric; import com.beanbeanjuice.cafeapi.wrapper.requests.RequestLocation; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.*; import java.sql.Timestamp; import java.util.concurrent.ExecutionException; public class DonationUsersEndpointTests { - @Test - @DisplayName("Donation Users Endpoint Test") - public void testDonationUsersEndpoint() throws ExecutionException, InterruptedException { - CafeAPI cafeAPI = new CafeAPI("beanbeanjuice", System.getenv("API_PASSWORD"), RequestLocation.BETA); + private static CafeAPI cafeAPI; + + @BeforeAll + public static void login() { + cafeAPI = new CafeAPI("beanbeanjuice", System.getenv("API_PASSWORD"), RequestLocation.BETA); + } + @BeforeEach + public void deleteUser() throws ExecutionException, InterruptedException { Assertions.assertTrue(cafeAPI.getDonationUsersEndpoint().deleteDonationUser("738590591767543921").get()); + } + @Test + @DisplayName("Make Sure Donation User Does Not Exist") + public void testAbsence() { try { cafeAPI.getDonationUsersEndpoint().getUserDonationTime("738590591767543921").get(); Assertions.fail(); } catch (Exception e) { Assertions.assertInstanceOf(NotFoundException.class, e.getCause()); } + } + + @Test + @DisplayName("Make Sure Donation User Can Be Created") + public void testCreateDonationUser() throws ExecutionException, InterruptedException { + Timestamp currentTimestamp = CafeGeneric.parseTimestamp((new Timestamp(System.currentTimeMillis())).toString()).orElse(null); + Assertions.assertTrue(cafeAPI.getDonationUsersEndpoint().addDonationUser("738590591767543921", currentTimestamp).get()); + } + @Test + @DisplayName("Make Sure Donation User Time Exists") + public void testCreateDonationUserTime() throws ExecutionException, InterruptedException { Timestamp currentTimestamp = CafeGeneric.parseTimestamp((new Timestamp(System.currentTimeMillis())).toString()).orElse(null); Assertions.assertTrue(cafeAPI.getDonationUsersEndpoint().addDonationUser("738590591767543921", currentTimestamp).get()); @@ -39,9 +56,11 @@ public void testDonationUsersEndpoint() throws ExecutionException, InterruptedEx } Assertions.assertEquals(currentTimestamp, cafeAPI.getDonationUsersEndpoint().getAllUserDonationTimes().get().get("738590591767543921")); Assertions.assertEquals(currentTimestamp, cafeAPI.getDonationUsersEndpoint().getUserDonationTime("738590591767543921").get().orElse(null)); + } - Assertions.assertTrue(cafeAPI.getDonationUsersEndpoint().deleteDonationUser("738590591767543921").get()); - + @Test + @DisplayName("Confirm Exception After Deletion") + public void testDonationUsersEndpoint() { try { cafeAPI.getDonationUsersEndpoint().getUserDonationTime("738590591767543921").get(); Assertions.fail(); diff --git a/bot/wrappers/cafe-api-wrapper/src/test/java/com/beanbeanjuice/cafeapi/beta/InteractionsEndpointTests.java b/bot/wrappers/cafe-api-wrapper/src/test/java/com/beanbeanjuice/cafeapi/beta/InteractionsEndpointTests.java index d5f256a9..4f3dec7d 100644 --- a/bot/wrappers/cafe-api-wrapper/src/test/java/com/beanbeanjuice/cafeapi/beta/InteractionsEndpointTests.java +++ b/bot/wrappers/cafe-api-wrapper/src/test/java/com/beanbeanjuice/cafeapi/beta/InteractionsEndpointTests.java @@ -18,7 +18,7 @@ public class InteractionsEndpointTests { public void testInteractionSendersEndpoint() throws ExecutionException, InterruptedException { CafeAPI cafeAPI = new CafeAPI("beanbeanjuice", System.getenv("API_PASSWORD"), RequestLocation.BETA); - // Makes sure to delete the user before-hand. + // Makes sure to delete the user beforehand. Assertions.assertTrue(cafeAPI.getInteractionsEndpoint().deleteUserInteractionsSent("879761226761109544").get()); // Makes sure that a NotFoundException is thrown when trying to get the user. diff --git a/bot/wrappers/cafe-api-wrapper/src/test/java/com/beanbeanjuice/classes/BirthdayTest.java b/bot/wrappers/cafe-api-wrapper/src/test/java/com/beanbeanjuice/classes/BirthdayTest.java index 9cc79de5..20b8b7a7 100644 --- a/bot/wrappers/cafe-api-wrapper/src/test/java/com/beanbeanjuice/classes/BirthdayTest.java +++ b/bot/wrappers/cafe-api-wrapper/src/test/java/com/beanbeanjuice/classes/BirthdayTest.java @@ -19,15 +19,15 @@ public class BirthdayTest { @DisplayName("Birthday Class Test") public void testBirthdayException() { Assertions.assertThrows(InvalidTimeZoneException.class, () -> { - new Birthday(BirthdayMonth.FEBRUARY, 25, "burger", false); + new Birthday(BirthdayMonth.FEBRUARY, 25, "burger"); }); Assertions.assertThrows(BirthdayOverfillException.class, () -> { - new Birthday(BirthdayMonth.FEBRUARY, 30, "EST", false); + new Birthday(BirthdayMonth.FEBRUARY, 30, "EST"); }); Assertions.assertDoesNotThrow(() -> { - new Birthday(BirthdayMonth.JANUARY, 1, "UTC", false); + new Birthday(BirthdayMonth.JANUARY, 1, "UTC"); }); } @@ -37,11 +37,11 @@ public void testBirthdayFormat() throws ParseException { TimeZone.setDefault(TimeZone.getTimeZone("UTC")); int birthday1Year = Year.now(TimeZone.getTimeZone("EST").toZoneId()).getValue(); - Birthday birthday1 = new Birthday(BirthdayMonth.JANUARY, 1, "EST", false); + Birthday birthday1 = new Birthday(BirthdayMonth.JANUARY, 1, "EST"); Assertions.assertEquals(String.format("Mon Jan 01 05:00:00 UTC %d", birthday1Year), birthday1.getDate().toString()); int birthday2Year = Year.now(TimeZone.getTimeZone("PST").toZoneId()).getValue(); - Birthday birthday2 = new Birthday(BirthdayMonth.JANUARY, 1, "PST", false); + Birthday birthday2 = new Birthday(BirthdayMonth.JANUARY, 1, "PST"); Assertions.assertEquals(String.format("Mon Jan 01 08:00:00 UTC %d", birthday2Year), birthday2.getDate().toString()); Assertions.assertTrue(birthday1.getDate().before(birthday2.getDate())); @@ -56,7 +56,7 @@ public void testNotBirthday() { BirthdayMonth month = BirthdayMonth.values()[cal.get(Calendar.MONTH)]; int dayOfMonth = cal.get(Calendar.DAY_OF_MONTH); - Birthday birthday1 = new Birthday(month, dayOfMonth + 2, "EST", false); + Birthday birthday1 = new Birthday(month, dayOfMonth + 2, "EST"); Assertions.assertFalse(birthday1.isBirthday()); }