From 1d128bfd9bfb1306c92bcd072020708695d8a1e9 Mon Sep 17 00:00:00 2001 From: Dmitry Aleksandrov Date: Mon, 15 May 2023 17:16:50 +0300 Subject: [PATCH 1/3] Add generic Config convert and tests --- .../java/io/helidon/config/mp/MpConfig.java | 57 ++++++++++++------- .../config/MpConfigConvertTest.java | 50 ++++++++++++++++ 2 files changed, 86 insertions(+), 21 deletions(-) create mode 100644 microprofile/config/src/test/java/io/helidon/microprofile/config/MpConfigConvertTest.java diff --git a/config/config-mp/src/main/java/io/helidon/config/mp/MpConfig.java b/config/config-mp/src/main/java/io/helidon/config/mp/MpConfig.java index 09a635bdd56..fcf9bc7664d 100644 --- a/config/config-mp/src/main/java/io/helidon/config/mp/MpConfig.java +++ b/config/config-mp/src/main/java/io/helidon/config/mp/MpConfig.java @@ -16,7 +16,9 @@ package io.helidon.config.mp; +import java.util.HashMap; import java.util.Iterator; +import java.util.Map; import io.helidon.config.ConfigSources; import io.helidon.config.OverrideSources; @@ -48,29 +50,42 @@ public static io.helidon.config.Config toHelidonConfig(Config mpConfig) { return (io.helidon.config.Config) mpConfig; } - // If the mpConfig is based on an SE config (such as when we use meta configuration)pom.xml - // we must reuse that se config instance - Iterator configSources = mpConfig.getConfigSources().iterator(); - ConfigSource first = configSources.hasNext() ? configSources.next() : null; - if (!configSources.hasNext() && first instanceof MpHelidonConfigSource) { - // we only have Helidon SE config as a source - let's just use it - return ((MpHelidonConfigSource) first).unwrap(); + if (mpConfig instanceof MpConfigImpl) { + + // If the mpConfig is based on an SE config (such as when we use meta configuration)pom.xml + // we must reuse that se config instance + Iterator configSources = mpConfig.getConfigSources().iterator(); + ConfigSource first = configSources.hasNext() ? configSources.next() : null; + if (!configSources.hasNext() && first instanceof MpHelidonConfigSource) { + // we only have Helidon SE config as a source - let's just use it + return ((MpHelidonConfigSource) first).unwrap(); + } + + // we use Helidon SE config to handle object mapping (and possible other mappers on classpath) + io.helidon.config.Config mapper = io.helidon.config.Config.builder() + .sources(ConfigSources.empty()) + .overrides(OverrideSources.empty()) + .disableEnvironmentVariablesSource() + .disableSystemPropertiesSource() + .disableParserServices() + .disableFilterServices() + .disableCaching() + .disableValueResolving() + .changesExecutor(command -> { + }) + .build(); + + return new SeConfig(mapper, mpConfig); } - // we use Helidon SE config to handle object mapping (and possible other mappers on classpath) - io.helidon.config.Config mapper = io.helidon.config.Config.builder() - .sources(ConfigSources.empty()) - .overrides(OverrideSources.empty()) - .disableEnvironmentVariablesSource() - .disableSystemPropertiesSource() - .disableParserServices() - .disableFilterServices() - .disableCaching() - .disableValueResolving() - .changesExecutor(command -> { - }) - .build(); + // Generic Properties convert + Map propertyMap = new HashMap<>(); + for (ConfigSource configSource : mpConfig.getConfigSources()) { + for (String propertyName : configSource.getPropertyNames()) { + propertyMap.putIfAbsent(propertyName, configSource.getValue(propertyName)); + } + } - return new SeConfig(mapper, mpConfig); + return io.helidon.config.Config.create(ConfigSources.create(propertyMap)); } } diff --git a/microprofile/config/src/test/java/io/helidon/microprofile/config/MpConfigConvertTest.java b/microprofile/config/src/test/java/io/helidon/microprofile/config/MpConfigConvertTest.java new file mode 100644 index 00000000000..597bcba909b --- /dev/null +++ b/microprofile/config/src/test/java/io/helidon/microprofile/config/MpConfigConvertTest.java @@ -0,0 +1,50 @@ +/* +<<<<<<< HEAD + * Copyright (c) 2013 Oracle and/or its affiliates. +======= + * Copyright (c) 2023 Oracle and/or its affiliates. +>>>>>>> 53788d2db4 (Refactor tests) + * + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.helidon.microprofile.config; + +import io.helidon.config.mp.MpConfig; +import io.helidon.microprofile.tests.junit5.AddConfig; +import io.helidon.microprofile.tests.junit5.HelidonTest; +import jakarta.inject.Inject; +import org.eclipse.microprofile.config.Config; +import org.junit.jupiter.api.Test; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; + +/** + * Test for correct handling of other implementations of {@link Config}. + * + * @see "https://github.com/helidon-io/helidon/issues/6668" + */ +@HelidonTest +@AddConfig(key = "key", value = "value") +public class MpConfigConvertTest { + + @Inject + private Config mpConfig; + + //No exceptions should occur. + @Test + void testConvertToHelidonConfig() { + io.helidon.config.Config helidonConfig = MpConfig.toHelidonConfig(mpConfig); + assertThat(helidonConfig.get("key").asString().get(), is("value")); + } +} From 39061f4c87a91cf03ff172a00368a652248544ca Mon Sep 17 00:00:00 2001 From: Dmitry Aleksandrov Date: Tue, 16 May 2023 16:23:08 +0300 Subject: [PATCH 2/3] Fix copyright --- .../config-mp/src/main/java/io/helidon/config/mp/MpConfig.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/config-mp/src/main/java/io/helidon/config/mp/MpConfig.java b/config/config-mp/src/main/java/io/helidon/config/mp/MpConfig.java index fcf9bc7664d..4cae9f4b0ec 100644 --- a/config/config-mp/src/main/java/io/helidon/config/mp/MpConfig.java +++ b/config/config-mp/src/main/java/io/helidon/config/mp/MpConfig.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020, 2021 Oracle and/or its affiliates. + * Copyright (c) 2020, 2023 Oracle and/or its affiliates. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. From a3d6d6d5278d20997cdd754f7c660a31483bcfd4 Mon Sep 17 00:00:00 2001 From: Dmitry Aleksandrov Date: Tue, 16 May 2023 16:33:29 +0300 Subject: [PATCH 3/3] Fix header --- .../io/helidon/microprofile/config/MpConfigConvertTest.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/microprofile/config/src/test/java/io/helidon/microprofile/config/MpConfigConvertTest.java b/microprofile/config/src/test/java/io/helidon/microprofile/config/MpConfigConvertTest.java index 597bcba909b..99c6ff41ddd 100644 --- a/microprofile/config/src/test/java/io/helidon/microprofile/config/MpConfigConvertTest.java +++ b/microprofile/config/src/test/java/io/helidon/microprofile/config/MpConfigConvertTest.java @@ -1,9 +1,5 @@ /* -<<<<<<< HEAD - * Copyright (c) 2013 Oracle and/or its affiliates. -======= * Copyright (c) 2023 Oracle and/or its affiliates. ->>>>>>> 53788d2db4 (Refactor tests) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License.