Skip to content

Commit

Permalink
Api client integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonShapovalov committed Dec 1, 2024
1 parent f9702a3 commit 6b5929a
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 14 deletions.
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ dependencies {
testImplementation("io.projectreactor:reactor-test")
testImplementation("org.jetbrains.kotlin:kotlin-test-junit5")
testImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-test")
testImplementation("org.wiremock.integrations:wiremock-spring-boot:3.2.0")
testRuntimeOnly("org.junit.platform:junit-platform-launcher")

detektPlugins("io.gitlab.arturbosch.detekt:detekt-formatting:1.23.7")
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/concept/stc/STCApplication.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package concept.stc

import concept.stc.config.OmdbApiConfigProperties
import concept.stc.config.OmdbApiProperties
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.boot.runApplication
Expand All @@ -9,7 +9,7 @@ import org.springframework.boot.runApplication
* Application entry point.
*/
@SpringBootApplication
@EnableConfigurationProperties(OmdbApiConfigProperties::class)
@EnableConfigurationProperties(OmdbApiProperties::class)
class STCApplication

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import org.springframework.boot.context.properties.ConfigurationProperties
* @property apiKey the api key.
*/
@ConfigurationProperties(prefix = "omdb")
data class OmdbApiConfigProperties(
data class OmdbApiProperties(
val baseUrl: String,
val apiKey: String = "" // be sure that "secrets.properties" contains the api key
)
16 changes: 7 additions & 9 deletions src/main/kotlin/concept/stc/data/remote/ApiClient.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package concept.stc.data.remote

import concept.stc.config.OmdbApiConfigProperties
import concept.stc.config.OmdbApiProperties
import kotlinx.coroutines.reactive.awaitFirst
import org.springframework.stereotype.Component
import org.springframework.web.reactive.function.client.WebClient
Expand All @@ -12,7 +12,7 @@ import org.springframework.web.reactive.function.client.WebClient
*/
@Component
class ApiClient(
private val properties: OmdbApiConfigProperties
private val properties: OmdbApiProperties
) {

private val webClient = WebClient.create(properties.baseUrl)
Expand All @@ -22,11 +22,9 @@ class ApiClient(
*
* @param query the search query.
*/
suspend fun getMovies(query: String) {
webClient.get()
.uri("/?apikey={apiKey}&s={query}", properties.apiKey, query)
.retrieve()
.bodyToMono(String::class.java)
.awaitFirst()
}
suspend fun getMovies(query: String): String = webClient.get()
.uri("/?apikey={apiKey}&s={query}", properties.apiKey, query)
.retrieve()
.bodyToMono(String::class.java)
.awaitFirst()
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import kotlin.test.Test
import kotlin.test.assertTrue

@SpringBootTest(webEnvironment = WebEnvironment.NONE)
class OmdbApiConfigIntegrationTest {
class OmdbPropertiesIntegrationTest {

@Autowired
private lateinit var properties: OmdbApiConfigProperties
private lateinit var properties: OmdbApiProperties

@Test
fun `when getting properties, given config, then values are not empty`() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package concept.stc.data.remote

import com.github.tomakehurst.wiremock.client.WireMock
import com.github.tomakehurst.wiremock.client.WireMock.aResponse
import com.github.tomakehurst.wiremock.client.WireMock.get
import com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo
import concept.stc.config.OmdbApiProperties
import kotlinx.coroutines.test.runTest
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Value
import org.springframework.boot.test.context.SpringBootTest
import org.wiremock.spring.EnableWireMock
import kotlin.test.assertEquals

@SpringBootTest
@EnableWireMock
class ApiClientIntegrationTest {

@Value("\${wiremock.server.baseUrl}")
private lateinit var wiremockUrl: String

@Test
fun `when getting movies, given api properties, then response is correct`() = runTest {
// Given
val key = "testKey"
val title = "testTitle"
val properties = OmdbApiProperties(baseUrl = wiremockUrl, apiKey = key)

val url = "/?apikey=$key&s=$title"
val aResponse = aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/json")
.withBody("SUCCESS")

WireMock.stubFor(get(urlEqualTo(url)).willReturn(aResponse))

// When
val response = ApiClient(properties).getMovies(title)

// Then
assertEquals("SUCCESS", response)
}
}

0 comments on commit 6b5929a

Please sign in to comment.