Skip to content

Commit

Permalink
Migrate old configuration properties automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
martinvisser committed Sep 11, 2023
1 parent a684d31 commit c8e1b2c
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 8 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>io.github.rabobank</groupId>
<artifactId>ret</artifactId>
<version>0.1.5-SNAPSHOT</version>
<version>0.2.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>RET Engineering Tools</name>
<url>https://github.com/rabobank/ret-engineering-tools</url>
Expand Down
2 changes: 1 addition & 1 deletion ret-cli/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>io.github.rabobank</groupId>
<artifactId>ret</artifactId>
<version>0.1.5-SNAPSHOT</version>
<version>0.2.0-SNAPSHOT</version>
</parent>

<artifactId>ret-cli</artifactId>
Expand Down
7 changes: 6 additions & 1 deletion ret-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>io.github.rabobank</groupId>
<artifactId>ret</artifactId>
<version>0.1.5-SNAPSHOT</version>
<version>0.2.0-SNAPSHOT</version>
</parent>

<artifactId>ret-core</artifactId>
Expand Down Expand Up @@ -54,6 +54,11 @@
<artifactId>quarkus-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.quarkiverse.mockk</groupId>
<artifactId>quarkus-junit5-mockk</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ import jakarta.inject.Inject
import org.eclipse.microprofile.config.inject.ConfigProperty

/**
* Implement this interface in an ApplicationScoped class if you need custom configurations in your plugin.
* Upon initializing the plugin, the user will be prompted for all provided configuration properties,
* Implement this class in an ApplicationScoped class if you need custom configurations in your plugin.
* Upon configuring the plugin, the user will be prompted for all provided configuration properties,
* and inputs are saved in the RET configuration file.
*
* This class will also take care of migrating old "generic" configuration to plugin specific configuration.
*/
open class Configurable {
@ConfigProperty(name = "plugin.name", defaultValue = "ret")
Expand Down Expand Up @@ -51,6 +53,19 @@ open class Configurable {
}
}

val oldConfigToMigrate = properties().map { it.key }
if (oldConfigToMigrate.isNotEmpty()) {
oldConfigToMigrate.forEach {
val oldValue = retConfig[it]
if (oldValue != null) {
configCopy[it] = oldValue

retConfig.remove(it)
configMigrated = true
}
}
}

return if (configMigrated) {
Log.debug("Migrated old configuration to plugin specific configuration")
retConfig.save()
Expand All @@ -62,6 +77,9 @@ open class Configurable {

fun load() = pluginConfig

/**
* An optional list of configuration properties that will be used to prompt the user to answer questions.
*/
open fun properties() = emptyList<ConfigurationProperty>()

/**
Expand Down
2 changes: 0 additions & 2 deletions ret-core/src/test/kotlin/io/rabobank/ret/IntelliSearchTest.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package io.rabobank.ret

import io.quarkus.test.junit.QuarkusTest
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.Arguments
import org.junit.jupiter.params.provider.MethodSource

@QuarkusTest
internal class IntelliSearchTest {

companion object {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package io.rabobank.ret.configuration

import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import io.mockk.every
import io.mockk.impl.annotations.MockK
import io.mockk.impl.annotations.SpyK
import io.mockk.junit5.MockKExtension
import io.mockk.justRun
import io.rabobank.ret.RetConsole
import io.rabobank.ret.util.OsUtils
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import org.junit.jupiter.api.io.TempDir
import java.nio.file.Path
import kotlin.io.path.createDirectories

@ExtendWith(MockKExtension::class)
class ConfigurableTest {
@SpyK
private var osUtils = OsUtils()

@MockK
private lateinit var retConfig: RetConfig

@MockK
private lateinit var retConsole: RetConsole

private val pluginName = "test"
private val objectMapper = jacksonObjectMapper()

@TempDir
lateinit var mockUserHomeDirectory: Path

@BeforeEach
fun setUp() {
mockUserHomeDirectory.createDirectories()

every { osUtils.getRetHomeDirectory() } returns mockUserHomeDirectory
}

@Test
fun `migrate configuration with key rename automatically`() {
val testConfig = TestConfig()
testConfig.pluginName = pluginName
testConfig.osUtils = osUtils
testConfig.objectMapper = objectMapper
testConfig.retConfig = retConfig
testConfig.retConsole = retConsole

every { retConfig["key_1"] } returns "value 1"
every { retConfig["old_key_2"] } returns "value 2"
justRun { retConfig.remove(any()) }
justRun { retConfig.save() }

assertThat(testConfig.pluginConfig.get<String>("key_1")).isEqualTo("value 1")
assertThat(testConfig.pluginConfig.get<String>("new_key_2")).isEqualTo("value 2")
}

inner class TestConfig : Configurable() {
override fun properties() = listOf(ConfigurationProperty("key_1", "Question 1"))

override fun keysToMigrate() = listOf("old_key_2" to "new_key_2")
}
}
2 changes: 1 addition & 1 deletion ret-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>io.github.rabobank</groupId>
<artifactId>ret</artifactId>
<version>0.1.5-SNAPSHOT</version>
<version>0.2.0-SNAPSHOT</version>
</parent>

<artifactId>ret-plugin</artifactId>
Expand Down

0 comments on commit c8e1b2c

Please sign in to comment.