Skip to content

Commit 6f0f7bb

Browse files
committed
Urban Dictionary HTTP client
1 parent 9529194 commit 6f0f7bb

File tree

9 files changed

+164
-1
lines changed

9 files changed

+164
-1
lines changed

.github/workflows/default.yml

+24
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,27 @@ jobs:
7070
**/build/test-results
7171
**/build/reports
7272
73+
integration-test:
74+
name: Integration test
75+
runs-on: ubuntu-20.04
76+
steps:
77+
- uses: actions/checkout@v3
78+
- uses: actions/setup-java@v3
79+
with:
80+
java-version: 11
81+
distribution: adopt
82+
- uses: gradle/gradle-build-action@v2
83+
with:
84+
arguments: clean integrationTest
85+
cache-read-only: ${{ github.ref != 'refs/heads/master' }}
86+
- uses: actions/upload-artifact@v3
87+
if: always()
88+
with:
89+
name: test-results
90+
path: |
91+
**/build/test-results
92+
**/build/reports
93+
7394
cdk-test:
7495
name: CDK test
7596
runs-on: ubuntu-20.04
@@ -106,6 +127,7 @@ jobs:
106127
needs:
107128
- test
108129
- db-test
130+
- integration-test
109131
- cdk-test
110132
if: always()
111133
steps:
@@ -117,10 +139,12 @@ jobs:
117139
report_paths: |-
118140
**/test-results/test/TEST-*.xml
119141
**/test-results/dbTest/TEST-*.xml
142+
**/test-results/integrationTest/TEST-*.xml
120143
**/junit.xml
121144
check_name: |-
122145
Test reports
123146
DB test reports
147+
Integration test reports
124148
CDK test reports
125149
include_passed: true
126150
github_token: ${{ secrets.GITHUB_TOKEN }}

build.gradle.kts

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ subprojects {
3232
}
3333
withType<Test> {
3434
useJUnitPlatform {
35-
excludeTags("db")
35+
excludeTags("db", "it")
3636
}
3737
testLogging {
3838
showStandardStreams = true

english/urban-dictionary/README.adoc

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
= English / Urban Dictionary
2+
3+
https://urbandictionary.com[Urban Dictionary] client.
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
plugins {
2+
kotlin("jvm")
3+
kotlin("plugin.serialization")
4+
}
5+
6+
dependencies {
7+
implementation(platform(libs.ktor.bom))
8+
9+
implementation(libs.ktor.client.apache)
10+
implementation(libs.ktor.client.content.negotiation)
11+
implementation(libs.ktor.serialization.kotlinx.json)
12+
implementation(libs.log4j.api)
13+
14+
testImplementation(libs.junit.jupiter.api)
15+
testImplementation(libs.junit.jupiter.params)
16+
testImplementation(libs.mockk)
17+
testRuntimeOnly(libs.junit.jupiter.engine)
18+
testRuntimeOnly(libs.log4j.core)
19+
}
20+
21+
tasks {
22+
val integrationTest by registering(Test::class) {
23+
group = LifecycleBasePlugin.VERIFICATION_GROUP
24+
description = "Runs the integration tests."
25+
shouldRunAfter("test")
26+
outputs.upToDateWhen { false }
27+
useJUnitPlatform {
28+
includeTags("it")
29+
}
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package by.jprof.telegram.bot.english.urban_dictionary
2+
3+
import kotlinx.serialization.SerialName
4+
import kotlinx.serialization.Serializable
5+
6+
@Serializable
7+
data class Definition(
8+
val definition: String,
9+
val permalink: String,
10+
val author: String,
11+
val word: String,
12+
val example: String,
13+
@SerialName("thumbs_up")
14+
val thumbsUp: Int,
15+
@SerialName("thumbs_down")
16+
val thumbsDown: Int,
17+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package by.jprof.telegram.bot.english.urban_dictionary
2+
3+
import io.ktor.client.HttpClient
4+
import io.ktor.client.call.body
5+
import io.ktor.client.engine.apache.Apache
6+
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
7+
import io.ktor.client.request.get
8+
import io.ktor.client.request.parameter
9+
import io.ktor.client.request.url
10+
import io.ktor.serialization.kotlinx.json.json
11+
import java.io.Closeable
12+
import kotlinx.serialization.Serializable
13+
import kotlinx.serialization.json.Json
14+
15+
class KtorUrbanDictionaryClient(
16+
private val baseUrl: String = "https://api.urbandictionary.com/v0"
17+
) : UrbanDictionaryClient, Closeable {
18+
private val client = HttpClient(Apache) {
19+
install(ContentNegotiation) {
20+
json(
21+
Json {
22+
ignoreUnknownKeys = true
23+
}
24+
)
25+
}
26+
}
27+
28+
override suspend fun define(term: String): Collection<Definition> =
29+
client.get {
30+
url("$baseUrl/define")
31+
parameter("term", term)
32+
}.body<UrbanDictionaryResponse>().list
33+
34+
override suspend fun random(): Collection<Definition> =
35+
client.get {
36+
url("$baseUrl/random")
37+
}.body<UrbanDictionaryResponse>().list
38+
39+
override fun close() {
40+
client.close()
41+
}
42+
43+
@Serializable
44+
private data class UrbanDictionaryResponse(
45+
val list: List<Definition>
46+
)
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package by.jprof.telegram.bot.english.urban_dictionary
2+
3+
interface UrbanDictionaryClient {
4+
suspend fun define(term: String): Collection<Definition>
5+
6+
suspend fun random(): Collection<Definition>
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package by.jprof.telegram.bot.english.urban_dictionary
2+
3+
import kotlinx.coroutines.runBlocking
4+
import org.junit.jupiter.api.Assertions.*
5+
import org.junit.jupiter.api.BeforeAll
6+
import org.junit.jupiter.api.Tag
7+
import org.junit.jupiter.api.Test
8+
import org.junit.jupiter.api.TestInstance
9+
10+
@Tag("it")
11+
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
12+
internal class KtorUrbanDictionaryClientIntegrationTest {
13+
private lateinit var sut: KtorUrbanDictionaryClient
14+
15+
@BeforeAll
16+
internal fun setup() {
17+
sut = KtorUrbanDictionaryClient()
18+
}
19+
20+
@Test
21+
fun define() = runBlocking {
22+
val definitions = sut.define("motherfucker")
23+
24+
assertTrue(definitions.isNotEmpty())
25+
}
26+
27+
@Test
28+
fun random() = runBlocking {
29+
val definitions = sut.random()
30+
31+
assertTrue(definitions.isNotEmpty())
32+
}
33+
}

settings.gradle.kts

+1
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,5 @@ include(":times")
3636
include(":english")
3737
include(":english:language-rooms")
3838
include(":english:language-rooms:dynamodb")
39+
include(":english:urban-dictionary")
3940
include(":launchers:lambda")

0 commit comments

Comments
 (0)