Reworked and added test.
This commit is contained in:
parent
518703b826
commit
bdf0ca3006
2 changed files with 84 additions and 50 deletions
|
@ -83,7 +83,13 @@ public final class GoogleSearch extends ThreadedModule {
|
||||||
* @param cseKey The Google search results.
|
* @param cseKey The Google search results.
|
||||||
*/
|
*/
|
||||||
@SuppressFBWarnings(value = {"URLCONNECTION_SSRF_FD", "REC_CATCH_EXCEPTION"})
|
@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<>();
|
final ArrayList<Message> results = new ArrayList<>();
|
||||||
try {
|
try {
|
||||||
final String q = URLEncoder.encode(query, StandardCharsets.UTF_8.toString());
|
final String q = URLEncoder.encode(query, StandardCharsets.UTF_8.toString());
|
||||||
|
@ -120,6 +126,9 @@ public final class GoogleSearch extends ThreadedModule {
|
||||||
}
|
}
|
||||||
|
|
||||||
return results;
|
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, "To search Google:");
|
||||||
bot.send(sender, bot.helpIndent(bot.getNick() + ": " + GOOGLE_CMD + " <query>"));
|
bot.send(sender, bot.helpIndent(bot.getNick() + ": " + GOOGLE_CMD + " <query>"));
|
||||||
} else {
|
} 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.
|
* Searches Google.
|
||||||
*/
|
*/
|
||||||
void run(final Mobibot bot, final String sender, final String query) {
|
void run(final Mobibot bot, final String sender, final String query) {
|
||||||
|
if (Utils.isValidString(query)) {
|
||||||
try {
|
try {
|
||||||
final ArrayList<Message> results = searchGoogle(query, properties.get(GOOGLE_API_KEY_PROP),
|
final ArrayList<Message> results = searchGoogle(query, properties.get(GOOGLE_API_KEY_PROP),
|
||||||
properties.get(GOOGLE_CSE_KEY_PROP));
|
properties.get(GOOGLE_CSE_KEY_PROP));
|
||||||
|
|
||||||
int i = 0;
|
int i = 0;
|
||||||
for (Message msg : results) {
|
for (final Message msg : results) {
|
||||||
if (i % 2 == 0) {
|
if (i % 2 == 0) {
|
||||||
bot.send(sender, Utils.green(TAB_INDENT + msg.getMessage()));
|
bot.send(sender, Utils.green(TAB_INDENT + msg.getMessage()));
|
||||||
} else {
|
} else {
|
||||||
|
@ -156,5 +166,8 @@ public final class GoogleSearch extends ThreadedModule {
|
||||||
bot.getLogger().warn(e.getDebugMessage(), e);
|
bot.getLogger().warn(e.getDebugMessage(), e);
|
||||||
bot.send(sender, e.getMessage());
|
bot.send(sender, e.getMessage());
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
helpResponse(bot, sender, query, true);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -45,7 +45,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||||
* @created 2019-04-08
|
* @created 2019-04-08
|
||||||
* @since 1.0
|
* @since 1.0
|
||||||
*/
|
*/
|
||||||
public class GoogleSearchTest {
|
public class GoogleSearchTest extends LocalProperties {
|
||||||
@Test
|
@Test
|
||||||
public void testGoogleSearchImpl() {
|
public void testGoogleSearchImpl() {
|
||||||
AbstractModuleTest.testAbstractModule(new GoogleSearch());
|
AbstractModuleTest.testAbstractModule(new GoogleSearch());
|
||||||
|
@ -63,6 +63,27 @@ public class GoogleSearchTest {
|
||||||
messages = GoogleSearch.searchGoogle("aapl", apiKey, cseKey);
|
messages = GoogleSearch.searchGoogle("aapl", apiKey, cseKey);
|
||||||
assertThat(messages).as("aapl results not empty").isNotEmpty();
|
assertThat(messages).as("aapl results not empty").isNotEmpty();
|
||||||
assertThat(messages.get(0).getMessage()).as("found apple").containsIgnoringCase("apple");
|
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) {
|
} catch (ModuleException e) {
|
||||||
// Avoid displaying api keys in CI logs.
|
// Avoid displaying api keys in CI logs.
|
||||||
if ("true".equals(System.getenv("CI"))) {
|
if ("true".equals(System.getenv("CI"))) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue