-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Only record config properties coming from ConfigSources
- Loading branch information
Showing
4 changed files
with
169 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
...nt/src/test/java/io/quarkus/restclient/configuration/RestClientBuildTimeConfigSource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package io.quarkus.restclient.configuration; | ||
|
||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.Set; | ||
|
||
import org.eclipse.microprofile.config.ConfigProvider; | ||
import org.eclipse.microprofile.config.spi.ConfigSource; | ||
|
||
import io.smallrye.config.common.MapBackedConfigSource; | ||
|
||
public class RestClientBuildTimeConfigSource extends MapBackedConfigSource { | ||
public RestClientBuildTimeConfigSource() { | ||
super(RestClientBuildTimeConfigSource.class.getName(), new HashMap<>()); | ||
} | ||
|
||
@Override | ||
public String getValue(final String propertyName) { | ||
if (!propertyName.equals("io.quarkus.restclient.configuration.EchoClient/mp-rest/url")) { | ||
return null; | ||
} | ||
|
||
if (isBuildTime()) { | ||
return "http://nohost:${quarkus.http.test-port:8081}"; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
@Override | ||
public Set<String> getPropertyNames() { | ||
return Collections.singleton("io.quarkus.restclient.configuration.EchoClient/mp-rest/url"); | ||
} | ||
|
||
@Override | ||
public int getOrdinal() { | ||
return Integer.MAX_VALUE; | ||
} | ||
|
||
private static boolean isBuildTime() { | ||
for (ConfigSource configSource : ConfigProvider.getConfig().getConfigSources()) { | ||
if (configSource.getClass().getSimpleName().equals("BuildTimeEnvConfigSource")) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
...rc/test/java/io/quarkus/restclient/configuration/RestClientOverrideRuntimeConfigTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package io.quarkus.restclient.configuration; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import java.util.Optional; | ||
|
||
import javax.inject.Inject; | ||
|
||
import org.eclipse.microprofile.config.spi.ConfigSource; | ||
import org.eclipse.microprofile.rest.client.inject.RestClient; | ||
import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
import org.jboss.shrinkwrap.api.spec.JavaArchive; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.test.QuarkusUnitTest; | ||
import io.smallrye.config.ConfigValue; | ||
import io.smallrye.config.SmallRyeConfig; | ||
|
||
public class RestClientOverrideRuntimeConfigTest { | ||
@RegisterExtension | ||
static final QuarkusUnitTest TEST = new QuarkusUnitTest().setArchiveProducer( | ||
() -> ShrinkWrap.create(JavaArchive.class) | ||
.addClasses(EchoResource.class, EchoClient.class, RestClientBuildTimeConfigSource.class, | ||
RestClientRunTimeConfigSource.class) | ||
.addAsServiceProvider("org.eclipse.microprofile.config.spi.ConfigSource", | ||
"io.quarkus.restclient.configuration.RestClientBuildTimeConfigSource", | ||
"io.quarkus.restclient.configuration.RestClientRunTimeConfigSource")); | ||
|
||
@Inject | ||
@RestClient | ||
EchoClient echoClient; | ||
@Inject | ||
SmallRyeConfig config; | ||
|
||
@Test | ||
void overrideConfig() { | ||
Optional<ConfigSource> specifiedDefaultValues = config | ||
.getConfigSource("PropertiesConfigSource[source=Specified default values]"); | ||
assertTrue(specifiedDefaultValues.isPresent()); | ||
assertTrue(specifiedDefaultValues.get().getPropertyNames() | ||
.contains("io.quarkus.restclient.configuration.EchoClient/mp-rest/url")); | ||
// This config key comes from the interceptor and it is recorded with an empty value. This allow the user to still override using the original key | ||
assertEquals("", specifiedDefaultValues.get() | ||
.getValue("quarkus.rest-client.\"io.quarkus.restclient.configuration.EchoClient\".url")); | ||
|
||
ConfigValue mpValue = config.getConfigValue("io.quarkus.restclient.configuration.EchoClient/mp-rest/url"); | ||
// Fallbacks from runtime to the override build time value | ||
ConfigValue quarkusValue = config | ||
.getConfigValue("quarkus.rest-client.\"io.quarkus.restclient.configuration.EchoClient\".url"); | ||
assertEquals(mpValue.getValue(), quarkusValue.getValue()); | ||
assertEquals(RestClientRunTimeConfigSource.class.getName(), quarkusValue.getConfigSourceName()); | ||
|
||
assertEquals("Hi", echoClient.echo("Hi")); | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
...ment/src/test/java/io/quarkus/restclient/configuration/RestClientRunTimeConfigSource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package io.quarkus.restclient.configuration; | ||
|
||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.Set; | ||
|
||
import org.eclipse.microprofile.config.ConfigProvider; | ||
import org.eclipse.microprofile.config.spi.ConfigSource; | ||
|
||
import io.quarkus.runtime.annotations.StaticInitSafe; | ||
import io.smallrye.config.common.MapBackedConfigSource; | ||
|
||
@StaticInitSafe | ||
public class RestClientRunTimeConfigSource extends MapBackedConfigSource { | ||
public RestClientRunTimeConfigSource() { | ||
super(RestClientRunTimeConfigSource.class.getName(), new HashMap<>()); | ||
} | ||
|
||
@Override | ||
public String getValue(final String propertyName) { | ||
if (!propertyName.equals("io.quarkus.restclient.configuration.EchoClient/mp-rest/url")) { | ||
return null; | ||
} | ||
|
||
if (isRuntime()) { | ||
return "http://localhost:${quarkus.http.test-port:8081}"; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
@Override | ||
public Set<String> getPropertyNames() { | ||
return Collections.singleton("io.quarkus.restclient.configuration.EchoClient/mp-rest/url"); | ||
} | ||
|
||
@Override | ||
public int getOrdinal() { | ||
return Integer.MAX_VALUE; | ||
} | ||
|
||
private static boolean isRuntime() { | ||
for (ConfigSource configSource : ConfigProvider.getConfig().getConfigSources()) { | ||
if (configSource.getName().equals("PropertiesConfigSource[source=Specified default values]")) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
} |