diff --git a/spring-cloud-consul-config/src/main/java/org/springframework/cloud/consul/config/ConsulConfigProperties.java b/spring-cloud-consul-config/src/main/java/org/springframework/cloud/consul/config/ConsulConfigProperties.java index ea82366f..151552ae 100644 --- a/spring-cloud-consul-config/src/main/java/org/springframework/cloud/consul/config/ConsulConfigProperties.java +++ b/spring-cloud-consul-config/src/main/java/org/springframework/cloud/consul/config/ConsulConfigProperties.java @@ -35,7 +35,6 @@ import lombok.Data; public class ConsulConfigProperties { private boolean enabled = true; - @NotEmpty private String prefix = "config"; @NotEmpty diff --git a/spring-cloud-consul-config/src/main/java/org/springframework/cloud/consul/config/ConsulPropertySourceLocator.java b/spring-cloud-consul-config/src/main/java/org/springframework/cloud/consul/config/ConsulPropertySourceLocator.java index 84bdac1d..bdf1b087 100644 --- a/spring-cloud-consul-config/src/main/java/org/springframework/cloud/consul/config/ConsulPropertySourceLocator.java +++ b/spring-cloud-consul-config/src/main/java/org/springframework/cloud/consul/config/ConsulPropertySourceLocator.java @@ -31,6 +31,7 @@ import org.springframework.core.env.Environment; import org.springframework.core.env.PropertySource; import org.springframework.retry.annotation.Retryable; import org.springframework.util.ReflectionUtils; +import org.springframework.util.StringUtils; import com.ecwid.consul.v1.ConsulClient; import com.ecwid.consul.v1.Response; @@ -94,7 +95,8 @@ public class ConsulPropertySourceLocator implements PropertySourceLocator { suffixes.add(".properties"); } - String defaultContext = prefix + "/" + this.properties.getDefaultContext(); + String defaultContext = getContext(prefix, this.properties.getDefaultContext()); + for (String suffix : suffixes) { this.contexts.add(defaultContext + suffix); } @@ -102,7 +104,8 @@ public class ConsulPropertySourceLocator implements PropertySourceLocator { addProfiles(this.contexts, defaultContext, profiles, suffix); } - String baseContext = prefix + "/" + appName; + String baseContext = getContext(prefix, appName); + for (String suffix : suffixes) { this.contexts.add(baseContext + suffix); } @@ -146,6 +149,14 @@ public class ConsulPropertySourceLocator implements PropertySourceLocator { return null; } + private String getContext(String prefix, String context) { + if (StringUtils.isEmpty(prefix)) { + return context; + } else { + return prefix + "/" + context; + } + } + private void addIndex(String propertySourceContext, Long consulIndex) { contextIndex.put(propertySourceContext, consulIndex); } diff --git a/spring-cloud-consul-config/src/test/java/org/springframework/cloud/consul/config/ConsulPropertyPrefixTests.java b/spring-cloud-consul-config/src/test/java/org/springframework/cloud/consul/config/ConsulPropertyPrefixTests.java new file mode 100644 index 00000000..460bed9e --- /dev/null +++ b/spring-cloud-consul-config/src/test/java/org/springframework/cloud/consul/config/ConsulPropertyPrefixTests.java @@ -0,0 +1,54 @@ +package org.springframework.cloud.consul.config; + +import com.ecwid.consul.v1.ConsulClient; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.springframework.cloud.consul.ConsulProperties; + +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.notNullValue; +import static org.junit.Assert.assertThat; + +public class ConsulPropertyPrefixTests { + + private ConsulClient client; + + @Before + public void setup() { + ConsulProperties properties = new ConsulProperties(); + client = new ConsulClient(properties.getHost(), properties.getPort()); + } + + @After + public void teardown() { + client.deleteKVValues(""); + } + + @Test + public void testEmptyPrefix() { + // because prefix is empty, a leading forward slash is omitted + String kvContext = "appname"; + client.setKVValue(kvContext + "/fooprop", "fookvval"); + client.setKVValue(kvContext + "/bar/prop", "8080"); + + ConsulPropertySource source = getConsulPropertySource(new ConsulConfigProperties(), kvContext); + assertProperties(source, "fookvval", "8080"); + } + + private void assertProperties(ConsulPropertySource source, Object fooval, Object barval) { + assertThat("fooprop was wrong", source.getProperty("fooprop"), is(equalTo(fooval))); + assertThat("bar.prop was wrong", source.getProperty("bar.prop"), is(equalTo(barval))); + } + + @SuppressWarnings("Duplicates") + private ConsulPropertySource getConsulPropertySource(ConsulConfigProperties configProperties, String context) { + ConsulPropertySource source = new ConsulPropertySource(context, client, configProperties); + source.init(); + String[] names = source.getPropertyNames(); + assertThat("names was null", names, is(notNullValue())); + assertThat("names was wrong size", names.length, is(equalTo(2))); + return source; + } +}