Skip to content

Commit

Permalink
Add secrets config
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonShapovalov committed Nov 25, 2024
1 parent e994ed2 commit cddba70
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ build/
!gradle/wrapper/gradle-wrapper.jar
!**/src/main/**/build/
!**/src/test/**/build/
src/main/resources/secrets.properties

### STS ###
.apt_generated
Expand Down
2 changes: 2 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ dependencies {
testRuntimeOnly("org.junit.platform:junit-platform-launcher")

detektPlugins("io.gitlab.arturbosch.detekt:detekt-formatting:1.23.7")
annotationProcessor("org.springframework.boot:spring-boot-configuration-processor")
}

kotlin {
Expand All @@ -64,3 +65,4 @@ kotlin {
tasks.withType<Test> {
useJUnitPlatform()
}

Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
package concept.stc

import concept.stc.config.OmdbApiConfigProperties
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.boot.runApplication

/**
* Application entry point.
*/
@SpringBootApplication
class SpringThymeleafCoroutinesApplication
@EnableConfigurationProperties(OmdbApiConfigProperties::class)
class STCApplication

/**
* Application entry point.
*/
fun main(args: Array<String>) {
runApplication<SpringThymeleafCoroutinesApplication>(args = args)
runApplication<STCApplication>(args = args)
}
15 changes: 15 additions & 0 deletions src/main/kotlin/concept/stc/config/OmdbApiConfigProperties.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package concept.stc.config

import org.springframework.boot.context.properties.ConfigurationProperties

/**
* Config definition for Open Movie Database (OMDB) API properties.
*
* @property baseUrl the api base url.
* @property apiKey the api key.
*/
@ConfigurationProperties(prefix = "omdb")
data class OmdbApiConfigProperties(
val baseUrl: String,
val apiKey: String
)
32 changes: 32 additions & 0 deletions src/main/kotlin/concept/stc/data/remote/ApiClient.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package concept.stc.data.remote

import concept.stc.config.OmdbApiConfigProperties
import kotlinx.coroutines.reactive.awaitFirst
import org.springframework.stereotype.Component
import org.springframework.web.reactive.function.client.WebClient

/**
* Api client for external API.
*
* @param configProperties the API config properties.
*/
@Component
class ApiClient(
private val configProperties: OmdbApiConfigProperties
) {

private val webClient = WebClient.create(configProperties.baseUrl)

/**
* Get movies list from the external API.
*
* @param query the search query.
*/
suspend fun getMovies(query: String) {
webClient.get()
.uri("/?apikey={apiKey}&s={query}", configProperties.apiKey, query)
.retrieve()
.bodyToMono(String::class.java)
.awaitFirst()
}
}
5 changes: 4 additions & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
spring.config.import=optional:secrets.properties
omdb.base-url=https://www.omdbapi.com

spring.application.name=spring-thymeleaf-coroutines
logging.level.org.springframework.jdbc.datasource.init.ScriptUtils=debug
spring.jpa.hibernate.ddl-auto=none
spring.jpa.hibernate.ddl-auto=none
2 changes: 1 addition & 1 deletion src/main/resources/schema.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CREATE TABLE movies (
CREATE TABLE IF NOT EXISTS movies (
id INTEGER PRIMARY KEY AUTO_INCREMENT,
title VARCHAR(255),
movie_year VARCHAR(4),
Expand Down
24 changes: 24 additions & 0 deletions src/test/kotlin/concept/stc/config/OmdbApiConfigIntegrationTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package concept.stc.config

import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase.Replace
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment
import kotlin.test.Test
import kotlin.test.assertTrue

@SpringBootTest(webEnvironment = WebEnvironment.NONE)
@AutoConfigureTestDatabase(replace = Replace.ANY)
class OmdbApiConfigIntegrationTest {

@Autowired
private lateinit var configProperties: OmdbApiConfigProperties

@Test
fun `when getting properties, given config, then values are not empty`() {
assertTrue(configProperties.baseUrl.isNotEmpty())
// Be sure that "secrets.properties" contains the api key
// assertTrue(configProperties.apiKey.isNotEmpty())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class MovieRepositoryIntegrationTest {
}

@Test
fun `when saving movie, given entity, then saved with not null id`() = runTest {
fun `when saving movie, given entity, then saved id is not null`() = runTest {
// Given
val entity = _entity.copy(imdbID = "test123")

Expand Down

0 comments on commit cddba70

Please sign in to comment.