From 2d5fc4c0cb2aac3500c6057eaaf9240f441535f7 Mon Sep 17 00:00:00 2001 From: "Erik C. Thauvin" Date: Tue, 9 Apr 2019 02:33:07 -0700 Subject: [PATCH] Improved tests. --- .../mobibot/modules/AbstractModuleTest.java | 4 +- .../erik/mobibot/modules/JokeTest.java | 2 +- .../erik/mobibot/modules/StockQuoteTest.java | 38 ++++++++----------- 3 files changed, 18 insertions(+), 26 deletions(-) diff --git a/src/test/java/net/thauvin/erik/mobibot/modules/AbstractModuleTest.java b/src/test/java/net/thauvin/erik/mobibot/modules/AbstractModuleTest.java index 5771ce5..618b5ba 100644 --- a/src/test/java/net/thauvin/erik/mobibot/modules/AbstractModuleTest.java +++ b/src/test/java/net/thauvin/erik/mobibot/modules/AbstractModuleTest.java @@ -43,8 +43,8 @@ import static org.assertj.core.api.Assertions.assertThat; final class AbstractModuleTest { static void testAbstractModule(AbstractModule module) { 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); if (!module.hasProperties()) { assertThat(module.getPropertyKeys().size()).as(name + ": no properties").isEqualTo(0); diff --git a/src/test/java/net/thauvin/erik/mobibot/modules/JokeTest.java b/src/test/java/net/thauvin/erik/mobibot/modules/JokeTest.java index 90fbd4d..edc821d 100644 --- a/src/test/java/net/thauvin/erik/mobibot/modules/JokeTest.java +++ b/src/test/java/net/thauvin/erik/mobibot/modules/JokeTest.java @@ -51,6 +51,6 @@ public class JokeTest { @Test public void testRamdomJoke() throws ModuleException { 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"); } } diff --git a/src/test/java/net/thauvin/erik/mobibot/modules/StockQuoteTest.java b/src/test/java/net/thauvin/erik/mobibot/modules/StockQuoteTest.java index d3c9a69..796f672 100644 --- a/src/test/java/net/thauvin/erik/mobibot/modules/StockQuoteTest.java +++ b/src/test/java/net/thauvin/erik/mobibot/modules/StockQuoteTest.java @@ -34,13 +34,7 @@ package net.thauvin.erik.mobibot.modules; import net.thauvin.erik.mobibot.msg.Message; 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.Properties; import static org.assertj.core.api.Assertions.assertThat; @@ -53,25 +47,23 @@ import static org.assertj.core.api.Assertions.assertThat; */ public class StockQuoteTest { @Test - public void testGetQuote() throws ModuleException, IOException { - String apiKey = System.getenv("ALPHA_ADVANTAGE"); - if (apiKey == null) { - 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); - } + public void testGetQuote() throws ModuleException { + final String apiKey = LocalProperties.getProperty(StockQuote.ALPHAVANTAGE_API_KEY_PROP); + try { + ArrayList messages = StockQuote.getQuote("AAPL", apiKey); + assertThat(messages).as("response not empty").isNotEmpty(); + assertThat(messages.get(0).getMessage()).as("same stock symbol").contains("AAPL"); + + messages = StockQuote.getQuote("012", apiKey); + 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; } } - - ArrayList messages = StockQuote.getQuote("AAPL", apiKey); - assertThat(messages).as("response not empty").isNotEmpty(); - assertThat(messages.get(0).getMessage()).as("same stock symbol").contains("AAPL"); - - messages = StockQuote.getQuote("012", apiKey); - assertThat(messages.get(0).isError()).as("invalid symbol error").isTrue(); } @Test