Skip to content

Commit

Permalink
Merge pull request #32130 from geoand/#32097
Browse files Browse the repository at this point in the history
Add support for specifying custom profiles in Spring Cloud Config client
  • Loading branch information
geoand authored Mar 25, 2023
2 parents d341764 + ec8dea2 commit 4c381bb
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.nio.file.Path;
import java.time.Duration;
import java.util.List;
import java.util.Map;
import java.util.Optional;

Expand Down Expand Up @@ -110,6 +111,11 @@ public interface SpringCloudConfigClientConfig {
*/
Map<String, String> headers();

/**
* The profiles to use for lookup
*/
Optional<List<String>> profiles();

/** */
default boolean usernameAndPasswordSet() {
return username().isPresent() && password().isPresent();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public Iterable<ConfigSource> getConfigSources(final ConfigSourceContext context
VertxSpringCloudConfigGateway client = new VertxSpringCloudConfigGateway(config);
try {
List<Response> responses = new ArrayList<>();
for (String profile : context.getProfiles()) {
for (String profile : determineProfiles(context, config)) {
Response response;
if (connectionTimeoutIsGreaterThanZero || readTimeoutIsGreaterThanZero) {
response = client.exchange(applicationName.getValue(), profile).await()
Expand Down Expand Up @@ -88,6 +88,13 @@ public Iterable<ConfigSource> getConfigSources(final ConfigSourceContext context
}
}

private static List<String> determineProfiles(ConfigSourceContext context, SpringCloudConfigClientConfig config) {
if (config.profiles().isPresent()) {
return config.profiles().get();
}
return context.getProfiles();
}

private static class SpringCloudPropertySource extends MapBackedConfigSource {
private SpringCloudPropertySource(final String name, final Map<String, String> propertyMap, final int defaultOrdinal) {
super(name, propertyMap, defaultOrdinal);
Expand Down
29 changes: 29 additions & 0 deletions integration-tests/spring-cloud-config-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,35 @@
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<executions>
<execution>
<id>default-test</id>
<configuration>
<!-- include tags -->
<groups>common,test</groups>
<!-- exclude tags -->
<excludedGroups>test-only</excludedGroups>
</configuration>
</execution>
<!-- This configuration is used in order to test that 'quarkus.spring-cloud-config.profiles' is taken into account -->
<execution>
<id>profiles-test</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<systemPropertyVariables>
<quarkus.spring-cloud-config.profiles>test</quarkus.spring-cloud-config.profiles>
</systemPropertyVariables>
<!-- include tags -->
<groups>test-only</groups>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@
import static jakarta.ws.rs.core.Response.Status.OK;
import static org.hamcrest.Matchers.equalTo;

import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

import io.quarkus.test.common.QuarkusTestResource;
import io.quarkus.test.junit.QuarkusTest;

@QuarkusTest
@QuarkusTestResource(SpringCloudConfigServerResource.class)
public class SpringCloudConfigClientTest {
@Tag("common")
@Tag("test")
public class CommonAndTestProfilesTest {
@Test
void config() {
given()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package io.quarkus.spring.cloud.config.client.runtime;

import static io.restassured.RestAssured.given;
import static jakarta.ws.rs.core.Response.Status.OK;
import static org.hamcrest.Matchers.equalTo;

import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

import io.quarkus.test.common.QuarkusTestResource;
import io.quarkus.test.junit.QuarkusTest;

@QuarkusTest
@QuarkusTestResource(SpringCloudConfigServerResource.class)
@Tag("test")
@Tag("test-only")
public class OnlyTestProfileTest {
@Test
void config() {
given()
.get("/config/{name}", "greeting.message")
.then()
.statusCode(OK.getStatusCode())
.body("value", equalTo("hello from spring cloud config server"));
}

@Test
void ordinal() {
given()
.get("/config/{name}", "foo")
.then()
.statusCode(OK.getStatusCode())
.body("value", equalTo("from foo development"))
.body("sourceName", equalTo("https://github.com/spring-cloud-samples/config-repo/testapp-prod.yml"));

given()
.get("/config/{name}", "info.description")
.then()
.statusCode(OK.getStatusCode())
.body("value", equalTo("Sample"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
import io.quarkus.test.junit.QuarkusIntegrationTest;

@QuarkusIntegrationTest
public class SpringCloudConfigClientIT extends SpringCloudConfigClientTest {
public class SpringCloudConfigClientIT extends CommonAndTestProfilesTest {

}

0 comments on commit 4c381bb

Please sign in to comment.