Merge branch '1.3.x'

This commit is contained in:
Spencer Gibb
2018-02-07 14:45:53 -05:00
3 changed files with 67 additions and 3 deletions

View File

@@ -35,7 +35,6 @@ import lombok.Data;
public class ConsulConfigProperties {
private boolean enabled = true;
@NotEmpty
private String prefix = "config";
@NotEmpty

View File

@@ -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);
}

View File

@@ -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;
}
}