Skip to content

Commit

Permalink
Adding even more tests! Yayyy
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Villavicencio authored and Daniel Villavicencio committed Jul 21, 2024
1 parent 9c175b7 commit 6dc4437
Show file tree
Hide file tree
Showing 31 changed files with 599 additions and 232 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ Mono<BungieResponse<ManifestResponseFields>> getManifestEntity(
* @param destinyMembershipId the destiny membership id of the user
* @return {@link Mono} containing {@link CharactersResponse}
*/
@GetExchange("/Destiny2/{membershipType}/Profile/{destinyMembershipId}/?components=200")
@GetExchange(value = "/Destiny2/{membershipType}/Profile/{destinyMembershipId}/?components=200")
Mono<BungieResponse<CharactersResponse>> getUserCharacters(
@PathVariable Integer membershipType,
@PathVariable String destinyMembershipId
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,4 @@ public class InteractionData {
* List of resolved values for a select menu option by a user
*/
private List<String> values;

private ResolvedData resolved;

}
21 changes: 0 additions & 21 deletions src/main/java/com/deahtstroke/rivenbot/dto/discord/Message.java

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ public enum ButtonStyle {
BLURPLE(1),
GREY(2),
GREEN(3),
RED(4),
LINK(5);
RED(4);

@Getter
private final int buttonValue;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.deahtstroke.rivenbot.enums;

import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.stream.Stream;
import lombok.Getter;

public enum MessageComponentId {
RAID_STATS_COMPREHENSION("raid_stats_comprehension"),
MESSAGE_COMPONENT_TEST("message_component_test");

@Getter
private final String id;

MessageComponentId(String componentId) {
this.id = componentId;
}

public static MessageComponentId findById(String id) {
return Stream.of(MessageComponentId.values())
.filter(mci -> Objects.equals(mci.id, id))
.findFirst()
.orElseThrow(() -> new NoSuchElementException(
"Was not able to find a message component ID for id [%s]".formatted(id)));
}
}
20 changes: 8 additions & 12 deletions src/main/java/com/deahtstroke/rivenbot/enums/SlashCommand.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,21 @@
package com.deahtstroke.rivenbot.enums;

import com.deahtstroke.rivenbot.exception.ResourceNotFoundException;
import java.util.NoSuchElementException;
import java.util.stream.Stream;
import lombok.Getter;

public enum SlashCommand {

WEEKLY_DUNGEON("weekly_dungeon", false),
WEEKLY_RAID("weekly_raid", false),
RAID_STATS("raid_stats", true),
RAID_MAP("raid_map", false);

@Getter
private final String commandName;
WEEKLY_DUNGEON("weekly_dungeon"),
WEEKLY_RAID("weekly_raid"),
RAID_STATS("raid_stats"),
TEST_COMMAND("test_command");

@Getter
private final boolean authorized;
private final String commandName;

SlashCommand(String commandName, boolean authorized) {
SlashCommand(String commandName) {
this.commandName = commandName;
this.authorized = authorized;
}

/**
Expand All @@ -32,7 +28,7 @@ public static SlashCommand findByName(String commandName) {
return Stream.of(SlashCommand.values())
.filter(e -> e.getCommandName().equalsIgnoreCase(commandName))
.findFirst()
.orElseThrow(() -> new ResourceNotFoundException(
.orElseThrow(() -> new NoSuchElementException(
"Command with name [%s] was not found".formatted(commandName)));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.deahtstroke.rivenbot.exception;

public class NoRaidDataFoundException extends BaseDiscordChatException {

public NoRaidDataFoundException(String message, String chatErrorMessage) {
super(message, chatErrorMessage);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.deahtstroke.rivenbot.exception;

public class NoSuchHandlerException extends RuntimeException {

public NoSuchHandlerException(String errorMessage) {
super(errorMessage);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.deahtstroke.rivenbot.factory;

import com.deahtstroke.rivenbot.enums.SlashCommand;
import com.deahtstroke.rivenbot.exception.ResourceNotFoundException;
import com.deahtstroke.rivenbot.exception.NoSuchHandlerException;
import com.deahtstroke.rivenbot.handler.ApplicationCommandSource;
import com.deahtstroke.rivenbot.handler.RaidStatsHandler;
import com.deahtstroke.rivenbot.handler.WeeklyDungeonHandler;
Expand Down Expand Up @@ -33,7 +33,7 @@ public ApplicationCommandFactory(
public ApplicationCommandSource getHandler(SlashCommand command) {
ApplicationCommandSource creator = messageFactory.get(command);
if (Objects.isNull(creator)) {
throw new ResourceNotFoundException(
throw new NoSuchHandlerException(
"No message creator found for command [%s]".formatted(command));
}
return creator;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.deahtstroke.rivenbot.factory;

import com.deahtstroke.rivenbot.enums.SlashCommand;
import com.deahtstroke.rivenbot.exception.ResourceNotFoundException;
import com.deahtstroke.rivenbot.exception.NoSuchHandlerException;
import com.deahtstroke.rivenbot.handler.AutocompleteSource;
import com.deahtstroke.rivenbot.handler.RaidStatsHandler;
import java.util.Map;
Expand All @@ -20,10 +20,10 @@ public AutocompleteFactory(
}

@Override
public AutocompleteSource getHandler(SlashCommand command) {
public AutocompleteSource getHandler(SlashCommand command) throws NoSuchHandlerException {
AutocompleteSource creator = autocompleteMap.get(command);
if (Objects.isNull(creator)) {
throw new ResourceNotFoundException(
throw new NoSuchHandlerException(
"No message creator found for command [%s]".formatted(command));
}
return creator;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,31 @@
package com.deahtstroke.rivenbot.factory;

import com.deahtstroke.rivenbot.enums.MessageComponentId;
import com.deahtstroke.rivenbot.exception.NoSuchHandlerException;
import com.deahtstroke.rivenbot.handler.MessageComponentSource;
import com.deahtstroke.rivenbot.handler.RaidStatsButtonHandler;
import java.util.Map;
import java.util.Objects;
import org.springframework.stereotype.Component;

@Component
public class MessageComponentFactory implements MessageComponentHandler<MessageComponentSource> {

private static final String STATS_COMPREHENSION_ID = "raid_stats_comprehension";

private final Map<String, MessageComponentSource> componentFactory;
private final Map<MessageComponentId, MessageComponentSource> componentFactory;

public MessageComponentFactory(
RaidStatsButtonHandler raidStatsButtonHandler) {
componentFactory = Map.of(STATS_COMPREHENSION_ID, raidStatsButtonHandler);
componentFactory = Map.of(MessageComponentId.RAID_STATS_COMPREHENSION, raidStatsButtonHandler);
}

@Override
public MessageComponentSource getHandler(String componentId) {
return componentFactory.get(componentId);
public MessageComponentSource getHandler(MessageComponentId componentId) {
MessageComponentSource handler = componentFactory.get(componentId);
if (Objects.isNull(handler)) {
throw new NoSuchHandlerException(
"No handler found for component [%s] with ID [%s]".formatted(componentId,
componentId.getId()));
}
return handler;
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.deahtstroke.rivenbot.factory;

import com.deahtstroke.rivenbot.enums.MessageComponentId;

public interface MessageComponentHandler<T> {

/**
* Return a message component of type T based on a componentId
* Return a message component of type T based on a Message Component ID
*
* @param componentId The componentId of the button
* @param componentId The MessageComponentId of the button
* @return Type of the button handler
*/
T getHandler(String componentId);
T getHandler(MessageComponentId componentId);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.deahtstroke.rivenbot.factory;

import com.deahtstroke.rivenbot.enums.SlashCommand;
import com.deahtstroke.rivenbot.exception.NoSuchHandlerException;

public interface SlashCommandHandler<T> {

Expand All @@ -10,5 +11,5 @@ public interface SlashCommandHandler<T> {
* @param slashCommand The slash command that is invoked
* @return Message creator of type T
*/
T getHandler(SlashCommand slashCommand);
T getHandler(SlashCommand slashCommand) throws NoSuchHandlerException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.deahtstroke.rivenbot.dto.discord.Interaction;
import com.deahtstroke.rivenbot.dto.discord.InteractionResponse;
import com.deahtstroke.rivenbot.enums.InteractionType;
import com.deahtstroke.rivenbot.enums.MessageComponentId;
import com.deahtstroke.rivenbot.enums.SlashCommand;
import com.deahtstroke.rivenbot.exception.BaseException;
import com.deahtstroke.rivenbot.factory.ApplicationCommandFactory;
Expand Down Expand Up @@ -61,7 +62,8 @@ private Mono<InteractionResponse> resolveResponse(Interaction interaction,
InteractionType interactionType) {
return switch (interactionType) {
case MESSAGE_COMPONENT -> {
String componentId = interaction.getData().getCustomId();
MessageComponentId componentId = MessageComponentId.findById(
interaction.getData().getCustomId());
var handler = messageComponentFactory.getHandler(componentId);
yield handler.handle(interaction);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
import com.deahtstroke.rivenbot.dto.discord.InteractionResponseData;
import com.deahtstroke.rivenbot.entity.ButtonStyle;
import com.deahtstroke.rivenbot.entity.RaidStatistics;
import com.deahtstroke.rivenbot.enums.MessageComponentId;
import com.deahtstroke.rivenbot.exception.BaseDiscordChatException;
import com.deahtstroke.rivenbot.exception.MembershipsNotFoundException;
import com.deahtstroke.rivenbot.exception.NoRaidDataFoundException;
import com.deahtstroke.rivenbot.exception.ProfileNotPublicException;
import com.deahtstroke.rivenbot.service.DiscordAPIService;
import com.deahtstroke.rivenbot.service.RaidStatsService;
Expand Down Expand Up @@ -103,17 +105,17 @@ private Mono<InteractionResponseData> createRaidStatsResponse(String username, S
return defaultBungieClient.searchUserByExactNameAndCode(
new ExactUserSearchRequest(username, userTag))
.filter(response -> CollectionUtils.isNotEmpty(response.getResponse()))
.switchIfEmpty(Mono.deferContextual(ctx -> Mono.error(new MembershipsNotFoundException(
.switchIfEmpty(Mono.error(new MembershipsNotFoundException(
"User [%s] does not have any valid Destiny 2 memberships".formatted(displayUsername),
"User %s has no valid Destiny 2 memberships! Please contact someone from the Dev Team in order to solve this issue or try again later"
.formatted(displayUsername)))))
.formatted(displayUsername))))
.flatMap(response -> {
ExactUserSearchResponse firstResponse = response.getResponse().getFirst();
if (Boolean.FALSE.equals(firstResponse.getIsPublic())) {
return Mono.deferContextual(ctx -> Mono.error(new ProfileNotPublicException(
return Mono.error(new ProfileNotPublicException(
"The Bungie.net profile for user [%s] is set to private".formatted(displayUsername),
"Oh no! Seems that %s has their privacy settings turned on, therefore we cannot access their stuff right now. Sorry about that."
.formatted(displayUsername))));
.formatted(displayUsername)));
}
String membershipId = firstResponse.getMembershipId();
Integer membershipType = firstResponse.getMembershipType();
Expand All @@ -126,10 +128,10 @@ private Mono<InteractionResponseData> createRaidStatsResponse(String username, S
return InteractionResponseData.builder()
.embeds(MessageComponents.embeds(
createEmbed(displayUsername, usernameIcon, fields)))
.components(MessageComponents.builder()
.components(MessageComponents.components()
.addActionRow(MessageComponents.actionRow()
.button(RAID_COMPREHENSION_BUTTON_ID, "What is this?",
ButtonStyle.BLURPLE))
.button(MessageComponentId.RAID_STATS_COMPREHENSION.getId(),
"What is this?", ButtonStyle.BLURPLE))
.build())
.build();
});
Expand All @@ -144,6 +146,12 @@ private Mono<MembershipResponse> getBungieUserInfo(String membershipId, Integer
private Mono<List<EmbeddedField>> createEmbedFields(String username, String userTag,
String membershipId, Integer membershipType) {
return raidStatsService.calculateRaidStats(username, userTag, membershipId, membershipType)
.switchIfEmpty(Mono.error(new NoRaidDataFoundException(
"User [%s] has no raid data available".formatted(username + HASHTAG + userTag), """
Huh... It seems as if %s does not have any raids completed, either this or something went wrong when retrieving the data.\
If you are sure this is a bug be sure to let one of the developers know!""".formatted(
username + HASHTAG + userTag)
)))
.collectMap(RaidStatistics::getRaidName)
.flatMapIterable(Map::entrySet)
.map(MessageComponents::createField)
Expand Down
Loading

0 comments on commit 6dc4437

Please sign in to comment.