From ff2f5b1484993dee7bacd8b00d2f7d15cc336cf3 Mon Sep 17 00:00:00 2001 From: Abhishek Anuj <26abhishekanuj@gmail.com> Date: Thu, 23 Feb 2023 02:56:12 +0530 Subject: [PATCH 1/6] [Abhishek]Add verify the content length [Abhishek]Add verify the content length --- .../tjbot/features/basic/SlashCommandEducator.java | 3 ++- .../tjbot/features/basic/SlashCommandEducatorTest.java | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/features/basic/SlashCommandEducator.java b/application/src/main/java/org/togetherjava/tjbot/features/basic/SlashCommandEducator.java index a9d8056e1a..143feb95b3 100644 --- a/application/src/main/java/org/togetherjava/tjbot/features/basic/SlashCommandEducator.java +++ b/application/src/main/java/org/togetherjava/tjbot/features/basic/SlashCommandEducator.java @@ -33,7 +33,8 @@ public void onMessageReceived(MessageReceivedEvent event) { } String content = event.getMessage().getContentRaw(); - if (IS_MESSAGE_COMMAND.test(content)) { + int MAX_COMMAND_LENGTH = 30; + if (IS_MESSAGE_COMMAND.test(content) && content.length() < MAX_COMMAND_LENGTH) { sendAdvice(event.getMessage()); } } diff --git a/application/src/test/java/org/togetherjava/tjbot/features/basic/SlashCommandEducatorTest.java b/application/src/test/java/org/togetherjava/tjbot/features/basic/SlashCommandEducatorTest.java index 6e118a5c76..70082caa8e 100644 --- a/application/src/test/java/org/togetherjava/tjbot/features/basic/SlashCommandEducatorTest.java +++ b/application/src/test/java/org/togetherjava/tjbot/features/basic/SlashCommandEducatorTest.java @@ -65,6 +65,6 @@ private static Stream provideMessageCommands() { } private static Stream provideOtherMessages() { - return Stream.of(" a ", "foo", "#foo", "/foo", "!!!", "?!?!?", "?", ".,-", "!f", "! foo"); + return Stream.of(" a ", "foo", "#foo", "/foo", "!!!", "?!?!?", "?", ".,-", "!f", "! foo","thisIsAWordWhichLengthIsMoreThanThrityLetterSoItShouldNotReply"); } } From d4c5bdfcae91a448d5372a7cb18ee9ad4d691fbd Mon Sep 17 00:00:00 2001 From: Abhishek Anuj <26abhishekanuj@gmail.com> Date: Thu, 23 Feb 2023 10:50:33 +0530 Subject: [PATCH 2/6] [Abhishek] Made the max content length as constant --- .../tjbot/features/basic/SlashCommandEducator.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/features/basic/SlashCommandEducator.java b/application/src/main/java/org/togetherjava/tjbot/features/basic/SlashCommandEducator.java index 143feb95b3..c9be00d66d 100644 --- a/application/src/main/java/org/togetherjava/tjbot/features/basic/SlashCommandEducator.java +++ b/application/src/main/java/org/togetherjava/tjbot/features/basic/SlashCommandEducator.java @@ -19,6 +19,7 @@ * then educates the user about using slash commands, such as {@code /foo} instead. */ public final class SlashCommandEducator extends MessageReceiverAdapter { + private static final int MAX_CONTENT_LENGTH = 30; private static final String SLASH_COMMAND_POPUP_ADVICE_PATH = "slashCommandPopupAdvice.png"; private static final Predicate IS_MESSAGE_COMMAND = Pattern.compile(""" [.!?] #Start of message command @@ -33,8 +34,8 @@ public void onMessageReceived(MessageReceivedEvent event) { } String content = event.getMessage().getContentRaw(); - int MAX_COMMAND_LENGTH = 30; - if (IS_MESSAGE_COMMAND.test(content) && content.length() < MAX_COMMAND_LENGTH) { + + if (IS_MESSAGE_COMMAND.test(content) && content.length() < MAX_CONTENT_LENGTH) { sendAdvice(event.getMessage()); } } From f5b818f3eddfe5278725b1590be4c8504bfa1fc6 Mon Sep 17 00:00:00 2001 From: Abhishek Anuj <26abhishekanuj@gmail.com> Date: Thu, 23 Feb 2023 12:30:04 +0530 Subject: [PATCH 3/6] [Abhishek] Added. Test when message's length is greater than thirty. --- .../basic/SlashCommandEducatorTest.java | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/application/src/test/java/org/togetherjava/tjbot/features/basic/SlashCommandEducatorTest.java b/application/src/test/java/org/togetherjava/tjbot/features/basic/SlashCommandEducatorTest.java index 70082caa8e..96cde733bc 100644 --- a/application/src/test/java/org/togetherjava/tjbot/features/basic/SlashCommandEducatorTest.java +++ b/application/src/test/java/org/togetherjava/tjbot/features/basic/SlashCommandEducatorTest.java @@ -60,11 +60,27 @@ void ignoresOtherMessages(String message) { verify(event.getMessage(), never()).replyEmbeds(any(MessageEmbed.class)); } + @ParameterizedTest + @MethodSource("provideOtherCommands") + void ignoresMessagesWhereLengthIsGreaterThanThirty(String message) { + // GIVEN a message's length is more than Thirty + // WHEN the message is sent + MessageReceivedEvent event = sendMessage(message); + + // THEN the system ignores the message and does not reply to it + verify(event.getMessage(), never()).replyEmbeds(any(MessageEmbed.class)); + } + private static Stream provideMessageCommands() { return Stream.of("!foo", ".foo", "?foo", ".test", "!whatever", "!this is a test"); } private static Stream provideOtherMessages() { - return Stream.of(" a ", "foo", "#foo", "/foo", "!!!", "?!?!?", "?", ".,-", "!f", "! foo","thisIsAWordWhichLengthIsMoreThanThrityLetterSoItShouldNotReply"); + return Stream.of(" a ", "foo", "#foo", "/foo", "!!!", "?!?!?", "?", ".,-", "!f", "! foo"); + } + + private static Stream provideOtherCommands() { + return Stream.of("thisIsAWordWhichLengthIsMoreThanThirtyLetterSoItShouldNotReply", + "abcdeabcdeabcdeabcdeabcdeabcdeabcdeabcde"); } } From 6136542a7902f9f4cccf6a1a5bff4e3fa859e70a Mon Sep 17 00:00:00 2001 From: Abhishek Anuj <26abhishekanuj@gmail.com> Date: Thu, 23 Feb 2023 13:36:33 +0530 Subject: [PATCH 4/6] [Abhishek] Refactored. content to command --- .../tjbot/features/basic/SlashCommandEducator.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/features/basic/SlashCommandEducator.java b/application/src/main/java/org/togetherjava/tjbot/features/basic/SlashCommandEducator.java index c9be00d66d..9a6e77464a 100644 --- a/application/src/main/java/org/togetherjava/tjbot/features/basic/SlashCommandEducator.java +++ b/application/src/main/java/org/togetherjava/tjbot/features/basic/SlashCommandEducator.java @@ -19,7 +19,7 @@ * then educates the user about using slash commands, such as {@code /foo} instead. */ public final class SlashCommandEducator extends MessageReceiverAdapter { - private static final int MAX_CONTENT_LENGTH = 30; + private static final int MAX_COMMAND_LENGTH = 30; private static final String SLASH_COMMAND_POPUP_ADVICE_PATH = "slashCommandPopupAdvice.png"; private static final Predicate IS_MESSAGE_COMMAND = Pattern.compile(""" [.!?] #Start of message command @@ -35,7 +35,7 @@ public void onMessageReceived(MessageReceivedEvent event) { String content = event.getMessage().getContentRaw(); - if (IS_MESSAGE_COMMAND.test(content) && content.length() < MAX_CONTENT_LENGTH) { + if (IS_MESSAGE_COMMAND.test(content) && content.length() < MAX_COMMAND_LENGTH) { sendAdvice(event.getMessage()); } } From ac2a64c5c6c62470583f8eef95ac6596b436ba93 Mon Sep 17 00:00:00 2001 From: Abhishek Anuj <26abhishekanuj@gmail.com> Date: Thu, 23 Feb 2023 13:43:10 +0530 Subject: [PATCH 5/6] [Abhishek] Refactored. test cases --- .../tjbot/features/basic/SlashCommandEducatorTest.java | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/application/src/test/java/org/togetherjava/tjbot/features/basic/SlashCommandEducatorTest.java b/application/src/test/java/org/togetherjava/tjbot/features/basic/SlashCommandEducatorTest.java index 96cde733bc..819b58451b 100644 --- a/application/src/test/java/org/togetherjava/tjbot/features/basic/SlashCommandEducatorTest.java +++ b/application/src/test/java/org/togetherjava/tjbot/features/basic/SlashCommandEducatorTest.java @@ -61,7 +61,7 @@ void ignoresOtherMessages(String message) { } @ParameterizedTest - @MethodSource("provideOtherCommands") + @MethodSource("provideOtherMessages") void ignoresMessagesWhereLengthIsGreaterThanThirty(String message) { // GIVEN a message's length is more than Thirty // WHEN the message is sent @@ -76,11 +76,9 @@ private static Stream provideMessageCommands() { } private static Stream provideOtherMessages() { - return Stream.of(" a ", "foo", "#foo", "/foo", "!!!", "?!?!?", "?", ".,-", "!f", "! foo"); - } + return Stream.of(" a ", "foo", "#foo", "/foo", "!!!", "?!?!?", "?", ".,-", "!f", "! foo", + "thisIsAWordWhichLengthIsMoreThanThirtyLetterSoItShouldNotReply", + ".isLetter and .isNumber are available"); - private static Stream provideOtherCommands() { - return Stream.of("thisIsAWordWhichLengthIsMoreThanThirtyLetterSoItShouldNotReply", - "abcdeabcdeabcdeabcdeabcdeabcdeabcdeabcde"); } } From 6c1606e12bb8aee77a3aba9a60e6416fa5fb940a Mon Sep 17 00:00:00 2001 From: Abhishek Anuj <26abhishekanuj@gmail.com> Date: Thu, 23 Feb 2023 14:02:45 +0530 Subject: [PATCH 6/6] [Abhishek] Removed. Unnecessary test case --- .../features/basic/SlashCommandEducatorTest.java | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/application/src/test/java/org/togetherjava/tjbot/features/basic/SlashCommandEducatorTest.java b/application/src/test/java/org/togetherjava/tjbot/features/basic/SlashCommandEducatorTest.java index 819b58451b..261e1db3af 100644 --- a/application/src/test/java/org/togetherjava/tjbot/features/basic/SlashCommandEducatorTest.java +++ b/application/src/test/java/org/togetherjava/tjbot/features/basic/SlashCommandEducatorTest.java @@ -60,17 +60,6 @@ void ignoresOtherMessages(String message) { verify(event.getMessage(), never()).replyEmbeds(any(MessageEmbed.class)); } - @ParameterizedTest - @MethodSource("provideOtherMessages") - void ignoresMessagesWhereLengthIsGreaterThanThirty(String message) { - // GIVEN a message's length is more than Thirty - // WHEN the message is sent - MessageReceivedEvent event = sendMessage(message); - - // THEN the system ignores the message and does not reply to it - verify(event.getMessage(), never()).replyEmbeds(any(MessageEmbed.class)); - } - private static Stream provideMessageCommands() { return Stream.of("!foo", ".foo", "?foo", ".test", "!whatever", "!this is a test"); }