diff --git a/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/endpoints/birthdays/Birthday.java b/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/endpoints/birthdays/Birthday.java index ae506819..55778a6b 100644 --- a/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/endpoints/birthdays/Birthday.java +++ b/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/endpoints/birthdays/Birthday.java @@ -27,8 +27,7 @@ public class Birthday { * @param day The {@link Integer day} of the {@link Birthday}. * @param isAlreadyMentioned False, if the user's birthday has not been mentioned by cafeBot. */ - public Birthday(final BirthdayMonth month, final int day, final String timeZone, - final boolean isAlreadyMentioned) throws InvalidTimeZoneException, BirthdayOverfillException { + public Birthday(final BirthdayMonth month, final int day, final String timeZone, final boolean isAlreadyMentioned) { this.month = month; this.day = day; diff --git a/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/endpoints/birthdays/BirthdaysEndpoint.java b/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/endpoints/birthdays/BirthdaysEndpoint.java index 7046273a..58384713 100644 --- a/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/endpoints/birthdays/BirthdaysEndpoint.java +++ b/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/endpoints/birthdays/BirthdaysEndpoint.java @@ -10,9 +10,9 @@ import com.beanbeanjuice.cafeapi.wrapper.utility.Time; import com.fasterxml.jackson.databind.JsonNode; -import java.text.ParseException; import java.util.*; -import java.util.function.Consumer; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CompletionException; /** * A class used for {@link Birthday} requests to the {@link CafeAPI CafeAPI}. @@ -38,67 +38,31 @@ private HashMap requestToBirthdayMap(Request request) { * @throws AuthorizationException Thrown when the {@link String apiKey} is invalid. * @throws ResponseException Thrown when there is a generic server-side {@link CafeException}. */ - public HashMap getAllBirthdays() throws AuthorizationException, ResponseException { - Request request = RequestBuilder.create(RequestRoute.CAFEBOT, RequestType.GET) + public CompletableFuture> getAllBirthdays() throws AuthorizationException, ResponseException { + return RequestBuilder.create(RequestRoute.CAFEBOT, RequestType.GET) .setRoute("/birthdays") .setAuthorization(apiKey) - .build().orElseThrow(); - - return requestToBirthdayMap(request); - } - - public void getAllBirthdaysAsync(Consumer> successFunction, Consumer errorFunction) { - RequestBuilder.create(RequestRoute.CAFEBOT, RequestType.GET) - .setRoute("/birthdays") - .setAuthorization(apiKey) - .onSuccess((request) -> successFunction.accept(requestToBirthdayMap(request))) - .onError(errorFunction) - .buildAsync(); - } - - public void getAllBirthdaysAsync(Consumer> successFunction) { - RequestBuilder.create(RequestRoute.CAFEBOT, RequestType.GET) - .setRoute("/birthdays") - .setAuthorization(apiKey) - .buildAsync((request) -> successFunction.accept(requestToBirthdayMap(request))); + .buildAsync() + .thenApplyAsync((optionalRequest) -> { + if (optionalRequest.isPresent()) return requestToBirthdayMap(optionalRequest.get()); + throw new CompletionException("Error getting all birthdays. Request is empty.", null); + }); } /** * Retrieves the {@link Birthday} for a specified {@link String userID}. * @param userID The specified {@link String userID}. * @return The {@link Birthday} for the specified {@link String userID}. - * @throws AuthorizationException Thrown when the {@link String apiKey} is invalid. - * @throws ResponseException Thrown when there is a generic server-side {@link CafeException}. - * @throws NotFoundException Thrown when the {@link Birthday} for the specified {@link String userID} does not exist. - * @throws ParseException Thrown when there was an error parsing the {@link Birthday}. */ - public Optional getUserBirthday(final String userID) - throws AuthorizationException, ResponseException, NotFoundException { - Request request = RequestBuilder.create(RequestRoute.CAFEBOT, RequestType.GET) + public CompletableFuture> getUserBirthday(final String userID) { + return RequestBuilder.create(RequestRoute.CAFEBOT, RequestType.GET) .setRoute("/birthdays/" + userID) .setAuthorization(apiKey) - .build().orElseThrow(); - - return parseBirthday(request.getData().get("birthday")); - } - - public void getUserBirthdayAsync(final String userID, final Consumer successFunction, final Consumer errorFunction) { - RequestBuilder.create(RequestRoute.CAFEBOT, RequestType.GET) - .setRoute("/birthdays/" + userID) - .setAuthorization(apiKey) - .onSuccess((request) -> parseBirthday(request.getData().get("birthday")).ifPresentOrElse( - successFunction, - () -> errorFunction.accept(new ParseException("There was an error parsing the birthday.", 0)) - )) - .onError(errorFunction) - .buildAsync(); - } - - public void getUserBirthdayAsync(final String userID, final Consumer successFunction) { - RequestBuilder.create(RequestRoute.CAFEBOT, RequestType.GET) - .setRoute("/birthdays/" + userID) - .setAuthorization(apiKey) - .buildAsync((request) -> parseBirthday(request.getData().get("birthday")).ifPresent(successFunction)); + .buildAsync() + .thenApplyAsync((optionalRequest) -> { + if (optionalRequest.isPresent()) return parseBirthday(optionalRequest.get().getData().get("birthday")); + throw new CompletionException("Error getting user birthday. Request is empty.", null); + }); } /** @@ -106,60 +70,21 @@ public void getUserBirthdayAsync(final String userID, final Consumer s * @param userID The specified {@link String userID}. * @param birthday The {@link Birthday} for the specified {@link String userID}. * @return True, if the {@link Birthday} was successfully updated in the {@link CafeAPI CafeAPI}. - * @throws AuthorizationException Thrown when the {@link String apiKey} is invalid. - * @throws ResponseException Thrown when there is a generic server-side {@link CafeException}. - * @throws NotFoundException Thrown when the {@link Birthday} for the specified {@link String userID} does not exist. - * @throws UndefinedVariableException Thrown when a variable is undefined. - * @throws TeaPotException Thrown when the {@link BirthdayMonth month} or {@link Integer day} is invalid. */ - public boolean updateUserBirthday(final String userID, final Birthday birthday) - throws AuthorizationException, ResponseException, NotFoundException, UndefinedVariableException { + public CompletableFuture updateUserBirthday(final String userID, final Birthday birthday) { BirthdayMonth month = birthday.getMonth(); int day = birthday.getDay(); - // TODO: Check for invalid days/month. -// // Checking for days in a month. -// if (month.getDaysInMonth() < day) -// throw new TeaPotException("There are only " + month.getDaysInMonth() + " days in " + month + ", not " + day + "."); -// -// // Checking if the month is valid. -// if (month == BirthdayMonth.ERROR) -// throw new TeaPotException(month + " is an invalid month."); - - Request request = RequestBuilder.create(RequestRoute.CAFEBOT, RequestType.PATCH) - .setRoute("/birthdays/" + userID) - .addParameter("birthday", getBirthdayString(month, day)) - .addParameter("time_zone", birthday.getTimeZone().getID()) - .setAuthorization(apiKey) - .build().orElseThrow(); - - return request.getStatusCode() == 200; - } - - public void updateUserBirthdayAsync(final String userID, final Birthday birthday, final Runnable successFunction, final Consumer errorFunction) { - BirthdayMonth month = birthday.getMonth(); - int day = birthday.getDay(); - - RequestBuilder.create(RequestRoute.CAFEBOT, RequestType.PATCH) + return RequestBuilder.create(RequestRoute.CAFEBOT, RequestType.PATCH) .setRoute("/birthdays/" + userID) .addParameter("birthday", getBirthdayString(month, day)) .addParameter("time_zone", birthday.getTimeZone().getID()) .setAuthorization(apiKey) - .onSuccess((request) -> successFunction.run()) - .onError(errorFunction) - .buildAsync(); - } - - public void updateUserBirthdayAsync(final String userID, final Birthday birthday, final Runnable successFunction) { - BirthdayMonth month = birthday.getMonth(); - int day = birthday.getDay(); - - RequestBuilder.create(RequestRoute.CAFEBOT, RequestType.PATCH) - .setRoute("/birthdays/" + userID) - .addParameter("birthday", getBirthdayString(month, day)) - .addParameter("time_zone", birthday.getTimeZone().getID()) - .setAuthorization(apiKey) - .buildAsync((request) -> successFunction.run()); + .buildAsync() + .thenApplyAsync((optionalRequest) -> { + if (optionalRequest.isPresent()) return optionalRequest.get().getStatusCode() == 200; + throw new CompletionException("Error updating user birthday. Request is empty.", null); + }); } /** @@ -167,38 +92,17 @@ public void updateUserBirthdayAsync(final String userID, final Birthday birthday * @param userID The specified {@link String userID}. * @param alreadyMentioned The new {@link Boolean alreadyMentioned} state. * @return True, if the {@link Birthday} was successfully updated in the {@link CafeAPI CafeAPI}. - * @throws AuthorizationException Thrown when the {@link String apiKey} is invalid. - * @throws ResponseException Thrown when there is a generic server-side {@link CafeException}. - * @throws NotFoundException Thrown when the {@link Birthday} does not exist for the specified {@link String userID}. - * @throws UndefinedVariableException Thrown when a variable is undefined. */ - public boolean updateUserBirthdayMention(final String userID, final boolean alreadyMentioned) - throws AuthorizationException, ResponseException, NotFoundException, UndefinedVariableException { - Request request = RequestBuilder.create(RequestRoute.CAFEBOT, RequestType.PATCH) + 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) - .build().orElseThrow(); - - return request.getStatusCode() == 200; - } - - public void updateUserBirthdayMentionAsync(final String userID, final boolean alreadyMentioned, Runnable successFunction, Consumer errorFunction) { - RequestBuilder.create(RequestRoute.CAFEBOT, RequestType.PATCH) - .setRoute("/birthdays/" + userID + "/mention") - .addParameter("already_mentioned", String.valueOf(alreadyMentioned)) - .setAuthorization(apiKey) - .onSuccess((request) -> successFunction.run()) - .onError(errorFunction) - .buildAsync(); - } - - public void updateUserBirthdayMentionAsync(final String userID, final boolean alreadyMentioned, Runnable successFunction) { - RequestBuilder.create(RequestRoute.CAFEBOT, RequestType.PATCH) - .setRoute("/birthdays/" + userID + "/mention") - .addParameter("already_mentioned", String.valueOf(alreadyMentioned)) - .setAuthorization(apiKey) - .buildAsync((request) -> successFunction.run()); + .buildAsync() + .thenApplyAsync((optionalRequest) -> { + if (optionalRequest.isPresent()) return optionalRequest.get().getStatusCode() == 200; + throw new CompletionException("Error updating user mention. Request is empty.", null); + }); } /** @@ -206,93 +110,37 @@ public void updateUserBirthdayMentionAsync(final String userID, final boolean al * @param userID The specified {@link String userID}. * @param birthday The {@link Birthday} specified for the {@link String userID}. * @return True, if the {@link Birthday} was successfully created. - * @throws AuthorizationException Thrown when the {@link String apiKey} is invalid. - * @throws ResponseException Thrown when there is a generic server-side {@link CafeException}. - * @throws ConflictException Thrown when the {@link Birthday} for the specified {@link String userID} already exists. - * @throws UndefinedVariableException Thrown when a variable is undefined. - * @throws TeaPotException Thrown when the {@link BirthdayMonth month} or {@link Integer day} is invalid. */ - public boolean createUserBirthday(final String userID, final Birthday birthday) - throws AuthorizationException, ResponseException, ConflictException, UndefinedVariableException, TeaPotException { - BirthdayMonth month = birthday.getMonth(); - int day = birthday.getDay(); - - // TODO: Check if this error checking is required. -// // Checking for days in a month. -// if (month.getDaysInMonth() < day) -// throw new TeaPotException("There are only " + month.getDaysInMonth() + " days in " + month + ", not " + day + "."); -// -// // Checking if the month is valid. -// if (month == BirthdayMonth.ERROR) -// throw new TeaPotException(month + " is an invalid month."); - - Request request = RequestBuilder.create(RequestRoute.CAFEBOT, RequestType.POST) - .setRoute("/birthdays/" + userID) - .addParameter("birthday", getBirthdayString(month, day)) - .addParameter("time_zone", birthday.getTimeZone().getID()) - .setAuthorization(apiKey) - .build().orElseThrow(); - - return request.getStatusCode() == 201; - } - - public void createUserBirthdayAsync(final String userID, final Birthday birthday, final Runnable successFunction, final Consumer errorFunction) { - BirthdayMonth month = birthday.getMonth(); - int day = birthday.getDay(); - - RequestBuilder.create(RequestRoute.CAFEBOT, RequestType.POST) - .setRoute("/birthdays/" + userID) - .addParameter("birthday", getBirthdayString(month, day)) - .addParameter("time_zone", birthday.getTimeZone().getID()) - .setAuthorization(apiKey) - .onSuccess((request) -> successFunction.run()) - .onError(errorFunction) - .buildAsync(); - } - - public void createUserBirthdayAsync(final String userID, final Birthday birthday, final Runnable successFunction) { + public CompletableFuture createUserBirthday(final String userID, final Birthday birthday) { BirthdayMonth month = birthday.getMonth(); int day = birthday.getDay(); - RequestBuilder.create(RequestRoute.CAFEBOT, RequestType.POST) + return RequestBuilder.create(RequestRoute.CAFEBOT, RequestType.POST) .setRoute("/birthdays/" + userID) .addParameter("birthday", getBirthdayString(month, day)) .addParameter("time_zone", birthday.getTimeZone().getID()) .setAuthorization(apiKey) - .buildAsync((request) -> successFunction.run()); + .buildAsync() + .thenApplyAsync((optionalRequest) -> { + if (optionalRequest.isPresent()) return optionalRequest.get().getStatusCode() == 201; + throw new CompletionException("Error creating user birthday. Request is empty.", null); + }); } /** * Removes a {@link Birthday} for a specified {@link String userID}. * @param userID The specified {@link String userID}. * @return True, if the {@link Birthday} has been successfully removed from the {@link CafeAPI CafeAPI}. - * @throws AuthorizationException Thrown when the {@link String apiKey} is invalid. - * @throws ResponseException Thrown when there is a generic server-side {@link CafeException}. */ - public boolean removeUserBirthday(final String userID) - throws AuthorizationException, ResponseException { - Request request = RequestBuilder.create(RequestRoute.CAFEBOT, RequestType.DELETE) - .setRoute("/birthdays/" + userID) - .setAuthorization(apiKey) - .build().orElseThrow(); - - return request.getStatusCode() == 200; - } - - public void removeUserBirthdayAsync(final String userID, final Runnable successFunction, final Consumer errorFunction) { - RequestBuilder.create(RequestRoute.CAFEBOT, RequestType.DELETE) - .setRoute("/birthdays/" + userID) - .setAuthorization(apiKey) - .onSuccess((request) -> successFunction.run()) - .onError(errorFunction) - .buildAsync(); - } - - public void removeUserBirthdayAsync(final String userID, final Runnable successFunction) { - RequestBuilder.create(RequestRoute.CAFEBOT, RequestType.DELETE) + public CompletableFuture removeUserBirthday(final String userID) { + return RequestBuilder.create(RequestRoute.CAFEBOT, RequestType.DELETE) .setRoute("/birthdays/" + userID) .setAuthorization(apiKey) - .buildAsync((request) -> successFunction.run()); + .buildAsync() + .thenApplyAsync((optionalRequest) -> { + if (optionalRequest.isPresent()) return optionalRequest.get().getStatusCode() == 200; + throw new CompletionException("Error removing user birthday. Request is empty.", null); + }); } /** diff --git a/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/endpoints/interactions/pictures/InteractionPicturesEndpoint.java b/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/endpoints/interactions/pictures/InteractionPicturesEndpoint.java index 0fe358ae..ecb7e6e0 100644 --- a/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/endpoints/interactions/pictures/InteractionPicturesEndpoint.java +++ b/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/endpoints/interactions/pictures/InteractionPicturesEndpoint.java @@ -14,6 +14,8 @@ import com.beanbeanjuice.cafeapi.wrapper.exception.api.CafeException; import java.util.Optional; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CompletionException; /** * A class used to retrieve random pictures from the {@link CafeAPI CafeAPI}. @@ -35,24 +37,23 @@ public InteractionPicturesEndpoint(final CafeAPI cafeAPI) { /** * Retrieves a random {@link String interactionURL} for a specified {@link InteractionType}. + * * @param type The {@link InteractionType} specified. - * @return The {@link String url} to the {@link Interaction Interaction} image. - * @throws AuthorizationException Thrown when the {@link String apiKey} is invalid. - * @throws ResponseException Thrown when there is a generic server-side {@link CafeException CafeException}. - * @throws TeaPotException Thrown when an invalid {@link InteractionType} is entered. + * @return A {@link CompletableFuture} containing an {@link Optional} {@link String url}. */ - public String getRandomInteractionPicture(final InteractionType type) - throws AuthorizationException, ResponseException, TeaPotException { + public CompletableFuture> getRandomInteractionPicture(final InteractionType type) { Optional potentialString = type.getKawaiiAPIString(); - if (potentialString.isPresent()) return cafeAPI.getKawaiiAPI().getGifEndpoint().getGIF(potentialString.get()).orElseThrow(); + if (potentialString.isPresent()) return cafeAPI.getKawaiiAPI().getGifEndpoint().getGIF(potentialString.get()); - Request request = RequestBuilder.create(RequestRoute.CAFEBOT, RequestType.GET) + return RequestBuilder.create(RequestRoute.CAFEBOT, RequestType.GET) .setRoute("/interaction_pictures/" + type) .setAuthorization(apiKey) - .build().orElseThrow(); - - return request.getData().get("url").asText(); + .buildAsync() + .thenApplyAsync((optionalRequest) -> { + if (optionalRequest.isPresent()) return Optional.of(optionalRequest.get().getData().get("url").asText()); + throw new CompletionException("Unable to get a random interaction picture. Request is empty.", null); + }); } } diff --git a/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/requests/RequestBuilder.java b/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/requests/RequestBuilder.java index b4dbc914..fbc4547d 100644 --- a/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/requests/RequestBuilder.java +++ b/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/requests/RequestBuilder.java @@ -15,8 +15,7 @@ import java.net.URISyntaxException; import java.util.HashMap; import java.util.Optional; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.Future; +import java.util.concurrent.*; import java.util.function.Consumer; import java.util.logging.Level; import java.util.logging.Logger; @@ -34,8 +33,6 @@ public class RequestBuilder { private final String apiURL; private String apiKey; - private Consumer successConsumer; - private Consumer errorConsumer; /** * Creates a new {@link RequestBuilder}. @@ -84,10 +81,7 @@ public RequestBuilder setAuthorization(final String apiKey) { return this; } - /** - * Builds the {@link RequestBuilder}. - * @return An {@link Optional} containing the resulting {@link Request}. - */ + // TODO: Change this to private. public Optional build() { try { URIBuilder uriBuilder = new URIBuilder(apiURL + route); @@ -111,15 +105,10 @@ public Optional build() { case 500 -> throw new ResponseException(request); } - if (successConsumer != null) successConsumer.accept(request); - return Optional.of(request); } catch (URISyntaxException | ExecutionException | InterruptedException | IOException e) { Logger.getLogger(RequestBuilder.class.getName()).log(Level.WARNING, "Error queuing request: " + e.getMessage()); return Optional.empty(); - } catch (Exception e) { - if (errorConsumer != null) errorConsumer.accept(e); - else throw e; } // try { @@ -155,34 +144,14 @@ public Optional build() { // } catch (URISyntaxException | IOException e) { // return Optional.empty(); // } - return Optional.empty(); - } - - /** - * Builds and runs the {@link Request} with a provided {@link Consumer function}. - * @param consumer The {@link Consumer function} to run. - * @return An {@link Optional} containing the {@link Request}. - */ - public Optional build(final Consumer consumer) { - this.successConsumer = consumer; - return build(); - } - - /** - * Builds the {@link Request} asynchronously on a separate {@link Thread}. - */ - public void buildAsync() { - Thread thread = new Thread(this::build); - thread.start(); } /** * Builds the {@link Request} asynchronously on a separate {@link Thread}. - * @param consumer The {@link Consumer} function to run on the {@link Request} once finished. */ - public void buildAsync(final Consumer consumer) { - this.successConsumer = consumer; - buildAsync(); + public CompletableFuture> buildAsync() { + ExecutorService exec = Executors.newSingleThreadExecutor(); + return CompletableFuture.supplyAsync(this::build, exec); } private HttpResponse get(final SimpleHttpRequest request) throws ExecutionException, InterruptedException, IOException { @@ -198,24 +167,6 @@ private HttpResponse get(final SimpleHttpRequest request) throws ExecutionExcept return response; } - /** - * The function to run upon a successful 200 return code. - * @param consumer The {@link Consumer} function taking in a {@link Request} to run on. - */ - public RequestBuilder onSuccess(Consumer consumer) { - this.successConsumer = consumer; - return this; - } - - /** - * The function to run upon a failure. This is any code other than 200. - * @param consumer The {@link Consumer} function taking in a {@link Exception} to run on. - */ - public RequestBuilder onError(Consumer consumer) { - this.errorConsumer = consumer; - return this; - } - // /** // * Retrieves the {@link HttpResponse} for a {@link RequestType GET} request. // * @return The {@link RequestType GET} {@link HttpResponse}. diff --git a/wrappers/cafe-api-wrapper/src/test/java/com/beanbeanjuice/cafeapi/beta/BirthdayTest.java b/wrappers/cafe-api-wrapper/src/test/java/com/beanbeanjuice/cafeapi/beta/BirthdayTest.java index 638a8208..63bce87b 100644 --- a/wrappers/cafe-api-wrapper/src/test/java/com/beanbeanjuice/cafeapi/beta/BirthdayTest.java +++ b/wrappers/cafe-api-wrapper/src/test/java/com/beanbeanjuice/cafeapi/beta/BirthdayTest.java @@ -13,81 +13,127 @@ import org.junit.jupiter.api.Test; import java.util.Optional; +import java.util.concurrent.ExecutionException; public class BirthdayTest { @Test @DisplayName("Birthdays Endpoint Test") - public void testBirthdaysEndpoint() { + public void testBirthdaysEndpoint() throws ExecutionException, InterruptedException { CafeAPI cafeAPI = new CafeAPI("beanbeanjuice", System.getenv("API_PASSWORD"), RequestLocation.BETA); // Makes sure the user's birthday doesn't exist before starting. - Assertions.assertTrue(cafeAPI.getBirthdaysEndpoint().removeUserBirthday("178272524533104642")); + Assertions.assertTrue(cafeAPI.getBirthdaysEndpoint().removeUserBirthday("178272524533104642").get()); // Makes sure the user's birthday cannot be found. - Assertions.assertThrows(NotFoundException.class, () -> cafeAPI.getBirthdaysEndpoint().getUserBirthday("178272524533104642")); + try { + cafeAPI.getBirthdaysEndpoint().getUserBirthday("178272524533104642").get(); + Assertions.fail(); + } catch (Exception e) { + Assertions.assertInstanceOf(NotFoundException.class, e.getCause()); + } // Makes sure the user's birthday can be created. - Assertions.assertTrue(cafeAPI.getBirthdaysEndpoint().createUserBirthday("178272524533104642", new Birthday(BirthdayMonth.DECEMBER, 31, "EST", false))); + Assertions.assertTrue(cafeAPI.getBirthdaysEndpoint().createUserBirthday("178272524533104642", new Birthday(BirthdayMonth.DECEMBER, 31, "EST", false)).get()); // Makes sure the user's birthday cannot be duplicated. - Assertions.assertThrows(ConflictException.class, () -> cafeAPI.getBirthdaysEndpoint().createUserBirthday("178272524533104642", new Birthday(BirthdayMonth.DECEMBER, 20, "EST", false))); + try { + cafeAPI.getBirthdaysEndpoint().createUserBirthday("178272524533104642", new Birthday(BirthdayMonth.DECEMBER, 20, "EST", false)).get(); + Assertions.fail(); + } catch (Exception e) { + Assertions.assertInstanceOf(ConflictException.class, e.getCause()); + } // Makes sure the month is the same. - Assertions.assertEquals(BirthdayMonth.DECEMBER, cafeAPI.getBirthdaysEndpoint().getAllBirthdays().get("178272524533104642").getMonth()); + Assertions.assertEquals(BirthdayMonth.DECEMBER, cafeAPI.getBirthdaysEndpoint().getAllBirthdays().get().get("178272524533104642").getMonth()); // Makes sure the date is the same. Assertions.assertTrue(() -> { - Optional optionalBirthday = cafeAPI.getBirthdaysEndpoint().getUserBirthday("178272524533104642"); + 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 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))); + Assertions.assertThrows(BirthdayOverfillException.class, () -> cafeAPI.getBirthdaysEndpoint().updateUserBirthday("178272524533104642", new Birthday(BirthdayMonth.FEBRUARY, 30, "EST", false)).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))); + Assertions.assertThrows(TeaPotException.class, () -> cafeAPI.getBirthdaysEndpoint().updateUserBirthday("178272524533104642", new Birthday(BirthdayMonth.ERROR, 15, "EST", false)).get()); // Makes sure the birthday can be changed. - Assertions.assertTrue(cafeAPI.getBirthdaysEndpoint().updateUserBirthday("178272524533104642", new Birthday(BirthdayMonth.FEBRUARY, 29, "UTC", false))); + 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 birthday = cafeAPI.getBirthdaysEndpoint().getUserBirthday("178272524533104642"); - Assertions.assertTrue(birthday.isPresent()); - return birthday.get().getMonth() == BirthdayMonth.FEBRUARY; + 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. Assertions.assertTrue(() -> { - Optional optionalBirthday = cafeAPI.getBirthdaysEndpoint().getUserBirthday("178272524533104642"); + 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() == 29; }); // Makes sure that alreadyMentioned is false by default. Assertions.assertFalse(() -> { - Optional optionalBirthday = cafeAPI.getBirthdaysEndpoint().getUserBirthday("178272524533104642"); + 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)); + Assertions.assertTrue(cafeAPI.getBirthdaysEndpoint().updateUserBirthdayMention("178272524533104642", true).get()); // Makes sure alreadyMentioned HAS updated. Assertions.assertTrue(() -> { - Optional optionalBirthday = cafeAPI.getBirthdaysEndpoint().getUserBirthday("178272524533104642"); + 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")); + Assertions.assertTrue(cafeAPI.getBirthdaysEndpoint().removeUserBirthday("178272524533104642").get()); // Makes sure the user no longer exists. - Assertions.assertThrows(NotFoundException.class, () -> cafeAPI.getBirthdaysEndpoint().getUserBirthday("178272524533104642")); + try { + cafeAPI.getBirthdaysEndpoint().getUserBirthday("178272524533104642").get(); + Assertions.fail(); + } catch (Exception e) { + Assertions.assertInstanceOf(NotFoundException.class, e.getCause()); + } } } diff --git a/wrappers/cafe-api-wrapper/src/test/java/com/beanbeanjuice/cafeapi/beta/InteractionPictureTest.java b/wrappers/cafe-api-wrapper/src/test/java/com/beanbeanjuice/cafeapi/beta/InteractionPictureTest.java index 0fa9b7fa..e556e1c9 100644 --- a/wrappers/cafe-api-wrapper/src/test/java/com/beanbeanjuice/cafeapi/beta/InteractionPictureTest.java +++ b/wrappers/cafe-api-wrapper/src/test/java/com/beanbeanjuice/cafeapi/beta/InteractionPictureTest.java @@ -2,6 +2,7 @@ import com.beanbeanjuice.cafeapi.wrapper.CafeAPI; import com.beanbeanjuice.cafeapi.wrapper.endpoints.interactions.InteractionType; +import com.beanbeanjuice.cafeapi.wrapper.exception.api.AuthorizationException; import com.beanbeanjuice.cafeapi.wrapper.requests.RequestLocation; import com.beanbeanjuice.cafeapi.wrapper.endpoints.interactions.pictures.InteractionPicturesEndpoint; import com.beanbeanjuice.kawaiiapi.wrapper.KawaiiAPI; @@ -9,6 +10,10 @@ import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import java.util.Optional; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicReference; + /** * Tests the {@link InteractionPicturesEndpoint InteractionPictures}. * This confirms that the {@link CafeAPI} and {@link KawaiiAPI KawaiiAPI} are both working. @@ -27,11 +32,33 @@ public void testInteractionPicturesEndpoint() { Assertions.assertDoesNotThrow(() -> { for (InteractionType type : InteractionType.values()) { type.getKawaiiAPIString().ifPresentOrElse( - (kawaiiAPIString) -> Assertions.assertTrue(cafeAPI.getInteractionPicturesEndpoint().getRandomInteractionPicture(type).startsWith("https://api.kawaii.red/gif/")), + (kawaiiAPIString) -> { + try { + Assertions.assertTrue(cafeAPI.getInteractionPicturesEndpoint().getRandomInteractionPicture(type).get().get().startsWith("https://api.kawaii.red/gif/")); + } catch (Exception e) { + Assertions.fail(); + } + }, () -> Assertions.assertNotNull(cafeAPI.getInteractionPicturesEndpoint().getRandomInteractionPicture(type)) ); } }); } + @Test + @DisplayName("Interaction Pictures Endpoint Authorization Error Test") + public void testInteractionPicturesAuthorizationError() throws InterruptedException { + CafeAPI cafeAPI = new CafeAPI("beanbeanjuice", "fake", RequestLocation.BETA); + + AtomicReference cause = new AtomicReference<>(); + + cafeAPI.getInteractionPicturesEndpoint().getRandomInteractionPicture(InteractionType.STAB).exceptionally((exception) -> { + cause.set(exception.getCause()); + return Optional.empty(); + }); + + Thread.sleep(1000); + Assertions.assertInstanceOf(AuthorizationException.class, cause.get()); + } + } diff --git a/wrappers/kawaii-api-wrapper/src/main/java/com/beanbeanjuice/kawaiiapi/wrapper/requests/RequestBuilder.java b/wrappers/kawaii-api-wrapper/src/main/java/com/beanbeanjuice/kawaiiapi/wrapper/requests/RequestBuilder.java index 728e42ff..e7ffe4fa 100644 --- a/wrappers/kawaii-api-wrapper/src/main/java/com/beanbeanjuice/kawaiiapi/wrapper/requests/RequestBuilder.java +++ b/wrappers/kawaii-api-wrapper/src/main/java/com/beanbeanjuice/kawaiiapi/wrapper/requests/RequestBuilder.java @@ -65,7 +65,7 @@ public RequestBuilder setAuthorization(final String apiKey) { * Builds and runs the {@link Request}. * @return An {@link Optional} containing the {@link Request}. */ - public Optional build() { + private Optional build() { try { SimpleHttpRequest httpRequest = SimpleRequestBuilder.get(apiURL).build(); SimpleHttpResponse httpResponse = (SimpleHttpResponse) get(httpRequest);