From c977a270adee64ba4e6cc65ac5ef6477f990b35c Mon Sep 17 00:00:00 2001 From: Alexey Zverev Date: Mon, 10 Jun 2024 21:39:22 +0300 Subject: [PATCH] Extract paramaters from the code --- .../vcsfacade/BaseVcsFacadeFunctionalTest.kt | 3 +- .../VcsFacadeFunctionalTestBitbucket.kt | 11 ++- .../vcsfacade/VcsFacadeFunctionalTestGitea.kt | 18 ++-- .../VcsFacadeFunctionalTestGitlab.kt | 14 ++- .../src/test/resources/test-properties.yaml | 22 +++++ .../octopus/vcsfacade/Configuration.kt | 88 +++++++++++++++++++ .../octopus/vcsfacade/TestService.kt | 9 +- .../src/main/resources/test-properties.yaml | 22 +++++ 8 files changed, 169 insertions(+), 18 deletions(-) create mode 100644 server/src/test/resources/test-properties.yaml create mode 100644 test-common/src/main/kotlin/org/octopusden/octopus/vcsfacade/Configuration.kt create mode 100644 test-common/src/main/resources/test-properties.yaml diff --git a/ft/src/ft/kotlin/org/octopusden/octopus/vcsfacade/BaseVcsFacadeFunctionalTest.kt b/ft/src/ft/kotlin/org/octopusden/octopus/vcsfacade/BaseVcsFacadeFunctionalTest.kt index c6b1c72..894d455 100644 --- a/ft/src/ft/kotlin/org/octopusden/octopus/vcsfacade/BaseVcsFacadeFunctionalTest.kt +++ b/ft/src/ft/kotlin/org/octopusden/octopus/vcsfacade/BaseVcsFacadeFunctionalTest.kt @@ -2,7 +2,6 @@ package org.octopusden.octopus.vcsfacade import java.util.Date import org.octopusden.octopus.infrastructure.common.test.TestClient -import org.octopusden.octopus.vcsfacade.TestService.Companion.VCS_FACADE_API_URL import org.octopusden.octopus.vcsfacade.client.common.dto.CreatePullRequest import org.octopusden.octopus.vcsfacade.client.common.dto.SearchIssuesInRangesRequest import org.octopusden.octopus.vcsfacade.client.impl.ClassicVcsFacadeClient @@ -59,7 +58,7 @@ abstract class BaseVcsFacadeFunctionalTest( companion object { private val client = ClassicVcsFacadeClient(object : VcsFacadeClientParametersProvider { - override fun getApiUrl() = VCS_FACADE_API_URL + override fun getApiUrl() = Configuration.model.vcsFacadeUrl override fun getTimeRetryInMillis() = 180000 }) } diff --git a/ft/src/ft/kotlin/org/octopusden/octopus/vcsfacade/VcsFacadeFunctionalTestBitbucket.kt b/ft/src/ft/kotlin/org/octopusden/octopus/vcsfacade/VcsFacadeFunctionalTestBitbucket.kt index f2613d3..c64ac13 100644 --- a/ft/src/ft/kotlin/org/octopusden/octopus/vcsfacade/VcsFacadeFunctionalTestBitbucket.kt +++ b/ft/src/ft/kotlin/org/octopusden/octopus/vcsfacade/VcsFacadeFunctionalTestBitbucket.kt @@ -3,8 +3,13 @@ package org.octopusden.octopus.vcsfacade import org.junit.jupiter.api.condition.EnabledIfSystemProperty import org.octopusden.octopus.infastructure.bitbucket.test.BitbucketTestClient -@EnabledIfSystemProperty(named = "test.profile", matches = "bitbucket") +@EnabledIfSystemProperty(named = TEST_PROFILE, matches = BITBUCKET) class VcsFacadeFunctionalTestBitbucket : BaseVcsFacadeFunctionalTest( - TestService.Bitbucket(BITBUCKET_HOST, BITBUCKET_EXTERNAL_HOST), - BitbucketTestClient("http://$BITBUCKET_HOST", BITBUCKET_USER, BITBUCKET_PASSWORD, BITBUCKET_EXTERNAL_HOST) + TestService.Bitbucket(Configuration.model.bitbucket.host, Configuration.model.bitbucket.externalHost), + BitbucketTestClient( + Configuration.model.bitbucket.url, + Configuration.model.bitbucket.user, + Configuration.model.bitbucket.password, + Configuration.model.bitbucket.externalHost + ) ) diff --git a/ft/src/ft/kotlin/org/octopusden/octopus/vcsfacade/VcsFacadeFunctionalTestGitea.kt b/ft/src/ft/kotlin/org/octopusden/octopus/vcsfacade/VcsFacadeFunctionalTestGitea.kt index d7cfe81..07a8c66 100644 --- a/ft/src/ft/kotlin/org/octopusden/octopus/vcsfacade/VcsFacadeFunctionalTestGitea.kt +++ b/ft/src/ft/kotlin/org/octopusden/octopus/vcsfacade/VcsFacadeFunctionalTestGitea.kt @@ -10,19 +10,27 @@ import org.octopusden.octopus.infrastructure.common.test.dto.NewChangeSet import org.octopusden.octopus.infrastructure.gitea.test.GiteaTestClient import org.octopusden.octopus.vcsfacade.client.common.dto.CreatePullRequest -@EnabledIfSystemProperty(named = "test.profile", matches = "gitea") +@EnabledIfSystemProperty(named = TEST_PROFILE, matches = GITEA) class VcsFacadeFunctionalTestGitea : BaseVcsFacadeFunctionalTest( - TestService.Gitea(GITEA_HOST, GITEA_EXTERNAL_HOST, true), - GiteaTestClient("http://$GITEA_HOST", GITEA_USER, GITEA_PASSWORD, GITEA_EXTERNAL_HOST) + TestService.Gitea( + Configuration.model.gitea.host, + Configuration.model.gitea.externalHost, + true), + GiteaTestClient( + Configuration.model.gitea.url, + Configuration.model.gitea.user, + Configuration.model.gitea.password, + Configuration.model.gitea.externalHost + ) ) { @BeforeAll fun beforeAllVcsFacadeFunctionalTestGitea() { - val url = URI("http://$GITEA_HOST/api/v1/repos/$GROUP/$REPOSITORY_2/hooks").toURL() + val url = URI("http://${Configuration.model.gitea.host}/api/v1/repos/$GROUP/$REPOSITORY_2/hooks").toURL() with(url.openConnection() as HttpURLConnection) { setRequestMethod("POST") setRequestProperty( "Authorization", - "Basic " + Base64.getEncoder().encodeToString("$GITEA_USER:$GITEA_PASSWORD".toByteArray()) + "Basic " + Base64.getEncoder().encodeToString("${Configuration.model.gitea.user}:${Configuration.model.gitea.password}".toByteArray()) ) setRequestProperty("Content-Type", "application/json") setRequestProperty("Accept", "application/json") diff --git a/ft/src/ft/kotlin/org/octopusden/octopus/vcsfacade/VcsFacadeFunctionalTestGitlab.kt b/ft/src/ft/kotlin/org/octopusden/octopus/vcsfacade/VcsFacadeFunctionalTestGitlab.kt index f461f38..99fe2f6 100644 --- a/ft/src/ft/kotlin/org/octopusden/octopus/vcsfacade/VcsFacadeFunctionalTestGitlab.kt +++ b/ft/src/ft/kotlin/org/octopusden/octopus/vcsfacade/VcsFacadeFunctionalTestGitlab.kt @@ -3,8 +3,16 @@ package org.octopusden.octopus.vcsfacade import org.junit.jupiter.api.condition.EnabledIfSystemProperty import org.octopusden.octopus.infrastructure.gitlab.test.GitlabTestClient -@EnabledIfSystemProperty(named = "test.profile", matches = "gitlab") +@EnabledIfSystemProperty(named = TEST_PROFILE, matches = GITLAB) class VcsFacadeFunctionalTestGitlab : BaseVcsFacadeFunctionalTest( - TestService.Gitlab(GITLAB_HOST, GITLAB_EXTERNAL_HOST), - GitlabTestClient("http://$GITLAB_HOST", GITLAB_USER, GITLAB_PASSWORD, GITLAB_EXTERNAL_HOST) + TestService.Gitlab( + Configuration.model.gitlab.host, + Configuration.model.gitlab.externalHost + ), + GitlabTestClient( + Configuration.model.gitlab.url, + Configuration.model.gitlab.user, + Configuration.model.gitlab.password, + Configuration.model.gitlab.externalHost + ) ) diff --git a/server/src/test/resources/test-properties.yaml b/server/src/test/resources/test-properties.yaml new file mode 100644 index 0000000..9c04cce --- /dev/null +++ b/server/src/test/resources/test-properties.yaml @@ -0,0 +1,22 @@ +bitbucket: + host: "localhost:7990" + externalHost: "bitbucket:7990" + user: "admin" + password: "admin" + url: "http://localhost:7990" + +gitlab: + host: "localhost:8990" + externalHost: "gitlab:8990" + user: "root" + password: "VomkaEa6PD1OIgY7dQVbPUuO8wi9RMCaZw/i9yPXcI0=" + url: "http://localhost:8990" + +gitea: + host: "localhost:3000" + externalHost: "gitea:3000" + user: "test-admin" + password: "test-admin" + url: "http://localhost:3000" + +vcsFacadeUrl: "http://localhost:8080" diff --git a/test-common/src/main/kotlin/org/octopusden/octopus/vcsfacade/Configuration.kt b/test-common/src/main/kotlin/org/octopusden/octopus/vcsfacade/Configuration.kt new file mode 100644 index 0000000..c40fad5 --- /dev/null +++ b/test-common/src/main/kotlin/org/octopusden/octopus/vcsfacade/Configuration.kt @@ -0,0 +1,88 @@ +package org.octopusden.octopus.vcsfacade + +import org.yaml.snakeyaml.Yaml + +private const val PROPERTY_FILE = "test-properties.yaml" +const val TEST_PROFILE = "test.profile" +const val GITLAB = "gitlab" +const val GITEA = "gitea" +const val BITBUCKET = "bitbucket" + +/** + * Configuration object that reads the test-properties.yaml file and provides access to its properties. + */ +object Configuration { + + val model: ConfigModel by lazy { + val yaml = Yaml() + val inputStream = ConfigModel::class.java.getClassLoader().getResourceAsStream(PROPERTY_FILE) + requireNotNull(inputStream) { "$PROPERTY_FILE not found" } + overrideWithPropVariables(yaml.loadAs(inputStream, ConfigModel::class.java)) + } + + private fun overrideWithPropVariables(configModel: ConfigModel): ConfigModel { + configModel.bitbucket.host = System.getProperty("bitbucketHost", configModel.bitbucket.host) + configModel.bitbucket.externalHost = System.getProperty("bitbucketExternalHost", configModel.bitbucket.externalHost) + configModel.bitbucket.url = System.getProperty("bitbucketUrl", configModel.bitbucket.url) + configModel.gitea.host = System.getProperty("giteaHost", configModel.gitea.host) + configModel.gitea.externalHost = System.getProperty("giteaExternalHost", configModel.gitea.externalHost) + configModel.gitea.url = System.getProperty("giteaUrl", configModel.gitea.url) + configModel.gitlab.host = System.getProperty("gitlabHost", configModel.gitlab.host) + configModel.gitlab.externalHost = System.getProperty("gitlabExternalHost", configModel.gitlab.externalHost) + configModel.gitlab.url = System.getProperty("gitlabUrl", configModel.gitlab.url) + configModel.vcsFacadeUrl = System.getProperty("vcsFacadeUrl", configModel.vcsFacadeUrl) + return configModel + } +} + +/** + * Configuration model representing the structure of the test-properties.yaml file. + */ +data class ConfigModel( + var bitbucket: VcsModel = VcsModel( + "admin", + "admin", + "localhost:7990", + "bitbucket:7990", + "http://localhost:7990"), + var gitlab: VcsModel = VcsModel( + "root", + "VomkaEa6PD1OIgY7dQVbPUuO8wi9RMCaZw/i9yPXcI0=", + "localhost:3000", + "gitlab:8990", + "http://localhost:8990" + ), + var gitea: VcsModel = VcsModel( + "test-admin", + "test-admin", + "localhost:8990", + "gitea:3000", + "http://localhost:3000"), + + var vcsFacadeUrl: String = "http://localhost:8080", + var project: String = "test-project", + var repository: String = "test-repository", + var repository2: String = "test-repository-2", + var mainBranch: String = "master", + var featureBranch: String = "feature/FEATURE-1", + var messageInit: String = "initial commit", + var message1: String = "TEST-1 First commit", + var message2: String = "TEST-1 Second commit", + var message3: String = "TEST-2 Third commit", + var featureMessage: String = "FEATURE-1 First commit", + var tag1: String = "commit-1-tag", + var tag2: String = "commit-2-tag", + var tag3: String = "commit-3-tag", + var defaultId: String = "0123456789abcde", +) + +/** + * Configuration model representing the structure of the VCS properties. + */ +data class VcsModel( + var user: String = "", + var password: String = "", + var host: String = "", + var externalHost: String = "", + var url: String = "", +) \ No newline at end of file diff --git a/test-common/src/main/kotlin/org/octopusden/octopus/vcsfacade/TestService.kt b/test-common/src/main/kotlin/org/octopusden/octopus/vcsfacade/TestService.kt index c5dfd69..3044e03 100644 --- a/test-common/src/main/kotlin/org/octopusden/octopus/vcsfacade/TestService.kt +++ b/test-common/src/main/kotlin/org/octopusden/octopus/vcsfacade/TestService.kt @@ -104,7 +104,7 @@ sealed class TestService( class Bitbucket( host: String, externalHost: String? = null ) : TestService(host, externalHost) { - override val type = "bitbucket" + override val type = BITBUCKET override fun sshUrl(group: String, repository: String) = "ssh://git@$effectiveHost/$group/$repository.git" @@ -113,14 +113,14 @@ sealed class TestService( class Gitea( host: String, externalHost: String? = null, private val useColon: Boolean = false ) : TestService(host, externalHost) { - override val type = "gitea" + override val type = GITEA override fun sshUrl(group: String, repository: String) = "ssh://git@$effectiveHost${if (useColon) ":" else "/"}$group/$repository.git" fun scan(group: String, repository: String) { val query = "sshUrl=${URLEncoder.encode(sshUrl(group, repository), StandardCharsets.UTF_8)}" - val url = URI("$VCS_FACADE_API_URL/rest/api/1/indexer/gitea/scan?$query").toURL() + val url = URI("${Configuration.model.vcsFacadeUrl}/rest/api/1/indexer/gitea/scan?$query").toURL() with(url.openConnection() as HttpURLConnection) { setRequestMethod("POST") if (getResponseCode() / 100 != 2) { @@ -134,14 +134,13 @@ sealed class TestService( class Gitlab( host: String, externalHost: String? = null ) : TestService(host, externalHost) { - override val type = "gitlab" + override val type = GITLAB override fun sshUrl(group: String, repository: String) = "ssh://git@$effectiveHost:$group/$repository.git" } companion object { - const val VCS_FACADE_API_URL = "http://localhost:8080" //TODO: use some custom port? private val OBJECT_MAPPER = ObjectMapper().registerKotlinModule() diff --git a/test-common/src/main/resources/test-properties.yaml b/test-common/src/main/resources/test-properties.yaml new file mode 100644 index 0000000..9c04cce --- /dev/null +++ b/test-common/src/main/resources/test-properties.yaml @@ -0,0 +1,22 @@ +bitbucket: + host: "localhost:7990" + externalHost: "bitbucket:7990" + user: "admin" + password: "admin" + url: "http://localhost:7990" + +gitlab: + host: "localhost:8990" + externalHost: "gitlab:8990" + user: "root" + password: "VomkaEa6PD1OIgY7dQVbPUuO8wi9RMCaZw/i9yPXcI0=" + url: "http://localhost:8990" + +gitea: + host: "localhost:3000" + externalHost: "gitea:3000" + user: "test-admin" + password: "test-admin" + url: "http://localhost:3000" + +vcsFacadeUrl: "http://localhost:8080"