Fix new validation

This commit is contained in:
Rubenlagus 2017-02-22 23:23:55 +01:00 committed by Ruben Bermudez
parent a565b5fdd5
commit a65e1ec97e
2 changed files with 135 additions and 1 deletions

View File

@ -12,6 +12,7 @@ import org.telegram.telegrambots.exceptions.TelegramApiValidationException;
import java.io.IOException; import java.io.IOException;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.regex.Pattern;
/** /**
* @author Ruben Bermudez * @author Ruben Bermudez
@ -119,12 +120,26 @@ public class AnswerInlineQuery extends BotApiMethod<Boolean> {
@Override @Override
public void validate() throws TelegramApiValidationException { public void validate() throws TelegramApiValidationException {
if (inlineQueryId == null) { if (inlineQueryId == null || inlineQueryId.isEmpty()) {
throw new TelegramApiValidationException("InlineQueryId can't be empty", this); throw new TelegramApiValidationException("InlineQueryId can't be empty", this);
} }
if (results == null) { if (results == null) {
throw new TelegramApiValidationException("Results array can't be null", this); 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) { for (InlineQueryResult result : results) {
result.validate(); result.validate();
} }

View File

@ -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());
}
}
}