diff --git a/build.gradle b/build.gradle index 0a7562505..97b1c3a54 100644 --- a/build.gradle +++ b/build.gradle @@ -157,6 +157,7 @@ dependencies { implementation "io.github.microutils:kotlin-logging:3.0.5" + testImplementation 'org.hamcrest:hamcrest:2.2' testImplementation 'javax.annotation:javax.annotation-api:1.3.2' testImplementation "org.junit.jupiter:junit-jupiter:$junitVersion" testImplementation "org.mockito.kotlin:mockito-kotlin:5.2.1" diff --git a/src/main/java/com/exactpro/th2/common/schema/factory/AbstractCommonFactory.java b/src/main/java/com/exactpro/th2/common/schema/factory/AbstractCommonFactory.java index d2063237d..8b794f4c4 100644 --- a/src/main/java/com/exactpro/th2/common/schema/factory/AbstractCommonFactory.java +++ b/src/main/java/com/exactpro/th2/common/schema/factory/AbstractCommonFactory.java @@ -522,9 +522,7 @@ public T getCustomConfiguration(Class confClass) { * * @return Dictionary as {@link InputStream} * @throws IllegalStateException if can not read dictionary or found more than one target - * @deprecated please use {@link #loadDictionary(String)} */ - @Deprecated(since = "6", forRemoval = true) public abstract InputStream loadSingleDictionary(); /** diff --git a/src/main/java/com/exactpro/th2/common/schema/factory/CommonFactory.java b/src/main/java/com/exactpro/th2/common/schema/factory/CommonFactory.java index 511e60803..e0b025bfe 100644 --- a/src/main/java/com/exactpro/th2/common/schema/factory/CommonFactory.java +++ b/src/main/java/com/exactpro/th2/common/schema/factory/CommonFactory.java @@ -65,8 +65,6 @@ import java.util.Base64; import java.util.EnumMap; import java.util.HashMap; -import java.util.HashSet; -import java.util.List; import java.util.Map; import java.util.Objects; import java.util.Set; @@ -145,12 +143,9 @@ public IConfigurationProvider getConfigurationProvider() { return configurationProvider; } - public static CommonFactory createFromProvider(IConfigurationProvider provider) { - FactorySettings settings = new FactorySettings(); - - - - return new CommonFactory(settings); + public static CommonFactory createFromProvider(@NotNull IConfigurationProvider configurationProvider, + @NotNull IDictionaryProvider dictionaryProvider) { + return new CommonFactory(configurationProvider, dictionaryProvider); } /** @@ -593,10 +588,6 @@ private static ConfigurationManager createConfigurationManager(IConfigurationPro return new ConfigurationManager(configurationProvider, paths); } - private static Path defaultPathIfNull(Path customPath, Path basePath, String name) { - return customPath == null ? getConfigPath(basePath).resolve(name) : customPath; - } - private static void putIfNotNull(@NotNull Map paths, @NotNull T key, @Nullable Path path) { requireNonNull(paths, "'Paths' can't be null"); requireNonNull(key, "'Key' can't be null"); diff --git a/src/main/kotlin/com/exactpro/th2/common/schema/configuration/IDictionaryProvider.kt b/src/main/kotlin/com/exactpro/th2/common/schema/configuration/IDictionaryProvider.kt index 7ee0c8484..3121b9c2d 100644 --- a/src/main/kotlin/com/exactpro/th2/common/schema/configuration/IDictionaryProvider.kt +++ b/src/main/kotlin/com/exactpro/th2/common/schema/configuration/IDictionaryProvider.kt @@ -23,6 +23,5 @@ interface IDictionaryProvider { fun load(alias: String): InputStream @Deprecated("Load dictionary by type is deprecated, please use load by alias") fun load(type: DictionaryType): InputStream - @Deprecated("Load single dictionary is deprecated, please use load by alias") fun load(): InputStream } \ No newline at end of file diff --git a/src/main/kotlin/com/exactpro/th2/common/schema/configuration/impl/DictionaryProvider.kt b/src/main/kotlin/com/exactpro/th2/common/schema/configuration/impl/DictionaryProvider.kt index 3d4ad2eda..3ccafdfee 100644 --- a/src/main/kotlin/com/exactpro/th2/common/schema/configuration/impl/DictionaryProvider.kt +++ b/src/main/kotlin/com/exactpro/th2/common/schema/configuration/impl/DictionaryProvider.kt @@ -27,19 +27,15 @@ import java.io.IOException import java.io.InputStream import java.nio.file.Files import java.nio.file.Path -import kotlin.io.path.exists +import java.util.Locale import kotlin.io.path.isDirectory import kotlin.io.path.isRegularFile import kotlin.streams.asSequence class DictionaryProvider @JvmOverloads constructor( - private val baseDir: Path, + baseDir: Path, paths: Map = emptyMap() -): IDictionaryProvider { - init { - require(baseDir.exists()) { "Base dir '$baseDir' doesn't exist" } - require(baseDir.isDirectory()) { "Base dir '$baseDir' isn't a dictionary" } - } +) : IDictionaryProvider { private val directoryPaths = DictionaryKind.createMapping(baseDir) .plus(paths.mapValues { (_, value) -> value.toAbsolutePath() }) @@ -61,15 +57,31 @@ class DictionaryProvider @JvmOverloads constructor( override fun aliases(): Set { try { - if(!dictionaryAliasPath.isDirectory()) { + if (!dictionaryAliasPath.isDirectory()) { return emptySet() } - return Files.walk(dictionaryAliasPath).asSequence() + + val fileList = Files.walk(dictionaryAliasPath, 1).asSequence() .filter(Path::isRegularFile) - .map(::toAlias) - .toSet() + .toList() + val aliasSet: MutableSet = mutableSetOf() + val duplicates: MutableMap> = mutableMapOf() + for (path in fileList) { + val alias = FilenameUtils.removeExtension(path.fileName.toString()) + .lowercase(Locale.getDefault()) + if (!aliasSet.add(alias)) { + duplicates.getOrPut(alias, ::mutableSetOf).add(path.fileName.toString()) + } + } + check(duplicates.isEmpty()) { + "Dictionary directory contains files with the same name in different cases, files: $duplicates, path: $dictionaryAliasPath" + } + return aliasSet } catch (e: IOException) { - throw IllegalStateException("Can not get dictionaries aliases from path: ${dictionaryAliasPath.toAbsolutePath()}", e) + throw IllegalStateException( + "Can not get dictionaries aliases from path: ${dictionaryAliasPath.toAbsolutePath()}", + e + ) } } @@ -88,7 +100,10 @@ class DictionaryProvider @JvmOverloads constructor( return open(file) } catch (e: IOException) { - throw IllegalStateException("Can not load dictionary by '$alias' alias from path: ${dictionaryAliasPath.toAbsolutePath()}", e) + throw IllegalStateException( + "Can not load dictionary by '$alias' alias from path: ${dictionaryAliasPath.toAbsolutePath()}", + e + ) } } @@ -117,19 +132,19 @@ class DictionaryProvider @JvmOverloads constructor( val dirs = listOf(dictionaryAliasPath, dictionaryTypePath) try { var files: List = if (dictionaryAliasPath.isDirectory()) { - emptyList() - } else { - Files.walk(dictionaryAliasPath).asSequence() + Files.walk(dictionaryAliasPath, 1).asSequence() .filter(Path::isRegularFile) .toList() + } else { + emptyList() } - + if (files.isEmpty()) { if (dictionaryTypePath.isDirectory()) { - files = Files.walk(dictionaryTypePath).asSequence() + files = Files.walk(dictionaryTypePath, 1).asSequence() .filter(Path::isDirectory) .flatMap { dir -> - Files.walk(dir).asSequence() + Files.walk(dir, 1).asSequence() .filter(Path::isRegularFile) }.toList() } @@ -163,33 +178,33 @@ class DictionaryProvider @JvmOverloads constructor( } private fun searchInOldDir(name: String): List { - if (!dictionaryOldPath.isDirectory()) { - return emptyList() + if (dictionaryOldPath.isDirectory()) { + return Files.walk(dictionaryOldPath, 1).asSequence() + .filter(Path::isRegularFile) + .filter { file -> file.fileName.toString().contains(name) } + .toList() } - return Files.walk(dictionaryOldPath).asSequence() - .filter(Path::isRegularFile) - .filter { file -> file.fileName.toString().contains(name) } - .toList() + return emptyList() } private fun searchInTypeDir(type: DictionaryType): List { val path = type.getDictionary(dictionaryTypePath) - if (!path.isDirectory()) { - return emptyList() + if (path.isDirectory()) { + return Files.walk(path, 1).asSequence() + .filter(Path::isRegularFile) + .toList() } - return Files.walk(path).asSequence() - .filter(Path::isRegularFile) - .toList() + return emptyList() } private fun searchInAliasDir(alias: String): List { - if (!dictionaryAliasPath.isDirectory()) { - return emptyList() + if (dictionaryAliasPath.isDirectory()) { + return Files.walk(dictionaryAliasPath, 1).asSequence() + .filter(Path::isRegularFile) + .filter { file -> alias.equals(toAlias(file), true) } + .toList() } - return Files.walk(dictionaryAliasPath).asSequence() - .filter(Path::isRegularFile) - .filter { file -> alias.equals(toAlias(file), true) } - .toList() + return emptyList() } companion object { @@ -206,8 +221,6 @@ enum class DictionaryKind( companion object { fun createMapping(baseDir: Path): Map { - require(baseDir.exists()) { "Base dir '$baseDir' doesn't exist" } - require(baseDir.isDirectory()) { "Base dir '$baseDir' isn't a dictionary" } return buildMap { DictionaryKind.values().forEach { put(it, baseDir.resolve(it.directoryName).toAbsolutePath()) diff --git a/src/test/kotlin/com/exactpro/th2/common/schema/TestDictionaryLoad.kt b/src/test/kotlin/com/exactpro/th2/common/schema/DictionaryLoadTest.kt similarity index 98% rename from src/test/kotlin/com/exactpro/th2/common/schema/TestDictionaryLoad.kt rename to src/test/kotlin/com/exactpro/th2/common/schema/DictionaryLoadTest.kt index 172299b9a..d8c0b9bca 100644 --- a/src/test/kotlin/com/exactpro/th2/common/schema/TestDictionaryLoad.kt +++ b/src/test/kotlin/com/exactpro/th2/common/schema/DictionaryLoadTest.kt @@ -32,7 +32,8 @@ import kotlin.io.path.writeBytes import kotlin.io.path.writeText import kotlin.test.assertEquals -class TestDictionaryLoad { +@Suppress("DEPRECATION") +class DictionaryLoadTest { @TempDir lateinit var tempDir: Path @@ -64,7 +65,7 @@ class TestDictionaryLoad { @ParameterizedTest @ValueSource(strings = ["MAIN", "main", "test-dictionary"]) - fun `test read dictionary from type dictionary dir`(fileName: String, ) { + fun `test read dictionary from type dictionary dir`(fileName: String) { val content = writeDictionary(tempDir.resolve(Path.of("dictionary", "main", fileName))) CommonFactory.createFromArguments("-c", tempDir.absolutePathString()).use { commonFactory -> assertEquals(content, commonFactory.readDictionary().use { String(it.readAllBytes()) }) @@ -87,7 +88,7 @@ class TestDictionaryLoad { @ParameterizedTest @ValueSource(strings = ["MAIN", "main", "MAIN.xml", "main.json"]) - fun `test read dictionary from alias dictionary dir`(fileName: String, ) { + fun `test read dictionary from alias dictionary dir`(fileName: String) { val content = writeDictionary(tempDir.resolve(Path.of("dictionaries", fileName))) CommonFactory.createFromArguments("-c", tempDir.absolutePathString()).use { commonFactory -> assertEquals(content, commonFactory.readDictionary().use { String(it.readAllBytes()) }) @@ -120,7 +121,7 @@ class TestDictionaryLoad { @ParameterizedTest @ValueSource(strings = ["INCOMING", "incoming", "test-dictionary"]) - fun `test read dictionary by type from type dictionary dir`(fileName: String, ) { + fun `test read dictionary by type from type dictionary dir`(fileName: String) { val content = writeDictionary(tempDir.resolve(Path.of("dictionary", "incoming", fileName))) writeDictionary(tempDir.resolve(Path.of("dictionary", "main", "MAIN"))) CommonFactory.createFromArguments("-c", tempDir.absolutePathString()).use { commonFactory -> @@ -147,7 +148,7 @@ class TestDictionaryLoad { @ParameterizedTest @ValueSource(strings = ["INCOMING", "incoming", "INCOMING.xml", "incoming.json"]) - fun `test read dictionary by type from alias dictionary dir`(fileName: String, ) { + fun `test read dictionary by type from alias dictionary dir`(fileName: String) { val content = writeDictionary(tempDir.resolve(Path.of("dictionaries", fileName))) writeDictionary(tempDir.resolve(Path.of("dictionaries", "MAIN"))) CommonFactory.createFromArguments("-c", tempDir.absolutePathString()).use { commonFactory -> @@ -193,7 +194,7 @@ class TestDictionaryLoad { @ParameterizedTest @ValueSource(strings = ["TEST-ALIAS", "test-alias"]) - fun `test load dictionary by alias from alias dictionary dir`(fileName: String, ) { + fun `test load dictionary by alias from alias dictionary dir`(fileName: String) { val content = writeDictionary(tempDir.resolve(Path.of("dictionaries", fileName))) writeDictionary(tempDir.resolve(Path.of("dictionaries", "test-dictionary"))) CommonFactory.createFromArguments("-c", tempDir.absolutePathString()).use { commonFactory -> @@ -255,7 +256,7 @@ class TestDictionaryLoad { cfgPath.resolve("prometheus.json").writeText("{\"enabled\":false}") } - private fun writeDictionary(path: Path): String? { + private fun writeDictionary(path: Path): String { val content = RandomStringUtils.randomAlphanumeric(10) path.parent.createDirectories() path.writeBytes(ArchiveUtils.getGzipBase64StringEncoder().encode(content)) diff --git a/src/test/kotlin/com/exactpro/th2/common/schema/factory/CommonFactoryTest.kt b/src/test/kotlin/com/exactpro/th2/common/schema/factory/CommonFactoryTest.kt index 96b788c23..9aecfd34f 100644 --- a/src/test/kotlin/com/exactpro/th2/common/schema/factory/CommonFactoryTest.kt +++ b/src/test/kotlin/com/exactpro/th2/common/schema/factory/CommonFactoryTest.kt @@ -1,5 +1,5 @@ /* - * Copyright 2023 Exactpro (Exactpro Systems Limited) + * Copyright 2023-2024 Exactpro (Exactpro Systems Limited) * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -14,52 +14,82 @@ */ package com.exactpro.th2.common.schema.factory +import com.exactpro.th2.common.metrics.PrometheusConfiguration +import com.exactpro.th2.common.schema.box.configuration.BoxConfiguration import com.exactpro.th2.common.schema.configuration.impl.JsonConfigurationProvider -import com.exactpro.th2.common.schema.factory.AbstractCommonFactory.CUSTOM_CFG_ALIAS -import com.exactpro.th2.common.schema.factory.CommonFactory.BOX_CFG_ALIAS +import com.exactpro.th2.common.schema.cradle.CradleConfidentialConfiguration +import com.exactpro.th2.common.schema.cradle.CradleNonConfidentialConfiguration +import com.exactpro.th2.common.schema.dictionary.DictionaryType +import com.exactpro.th2.common.schema.dictionary.DictionaryType.INCOMING +import com.exactpro.th2.common.schema.dictionary.DictionaryType.MAIN +import com.exactpro.th2.common.schema.dictionary.DictionaryType.OUTGOING import com.exactpro.th2.common.schema.factory.CommonFactory.CONFIG_DEFAULT_PATH -import com.exactpro.th2.common.schema.factory.CommonFactory.CONNECTION_MANAGER_CFG_ALIAS -import com.exactpro.th2.common.schema.factory.CommonFactory.CRADLE_CONFIDENTIAL_CFG_ALIAS -import com.exactpro.th2.common.schema.factory.CommonFactory.CRADLE_NON_CONFIDENTIAL_CFG_ALIAS -import com.exactpro.th2.common.schema.factory.CommonFactory.DICTIONARY_ALIAS_DIR_NAME -import com.exactpro.th2.common.schema.factory.CommonFactory.DICTIONARY_TYPE_DIR_NAME -import com.exactpro.th2.common.schema.factory.CommonFactory.GRPC_CFG_ALIAS -import com.exactpro.th2.common.schema.factory.CommonFactory.PROMETHEUS_CFG_ALIAS -import com.exactpro.th2.common.schema.factory.CommonFactory.RABBIT_MQ_CFG_ALIAS -import com.exactpro.th2.common.schema.factory.CommonFactory.ROUTER_GRPC_CFG_ALIAS -import com.exactpro.th2.common.schema.factory.CommonFactory.ROUTER_MQ_CFG_ALIAS import com.exactpro.th2.common.schema.factory.CommonFactory.TH2_COMMON_CONFIGURATION_DIRECTORY_SYSTEM_PROPERTY +import com.exactpro.th2.common.schema.factory.CommonFactoryTest.Companion.DictionaryHelper.ALIAS +import com.exactpro.th2.common.schema.factory.CommonFactoryTest.Companion.DictionaryHelper.Companion.assertDictionary +import com.exactpro.th2.common.schema.factory.CommonFactoryTest.Companion.DictionaryHelper.OLD +import com.exactpro.th2.common.schema.factory.CommonFactoryTest.Companion.DictionaryHelper.TYPE +import com.exactpro.th2.common.schema.grpc.configuration.GrpcConfiguration +import com.exactpro.th2.common.schema.grpc.configuration.GrpcRouterConfiguration +import com.exactpro.th2.common.schema.message.configuration.MessageRouterConfiguration +import com.exactpro.th2.common.schema.message.impl.rabbitmq.configuration.ConnectionManagerConfiguration +import com.exactpro.th2.common.schema.message.impl.rabbitmq.configuration.RabbitMQConfiguration +import com.exactpro.th2.common.schema.util.ArchiveUtils +import org.apache.commons.io.file.PathUtils.deleteDirectory +import org.apache.commons.lang3.RandomStringUtils +import org.hamcrest.MatcherAssert.assertThat +import org.hamcrest.Matchers.samePropertyValuesAs +import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.Nested import org.junit.jupiter.api.Test +import org.junit.jupiter.api.assertAll +import org.junit.jupiter.api.assertThrows import org.junit.jupiter.params.ParameterizedTest -import org.junit.jupiter.params.provider.ValueSource +import org.junit.jupiter.params.provider.EnumSource import org.junitpioneer.jupiter.ClearSystemProperty import org.junitpioneer.jupiter.SetSystemProperty import java.nio.file.Path +import java.util.Locale +import kotlin.io.path.absolutePathString +import kotlin.io.path.createDirectories +import kotlin.io.path.exists +import kotlin.io.path.writeBytes import kotlin.reflect.cast +import kotlin.test.assertContains import kotlin.test.assertEquals import kotlin.test.assertNotNull +@Suppress("DEPRECATION", "removal") class CommonFactoryTest { + @BeforeEach + fun beforeEach() { + if (TEMP_DIR.toPath().exists()) { + deleteDirectory(TEMP_DIR.toPath()) + } + CMD_ARG_CFG_DIR_PATH.createDirectories() + SYSTEM_PROPERTY_CFG_DIR_PATH.createDirectories() + CUSTOM_DIR_PATH.createDirectories() + } + @Nested @ClearSystemProperty(key = TH2_COMMON_CONFIGURATION_DIRECTORY_SYSTEM_PROPERTY) inner class CreateByDefaultConstructor { @Test fun `test default`() { CommonFactory().use { commonFactory -> - assertDictionaryDir(commonFactory, CONFIG_DEFAULT_PATH) - assertConfigs(commonFactory) + assertThrowsDictionaryDir(commonFactory) + assertThrowsConfigs(commonFactory) } } @Test - @SetSystemProperty(key = TH2_COMMON_CONFIGURATION_DIRECTORY_SYSTEM_PROPERTY, value = SYSTEM_PROPERTY_CONFIG_DIR) + @SetSystemProperty(key = TH2_COMMON_CONFIGURATION_DIRECTORY_SYSTEM_PROPERTY, value = SYSTEM_PROPERTY_CFG_DIR) fun `test with system property`() { CommonFactory().use { commonFactory -> - assertDictionaryDir(commonFactory, SYSTEM_PROPERTY_CONFIG_DIR.toPath()) - assertConfigs(commonFactory, SYSTEM_PROPERTY_CONFIG_DIR.toPath(), false) + assertDictionaryDirs(commonFactory, SYSTEM_PROPERTY_CFG_DIR_PATH) + assertConfigs(commonFactory, SYSTEM_PROPERTY_CFG_DIR_PATH) } } } @@ -70,198 +100,161 @@ class CommonFactoryTest { @Test fun `test default`() { CommonFactory(FactorySettings()).use { commonFactory -> - assertDictionaryDir(commonFactory, CONFIG_DEFAULT_PATH) - assertConfigs(commonFactory) + assertThrowsDictionaryDir(commonFactory) + assertThrowsConfigs(commonFactory) } } @ParameterizedTest - @ValueSource(strings = [ - ALIASES_DICTIONARY_NAME, - TYPES_DICTIONARY_NAME, - OLD_DICTIONARY_NAME - ]) - fun `test custom dictionary path`(name: String) { - val factorySettings = FactorySettings().apply { - setCustomDictionaryPath(name) - } - CommonFactory(factorySettings).use { commonFactory -> - assertCustomDictionaryPath(name, CONFIG_DEFAULT_PATH, commonFactory) - assertConfigs(commonFactory) + @EnumSource(value = DictionaryHelper::class) + fun `test custom dictionary path`(helper: DictionaryHelper) { + CommonFactory(FactorySettings().apply { + helper.setOption(this, CUSTOM_DIR_PATH) + }).use { commonFactory -> + val content = helper.writeDictionaryByCustomPath(CUSTOM_DIR_PATH, DICTIONARY_NAME) + assertDictionary(commonFactory, content) + + assertThrowsConfigs(commonFactory) } } @ParameterizedTest - @ValueSource(strings = [ - RABBIT_MQ_CFG_ALIAS, - ROUTER_MQ_CFG_ALIAS, - CONNECTION_MANAGER_CFG_ALIAS, - GRPC_CFG_ALIAS, - ROUTER_GRPC_CFG_ALIAS, - CRADLE_CONFIDENTIAL_CFG_ALIAS, - CRADLE_NON_CONFIDENTIAL_CFG_ALIAS, - PROMETHEUS_CFG_ALIAS, - BOX_CFG_ALIAS, - CUSTOM_CFG_ALIAS, - ]) - fun `test custom path for config`(alias: String) { - val factorySettings = FactorySettings().apply { - setCustomPathForConfig(alias) - } - CommonFactory(factorySettings).use { commonFactory -> - assertDictionaryDir(commonFactory, CONFIG_DEFAULT_PATH) - assertCustomPathForConfig(commonFactory, CONFIG_DEFAULT_PATH, alias) + @EnumSource(ConfigHelper::class) + fun `test custom path for config`(helper: ConfigHelper) { + val configPath = CUSTOM_DIR_PATH.resolve(helper.alias) + CommonFactory(FactorySettings().apply { + helper.setOption(this, configPath) + }).use { commonFactory -> + assertThrowsDictionaryDir(commonFactory) + + val cfgBean = helper.writeConfigByCustomPath(configPath) + assertThat(helper.loadConfig(commonFactory), samePropertyValuesAs(cfgBean)) } } @Test fun `test with custom config path`() { CommonFactory(FactorySettings().apply { - baseConfigDir = CMD_ARG_CONFIG_DIR.toPath() + baseConfigDir = CMD_ARG_CFG_DIR_PATH }).use { commonFactory -> - assertDictionaryDir(commonFactory, CMD_ARG_CONFIG_DIR.toPath()) - assertConfigs(commonFactory, CMD_ARG_CONFIG_DIR.toPath(), false) + assertDictionaryDirs(commonFactory, CMD_ARG_CFG_DIR_PATH) + assertConfigs(commonFactory, CMD_ARG_CFG_DIR_PATH) } } @ParameterizedTest - @ValueSource(strings = [ - ALIASES_DICTIONARY_NAME, - TYPES_DICTIONARY_NAME, - OLD_DICTIONARY_NAME - ]) - fun `test with custom config path and custom dictionary path`(name: String) { - CommonFactory(FactorySettings().apply { - baseConfigDir = CMD_ARG_CONFIG_DIR.toPath() - setCustomDictionaryPath(name) - }).use { commonFactory -> - assertCustomDictionaryPath(name, CMD_ARG_CONFIG_DIR.toPath(), commonFactory) - assertConfigs(commonFactory, CMD_ARG_CONFIG_DIR.toPath(), false) + @EnumSource(value = DictionaryHelper::class) + fun `test with custom config path and custom dictionary path`(helper: DictionaryHelper) { + val settings = FactorySettings().apply { + baseConfigDir = CMD_ARG_CFG_DIR_PATH + helper.setOption(this, CMD_ARG_CFG_DIR_PATH) + } + CommonFactory(settings).use { commonFactory -> + val content = helper.writeDictionaryByCustomPath(CMD_ARG_CFG_DIR_PATH, DICTIONARY_NAME) + assertDictionary(commonFactory, content) + + assertConfigs(commonFactory, CMD_ARG_CFG_DIR_PATH) } } @ParameterizedTest - @ValueSource(strings = [ - RABBIT_MQ_CFG_ALIAS, - ROUTER_MQ_CFG_ALIAS, - CONNECTION_MANAGER_CFG_ALIAS, - GRPC_CFG_ALIAS, - ROUTER_GRPC_CFG_ALIAS, - CRADLE_CONFIDENTIAL_CFG_ALIAS, - CRADLE_NON_CONFIDENTIAL_CFG_ALIAS, - PROMETHEUS_CFG_ALIAS, - BOX_CFG_ALIAS, - CUSTOM_CFG_ALIAS, - ]) - fun `test with custom config path and custom path for config`(alias: String) { + @EnumSource(ConfigHelper::class) + fun `test with custom config path and custom path for config`(helper: ConfigHelper) { + val configPath = CUSTOM_DIR_PATH.resolve(helper.alias) CommonFactory(FactorySettings().apply { - baseConfigDir = CMD_ARG_CONFIG_DIR.toPath() - setCustomPathForConfig(alias) + baseConfigDir = CMD_ARG_CFG_DIR_PATH + helper.setOption(this, configPath) }).use { commonFactory -> - assertDictionaryDir(commonFactory, CMD_ARG_CONFIG_DIR.toPath()) - assertCustomPathForConfig(commonFactory, CMD_ARG_CONFIG_DIR.toPath(), alias) + assertDictionaryDirs(commonFactory, CMD_ARG_CFG_DIR_PATH) + + val cfgBean = helper.writeConfigByCustomPath(configPath) + assertThat(helper.loadConfig(commonFactory), samePropertyValuesAs(cfgBean)) } } - @Test - @SetSystemProperty(key = TH2_COMMON_CONFIGURATION_DIRECTORY_SYSTEM_PROPERTY, value = SYSTEM_PROPERTY_CONFIG_DIR) - fun `test with system property`() { + @ParameterizedTest + @SetSystemProperty(key = TH2_COMMON_CONFIGURATION_DIRECTORY_SYSTEM_PROPERTY, value = SYSTEM_PROPERTY_CFG_DIR) + @EnumSource(DictionaryHelper::class) + fun `test with system property`(helper: DictionaryHelper) { CommonFactory(FactorySettings()).use { commonFactory -> - assertDictionaryDir(commonFactory, SYSTEM_PROPERTY_CONFIG_DIR.toPath()) - assertConfigs(commonFactory, SYSTEM_PROPERTY_CONFIG_DIR.toPath(), false) + val content = helper.writeDictionaryByDefaultPath(SYSTEM_PROPERTY_CFG_DIR_PATH, DICTIONARY_NAME) + assertDictionary(commonFactory, content) + + assertConfigs(commonFactory, SYSTEM_PROPERTY_CFG_DIR_PATH) } } @ParameterizedTest - @SetSystemProperty(key = TH2_COMMON_CONFIGURATION_DIRECTORY_SYSTEM_PROPERTY, value = SYSTEM_PROPERTY_CONFIG_DIR) - @ValueSource(strings = [ - ALIASES_DICTIONARY_NAME, - TYPES_DICTIONARY_NAME, - OLD_DICTIONARY_NAME - ]) - fun `test with system property and custom dictionary path`(name: String) { + @SetSystemProperty(key = TH2_COMMON_CONFIGURATION_DIRECTORY_SYSTEM_PROPERTY, value = SYSTEM_PROPERTY_CFG_DIR) + @EnumSource(value = DictionaryHelper::class) + fun `test with system property and custom dictionary path`(helper: DictionaryHelper) { val factorySettings = FactorySettings().apply { - setCustomDictionaryPath(name) + helper.setOption(this, CUSTOM_DIR_PATH) } CommonFactory(factorySettings).use { commonFactory -> - assertCustomDictionaryPath(name, SYSTEM_PROPERTY_CONFIG_DIR.toPath(), commonFactory) - assertConfigs(commonFactory, SYSTEM_PROPERTY_CONFIG_DIR.toPath(), false) + val content = helper.writeDictionaryByCustomPath(CUSTOM_DIR_PATH, DICTIONARY_NAME) + assertDictionary(commonFactory, content) + + assertConfigs(commonFactory, SYSTEM_PROPERTY_CFG_DIR_PATH) } } @ParameterizedTest - @SetSystemProperty(key = TH2_COMMON_CONFIGURATION_DIRECTORY_SYSTEM_PROPERTY, value = SYSTEM_PROPERTY_CONFIG_DIR) - @ValueSource(strings = [ - RABBIT_MQ_CFG_ALIAS, - ROUTER_MQ_CFG_ALIAS, - CONNECTION_MANAGER_CFG_ALIAS, - GRPC_CFG_ALIAS, - ROUTER_GRPC_CFG_ALIAS, - CRADLE_CONFIDENTIAL_CFG_ALIAS, - CRADLE_NON_CONFIDENTIAL_CFG_ALIAS, - PROMETHEUS_CFG_ALIAS, - BOX_CFG_ALIAS, - CUSTOM_CFG_ALIAS, - ]) - fun `test with system property and custom path for config`(alias: String) { + @SetSystemProperty(key = TH2_COMMON_CONFIGURATION_DIRECTORY_SYSTEM_PROPERTY, value = SYSTEM_PROPERTY_CFG_DIR) + @EnumSource(ConfigHelper::class) + fun `test with system property and custom path for config`(helper: ConfigHelper) { + val configPath = CUSTOM_DIR_PATH.resolve(helper.alias) val factorySettings = FactorySettings().apply { - setCustomPathForConfig(alias) + helper.setOption(this, configPath) } CommonFactory(factorySettings).use { commonFactory -> - assertDictionaryDir(commonFactory, SYSTEM_PROPERTY_CONFIG_DIR.toPath()) - assertCustomPathForConfig(commonFactory, SYSTEM_PROPERTY_CONFIG_DIR.toPath(), alias) + assertDictionaryDirs(commonFactory, SYSTEM_PROPERTY_CFG_DIR_PATH) + + val cfgBean = helper.writeConfigByCustomPath(configPath) + assertThat(helper.loadConfig(commonFactory), samePropertyValuesAs(cfgBean)) } } @Test - @SetSystemProperty(key = TH2_COMMON_CONFIGURATION_DIRECTORY_SYSTEM_PROPERTY, value = SYSTEM_PROPERTY_CONFIG_DIR) + @SetSystemProperty(key = TH2_COMMON_CONFIGURATION_DIRECTORY_SYSTEM_PROPERTY, value = SYSTEM_PROPERTY_CFG_DIR) fun `test with cmd config argument and system property`() { CommonFactory(FactorySettings().apply { - baseConfigDir = CMD_ARG_CONFIG_DIR.toPath() + baseConfigDir = CMD_ARG_CFG_DIR_PATH }).use { commonFactory -> - assertDictionaryDir(commonFactory, CMD_ARG_CONFIG_DIR.toPath()) - assertConfigs(commonFactory, CMD_ARG_CONFIG_DIR.toPath(), false) + assertDictionaryDirs(commonFactory, CMD_ARG_CFG_DIR_PATH) + assertConfigs(commonFactory, CMD_ARG_CFG_DIR_PATH) } } @ParameterizedTest - @SetSystemProperty(key = TH2_COMMON_CONFIGURATION_DIRECTORY_SYSTEM_PROPERTY, value = SYSTEM_PROPERTY_CONFIG_DIR) - @ValueSource(strings = [ - ALIASES_DICTIONARY_NAME, - TYPES_DICTIONARY_NAME, - OLD_DICTIONARY_NAME - ]) - fun `test with cmd config argument and system property and custom dictionary path`(name: String) { - CommonFactory(FactorySettings().apply { - baseConfigDir = CMD_ARG_CONFIG_DIR.toPath() - setCustomDictionaryPath(name) - }).use { commonFactory -> - assertCustomDictionaryPath(name, CMD_ARG_CONFIG_DIR.toPath(), commonFactory) - assertConfigs(commonFactory, CMD_ARG_CONFIG_DIR.toPath(), false) + @SetSystemProperty(key = TH2_COMMON_CONFIGURATION_DIRECTORY_SYSTEM_PROPERTY, value = SYSTEM_PROPERTY_CFG_DIR) + @EnumSource(value = DictionaryHelper::class) + fun `test with cmd config argument and system property and custom dictionary path`(helper: DictionaryHelper) { + val settings = FactorySettings().apply { + baseConfigDir = CMD_ARG_CFG_DIR_PATH + helper.setOption(this, CUSTOM_DIR_PATH) + } + CommonFactory(settings).use { commonFactory -> + val content = helper.writeDictionaryByCustomPath(CUSTOM_DIR_PATH, DICTIONARY_NAME) + assertDictionary(commonFactory, content) + + assertConfigs(commonFactory, CMD_ARG_CFG_DIR_PATH) } } @ParameterizedTest - @SetSystemProperty(key = TH2_COMMON_CONFIGURATION_DIRECTORY_SYSTEM_PROPERTY, value = SYSTEM_PROPERTY_CONFIG_DIR) - @ValueSource(strings = [ - RABBIT_MQ_CFG_ALIAS, - ROUTER_MQ_CFG_ALIAS, - CONNECTION_MANAGER_CFG_ALIAS, - GRPC_CFG_ALIAS, - ROUTER_GRPC_CFG_ALIAS, - CRADLE_CONFIDENTIAL_CFG_ALIAS, - CRADLE_NON_CONFIDENTIAL_CFG_ALIAS, - PROMETHEUS_CFG_ALIAS, - BOX_CFG_ALIAS, - CUSTOM_CFG_ALIAS, - ]) - fun `test with cmd config argument and system property and custom path for config`(alias: String) { + @SetSystemProperty(key = TH2_COMMON_CONFIGURATION_DIRECTORY_SYSTEM_PROPERTY, value = SYSTEM_PROPERTY_CFG_DIR) + @EnumSource(ConfigHelper::class) + fun `test with cmd config argument and system property and custom path for config`(helper: ConfigHelper) { + val configPath = CUSTOM_DIR_PATH.resolve(helper.alias) CommonFactory(FactorySettings().apply { - baseConfigDir = CMD_ARG_CONFIG_DIR.toPath() - setCustomPathForConfig(alias) + baseConfigDir = CMD_ARG_CFG_DIR_PATH + helper.setOption(this, configPath) }).use { commonFactory -> - assertDictionaryDir(commonFactory, CMD_ARG_CONFIG_DIR.toPath()) - assertCustomPathForConfig(commonFactory, CMD_ARG_CONFIG_DIR.toPath(), alias) + assertDictionaryDirs(commonFactory, CMD_ARG_CFG_DIR_PATH) + + val cfgBean = helper.writeConfigByCustomPath(configPath) + assertThat(helper.loadConfig(commonFactory), samePropertyValuesAs(cfgBean)) } } } @@ -272,97 +265,132 @@ class CommonFactoryTest { @Test fun `test without parameters`() { CommonFactory.createFromArguments().use { commonFactory -> - assertDictionaryDir(commonFactory, CONFIG_DEFAULT_PATH) - assertConfigs(commonFactory) + assertThrowsDictionaryDir(commonFactory) + assertThrowsConfigs(commonFactory) } } @Test fun `test with cmd config argument`() { - CommonFactory.createFromArguments("-c", CMD_ARG_CONFIG_DIR).use { commonFactory -> - assertDictionaryDir(commonFactory, CMD_ARG_CONFIG_DIR.toPath()) - assertConfigs(commonFactory, CMD_ARG_CONFIG_DIR.toPath(), false) + CommonFactory.createFromArguments("-c", CMD_ARG_CFG_DIR).use { commonFactory -> + assertDictionaryDirs(commonFactory, CMD_ARG_CFG_DIR_PATH) + assertConfigs(commonFactory, CMD_ARG_CFG_DIR_PATH) } } @Test - @SetSystemProperty(key = TH2_COMMON_CONFIGURATION_DIRECTORY_SYSTEM_PROPERTY, value = SYSTEM_PROPERTY_CONFIG_DIR) + @SetSystemProperty(key = TH2_COMMON_CONFIGURATION_DIRECTORY_SYSTEM_PROPERTY, value = SYSTEM_PROPERTY_CFG_DIR) fun `test with system property`() { CommonFactory.createFromArguments().use { commonFactory -> - assertDictionaryDir(commonFactory, SYSTEM_PROPERTY_CONFIG_DIR.toPath()) - assertConfigs(commonFactory, SYSTEM_PROPERTY_CONFIG_DIR.toPath(), false) + assertDictionaryDirs(commonFactory, SYSTEM_PROPERTY_CFG_DIR_PATH) + assertConfigs(commonFactory, SYSTEM_PROPERTY_CFG_DIR_PATH) } } @Test - @SetSystemProperty(key = TH2_COMMON_CONFIGURATION_DIRECTORY_SYSTEM_PROPERTY, value = SYSTEM_PROPERTY_CONFIG_DIR) + @SetSystemProperty(key = TH2_COMMON_CONFIGURATION_DIRECTORY_SYSTEM_PROPERTY, value = SYSTEM_PROPERTY_CFG_DIR) fun `test with cmd config argument and system property`() { - CommonFactory.createFromArguments("-c", CMD_ARG_CONFIG_DIR).use { commonFactory -> - assertDictionaryDir(commonFactory, CMD_ARG_CONFIG_DIR.toPath()) - assertConfigs(commonFactory, CMD_ARG_CONFIG_DIR.toPath(), false) + CommonFactory.createFromArguments("-c", CMD_ARG_CFG_DIR).use { commonFactory -> + assertDictionaryDirs(commonFactory, CMD_ARG_CFG_DIR_PATH) + assertConfigs(commonFactory, CMD_ARG_CFG_DIR_PATH) } } } companion object { - private const val CMD_ARG_CONFIG_DIR = "src/test/resources/test_cmd_arg_config" - private const val SYSTEM_PROPERTY_CONFIG_DIR = "src/test/resources/test_system_property_config" - private val CUSTOM_DIR = Path.of("dictionary/custom/path") - - private const val ALIASES_DICTIONARY_NAME = "aliases" - private const val TYPES_DICTIONARY_NAME = "types" - private const val OLD_DICTIONARY_NAME = "old" - - private val CONFIG_NAME_TO_COMMON_FACTORY_SUPPLIER: Map = hashMapOf( - ALIASES_DICTIONARY_NAME to DictionaryDirMetadata(DICTIONARY_ALIAS_DIR_NAME, FactorySettings::dictionaryAliasesDir, CommonFactory::getPathToDictionaryAliasesDir), - TYPES_DICTIONARY_NAME to DictionaryDirMetadata(DICTIONARY_TYPE_DIR_NAME, FactorySettings::dictionaryTypesDir, CommonFactory::getPathToDictionaryTypesDir), - OLD_DICTIONARY_NAME to DictionaryDirMetadata("", FactorySettings::oldDictionariesDir, CommonFactory::getOldPathToDictionariesDir), - ) - - private val CONFIG_ALIASES: Map = hashMapOf( - RABBIT_MQ_CFG_ALIAS to ConfigMetadata(FactorySettings::rabbitMQ), - ROUTER_MQ_CFG_ALIAS to ConfigMetadata(FactorySettings::routerMQ), - CONNECTION_MANAGER_CFG_ALIAS to ConfigMetadata(FactorySettings::connectionManagerSettings), - GRPC_CFG_ALIAS to ConfigMetadata(FactorySettings::grpc), - ROUTER_GRPC_CFG_ALIAS to ConfigMetadata(FactorySettings::routerGRPC), - CRADLE_CONFIDENTIAL_CFG_ALIAS to ConfigMetadata(FactorySettings::cradleConfidential), - CRADLE_NON_CONFIDENTIAL_CFG_ALIAS to ConfigMetadata(FactorySettings::cradleNonConfidential), - PROMETHEUS_CFG_ALIAS to ConfigMetadata(FactorySettings::prometheus), - BOX_CFG_ALIAS to ConfigMetadata(FactorySettings::boxConfiguration), - CUSTOM_CFG_ALIAS to ConfigMetadata(FactorySettings::custom), - ) + private const val TEMP_DIR = "build/tmp/test/common-factory" + private const val CMD_ARG_CFG_DIR = "$TEMP_DIR/test-cmd-arg-config" + private const val SYSTEM_PROPERTY_CFG_DIR = "$TEMP_DIR/test-system-property-config" + private const val CUSTOM_DIR = "$TEMP_DIR/test-custom-dictionary-path" + + private val CMD_ARG_CFG_DIR_PATH = CMD_ARG_CFG_DIR.toPath() + private val SYSTEM_PROPERTY_CFG_DIR_PATH = SYSTEM_PROPERTY_CFG_DIR.toPath() + private val CUSTOM_DIR_PATH = CUSTOM_DIR.toPath() + + private val DICTIONARY_NAME = MAIN.name private fun String.toPath() = Path.of(this) - private fun assertDictionaryDir(commonFactory: CommonFactory, configPath: Path) { - CONFIG_NAME_TO_COMMON_FACTORY_SUPPLIER.forEach { (name, dictionaryDirMetadata) -> - assertEquals( - configPath.resolve(dictionaryDirMetadata.dirName), - dictionaryDirMetadata.getPath.invoke(commonFactory), - "Configured config path: $configPath, config name: $name" - ) + // FIXME: find another way to check default dictionary path + private fun assertThrowsDictionaryDir(commonFactory: CommonFactory) { + val exception = assertThrows("Search dictionary by default path") { + commonFactory.readDictionary() } + val message = assertNotNull(exception.message, "Exception message is null") + assertAll( + { + assertContains( + message, + other = CONFIG_DEFAULT_PATH.resolve("dictionaries").absolutePathString(), + message = "Exception message contains alias dictionaries path" + ) + }, + { + assertContains( + message, + other = CONFIG_DEFAULT_PATH.resolve("dictionary").absolutePathString(), + message = "Exception message contains type dictionaries path" + ) + }, + { + assertContains( + message, + other = CONFIG_DEFAULT_PATH.absolutePathString(), + message = "Exception message contains old dictionaries path" + ) + } + ) } - private fun assertConfigs(commonFactory: CommonFactory) { + private fun assertDictionaryDirs(commonFactory: CommonFactory, basePath: Path) { + assertAll( + { + val content = ALIAS.writeDictionaryByDefaultPath(basePath, MAIN.name) + ALIAS.assertDictionary(commonFactory, MAIN, content) + }, + { + val content = TYPE.writeDictionaryByDefaultPath(basePath, INCOMING.name) + TYPE.assertDictionary(commonFactory, INCOMING, content) + }, + { + val content = OLD.writeDictionaryByDefaultPath(basePath, OUTGOING.name) + OLD.assertDictionary(commonFactory, OUTGOING, content) + } + ) + } + + // FIXME: find another way to check default config path + private fun assertThrowsConfigs(commonFactory: CommonFactory) { + assertAll( + *ConfigHelper.values().asSequence() + .filter { it != ConfigHelper.PROMETHEUS_CFG_ALIAS && it != ConfigHelper.BOX_CFG_ALIAS } + .map { helper -> + { + val exception = assertThrows( + "Search config $helper by default path" + ) { + helper.loadConfig(commonFactory) + } + val message = assertNotNull(exception.message, "Exception message is null") + assertContains(message, other = CONFIG_DEFAULT_PATH.resolve("${helper.alias}.json").absolutePathString(), message = "Exception message contains alias config path") + } + }.toList().toTypedArray() + ) + val provider = assertProvider(commonFactory) assertEquals(CONFIG_DEFAULT_PATH, provider.baseDir) assertEquals(0, provider.customPaths.size) } - private fun assertConfigs(commonFactory: CommonFactory, configPath: Path, customPaths: Boolean) { - val provider = assertProvider(commonFactory) - assertEquals(configPath, provider.baseDir) - - if (customPaths) { - assertEquals(CONFIG_ALIASES.size, provider.customPaths.size) - CONFIG_ALIASES.keys.forEach { alias -> - val path = assertNotNull(provider.customPaths[alias]) - assertEquals(configPath, path.parent, "Configured config path: $configPath") + private fun assertConfigs(commonFactory: CommonFactory, configPath: Path) { + assertAll( + ConfigHelper.values().map { helper -> + { + val cfgBean = helper.writeConfigByDefaultPath(configPath) + assertThat(helper.loadConfig(commonFactory), samePropertyValuesAs(cfgBean)) + } } - } else { - assertEquals(0, provider.customPaths.size) - } + ) } private fun assertProvider(commonFactory: CommonFactory): JsonConfigurationProvider { @@ -370,57 +398,175 @@ class CommonFactoryTest { return JsonConfigurationProvider::class.cast(commonFactory.getConfigurationProvider()) } - private fun assertCustomPathForConfig(commonFactory: CommonFactory, basePath: Path, alias: String) { - val provider = assertProvider(commonFactory) - assertEquals(basePath, provider.baseDir) - assertEquals(1, provider.customPaths.size) - assertEquals(CUSTOM_DIR, provider.customPaths[alias]) - } - - private fun FactorySettings.setCustomPathForConfig(alias: String) { - CONFIG_ALIASES.asSequence() - .find { (optionName, _) -> optionName == alias } - ?.value?.setPath?.invoke(this, CUSTOM_DIR) - } + data class TestCustomConfig(val testField: String = "test-value") - private fun assertCustomDictionaryPath( - name: String, - basePath: Path, - commonFactory: CommonFactory + enum class ConfigHelper( + val alias: String, + private val configClass: Class<*> ) { - CONFIG_NAME_TO_COMMON_FACTORY_SUPPLIER.forEach { (optionName, dictionaryDirMetadata) -> - if (optionName == name) { - assertEquals( - CUSTOM_DIR, - dictionaryDirMetadata.getPath.invoke(commonFactory), - "Configured config path: $CUSTOM_DIR, config optionName: $optionName" - ) - } else { - assertEquals( - basePath.resolve(dictionaryDirMetadata.dirName), - dictionaryDirMetadata.getPath.invoke(commonFactory), - "Configured config path: $basePath, config optionName: $optionName" - ) + RABBIT_MQ_CFG_ALIAS("rabbitMQ", RabbitMQConfiguration::class.java) { + override fun setOption(settings: FactorySettings, filePath: Path) { + settings.rabbitMQ = filePath } - } - } - private fun FactorySettings.setCustomDictionaryPath( - name: String - ) { - CONFIG_NAME_TO_COMMON_FACTORY_SUPPLIER.asSequence() - .find { (optionName, _) -> optionName == name } - ?.value?.setPath?.invoke(this, CUSTOM_DIR) + override fun writeConfigByCustomPath(filePath: Path): RabbitMQConfiguration = RabbitMQConfiguration( + "test-host", + "test-vHost", + 1234, + "test-username", + "test-password", + ).also { CommonFactory.MAPPER.writeValue(filePath.toFile(), it) } + }, + ROUTER_MQ_CFG_ALIAS("mq", MessageRouterConfiguration::class.java) { + override fun setOption(settings: FactorySettings, filePath: Path) { + settings.routerMQ = filePath + } + + override fun writeConfigByCustomPath(filePath: Path): MessageRouterConfiguration = MessageRouterConfiguration() + .also { CommonFactory.MAPPER.writeValue(filePath.toFile(), it) } + }, + CONNECTION_MANAGER_CFG_ALIAS("mq_router", ConnectionManagerConfiguration::class.java) { + override fun setOption(settings: FactorySettings, filePath: Path) { + settings.connectionManagerSettings = filePath + } + override fun writeConfigByCustomPath(filePath: Path): ConnectionManagerConfiguration = ConnectionManagerConfiguration() + .also { CommonFactory.MAPPER.writeValue(filePath.toFile(), it) } + }, + GRPC_CFG_ALIAS("grpc", GrpcConfiguration::class.java) { + override fun setOption(settings: FactorySettings, filePath: Path) { + settings.grpc = filePath + } + + override fun writeConfigByCustomPath(filePath: Path): GrpcConfiguration = GrpcConfiguration() + .also { CommonFactory.MAPPER.writeValue(filePath.toFile(), it) } + }, + ROUTER_GRPC_CFG_ALIAS("grpc_router", GrpcRouterConfiguration::class.java) { + override fun setOption(settings: FactorySettings, filePath: Path) { + settings.routerGRPC = filePath + } + + override fun writeConfigByCustomPath(filePath: Path): GrpcRouterConfiguration = GrpcRouterConfiguration() + .also { CommonFactory.MAPPER.writeValue(filePath.toFile(), it) } + }, + CRADLE_CONFIDENTIAL_CFG_ALIAS("cradle", CradleConfidentialConfiguration::class.java) { + override fun setOption(settings: FactorySettings, filePath: Path) { + settings.cradleConfidential = filePath + } + + override fun writeConfigByCustomPath(filePath: Path): CradleConfidentialConfiguration = CradleConfidentialConfiguration( + "test-dataCenter", + "test-host", + "test-keyspace", + ).also { CommonFactory.MAPPER.writeValue(filePath.toFile(), it) } + }, + CRADLE_NON_CONFIDENTIAL_CFG_ALIAS("cradle_manager", CradleNonConfidentialConfiguration::class.java) { + override fun setOption(settings: FactorySettings, filePath: Path) { + settings.cradleNonConfidential = filePath + } + + override fun writeConfigByCustomPath(filePath: Path): CradleNonConfidentialConfiguration = CradleNonConfidentialConfiguration() + .also { CommonFactory.MAPPER.writeValue(filePath.toFile(), it) } + }, + PROMETHEUS_CFG_ALIAS("prometheus", PrometheusConfiguration::class.java) { + override fun setOption(settings: FactorySettings, filePath: Path) { + settings.prometheus = filePath + } + + override fun writeConfigByCustomPath(filePath: Path): PrometheusConfiguration = PrometheusConfiguration() + .also { CommonFactory.MAPPER.writeValue(filePath.toFile(), it) } + }, + BOX_CFG_ALIAS("box", BoxConfiguration::class.java) { + override fun setOption(settings: FactorySettings, filePath: Path) { + settings.boxConfiguration = filePath + } + + override fun writeConfigByCustomPath(filePath: Path): BoxConfiguration = BoxConfiguration() + .also { CommonFactory.MAPPER.writeValue(filePath.toFile(), it) } + }, + CUSTOM_CFG_ALIAS("custom", TestCustomConfig::class.java) { + override fun setOption(settings: FactorySettings, filePath: Path) { + settings.custom = filePath + } + + override fun writeConfigByCustomPath(filePath: Path): TestCustomConfig = TestCustomConfig() + .also { CommonFactory.MAPPER.writeValue(filePath.toFile(), it) } + }; + + abstract fun setOption(settings: FactorySettings, filePath: Path) + + abstract fun writeConfigByCustomPath(filePath: Path): Any + + open fun writeConfigByDefaultPath(configPath: Path): Any = writeConfigByCustomPath(configPath.resolve("${alias}.json")) + + fun loadConfig(factory: CommonFactory): Any = + factory.getConfigurationProvider().load(alias, configClass) } - private data class DictionaryDirMetadata( - val dirName: String, - val setPath: FactorySettings.(Path) -> Unit, - val getPath: CommonFactory.() -> Path, - ) + enum class DictionaryHelper { + ALIAS { + override fun setOption(settings: FactorySettings, path: Path) { + settings.dictionaryAliasesDir = path + } + + override fun writeDictionaryByDefaultPath(basePath: Path, fileName: String): String { + return DictionaryHelper.writeDictionary(basePath.resolve("dictionaries").resolve(fileName)) + } + + override fun writeDictionaryByCustomPath(basePath: Path, fileName: String): String { + return DictionaryHelper.writeDictionary(basePath.resolve(fileName)) + } + }, + TYPE { + override fun setOption(settings: FactorySettings, path: Path) { + settings.dictionaryTypesDir = path + } + + override fun writeDictionaryByDefaultPath(basePath: Path, fileName: String): String { + return DictionaryHelper.writeDictionary(basePath.resolve("dictionary").resolve(fileName.lowercase( + Locale.getDefault() + )).resolve(fileName)) + } + + override fun writeDictionaryByCustomPath(basePath: Path, fileName: String): String { + return DictionaryHelper.writeDictionary(basePath.resolve(fileName.lowercase( + Locale.getDefault() + )).resolve(fileName)) + } + }, + OLD { + override fun setOption(settings: FactorySettings, path: Path) { + settings.oldDictionariesDir = path + } + + override fun writeDictionaryByDefaultPath(basePath: Path, fileName: String): String { + return DictionaryHelper.writeDictionary(basePath.resolve(fileName)) + } + + override fun writeDictionaryByCustomPath(basePath: Path, fileName: String): String { + return DictionaryHelper.writeDictionary(basePath.resolve(fileName)) + } + }; + + abstract fun setOption(settings: FactorySettings, path: Path) + abstract fun writeDictionaryByDefaultPath(basePath: Path, fileName: String): String + abstract fun writeDictionaryByCustomPath(basePath: Path, fileName: String): String + + fun assertDictionary(factory: CommonFactory, type: DictionaryType, content: String) { + assertEquals(content, factory.readDictionary(type).use { String(it.readAllBytes()) }) + } + + companion object { + fun assertDictionary(factory: CommonFactory, content: String) { + assertEquals(content, factory.readDictionary().use { String(it.readAllBytes()) }) + } - private data class ConfigMetadata( - val setPath: FactorySettings.(Path) -> Unit, - ) + private fun writeDictionary(path: Path): String { + val content = RandomStringUtils.randomAlphanumeric(10) + path.parent.createDirectories() + path.writeBytes(ArchiveUtils.getGzipBase64StringEncoder().encode(content)) + return content + } + } + } } } \ No newline at end of file