Skip to content

Commit

Permalink
Add generic Config convert and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dalexandrov committed May 16, 2023
1 parent 870fed6 commit 1d128bf
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 21 deletions.
57 changes: 36 additions & 21 deletions config/config-mp/src/main/java/io/helidon/config/mp/MpConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<ConfigSource> 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<ConfigSource> 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<String, String> 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));
}
}
Original file line number Diff line number Diff line change
@@ -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"));
}
}

0 comments on commit 1d128bf

Please sign in to comment.