Skip to content

Commit 94aec5c

Browse files
committed
Add tests
1 parent a6eadd7 commit 94aec5c

3 files changed

Lines changed: 24 additions & 41 deletions

File tree

src/test/kotlin/failchat/Utils.kt

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,19 @@
11
package failchat
22

3-
import failchat.util.sleep
4-
import java.time.Duration
5-
6-
fun <T> doAwhile(tries: Int, retryDelay: Duration, operation: () -> T): T {
7-
lateinit var lastException: Throwable
8-
9-
repeat(tries) {
10-
try {
11-
return operation.invoke()
12-
} catch (t: Throwable) {
13-
lastException = t
14-
sleep(retryDelay)
15-
}
16-
}
17-
18-
throw lastException
19-
}
20-
21-
fun Long.s(): Duration = Duration.ofSeconds(this)
22-
fun Int.s(): Duration = Duration.ofSeconds(this.toLong())
23-
24-
fun Long.ms(): Duration = Duration.ofMillis(this)
25-
fun Int.ms(): Duration = Duration.ofMillis(this.toLong())
3+
import failchat.util.await
4+
import okhttp3.Request
5+
import kotlin.test.assertEquals
266

277
object Utils
288

299
fun readResourceAsString(resource: String): String {
3010
val bytes = Utils::class.java.getResourceAsStream(resource)?.readBytes() ?: error("No resource $resource")
3111
return String(bytes)
3212
}
13+
14+
suspend fun assertRequestToUrlReturns200(url: String) {
15+
val request = Request.Builder().url(url).get().build()
16+
okHttpClient.newCall(request).await().use {
17+
assertEquals(200, it.code)
18+
}
19+
}
Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
package failchat.twitch
22

3+
import failchat.assertRequestToUrlReturns200
34
import failchat.okHttpClient
45
import failchat.testObjectMapper
5-
import failchat.util.await
66
import kotlinx.coroutines.runBlocking
77
import mu.KLogging
8-
import okhttp3.Request
98
import org.junit.Test
10-
import kotlin.test.assertEquals
119

1210
class SevenTvApiClientTest {
1311

@@ -23,21 +21,14 @@ class SevenTvApiClientTest {
2321
val emoticons = apiClient.loadGlobalEmoticons()
2422
logger.info("7tv global emoticons count: {}", emoticons.size)
2523

26-
assertEmoteIsRetrievable(emoticons.first())
24+
assertRequestToUrlReturns200(emoticons.first().url)
2725
}
2826

2927
@Test
3028
fun loadChannelEmoticons() = runBlocking {
3129
val emoticons = apiClient.loadChannelEmoticons(23161357L) // lirik
3230
logger.info("7tv channel emoticons count: {}", emoticons.size)
3331

34-
assertEmoteIsRetrievable(emoticons.first())
35-
}
36-
37-
private suspend fun assertEmoteIsRetrievable(emote: SevenTvEmoticon) {
38-
val request = Request.Builder().url(emote.url).get().build()
39-
okHttpClient.newCall(request).await().use {
40-
assertEquals(200, it.code)
41-
}
32+
assertRequestToUrlReturns200(emoticons.first().url)
4233
}
4334
}

src/test/kotlin/failchat/twitch/TwitchApiClientTest.kt

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
11
package failchat.twitch
22

3+
import failchat.assertRequestToUrlReturns200
34
import failchat.exception.ChannelNotFoundException
45
import failchat.exception.ChannelOfflineException
56
import failchat.exception.UnexpectedResponseCodeException
67
import failchat.okHttpClient
78
import failchat.testObjectMapper
89
import failchat.userHomeConfig
910
import kotlinx.coroutines.runBlocking
11+
import mu.KLogging
1012
import org.junit.Test
11-
import org.slf4j.Logger
12-
import org.slf4j.LoggerFactory
1313
import kotlin.test.assertEquals
1414
import kotlin.test.assertFails
1515
import kotlin.test.assertIs
1616

1717
class TwitchApiClientTest {
1818

19-
private companion object {
20-
val log: Logger = LoggerFactory.getLogger(TwitchApiClientTest::class.java)
19+
private companion object : KLogging() {
2120
const val userName = "fail_chatbot"
2221
const val userId = 90826142L
2322
const val nonExistingUserName = "fail_chatbot2"
@@ -67,20 +66,26 @@ class TwitchApiClientTest {
6766
fun getGlobalEmoticonsTest() = runBlocking {
6867
val emoticons = apiClient.getGlobalEmoticons()
6968
assert(emoticons.isNotEmpty())
69+
70+
assertRequestToUrlReturns200(emoticons.first().url)
7071
}
7172

7273
@Test
7374
fun globalBadgesTest() = runBlocking {
7475
val badges = apiClient.getGlobalBadges()
7576
assert(badges.isNotEmpty())
76-
log.debug("{} global badges was loaded", badges.size)
77+
logger.debug("{} global badges was loaded", badges.size)
78+
79+
assertRequestToUrlReturns200(badges.values.first().url)
7780
}
7881

7982
@Test
8083
fun channelBadgesTest() = runBlocking {
8184
val channelId = 23161357L // lirik
8285
val badges = apiClient.getChannelBadges(channelId)
8386
assert(badges.isNotEmpty())
84-
log.debug("{} channel badges was loaded for channel '{}'", badges.size, channelId)
87+
logger.debug("{} channel badges was loaded for channel '{}'", badges.size, channelId)
88+
89+
assertRequestToUrlReturns200(badges.values.first().url)
8590
}
8691
}

0 commit comments

Comments
 (0)