Skip to content

Commit

Permalink
4.x: Enable microprofile/tests/tck/tck-config/src/test/tck-suite.xml …
Browse files Browse the repository at this point in the history
…tests #8173

Signed-off-by: Jorge Bescos Gascon <jorge.bescos.gascon@oracle.com>
  • Loading branch information
jbescos committed Dec 21, 2023
1 parent 99ab65c commit 1b27d61
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 75 deletions.
167 changes: 100 additions & 67 deletions config/config-mp/src/main/java/io/helidon/config/mp/MpConfigImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,15 @@ class MpConfigImpl implements Config {

@Override
public ConfigValue getConfigValue(String key) {
String profiledKey = null;
if (configProfile != null) {
profiledKey = "%" + configProfile + "." + key;
}
if (configProfile == null) {
return findConfigValue(key)
return findConfigValue(key, Optional.empty())
.orElseGet(() -> new ConfigValueImpl(key, null, null, null, 0));
}
return findConfigValue("%" + configProfile + "." + key)
.or(() -> findConfigValue(key))
return findConfigValue(key, Optional.of(profiledKey))
.orElseGet(() -> new ConfigValueImpl(key, null, null, null, 0));
}

Expand All @@ -126,70 +129,88 @@ public <T> T getValue(String propertyName, Class<T> propertyType) {
@SuppressWarnings("unchecked")
@Override
public <T> Optional<T> getOptionalValue(String propertyName, Class<T> propertyType) {
String profiledPropertyName = null;
if (configProfile != null) {
profiledPropertyName = "%" + configProfile + "." + propertyName;
}
if (configProfile == null) {
return optionalValue(propertyName, propertyType);
return optionalValue(propertyName, propertyType, Optional.empty());
}

return optionalValue("%" + configProfile + "." + propertyName, propertyType)
.or(() -> optionalValue(propertyName, propertyType));
return optionalValue(propertyName, propertyType, Optional.of(profiledPropertyName));
}

@SuppressWarnings("unchecked")
private <T> Optional<T> optionalValue(String propertyName, Class<T> propertyType) {
// let's resolve arrays
if (propertyType.isArray()) {
Class<?> componentType = propertyType.getComponentType();
// first try to see if we have a direct value
Optional<String> optionalValue = getOptionalValue(propertyName, String.class);
if (optionalValue.isPresent()) {
try {
return Optional.of((T) toArray(propertyName, optionalValue.get(), componentType));
} catch (NoSuchElementException e) {
return Optional.empty();
}
private <T> Optional<T> arrayValue(String propertyName, Class<T> propertyType) {
Class<?> componentType = propertyType.getComponentType();
// first try to see if we have a direct value
Optional<String> optionalValue = getOptionalValue(propertyName, String.class);
if (optionalValue.isPresent()) {
try {
return Optional.of((T) toArray(propertyName, optionalValue.get(), componentType));
} catch (NoSuchElementException e) {
return Optional.empty();
}
}

/*
we also support indexed value
e.g. for key "my.list" you can have both:
my.list=12,13,14
or (not and):
my.list.0=12
my.list.1=13
*/

String indexedConfigKey = propertyName + ".0";
optionalValue = getOptionalValue(indexedConfigKey, String.class);
if (optionalValue.isPresent()) {
List<Object> result = new LinkedList<>();

// first element is already in
result.add(convert(indexedConfigKey, componentType, optionalValue.get()));

// hardcoded limit to lists of 1000 elements
for (int i = 1; i < 1000; i++) {
indexedConfigKey = propertyName + "." + i;
optionalValue = getOptionalValue(indexedConfigKey, String.class);
if (optionalValue.isPresent()) {
result.add(convert(indexedConfigKey, componentType, optionalValue.get()));
} else {
// finish the iteration on first missing index
break;
}
/*
we also support indexed value
e.g. for key "my.list" you can have both:
my.list=12,13,14
or (not and):
my.list.0=12
my.list.1=13
*/

String indexedConfigKey = propertyName + ".0";
optionalValue = getOptionalValue(indexedConfigKey, String.class);
if (optionalValue.isPresent()) {
List<Object> result = new LinkedList<>();

// first element is already in
result.add(convert(indexedConfigKey, componentType, optionalValue.get()));

// hardcoded limit to lists of 1000 elements
for (int i = 1; i < 1000; i++) {
indexedConfigKey = propertyName + "." + i;
optionalValue = getOptionalValue(indexedConfigKey, String.class);
if (optionalValue.isPresent()) {
result.add(convert(indexedConfigKey, componentType, optionalValue.get()));
} else {
// finish the iteration on first missing index
break;
}
Object array = Array.newInstance(componentType, result.size());
for (int i = 0; i < result.size(); i++) {
Object component = result.get(i);
Array.set(array, i, component);
}
return Optional.of((T) array);
} else {
return Optional.empty();
}
Object array = Array.newInstance(componentType, result.size());
for (int i = 0; i < result.size(); i++) {
Object component = result.get(i);
Array.set(array, i, component);
}
return Optional.of((T) array);
} else {
return findConfigValue(propertyName)
return Optional.empty();
}
}

private <T> Optional<T> optionalValue(String propertyName, Class<T> propertyType, Optional<String> profiledPropertyName) {
// let's resolve arrays
if (propertyType.isArray()) {
Optional<T> array = Optional.empty();
if (profiledPropertyName.isPresent()) {
// Try first with profiled property
array = arrayValue(profiledPropertyName.get(), propertyType);
}
if (array.isEmpty()) {
array = arrayValue(propertyName, propertyType);
}
return array;
} else {
Optional<ConfigValue> configVal = findConfigValue(propertyName, profiledPropertyName);
String name = configVal.isPresent() ? configVal.get().getName() : null;
return configVal
.map(ConfigValue::getValue)
.map(it -> convert(propertyName, propertyType, it));
.map(it -> convert(name, propertyType, it));

}
}

Expand Down Expand Up @@ -313,35 +334,47 @@ private <T> T convert(String propertyName, Class<T> type, String value) {
}
}

private Optional<ConfigValue> findConfigValue(String propertyName) {
private Optional<ConfigValue> findConfigValue(String propertyName, Optional<String> profiledPropertyName) {
for (ConfigSource source : sources) {
String value = source.getValue(propertyName);
String selectedProperty = null;
String value = null;
if (profiledPropertyName.isPresent()) {
// Try profiled property first
selectedProperty = profiledPropertyName.get();
value = source.getValue(profiledPropertyName.get());
}
if (value == null) {
selectedProperty = propertyName;
value = source.getValue(propertyName);
}

if (null == value) {
// not in this one
continue;
}

String rawValue = value;
String name = source.getName();
int ordinal = source.getOrdinal();
final String propName = selectedProperty;
if (value.isEmpty()) {
if (LOGGER.isLoggable(Level.TRACE)) {
LOGGER.log(Level.TRACE, "Found property " + propertyName
LOGGER.log(Level.TRACE, "Found property " + propName
+ " in source " + source.getName()
+ " and it is empty (removed)");
}
return Optional.empty();
return Optional.of(new ConfigValueImpl(propName, null, rawValue, name, ordinal));
}

if (LOGGER.isLoggable(Level.TRACE)) {
LOGGER.log(Level.TRACE, "Found property " + propertyName + " in source " + source.getName());
LOGGER.log(Level.TRACE, "Found property " + propName + " in source " + source.getName());
}
String rawValue = value;

try {
return applyFilters(propertyName, value)
.map(it -> resolveReferences(propertyName, it))
.map(it -> new ConfigValueImpl(propertyName, it, rawValue, source.getName(), source.getOrdinal()));
return applyFilters(propName, value)
.map(it -> resolveReferences(propName, it))
.map(it -> new ConfigValueImpl(propName, it, rawValue, name, ordinal));
} catch (NoSuchElementException e) {
// Property expression does not resolve
return Optional.empty();
return Optional.of(new ConfigValueImpl(propName, null, rawValue, name, ordinal));
}
}

Expand Down
3 changes: 1 addition & 2 deletions dependencies/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,7 @@
<version.lib.micronaut>3.8.7</version.lib.micronaut>
<version.lib.micronaut.data>3.4.3</version.lib.micronaut.data>
<version.lib.micronaut.sql>4.8.0</version.lib.micronaut.sql>
<!-- FIXME upgrade to 3.1 when it is released in Maven -->
<version.lib.microprofile-config>3.0.3</version.lib.microprofile-config>
<version.lib.microprofile-config>3.1</version.lib.microprofile-config>
<!-- FIXME upgrade to 4.1 when it is released in Maven -->
<version.lib.microprofile-fault-tolerance-api>4.0.2</version.lib.microprofile-fault-tolerance-api>
<version.lib.microprofile-graphql>2.0</version.lib.microprofile-graphql>
Expand Down
21 changes: 21 additions & 0 deletions microprofile/tests/server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,25 @@
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!--
Override plugin to be executed every test in a new JVM.
This is required because there are some system property set.
-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<forkCount>1</forkCount>
<reuseForks>false</reuseForks>
<useModulePath>false</useModulePath>
<trimStackTrace>false</trimStackTrace>
<!-- DO NOT override argLine instead use surefire.argLine -->
<argLine>${surefire.argLine} ${surefire.coverage.argline}</argLine>
<redirectTestOutputToFile>true</redirectTestOutputToFile>
</configuration>
</plugin>
</plugins>
</build>
</project>
6 changes: 0 additions & 6 deletions microprofile/tests/tck/tck-config/src/test/tck-suite.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,6 @@
<test name="microprofile-config TCK">
<packages>
<package name="org.eclipse.microprofile.config.tck.*">
<!--
Currently failing because requires this PR:
https://github.com/eclipse/microprofile-config/pull/743
Ignoring meanwhile microprofile-config 3.1 is not released
-->
<exclude name="org.eclipse.microprofile.config.tck.broken"></exclude>
</package>
</packages>
</test>
Expand Down

0 comments on commit 1b27d61

Please sign in to comment.