Improved tests.

This commit is contained in:
Erik C. Thauvin 2019-04-09 02:33:07 -07:00
parent 799cf697d4
commit 2d5fc4c0cb
3 changed files with 18 additions and 26 deletions

View file

@ -44,7 +44,7 @@ final class AbstractModuleTest {
static void testAbstractModule(AbstractModule module) { static void testAbstractModule(AbstractModule module) {
final String name = module.getClass().getName(); final String name = module.getClass().getName();
assertThat(module.isEnabled()).as(name + ": enabled").isTrue(); assertThat(module.isEnabled()).as(name + ": enabled").isNotEqualTo(module.hasProperties());
assertThat(module.getCommands().size()).as(name + ": commands > 0").isGreaterThan(0); assertThat(module.getCommands().size()).as(name + ": commands > 0").isGreaterThan(0);
if (!module.hasProperties()) { if (!module.hasProperties()) {
assertThat(module.getPropertyKeys().size()).as(name + ": no properties").isEqualTo(0); assertThat(module.getPropertyKeys().size()).as(name + ": no properties").isEqualTo(0);

View file

@ -51,6 +51,6 @@ public class JokeTest {
@Test @Test
public void testRamdomJoke() throws ModuleException { public void testRamdomJoke() throws ModuleException {
assertThat(Joke.randomJoke().getMessage().length() > 0).as("randomJoke() > 0").isTrue(); assertThat(Joke.randomJoke().getMessage().length() > 0).as("randomJoke() > 0").isTrue();
assertThat(Joke.randomJoke().getMessage()).as("randomJoke()").contains("Chuck"); assertThat(Joke.randomJoke().getMessage()).as("randomJoke()").containsIgnoringCase("chuck");
} }
} }

View file

@ -34,13 +34,7 @@ package net.thauvin.erik.mobibot.modules;
import net.thauvin.erik.mobibot.msg.Message; import net.thauvin.erik.mobibot.msg.Message;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Properties;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@ -53,25 +47,23 @@ import static org.assertj.core.api.Assertions.assertThat;
*/ */
public class StockQuoteTest { public class StockQuoteTest {
@Test @Test
public void testGetQuote() throws ModuleException, IOException { public void testGetQuote() throws ModuleException {
String apiKey = System.getenv("ALPHA_ADVANTAGE"); final String apiKey = LocalProperties.getProperty(StockQuote.ALPHAVANTAGE_API_KEY_PROP);
if (apiKey == null) { try {
final Path localProps = Paths.get("local.properties");
if (Files.exists(localProps)) {
try (final InputStream stream = Files.newInputStream(localProps)) {
final Properties p = new Properties();
p.load(stream);
apiKey = p.getProperty(StockQuote.ALPHAVANTAGE_API_KEY_PROP);
}
}
}
ArrayList<Message> messages = StockQuote.getQuote("AAPL", apiKey); ArrayList<Message> messages = StockQuote.getQuote("AAPL", apiKey);
assertThat(messages).as("response not empty").isNotEmpty(); assertThat(messages).as("response not empty").isNotEmpty();
assertThat(messages.get(0).getMessage()).as("same stock symbol").contains("AAPL"); assertThat(messages.get(0).getMessage()).as("same stock symbol").contains("AAPL");
messages = StockQuote.getQuote("012", apiKey); messages = StockQuote.getQuote("012", apiKey);
assertThat(messages.get(0).isError()).as("invalid symbol error").isTrue(); assertThat(messages.get(0).isError()).as("invalid symbol error").isTrue();
} catch (ModuleException e) {
// Avoid displaying api keys in CI logs.
if ("true".equals(System.getenv("CI"))) {
throw new ModuleException(e.getDebugMessage(), e.getSanitizedMessage());
} else {
throw e;
}
}
} }
@Test @Test