diff --git a/src/test/kotlin/failchat/Utils.kt b/src/test/kotlin/failchat/Utils.kt index a7c71215..8d8964f4 100644 --- a/src/test/kotlin/failchat/Utils.kt +++ b/src/test/kotlin/failchat/Utils.kt @@ -1,28 +1,8 @@ package failchat -import failchat.util.sleep -import java.time.Duration - -fun doAwhile(tries: Int, retryDelay: Duration, operation: () -> T): T { - lateinit var lastException: Throwable - - repeat(tries) { - try { - return operation.invoke() - } catch (t: Throwable) { - lastException = t - sleep(retryDelay) - } - } - - throw lastException -} - -fun Long.s(): Duration = Duration.ofSeconds(this) -fun Int.s(): Duration = Duration.ofSeconds(this.toLong()) - -fun Long.ms(): Duration = Duration.ofMillis(this) -fun Int.ms(): Duration = Duration.ofMillis(this.toLong()) +import failchat.util.await +import okhttp3.Request +import kotlin.test.assertEquals object Utils @@ -30,3 +10,10 @@ fun readResourceAsString(resource: String): String { val bytes = Utils::class.java.getResourceAsStream(resource)?.readBytes() ?: error("No resource $resource") return String(bytes) } + +suspend fun assertRequestToUrlReturns200(url: String) { + val request = Request.Builder().url(url).get().build() + okHttpClient.newCall(request).await().use { + assertEquals(200, it.code) + } +} diff --git a/src/test/kotlin/failchat/twitch/SevenTvApiClientTest.kt b/src/test/kotlin/failchat/twitch/SevenTvApiClientTest.kt index d1f316fe..dfb46c25 100644 --- a/src/test/kotlin/failchat/twitch/SevenTvApiClientTest.kt +++ b/src/test/kotlin/failchat/twitch/SevenTvApiClientTest.kt @@ -1,13 +1,11 @@ package failchat.twitch +import failchat.assertRequestToUrlReturns200 import failchat.okHttpClient import failchat.testObjectMapper -import failchat.util.await import kotlinx.coroutines.runBlocking import mu.KLogging -import okhttp3.Request import org.junit.Test -import kotlin.test.assertEquals class SevenTvApiClientTest { @@ -23,7 +21,7 @@ class SevenTvApiClientTest { val emoticons = apiClient.loadGlobalEmoticons() logger.info("7tv global emoticons count: {}", emoticons.size) - assertEmoteIsRetrievable(emoticons.first()) + assertRequestToUrlReturns200(emoticons.first().url) } @Test @@ -31,13 +29,6 @@ class SevenTvApiClientTest { val emoticons = apiClient.loadChannelEmoticons(23161357L) // lirik logger.info("7tv channel emoticons count: {}", emoticons.size) - assertEmoteIsRetrievable(emoticons.first()) - } - - private suspend fun assertEmoteIsRetrievable(emote: SevenTvEmoticon) { - val request = Request.Builder().url(emote.url).get().build() - okHttpClient.newCall(request).await().use { - assertEquals(200, it.code) - } + assertRequestToUrlReturns200(emoticons.first().url) } } diff --git a/src/test/kotlin/failchat/twitch/TwitchApiClientTest.kt b/src/test/kotlin/failchat/twitch/TwitchApiClientTest.kt index e233fdc8..a7fce3e1 100644 --- a/src/test/kotlin/failchat/twitch/TwitchApiClientTest.kt +++ b/src/test/kotlin/failchat/twitch/TwitchApiClientTest.kt @@ -1,5 +1,6 @@ package failchat.twitch +import failchat.assertRequestToUrlReturns200 import failchat.exception.ChannelNotFoundException import failchat.exception.ChannelOfflineException import failchat.exception.UnexpectedResponseCodeException @@ -7,17 +8,15 @@ import failchat.okHttpClient import failchat.testObjectMapper import failchat.userHomeConfig import kotlinx.coroutines.runBlocking +import mu.KLogging import org.junit.Test -import org.slf4j.Logger -import org.slf4j.LoggerFactory import kotlin.test.assertEquals import kotlin.test.assertFails import kotlin.test.assertIs class TwitchApiClientTest { - private companion object { - val log: Logger = LoggerFactory.getLogger(TwitchApiClientTest::class.java) + private companion object : KLogging() { const val userName = "fail_chatbot" const val userId = 90826142L const val nonExistingUserName = "fail_chatbot2" @@ -67,13 +66,17 @@ class TwitchApiClientTest { fun getGlobalEmoticonsTest() = runBlocking { val emoticons = apiClient.getGlobalEmoticons() assert(emoticons.isNotEmpty()) + + assertRequestToUrlReturns200(emoticons.first().url) } @Test fun globalBadgesTest() = runBlocking { val badges = apiClient.getGlobalBadges() assert(badges.isNotEmpty()) - log.debug("{} global badges was loaded", badges.size) + logger.debug("{} global badges was loaded", badges.size) + + assertRequestToUrlReturns200(badges.values.first().url) } @Test @@ -81,6 +84,8 @@ class TwitchApiClientTest { val channelId = 23161357L // lirik val badges = apiClient.getChannelBadges(channelId) assert(badges.isNotEmpty()) - log.debug("{} channel badges was loaded for channel '{}'", badges.size, channelId) + logger.debug("{} channel badges was loaded for channel '{}'", badges.size, channelId) + + assertRequestToUrlReturns200(badges.values.first().url) } }