diff --git a/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/methods/AnswerInlineQuery.java b/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/methods/AnswerInlineQuery.java index 5f4e3572..97ec161e 100644 --- a/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/methods/AnswerInlineQuery.java +++ b/telegrambots-meta/src/main/java/org/telegram/telegrambots/api/methods/AnswerInlineQuery.java @@ -12,6 +12,7 @@ import org.telegram.telegrambots.exceptions.TelegramApiValidationException; import java.io.IOException; import java.util.Arrays; import java.util.List; +import java.util.regex.Pattern; /** * @author Ruben Bermudez @@ -119,12 +120,26 @@ public class AnswerInlineQuery extends BotApiMethod { @Override public void validate() throws TelegramApiValidationException { - if (inlineQueryId == null) { + if (inlineQueryId == null || inlineQueryId.isEmpty()) { throw new TelegramApiValidationException("InlineQueryId can't be empty", this); } if (results == null) { throw new TelegramApiValidationException("Results array can't be null", this); } + if (switchPmText != null) { + if (switchPmText.isEmpty()) { + throw new TelegramApiValidationException("SwitchPmText can't be empty", this); + } + if (switchPmParameter == null || switchPmParameter.isEmpty()) { + throw new TelegramApiValidationException("SwitchPmParameter can't be empty if switchPmText is present", this); + } + if (switchPmParameter.length() > 64) { + throw new TelegramApiValidationException("SwitchPmParameter can't be longer than 64 chars", this); + } + if (!Pattern.matches("[A-Za-z0-9_]+", switchPmParameter.trim() )) { + throw new TelegramApiValidationException("SwitchPmParameter only allows A-Z, a-z, 0-9 and _ characters", this); + } + } for (InlineQueryResult result : results) { result.validate(); } diff --git a/telegrambots-meta/src/test/java/org/telegram/telegrambots/test/apimethods/TestAnswerInlineQuery.java b/telegrambots-meta/src/test/java/org/telegram/telegrambots/test/apimethods/TestAnswerInlineQuery.java new file mode 100644 index 00000000..49467789 --- /dev/null +++ b/telegrambots-meta/src/test/java/org/telegram/telegrambots/test/apimethods/TestAnswerInlineQuery.java @@ -0,0 +1,119 @@ +package org.telegram.telegrambots.test.apimethods; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.telegram.telegrambots.api.methods.AnswerInlineQuery; +import org.telegram.telegrambots.exceptions.TelegramApiValidationException; + +import java.util.ArrayList; + +/** + * @author Ruben Bermudez + * @version 1.0 + */ +public class TestAnswerInlineQuery { + private AnswerInlineQuery answerInlineQuery; + + @Before + public void setUp() throws Exception { + answerInlineQuery = new AnswerInlineQuery(); + } + + @Test + public void TestInlineQueryIdMustBePresent() throws Exception { + try { + answerInlineQuery.validate(); + } catch (TelegramApiValidationException e) { + Assert.assertEquals("InlineQueryId can't be empty", e.getMessage()); + } + } + + @Test + public void TestInlineQueryIdCanNotBeEmpty() throws Exception { + answerInlineQuery.setInlineQueryId(""); + try { + answerInlineQuery.validate(); + } catch (TelegramApiValidationException e) { + Assert.assertEquals("InlineQueryId can't be empty", e.getMessage()); + } + } + + @Test + public void TestResultsMustBePresent() throws Exception { + answerInlineQuery.setInlineQueryId("RANDOMEID"); + try { + answerInlineQuery.validate(); + } catch (TelegramApiValidationException e) { + Assert.assertEquals("Results array can't be null", e.getMessage()); + } + } + + @Test + public void TestSwitchPmTextCanNotBeEmpty() throws Exception { + answerInlineQuery.setInlineQueryId("RANDOMEID"); + answerInlineQuery.setResults(new ArrayList<>()); + answerInlineQuery.setSwitchPmText(""); + + try { + answerInlineQuery.validate(); + } catch (TelegramApiValidationException e) { + Assert.assertEquals("SwitchPmText can't be empty", e.getMessage()); + } + } + + @Test + public void TestSwitchPmParameterIsMandatoryIfSwitchPmTextIsPresent() throws Exception { + answerInlineQuery.setInlineQueryId("RANDOMEID"); + answerInlineQuery.setResults(new ArrayList<>()); + answerInlineQuery.setSwitchPmText("Test Text"); + + try { + answerInlineQuery.validate(); + } catch (TelegramApiValidationException e) { + Assert.assertEquals("SwitchPmParameter can't be empty if switchPmText is present", e.getMessage()); + } + } + + @Test + public void TestSwitchPmParameterCanNotBeEmptyIfSwitchPmTextIsPresent() throws Exception { + answerInlineQuery.setInlineQueryId("RANDOMEID"); + answerInlineQuery.setResults(new ArrayList<>()); + answerInlineQuery.setSwitchPmText("Test Text"); + answerInlineQuery.setSwitchPmParameter(""); + + try { + answerInlineQuery.validate(); + } catch (TelegramApiValidationException e) { + Assert.assertEquals("SwitchPmParameter can't be empty if switchPmText is present", e.getMessage()); + } + } + + @Test + public void TestSwitchPmParameterContainsUpTo64Chars() throws Exception { + answerInlineQuery.setInlineQueryId("RANDOMEID"); + answerInlineQuery.setResults(new ArrayList<>()); + answerInlineQuery.setSwitchPmText("Test Text"); + answerInlineQuery.setSwitchPmParameter("2AAQlw4BwzXwFNXMk5rReQC3YbhbgNqq4BGqyozjRTtrsok4shsB8u4NXeslfpOsL"); + + try { + answerInlineQuery.validate(); + } catch (TelegramApiValidationException e) { + Assert.assertEquals("SwitchPmParameter can't be longer than 64 chars", e.getMessage()); + } + } + + @Test + public void TestSwitchPmParameterOnlyContainsAcceptedCharacters() throws Exception { + answerInlineQuery.setInlineQueryId("RANDOMEID"); + answerInlineQuery.setResults(new ArrayList<>()); + answerInlineQuery.setSwitchPmText("Test Text"); + answerInlineQuery.setSwitchPmParameter("*"); + + try { + answerInlineQuery.validate(); + } catch (TelegramApiValidationException e) { + Assert.assertEquals("SwitchPmParameter only allows A-Z, a-z, 0-9 and _ characters", e.getMessage()); + } + } +}