[AB-99/AB-66] changed commands to use embeds for exceptions instead of direct messages

added models instead of using HashMaps for exceptions
added a lot of exceptions for different cases
refactored a few commands to be fully async instead of fire and forget
This commit is contained in:
Sheldan
2020-08-29 01:24:06 +02:00
parent fbb36ae9d5
commit 552ecc26b8
285 changed files with 1607 additions and 847 deletions

View File

@@ -40,6 +40,7 @@ public class ServerInfo extends AbstractConditionableCommand {
.name("serverInfo")
.module(UtilityModuleInterface.UTILITY)
.templated(true)
.supportsEmbedException(true)
.causesReaction(false)
.parameters(parameters)
.help(helpInfo)

View File

@@ -46,6 +46,7 @@ public class ShowAvatar extends AbstractConditionableCommand {
.name("showAvatar")
.module(UtilityModuleInterface.UTILITY)
.templated(true)
.supportsEmbedException(true)
.causesReaction(false)
.parameters(parameters)
.help(helpInfo)

View File

@@ -45,6 +45,7 @@ public class ShowEmote extends AbstractConditionableCommand {
.name("showEmote")
.module(UtilityModuleInterface.UTILITY)
.templated(true)
.supportsEmbedException(true)
.causesReaction(false)
.parameters(parameters)
.help(helpInfo)

View File

@@ -44,6 +44,7 @@ public class StarStats extends AbstractConditionableCommand {
.name("starStats")
.module(UtilityModuleInterface.UTILITY)
.templated(true)
.supportsEmbedException(true)
.causesReaction(false)
.parameters(parameters)
.help(helpInfo)

View File

@@ -65,6 +65,7 @@ public class UserInfo extends AbstractConditionableCommand {
.name("userInfo")
.module(UtilityModuleInterface.UTILITY)
.templated(true)
.supportsEmbedException(true)
.causesReaction(false)
.parameters(parameters)
.help(helpInfo)

View File

@@ -58,6 +58,7 @@ public class Remind extends AbstractConditionableCommand {
.name("remind")
.module(UtilityModuleInterface.UTILITY)
.templated(true)
.supportsEmbedException(true)
.causesReaction(true)
.parameters(parameters)
.help(helpInfo)

View File

@@ -45,6 +45,7 @@ public class Reminders extends AbstractConditionableCommand {
.name("reminders")
.module(UtilityModuleInterface.UTILITY)
.templated(true)
.supportsEmbedException(true)
.causesReaction(true)
.help(helpInfo)
.build();

View File

@@ -41,6 +41,7 @@ public class UnRemind extends AbstractConditionableCommand {
.name("unRemind")
.module(UtilityModuleInterface.UTILITY)
.templated(true)
.supportsEmbedException(true)
.causesReaction(true)
.parameters(parameters)
.help(helpInfo)

View File

@@ -15,6 +15,7 @@ import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
@Component
public class Accept extends AbstractConditionableCommand {
@@ -23,14 +24,14 @@ public class Accept extends AbstractConditionableCommand {
private SuggestionService suggestionService;
@Override
public CommandResult execute(CommandContext commandContext) {
public CompletableFuture<CommandResult> executeAsync(CommandContext commandContext) {
checkParameters(commandContext);
List<Object> parameters = commandContext.getParameters().getParameters();
Long suggestionId = (Long) parameters.get(0);
String text = parameters.size() == 2 ? (String) parameters.get(1) : "";
SuggestionLog suggestionModel = (SuggestionLog) ContextConverter.fromCommandContext(commandContext, SuggestionLog.class);
suggestionService.acceptSuggestion(suggestionId, text, suggestionModel);
return CommandResult.fromSuccess();
return suggestionService.acceptSuggestion(suggestionId, text, suggestionModel)
.thenApply(aVoid -> CommandResult.fromSuccess());
}
@Override
@@ -43,6 +44,8 @@ public class Accept extends AbstractConditionableCommand {
.name("accept")
.module(UtilityModuleInterface.UTILITY)
.templated(true)
.async(true)
.supportsEmbedException(true)
.causesReaction(true)
.parameters(parameters)
.help(helpInfo)

View File

@@ -15,6 +15,7 @@ import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
@Component
public class Reject extends AbstractConditionableCommand {
@@ -23,14 +24,14 @@ public class Reject extends AbstractConditionableCommand {
private SuggestionService suggestionService;
@Override
public CommandResult execute(CommandContext commandContext) {
public CompletableFuture<CommandResult> executeAsync(CommandContext commandContext) {
checkParameters(commandContext);
List<Object> parameters = commandContext.getParameters().getParameters();
Long suggestionId = (Long) parameters.get(0);
String text = parameters.size() == 2 ? (String) parameters.get(1) : "";
SuggestionLog suggestionModel = (SuggestionLog) ContextConverter.fromCommandContext(commandContext, SuggestionLog.class);
suggestionService.rejectSuggestion(suggestionId, text, suggestionModel);
return CommandResult.fromSuccess();
return suggestionService.rejectSuggestion(suggestionId, text, suggestionModel)
.thenApply(aVoid -> CommandResult.fromSuccess());
}
@Override
@@ -43,6 +44,8 @@ public class Reject extends AbstractConditionableCommand {
.name("reject")
.module(UtilityModuleInterface.UTILITY)
.templated(true)
.async(true)
.supportsEmbedException(true)
.causesReaction(true)
.parameters(parameters)
.help(helpInfo)

View File

@@ -15,6 +15,7 @@ import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
@Component
public class Suggest extends AbstractConditionableCommand {
@@ -23,14 +24,14 @@ public class Suggest extends AbstractConditionableCommand {
private SuggestionService suggestionService;
@Override
public CommandResult execute(CommandContext commandContext) {
public CompletableFuture<CommandResult> executeAsync(CommandContext commandContext) {
checkParameters(commandContext);
List<Object> parameters = commandContext.getParameters().getParameters();
String text = (String) parameters.get(0);
SuggestionLog suggestLogModel = (SuggestionLog) ContextConverter.fromCommandContext(commandContext, SuggestionLog.class);
suggestLogModel.setSuggester(commandContext.getAuthor());
suggestionService.createSuggestion(commandContext.getAuthor(), text, suggestLogModel);
return CommandResult.fromSuccess();
return suggestionService.createSuggestion(commandContext.getAuthor(), text, suggestLogModel)
.thenApply(aVoid -> CommandResult.fromSuccess());
}
@Override
@@ -42,6 +43,8 @@ public class Suggest extends AbstractConditionableCommand {
.name("suggest")
.module(UtilityModuleInterface.UTILITY)
.templated(true)
.async(true)
.supportsEmbedException(true)
.causesReaction(true)
.parameters(parameters)
.help(helpInfo)

View File

@@ -1,5 +1,8 @@
package dev.sheldan.abstracto.utility.service;
import dev.sheldan.abstracto.core.exception.AbstractoRunTimeException;
import dev.sheldan.abstracto.core.exception.ChannelNotFoundException;
import dev.sheldan.abstracto.core.exception.GuildNotFoundException;
import dev.sheldan.abstracto.core.models.database.AUserInAServer;
import dev.sheldan.abstracto.core.service.management.UserInServerManagementService;
import dev.sheldan.abstracto.templating.model.MessageToSend;
@@ -57,7 +60,7 @@ public class SuggestionServiceBean implements SuggestionService {
private SuggestionServiceBean self;
@Override
public void createSuggestion(Member member, String text, SuggestionLog suggestionLog) {
public CompletableFuture<Void> createSuggestion(Member member, String text, SuggestionLog suggestionLog) {
Suggestion suggestion = suggestionManagementService.createSuggestion(member, text);
suggestionLog.setSuggestion(suggestion);
suggestionLog.setSuggesterUser(suggestion.getSuggester());
@@ -66,31 +69,30 @@ public class SuggestionServiceBean implements SuggestionService {
MessageToSend messageToSend = templateService.renderEmbedTemplate(SUGGESTION_LOG_TEMPLATE, suggestionLog);
long guildId = member.getGuild().getIdLong();
List<CompletableFuture<Message>> completableFutures = postTargetService.sendEmbedInPostTarget(messageToSend, SuggestionPostTarget.SUGGESTION, guildId);
CompletableFuture.allOf(completableFutures.toArray(new CompletableFuture[0])).thenAccept(aVoid -> {
return CompletableFuture.allOf(completableFutures.toArray(new CompletableFuture[0])).thenCompose(aVoid -> {
Suggestion innerSuggestion = suggestionManagementService.getSuggestion(suggestionId).orElseThrow(() -> new SuggestionNotFoundException(suggestionId));
try {
Message message = completableFutures.get(0).get();
suggestionManagementService.setPostedMessage(innerSuggestion, message);
messageService.addReactionToMessage(SUGGESTION_YES_EMOTE, guildId, message);
messageService.addReactionToMessage(SUGGESTION_NO_EMOTE, guildId, message);
CompletableFuture<Void> firstReaction = messageService.addReactionToMessageWithFuture(SUGGESTION_YES_EMOTE, guildId, message);
CompletableFuture<Void> secondReaction = messageService.addReactionToMessageWithFuture(SUGGESTION_NO_EMOTE, guildId, message);
return CompletableFuture.allOf(firstReaction, secondReaction);
} catch (InterruptedException | ExecutionException e) {
log.warn("Failed to post suggestion", e);
Thread.currentThread().interrupt();
}
}) .exceptionally(throwable -> {
log.error("Failed to post suggestion {}", suggestionId, throwable);
return null;
throw new AbstractoRunTimeException();
});
}
@Override
public void acceptSuggestion(Long suggestionId, String text, SuggestionLog suggestionLog) {
public CompletableFuture<Void> acceptSuggestion(Long suggestionId, String text, SuggestionLog suggestionLog) {
Suggestion suggestion = suggestionManagementService.getSuggestion(suggestionId).orElseThrow(() -> new SuggestionNotFoundException(suggestionId));
suggestionManagementService.setSuggestionState(suggestion, SuggestionState.ACCEPTED);
updateSuggestion(text, suggestionLog, suggestion);
return updateSuggestion(text, suggestionLog, suggestion);
}
private void updateSuggestion(String text, SuggestionLog suggestionLog, Suggestion suggestion) {
private CompletableFuture<Void> updateSuggestion(String text, SuggestionLog suggestionLog, Suggestion suggestion) {
suggestionLog.setSuggesterUser(suggestion.getSuggester());
Long channelId = suggestion.getChannel().getId();
Long originalMessageId = suggestion.getMessageId();
@@ -108,36 +110,39 @@ public class SuggestionServiceBean implements SuggestionService {
suggestionLog.setSuggestion(suggestion);
TextChannel textChannelById = guildById.getTextChannelById(channelId);
if(textChannelById != null) {
textChannelById.retrieveMessageById(originalMessageId).queue(message ->
return textChannelById.retrieveMessageById(originalMessageId).submit().thenCompose(message ->
self.updateSuggestionMessageText(text, suggestionLog, message)
);
} else {
log.warn("Not possible to update suggestion {}, because text channel {} was not found in guild {}.", suggestion.getId(), channelId, serverId);
throw new ChannelNotFoundException(channelId);
}
} else {
log.warn("Not possible to update suggestion {}, because guild {} was not found.", suggestion.getId(), serverId);
throw new GuildNotFoundException(serverId);
}
}
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void updateSuggestionMessageText(String text, SuggestionLog suggestionLog, Message message) {
public CompletableFuture<Void> updateSuggestionMessageText(String text, SuggestionLog suggestionLog, Message message) {
Optional<MessageEmbed> embedOptional = message.getEmbeds().stream().filter(embed -> embed.getDescription() != null).findFirst();
if(embedOptional.isPresent()) {
MessageEmbed suggestionEmbed = embedOptional.get();
suggestionLog.setReason(text);
suggestionLog.setText(suggestionEmbed.getDescription());
MessageToSend messageToSend = templateService.renderEmbedTemplate(SUGGESTION_LOG_TEMPLATE, suggestionLog);
postTargetService.sendEmbedInPostTarget(messageToSend, SuggestionPostTarget.SUGGESTION, suggestionLog.getServer().getId());
List<CompletableFuture<Message>> completableFutures = postTargetService.sendEmbedInPostTarget(messageToSend, SuggestionPostTarget.SUGGESTION, suggestionLog.getServer().getId());
return CompletableFuture.allOf(completableFutures.toArray(new CompletableFuture[0]));
} else {
log.warn("The message to update the suggestion for, did not contain an embed to update. Suggestions require an embed with a description as a container. MessageURL: {}", message.getJumpUrl());
throw new SuggestionUpdateException("Not possible to update suggestion.");
throw new SuggestionUpdateException();
}
}
@Override
public void rejectSuggestion(Long suggestionId, String text, SuggestionLog log) {
public CompletableFuture<Void> rejectSuggestion(Long suggestionId, String text, SuggestionLog log) {
Suggestion suggestion = suggestionManagementService.getSuggestion(suggestionId).orElseThrow(() -> new SuggestionNotFoundException(suggestionId));
suggestionManagementService.setSuggestionState(suggestion, SuggestionState.REJECTED);
updateSuggestion(text, log, suggestion);
return updateSuggestion(text, log, suggestion);
}
}

View File

@@ -1,6 +1,6 @@
package dev.sheldan.abstracto.utility.commands;
import dev.sheldan.abstracto.core.command.exception.IncorrectParameter;
import dev.sheldan.abstracto.core.command.exception.IncorrectParameterException;
import dev.sheldan.abstracto.core.command.execution.CommandContext;
import dev.sheldan.abstracto.core.command.execution.CommandResult;
import dev.sheldan.abstracto.core.service.ChannelService;
@@ -30,7 +30,7 @@ public class ShowAvatarTest {
@Captor
private ArgumentCaptor<ShowAvatarModel> argumentCaptor;
@Test(expected = IncorrectParameter.class)
@Test(expected = IncorrectParameterException.class)
public void testIncorrectParameterType() {
CommandTestUtilities.executeWrongParametersTest(testUnit);
}

View File

@@ -1,7 +1,7 @@
package dev.sheldan.abstracto.utility.commands;
import dev.sheldan.abstracto.core.command.exception.IncorrectParameter;
import dev.sheldan.abstracto.core.command.exception.InsufficientParameters;
import dev.sheldan.abstracto.core.command.exception.IncorrectParameterException;
import dev.sheldan.abstracto.core.command.exception.InsufficientParametersException;
import dev.sheldan.abstracto.core.command.execution.CommandContext;
import dev.sheldan.abstracto.core.command.execution.CommandResult;
import dev.sheldan.abstracto.core.service.ChannelService;
@@ -31,12 +31,12 @@ public class ShowEmoteTest {
@Captor
private ArgumentCaptor<ShowEmoteLog> emoteLogArgumentCaptor;
@Test(expected = IncorrectParameter.class)
@Test(expected = IncorrectParameterException.class)
public void testIncorrectParameterType() {
CommandTestUtilities.executeWrongParametersTest(testUnit);
}
@Test(expected = InsufficientParameters.class)
@Test(expected = InsufficientParametersException.class)
public void testTooLittleParameters() {
CommandTestUtilities.executeNoParametersTest(testUnit);
}

View File

@@ -1,6 +1,6 @@
package dev.sheldan.abstracto.utility.commands;
import dev.sheldan.abstracto.core.command.exception.IncorrectParameter;
import dev.sheldan.abstracto.core.command.exception.IncorrectParameterException;
import dev.sheldan.abstracto.core.command.execution.CommandContext;
import dev.sheldan.abstracto.core.command.execution.CommandResult;
import dev.sheldan.abstracto.core.service.BotService;
@@ -38,7 +38,7 @@ public class UserInfoTest {
@Captor
private ArgumentCaptor<UserInfoModel> modelArgumentCaptor;
@Test(expected = IncorrectParameter.class)
@Test(expected = IncorrectParameterException.class)
public void testIncorrectParameterType() {
CommandTestUtilities.executeWrongParametersTest(testUnit);
}

View File

@@ -1,7 +1,7 @@
package dev.sheldan.abstracto.utility.commands.remind;
import dev.sheldan.abstracto.core.command.exception.IncorrectParameter;
import dev.sheldan.abstracto.core.command.exception.InsufficientParameters;
import dev.sheldan.abstracto.core.command.exception.IncorrectParameterException;
import dev.sheldan.abstracto.core.command.exception.InsufficientParametersException;
import dev.sheldan.abstracto.core.command.execution.CommandContext;
import dev.sheldan.abstracto.core.command.execution.CommandResult;
import dev.sheldan.abstracto.core.service.ChannelService;
@@ -38,17 +38,17 @@ public class RemindTest {
@Captor
private ArgumentCaptor<ReminderModel> captor;
@Test(expected = InsufficientParameters.class)
@Test(expected = InsufficientParametersException.class)
public void testTooLittleParameters() {
CommandTestUtilities.executeNoParametersTest(testUnit);
}
@Test(expected = IncorrectParameter.class)
@Test(expected = IncorrectParameterException.class)
public void testIncorrectParameterType() {
CommandTestUtilities.executeWrongParametersTest(testUnit);
}
@Test(expected = InsufficientParameters.class)
@Test(expected = InsufficientParametersException.class)
public void testOnlyRemindDateParameter() {
CommandContext durationParameter = CommandTestUtilities.getWithParameters(Arrays.asList(Duration.ofDays(4)));
testUnit.execute(durationParameter);

View File

@@ -1,7 +1,7 @@
package dev.sheldan.abstracto.utility.commands.remind;
import dev.sheldan.abstracto.core.command.exception.IncorrectParameter;
import dev.sheldan.abstracto.core.command.exception.InsufficientParameters;
import dev.sheldan.abstracto.core.command.exception.IncorrectParameterException;
import dev.sheldan.abstracto.core.command.exception.InsufficientParametersException;
import dev.sheldan.abstracto.core.command.execution.CommandContext;
import dev.sheldan.abstracto.core.command.execution.CommandResult;
import dev.sheldan.abstracto.test.command.CommandConfigValidator;
@@ -26,12 +26,12 @@ public class UnRemindTest {
@Mock
private ReminderService reminderService;
@Test(expected = IncorrectParameter.class)
@Test(expected = IncorrectParameterException.class)
public void testIncorrectParameterType() {
CommandTestUtilities.executeWrongParametersTest(testUnit);
}
@Test(expected = InsufficientParameters.class)
@Test(expected = InsufficientParametersException.class)
public void testTooLittleParameters() {
CommandTestUtilities.executeNoParametersTest(testUnit);
}

View File

@@ -1,7 +1,7 @@
package dev.sheldan.abstracto.utility.commands.suggest;
import dev.sheldan.abstracto.core.command.exception.IncorrectParameter;
import dev.sheldan.abstracto.core.command.exception.InsufficientParameters;
import dev.sheldan.abstracto.core.command.exception.IncorrectParameterException;
import dev.sheldan.abstracto.core.command.exception.InsufficientParametersException;
import dev.sheldan.abstracto.core.command.execution.CommandContext;
import dev.sheldan.abstracto.core.command.execution.CommandResult;
import dev.sheldan.abstracto.test.command.CommandConfigValidator;
@@ -15,11 +15,12 @@ import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.Arrays;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.*;
@RunWith(MockitoJUnitRunner.class)
public class AcceptTest {
@@ -30,24 +31,25 @@ public class AcceptTest {
@Mock
private SuggestionService suggestionService;
@Test(expected = InsufficientParameters.class)
@Test(expected = InsufficientParametersException.class)
public void testTooLittleParameters() {
CommandTestUtilities.executeNoParametersTest(testUnit);
CommandTestUtilities.executeAsyncNoParametersTest(testUnit);
}
@Test(expected = IncorrectParameter.class)
@Test(expected = IncorrectParameterException.class)
public void testIncorrectParameterType() {
CommandTestUtilities.executeWrongParametersTest(testUnit);
CommandTestUtilities.executeAsyncWrongParametersTest(testUnit);
}
@Test
public void testExecuteCommand() {
public void testExecuteCommand() throws ExecutionException, InterruptedException {
String text = "text";
Long suggestionId = 5L;
CommandContext context = CommandTestUtilities.getWithParameters(Arrays.asList(suggestionId, text));
CommandResult result = testUnit.execute(context);
when(suggestionService.acceptSuggestion(eq(suggestionId), eq(text), any(SuggestionLog.class))).thenReturn(CompletableFuture.completedFuture(null));
CompletableFuture<CommandResult> result = testUnit.executeAsync(context);
verify(suggestionService, times(1)).acceptSuggestion(eq(suggestionId), eq(text), any(SuggestionLog.class));
CommandTestUtilities.checkSuccessfulCompletion(result);
CommandTestUtilities.checkSuccessfulCompletion(result.get());
}
@Test

View File

@@ -1,7 +1,7 @@
package dev.sheldan.abstracto.utility.commands.suggest;
import dev.sheldan.abstracto.core.command.exception.IncorrectParameter;
import dev.sheldan.abstracto.core.command.exception.InsufficientParameters;
import dev.sheldan.abstracto.core.command.exception.IncorrectParameterException;
import dev.sheldan.abstracto.core.command.exception.InsufficientParametersException;
import dev.sheldan.abstracto.core.command.execution.CommandContext;
import dev.sheldan.abstracto.core.command.execution.CommandResult;
import dev.sheldan.abstracto.test.command.CommandConfigValidator;
@@ -15,11 +15,12 @@ import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.Arrays;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.*;
@RunWith(MockitoJUnitRunner.class)
public class RejectTest {
@@ -30,24 +31,24 @@ public class RejectTest {
@Mock
private SuggestionService suggestionService;
@Test(expected = InsufficientParameters.class)
@Test(expected = InsufficientParametersException.class)
public void testTooLittleParameters() {
CommandTestUtilities.executeNoParametersTest(testUnit);
CommandTestUtilities.executeAsyncNoParametersTest(testUnit);
}
@Test(expected = IncorrectParameter.class)
@Test(expected = IncorrectParameterException.class)
public void testIncorrectParameterType() {
CommandTestUtilities.executeWrongParametersTest(testUnit);
CommandTestUtilities.executeAsyncWrongParametersTest(testUnit);
}
@Test
public void testExecuteCommand() {
public void testExecuteCommand() throws ExecutionException, InterruptedException {
String text = "text";
Long suggestionId = 5L;
CommandContext context = CommandTestUtilities.getWithParameters(Arrays.asList(suggestionId, text));
CommandResult result = testUnit.execute(context);
when(suggestionService.rejectSuggestion(eq(suggestionId), eq(text), any(SuggestionLog.class))).thenReturn(CompletableFuture.completedFuture(null));
CompletableFuture<CommandResult> result = testUnit.executeAsync(context);
verify(suggestionService, times(1)).rejectSuggestion(eq(suggestionId), eq(text), any(SuggestionLog.class));
CommandTestUtilities.checkSuccessfulCompletion(result);
CommandTestUtilities.checkSuccessfulCompletion(result.get());
}
@Test

View File

@@ -1,7 +1,7 @@
package dev.sheldan.abstracto.utility.commands.suggest;
import dev.sheldan.abstracto.core.command.exception.IncorrectParameter;
import dev.sheldan.abstracto.core.command.exception.InsufficientParameters;
import dev.sheldan.abstracto.core.command.exception.IncorrectParameterException;
import dev.sheldan.abstracto.core.command.exception.InsufficientParametersException;
import dev.sheldan.abstracto.core.command.execution.CommandContext;
import dev.sheldan.abstracto.core.command.execution.CommandResult;
import dev.sheldan.abstracto.test.command.CommandConfigValidator;
@@ -15,6 +15,8 @@ import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.Arrays;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import static org.mockito.Mockito.*;
@@ -27,23 +29,24 @@ public class SuggestTest {
@Mock
private SuggestionService suggestionService;
@Test(expected = InsufficientParameters.class)
@Test(expected = InsufficientParametersException.class)
public void testTooLittleParameters() {
CommandTestUtilities.executeNoParametersTest(testUnit);
CommandTestUtilities.executeAsyncNoParametersTest(testUnit);
}
@Test(expected = IncorrectParameter.class)
@Test(expected = IncorrectParameterException.class)
public void testIncorrectParameterType() {
CommandTestUtilities.executeWrongParametersTest(testUnit);
CommandTestUtilities.executeAsyncWrongParametersTest(testUnit);
}
@Test
public void testExecuteCommand() {
public void testExecuteCommand() throws ExecutionException, InterruptedException {
String text = "text";
CommandContext context = CommandTestUtilities.getWithParameters(Arrays.asList(text));
CommandResult result = testUnit.execute(context);
when(suggestionService.createSuggestion(eq(context.getAuthor()), eq(text), any(SuggestionLog.class))).thenReturn(CompletableFuture.completedFuture(null));
CompletableFuture<CommandResult> result = testUnit.executeAsync(context);
verify(suggestionService, times(1)).createSuggestion(eq(context.getAuthor()), eq(text), any(SuggestionLog.class));
CommandTestUtilities.checkSuccessfulCompletion(result);
CommandTestUtilities.checkSuccessfulCompletion(result.get());
}
@Test

View File

@@ -1,5 +1,7 @@
package dev.sheldan.abstracto.utility.service;
import dev.sheldan.abstracto.core.exception.ChannelNotFoundException;
import dev.sheldan.abstracto.core.exception.GuildNotFoundException;
import dev.sheldan.abstracto.core.models.database.AChannel;
import dev.sheldan.abstracto.core.models.database.AServer;
import dev.sheldan.abstracto.core.models.database.AUserInAServer;
@@ -29,7 +31,6 @@ import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;
import static org.mockito.Mockito.*;
@@ -86,8 +87,8 @@ public class SuggestionServiceBeanTest {
when(suggestionManagementService.getSuggestion(suggestionId)).thenReturn(Optional.of(createdSuggestion));
testUnit.createSuggestion(suggestionCreator, suggestionText, log);
verify(suggestionManagementService, times(1)).setPostedMessage(createdSuggestion, suggestionMessage);
verify( messageService, times(1)).addReactionToMessage(SuggestionServiceBean.SUGGESTION_YES_EMOTE, server.getId(), suggestionMessage);
verify( messageService, times(1)).addReactionToMessage(SuggestionServiceBean.SUGGESTION_NO_EMOTE, server.getId(), suggestionMessage);
verify( messageService, times(1)).addReactionToMessageWithFuture(SuggestionServiceBean.SUGGESTION_YES_EMOTE, server.getId(), suggestionMessage);
verify( messageService, times(1)).addReactionToMessageWithFuture(SuggestionServiceBean.SUGGESTION_NO_EMOTE, server.getId(), suggestionMessage);
}
@Test
@@ -108,7 +109,7 @@ public class SuggestionServiceBeanTest {
executeAcceptWithMember(null);
}
@Test
@Test(expected = ChannelNotFoundException.class)
public void testAcceptSuggestionInNoTextChannel() {
Long suggestionId = 5L;
setupForNoTextChannel(suggestionId);
@@ -137,7 +138,7 @@ public class SuggestionServiceBeanTest {
when(guild.getTextChannelById(channelId)).thenReturn(null);
}
@Test
@Test(expected = GuildNotFoundException.class)
public void testAcceptSuggestionInNoGuild() {
Long suggestionId = 5L;
setupForNoGuild(suggestionId);
@@ -183,13 +184,13 @@ public class SuggestionServiceBeanTest {
executeRejectWithMember(null);
}
@Test
@Test(expected = ChannelNotFoundException.class)
public void testRejectSuggestionInNoTextChannel() {
Long suggestionId = setupForNoTextChannel();
testUnit.rejectSuggestion(suggestionId, CLOSING_TEXT, SuggestionLog.builder().build());
}
@Test
@Test(expected = GuildNotFoundException.class)
public void testRejectSuggestionInNoGuild() {
Long suggestionId = 5L;
setupForNoGuild(suggestionId);
@@ -225,9 +226,10 @@ public class SuggestionServiceBeanTest {
Suggestion suggestionToAccept = setupClosing(suggesterMember, suggestionId, channelId, messageId);
RestAction<Message> retrievalAction = Mockito.mock(RestAction.class);
when(textChannel.retrieveMessageById(messageId)).thenReturn(retrievalAction);
Message suggestionMessage = Mockito.mock(Message.class);
when(retrievalAction.submit()).thenReturn(CompletableFuture.completedFuture(suggestionMessage));
testUnit.acceptSuggestion(suggestionId, CLOSING_TEXT, logParameter);
verify(suggestionManagementService, times(1)).setSuggestionState(suggestionToAccept, SuggestionState.ACCEPTED);
verify(retrievalAction, times(1)).queue(any(Consumer.class));
}
private void executeRejectWithMember(Member suggesterMember) {
@@ -238,9 +240,10 @@ public class SuggestionServiceBeanTest {
Suggestion suggestionToAccept = setupClosing(suggesterMember, suggestionId, channelId, messageId);
RestAction<Message> retrievalAction = Mockito.mock(RestAction.class);
when(textChannel.retrieveMessageById(messageId)).thenReturn(retrievalAction);
Message suggestionMessage = Mockito.mock(Message.class);
when(retrievalAction.submit()).thenReturn(CompletableFuture.completedFuture(suggestionMessage));
testUnit.rejectSuggestion(suggestionId, CLOSING_TEXT, logParameter);
verify(suggestionManagementService, times(1)).setSuggestionState(suggestionToAccept, SuggestionState.REJECTED);
verify(retrievalAction, times(1)).queue(any(Consumer.class));
}
private Suggestion setupClosing(Member suggesterMember, Long suggestionId, Long channelId, Long messageId) {