[AB-225] adding parameter handler for messages

reworking parameter handling in command received handler
adding string parameter handler to explicitly parse strings
This commit is contained in:
Sheldan
2021-04-07 23:16:25 +02:00
parent c56a037d28
commit 400181a280
62 changed files with 815 additions and 153 deletions

View File

@@ -1,9 +1,12 @@
package dev.sheldan.abstracto.statistic.emote.command.parameter.handler;
import dev.sheldan.abstracto.core.command.Command;
import dev.sheldan.abstracto.core.command.config.Parameter;
import dev.sheldan.abstracto.core.command.execution.UnparsedCommandParameterPiece;
import dev.sheldan.abstracto.core.command.handler.CommandParameterHandler;
import dev.sheldan.abstracto.core.command.handler.CommandParameterIterators;
import dev.sheldan.abstracto.core.command.handler.provided.EmoteParameterHandler;
import dev.sheldan.abstracto.core.command.service.CommandService;
import dev.sheldan.abstracto.statistic.emote.model.database.TrackedEmote;
import dev.sheldan.abstracto.statistic.emote.service.TrackedEmoteService;
import net.dv8tion.jda.api.entities.Emote;
@@ -24,6 +27,9 @@ public class TrackedEmoteParameterHandler implements CommandParameterHandler {
@Autowired
private TrackedEmoteService trackedEmoteService;
@Autowired
private CommandService commandService;
/**
* This {@link CommandParameterHandler} only handles {@link TrackedEmote}
* @param clazz The desired {@link Class} of a parameter
@@ -42,14 +48,17 @@ public class TrackedEmoteParameterHandler implements CommandParameterHandler {
* @param input The {@link String} input at the current position
* @param iterators The {@link CommandParameterIterators} containing all available iterators to directly retrieve JDA related
* entities from
* @param clazz The {@link Class} which this type should handle
* @param param The {@link Class} which this type should handle
* @param context The {@link Message} which caused the command to be executed
* @param command
* @return A faked {@link TrackedEmote} based on the given input or from {@link CommandParameterIterators} directly. This {@link TrackedEmote}
* does not need to actually exist.
*/
@Override
public Object handle(UnparsedCommandParameterPiece input, CommandParameterIterators iterators, Class clazz, Message context) {
Emote emote = (Emote) emoteParameterHandler.handle(input, iterators, Emote.class, context);
public Object handle(UnparsedCommandParameterPiece input, CommandParameterIterators iterators, Parameter param, Message context, Command command) {
Parameter cloned = commandService.cloneParameter(param);
cloned.setType(Emote.class);
Emote emote = (Emote) emoteParameterHandler.handle(input, iterators, cloned, context, command);
if(emote != null) {
return trackedEmoteService.getFakeTrackedEmote(emote, context.getGuild());
} else {

View File

@@ -1,9 +1,12 @@
package dev.sheldan.abstracto.statistic.emote.command.parameter.handler;
import dev.sheldan.abstracto.core.command.Command;
import dev.sheldan.abstracto.core.command.config.Parameter;
import dev.sheldan.abstracto.core.command.execution.UnparsedCommandParameterPiece;
import dev.sheldan.abstracto.core.command.handler.CommandParameterHandler;
import dev.sheldan.abstracto.core.command.handler.CommandParameterIterators;
import dev.sheldan.abstracto.core.command.handler.provided.EmoteParameterHandler;
import dev.sheldan.abstracto.core.command.service.CommandService;
import dev.sheldan.abstracto.statistic.emote.command.parameter.TrackEmoteParameter;
import dev.sheldan.abstracto.statistic.emote.model.database.TrackedEmote;
import dev.sheldan.abstracto.statistic.emote.service.TrackedEmoteService;
@@ -28,6 +31,9 @@ public class TrackedEmoteParameterParameterHandler implements CommandParameterHa
@Autowired
private TrackedEmoteService trackedEmoteService;
@Autowired
private CommandService commandService;
/**
* This {@link CommandParameterHandler} only handles {@link TrackEmoteParameter}
* @param clazz The desired {@link Class} of a parameter
@@ -45,15 +51,18 @@ public class TrackedEmoteParameterParameterHandler implements CommandParameterHa
* @param input The {@link String} input at the current position
* @param iterators The {@link CommandParameterIterators} containing all available iterators to directly retrieve JDA related
* entities from
* @param clazz The {@link Class} which this type should handle
* @param param The {@link Class} which this type should handle
* @param context The {@link Message} which caused the command to be executed
* @param command
* @return An instance of {@link TrackEmoteParameter} which contains the available instances. This is an {@link Emote} in case it was
* used directly. In every successful case, it will contain a faked {@link TrackedEmote}.
*/
@Override
public Object handle(UnparsedCommandParameterPiece input, CommandParameterIterators iterators, Class clazz, Message context) {
public Object handle(UnparsedCommandParameterPiece input, CommandParameterIterators iterators, Parameter param, Message context, Command command) {
TrackEmoteParameter parameter = TrackEmoteParameter.builder().build();
Emote emote = (Emote) emoteParameterHandler.handle(input, iterators, Emote.class, context);
Parameter cloned = commandService.cloneParameter(param);
cloned.setType(Emote.class);
Emote emote = (Emote) emoteParameterHandler.handle(input, iterators, cloned, context, command);
if(emote != null) {
parameter.setEmote(emote);
parameter.setTrackedEmote(trackedEmoteService.getFakeTrackedEmote(emote, context.getGuild()));

View File

@@ -1,8 +1,11 @@
package dev.sheldan.abstracto.statistic.emote.command.handler;
import dev.sheldan.abstracto.core.command.Command;
import dev.sheldan.abstracto.core.command.config.Parameter;
import dev.sheldan.abstracto.core.command.execution.UnparsedCommandParameterPiece;
import dev.sheldan.abstracto.core.command.handler.CommandParameterIterators;
import dev.sheldan.abstracto.core.command.handler.provided.EmoteParameterHandler;
import dev.sheldan.abstracto.core.command.service.CommandService;
import dev.sheldan.abstracto.statistic.emote.command.parameter.handler.TrackedEmoteParameterHandler;
import dev.sheldan.abstracto.statistic.emote.model.database.TrackedEmote;
import dev.sheldan.abstracto.statistic.emote.service.TrackedEmoteService;
@@ -31,6 +34,9 @@ public class TrackedEmoteParameterHandlerTest {
@Mock
private TrackedEmoteService trackedEmoteService;
@Mock
private CommandService commandService;
@Mock
private Message contextMessage;
@@ -40,6 +46,15 @@ public class TrackedEmoteParameterHandlerTest {
@Mock
private Guild guild;
@Mock
private Parameter parameter;
@Mock
private Parameter parameter2;
@Mock
private Command command;
@Mock
private TrackedEmote trackedEmote;
@@ -60,9 +75,10 @@ public class TrackedEmoteParameterHandlerTest {
when(contextMessage.getGuild()).thenReturn(guild);
Emote emote = Mockito.mock(Emote.class);
UnparsedCommandParameterPiece input = Mockito.mock(UnparsedCommandParameterPiece.class);
when(emoteParameterHandler.handle(input, iterators, Emote.class, contextMessage)).thenReturn(emote);
when(commandService.cloneParameter(parameter)).thenReturn(parameter2);
when(emoteParameterHandler.handle(input, iterators, parameter2, contextMessage, command)).thenReturn(emote);
when(trackedEmoteService.getFakeTrackedEmote(emote, guild)).thenReturn(trackedEmote);
TrackedEmote parsedEmote = (TrackedEmote) testUnit.handle(input, iterators, TrackedEmote.class, contextMessage);
TrackedEmote parsedEmote = (TrackedEmote) testUnit.handle(input, iterators, parameter, contextMessage, command);
Assert.assertEquals(trackedEmote, parsedEmote);
}
@@ -72,9 +88,10 @@ public class TrackedEmoteParameterHandlerTest {
when(contextMessage.getGuild()).thenReturn(guild);
UnparsedCommandParameterPiece input = Mockito.mock(UnparsedCommandParameterPiece.class);
when(input.getValue()).thenReturn(emoteId.toString());
when(commandService.cloneParameter(parameter)).thenReturn(parameter2);
when(trackedEmoteService.getFakeTrackedEmote(emoteId, guild)).thenReturn(trackedEmote);
when(emoteParameterHandler.handle(input, iterators, Emote.class, contextMessage)).thenReturn(null);
TrackedEmote parsedEmote = (TrackedEmote) testUnit.handle(input, iterators, TrackedEmote.class, contextMessage);
when(emoteParameterHandler.handle(input, iterators, parameter2, contextMessage, command)).thenReturn(null);
TrackedEmote parsedEmote = (TrackedEmote) testUnit.handle(input, iterators, parameter, contextMessage, command);
verify(trackedEmoteService, times(0)).getFakeTrackedEmote(any(Emote.class), eq(guild));
Assert.assertEquals(trackedEmote, parsedEmote);
}
@@ -83,7 +100,8 @@ public class TrackedEmoteParameterHandlerTest {
public void testWithIllegalInput() {
UnparsedCommandParameterPiece input = Mockito.mock(UnparsedCommandParameterPiece.class);
when(input.getValue()).thenReturn(WRONG_FORMATTED_INPUT);
when(emoteParameterHandler.handle(input, iterators, Emote.class, contextMessage)).thenReturn(null);
testUnit.handle(input, iterators, TrackedEmote.class, contextMessage);
when(commandService.cloneParameter(parameter)).thenReturn(parameter2);
when(emoteParameterHandler.handle(input, iterators, parameter2, contextMessage, command)).thenReturn(null);
testUnit.handle(input, iterators, parameter, contextMessage, command);
}
}

View File

@@ -1,8 +1,11 @@
package dev.sheldan.abstracto.statistic.emote.command.handler;
import dev.sheldan.abstracto.core.command.Command;
import dev.sheldan.abstracto.core.command.config.Parameter;
import dev.sheldan.abstracto.core.command.execution.UnparsedCommandParameterPiece;
import dev.sheldan.abstracto.core.command.handler.CommandParameterIterators;
import dev.sheldan.abstracto.core.command.handler.provided.EmoteParameterHandler;
import dev.sheldan.abstracto.core.command.service.CommandService;
import dev.sheldan.abstracto.statistic.emote.command.parameter.TrackEmoteParameter;
import dev.sheldan.abstracto.statistic.emote.command.parameter.handler.TrackedEmoteParameterParameterHandler;
import dev.sheldan.abstracto.statistic.emote.model.database.TrackedEmote;
@@ -34,6 +37,9 @@ public class TrackedEmoteParameterParameterHandlerTest {
@Mock
private TrackedEmoteService trackedEmoteService;
@Mock
private CommandService commandService;
@Mock
private Message contextMessage;
@@ -46,6 +52,15 @@ public class TrackedEmoteParameterParameterHandlerTest {
@Mock
private TrackedEmote trackedEmote;
@Mock
private Parameter parameter;
@Mock
private Parameter parameter2;
@Mock
private Command command;
private static final String WRONG_FORMATTED_INPUT = "input";
@Test
@@ -63,9 +78,10 @@ public class TrackedEmoteParameterParameterHandlerTest {
when(contextMessage.getGuild()).thenReturn(guild);
Emote emote = Mockito.mock(Emote.class);
UnparsedCommandParameterPiece input = Mockito.mock(UnparsedCommandParameterPiece.class);
when(emoteParameterHandler.handle(input, iterators, Emote.class, contextMessage)).thenReturn(emote);
when(commandService.cloneParameter(parameter)).thenReturn(parameter2);
when(emoteParameterHandler.handle(input, iterators, parameter2, contextMessage, command)).thenReturn(emote);
when(trackedEmoteService.getFakeTrackedEmote(emote, guild)).thenReturn(trackedEmote);
TrackEmoteParameter parsedEmote = (TrackEmoteParameter) testUnit.handle(input, iterators, TrackedEmote.class, contextMessage);
TrackEmoteParameter parsedEmote = (TrackEmoteParameter) testUnit.handle(input, iterators, parameter, contextMessage, command);
Assert.assertEquals(trackedEmote, parsedEmote.getTrackedEmote());
Assert.assertEquals(emote, parsedEmote.getEmote());
}
@@ -77,8 +93,9 @@ public class TrackedEmoteParameterParameterHandlerTest {
UnparsedCommandParameterPiece input = Mockito.mock(UnparsedCommandParameterPiece.class);
when(input.getValue()).thenReturn(emoteId.toString());
when(trackedEmoteService.getFakeTrackedEmote(emoteId, guild)).thenReturn(trackedEmote);
when(emoteParameterHandler.handle(input, iterators, Emote.class, contextMessage)).thenReturn(null);
TrackEmoteParameter parsedEmote = (TrackEmoteParameter) testUnit.handle(input, iterators, TrackedEmote.class, contextMessage);
when(commandService.cloneParameter(parameter)).thenReturn(parameter2);
when(emoteParameterHandler.handle(input, iterators, parameter2, contextMessage, command)).thenReturn(null);
TrackEmoteParameter parsedEmote = (TrackEmoteParameter) testUnit.handle(input, iterators, parameter, contextMessage, command);
verify(trackedEmoteService, times(0)).getFakeTrackedEmote(any(Emote.class), eq(guild));
Assert.assertEquals(trackedEmote, parsedEmote.getTrackedEmote());
}
@@ -87,8 +104,9 @@ public class TrackedEmoteParameterParameterHandlerTest {
public void testWithIllegalInput() {
UnparsedCommandParameterPiece input = Mockito.mock(UnparsedCommandParameterPiece.class);
when(input.getValue()).thenReturn(WRONG_FORMATTED_INPUT);
when(emoteParameterHandler.handle(input, iterators, Emote.class, contextMessage)).thenReturn(null);
testUnit.handle(input, iterators, TrackedEmote.class, contextMessage);
when(commandService.cloneParameter(parameter)).thenReturn(parameter2);
when(emoteParameterHandler.handle(input, iterators, parameter2, contextMessage, command)).thenReturn(null);
testUnit.handle(input, iterators, parameter, contextMessage, command);
}
}