Add unit test

添加相关单元测试
现在没有配置 dataId 时会默认使用 {application}-{profile}.{fileExtension}
pull/2349/head
Freeman Lau 3 years ago
parent 695938280f
commit b96b762001

@ -146,7 +146,7 @@ public class NacosConfigDataLocationResolver
.setNamespace(Objects.toString(properties.getNamespace(),
DEFAULT_NAMESPACE))
.setGroup(groupFor(uris, properties))
.setDataId(dataIdFor(uris, properties))
.setDataId(dataIdFor(resolverContext.getBinder(), uris, properties))
.setSuffix(suffixFor(uris, properties))
.setRefreshEnabled(refreshEnabledFor(uris, properties)));
@ -159,13 +159,13 @@ public class NacosConfigDataLocationResolver
private void registerOrUpdateIndexes(String uris, NacosConfigProperties properties,
ConfigurableBootstrapContext bootstrapContext) {
String namespace = Objects.toString(properties.getNamespace(), DEFAULT_NAMESPACE);
Properties prop = new Properties();
prop.put(PropertyKeyConst.NAMESPACE, namespace);
prop.put(PropertyKeyConst.SERVER_ADDR, serverAddrFor(uris));
if (bootstrapContext.isRegistered(ConfigServiceIndexes.class)) {
ConfigServiceIndexes indexes = bootstrapContext
.get(ConfigServiceIndexes.class);
if (!indexes.getIndexes().containsKey(namespace)) {
Properties prop = new Properties();
prop.put(PropertyKeyConst.NAMESPACE, namespace);
prop.put(PropertyKeyConst.SERVER_ADDR, serverAddrFor(uris));
try {
indexes.getIndexes().put(namespace,
ConfigFactory.createConfigService(prop));
@ -177,9 +177,6 @@ public class NacosConfigDataLocationResolver
}
bootstrapContext.register(ConfigServiceIndexes.class, (context) -> {
try {
Properties prop = new Properties();
prop.put(PropertyKeyConst.NAMESPACE, namespace);
prop.put(PropertyKeyConst.SERVER_ADDR, serverAddrFor(uris));
ConfigService configService = ConfigFactory.createConfigService(prop);
Map<String, ConfigService> indexes = new ConcurrentHashMap<>(4);
@ -244,17 +241,36 @@ public class NacosConfigDataLocationResolver
return properties.getGroup();
}
private String dataIdFor(String uris, NacosConfigProperties properties) {
private String dataIdFor(Binder binder, String uris,
NacosConfigProperties properties) {
String[] part = split(uris);
if (part.length >= 2 && !part[1].isEmpty()) {
return part[1];
}
return properties.getName();
if (properties.getName() != null && !properties.getName().isEmpty()) {
return properties.getName();
}
// support {application}-{profile}.{suffix}
String application = binder.bind("spring.application.name", String.class)
.orElse("application");
String profile = binder.bind("spring.profiles.active", String.class).orElse("");
String suffix = suffixFor(uris, properties);
if (profile == null || profile.isEmpty()) {
return application + "." + suffix;
}
return application + "-" + profile + "." + suffix;
}
private String suffixFor(String uris, NacosConfigProperties properties) {
String dataId = dataIdFor(uris, properties);
if (dataId.contains(".")) {
String[] part = split(uris);
String dataId = null;
if (part.length >= 2 && !part[1].isEmpty()) {
dataId = part[1];
}
else if (properties.getName() != null && !properties.getName().isEmpty()) {
dataId = properties.getName();
}
if (dataId != null && dataId.contains(".")) {
return dataId.substring(dataId.lastIndexOf('.') + 1);
}
return properties.getFileExtension();

@ -0,0 +1,47 @@
package com.alibaba.cloud.nacos;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import static org.assertj.core.api.Assertions.assertThat;
/**
* NacosConfigAutoConfiguration Tester.
*
* @author freeman
*/
public class NacosConfigAutoConfigurationTest {
@Test
public void noImports_thenCreateProperties() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
NacosConfigAutoConfiguration.class);
assertThat(BeanFactoryUtils.beanNamesForTypeIncludingAncestors(context,
NacosConfigProperties.class).length).isEqualTo(1);
assertThat(context.getBean(NacosConfigProperties.class).getServerAddr())
.isEqualTo("localhost:8848");
context.close();
}
@Test
public void imports_thenNoCreateProperties() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
NacosConfigAutoConfiguration.class);
// mock import
context.registerBean(NacosConfigProperties.class, () -> {
NacosConfigProperties properties = new NacosConfigProperties();
properties.setServerAddr("localhost");
return properties;
});
assertThat(BeanFactoryUtils.beanNamesForTypeIncludingAncestors(context,
NacosConfigProperties.class).length).isEqualTo(1);
assertThat(context.getBean(NacosConfigProperties.class).getServerAddr())
.isEqualTo("localhost");
context.close();
}
}

@ -0,0 +1,160 @@
package com.alibaba.cloud.nacos.configdata;
import java.util.Collections;
import java.util.List;
import com.alibaba.cloud.nacos.NacosConfigProperties;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.ConfigurableBootstrapContext;
import org.springframework.boot.context.config.ConfigDataLocation;
import org.springframework.boot.context.config.ConfigDataLocationResolverContext;
import org.springframework.boot.context.config.Profiles;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.boot.logging.DeferredLog;
import org.springframework.mock.env.MockEnvironment;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.*;
/**
* NacosConfigDataLocationResolver Tester.
*
* @author freeman
*/
public class NacosConfigDataLocationResolverTest {
private NacosConfigDataLocationResolver resolver;
private ConfigDataLocationResolverContext context = mock(ConfigDataLocationResolverContext.class);
private MockEnvironment environment;
private Binder environmentBinder;
@BeforeEach
void setup() {
this.environment = new MockEnvironment();
this.environmentBinder = Binder.get(this.environment);
this.resolver = new NacosConfigDataLocationResolver(new DeferredLog());
when(context.getBinder()).thenReturn(environmentBinder);
}
@Test
void testIsResolvable_givenIncorrectPrefix_thenReturnFalse() {
assertThat(
this.resolver.isResolvable(this.context, ConfigDataLocation.of("test:")))
.isFalse();
}
@Test
void testIsResolvable_givenCorrectPrefix_thenReturnTure() {
assertThat(
this.resolver.isResolvable(this.context, ConfigDataLocation.of("nacos:")))
.isTrue();
assertThat(this.resolver.isResolvable(this.context,
ConfigDataLocation.of("optional:nacos:"))).isTrue();
}
@Test
void testIsResolvable_givenDisable_thenReturnFalse() {
this.environment.setProperty(NacosConfigProperties.PREFIX + ".enabled", "false");
assertThat(
this.resolver.isResolvable(this.context, ConfigDataLocation.of("nacos:")))
.isFalse();
}
@Test
void testResolveProfileSpecific_givenNothing_thenReturnDefaultProfile() {
NacosConfigDataResource resource = testResolveProfileSpecific();
assertThat(resource.getProfiles()).isEqualTo("default");
}
@Test
void whenNotSetProfile_thenDataIdIsShorter() {
environment.setProperty("spring.application.name", "nacos-test");
String locationUri = "nacos:localhost:8888";
NacosConfigDataResource resource = testUri(locationUri);
assertThat(resource.getConfig().getDataId()).isEqualTo("nacos-test.properties");
}
@Test
void whenCustomizeSuffix_thenOverrideDefault() {
environment.setProperty("spring.application.name", "nacos-test");
environment.setProperty("spring.cloud.nacos.config.file-extension", "yml");
String locationUri = "nacos:localhost:8888";
NacosConfigDataResource resource = testUri(locationUri);
assertThat(resource.getConfig().getDataId()).isEqualTo("nacos-test.yml");
}
@Test
void testUrisInLocationShouldOverridesProperty() {
environment.setProperty("spring.application.name", "nacos-test");
environment.setProperty("spring.profiles.active", "dev");
String locationUri = "nacos:localhost:8888/group01";
NacosConfigDataResource resource = testUri(locationUri);
assertThat(resource.getConfig().getGroup()).isEqualTo("group01");
assertThat(resource.getConfig().getSuffix()).isEqualTo("properties");
assertThat(resource.getConfig().getNamespace()).isEqualTo("");
assertThat(resource.getConfig().isRefreshEnabled()).isTrue();
assertThat(resource.getConfig().getDataId()).isEqualTo("nacos-test-dev.properties");
locationUri = "nacos:localhost:8888/group01/test.yml?refreshEnabled=false";
resource = testUri(locationUri);
assertThat(resource.getConfig().getGroup()).isEqualTo("group01");
assertThat(resource.getConfig().getDataId()).isEqualTo("test.yml");
assertThat(resource.getConfig().getSuffix()).isEqualTo("yml");
assertThat(resource.getConfig().isRefreshEnabled()).isFalse();
}
private NacosConfigDataResource testUri(String locationUri) {
when(context.getBootstrapContext())
.thenReturn(mock(ConfigurableBootstrapContext.class));
Profiles profiles = mock(Profiles.class);
List<NacosConfigDataResource> resources = this.resolver.resolveProfileSpecific(
context, ConfigDataLocation.of("nacos:" + locationUri), profiles);
assertThat(resources).hasSize(1);
return resources.get(0);
}
@Test
void whenNoneInBootstrapContext_thenCreateNewConfigClientProperties() {
ConfigurableBootstrapContext bootstrapContext = mock(
ConfigurableBootstrapContext.class);
when(context.getBootstrapContext()).thenReturn(bootstrapContext);
when(bootstrapContext.isRegistered(eq(NacosConfigProperties.class)))
.thenReturn(false);
when(bootstrapContext.get(eq(NacosConfigProperties.class)))
.thenReturn(new NacosConfigProperties());
List<NacosConfigDataResource> resources = this.resolver.resolveProfileSpecific(
context, ConfigDataLocation.of("nacos:localhost:8888/group/test.yml"),
mock(Profiles.class));
assertThat(resources).hasSize(1);
verify(bootstrapContext, times(0)).get(eq(NacosConfigProperties.class));
NacosConfigDataResource resource = resources.get(0);
assertThat(resource.getConfig().getGroup()).isEqualTo("group");
assertThat(resource.getConfig().getDataId()).isEqualTo("test.yml");
}
private NacosConfigDataResource testResolveProfileSpecific() {
return testResolveProfileSpecific("default");
}
private NacosConfigDataResource testResolveProfileSpecific(String activeProfile) {
when(context.getBootstrapContext())
.thenReturn(mock(ConfigurableBootstrapContext.class));
Profiles profiles = mock(Profiles.class);
if (activeProfile != null) {
when(profiles.getAccepted())
.thenReturn(Collections.singletonList(activeProfile));
}
List<NacosConfigDataResource> resources = this.resolver.resolveProfileSpecific(
context, ConfigDataLocation.of("nacos:localhost:8848"), profiles);
assertThat(resources).hasSize(1);
return resources.get(0);
}
}
Loading…
Cancel
Save