diff --git a/build.gradle.kts b/build.gradle.kts index 29195fd..3a640ee 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -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") diff --git a/src/main/kotlin/concept/stc/STCApplication.kt b/src/main/kotlin/concept/stc/STCApplication.kt index fb0de36..104d26d 100644 --- a/src/main/kotlin/concept/stc/STCApplication.kt +++ b/src/main/kotlin/concept/stc/STCApplication.kt @@ -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 @@ -9,7 +9,7 @@ import org.springframework.boot.runApplication * Application entry point. */ @SpringBootApplication -@EnableConfigurationProperties(OmdbApiConfigProperties::class) +@EnableConfigurationProperties(OmdbApiProperties::class) class STCApplication /** diff --git a/src/main/kotlin/concept/stc/config/OmdbApiConfigProperties.kt b/src/main/kotlin/concept/stc/config/OmdbApiProperties.kt similarity index 91% rename from src/main/kotlin/concept/stc/config/OmdbApiConfigProperties.kt rename to src/main/kotlin/concept/stc/config/OmdbApiProperties.kt index 88c975f..98898ad 100644 --- a/src/main/kotlin/concept/stc/config/OmdbApiConfigProperties.kt +++ b/src/main/kotlin/concept/stc/config/OmdbApiProperties.kt @@ -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 ) diff --git a/src/main/kotlin/concept/stc/data/remote/ApiClient.kt b/src/main/kotlin/concept/stc/data/remote/ApiClient.kt index 6b596e4..541824e 100644 --- a/src/main/kotlin/concept/stc/data/remote/ApiClient.kt +++ b/src/main/kotlin/concept/stc/data/remote/ApiClient.kt @@ -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 @@ -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) @@ -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() } diff --git a/src/test/kotlin/concept/stc/config/OmdbApiConfigIntegrationTest.kt b/src/test/kotlin/concept/stc/config/OmdbPropertiesIntegrationTest.kt similarity index 86% rename from src/test/kotlin/concept/stc/config/OmdbApiConfigIntegrationTest.kt rename to src/test/kotlin/concept/stc/config/OmdbPropertiesIntegrationTest.kt index 9d752e0..d4b71b4 100644 --- a/src/test/kotlin/concept/stc/config/OmdbApiConfigIntegrationTest.kt +++ b/src/test/kotlin/concept/stc/config/OmdbPropertiesIntegrationTest.kt @@ -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`() { diff --git a/src/test/kotlin/concept/stc/data/remote/ApiClientIntegrationTest.kt b/src/test/kotlin/concept/stc/data/remote/ApiClientIntegrationTest.kt new file mode 100644 index 0000000..b4ca16b --- /dev/null +++ b/src/test/kotlin/concept/stc/data/remote/ApiClientIntegrationTest.kt @@ -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) + } +}