Reworked and added test.

This commit is contained in:
Erik C. Thauvin 2019-04-09 15:56:21 -07:00
parent 518703b826
commit bdf0ca3006
2 changed files with 84 additions and 50 deletions

View file

@ -83,7 +83,13 @@ public final class GoogleSearch extends ThreadedModule {
* @param cseKey The Google search results.
*/
@SuppressFBWarnings(value = {"URLCONNECTION_SSRF_FD", "REC_CATCH_EXCEPTION"})
static ArrayList<Message> searchGoogle(String query, String apiKey, String cseKey) throws ModuleException {
static ArrayList<Message> searchGoogle(final String query, final String apiKey, final String cseKey)
throws ModuleException {
if (!Utils.isValidString(apiKey) || !Utils.isValidString(cseKey)) {
throw new ModuleException(Utils.capitalize(GOOGLE_CMD) + " is disabled. The API keys are missing.");
}
if (Utils.isValidString(query)) {
final ArrayList<Message> results = new ArrayList<>();
try {
final String q = URLEncoder.encode(query, StandardCharsets.UTF_8.toString());
@ -120,6 +126,9 @@ public final class GoogleSearch extends ThreadedModule {
}
return results;
} else {
throw new ModuleException("Invalid query.");
}
}
/**
@ -131,7 +140,7 @@ public final class GoogleSearch extends ThreadedModule {
bot.send(sender, "To search Google:");
bot.send(sender, bot.helpIndent(bot.getNick() + ": " + GOOGLE_CMD + " <query>"));
} else {
bot.send(sender, "The Google searching facility is disabled.");
bot.send(sender, "The Google search module is disabled.");
}
}
@ -139,12 +148,13 @@ public final class GoogleSearch extends ThreadedModule {
* Searches Google.
*/
void run(final Mobibot bot, final String sender, final String query) {
if (Utils.isValidString(query)) {
try {
final ArrayList<Message> results = searchGoogle(query, properties.get(GOOGLE_API_KEY_PROP),
properties.get(GOOGLE_CSE_KEY_PROP));
int i = 0;
for (Message msg : results) {
for (final Message msg : results) {
if (i % 2 == 0) {
bot.send(sender, Utils.green(TAB_INDENT + msg.getMessage()));
} else {
@ -156,5 +166,8 @@ public final class GoogleSearch extends ThreadedModule {
bot.getLogger().warn(e.getDebugMessage(), e);
bot.send(sender, e.getMessage());
}
} else {
helpResponse(bot, sender, query, true);
}
}
}

View file

@ -45,7 +45,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* @created 2019-04-08
* @since 1.0
*/
public class GoogleSearchTest {
public class GoogleSearchTest extends LocalProperties {
@Test
public void testGoogleSearchImpl() {
AbstractModuleTest.testAbstractModule(new GoogleSearch());
@ -63,6 +63,27 @@ public class GoogleSearchTest {
messages = GoogleSearch.searchGoogle("aapl", apiKey, cseKey);
assertThat(messages).as("aapl results not empty").isNotEmpty();
assertThat(messages.get(0).getMessage()).as("found apple").containsIgnoringCase("apple");
try {
GoogleSearch.searchGoogle("test", "", "apiKey");
} catch (Exception e) {
assertThat(e).as("no API key").isInstanceOf(ModuleException.class);
assertThat(e).as("no API key exception has no cause").hasNoCause();
}
try {
GoogleSearch.searchGoogle("test", "apiKey", "");
} catch (Exception e) {
assertThat(e).as("no CSE API key").isInstanceOf(ModuleException.class);
assertThat(e).as("no CSE API key exception has no cause").hasNoCause();
}
try {
GoogleSearch.searchGoogle("", "apikey", "apiKey");
} catch (Exception e) {
assertThat(e).as("no query").isInstanceOf(ModuleException.class);
assertThat(e).as("no query exception has no cause").hasNoCause();
}
} catch (ModuleException e) {
// Avoid displaying api keys in CI logs.
if ("true".equals(System.getenv("CI"))) {